-
Notifications
You must be signed in to change notification settings - Fork 25
/
telegrammessagesearchmodel.cpp
173 lines (144 loc) · 3.98 KB
/
telegrammessagesearchmodel.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define DEFINE_DIS \
QPointer<TelegramMessageSearchModel> dis = this;
#include "telegrammessagesearchmodel.h"
#include "telegramsharedpointer.h"
#include <telegram/objects/typeobjects.h>
#include <telegram.h>
class TelegramMessageSearchModelPrivate
{
public:
TelegramSharedPointer<InputPeerObject> peer;
QString keyword;
QDateTime minDate;
QDateTime maxDate;
int filter;
qint64 lastRequest;
};
TelegramMessageSearchModel::TelegramMessageSearchModel(QObject *parent) :
TelegramMessageListModel(parent)
{
p = new TelegramMessageSearchModelPrivate;
p->lastRequest = 0;
p->minDate = QDateTime::fromTime_t(0);
p->maxDate = p->minDate;
p->filter = MessagesFilterObject::TypeInputMessagesFilterEmpty;
}
void TelegramMessageSearchModel::setCurrentPeer(InputPeerObject *peer)
{
if(p->peer == peer)
return;
p->peer = peer;
refresh();
Q_EMIT currentPeerChanged();
Q_EMIT keyChanged();
Q_EMIT megagroupChanged();
Q_EMIT editableChanged();
}
InputPeerObject *TelegramMessageSearchModel::currentPeer() const
{
return p->peer;
}
void TelegramMessageSearchModel::setKeyword(const QString &keyword)
{
if(p->keyword == keyword)
return;
p->keyword = keyword;
refresh();
Q_EMIT keywordChanged();
}
QString TelegramMessageSearchModel::keyword() const
{
return p->keyword;
}
void TelegramMessageSearchModel::setMessageFilter(int filter)
{
if(p->filter == filter)
return;
p->filter = filter;
refresh();
Q_EMIT messageFilterChanged();
}
int TelegramMessageSearchModel::messageFilter() const
{
return p->filter;
}
void TelegramMessageSearchModel::setMinimumDate(const QDateTime &minDate)
{
if(p->minDate == minDate)
return;
p->minDate = minDate;
refresh();
Q_EMIT minimumDateChanged();
}
QDateTime TelegramMessageSearchModel::minimumDate() const
{
return p->minDate;
}
void TelegramMessageSearchModel::setMaximumDate(const QDateTime &maxDate)
{
if(p->maxDate == maxDate)
return;
p->maxDate = maxDate;
refresh();
Q_EMIT maximumDateChanged();
}
QDateTime TelegramMessageSearchModel::maximumDate() const
{
return p->maxDate;
}
QStringList TelegramMessageSearchModel::requiredProperties()
{
return QStringList() << FUNCTION_NAME(engine)
<< FUNCTION_NAME(currentPeer)
<< FUNCTION_NAME(keyword);
}
void TelegramMessageSearchModel::loadBack()
{
getFromServer(true);
}
void TelegramMessageSearchModel::refresh()
{
clean();
getFromServer(false);
}
void TelegramMessageSearchModel::getFromServer(bool more)
{
if((p->keyword.isEmpty() && (p->filter == MessagesFilterObject::TypeInputMessagesFilterEmpty || !p->peer))
|| !mEngine || !mEngine->telegram())
return;
setRefreshing(true);
DEFINE_DIS;
Telegram::Callback<MessagesMessages> callback = [this, dis, more](TG_MESSAGES_GET_MESSAGES_CALLBACK){
if(!dis || p->lastRequest != msgId) return;
setRefreshing(false);
if(!error.null) {
setError(error.errorText, error.errorCode);
return;
}
setHasBackMore(false);
processOnResult(result, more);
};
MessagesFilterObject filter;
filter.setClassType(p->filter);
Telegram *tg = mEngine->telegram();
if(p->peer)
{
int offset = more? count() : 0;
p->lastRequest = tg->messagesSearch(p->peer->core(), p->keyword, InputUser::null, filter.core(), p->minDate.toTime_t(),
p->maxDate.toTime_t(), offset, 0, limit(), callback);
}
else
{
int offset = 0;
if(more)
{
TQmlMessageObject *msg = get(count(), RoleMessageItem).value<TQmlMessageObject*>();
if(msg) offset = msg->id();
}
p->lastRequest = tg->messagesSearchGlobal(p->keyword, 0, InputPeer::null, offset, limit(), callback);
}
}
TelegramMessageSearchModel::~TelegramMessageSearchModel()
{
delete p;
}