~liu-xiao-guo/debiantrial/dianpingvideo

« back to all changes in this revision

Viewing changes to src/scope/query.cpp

  • Committer: XiaoGuo, Liu
  • Date: 2014-12-02 03:15:12 UTC
  • Revision ID: xiaoguo.liu@canonical.com-20141202031512-phm316kxrdu1um0p
Initial release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <boost/algorithm/string/trim.hpp>
 
2
 
 
3
#include <scope/localization.h>
 
4
#include <scope/query.h>
 
5
 
 
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>
 
11
 
 
12
#include <iomanip>
 
13
#include <sstream>
 
14
 
 
15
namespace sc = unity::scopes;
 
16
namespace alg = boost::algorithm;
 
17
 
 
18
using namespace std;
 
19
using namespace api;
 
20
using namespace scope;
 
21
using namespace unity::scopes;
 
22
 
 
23
// Create a JSON string to be used tro create a category renderer - uses grid layout
 
24
const std::string CR_GRID = R"(
 
25
{
 
26
        "schema-version" : 1,
 
27
        "template" : {
 
28
            "category-layout" : "vertical-journal",
 
29
            "card-layout": "horizontal",
 
30
            "card-size": "small",
 
31
            "collapsed-rows": 0
 
32
        },
 
33
        "components" : {
 
34
            "title":"title",
 
35
            "subtitle":"subtitle",
 
36
            "summary":"summary",
 
37
            "art":{
 
38
                "field": "art2",
 
39
                "aspect-ratio": 1
 
40
            }
 
41
        }
 
42
})";
 
43
 
 
44
// Create a JSON string to be used tro create a category renderer - uses carousel layout
 
45
const std::string CR_CAROUSEL = R"(
 
46
{
 
47
     "schema-version" : 1,
 
48
     "template" : {
 
49
        "category-layout" : "carousel",
 
50
        "card-size": "small",
 
51
        "overlay" : true
 
52
      },
 
53
      "components" : {
 
54
        "title":"title",
 
55
        "art": {
 
56
        "field": "art",
 
57
        "aspect-ratio": 1.6,
 
58
        "fill-mode": "fit"
 
59
        }
 
60
      }
 
61
})";
 
62
 
 
63
Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata,
 
64
             Config::Ptr config) :
 
65
    sc::SearchQueryBase(query, metadata), client_(config) {
 
66
}
 
67
 
 
68
void Query::cancelled() {
 
69
    client_.cancel();
 
70
}
 
71
 
 
72
 
 
73
void Query::run(sc::SearchReplyProxy const& reply) {
 
74
    try {
 
75
        // Start by getting information about the query
 
76
        const sc::CannedQuery &query(sc::SearchQueryBase::query());
 
77
 
 
78
        QString queryString = QString::fromStdString(query.query_string());
 
79
 
 
80
        // Trim the query string of whitespace
 
81
        string query_string = alg::trim_copy(query.query_string());
 
82
 
 
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("北京");
 
88
        } else {
 
89
            // otherwise, get the current weather for the search string
 
90
            datalist = client_.getData(query_string);
 
91
        }
 
92
 
 
93
        CategoryRenderer rdrGrid(CR_GRID);
 
94
        CategoryRenderer rdrCarousel(CR_CAROUSEL);
 
95
 
 
96
        QString title = queryString;
 
97
 
 
98
        // Register two categories
 
99
        auto carousel = reply->register_category("dianpingcarousel", title.toStdString(), "", rdrCarousel);
 
100
        auto grid = reply->register_category("dianpinggrid", "", "", rdrGrid);
 
101
 
 
102
        bool isgrid = false;
 
103
 
 
104
        // For each of the entry in the datalist
 
105
        for (const Client::Data &data : datalist) {
 
106
 
 
107
            // for each result
 
108
            const std::shared_ptr<const Category> * category;
 
109
 
 
110
            if ( isgrid ) {
 
111
                category = &grid;
 
112
                isgrid = false;
 
113
            } else {
 
114
                isgrid = true;
 
115
                category = &carousel;
 
116
            }
 
117
 
 
118
            //create our categorized result using our pointer, which is either to out
 
119
            //grid or our carousel Category
 
120
            CategorisedResult catres((*category));
 
121
 
 
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);
 
126
 
 
127
            catres["subtitle"] = data.address;
 
128
            catres["summary"] = data.summary;
 
129
            catres["fulldesc"] = data.summary;
 
130
            catres["art2"] = data.s_photo_url;
 
131
 
 
132
            catres.set_art(data.photo_url);
 
133
 
 
134
            catres["address"] = Variant(data.address);
 
135
            catres["telephone"] = Variant(data.telephone);
 
136
 
 
137
            // Push the result
 
138
            if (!reply->push(catres)) {
 
139
                // If we fail to push, it means the query has been cancelled.
 
140
                // So don't continue;
 
141
                return;
 
142
            }
 
143
        }
 
144
    } catch (domain_error &e) {
 
145
        // Handle exceptions being thrown by the client API
 
146
        cerr << e.what() << endl;
 
147
        reply->error(current_exception());
 
148
    }
 
149
}
 
150