File: preferences.cpp

package info (click to toggle)
konversation 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 19,956 kB
  • sloc: cpp: 46,035; python: 554; xml: 481; perl: 455; sh: 126; makefile: 12
file content (636 lines) | stat: -rw-r--r-- 19,294 bytes parent folder | download | duplicates (2)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
  This program is free software; you can redistribute it and/or self()->modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  Copyright (C) 2002 Dario Abatianni <eisfuchs@tigress.com>
  Copyright (C) 2005 Ismail Donmez <ismail@kde.org>
  Copyright (C) 2005 Peter Simonsson <psn@linux.se>
  Copyright (C) 2005 John Tapsell <johnflux@gmail.com>
  Copyright (C) 2005-2008 Eike Hein <hein@kde.org>
*/

#include "config/preferences.h"
#include "ignore.h"
#include "highlight.h"

#include <QFileInfo>
#include <QHashIterator>
#include <QHeaderView>
#include <QTreeView>

#include <KLocalizedString>
#include <KUser>
#include <KSharedConfig>
#include <QStandardPaths>

struct PreferencesSingleton
{
    Preferences prefs;
};

Q_GLOBAL_STATIC(PreferencesSingleton, s_prefs)

Preferences *Preferences::self()
{
  if ( !s_prefs.exists() ) {
    s_prefs->prefs.load();
  }

  return &s_prefs->prefs;
}


Preferences::Preferences()
{
    // create default identity
    mIdentity=new Identity();
    mIdentity->setName(i18n("Default Identity"));
    mIdentityList.append(mIdentity);

    KUser user(KUser::UseRealUserID);
    mIdentity->setIdent(user.loginName());
    mIdentity->setRealName(user.property(KUser::FullName).toString());

    QStringList nickList;
    nickList.append(user.loginName());
    nickList.append(user.loginName() + QLatin1Char('_'));
    nickList.append(user.loginName() + QStringLiteral("__"));
    mIdentity->setNicknameList(nickList);

    Konversation::ServerGroupSettingsPtr serverGroup(new Konversation::ServerGroupSettings);
    serverGroup->setName(QStringLiteral("freenode"));
    Konversation::ServerSettings server;
    server.setHost(QStringLiteral("irc.debian.org"));
    server.setPort(7000);
    serverGroup->addServer(server);
    serverGroup->setIdentityId(mIdentity->id());
    Konversation::ChannelSettings channel;
    channel.setName(QStringLiteral("#debian"));
    serverGroup->addChannel(channel);
    channel.setName(QStringLiteral("#debian-kde"));
    serverGroup->addChannel(channel);
    serverGroup->setExpanded(false);
    mServerGroupHash.insert(0, serverGroup);
    mQuickButtonList = defaultQuickButtonList();
    mAutoreplaceList = defaultAutoreplaceList();
}

Preferences::~Preferences()
{
    mIdentityList.clear();
    qDeleteAll(mIgnoreList);
    qDeleteAll(mHighlightList);
}
const Konversation::ServerGroupHash Preferences::serverGroupHash()
{
    return self()->mServerGroupHash;
}

const QStringList Preferences::defaultQuickButtonList()
{
    return QStringList() << QStringLiteral("Op,/OP %u%n")
                         << QStringLiteral("DeOp,/DEOP %u%n")
                         << QStringLiteral("WhoIs,/WHOIS %s,%%u%n")
                         << QStringLiteral("Version,/CTCP %s,%%u VERSION%n")
                         << QStringLiteral("Kick,/KICK %u%n")
                         << QStringLiteral("Ban,/BAN %u%n")
                         << QStringLiteral("Part,/PART %c Leaving...%n")
                         << QStringLiteral("Quit,/QUIT Leaving...%n");
}

const QStringList Preferences::quickButtonList()
{
  return self()->mQuickButtonList;
}

void Preferences::setQuickButtonList(const QStringList& newList)
{
  self()->mQuickButtonList=newList;
}

void Preferences::clearQuickButtonList()
{
  self()->mQuickButtonList.clear();
}

