~ubuntu-branches/ubuntu/hoary/psi/hoary

« back to all changes in this revision

Viewing changes to src/options/opt_iconset.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "opt_iconset.h"
 
2
#include "common.h"
 
3
#include "iconwidget.h"
 
4
#include "psicon.h"
 
5
 
 
6
#include <qbuttongroup.h>
 
7
#include <qwhatsthis.h>
 
8
#include <qcheckbox.h>
 
9
#include <qradiobutton.h>
 
10
#include <qcombobox.h>
 
11
#include <qlineedit.h>
 
12
#include <qfileinfo.h>
 
13
#include <qlistview.h>
 
14
#include <qheader.h>
 
15
#include <qapplication.h>
 
16
#include <qthread.h>
 
17
#include <qmutex.h>
 
18
#include <qevent.h>
 
19
#include <qprogressbar.h>
 
20
#include <qcursor.h>
 
21
#include <qpalette.h>
 
22
#include <qtabwidget.h>
 
23
 
 
24
#include "opt_iconset_emo.h"
 
25
#include "opt_iconset_system.h"
 
26
#include "opt_iconset_roster.h"
 
27
#include "ui_isdetails.h"
 
28
 
 
29
static void isDetails(const Iconset &is, QWidget *parent)
 
30
{
 
31
        IconsetDetailsDlg *isd = new IconsetDetailsDlg(parent, "IconsetDetailsDlg", true);
 
32
        isd->setIconset(is);
 
33
        isd->exec();
 
34
        delete isd;
 
35
}
 
36
 
 
37
static QStringList dirs;
 
38
 
 
39
static int countIconsets(QString addDir, QStringList excludeList)
 
40
{
 
41
        int count = 0;
 
42
 
 
43
        QStringList::Iterator it = dirs.begin();
 
44
        for ( ; it != dirs.end(); ++it) {
 
45
                QString fileName = *it + "/iconsets" + addDir;
 
46
                QDir dir (fileName);
 
47
 
 
48
                QStringList list = dir.entryList("*");
 
49
                QStringList::Iterator it2 = list.begin();
 
50
                for ( ; it2 != list.end(); ++it2) {
 
51
                        if ( *it2 == "." || *it2 == ".." )
 
52
                                continue;
 
53
 
 
54
                        bool found = false;
 
55
                        QStringList::Iterator it3 = excludeList.begin();
 
56
                        for ( ; it3 != excludeList.end(); ++it3) {
 
57
                                if ( *it2 == *it3 ) {
 
58
                                        found = true;
 
59
                                        break;
 
60
                                }
 
61
                        }
 
62
 
 
63
                        if ( found )
 
64
                                continue;
 
65
 
 
66
                        count++;
 
67
                        excludeList << *it2;
 
68
                }
 
69
        }
 
70
 
 
71
        return count;
 
72
}
 
73
 
 
74
//----------------------------------------------------------------------------
 
75
// IconsetLoadEvent
 
76
//----------------------------------------------------------------------------
 
77
 
 
78
class IconsetLoadEvent : public QEvent
 
79
{
 
80
public:
 
81
        IconsetLoadEvent(IconsetLoadThread *par, Iconset *i)
 
82
        : QEvent(QEvent::User)
 
83
        {
 
84
                p  = par;
 
85
                is = i;
 
86
        }
 
87
 
 
88
        IconsetLoadThread *thread() const { return p; }
 
89
 
 
90
        // if iconset() is '0' then it means that iconset wans't loaded successfully
 
91
        Iconset *iconset() const { return is; }
 
92
 
 
93
private:
 
94
        IconsetLoadThread *p;
 
95
        Iconset *is;
 
96
};
 
97
 
 
98
//----------------------------------------------------------------------------
 
99
// IconsetFinishEvent
 
100
//----------------------------------------------------------------------------
 
101
 
 
102
class IconsetFinishEvent : public QEvent
 
103
{
 
104
public:
 
105
        IconsetFinishEvent()
 
106
        : QEvent( (QEvent::Type)(QEvent::User + 1) )
 
107
        {
 
108
        }
 
109
};
 
110
 
 
111
 
 
112
//----------------------------------------------------------------------------
 
113
// IconsetLoadThreadDestroyEvent
 
114
//----------------------------------------------------------------------------
 
115
 
 
116
class IconsetLoadThreadDestroyEvent : public QEvent
 
117
{
 
118
public:
 
119
        IconsetLoadThreadDestroyEvent(QThread *t)
 
120
        : QEvent( (QEvent::Type)(QEvent::User + 2) )
 
121
        {
 
122
                thread = t;
 
123
        }
 
124
 
 
125
        ~IconsetLoadThreadDestroyEvent()
 
126
        {
 
127
                thread->wait();
 
128
                delete thread;
 
129
        }
 
130
 
 
131
private:
 
132
        QThread *thread;
 
133
};
 
134
 
 
135
//----------------------------------------------------------------------------
 
136
// IconsetLoadThread
 
137
//----------------------------------------------------------------------------
 
138
 
 
139
class IconsetLoadThread : public QThread
 
