-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraktsearchmodel.cpp
134 lines (116 loc) · 3.12 KB
/
traktsearchmodel.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "traktsearchmodel.h"
#include "traktitem.h"
#include "traktmovie.h"
#include "traktshow.h"
#include "traktseason.h"
#include "traktepisode.h"
#include "traktperson.h"
TraktSearchModel::TraktSearchModel(QObject *parent) :
TraktPaginatedModel(buildRequest(), parent, false),
m_type(TypeMovie | TypeShow),
m_year(0)
{
}
TraktRequest *TraktSearchModel::buildRequest() const
{
TraktRequest *request = new TraktRequest();
request->setPath("/search");
request->addQueryItem("extended", "full,images");
return request;
}
QString TraktSearchModel::query() const
{
return m_query;
}
void TraktSearchModel::setQuery(const QString &query)
{
if (m_query != query) {
m_query = query;
emit queryChanged();
refresh();
}
}
TraktSearchModel::Types TraktSearchModel::type() const
{
return m_type;
}
void TraktSearchModel::setType(Types type)
{
if (type == 0) {
type = TypeMovie | TypeShow;
}
if (m_type != type) {
m_type = type;
emit typeChanged();
refresh();
}
}
int TraktSearchModel::year() const
{
return m_year;
}
void TraktSearchModel::setYear(int year)
{
if (m_year != year) {
m_year = year;
emit yearChanged();
refresh();
}
}
void TraktSearchModel::fetchMore(const QModelIndex &parent)
{
if (!m_request->query().queryItemValue("query").isEmpty()) {
TraktPaginatedModel<TraktItem*>::fetchMore(parent);
}
}
TraktItem *TraktSearchModel::convertItem(const QVariantMap &item)
{
QString type = item.value("type").toString();
if (type == "movie") {
return new TraktMovie(item.value("movie").toMap(), this);
} else if (type == "show") {
return new TraktShow(item.value("show").toMap(), this);
} else if (type == "episode") {
QVariantMap episodeMap = item.value("episode").toMap();
TraktShow *show = new TraktShow(item.value("show").toMap(), this);
TraktSeason *season = new TraktSeason(show);
season->setNumber(episodeMap.value("season").toInt());
return new TraktEpisode(episodeMap, season);
} else if (type == "person") {
return new TraktPerson(item.value("person").toMap(), this);
}
return 0;
}
TraktItem *TraktSearchModel::at(int i) const
{
return TraktPaginatedModel::at(i);
}
void TraktSearchModel::refresh()
{
if (!m_query.isEmpty()) {
m_request->replaceQueryItem("query", m_query);
if (m_year > 0) {
m_request->replaceQueryItem("year", QString::number(m_year));
} else {
m_request->removeQueryItem("year");
}
QStringList types;
if (type().testFlag(TypeMovie)) {
types.append("movie");
}
if (type().testFlag(TypeShow)) {
types.append("show");
}
if (type().testFlag(TypeEpisode)) {
types.append("episode");
}
if (type().testFlag(TypePerson)) {
types.append("person");
}
if (type().testFlag(TypeList)) {
types.append("list");
}
m_request->replaceQueryItem("type", types.join(","));
}
reset();
}