~mzanetti/+junk/nichtlustig-scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <boost/algorithm/string/trim.hpp>

#include <scope/query.h>
#include "api/client.h"

#include <unity/scopes/Annotation.h>
#include <unity/scopes/CategorisedResult.h>
#include <unity/scopes/CategoryRenderer.h>
#include <unity/scopes/QueryBase.h>
#include <unity/scopes/SearchReply.h>

#include <iomanip>
#include <sstream>
#include <QDebug>

namespace sc = unity::scopes;
namespace alg = boost::algorithm;

using namespace std;
using namespace api;
using namespace scope;


const static string NEWS_RENDERER =
        R"(
{
        "schema-version": 1,
        "template": {
        "category-layout": "vertical-journal",
        "card-layout": "vertical",
        "card-size": "medium",
        "overlay": true
        },
        "components": {
        "title": "title",
        "subtitle": "date",
        "art" : "art"
        }
        }
        )";


Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata) :
    sc::SearchQueryBase(query, metadata) {
}

void Query::cancelled() {
    client_.cancel();
}

void Query::run(sc::SearchReplyProxy const& reply) {
    try {
        const sc::CannedQuery &query(sc::SearchQueryBase::query());
        // Trim the query string of whitespace
        //string query_string = alg::trim_copy(query.query_string());
        germanVersion(reply, query);
    } catch (domain_error &e) {
        // Handle exceptions being thrown by the client API
        cerr << "EXCEPTION" << endl;
        cerr << e.what() << endl;
        reply->error(current_exception());
    }
}

void Query::germanVersion(const unity::scopes::SearchReplyProxy &reply,
                          const unity::scopes::CannedQuery &query) {
//    sc::Department::SPtr newest = move(sc::Department::create("", query, "Neueste"));
//    sc::DepartmentList depList({ move(sc::Department::create("chronik", query, "Chronik")),
//                                 move(sc::Department::create("wirtschaft", query, "Wirtschaft")),
//                                 move(sc::Department::create("politik", query, "Politik")),
//                                 move(sc::Department::create("sport", query, "Sport")),
//                                 move(sc::Department::create("kultur", query, "Kultur")),
//                                 move(sc::Department::create("panorama", query, "Panorama")),
//                               });
//    newest->set_subdepartments(depList);
//    reply->register_departments(newest);

    Client::NewsList list;
    if (query.department_id().empty()) {
        list = client_.allNews("", "nichtrss.rss", "");
    } else {
        list = client_.allNews("", query.department_id(), "");
    }

    qDebug() << "query is" << QString::fromStdString(query.query_string());
    QString queryString = QString::fromStdString(query.query_string());
    sc::Category::SCPtr cat = reply->register_category("unknown", "", "", sc::CategoryRenderer(NEWS_RENDERER));
    QHash<QString, sc::Category::SCPtr> categories;
    categories.insert("unknown", cat);
    for (const Client::NewsItem &news : list) {
        if (query.query_string().length() > 0
                && !news.title.toLower().contains(queryString.toLower())
                && !news.description.toLower().contains(queryString.toLower())
                ) {
            continue;
        }
        if (!categories.contains(news.category)) {
            sc::Category::SCPtr newCat = reply->register_category(news.category.toStdString(), news.category.toStdString(), "", sc::CategoryRenderer(NEWS_RENDERER));
            categories.insert(news.category, newCat);
        }

        if (!pushResult(reply, categories.value(news.category), news))
            return;
    }
}

bool Query::pushResult(const sc::SearchReplyProxy &reply,
                       sc::Category::SCPtr category,
                       const Client::NewsItem &news) {
    sc::CategorisedResult res(category);
    res.set_title(news.title.toStdString());
    res.set_uri(news.link.toStdString());
    res.set_art(news.image.toStdString());
    res["date"] = news.date.toLocalTime().date().toString(Qt::DefaultLocaleShortDate).toStdString();
    //res["subtitle"] = news.description.toStdString();
    res["description"] = news.description.toStdString();
    return reply->push(res);
}