-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimelineview.cpp
executable file
·311 lines (284 loc) · 11.7 KB
/
timelineview.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
This file is part of Qween.
Copyright (C) 2009-2010 NOSE Takafumi <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, NOSE Takafumi
gives permission to link the code of its release of Qween with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables. You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL". If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so. If you do not wish to do
so, delete this exception statement from your version.
*/
#include "timelineview.h"
#include "qweensettings.h"
#include "twitter.h"
#include <QtXml>
#include <QKeyEvent>
#include <QRegExp>
TimelineView::TimelineView(QWidget *parent) :
QTreeView(parent),m_myId(0)
{
setSelectionBehavior(QAbstractItemView::SelectRows);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setRootIsDecorated(false);
setAlternatingRowColors(true);
int size = QweenSettings::globalSettings()->iconSize()*8 + 8;
if(size==8) size=0;
setIconSize(QSize(size,size));
}
bool TimelineView::isRelatedPost(int idx, int baseIdx){
Twitter::TwitterItem baseItem = model()->itemAt(baseIdx);
Twitter::TwitterItem item = model()->itemAt(idx);
if(item.id() == baseItem.inReplyToId() ||
baseItem.userId() == item.userId() ||
//item.replyToList().indexOf(QweenSettings::globalSettings()->userid())>=0 ||
baseItem.replyToList().indexOf(item.screenName())>=0 ||
item.replyToList().indexOf(baseItem.screenName())>=0 ){
return true;
}
return false;
}
void TimelineView::jumpToUnread(){
int i;
int next = -1;
for(i=model()->baseIndex()+1; i<model()->count(); i++){
if(!model()->itemAt(i).read()){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
}
void TimelineView::setReadAll(){
model()->setReadAll();
}
void TimelineView::keyPressEvent(QKeyEvent *event){
int i, next;
if(!event->modifiers().testFlag(Qt::ShiftModifier) &&
!event->modifiers().testFlag(Qt::ControlModifier) &&
!event->modifiers().testFlag(Qt::AltModifier)){
switch(event->key()){
case Qt::Key_M:
next = -1;
for(i=model()->baseIndex()-1; i>=0; i--){
if(isRelatedPost(i, model()->baseIndex())){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
case Qt::Key_N:
next = -1;
for(i=model()->baseIndex()+1; i<model()->count(); i++){
if(isRelatedPost(i, model()->baseIndex())){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
case Qt::Key_I:
emit favorite();
return;
case Qt::Key_H:
next = -1;
for(i=model()->baseIndex()+1; i<model()->count(); i++){
if(model()->itemAt(i).userId() == model()->itemAt(model()->baseIndex()).userId()){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
case Qt::Key_J:
QApplication::sendEvent(this, &QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier));
return;
case Qt::Key_K:
QApplication::sendEvent(this, &QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier));
return;
case Qt::Key_L:
next = -1;
for(i=model()->baseIndex()-1; i>=0; i--){
if(model()->itemAt(i).userId() == model()->itemAt(model()->baseIndex()).userId()){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
case Qt::Key_Space:
jumpToUnread();
return;
case Qt::Key_Return:
emit reply();
return;
default:
break;
}
}
if(event->modifiers().testFlag(Qt::ShiftModifier) &&
!event->modifiers().testFlag(Qt::ControlModifier) &&
!event->modifiers().testFlag(Qt::AltModifier)){
switch(event->key()){
case Qt::Key_M:
next = -1;
for(i=model()->baseIndex()-1; i>=0; i--){
if(model()->itemAt(i).favorited()){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
case Qt::Key_N:
next = -1;
for(i=model()->baseIndex()+1; i<model()->count(); i++){
if(model()->itemAt(i).favorited()){
next = i;
break;
}
}
if(next>=0) setCurrentIndex(model()->index(next,currentIndex().column()));
return;
}
}
QTreeView::keyPressEvent(event);
}
QDomElement TimelineView::saveToElement(QDomDocument& doc){
QDomElement elm = doc.createElement("tab");
elm.setAttribute("title", title());
elm.setAttribute("type", type());
QDomElement header = doc.createElement("header");
QString peHeaderState(this->header()->saveState().toPercentEncoding());
header.setAttribute("state", peHeaderState);
elm.appendChild(header);
QDomElement forward = doc.createElement("forward");
elm.appendChild(forward);
//TODO: 振り分け設定の書き出し
for(int i=0;i<forwardingRule.count();i++){
QDomElement ruleElm = doc.createElement("rule");
ForwardingRule rule = forwardingRule.at(i);
ruleElm.setAttribute("move", rule.moveFromRecent);
ruleElm.setAttribute("name", rule.name);
ruleElm.setAttribute("content", rule.content);
ruleElm.setAttribute("nameOrContent", rule.nameOrContent);
ruleElm.setAttribute("uri", rule.searchUri);
ruleElm.setAttribute("cs", rule.caseSensitive);
ruleElm.setAttribute("complex", rule.complex);
ruleElm.setAttribute("userx", rule.useRegex);
ruleElm.setAttribute("rt", rule.retweet);
ruleElm.setAttribute("exname", rule.exName);
ruleElm.setAttribute("excontent", rule.exContent);
ruleElm.setAttribute("exNameOrContent", rule.exNameOrContent);
ruleElm.setAttribute("exuri", rule.exSearchUri);
ruleElm.setAttribute("excs", rule.exCaseSensitive);
ruleElm.setAttribute("excomplex", rule.exComplex);
ruleElm.setAttribute("exuserx", rule.exUseRegex);
ruleElm.setAttribute("exrt", rule.exRetweet);
forward.appendChild(ruleElm);
}
return elm;
}
void TimelineView::restoreFromElement(const QDomElement& element){
QDomElement header = element.firstChildElement("header");
QString headerStateStr = header.attribute("state", "");
if(!headerStateStr.isEmpty()){
QByteArray arr;
this->header()->restoreState(arr.fromPercentEncoding(headerStateStr.toAscii()));
}
//TODO: 振り分け設定の読み込み
QDomElement forward = element.firstChildElement("forward");
if(forward.isNull()) return;
QDomElement ruleElm = forward.firstChildElement("rule");
while (!ruleElm.isNull()) {
ForwardingRule rule;
rule.moveFromRecent = (ruleElm.attribute("move", "0") == "1");
rule.name = ruleElm.attribute("name", "");
rule.content = ruleElm.attribute("content", "");
rule.nameOrContent = ruleElm.attribute("nameOrContent", "");
rule.searchUri = (ruleElm.attribute("uri", "0") == "1");
rule.caseSensitive = (ruleElm.attribute("cs", "0") == "1");
rule.complex = (ruleElm.attribute("complex", "0") == "1");
rule.useRegex = (ruleElm.attribute("userx", "0") == "1");
rule.retweet = (ruleElm.attribute("rt", "0") == "1");
rule.exName = ruleElm.attribute("exname", "");
rule.exContent = ruleElm.attribute("excontent", "");
rule.exNameOrContent = ruleElm.attribute("exNameOrContent", "");
rule.exSearchUri = (ruleElm.attribute("exuri", "0") == "1");
rule.exCaseSensitive = (ruleElm.attribute("excs", "0") == "1");
rule.exComplex = (ruleElm.attribute("excomplex", "0") == "1");
rule.exUseRegex = (ruleElm.attribute("exuserx", "0") == "1");
rule.exRetweet = (ruleElm.attribute("exrt", "0") == "1");
forwardingRule.append(rule);
ruleElm = ruleElm.nextSiblingElement("rule");
}
}
bool TimelineView::match(Twitter::TwitterItem &item, bool *copy){
for(int i=0;i<forwardingRule.count();i++){
if(match(item, forwardingRule.at(i))){
*copy = !forwardingRule.at(i).moveFromRecent;
return true;
}
}
*copy = false;
return false;
}
bool TimelineView::match(Twitter::TwitterItem &item, const ForwardingRule &rule){
QRegExp::PatternSyntax pat;
Qt::CaseSensitivity cs;
if(rule.useRegex) pat = QRegExp::RegExp;
else pat = QRegExp::FixedString;
if(rule.caseSensitive) cs = Qt::CaseSensitive;
else cs = Qt::CaseInsensitive;
QRegExp namerx(rule.name, cs, pat);
QRegExp contrx(rule.content, cs, pat);
QRegExp bothrx(rule.nameOrContent, cs, pat);
bool matched = false;
if(rule.complex){
if((!rule.name.isEmpty() && namerx.indexIn(item.screenName(),0) != -1) &&
(!rule.content.isEmpty() && contrx.indexIn(item.status(),0) != -1)){
matched = true;
}
}else{
if(!rule.nameOrContent.isEmpty() &&
(bothrx.indexIn(item.screenName(),0) != -1 ||
bothrx.indexIn(item.status(),0) != -1)){
matched = true;
}
}
if(!matched) return false;
if(rule.exUseRegex) pat = QRegExp::RegExp;
else pat = QRegExp::FixedString;
if(rule.exCaseSensitive) cs = Qt::CaseSensitive;
else cs = Qt::CaseInsensitive;
QRegExp exnamerx(rule.exName, cs, pat);
QRegExp excontrx(rule.exContent, cs, pat);
QRegExp exbothrx(rule.exNameOrContent, cs, pat);
bool exmatched = false;
if(rule.exComplex){
if((!rule.exName.isEmpty() && exnamerx.indexIn(item.screenName(),0) != -1) &&
(!rule.exContent.isEmpty() && excontrx.indexIn(item.status(),0) != -1)){
exmatched = true;
}
}else{
if(!rule.exNameOrContent.isEmpty() &&
(exbothrx.indexIn(item.screenName(),0) != -1 ||
exbothrx.indexIn(item.status(),0) != -1)){
exmatched = true;
}
}
return !exmatched;
}