cellar/src/bottles/bottles.cpp

105 lines
2.7 KiB
C++
Raw Normal View History

2017-03-13 19:03:07 -07:00
#include <cstdlib>
#include <fstream>
#include <map>
2017-03-13 19:03:07 -07:00
#include <string>
#include <sstream>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "json.hpp"
#include "bottles.hpp"
#include "internal/bottles.hpp"
2017-03-23 12:06:29 -07:00
#include "dll.hpp"
2017-03-13 19:03:07 -07:00
#include "fs.hpp"
#include "output.hpp"
2017-03-13 19:03:07 -07:00
using namespace std;
using namespace cellar;
using namespace cellar::bottles;
2017-03-13 19:03:07 -07:00
2017-03-23 12:06:29 -07:00
using CommandFunction = cellar::commands::CommandFunction;
2017-03-13 19:03:07 -07:00
using json = nlohmann::json;
Bottle::Bottle() {
// define a null bottle
// strings handle themselves
config = json({});
type = bottle_anonymous;
}
Bottle::Bottle(string patharg) {
config = json({});
path = patharg;
boost::filesystem::file_status path_status = boost::filesystem::symlink_status(path);
bool symlink = boost::filesystem::is_symlink(path_status);
if (symlink) {
boost::filesystem::path realpath = boost::filesystem::canonical(path);
canonical_path = realpath.string();
type = bottle_symlink;
} else {
canonical_path = path;
try {
if (load_config()) {
type = bottle_labelled;
} else {
type = bottle_anonymous;
}
}
catch (const exception &exc) {
type = bottle_error;
}
}
}
2017-03-23 12:06:29 -07:00
DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
map<string, Bottle> result;
2017-03-13 19:03:07 -07:00
string homepath = getenv("HOME");
vector<string> homedir = fs::listdir(homepath);
2017-03-13 19:03:07 -07:00
for (string item : homedir) {
if (item.substr(0,5) == ".wine") {
2017-03-22 18:12:12 -07:00
string fullitem = homepath + "/" + item;
Bottle output(fullitem);
2017-03-13 19:03:07 -07:00
result[item] = output;
}
2017-03-13 19:03:07 -07:00
}
return result;
}
2017-03-23 00:02:38 -07:00
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
2017-03-23 00:02:38 -07:00
map<string, Bottle> bottles = get_bottles();
stringstream outstr;
2017-03-23 00:02:38 -07:00
for (auto item : bottles) {
if (item.first == ".wine" || item.first == ".wine.template") {
// .wine is considered to be "active", and .wine.template is used as a template
// and therefore treated specially
continue;
}
2017-03-23 00:02:38 -07:00
Bottle bottle = item.second;
outstr << item.first << " - ";
2017-03-23 00:02:38 -07:00
switch (bottle.type) {
case bottle_anonymous:
outstr << "anonymous wine bottle";
2017-03-23 00:02:38 -07:00
break;
case bottle_symlink:
outstr << "symlink to " << bottle.canonical_path;
2017-03-23 00:02:38 -07:00
break;
case bottle_labelled:
outstr << bottle.config["name"];
2017-03-23 00:02:38 -07:00
break;
default:
outstr << "broken or unsupported wine bottle";
2017-03-23 00:02:38 -07:00
}
output::statement(outstr.str());
outstr.str("");
2017-03-23 00:02:38 -07:00
}
}