// --------------------------- AutoReplace ---------------------------

const QList<QStringList> Preferences::defaultAutoreplaceList()
{
    QList<QStringList> defaultList;
    defaultList.append(QStringList() << QStringLiteral("1") << QStringLiteral("o") << QStringLiteral("\\[\\[([^\\s]+)\\]\\]") << QStringLiteral("http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%1"));
    defaultList.append(QStringList() << QStringLiteral("1") << QStringLiteral("o") << QStringLiteral("(BUG:|bug:)([0-9]+)") << QStringLiteral("https://bugs.kde.org/show_bug.cgi?id=%2"));
    defaultList.append(QStringList() << QStringLiteral("1") << QStringLiteral("o") << QStringLiteral("([Dd][Bb][Uu][Gg]:)(\\w+)") << QStringLiteral("https://bugs.debian.org/%2"));
    return defaultList;
}

const QList<QStringList> Preferences::autoreplaceList()
{
  return self()->mAutoreplaceList;
}

void Preferences::setAutoreplaceList(const QList<QStringList> newList)
{
  self()->mAutoreplaceList=newList;
}

void Preferences::clearAutoreplaceList()
{
  self()->mAutoreplaceList.clear();
}

// --------------------------- AutoReplace ---------------------------

void Preferences::setServerGroupHash(const Konversation::ServerGroupHash& hash)
{
    self()->mServerGroupHash.clear();
    self()->mServerGroupHash = hash;
}

void Preferences::addServerGroup(Konversation::ServerGroupSettingsPtr serverGroup)
{
    Konversation::ServerGroupHash hash = self()->mServerGroupHash;
    hash.insert(serverGroup->id(), serverGroup);
    self()->mServerGroupHash = hash;
}

const Konversation::ServerGroupSettingsPtr Preferences::serverGroupById(int id)
{
    return  self()->mServerGroupHash.value(id);
}

const QList<Konversation::ServerGroupSettingsPtr> Preferences::serverGroupsByServer(const QString& server)
{
    QList<Konversation::ServerGroupSettingsPtr> serverGroups;
    QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
    while(it.hasNext())
    {
        it.next();
        for (int i=0; i != it.value()->serverList().count(); i++)
        {
            if(it.value()->serverByIndex(i).host().toLower() == server)
                serverGroups.append(it.value());
        }
    }
    return serverGroups;
}

QList<int> Preferences::serverGroupIdsByName(const QString& serverGroup)
{
    QList<int> serverIds;
    QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
    while(it.hasNext())
    {
        if(it.next().value()->name().toLower() == serverGroup.toLower())
            serverIds.append(it.key());
    }
    if (serverIds.isEmpty())
        serverIds.append(-1);

    return serverIds;
}

bool Preferences::isServerGroup(const QString& server)
{
    QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
    while(it.hasNext())
    {
        if(it.next().value()->name().toLower() == server.toLower())
            return true;
    }
    return false;
}

void Preferences::removeServerGroup(int id)
{
    self()->mServerGroupHash.remove(id);
}


const QList<Highlight*> Preferences::highlightList()
{
    return self()->mHighlightList;
}

void Preferences::setHighlightList(QList<Highlight*> newList)
{
    qDeleteAll(self()->mHighlightList);
    self()->mHighlightList.clear();
    self()->mHighlightList=newList;
}

void Preferences::addHighlight(const QString& highlight, bool regExp, const QColor& color,
    const QString& soundURL, const QString& autoText, const QString& chatWindows, bool notify)
{
    self()->mHighlightList.append(new Highlight(highlight, regExp, color,
        QUrl(soundURL), autoText, chatWindows, notify));
}

void Preferences::setIgnoreList(QList<Ignore*> newList)
{
    clearIgnoreList();
    self()->mIgnoreList=newList;
}

void Preferences::addIgnore(const QString &newIgnore)
{
    QStringList ignore = newIgnore.split(QLatin1Char(','));
    self()->mIgnoreList.append(new Ignore(ignore[0],ignore[1].toInt()));
}

