~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/contactlistgroup.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * contactlistgroup.cpp - flat contact list group class
 
3
 * Copyright (C) 2008-2010  Yandex LLC (Michail Pishchagin)
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "contactlistgroup.h"
 
22
 
 
23
#include <QTimer>
 
24
 
 
25
#include "contactlistitemproxy.h"
 
26
#include "contactlistgroupmenu.h"
 
27
#include "contactlistmodel.h"
 
28
#include "psicontact.h"
 
29
#include "contactlistgroupstate.h"
 
30
#include "contactlistgroupcache.h"
 
31
#ifdef YAPSI
 
32
#include "fakegroupcontact.h"
 
33
#endif
 
34
 
 
35
static QString GROUP_DELIMITER = "::";
 
36
 
 
37
/**
 
38
 * Flat group class.
 
39
 */
 
40
ContactListGroup::ContactListGroup(ContactListModel* model, ContactListGroup* parent)
 
41
        : ContactListItem()
 
42
        , model_(model)
 
43
        , parent_(parent)
 
44
        , updateOnlineContactsTimer_(0)
 
45
        , haveOnlineContacts_(false)
 
46
        , onlineContactsCount_(0)
 
47
        , totalContactsCount_(0)
 
48
{
 
49
        updateOnlineContactsTimer_ = new QTimer(this);
 
50
        connect(updateOnlineContactsTimer_, SIGNAL(timeout()), SLOT(updateOnlineContactsFlag()));
 
51
        updateOnlineContactsTimer_->setSingleShot(true);
 
52
        updateOnlineContactsTimer_->setInterval(100);
 
53
}
 
54
 
 
55
ContactListGroup::~ContactListGroup()
 
56
{
 
57
        clearGroup();
 
58
}
 
59
 
 
60
void ContactListGroup::clearGroup()
 
61
{
 
62
        foreach(PsiContact* contact, contacts_)
 
63
                removeContact(contact);
 
64
        qDeleteAll(contacts_);
 
65
        contacts_.clear();
 
66
}
 
67
 
 
68
/**
 
69
 * Used only in fullName().
 
70
 */
 
71
QString ContactListGroup::internalGroupName() const
 
72
{
 
73
        return name();
 
74
}
 
75
 
 
76
QString ContactListGroup::fullName() const
 
77
{
 
78
        QStringList name;
 
79
        const ContactListGroup* group = this;
 
80
        while (group) {
 
81
                if (!group->internalGroupName().isEmpty())
 
82
                        name.prepend(group->internalGroupName());
 
83
                group = group->parent();
 
84
        }
 
85
        return name.join(groupDelimiter());
 
86
}
 
87
 
 
88
const QString& ContactListGroup::groupDelimiter()
 
89
{
 
90
        return GROUP_DELIMITER;
 
91
}
 
92
 
 
93
void ContactListGroup::setGroupDelimiter(const QString& str)
 
94
{
 
95
        GROUP_DELIMITER = str;
 
96
}
 
97
 
 
98
QString ContactListGroup::sanitizeGroupName(const QString& name) const
 
99
{
 
100
        return name.split(groupDelimiter(), QString::SkipEmptyParts).join(groupDelimiter());
 
101
}
 
102
 
 
103
QStringList ContactListGroup::sanitizeGroupNames(const QStringList& names) const
 
104
{
 
105
        QStringList sanitized;
 
106
        foreach(QString name, names) {
 
107
                sanitized.append(sanitizeGroupName(name));
 
108
        }
 
109
        return sanitized;
 
110
}
 
111
 
 
112
ContactListModel::Type ContactListGroup::type() const
 
113
{
 
114
        return ContactListModel::GroupType;
 
115
}
 
116
 
 
117
const QString& ContactListGroup::name() const
 
118
{
 
119
        return name_;
 
120
}
 
121
 
 
122
void ContactListGroup::setName(const QString& name)
 
123
{
 
124
        model()->renameGroup(this, name);
 
125
}
 
126
 
 
127
/**
 
128
 * Renames the group without telling the model.
 
129
 */
 
130
void ContactListGroup::quietSetName(const QString& name)
 
131
{
 
132
        if (!name_.isNull())
 
133
                model_->groupCache()->removeGroup(this);
 
134
 
 
135
        name_ = name;
 
136
 
 
137
        if (!name_.isNull())
 
138
                model_->groupCache()->addGroup(this);
 
139
}
 