140
{
 
141
public:
 
142
        IconsetLoadThread(QObject *parent, QString addPath);
 
143
        void excludeIconsets(QStringList);
 
144
 
 
145
        bool cancelled;
 
146
 
 
147
protected:
 
148
        void run();
 
149
        void postEvent(QEvent *);
 
150
 
 
151
private:
 
152
        QObject *parent;
 
153
        QString addPath;
 
154
        QStringList excludeList;
 
155
};
 
156
 
 
157
IconsetLoadThread::IconsetLoadThread(QObject *p, QString path)
 
158
{
 
159
        cancelled = false;
 
160
        parent  = p;
 
161
        addPath = path;
 
162
}
 
163
 
 
164
void IconsetLoadThread::excludeIconsets(QStringList l)
 
165
{
 
166
        excludeList += l;
 
167
}
 
168
 
 
169
static QMutex threadCancelled, threadMutex;
 
170
 
 
171
void IconsetLoadThread::postEvent(QEvent *e)
 
172
{
 
173
        threadCancelled.lock();
 
174
        bool cancel = cancelled;
 
175
        threadCancelled.unlock();
 
176
 
 
177
        if ( cancel ) {
 
178
                delete e;
 
179
                return;
 
180
        }
 
181
 
 
182
        QApplication::postEvent(parent, e);
 
183
}
 
184
 
 
185
void IconsetLoadThread::run()
 
186
{
 
187
        threadMutex.lock();
 
188
        QStringList dirs = ::dirs;
 
189
        threadMutex.unlock();
 
190
 
 
191
        QStringList::Iterator it = dirs.begin();
 
192
        for ( ; it != dirs.end(); ++it) {
 
193
                QString fileName = *it + "/iconsets" + addPath;
 
194
                QDir dir (fileName);
 
195
 
 
196
                QStringList list = dir.entryList("*");
 
197
                QStringList::Iterator it2 = list.begin();
 
198
                for ( ; it2 != list.end(); ++it2) {
 
199
                        if ( *it2 == "." || *it2 == ".." )
 
200
                                continue;
 
201
 
 
202
                        threadCancelled.lock();
 
203
                        bool cancel = cancelled;
 
204
                        threadCancelled.unlock();
 
205
 
 
206
                        if ( cancel )
 
207
                                goto getout;
 
208
 
 
209
                        bool found = false;
 
210
                        QStringList::Iterator it3 = excludeList.begin();
 
211
                        for ( ; it3 != excludeList.end(); ++it3) {
 
212
                                if ( *it2 == *it3 ) {
 
213
                                        found = true;
 
214
                                        break;
 
215
                                }
 
216
                        }
 
217
 
 
218
                        if ( found )
 
219
                                continue;
 
220
 
 
221
                        IconsetLoadEvent *event = 0;
 
222
                        Iconset *is = new Iconset;
 
223
 
 
224
                        if ( is->load (fileName + "/" + *it2) ) {
 
225
                                excludeList << *it2;
 
226
 
 
227
                                // don't forget to delete iconset in ::event()!
 
228
                                event = new IconsetLoadEvent(this, is);
 
229
                        }
 
230
                        else {
 
231
                                delete is;
 
232
                                event = new IconsetLoadEvent(this, 0);
 
233
 
 
234
                                // without excluding corrupted iconset,
 
235
                                // counter will go nuts! read more comments
 
236
                                // about that...
 
237
                                excludeList << *it2;
 
238
                                // TODO: there is possibility,
 
239
                                // that there's a bunch of same-named
 
240
                                // iconsets, and some of them are corrupted.
 
241
                                // It is possible to write a hack that
 
242
                                // loads iconset even in that case.
 
243
 
 
244
                                // logic:
 
245
                                //   tried to load iconset --> unable to load
 
246
                                //   checking if there's same-named iconsets in
 
247
                                //   other directories
 
248
                                //   emit IconsetLoadEvent() only on success
 
249
                                //   or when last corrupted iconset was unable
 
250
                                //   to load
 
251
                        }
 
252
 
 
253
                        postEvent(event);
 
254
                }
 
255
        }
 
256
 
 
257
getout:
 
258
        postEvent(new IconsetFinishEvent());
 
259
        QApplication::postEvent(qApp, new IconsetLoadThreadDestroyEvent(this)); // self destruct
 
260
}
 
261
 
 
262
//----------------------------------------------------------------------------
 
263
// OptionsTabIconsetSystem
 
264
//----------------------------------------------------------------------------
 
265
 
 
266
OptionsTabIconsetSystem::OptionsTabIconsetSystem(QObject *parent)
 
267
: OptionsTab(parent, "iconset_system", "", tr("System Iconset"), tr("Select the system iconset"), "psi/info")
 
268
{
 
269
        w = 0;
 
270
        thread = 0;
 
271
 
 
272
        if ( dirs.isEmpty() ) {
 
273
                dirs << ".";
 
274
                dirs << g.pathHome;
 
275
                dirs << g.pathBase;
 
276
        }
 
277
}
 
278
 
 
279
OptionsTabIconsetSystem::~OptionsTabIconsetSystem()
 
