1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
//
// Copyleft RIME Developers
// License: GPLv3
//
// 2012-02-18 GONG Chen <chen.sst@gmail.com>
//
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <rime/config.h>
#include <rime/deployer.h>
#include <rime/lever/switcher_settings.h>
namespace fs = boost::filesystem;
namespace rime {
SwitcherSettings::SwitcherSettings(Deployer* deployer)
: CustomSettings(deployer, "default", "Rime::SwitcherSettings") {
}
bool SwitcherSettings::Load() {
if (!CustomSettings::Load())
return false;
fs::path user_data_path(deployer_->user_data_dir);
fs::path shared_data_path(deployer_->shared_data_dir);
available_.clear();
selection_.clear();
hotkeys_.clear();
GetAvailableSchemasFromDirectory(shared_data_path);
GetAvailableSchemasFromDirectory(user_data_path);
GetSelectedSchemasFromConfig();
GetHotkeysFromConfig();
return true;
}
bool SwitcherSettings::Select(const Selection& selection) {
selection_ = selection;
ConfigListPtr schema_list = make_shared<ConfigList>();
BOOST_FOREACH(const std::string& schema_id, selection_) {
ConfigMapPtr item = make_shared<ConfigMap>();
item->Set("schema", boost::make_shared<ConfigValue>(schema_id));
schema_list->Append(item);
}
return Customize("schema_list", schema_list);
}
bool SwitcherSettings::SetHotkeys(const std::string& hotkeys) {
// TODO: not implemented; validation required
return false;
}
void SwitcherSettings::GetAvailableSchemasFromDirectory(const fs::path& dir) {
if (!fs::exists(dir) || !fs::is_directory(dir)) {
LOG(INFO) << "directory '" << dir.string() << "' does not exist.";
return;
}
fs::directory_iterator it(dir);
fs::directory_iterator end;
for (; it != end; ++it) {
std::string file_path(it->path().string());
if (boost::ends_with(file_path, ".schema.yaml")) {
Config config;
if (config.LoadFromFile(file_path)) {
SchemaInfo info;
// required properties
if (!config.GetString("schema/schema_id", &info.schema_id)) continue;
if (!config.GetString("schema/name", &info.name)) continue;
// check for duplicates
bool duplicated = false;
BOOST_FOREACH(const SchemaInfo& other, available_) {
if (other.schema_id == info.schema_id) {
duplicated = true;
break;
}
}
if (duplicated) continue;
// details
config.GetString("schema/version", &info.version);
ConfigListPtr authors = config.GetList("schema/author");
if (authors) {
for (size_t i = 0; i < authors->size(); ++i) {
ConfigValuePtr author = authors->GetValueAt(i);
if (author && !author->str().empty()) {
if (!info.author.empty())
info.author += "\n";
info.author += author->str();
}
}
}
config.GetString("schema/description", &info.description);
info.file_path = file_path;
available_.push_back(info);
}
}
}
}
void SwitcherSettings::GetSelectedSchemasFromConfig() {
ConfigListPtr schema_list = config_.GetList("schema_list");
if (!schema_list) {
LOG(WARNING) << "schema list not defined.";
return;
}
ConfigList::Iterator it = schema_list->begin();
for (; it != schema_list->end(); ++it) {
ConfigMapPtr item = As<ConfigMap>(*it);
if (!item) continue;
ConfigValuePtr schema_property = item->GetValue("schema");
if (!schema_property) continue;
const std::string &schema_id(schema_property->str());
selection_.push_back(schema_id);
}
}
void SwitcherSettings::GetHotkeysFromConfig() {
ConfigListPtr hotkeys = config_.GetList("switcher/hotkeys");
if (!hotkeys) {
LOG(WARNING) << "hotkeys not defined.";
return;
}
ConfigList::Iterator it = hotkeys->begin();
for (; it != hotkeys->end(); ++it) {
ConfigValuePtr item = As<ConfigValue>(*it);
if (!item) continue;
const std::string &hotkey(item->str());
if (hotkey.empty()) continue;
if (!hotkeys_.empty())
hotkeys_ += ", ";
hotkeys_ += hotkey;
}
}
} // namespace rime
|