In this article, we'll explain how to set up our Ubuntu scope. For scope, there are times when we want to be able to use settings to change our display, or to redefine our search. For more scope development, please refer to the website: http://developer.ubuntu.com/scopes/
1) First create a basic scopeWe first open the SDK and select the "Unity Scope" template. We select an item with the name "settingscope":
Next, we select "Empty scope". This allows us to create one of our most basic scopes.
2) Add code to complete the Setup function
First, we open the "Data" folder in the project and create a file name like this:
Com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini
Note the file name and scope of the settings
Com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope.ini
There are only small differences. Just add "-settings" to the back of it. Remember never to change this rule.Note that this filename differs from the name of the item。
In order to be able to set up and install this file, we also need to add the following content to the "CMakeLists.txt" file in the "Data" directory:
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}")
This allows our setup files to be installed in the target. Below, we can configure our settings file. To open our settings file:
[Location]
Type=string
DefaultValue=London
DisplayName= location
[Distanceunit]
Type=list
DefaultValue=1
DisplayName=DistanceUnit
DisplayName [de] =Entfernungseinheit
Displayvalues=kilometers; Miles
displayvalues [de] =kilometer; Meilen
[Age]
Type= number
DefaultValue=
DisplayName= age
[Enabled]
Type=boolean
DefaultValue=true
DisplayName=Enabled
#Setting without a default Value
[Color]
Type=string
DisplayName=Color
[Limit]
Type= number
DefaultValue=
DisplayName = number of search bars
Here, we define the names of some of the settings, such as "location". It is defined as "string", and it also has a default value of "London". The prompt displayed is "location", and of course we can change it to "position" (for Chinese).
In order to be able to access us in the app, we can modify our code as follows:
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 ("Defa Ult "); } 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 bee N cancelled. So don ' t continue; Return }}} catch (Domain_error &e) {//Handle exceptions being thrown by the client API Cerr <& Lt 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;}
Here "Initscope" is called in "Run". In Initscope, we read the value of the setting through "settings ()". For the convenience of display, we also have a simple display of the values read in "Run":
Res.set_title (result.title); Res.set_title (m_location); res["subtitle"] = std::to_string (m_limit);
We re-run our scope and can see the following images:
We can also see the changes in the settings in our Application Output window:
The source code of the whole project can be downloaded at the following address:
BZR BranchLp:~liu-xiao-guo/debiantrial/settingscope
For more reading, refer to Http://developer.ubuntu.com/api/devel/ubuntu-14.10/cplusplus/unity-scopes/index.html#scopesettings
How to define set variables in Ubuntu scope and read