280
{
 
281
        cancelThread();
 
282
}
 
283
 
 
284
QWidget *OptionsTabIconsetSystem::widget()
 
285
{
 
286
        if ( w )
 
287
                return 0;
 
288
 
 
289
        w = new IconsetSystemUI;
 
290
        IconsetSystemUI *d = (IconsetSystemUI *)w;
 
291
 
 
292
        connect(d->pb_sysDetails, SIGNAL(clicked()), SLOT(previewIconset()));
 
293
 
 
294
        // TODO: add QWhatsThis
 
295
 
 
296
        return w;
 
297
}
 
298
 
 
299
void OptionsTabIconsetSystem::applyOptions(Options *opt)
 
300
{
 
301
        if ( !w || thread )
 
302
                return;
 
303
 
 
304
        IconsetSystemUI *d = (IconsetSystemUI *)w;
 
305
        const Iconset *is = d->iss_system->iconset();
 
306
        if ( is ) {
 
307
                QFileInfo fi( is->fileName() );
 
308
                opt->systemIconset = fi.fileName();
 
309
        }
 
310
}
 
311
 
 
312
void OptionsTabIconsetSystem::restoreOptions(const Options *opt)
 
313
{
 
314
        if ( !w || thread )
 
315
                return;
 
316
 
 
317
        o = (Options *)opt;
 
318
        IconsetSystemUI *d = (IconsetSystemUI *)w;
 
319
        d->iss_system->clear();
 
320
        QStringList loaded;
 
321
 
 
322
        d->setCursor(WaitCursor);
 
323
 
 
324
        QPalette customPal = d->palette();
 
325
        customPal.setDisabled(customPal.inactive());
 
326
        d->iss_system->setEnabled(false);
 
327
        d->iss_system->setPalette(customPal);
 
328
 
 
329
        d->pb_sysDetails->setEnabled(false);
 
330
        d->pb_sysDetails->setPalette(customPal);
 
331
 
 
332
        d->progress->show();
 
333
        d->progress->setProgress( 0 );
 
334
 
 
335
        numIconsets = countIconsets("/system", loaded);
 
336
        iconsetsLoaded = 0;
 
337
 
 
338
        cancelThread();
 
339
 
 
340
        thread = new IconsetLoadThread(this, "/system");
 
341
        thread->start();
 
342
}
 
343
 
 
344
bool OptionsTabIconsetSystem::event(QEvent *e)
 
345
{
 
346
        IconsetSystemUI *d = (IconsetSystemUI *)w;
 
347
        if ( e->type() == QEvent::User ) { // iconset load event
 
348
                IconsetLoadEvent *le = (IconsetLoadEvent *)e;
 
349
 
 
350
                if ( thread != le->thread() )
 
351
                        return false;
 
352
 
 
353
                if ( !numIconsets )
 
354
                        numIconsets = 1;
 
355
                d->progress->setProgress( (int)((float)100 * ++iconsetsLoaded / numIconsets) );
 
356
 
 
357
                Iconset *i = le->iconset();
 
358
                if ( i ) {
 
359
                        is->stripFirstAnimFrame(i);
 
360
                        d->iss_system->insert(*i);
 
361
 
 
362
                        QFileInfo fi( i->fileName() );
 
363
                        if ( fi.fileName() == o->systemIconset )
 
364
                                d->iss_system->setSelected(d->iss_system->count()-1, true);
 
365
 
 
366
                        delete i;
 
367
                }
 
368
 
 
369
                return true;
 
370
        }
 
371
        else if ( e->type() == QEvent::User + 1 ) { // finish event
 
372
                d->iss_system->setEnabled(true);
 
373
                d->iss_system->unsetPalette();
 
374
 
 
375
                d->pb_sysDetails->setEnabled(true);
 
376
                d->pb_sysDetails->unsetPalette();
 
377
 
 
378
                connect(d->iss_system, SIGNAL(selectionChanged()), SIGNAL(dataChanged()));
 
379
 
 
380
                d->progress->hide();
 
381
 
 
382
                d->unsetCursor();
 
383
                thread = 0;
 
384
 
 
385
                return true;
 
386
        }
 
387
 
 
388
        return false;
 
389
}
 
390
 
 
391
void OptionsTabIconsetSystem::previewIconset()
 
392
{
 
393
        IconsetSystemUI *d = (IconsetSystemUI *)w;
 
394
        const Iconset *is = d->iss_system->iconset();
 
395
        if ( is )
 
396
                isDetails(*is, parentWidget);
 
397
}
 
398
 
 
399
void OptionsTabIconsetSystem::setData(PsiCon *, QWidget *p)
 
400
{
 
401
        parentWidget = p;
 
402
}
 
403
 
 
404
void OptionsTabIconsetSystem::cancelThread()
 
405
{
 
406
        if ( thread ) {
 
407
                threadCancelled.lock();
 
408
                thread->cancelled = true;
 
409
                threadCancelled.unlock();
 
410
 
 
411
                thread = 0;
 
412
        }
 
413
}
 
414
 
 
415
//----------------------------------------------------------------------------
 
416
// OptionsTabIconsetEmoticons
 
