怎样在Ubuntu Scope中定义设置变量并读取
在本遍文章中,我们来解说怎么对我们的Ubuntu Scope进行设置。对Scope而言,有些时候我们希望可以使用设置来改变我们的显示。或对我们的搜索进行又一次定义。关于很多其它Scope的开发,请參阅站点:http://developer.ubuntu.com/scopes/
1)首先创建一个最主要的Scope
我们首先打开SDK。并选择“Unity Scope”模版。我们选择一个项目的名称为“settingscope”:
接下来,我们选择“Empty scope”。
这样我们就创建了我们的一个最主要的scope了。
2)增加代码来完毕设置功能
首先,我们打开项目中的“data”目录。并创建一个例如以下的文件名称:
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini
注意这个文件名称和Scope的设置文件
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope.ini
仅仅有细小的区别。仅仅是在它的后面加上“-settings"就可以。记住千万不要改变这个规则。注意这个文件名称和项目的名称的不同而不同。
为了可以对这个文件进行设置和安装,我们也同一时候须要对“data”文件夹下的“CMakeLists.txt”文件增加例如以下的内容:
configure_file(
"com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini"
"${CMAKE_BINARY_DIR}/src/com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini"
)
INSTALL(
FILES "${CMAKE_BINARY_DIR}/src/com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini"
DESTINATION "${SCOPE_INSTALL_DIR}"
)
这样我们的设置文件就能够安装到目标中了。
以下。我们能够对我们的设置文件进行配置。
打开我们的设置文件:
[location]
type = string
defaultValue = London
displayName = Location
[distanceUnit]
type = list
defaultValue = 1
displayName = Distance Unit
displayName[de] = Entfernungseinheit
displayValues = Kilometers;Miles
displayValues[de] = Kilometer;Meilen
[age]
type = number
defaultValue = 23
displayName = Age
[enabled]
type = boolean
defaultValue = true
displayName = Enabled
# Setting without a default value
[color]
type = string
displayName = Color
[limit]
type = number
defaultValue = 20
displayName = 搜寻条数
在这里。我们定义了一些设置的名称。比方“location”。
它被定义为“string”,同一时候它另一个默认的值“London”。
显示的提示为“Location”,当然我们也能够把它改动为“位置”(对中文而言)。
为了可以在应用中訪问我们。我们可以改动我们的代码例如以下:
void Query::run(sc::SearchReplyProxy const& reply) {
// Read the settings
initScope();
try {
// Start by getting information about the query
const sc::CannedQuery &query(sc::SearchQueryBase::query());
// Trim the query string of whitespace
string query_string = alg::trim_copy(query.query_string());
Client::ResultList results;
if (query_string.empty()) {
// If the string is empty, pick a default
results = client_.search("default");
} else {
// otherwise, use the search string
results = client_.search(query_string);
}
// Register a category
auto cat = reply->register_category("results", "Results", "",
sc::CategoryRenderer(CATEGORY_TEMPLATE));
for (const auto &result : results) {
sc::CategorisedResult res(cat);
cerr << "it comes here: " << m_limit << endl;
// We must have a URI
res.set_uri(result.uri);
// res.set_title(result.title);
res.set_title( m_location );
res["subtitle"] = std::to_string(m_limit);
// Set the rest of the attributes, art, description, etc
res.set_art(result.art);
res["description"] = result.description;
// Push the result
if (!reply->push(res)) {
// If we fail to push, it means the query has been cancelled.
// So don't continue;
return;
}
}
} catch (domain_error &e) {
// Handle exceptions being thrown by the client API
cerr << e.what() << endl;
reply->error(current_exception());
}
}
void Query::initScope()
{
unity::scopes::VariantMap config = settings(); // The settings method is provided by the base class
if (config.empty())
cerr << "CONFIG EMPTY!" << endl;
m_location = config["location"].get_string(); // Prints "London" unless the user changed the value
cerr << "location: " << m_location << endl;
m_limit = config["limit"].get_double();
cerr << "limit: " << m_limit << endl;
}
这里“initScope”在“Run”中被调用。
在InitScope中,我们通过“settings()”来读取设置的值。为了显示的方便,我们在“Run”中,也对读取的值进行简单的显示:
// res.set_title(result.title);
res.set_title( m_location );
res["subtitle"] = std::to_string(m_limit);我们又一次执行我们的Scope,并能够看到例如以下的图片:
我们也能够在我们的Application Output窗体中看到设置的变化:
整个项目的源代码能够在例如以下的地址下载:
bzr branch lp:~liu-xiao-guo/debiantrial/settingscope
很多其它阅读能够參考 http://developer.ubuntu.com/api/devel/ubuntu-14.10/cplusplus/unity-scopes/index.html#scopesettings
浙公网安备 33010602011771号