140
 
 
141
bool ContactListGroup::isExpandable() const
 
142
{
 
143
        return true;
 
144
}
 
145
 
 
146
bool ContactListGroup::expanded() const
 
147
{
 
148
        return model()->groupState()->groupExpanded(this);
 
149
}
 
150
 
 
151
void ContactListGroup::setExpanded(bool expanded)
 
152
{
 
153
        model()->groupState()->setGroupExpanded(this, expanded);
 
154
}
 
155
 
 
156
bool ContactListGroup::isEditable() const
 
157
{
 
158
        bool result = false;
 
159
        foreach(ContactListItemProxy* proxy, items_) {
 
160
                if (proxy->item()) {
 
161
                        if (proxy->item()->isEditable()) {
 
162
                                result = true;
 
163
                                break;
 
164
                        }
 
165
                }
 
166
        }
 
167
 
 
168
        return result && model()->contactList()->haveAvailableAccounts();
 
169
}
 
170
 
 
171
bool ContactListGroup::isRemovable() const
 
172
{
 
173
        bool result = false;
 
174
        foreach(ContactListItemProxy* proxy, items_) {
 
175
                if (proxy->item()) {
 
176
                        if (proxy->item()->isRemovable()) {
 
177
                                result = true;
 
178
                                break;
 
179
                        }
 
180
                }
 
181
        }
 
182
 
 
183
        return result && isEditable();
 
184
}
 
185
 
 
186
ContactListItemMenu* ContactListGroup::contextMenu(ContactListModel* model)
 
187
{
 
188
        return new ContactListGroupMenu(this, model);
 
189
}
 
190
 
 
191
void ContactListGroup::addItem(ContactListItemProxy* item)
 
192
{
 
193
        Q_ASSERT(!items_.contains(item));
 
194
        int index = items_.count();
 
195
        model_->itemAboutToBeInserted(this, index);
 
196
        items_.append(item);
 
197
        model_->insertedItem(this, index);
 
198
        updateOnlineContactsTimer_->start();
 
199
}
 
200
 
 
201
void ContactListGroup::removeItem(ContactListItemProxy* item)
 
202
{
 
203
        int index = items_.indexOf(item);
 
204
        Q_ASSERT(index != -1);
 
205
        model_->itemAboutToBeRemoved(this, index);
 
206
        items_.remove(index);
 
207
        delete item;
 
208
        model_->removedItem(this, index);
 
209
        updateOnlineContactsTimer_->start();
 
210
}
 
211
 
 
212
/**
 
213
 * contactGroups handling rules:
 
214
 * 1. List is empty: we must not add this contact to self;
 
215
 * 2. List contains null QString() element: we must add contact to General group
 
216
 * 3. List contains non-null QString() elements: those contain group names (with group separators)
 
217
 */
 
218
void ContactListGroup::addContact(PsiContact* contact, QStringList contactGroups)
 
219
{
 
220
        if (contactGroups.isEmpty())
 
221
                return;
 
222
 
 
223
        if (findContact(contact))
 
224
                return;
 
225
        Q_ASSERT(!contacts_.contains(contact));
 
226
CL_DEBUG("ContactListGroup(%x)::addContact: %s (items = %d, contacts = %d)", this, qPrintable(contact->jid().full()), items_.count(), contacts_.count());
 
227
        contacts_.append(contact);
 
228
        addItem(new ContactListItemProxy(this, contact));
 
229
 
 
230
        model_->groupCache()->addContact(this, contact);
 
231
}
 
232
 
 
233
void ContactListGroup::removeContact(PsiContact* contact)
 
234
{
 
235
        int index = contacts_.indexOf(contact);
 
236
        Q_ASSERT(index != -1);
 
237
CL_DEBUG("ContactListGroup(%x)::removeContact: %s (items = %d, contacts = %d)", this, qPrintable(contact->jid().full()), items_.count(), contacts_.count());
 
238
        removeItem(findContact(contact));
 
239
        contacts_.remove(index);
 
240
 
 
241
        model_->groupCache()->removeContact(this, contact);
 
242
}
 
243
 
 
244
// Some room for future optimizations here
 
245
ContactListItemProxy* ContactListGroup::findContact(PsiContact* contact) const
 
246
{
 
247
        foreach(ContactListItemProxy* item, items_)
 
248
                if (item->item() == contact)
 
249
                        return item;
 
250
        return 0;
 
251
}
 