417
//----------------------------------------------------------------------------
 
418
 
 
419
OptionsTabIconsetEmoticons::OptionsTabIconsetEmoticons(QObject *parent)
 
420
: OptionsTab(parent, "iconset_emoticons", "", tr("Emoticons"), tr("Select your emoticon iconsets"), "psi/smile")
 
421
{
 
422
        w = 0;
 
423
        thread = 0;
 
424
}
 
425
 
 
426
OptionsTabIconsetEmoticons::~OptionsTabIconsetEmoticons()
 
427
{
 
428
        cancelThread();
 
429
}
 
430
 
 
431
QWidget *OptionsTabIconsetEmoticons::widget()
 
432
{
 
433
        if ( w )
 
434
                return 0;
 
435
 
 
436
        w = new IconsetEmoUI;
 
437
        IconsetEmoUI *d = (IconsetEmoUI *)w;
 
438
 
 
439
        connect(d->pb_emoUp, SIGNAL(clicked()), d->iss_emoticons, SLOT(moveItemUp()));
 
440
        connect(d->pb_emoUp, SIGNAL(clicked()), SIGNAL(dataChanged()));
 
441
        connect(d->pb_emoDown, SIGNAL(clicked()), d->iss_emoticons, SLOT(moveItemDown()));
 
442
        connect(d->pb_emoDown, SIGNAL(clicked()), SIGNAL(dataChanged()));
 
443
        connect(d->pb_emoDetails, SIGNAL(clicked()), SLOT(previewIconset()));
 
444
 
 
445
        QWhatsThis::add(d->ck_useEmoticons,
 
446
                tr("<P>Emoticons are short sequences of characters that are used to convey an emotion or idea.</P>"
 
447
                "<P>Enable this option if you want Psi to replace common emoticons with a graphical image.</P>"
 
448
                "<P>For example, <B>:-)</B> would be relaced by <icon name=\"psi/smile\"></P>"));
 
449
 
 
450
        // TODO: add QWhatsThis
 
451
 
 
452
        return w;
 
453
}
 
454
 
 
455
void OptionsTabIconsetEmoticons::applyOptions(Options *opt)
 
456
{
 
457
        if ( !w || thread )
 
458
                return;
 
459
 
 
460
        IconsetEmoUI *d = (IconsetEmoUI *)w;
 
461
        opt->useEmoticons = d->ck_useEmoticons->isChecked();
 
462
 
 
463
        opt->emoticons.clear();
 
464
        IconWidgetItem *item = (IconWidgetItem *)d->iss_emoticons->firstItem();
 
465
        while ( item ) {
 
466
                if ( item->isSelected() ) {
 
467
                        const Iconset *is = item->iconset();
 
468
                        if ( is ) {
 
469
                                QFileInfo fi( is->fileName() );
 
470
                                opt->emoticons << fi.fileName();
 
471
                        }
 
472
                }
 
473
 
 
474
                item = (IconWidgetItem *)item->next();
 
475
        }
 
476
 
 
477
}
 
478
 
 
479
void OptionsTabIconsetEmoticons::restoreOptions(const Options *opt)
 
480
{
 
481
        if ( !w || thread )
 
482
                return;
 
483
 
 
484
        IconsetEmoUI *d = (IconsetEmoUI *)w;
 
485
        d->ck_useEmoticons->setChecked( opt->useEmoticons );
 
486
 
 
487
        // fill in the iconset view
 
488
        d->iss_emoticons->clear();
 
489
 
 
490
        {
 
491
                QPtrListIterator<Iconset> it ( is->emoticons );
 
492
                Iconset *is;
 
493
                for ( ; (is = it.current()); ++it) {
 
494
                        d->iss_emoticons->insert(*is);
 
495
                        d->iss_emoticons->setSelected(d->iss_emoticons->count()-1, true);
 
496
                }
 
497
        }
 
498
 
 
499
 
 
500
        {
 
501
                QStringList loaded;
 
502
                {
 
503
                        QPtrListIterator<Iconset> it( is->emoticons );
 
504
                        Iconset *tmp;
 
505
                        for ( ; (tmp = it.current()); ++it) {
 
506
                                QFileInfo fi ( tmp->fileName() );
 
507
                                loaded << fi.fileName();
 
508
                        }
 
509
                }
 
510
 
 
511
                d->setCursor(WaitCursor);
 
512
 
 
513
                QPalette customPal = d->palette();
 
514
                customPal.setDisabled(customPal.inactive());
 
515
                d->ck_useEmoticons->setEnabled(false);
 
516
                d->ck_useEmoticons->setPalette(customPal);
 
517
 
 
518
                d->groupBox9->setEnabled(false);
 
519
                d->groupBox9->setPalette(customPal);
 
520
 
 
521
                d->progress->show();
 
522
                d->progress->setProgress( 0 );
 
523
 
 
524
                numIconsets = countIconsets("/emoticons", loaded);
 
525
                iconsetsLoaded = 0;
 
526
 
 
527
                cancelThread();
 
528
 
 
529
                thread = new IconsetLoadThread(this, "/emoticons");
 
530
                thread->excludeIconsets(loaded);
 
531
                thread->start();
 
532
        }
 
533
}
 
