-
Notifications
You must be signed in to change notification settings - Fork 2
/
GroupListWidget.cpp
359 lines (318 loc) · 10.1 KB
/
GroupListWidget.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
/*
(c) 2009,2010,2011 Ian Clark
This file is part of Yasst.
Yasst 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.
Yasst 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 Yasst. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id: GroupListWidget.cpp,v 1.23 2010/07/25 14:51:22 ian Exp $
File: GroupListWidget.h
(c) 2009 Ian Clark
*/
#include <QMessageBox>
#include "GroupListWidget.h"
#include "GroupListWidgetItem.h"
#include "backend/fetchers/UserGroupFetcher.h"
GroupListWidget::GroupListWidget(QWidget *parent) : TweetListWidget(parent) {
type=1;
id="";
}
GroupListWidget::GroupListWidget(BirdBox *b,ImageCache *i, QWidget *parent) : TweetListWidget(b,i,parent),myGroup(0),id("") {
setListVisible();
QObject::connect(ui.save,SIGNAL(clicked()),this,SLOT(saveGroup()));
QObject::connect(ui.cancel,SIGNAL(clicked()),this,SLOT(handleCancelPressed()));
QObject::connect(ui.title,SIGNAL(doubleclicked()),this,SLOT(setListVisible()));
QObject::connect(ui.closeButton,SIGNAL(clicked()),this,SLOT(close()));
QObject::connect(ui.groupmembers,SIGNAL(itemClicked (QListWidgetItem *)),this,SLOT(handleItemActivated (QListWidgetItem *)));
QObject::connect(b,SIGNAL(haveUser(TweetUser *)),this,SLOT(handleHaveUser(TweetUser *)));
QObject::connect(i,SIGNAL(update(QString)),this,SLOT(handleImageUpdate(QString)));
QObject::connect(b,SIGNAL(groupUpdated(Group*)),this,SLOT(handleGroupUpdated(Group*)));
QString tt = ui.title->toolTip();
if (tt!="")
tt+="\n";
tt+=tr("Double click to change the group members.");
ui.title->setToolTip(tt);
// b->registerGroup(this);
type=1;
ui.type->setPixmap(QPixmap(":/buttons/group.png"));
hadSave=false;
}
GroupListWidget::~GroupListWidget() {
// tweets->unregisterGroup(this);
QObject::disconnect(ui.save,SIGNAL(clicked()),this,SLOT(saveGroup()));
QObject::disconnect(ui.cancel,SIGNAL(clicked()),this,SLOT(setTweetsVisible()));
QObject::disconnect(ui.title,SIGNAL(doubleclicked()),this,SLOT(setListVisible()));
QObject::disconnect(ui.closeButton,SIGNAL(clicked()),this,SLOT(close()));
if (myGroup) {
myGroup->goodbye();
if (!myGroup->twitterList && !myGroup->getRefCount()) {
tweets->removeGroup(myGroup);
myGroup->deleteLater();
}
}
}
void GroupListWidget::saveGroup() {
QList<QListWidgetItem*> selected;
bool twitterList = ui.twitterlist->isChecked();
QString name = ui.groupname->text();
groupmembers.clear();
ui.title->setText(ui.groupname->text());
selected=ui.groupmembers->selectedItems();
for(int x=0;x<selected.count();x++) {
QString name=selected[x]->text();
//u=tweets->getUserByScreenName(name);
groupmembers.append(name);
}
if (!myGroup && id=="") {
myGroup=new Group(tweets);
myGroup->hello();
myGroup->twitterList=false;
myGroup->setGroupMembers(groupmembers);
myGroup->updateName(ui.groupname->text());
tweets->handleGroup(myGroup);
id="";
}
if (myGroup) {
myGroup->updateGroupMembers(groupmembers);// NB: Causes update signal, resets checkbox
myGroup->updateName(name);
if (twitterList&&(!(myGroup->twitterList)||(myGroup->ownerScreenName!=myGroup->user))) {
if (tweets->hasCredentials()) {
myGroup->ownerScreenName=myGroup->user;
UserGroupFetcher *f = new UserGroupFetcher();
tweets->addFetcher(f);
f->promoteGroupToList(myGroup);
}
} else if (!twitterList && myGroup->twitterList) {
myGroup->convertToLocal();
id="";
}
} else if (!twitterList)
id="";
repopulate();
setTweetsVisible();
emit tweetsPending();
hadSave=true;
}
bool GroupListWidget::canDoUpdate(Tweet *t) {
return groupmembers.contains(t->screen_name)&&TweetListWidget::canDoUpdate(t);
}
void GroupListWidget::populateList() {
QList<TweetUser *> users = tweets->getUsers();
QList<QString> groupmembers(this->groupmembers);
ui.groupmembers->clear();
for(int x=0;x<users.count();x++) {
if (users[x]->is_friend) {
QListWidgetItem *i=new GroupListWidgetItem(users[x]->screen_name);
i->setIcon(QIcon(images->getPixMap(users[x]->image_url)));
ui.groupmembers->addItem(i);
if(groupmembers.contains(users[x]->screen_name)) {
i->setSelected(true);
groupmembers.removeOne(users[x]->screen_name);
}
}
}
while (groupmembers.count()) {
QString name = groupmembers.takeFirst();
QListWidgetItem *i=new GroupListWidgetItem(name);
i->setIcon(QIcon(images->getPixMap("")));
ui.groupmembers->addItem(i);
i->setSelected(true);
}
if (myGroup)
ui.twitterlist->setChecked(myGroup->twitterList);
else
ui.twitterlist->setChecked(id!="");
}
void GroupListWidget::setListVisible() {
populateList();
setStack(1);
}
void GroupListWidget::writeSettings(QSettings &settings) {
TweetListWidget::writeSettings(settings);
QList<QString> members;
// QList<QString,TweetUser*>::iterator i = groupmembers.begin();
QList<Tweet*> ret;
settings.setValue("name",ui.title->text());
if (myGroup) {
settings.setValue("list_id",myGroup->id);
settings.setValue("local_id",myGroup->local_id);
} else
settings.setValue("list_id",id);
settings.beginWriteArray("members");
for(int x=0;x<groupmembers.count();x++) {
settings.setArrayIndex(x);
settings.setValue("user_id",groupmembers.at(x));
}
settings.endArray();
}
void GroupListWidget::readSettings(QSettings &settings) {
TweetListWidget::readSettings(settings);
ui.title->setText(settings.value("name").toString());
ui.groupname->setText(settings.value("name").toString());
id=settings.value("list_id","").toString();
// QMessageBox::information(0,"ff",ui.groupname->text());
int count = settings.beginReadArray("members");
groupmembers.clear();
for(int x=0;x<count;x++) {
settings.setArrayIndex(x);
QString val = settings.value("user_id").toString();
groupmembers.append(val);
}
settings.endArray();
if (!myGroup && id=="") {
myGroup=new Group(tweets);
myGroup->twitterList=false;
myGroup->name=ui.groupname->text();
myGroup->updateGroupMembers(groupmembers);
tweets->handleGroup(myGroup);
}
repopulate();
setTweetsVisible();
hadSave=true;
}
void GroupListWidget::close() {
if ((myGroup&&myGroup->twitterList)||(!myGroup&&id!=""))
return;
emit groupListClosed(ui.title->text(),groupmembers);
}
void GroupListWidget::setMembers(QList<QString> members) {
groupmembers.append(members);
if (myGroup)
myGroup->updateGroupMembers(groupmembers);
populateList();
}
void GroupListWidget::setName(QString name) {
ui.groupname->setText(name);
ui.title->setText(name);
}
void GroupListWidget::handleHaveUser(TweetUser *u) {
if (ui.stack->currentIndex()!=1)
return;
if (u->is_friend) {
int count = ui.groupmembers->count();
for (int x=0;x<count;x++) {
if (ui.groupmembers->item(x)->text()==u->screen_name)
return;
}
QListWidgetItem *i=new GroupListWidgetItem(u->screen_name);
i->setIcon(QIcon(images->getPixMap(u->image_url)));
ui.groupmembers->addItem(i);
if(groupmembers.contains(u->screen_name)) {
i->setSelected(true);
}
}
}
void GroupListWidget::handleImageUpdate(QString url) {
if (ui.stack->currentIndex()!=1)
return;
int count = ui.groupmembers->count();
for (int x=0;x<count;x++) {
QListWidgetItem *i=ui.groupmembers->item(x);
TweetUser *user = tweets->getUserByScreenName(i->text());
if (user && user->image_url==url) {
i->setIcon(QIcon(images->getPixMap(url)));
}
}
//populateList();
}
QString GroupListWidget::getName() {
return ui.title->text();
}
void GroupListWidget::addUserByScreenName(QString name) {
groupmembers.append(name);
if (myGroup)
myGroup->updateName(name);
else {
populateList();
repopulate();
}
}
void GroupListWidget::removeUserByScreenName(QString name) {
if (groupmembers.removeOne(name)) {
if (myGroup)
myGroup->removeGroupMember(name);
else {
populateList();
repopulate();
}
}
}
void GroupListWidget::handleGroupUpdated(Group *g) {
if (g->id!="" && g->id==id) {
myGroup=g;
myGroup->hello();
id="";
}
if (myGroup && myGroup==g) {
groupmembers.clear();
groupmembers=g->groupmembers;
ui.title->setText(g->name);
ui.groupname->setText(g->name);
populateList();
repopulate();
}
}
void GroupListWidget::setGroup(Group *g) {
if (myGroup==g)
return;
if (myGroup)
myGroup->goodbye();
myGroup=g;
myGroup->hello();
groupmembers.clear();
groupmembers=g->groupmembers;
ui.title->setText(g->name);
ui.groupname->setText(g->name);
populateList();
repopulate();
}
void GroupListWidget::repopulate() {
if ((myGroup && myGroup->twitterList)||id!="")
if (myGroup && myGroup->ownerScreenName!=myGroup->user)
ui.type->setPixmap(QPixmap(":/buttons/twitter-dark.png"));
else
ui.type->setPixmap(QPixmap(":/buttons/twitter.png"));
else
ui.type->setPixmap(QPixmap(":/buttons/group.png"));
TweetListWidget::repopulate();
}
void GroupListWidget::handleCancelPressed() {
if (!hadSave&&!myGroup)
slowDelete();
else
TweetListWidget::setTweetsVisible();
}
void GroupListWidget::handleItemActivated ( QListWidgetItem * ){
if (myGroup && ui.stack->currentIndex()==1 && myGroup->twitterList && myGroup->ownerScreenName!=myGroup->user) {
QMessageBox box;
box.setText(tr("This is a list your follow, to add or remove members you must create a copy or convert to a local group.\n\nDoing this will not unfollow this list."));
box.addButton(tr("Create twitter list"),QMessageBox::AcceptRole);
box.addButton(tr("Convert to local group"),QMessageBox::ActionRole);
box.addButton(QMessageBox::Cancel);
box.exec();
QAbstractButton *b = box.clickedButton();
if (!b || box.buttonRole(b)==QMessageBox::RejectRole) {
populateList();
} else if (box.buttonRole(b)==QMessageBox::AcceptRole) {
// myGroup->convertToLocal(); // saving the group will create the list
} else if (box.buttonRole(b)==QMessageBox::ActionRole) {
ui.twitterlist->setChecked(false);
}
}
}
void GroupListWidget::handleRefreshButton() {
if (myGroup)
myGroup->refresh();
else {
TweetFetcher *f = tweets->getFetcher(1);
if (f)
f->populate();
}
}