-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqweentabctrl.cpp
executable file
·392 lines (354 loc) · 12.7 KB
/
qweentabctrl.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
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 <QTabWidget>
#include <QListWidget>
#include <QTableView>
#include <QHeaderView>
#include "qweentabctrl.h"
#include "qweenapplication.h"
#include "qweensettings.h"
#include "tabinfo.h"
#include "timelinemodel.h"
#include "timelineview.h"
#include "timelineitemdelegate.h"
//XXX: だんだん汚くなってきた…
QweenTabCtrl::QweenTabCtrl(QWidget *parent) :
QTabWidget(parent),m_homeView(NULL),m_replyView(NULL),
m_dmView(NULL),m_favView(NULL),settings(QweenSettings::globalSettings())
{
setMovable(true);
}
void QweenTabCtrl::saveState(QIODevice* device)
{
const int IndentSize = 2;
QTextStream out(device);
QDomDocument doc;
QDomElement root = doc.createElement("tabs");
doc.appendChild(root);
for(int i=0;i<this->count();i++){
TimelineView *view = (TimelineView*)(this->widget(i));
QDomElement elm = view->saveToElement(doc);
root.appendChild(elm);
}
doc.save(out, IndentSize);
}
void QweenTabCtrl::restoreState(QIODevice* device){
if(!device){
fixLackingTabs();
return;
}
QDomDocument doc;
QString errorStr;
int errorLine, errorColumn;
if (!doc.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
qDebug() << tr("Parse error at line %1, column %2: %3").arg(errorLine).arg(errorColumn).arg(errorStr);
fixLackingTabs();
return;
}
QDomElement root = doc.documentElement();
if (root.tagName() != "tabs") {
qDebug() << tr("invalid root element");
fixLackingTabs();
return;
}
QDomElement tabelm = root.firstChildElement("tab");
while (!tabelm.isNull()) {
//XXX: 振り分け設定とかのデータをViewが持ってるのが気持ち悪い
QString type = tabelm.attribute("type", "userdefined");
QString title = tabelm.attribute("title", "Untitled");
TimelineView* view = new TimelineView(this);
if(type == "home"){
if(!m_homeView) m_homeView = view;
else type = "userdefined";
}else if(type == "reply"){
if(!m_replyView) m_replyView = view;
else type = "userdefined";
}else if(type == "dm"){
if(!m_dmView) m_dmView = view;
else type = "userdefined";
}else if(type == "fav"){
if(!m_favView) m_favView = view;
else type = "userdefined";
}
view->setType(type);
view->setTitle(title);
view->restoreFromElement(tabelm);
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
makeConnection(view);
this->addTab(view, QIcon(), view->title());
tabelm = tabelm.nextSiblingElement("tab");
}
fixLackingTabs();
}
void QweenTabCtrl::fixLackingTabs(){
TimelineView* view;
//XXX: コピペ撲滅
if(!m_homeView){
view = new TimelineView(this);
view->setType("home");
view->setTitle("Home");
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
view->header()->resizeSection(1, 400);
makeConnection(view);
this->addTab(view,QIcon(),view->title());
m_homeView = view;
}
if(!m_replyView){
view = new TimelineView(this);
view->setType("reply");
view->setTitle("Reply");
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
view->header()->resizeSection(1, 400);
makeConnection(view);
this->addTab(view,QIcon(),view->title());
m_replyView = view;
}
if(!m_dmView){
view = new TimelineView(this);
view->setType("dm");
view->setTitle("DM");
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
view->header()->resizeSection(1, 400);
makeConnection(view);
this->addTab(view,QIcon(),view->title());
m_dmView = view;
}
if(!m_favView){
view = new TimelineView(this);
view->setType("fav");
view->setTitle("Favorites");
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
view->header()->resizeSection(1, 400);
makeConnection(view);
this->addTab(view,QIcon(),view->title());
m_favView = view;
}
}
void QweenTabCtrl::setRead(bool read){
TimelineView *v = currentTimelineView();
if(!v) return;
QModelIndex idx;
QModelIndexList indexes = v->selectionModel()->selectedIndexes();
foreach(idx, indexes){
v->model()->setRead(idx.row(), read);
}
}
TimelineView* QweenTabCtrl::findMatchingView(Twitter::TwitterItem &item, bool *copy){
TimelineView* v;
int i;
for(i=0;i<count();i++){
v = timelineView(i);
if(v->type() == "userdefined"){
if(v->match(item, copy)) return v;
}
}
*copy = false;
return NULL;
}
void QweenTabCtrl::addItem(Twitter::TwitterItem item){
if(settings->setReadMyPost() &&
item.screenName() == settings->userid()) item.setRead(true);
TimelineView *view;
bool copy;
switch(item.origin()){
case HOME_TIMELINE:
view = findMatchingView(item, ©);
if(view){
view->model()->appendItem(item);
if(copy && m_homeView) m_homeView->model()->appendItem(item);
}
else if(m_homeView) {
m_homeView->model()->appendItem(item);
}
break;
case MENTIONS:
if(m_replyView) m_replyView->model()->appendItem(item);
break;
case DIRECT_MESSAGES:
if(m_dmView) m_dmView->model()->appendItem(item);
break;
case SENT_DIRECT_MESSAGES:
if(m_dmView) m_dmView->model()->appendItem(item);
break;
case FAVORITES:
if(m_favView) m_favView->model()->appendItem(item);
break;
case UPDATE:
if(m_homeView) m_homeView->model()->appendItem(item);
break;
default:
break;
}
}
Twitter::TwitterItem QweenTabCtrl::currentItem(){
return m_curItem;
}
TimelineView *QweenTabCtrl::currentTimelineView(){
return (TimelineView*)this->currentWidget();
}
TimelineView *QweenTabCtrl::timelineView(int index){
return (TimelineView*)this->widget(index);
}
TimelineView *QweenTabCtrl::addTimelineView(const QString &title){
return insertTimelineView(count(), title);
}
TimelineView *QweenTabCtrl::insertTimelineView(int index, const QString& title){
TimelineView* view = new TimelineView(this);
view->setType("userdefined");
view->setTitle(title);
view->setItemDelegate(new TimelineItemDelegate(QweenApplication::iconManager()));
view->setModel(new TimelineModel(QweenApplication::iconManager(),this));
view->header()->resizeSection(1, 400);
makeConnection(view);
this->insertTab(index, view, QIcon(), view->title());
return view;
}
void QweenTabCtrl::removeTimelineView(int index){
TimelineView *view = timelineView(index);
if(view == m_homeView || view == m_dmView || view == m_favView || view == m_replyView) return;
delete view;
}
void QweenTabCtrl::favorited(quint64 id, bool faved){
//case CREATE_FAVORITE:
for(int i=0;i<this->count();i++){
this->timelineView(i)->favorited(id,faved);
}
//break;
}
void QweenTabCtrl::OnUnreadCountChanged(int count){
TimelineView* view = qobject_cast<TimelineView*>(sender());
int idx=indexOf(view);
if(count==0 || !settings->manageUnread()){
setTabText(idx, view->title());
setTabIcon(idx, QIcon());
}else{
setTabText(idx, QString("%0(%1)").arg(view->title()).arg(count));
if(settings->showUnreadIconInTab()){
setTabIcon(idx, QIcon(":/res/unread.png"));
}else{
setTabIcon(idx, QIcon());
}
}
}
void QweenTabCtrl::moveTimelineView(int before, int after){
TimelineView *view = timelineView(before);
this->removeTab(before);
if(before > after){
this->insertTab(after - 1, view, QIcon(), view->title());
}else{
this->insertTab(after, view, QIcon(), view->title());
}
}
void QweenTabCtrl::setMyId(quint64 myid){
//TODO: 各ModelにMyIDをセット
m_myID = myid;
for(int i=0;i<this->count();i++){
((TimelineView*)(this->widget(i)))->setMyId(myid);
}
}
quint64 QweenTabCtrl::getNewestHomeId() const {
if(!m_homeView) return 0;
return m_homeView->model()->newestId();
}
quint64 QweenTabCtrl::getNewestDMId() const {
if(!m_dmView) return 0;
return m_dmView->model()->newestId();
}
quint64 QweenTabCtrl::getNewestReplyId() const{
if(!m_replyView) return 0;
return m_replyView->model()->newestId();
}
quint64 QweenTabCtrl::getNewestFavId() const{
if(!m_favView) return 0;
return m_favView->model()->newestId();
}
void QweenTabCtrl::OnItemSelected(const Twitter::TwitterItem& item){
m_curItem = item;
emit itemSelected(item);
}
void QweenTabCtrl::setManageUnread(bool val){
if(m_manageUnread != val){
m_manageUnread = val;
if(m_manageUnread){
for(int i=0;i<count();i++){
TimelineView* view = timelineView(i);
int count = view->model()->unreadCount();
if(count==0){
setTabText(i, view->title());
setTabIcon(i, QIcon());
}else{
setTabText(i, QString("%0(%1)").arg(view->title()).arg(count));
if(settings->showUnreadIconInTab()){
setTabIcon(i, QIcon(":/res/unread.png"));
}else{
setTabIcon(i, QIcon());
}
}
}
}else{
for(int i=0;i<count();i++){
TimelineView* view = timelineView(i);
setTabText(i, view->title());
setTabIcon(i, QIcon());
}
}
}
}
void QweenTabCtrl::OnFavorite(){
emit favorite();
}
void QweenTabCtrl::OnReply(){
TimelineView* v = qobject_cast<TimelineView*>(sender());
if(v == m_dmView){
emit dm();
}else{
emit reply();
}
}
void QweenTabCtrl::jumpToUnread(){
TimelineView* v = currentTimelineView();
if(v)
v->jumpToUnread();
}
void QweenTabCtrl::setReadAll(){
TimelineView* v = currentTimelineView();
if(v)
v->setReadAll();
}
void QweenTabCtrl::makeConnection(TimelineView *view){
connect(view, SIGNAL(itemSelected(Twitter::TwitterItem)),
this, SLOT(OnItemSelected(Twitter::TwitterItem)));
connect(view, SIGNAL(unreadCountChanged(int)),
this, SLOT(OnUnreadCountChanged(int)));
connect(view, SIGNAL(favorite()),
this, SLOT(OnFavorite()));
connect(view, SIGNAL(reply()),
this, SLOT(OnReply()));
}