534
 
 
535
bool OptionsTabIconsetEmoticons::event(QEvent *e)
 
536
{
 
537
        IconsetEmoUI *d = (IconsetEmoUI *)w;
 
538
        if ( e->type() == QEvent::User ) { // iconset load event
 
539
                IconsetLoadEvent *le = (IconsetLoadEvent *)e;
 
540
 
 
541
                if ( thread != le->thread() )
 
542
                        return false;
 
543
 
 
544
                if ( !numIconsets )
 
545
                        numIconsets = 1;
 
546
                d->progress->setProgress( (int)((float)100 * ++iconsetsLoaded / numIconsets) );
 
547
 
 
548
                Iconset *is = le->iconset();
 
549
                if ( is ) {
 
550
                        PsiIconset::removeAnimation(is);
 
551
                        d->iss_emoticons->insert(*is);
 
552
                        delete is;
 
553
                }
 
554
 
 
555
                return true;
 
556
        }
 
557
        else if ( e->type() == QEvent::User + 1 ) { // finish event
 
558
                d->ck_useEmoticons->setEnabled(true);
 
559
                d->ck_useEmoticons->unsetPalette();
 
560
 
 
561
                d->groupBox9->setEnabled(true);
 
562
                d->groupBox9->unsetPalette();
 
563
 
 
564
                connect(d->iss_emoticons, SIGNAL(selectionChanged()), SIGNAL(dataChanged()));
 
565
 
 
566
                bool checked = d->ck_useEmoticons->isChecked();
 
567
                d->ck_useEmoticons->setChecked( true );
 
568
                d->ck_useEmoticons->setChecked( checked );
 
569
 
 
570
                d->progress->hide();
 
571
 
 
572
                d->unsetCursor();
 
573
                thread = 0;
 
574
 
 
575
                return true;
 
576
        }
 
577
 
 
578
        return false;
 
579
}
 
580
 
 
581
void OptionsTabIconsetEmoticons::previewIconset()
 
582
{
 
583
        IconsetEmoUI *d = (IconsetEmoUI *)w;
 
584
        const Iconset *is = d->iss_emoticons->iconset();
 
585
        if ( is )
 
586
                isDetails(*is, parentWidget);
 
587
}
 
588
 
 
589
void OptionsTabIconsetEmoticons::setData(PsiCon *, QWidget *p)
 
590
{
 
591
        parentWidget = p;
 
592
}
 
593
 
 
594
void OptionsTabIconsetEmoticons::cancelThread()
 
595
{
 
596
        if ( thread ) {
 
597
                threadCancelled.lock();
 
598
                thread->cancelled = true;
 
599
                threadCancelled.unlock();
 
600
 
 
601
                thread = 0;
 
602
        }
 
603
}
 
604
 
 
605
//----------------------------------------------------------------------------
 
606
// OptionsTabIconsetRoster
 
607
//----------------------------------------------------------------------------
 
608
 
 
609
OptionsTabIconsetRoster::OptionsTabIconsetRoster(QObject *parent)
 
610
: OptionsTab(parent, "iconset_roster", "", tr("Roster Iconsets"), tr("Select iconsets for your roster"), "psi/www")
 
611
{
 
612
        w = 0;
 
613
        thread = 0;
 
614
}
 
615
 
 
616
OptionsTabIconsetRoster::~OptionsTabIconsetRoster()
 
617
{
 
618
        cancelThread();
 
619
}
 
620
 
 
621
QWidget *OptionsTabIconsetRoster::widget()
 
622
{
 
623
        if ( w )
 
624
                return 0;
 
625
 
 
626
        w = new IconsetRosterUI;
 
627
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
628
 
 
629
        connect(d->pb_defRosterDetails, SIGNAL(clicked()), SLOT(defaultDetails()));
 
630
 
 
631
        connect(d->iss_servicesRoster, SIGNAL(selectionChanged(QListBoxItem *)), SLOT(isServices_iconsetSelected(QListBoxItem *)));
 
632
        connect(d->lv_isServices, SIGNAL(selectionChanged(QListViewItem *)), SLOT(isServices_selectionChanged(QListViewItem *)));
 
633
        connect(d->pb_servicesDetails, SIGNAL(clicked()), SLOT(servicesDetails()));
 
634
        isServices_selectionChanged(0);
 
635
 
 
636
        connect(d->iss_customRoster, SIGNAL(selectionChanged(QListBoxItem *)), SLOT(isCustom_iconsetSelected(QListBoxItem *)));
 
637
        connect(d->lv_customRoster, SIGNAL(selectionChanged(QListViewItem *)), SLOT(isCustom_selectionChanged(QListViewItem *)));
 
638
        connect(d->pb_customRosterDetails, SIGNAL(clicked()), SLOT(customDetails()));
 
639
        connect(d->le_customRoster, SIGNAL(textChanged(const QString &)), SLOT(isCustom_textChanged()));
 
640
        connect(d->pb_customRosterAdd, SIGNAL(clicked()), SLOT(isCustom_add()));
 
641
        connect(d->pb_customRosterDelete, SIGNAL(clicked()), SLOT(isCustom_delete()));
 
642
        isCustom_selectionChanged(0);
 
643
 
 
644
        QWhatsThis::add(d->ck_useTransportIconsForContacts,
 
645
                tr("Toggles use of transport icons to the contacts, that use that transports."));
 
646
 
 
647
        // TODO: add QWhatsThis
 
648
 
 
649
        return w;
 
650
}
 
