在本遍文章中,我们来讲解怎么对我们的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