bool Preferences::removeIgnore(const QString &oldIgnore)
{
    foreach (Ignore *ignore, self()->mIgnoreList)
    {
        if (ignore->getName().toLower() == oldIgnore.toLower())
        {
            self()->mIgnoreList.removeOne(ignore);
            delete ignore;
            return true;
        }
    }

    return false;
}

bool Preferences::isIgnored(const QString &nickname)
{
    foreach (Ignore *ignore, self()->mIgnoreList)
    {
        if (ignore->getName().section(QLatin1Char('!'),0,0).toLower()==nickname.toLower())
        {
            return true;
        }
    }

    return false;
}

void Preferences::setNotifyList(const QMap<int, QStringList> &newList)
{ self()->mNotifyList=newList; }

const QMap<int, QStringList> Preferences::notifyList() { return self()->mNotifyList; }

const QStringList Preferences::notifyListByGroupId(int serverGroupId)
{
    return self()->mNotifyList.value(serverGroupId);
}

const QString Preferences::notifyStringByGroupId(int serverGroupId)
{
    return notifyListByGroupId(serverGroupId).join(QStringLiteral(" "));
}

bool Preferences::addNotify(int serverGroupId, const QString& newPattern)
{
    QStringList& list = self()->mNotifyList[serverGroupId];

    if (!list.contains(newPattern, Qt::CaseInsensitive))
    {
        list.append(newPattern);

        if (list.size() == 1)
            emit self()->notifyListStarted(serverGroupId);

        return true;
    }

    return false;
}

bool Preferences::removeNotify(int serverGroupId, const QString& pattern)
{
    if (self()->mNotifyList.contains(serverGroupId))
    {
        QString lowered = pattern.toLower();
        QStringList& oldList = self()->mNotifyList[serverGroupId];
        QStringList newList;

        for (int i = 0; i < oldList.size(); ++i)
        {
            const QString& nick = oldList[i];

            if (nick.toLower() != lowered)
                newList << nick;
        }

        if (newList.size() < oldList.size())
        {
            if (newList.isEmpty())
                self()->mNotifyList.remove(serverGroupId);
            else
                self()->mNotifyList[serverGroupId] = newList;

            return true;
        }
    }

    return false;
}

bool Preferences::isNotify(int serverGroupId, const QString& pattern)
{
    if (self()->mNotifyList.contains(serverGroupId))
        if (self()->mNotifyList.value(serverGroupId).contains(pattern, Qt::CaseInsensitive))
            return true;

    return false;
}

// Default identity functions
void Preferences::addIdentity(IdentityPtr identity) { self()->mIdentityList.append(identity); }
void Preferences::removeIdentity(IdentityPtr identity) { self()->mIdentityList.removeOne(identity); }

void Preferences::clearIdentityList()
{
    self()->mIdentityList.clear();
}

const IdentityList Preferences::identityList() { return self()->mIdentityList; }

void Preferences::setIdentityList(const IdentityList& list)
{
    self()->mIdentityList.clear();
    self()->mIdentityList = list;
}

const IdentityPtr Preferences::identityByName(const QString& name)
{
    QList<IdentityPtr> identities = identityList();
    QList<IdentityPtr>::iterator it = identities.begin();

    while(it != identities.end())
    {
        if((*it)->getName() == name)
        {
            return (*it);
        }

        ++it;
    }

    // no self()->matching identity found, return default identity
    return identities.first();
}

const IdentityPtr Preferences::identityById(int id)
{
    QList<IdentityPtr> identList = identityList();
    for(QList<IdentityPtr>::iterator it = identList.begin(); it != identList.end(); ++it)
    {
        if((*it)->id() == id)
        {
            return (*it);
        }
    }

    return identList.first();
}