651
 
 
652
void OptionsTabIconsetRoster::applyOptions(Options *opt)
 
653
{
 
654
        if ( !w || thread )
 
655
                return;
 
656
 
 
657
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
658
        opt->useTransportIconsForContacts = d->ck_useTransportIconsForContacts->isChecked();
 
659
 
 
660
        // roster - default
 
661
        {
 
662
                const Iconset *is = d->iss_defRoster->iconset();
 
663
                if ( is ) {
 
664
                        QFileInfo fi( is->fileName() );
 
665
                        opt->defaultRosterIconset = fi.fileName();
 
666
                }
 
667
        }
 
668
 
 
669
        // roster - services
 
670
        {
 
671
                opt->serviceRosterIconset.clear();
 
672
 
 
673
                QListViewItemIterator it( d->lv_isServices );
 
674
                for ( ; it.current(); ++it) {
 
675
                        QListViewItem *item = it.current();
 
676
 
 
677
                        opt->serviceRosterIconset[item->text(2)] = item->text(3);
 
678
                }
 
679
        }
 
680
 
 
681
        // roster - custom
 
682
        {
 
683
                opt->customRosterIconset.clear();
 
684
 
 
685
                QListViewItemIterator it( d->lv_customRoster );
 
686
                for ( ; it.current(); ++it) {
 
687
                        QListViewItem *item = it.current();
 
688
 
 
689
                        opt->customRosterIconset[item->text(2)] = item->text(3);
 
690
                }
 
691
        }
 
692
}
 
693
 
 
694
void OptionsTabIconsetRoster::restoreOptions(const Options *opt)
 
695
{
 
696
        if ( !w || thread )
 
697
                return;
 
698
 
 
699
        o = (Options *)opt;
 
700
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
701
        d->ck_useTransportIconsForContacts->setChecked( opt->useTransportIconsForContacts );
 
702
 
 
703
        d->iss_defRoster->clear();
 
704
        d->iss_servicesRoster->clear();
 
705
        d->iss_customRoster->clear();
 
706
 
 
707
        QStringList loaded;
 
708
 
 
709
        d->setCursor(WaitCursor);
 
710
 
 
711
        QPalette customPal = d->palette();
 
712
        customPal.setDisabled(customPal.inactive());
 
713
        d->tabWidget3->setEnabled(false);
 
714
        d->tabWidget3->setPalette(customPal);
 
715
 
 
716
        d->progress->show();
 
717
        d->progress->setProgress( 0 );
 
718
 
 
719
        numIconsets = countIconsets("/roster", loaded);
 
720
        iconsetsLoaded = 0;
 
721
 
 
722
        cancelThread();
 
723
 
 
724
        thread = new IconsetLoadThread(this, "/roster");
 
725
        thread->start();
 
726
}
 
727
 
 
728
bool OptionsTabIconsetRoster::event(QEvent *e)
 
