在本遍文章中,我们来解说怎么对我们的Ubuntu Scope进行设置。对Scope而言,有些时候我们希望可以使用设置来改变我们的显示。或对我们的搜索进行又一次定义。关于很多其它Scope的开发,请參阅站点:http://developer.ubuntu.com/scopes/
1)首先创建一个最主要的Scope

这样我们就创建了我们的一个最主要的scope了。

2)增加代码来完毕设置功能
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini
注意这个文件名称和Scope的设置文件
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope.ini
仅仅有细小的区别。仅仅是在它的后面加上“-settings"就可以。记住千万不要改变这个规则。注意这个文件名称和项目的名称的不同而不同。
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 = 搜寻条数
它被定义为“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,并能够看到例如以下的图片:


watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="350" height="350" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="500" height="200" alt="">