QStringList Preferences::defaultAliasList()
{
    // Auto-alias scripts
    const QStringList scriptDirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("scripts"), QStandardPaths::LocateDirectory);
    QSet<QString> scripts;

    foreach(const QString &dir, scriptDirs) {

        const QStringList &scriptFiles = QDir(dir).entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::Executable);

        foreach(const QString &script, scriptFiles) {
            scripts << script;
        }
    }

    QStringList aliasList;

    foreach(const QString& script, scripts)
    {
        aliasList.append(QString("%1 /exec %1").arg(script));

        // FIXME: Historically, defaultAliasList() is primarily used to dynamically
        // compile a list of installed scripts and generate appropriate aliases for
        // them. It's not only used when the alias preferences are reset or initia-
        // lized, but also on application start. The following crudely adds two
        // aliases when the 'media' script is found, to provide easy access to its
        // capability to differentiate  between audio and video media. This method
        // needs at the very least to be split up in two, or scripts may in the
        // future determine what aliases they want to add.
        if (script == QStringLiteral("media"))
        {
            aliasList.append(QStringLiteral("audio /exec media audio"));
            aliasList.append(QStringLiteral("video /exec media video"));
        }
    }

    return aliasList;
}

void Preferences::clearIgnoreList() { qDeleteAll(self()->mIgnoreList); self()->mIgnoreList.clear(); }
const QList<Ignore*> Preferences::ignoreList() { return self()->mIgnoreList; }

void Preferences::setShowTrayIcon(bool state)
{
    self()->PreferencesBase::setShowTrayIcon(state);
    emit self()->updateTrayIcon();
}

void Preferences::setTrayNotify(bool state)
{
    self()->PreferencesBase::setTrayNotify(state);
    emit self()->updateTrayIcon();
}


void Preferences::setAutoUserhost(bool state)
{
    self()->PreferencesBase::setAutoUserhost(state);
}

bool Preferences::dialogFlag(const QString& flagName)
{
    KConfigGroup config(KSharedConfig::openConfig()->group("Notification self()->Messages"));

    if (!config.readEntry(flagName).isEmpty())
        return false;
    else
        return true;
}

void Preferences::setDialogFlag(const QString& flagName,bool state)
{
    KConfigGroup config(KSharedConfig::openConfig()->group("Notification self()->Messages"));

    if (state)
        config.deleteEntry(flagName);
    else
    {
        if (config.readEntry(flagName).isEmpty())
            config.writeEntry(flagName,"no");
    }

    config.sync();
}


// Channel Encodings
const QString Preferences::channelEncoding(const QString& server,const QString& channel)
{
    //check all matching server's encodings
    //TODO when we rewrite dbus, allow for multiple encodings to be returned
    //for true 'duplicity compatibility'
    QList<int> serverIds = serverGroupIdsByName(server);
    QString codec;
    if(serverIds.count() > 1)
    {
        for (int i=0; i < serverIds.count(); i++)
        {
            codec = channelEncoding(serverIds.at(i),channel);
            if(!codec.isEmpty())
                return codec;
        }
        return QString();
    }
    else
        return channelEncoding(serverIds.first(),channel);
}

const QString Preferences::channelEncoding(int serverGroupId,const QString& channel)
{
    if(self()->mChannelEncodingsMap.contains(serverGroupId))
        if(self()->mChannelEncodingsMap[serverGroupId].contains(channel.toLower()))
            return self()->mChannelEncodingsMap[serverGroupId][channel.toLower()];
    return QString();
}

void Preferences::setChannelEncoding(const QString& server,const QString& channel,const QString& encoding)
{
    //set channel encoding for ALL matching servergroups
    QList<int> serverIds = serverGroupIdsByName(server);
    if(serverIds.count() > 1)
    {
        for(int i=0; i < serverIds.count(); i++)
            setChannelEncoding(serverIds.at(i),channel,encoding);
    }
    else
        setChannelEncoding(serverIds.first(),channel,encoding);
}

void Preferences::setChannelEncoding(int serverGroupId,const QString& channel,const QString& encoding)
{
    self()->mChannelEncodingsMap[serverGroupId][channel.toLower()]=encoding;
}

const QList<int> Preferences::channelEncodingsServerGroupIdList()
{
    return self()->mChannelEncodingsMap.keys();
}