729
{
 
730
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
731
        if ( e->type() == QEvent::User ) { // iconset load event
 
732
                IconsetLoadEvent *le = (IconsetLoadEvent *)e;
 
733
 
 
734
                if ( thread != le->thread() )
 
735
                        return false;
 
736
 
 
737
                if ( !numIconsets )
 
738
                        numIconsets = 1;
 
739
                d->progress->setProgress( (int)((float)100 * ++iconsetsLoaded / numIconsets) );
 
740
 
 
741
                Iconset *i = le->iconset();
 
742
                if ( i ) {
 
743
                        is->stripFirstAnimFrame(i);
 
744
                        QFileInfo fi( i->fileName() );
 
745
 
 
746
                        // roster - default
 
747
                        d->iss_defRoster->insert(*i);
 
748
                        if ( fi.fileName() == o->defaultRosterIconset )
 
749
                                d->iss_defRoster->setSelected(d->iss_defRoster->count()-1, true);
 
750
 
 
751
                        // roster - service
 
752
                        d->iss_servicesRoster->insert(*i);
 
753
 
 
754
                        // roster - custom
 
755
                        d->iss_customRoster->insert(*i);
 
756
 
 
757
                        delete i;
 
758
                }
 
759
 
 
760
                return true;
 
761
        }
 
762
        else if ( e->type() == QEvent::User + 1 ) { // finish event
 
763
                // roster - service
 
764
                {
 
765
                        // fill the QListView
 
766
                        QListViewItemIterator it( d->lv_isServices );
 
767
                        for ( ; it.current(); ++it) {
 
768
                                QListViewItem *i = it.current();
 
769
                                if ( !i->text(2).isEmpty() ) {
 
770
                                        Iconset *iss = is->roster[o->serviceRosterIconset[i->text(2)]];
 
771
                                        if ( iss ) {
 
772
                                                i->setText(1, iss->name());
 
773
 
 
774
                                                QFileInfo fi ( iss->fileName() );
 
775
                                                i->setText(3, fi.fileName());
 
776
                                        }
 
777
                                }
 
778
                        }
 
779
 
 
780
                        QHeader *head = d->lv_isServices->header();
 
781
                        head->removeLabel(2);
 
782
                }
 
783
 
 
784
                // roster - custom
 
785
                {
 
786
                        // Then, fill the QListView
 
787
                        QListViewItem *last = 0;
 
788
                        QMap<QString, QString>::ConstIterator it = o->customRosterIconset.begin();
 
789
                        for ( ; it != o->customRosterIconset.end(); ++it) {
 
790
                                QListViewItem *item = new QListViewItem(d->lv_customRoster, last);
 
791
                                last = item;
 
792
 
 
793
                                item->setText(0, clipCustomText(it.key())); // RegExp
 
794
                                item->setText(2, it.key());
 
795
 
 
796
                                Iconset *iss = is->roster[it.data()];
 
797
                                if ( iss ) {
 
798
                                        item->setText(1, iss->name());
 
799
 
 
800
                                        QFileInfo fi ( iss->fileName() );
 
801
                                        item->setText(3, fi.fileName());
 
802
                                }
 
803
                        }
 
804
                }
 
805
 
 
806
                d->tabWidget3->setEnabled(true);
 
807
                d->tabWidget3->unsetPalette();
 
808
 
 
809
                connect(d->iss_defRoster, SIGNAL(selectionChanged()), SIGNAL(dataChanged()));
 
810
                connect(d->iss_servicesRoster, SIGNAL(selectionChanged()), SIGNAL(dataChanged()));
 
811
                connect(d->iss_customRoster, SIGNAL(selectionChanged()), SIGNAL(dataChanged()));
 
812
 
 
813
                d->progress->hide();
 
814
 
 
815
                d->unsetCursor();
 
816
                thread = 0;
 
817
 
 
818
                return true;
 
819
        }
 
820
 
 
821
        return false;
 
822
}
 
823
 
 
824
void OptionsTabIconsetRoster::setData(PsiCon *, QWidget *p)
 
825
{
 
826
        parentWidget = p;
 
827
}
 
828
 
 
829
void OptionsTabIconsetRoster::defaultDetails()
 
830
{
 
831
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
832
        const Iconset *is = d->iss_defRoster->iconset();
 
833
        if ( is )
 
834
                isDetails(*is, parentWidget);
 
835
}
 
836
 
 
837
void OptionsTabIconsetRoster::servicesDetails()
 
838
{
 
839
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
840
        const Iconset *is = d->iss_servicesRoster->iconset();
 
841
        if ( is )
 
842
                isDetails(*is, parentWidget);
 
843
}
 
844
 
 
845
void OptionsTabIconsetRoster::customDetails()
 
846
{
 
847
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
848
        const Iconset *is = d->iss_customRoster->iconset();
 
849
        if ( is )
 
850
                isDetails(*is, parentWidget);
 
851
}
 
852
 
 
853
//------------------------------------------------------------
 
854
 
 
855
void OptionsTabIconsetRoster::isServices_iconsetSelected(QListBoxItem *item)
 
856
{
 
857
        if ( !item )
 
858
                return;
 
859
 
 
860
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
861
        QListViewItem *it = d->lv_isServices->selectedItem();
 
862
        if ( !it )
 
863
                return;
 
864
 
 
865
        const Iconset *is = ((IconWidgetItem *)item)->iconset();
 
866
        if ( !is )
 
867
                return;
 
868
 
 
869
        it->setText(1, is->name());
 
870
        QFileInfo fi ( is->fileName() );
 
871
        it->setText(3, fi.fileName());
 
872
}
 
873
 
 
874
void OptionsTabIconsetRoster::isServices_selectionChanged(QListViewItem *it)
 
875
{
 
876
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
877
        d->iss_servicesRoster->setEnabled( it != 0 );
 
878
        d->pb_servicesDetails->setEnabled( it != 0 );
 
879
        d->iss_servicesRoster->clearSelection();
 
880
        if ( !it )
 
881
                return;
 
882
 
 
883
        if ( it->text(3).isEmpty() )
 
884
                return;
 
885
 
 
886
        QString name = it->text(3);
 
887
 
 
888
        emit noDirty(true);
 
889
        IconWidgetItem *item = (IconWidgetItem *)d->iss_servicesRoster->firstItem();
 
890
        for ( ; item; item = (IconWidgetItem *)item->next()) {
 
891
                const Iconset *is = item->iconset();
 
892
                if ( is ) {
 
893
                        QFileInfo fi ( is->fileName() );
 
894
                        if ( fi.fileName() == name ) {
 
895
                                emit noDirty(true);
 
896
                                d->iss_servicesRoster->setSelected(item, true);
 
897
                                d->iss_servicesRoster->ensureCurrentVisible();
 
898
                                emit noDirty(false);
 
899
                                break;
 
900
                        }
 
901
                }
 
902
        }
 
903
        qApp->processEvents();
 
904
        emit noDirty(false);
 
905
}
 