252
 
 
253
ContactListItemProxy* ContactListGroup::findGroup(ContactListGroup* group) const
 
254
{
 
255
        foreach(ContactListItemProxy* item, items_)
 
256
                if (item->item() == group)
 
257
                        return item;
 
258
        return 0;
 
259
}
 
260
 
 
261
// ContactListItemProxy* ContactListGroup::findAccount(ContactListAccountGroup* account) const
 
262
// {
 
263
//      foreach(ContactListItemProxy* item, items_)
 
264
//              if (item->item() == account)
 
265
//                      return item;
 
266
//      return 0;
 
267
// }
 
268
 
 
269
void ContactListGroup::contactUpdated(PsiContact* contact)
 
270
{
 
271
        ContactListItemProxy* item = findContact(contact);
 
272
        if (!item)
 
273
                return;
 
274
        updateOnlineContactsTimer_->start();
 
275
        model_->updatedItem(item);
 
276
}
 
277
 
 
278
void ContactListGroup::contactGroupsChanged(PsiContact* contact, QStringList contactGroups)
 
279
{
 
280
        if (contactGroups.isEmpty()) {
 
281
                if (contacts_.contains(contact))
 
282
                        removeContact(contact);
 
283
        }
 
284
        else if (!findContact(contact)) {
 
285
                addContact(contact, contactGroups);
 
286
        }
 
287
 
 
288
        updateOnlineContactsTimer_->start();
 
289
}
 
290
 
 
291
ContactListItemProxy* ContactListGroup::item(int index) const
 
292
{
 
293
        Q_ASSERT(index >= 0);
 
294
        Q_ASSERT(index < items_.count());
 
295
        return items_.at(index);
 
296
}
 
297
 
 
298
int ContactListGroup::itemsCount() const
 
299
{
 
300
        return items_.count();
 
301
}
 
302
 
 
303
// Some room for optimizations here
 
304
int ContactListGroup::indexOf(const ContactListItem* item) const
 
305
{
 
306
        for (int i = 0; i < items_.count(); ++i)
 
307
                if (items_.at(i)->item() == item)
 
308
                        return i;
 
309
        Q_ASSERT(false);
 
310
        return -1;
 
311
}
 
312
 
 
313
ContactListGroup* ContactListGroup::parent() const
 
314
{
 
315
        return parent_;
 
316
}
 
317
 
 
318
QModelIndex ContactListGroup::toModelIndex() const
 
319
{
 
320
        if (!parent())
 
321
                return QModelIndex();
 
322
 
 
323
        int index = parent()->indexOf(this);
 
324
        return model()->itemProxyToModelIndex(parent()->item(index), index);
 
325
}
 
326
 
 
327
const QVector<ContactListItemProxy*>& ContactListGroup::items() const
 
328
{
 
329
        return items_;
 
330
}
 
331
 
 
332
bool ContactListGroup::haveOnlineContacts() const
 
333
{
 
334
        return haveOnlineContacts_;
 
335
}
 
336
 
 
337
int ContactListGroup::onlineContactsCount() const
 
338
{
 
339
        return onlineContactsCount_;
 
340
}
 
341
 
 
342
int ContactListGroup::totalContactsCount() const
 
343
{
 
344
        return totalContactsCount_;
 
345
}
 
346
 
 
347
int ContactListGroup::contactsCount() const
 
348
{
 
349
        return contacts_.count();
 
350
}
 
351
 
 
352
void ContactListGroup::updateOnlineContactsFlag()
 