const QStringList Preferences::channelEncodingsChannelList(int serverGroupId)
{
    return self()->mChannelEncodingsMap[serverGroupId].keys();
}

const QString Preferences::spellCheckingLanguage(Konversation::ServerGroupSettingsPtr serverGroup, const QString& key)
{
    if (self()->mServerGroupSpellCheckingLanguages.contains(serverGroup))
        return self()->mServerGroupSpellCheckingLanguages.value(serverGroup).value(key);

    return QString();
}

const QString Preferences::spellCheckingLanguage(const QString& server, const QString& key)
{
    if (self()->mServerSpellCheckingLanguages.contains(server))
        return self()->mServerSpellCheckingLanguages.value(server).value(key);

    return QString();
}

void Preferences::setSpellCheckingLanguage(Konversation::ServerGroupSettingsPtr serverGroup, const QString& key, const QString& language)
{
    QHash<QString, QString> languageHash;

    if (self()->mServerGroupSpellCheckingLanguages.contains(serverGroup))
        languageHash = self()->mServerGroupSpellCheckingLanguages.value(serverGroup);

    languageHash.insert(key, language);

    self()->mServerGroupSpellCheckingLanguages.insert(serverGroup, languageHash);
}

void Preferences::setSpellCheckingLanguage(const QString& server, const QString& key, const QString& language)
{
    QHash<QString, QString> languageHash;

    if (self()->mServerSpellCheckingLanguages.contains(server))
        languageHash = self()->mServerSpellCheckingLanguages.value(server);

    languageHash.insert(key, language);

    self()->mServerSpellCheckingLanguages.insert(server, languageHash);
}

const QHash< Konversation::ServerGroupSettingsPtr, QHash< QString, QString > > Preferences::serverGroupSpellCheckingLanguages()
{
    return self()->mServerGroupSpellCheckingLanguages;
}

const QHash< QString, QHash< QString, QString > > Preferences::serverSpellCheckingLanguages()
{
    return self()->mServerSpellCheckingLanguages;
}

const QString Preferences::defaultNicknameSortingOrder()
{
  return QStringLiteral("qpohv-");
}

// override to add %u if needed
QString Preferences::webBrowserCmd()
{
  // add %u to command if it's not in there
  QString cmd=self()->mWebBrowserCmd;
  if (!cmd.contains(QStringLiteral("%u")))
      cmd += QStringLiteral(" %u");
  return cmd;
}

void Preferences::saveColumnState(QTreeView *treeView, QString name)
{
    KConfigGroup group(KSharedConfig::openConfig(), name);

    QList<int> columnWidths;
    for (int i = 0; i < treeView->header()->count(); ++i)
        columnWidths.append(treeView->columnWidth(i));
    // save column widths
    group.writeEntry("ColumnWidths", columnWidths);
    group.writeEntry("ColumnSorted", treeView->header()->sortIndicatorSection());
    group.writeEntry("ColumnSortDescending", treeView->header()->sortIndicatorOrder() == Qt::DescendingOrder ? true : false );
}

void Preferences::restoreColumnState(QTreeView* treeView, QString name, int defaultColumn , Qt::SortOrder defaultSortOrder)
{
    KConfigGroup group(KSharedConfig::openConfig(), name);

    QList<int> columnWidths = group.readEntry("ColumnWidths", QList<int>());
    for (int i = 0; i < columnWidths.count(); ++i)
        if (columnWidths.at(i))
            treeView->setColumnWidth(i, columnWidths.at(i));

    if (group.readEntry("ColumnSortDescending", (defaultSortOrder == Qt::DescendingOrder)))
        treeView->header()->setSortIndicator(group.readEntry("ColumnSorted", defaultColumn), Qt::DescendingOrder);
    else
        treeView->header()->setSortIndicator(group.readEntry("ColumnSorted", defaultColumn), Qt::AscendingOrder);
}

void Preferences::slotSetUseOSD(bool use)
{
    self()->setUseOSD(use);
}