906
 
 
907
//------------------------------------------------------------
 
908
 
 
909
void OptionsTabIconsetRoster::isCustom_iconsetSelected(QListBoxItem *item)
 
910
{
 
911
        if ( !item )
 
912
                return;
 
913
 
 
914
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
915
        QListViewItem *it = d->lv_customRoster->selectedItem();
 
916
        if ( !it )
 
917
                return;
 
918
 
 
919
        const Iconset *is = ((IconWidgetItem *)item)->iconset();
 
920
        if ( !is )
 
921
                return;
 
922
 
 
923
        it->setText(1, is->name());
 
924
        QFileInfo fi ( is->fileName() );
 
925
        it->setText(3, fi.fileName());
 
926
}
 
927
 
 
928
void OptionsTabIconsetRoster::isCustom_selectionChanged(QListViewItem *it)
 
929
{
 
930
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
931
        d->iss_customRoster->setEnabled( it != 0 );
 
932
        d->pb_customRosterDetails->setEnabled( it != 0 );
 
933
        //d->pb_customRosterAdd->setEnabled( it != 0 );
 
934
        d->pb_customRosterDelete->setEnabled( it != 0 );
 
935
        d->le_customRoster->setEnabled( it != 0 );
 
936
        d->iss_customRoster->clearSelection();
 
937
        if ( !it )
 
938
                return;
 
939
 
 
940
        if ( it->text(3).isEmpty() )
 
941
                return;
 
942
 
 
943
        emit noDirty(true);
 
944
        d->le_customRoster->setText(it->text(2));
 
945
        QString name = it->text(3);
 
946
 
 
947
        IconWidgetItem *item = (IconWidgetItem *)d->iss_customRoster->firstItem();
 
948
        for ( ; item; item = (IconWidgetItem *)item->next()) {
 
949
                const Iconset *is = item->iconset();
 
950
                if ( is ) {
 
951
                        QFileInfo fi ( is->fileName() );
 
952
                        if ( fi.fileName() == name ) {
 
953
                                d->iss_customRoster->setSelected(item, true);
 
954
                                d->iss_customRoster->ensureCurrentVisible();
 
955
                                break;
 
956
                        }
 
957
                }
 
958
        }
 
959
        qApp->processEvents();
 
960
        emit noDirty(false);
 
961
}
 
962
 
 
963
void OptionsTabIconsetRoster::isCustom_textChanged()
 
964
{
 
965
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
966
        QListViewItem *item = d->lv_customRoster->selectedItem();
 
967
        if ( !item )
 
968
                return;
 
969
 
 
970
        item->setText( 0, clipCustomText(d->le_customRoster->text()) );
 
971
        item->setText( 2, d->le_customRoster->text() );
 
972
}
 
973
 
 
974
void OptionsTabIconsetRoster::isCustom_add()
 
975
{
 
976
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
977
 
 
978
        QString def;
 
979
        const Iconset *iconset = d->iss_defRoster->iconset();
 
980
        if ( is ) {
 
981
                QFileInfo fi( iconset->fileName() );
 
982
                def = fi.fileName();
 
983
        }
 
984
        else
 
985
                qWarning("OptionsTabIconsetRoster::isCustom_add(): 'def' is null!");
 
986
 
 
987
        QListViewItem *item = new QListViewItem(d->lv_customRoster, d->lv_customRoster->lastItem());
 
988
        const Iconset *i = is->roster[def];
 
989
        if ( i ) {
 
990
                item->setText(1, i->name());
 
991
 
 
992
                QFileInfo fi(i->fileName());
 
993
                item->setText(3, fi.fileName());
 
994
        }
 
995
 
 
996
        d->lv_customRoster->setSelected(item, true);
 
997
        emit dataChanged();
 
998
 
 
999
        d->le_customRoster->setFocus();
 
1000
}
 
1001
 
 
1002
void OptionsTabIconsetRoster::isCustom_delete()
 
1003
{
 
1004
        IconsetRosterUI *d = (IconsetRosterUI *)w;
 
1005
        QListViewItem *item = d->lv_customRoster->selectedItem();
 
1006
        if ( !item )
 
1007
                return;
 
1008
 
 
1009
        delete item;
 
1010
        isCustom_selectionChanged(0);
 
1011
        emit dataChanged();
 
1012
}
 
1013
 
 
1014
QString OptionsTabIconsetRoster::clipCustomText(QString s)
 
1015
{
 
1016
        if ( s.length() > 10 ) {
 
1017
                s = s.left(9);
 
1018
                s += "...";
 
1019
        }
 
1020
 
 
1021
        return s;
 
1022
}
 
1023
 
 
1024
void OptionsTabIconsetRoster::cancelThread()
 
1025
{
 
1026
        if ( thread ) {
 
1027
                threadCancelled.lock();
 
1028
                thread->cancelled = true;
 
1029
                threadCancelled.unlock();
 
1030
 
 
1031
                thread = 0;
 
1032
        }
 
1033
}