353
{
 
354
        updateOnlineContactsTimer_->stop();
 
355
        if (!parent())
 
356
                return;
 
357
 
 
358
        bool haveOnlineContacts = false;
 
359
        int onlineContactsCount = 0;
 
360
        int totalContactsCount = 0;
 
361
        foreach(ContactListItemProxy* item, items_) {
 
362
                PsiContact* contact     = 0;
 
363
                ContactListGroup* group = 0;
 
364
                if ((contact = dynamic_cast<PsiContact*>(item->item()))) {
 
365
                        ++totalContactsCount;
 
366
                        if (contact->isOnline()) {
 
367
                                haveOnlineContacts = true;
 
368
                                ++onlineContactsCount;
 
369
                                // break;
 
370
                        }
 
371
                }
 
372
                else if ((group = dynamic_cast<ContactListGroup*>(item->item()))) {
 
373
                        totalContactsCount += group->totalContactsCount();
 
374
                        if (group->haveOnlineContacts()) {
 
375
                                haveOnlineContacts = true;
 
376
                                onlineContactsCount += group->onlineContactsCount();
 
377
                                // break;
 
378
                        }
 
379
                }
 
380
        }
 
381
 
 
382
        if (haveOnlineContacts_ != haveOnlineContacts) {
 
383
                haveOnlineContacts_ = haveOnlineContacts;
 
384
                if (parent()) {
 
385
                        parent()->updateOnlineContactsFlag();
 
386
                        model_->updatedGroupVisibility(this);
 
387
                }
 
388
        }
 
389
 
 
390
        if (onlineContactsCount != onlineContactsCount_ ||
 
391
            totalContactsCount != totalContactsCount_)
 
392
        {
 
393
                onlineContactsCount_ = onlineContactsCount;
 
394
                totalContactsCount_ = totalContactsCount;
 
395
                if (parent()) {
 
396
                        model()->updatedItem(parent()->findGroup(this));
 
397
                }
 
398
        }
 
399
}
 
400
 
 
401
bool ContactListGroup::isFake() const
 
402
{
 
403
        if (items_.count() != contacts_.count())
 
404
                return false;
 
405
 
 
406
        foreach(PsiContact* contact, contacts_) {
 
407
                if (!contact->isFake())
 
408
                        return false;
 
409
        }
 
410
 
 
411
// FIXME
 
412
#ifdef YAPSI
 
413
        return name().startsWith(FakeGroupContact::defaultGroupName());
 
414
#else
 
415
        return false;
 
416
#endif
 
417
}
 
418
 
 
419
bool ContactListGroup::compare(const ContactListItem* other) const
 
420
{
 
421
        const ContactListGroup* group = dynamic_cast<const ContactListGroup*>(other);
 
422
        if (group) {
 
423
                if (group->isSpecial()) {
 
424
                        return !group->compare(this);
 
425
                }
 
426
 
 
427
                int order = model()->groupState()->groupOrder(group) - model()->groupState()->groupOrder(this);
 
428
                if (order) {
 
429
                        return order > 0;
 
430
                }
 
431
        }
 
432
 
 
433
        return ContactListItem::compare(other);
 
434
}
 
435
 
 
436
QList<PsiContact*> ContactListGroup::contacts() const
 
437
{
 
438
        QList<PsiContact*> result;
 
439
        contactsHelper(&result);
 
440
        return result;
 
441
}
 
442
 
 
443
void ContactListGroup::contactsHelper(QList<PsiContact*>* contacts) const
 
444
{
 
445
        foreach(PsiContact* contact, contacts_) {
 
446
                if (!contacts->contains(contact))
 
447
                        contacts->append(contact);
 
448
        }
 
449
 
 
450
        foreach(ContactListItemProxy* item, items_) {
 
451
                ContactListGroup* group = dynamic_cast<ContactListGroup*>(item->item());
 
452
                if (group)
 
453
                        contactsHelper(contacts);
 
454
        }
 
455
}
 
456
 
 
457
#ifdef UNIT_TEST
 
458
void ContactListGroup::dumpTree(int indent) const
 
459
{
 
460
        dumpInfo(this, 0);
 
461
        foreach(ContactListItemProxy* item, items_) {
 
462
                ContactListGroup* group = dynamic_cast<ContactListGroup*>(item->item());
 
463
                if (group)
 
464
                        dumpTree(indent + 1);
 
465
                else
 
466
                        dumpInfo(item->item(), indent + 1);
 
467
        }
 
468
}
 
469
 
 
470
void ContactListGroup::dumpInfo(const ContactListItem* item, int indent) const
 
471
{
 
472
        qWarning("%sname = '%s', type = %s", qPrintable(QString(indent, ' ')),
 
473
                 qPrintable(item->name()),
 
474
                 dynamic_cast<const ContactListGroup*>(item) ? "group" : "contact");
 
475
}
 
476
 
 
477
void ContactListGroup::dumpTree() const
 
478
{
 
479
        dumpTree(0);
 
480
}
 
481
#endif
 
482
 
 
483
bool ContactListGroup::isSpecial() const
 
484
{
 
485
        return false;
 
486
}
 
487
 
 
488
ContactListGroup::SpecialType ContactListGroup::specialGroupType() const
 
489
{
 
490
        return SpecialType_None;
 
491
}