1
#include <boost/algorithm/string/trim.hpp>
3
#include <scope/localization.h>
4
#include <scope/query.h>
6
#include <unity/scopes/Annotation.h>
7
#include <unity/scopes/CategorisedResult.h>
8
#include <unity/scopes/CategoryRenderer.h>
9
#include <unity/scopes/QueryBase.h>
10
#include <unity/scopes/SearchReply.h>
15
namespace sc = unity::scopes;
16
namespace alg = boost::algorithm;
20
using namespace scope;
21
using namespace unity::scopes;
23
// Create a JSON string to be used tro create a category renderer - uses grid layout
24
const std::string CR_GRID = R"(
28
"category-layout" : "vertical-journal",
29
"card-layout": "horizontal",
35
"subtitle":"subtitle",
44
// Create a JSON string to be used tro create a category renderer - uses carousel layout
45
const std::string CR_CAROUSEL = R"(
49
"category-layout" : "carousel",
63
Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata,
65
sc::SearchQueryBase(query, metadata), client_(config) {
68
void Query::cancelled() {
73
void Query::run(sc::SearchReplyProxy const& reply) {
75
// Start by getting information about the query
76
const sc::CannedQuery &query(sc::SearchQueryBase::query());
78
QString queryString = QString::fromStdString(query.query_string());
80
// Trim the query string of whitespace
81
string query_string = alg::trim_copy(query.query_string());
83
Client::DataList datalist;
84
if (query_string.empty()) {
85
// If the string is empty, get the current weather for London
86
datalist = client_.getData("北京");
87
queryString = QString("北京");
89
// otherwise, get the current weather for the search string
90
datalist = client_.getData(query_string);
93
CategoryRenderer rdrGrid(CR_GRID);
94
CategoryRenderer rdrCarousel(CR_CAROUSEL);
96
QString title = queryString;
98
// Register two categories
99
auto carousel = reply->register_category("dianpingcarousel", title.toStdString(), "", rdrCarousel);
100
auto grid = reply->register_category("dianpinggrid", "", "", rdrGrid);
104
// For each of the entry in the datalist
105
for (const Client::Data &data : datalist) {
108
const std::shared_ptr<const Category> * category;
115
category = &carousel;
118
//create our categorized result using our pointer, which is either to out
119
//grid or our carousel Category
120
CategorisedResult catres((*category));
122
// We must have a URI
123
catres.set_uri(data.business_url);
124
catres.set_dnd_uri(data.business_url);
125
catres.set_title(data.name);
127
catres["subtitle"] = data.address;
128
catres["summary"] = data.summary;
129
catres["fulldesc"] = data.summary;
130
catres["art2"] = data.s_photo_url;
132
catres.set_art(data.photo_url);
134
catres["address"] = Variant(data.address);
135
catres["telephone"] = Variant(data.telephone);
138
if (!reply->push(catres)) {
139
// If we fail to push, it means the query has been cancelled.
140
// So don't continue;
144
} catch (domain_error &e) {
145
// Handle exceptions being thrown by the client API
146
cerr << e.what() << endl;
147
reply->error(current_exception());