2
This program is free software; you can redistribute it and/or self()->modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation; either version 2 of the License, or
5
(at your option) any later version.
9
Copyright (C) 2002 Dario Abatianni <eisfuchs@tigress.com>
10
Copyright (C) 2005 Ismail Donmez <ismail@kde.org>
11
Copyright (C) 2005 Peter Simonsson <psn@linux.se>
12
Copyright (C) 2005 John Tapsell <johnflux@gmail.com>
13
Copyright (C) 2005-2008 Eike Hein <hein@kde.org>
16
#include "config/preferences.h"
18
#include "highlight.h"
21
#include <QHashIterator>
22
#include <QHeaderView>
27
#include <KStandardDirs>
29
struct PreferencesSingleton
34
K_GLOBAL_STATIC(PreferencesSingleton, s_prefs)
36
Preferences *Preferences::self()
38
if ( !s_prefs.exists() ) {
39
s_prefs->prefs.readConfig();
42
return &s_prefs->prefs;
46
Preferences::Preferences()
48
// create default identity
49
mIdentity=new Identity();
50
mIdentity->setName(i18n("Default Identity"));
51
mIdentityList.append(mIdentity);
53
KUser user(KUser::UseRealUserID);
54
mIdentity->setIdent(user.loginName());
55
mIdentity->setRealName(user.property(KUser::FullName).toString());
58
nickList.append(user.loginName());
59
nickList.append(user.loginName() + '_');
60
nickList.append(user.loginName() + "__");
61
mIdentity->setNicknameList(nickList);
63
Konversation::ServerGroupSettingsPtr serverGroup(new Konversation::ServerGroupSettings);
64
serverGroup->setName("Ubuntu IRC");
65
Konversation::ServerSettings server;
66
server.setHost("irc.ubuntu.com");
68
serverGroup->addServer(server);
69
serverGroup->setIdentityId(mIdentity->id());
70
Konversation::ChannelSettings channel;
71
channel.setName("#kubuntu");
72
serverGroup->addChannel(channel);
73
serverGroup->setExpanded(false);
74
mServerGroupHash.insert(0, serverGroup);
75
mQuickButtonList = defaultQuickButtonList();
76
mAutoreplaceList = defaultAutoreplaceList();
79
Preferences::~Preferences()
81
mIdentityList.clear();
82
qDeleteAll(mIgnoreList);
83
qDeleteAll(mHighlightList);
85
const Konversation::ServerGroupHash Preferences::serverGroupHash()
87
return self()->mServerGroupHash;
90
const QStringList Preferences::defaultQuickButtonList()
92
return QStringList() << "Op,/OP %u%n"
94
<< "WhoIs,/WHOIS %s,%%u%n"
95
<< "Version,/CTCP %s,%%u VERSION%n"
98
<< "Part,/PART %c Leaving...%n"
99
<< "Quit,/QUIT Leaving...%n";
102
const QStringList Preferences::quickButtonList()
104
return self()->mQuickButtonList;
107
void Preferences::setQuickButtonList(const QStringList newList)
109
self()->mQuickButtonList=newList;
112
void Preferences::clearQuickButtonList()
114
self()->mQuickButtonList.clear();
117
// --------------------------- AutoReplace ---------------------------
119
const QList<QStringList> Preferences::defaultAutoreplaceList()
121
QList<QStringList> defaultList;
122
defaultList.append(QStringList() << "1" << "o" << "\\[\\[([^\\s]+)\\]\\]" << "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=%1");
123
defaultList.append(QStringList() << "1" << "o" << "(BUG:|bug:)([0-9]+)" << "https://bugs.kde.org/show_bug.cgi?id=%2");
124
defaultList.append(QStringList() << "1" << "o" << "([Dd][Bb][Uu][Gg]:)(\\w+)" << "http://bugs.debian.org/%2");
128
const QList<QStringList> Preferences::autoreplaceList()
130
return self()->mAutoreplaceList;
133
void Preferences::setAutoreplaceList(const QList<QStringList> newList)
135
self()->mAutoreplaceList=newList;
138
void Preferences::clearAutoreplaceList()
140
self()->mAutoreplaceList.clear();
143
// --------------------------- AutoReplace ---------------------------
145
void Preferences::setServerGroupHash(const Konversation::ServerGroupHash& hash)
147
self()->mServerGroupHash.clear();
148
self()->mServerGroupHash = hash;
151
void Preferences::addServerGroup(Konversation::ServerGroupSettingsPtr serverGroup)
153
Konversation::ServerGroupHash hash = self()->mServerGroupHash;
154
hash.insert(serverGroup->id(), serverGroup);
155
self()->mServerGroupHash = hash;
158
const Konversation::ServerGroupSettingsPtr Preferences::serverGroupById(int id)
160
return self()->mServerGroupHash.value(id);
163
const QList<Konversation::ServerGroupSettingsPtr> Preferences::serverGroupsByServer(const QString& server)
165
QList<Konversation::ServerGroupSettingsPtr> serverGroups;
166
QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
170
for (int i=0; i != it.value()->serverList().count(); i++)
172
if(it.value()->serverByIndex(i).host().toLower() == server)
173
serverGroups.append(it.value());
179
QList<int> Preferences::serverGroupIdsByName(const QString& serverGroup)
181
QList<int> serverIds;
182
QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
185
if(it.next().value()->name().toLower() == serverGroup.toLower())
186
serverIds.append(it.key());
188
if (serverIds.isEmpty())
189
serverIds.append(-1);
194
bool Preferences::isServerGroup(const QString& server)
196
QHashIterator<int, Konversation::ServerGroupSettingsPtr> it(self()->mServerGroupHash);
199
if(it.next().value()->name().toLower() == server.toLower())
205
void Preferences::removeServerGroup(int id)
207
self()->mServerGroupHash.remove(id);
211
const QList<Highlight*> Preferences::highlightList()
213
return self()->mHighlightList;
216
void Preferences::setHighlightList(QList<Highlight*> newList)
218
qDeleteAll(self()->mHighlightList);
219
self()->mHighlightList.clear();
220
self()->mHighlightList=newList;
223
void Preferences::addHighlight(const QString& newHighlight,
225
const QColor &newColor,
226
const QString& sound,
227
const QString& autoText)
229
self()->mHighlightList.append(new Highlight(newHighlight,regExp,newColor,KUrl(sound),autoText));
232
void Preferences::setIgnoreList(QList<Ignore*> newList)
235
self()->mIgnoreList=newList;
238
void Preferences::addIgnore(const QString &newIgnore)
240
QStringList ignore = newIgnore.split(',');
241
removeIgnore(ignore[0]);
242
self()->mIgnoreList.append(new Ignore(ignore[0],ignore[1].toInt()));
245
bool Preferences::removeIgnore(const QString &oldIgnore)
247
foreach (Ignore *ignore, self()->mIgnoreList)
249
if (ignore->getName().toLower() == oldIgnore.toLower())
251
self()->mIgnoreList.removeOne(ignore);
260
bool Preferences::isIgnored(const QString &nickname)
262
foreach (Ignore *ignore, self()->mIgnoreList)
264
if (ignore->getName().section('!',0,0).toLower()==nickname.toLower())
273
void Preferences::setNotifyList(const QMap<int, QStringList> &newList)
274
{ self()->mNotifyList=newList; }
276
const QMap<int, QStringList> Preferences::notifyList() { return self()->mNotifyList; }
278
const QStringList Preferences::notifyListByGroupId(int serverGroupId)
280
if (serverGroupId && self()->mNotifyList.find(serverGroupId) != self()->mNotifyList.end())
281
return self()->mNotifyList.value(serverGroupId);
283
return QStringList();
286
const QString Preferences::notifyStringByGroupId(int serverGroupId)
288
return notifyListByGroupId(serverGroupId).join(" ");
291
bool Preferences::addNotify(int serverGroupId, const QString& newPattern)
293
if (!self()->mNotifyList[serverGroupId].contains(newPattern))
295
QStringList nicknameList = self()->mNotifyList[serverGroupId];
296
nicknameList.append(newPattern);
297
self()->mNotifyList[serverGroupId] = nicknameList;
303
bool Preferences::removeNotify(int serverGroupId, const QString& pattern)
305
if (self()->mNotifyList.find(serverGroupId) != self()->mNotifyList.end())
307
QStringList nicknameList = self()->mNotifyList[serverGroupId];
308
nicknameList.removeAll(pattern); //FIXME: what semantics do you really want here?
309
if (nicknameList.isEmpty())
310
self()->mNotifyList.remove(serverGroupId);
312
self()->mNotifyList[serverGroupId] = nicknameList;
318
bool Preferences::isNotify(int serverGroupId, const QString& pattern)
320
if (self()->mNotifyList.find(serverGroupId) != self()->mNotifyList.end())
322
QStringList nicknameList = self()->mNotifyList[serverGroupId];
324
if (nicknameList.contains(pattern)) return true;
329
bool Preferences::hasNotifyList(int serverGroupId)
331
if (self()->mNotifyList.find(serverGroupId) != self()->mNotifyList.end())
337
// Default identity functions
338
void Preferences::addIdentity(IdentityPtr identity) { self()->mIdentityList.append(identity); }
339
void Preferences::removeIdentity(IdentityPtr identity) { self()->mIdentityList.removeOne(identity); }
341
void Preferences::clearIdentityList()
343
self()->mIdentityList.clear();
346
const IdentityList Preferences::identityList() { return self()->mIdentityList; }
348
void Preferences::setIdentityList(const IdentityList& list)
350
self()->mIdentityList.clear();
351
self()->mIdentityList = list;
354
const IdentityPtr Preferences::identityByName(const QString& name)
356
QList<IdentityPtr> identities = identityList();
357
QList<IdentityPtr>::iterator it = identities.begin();
359
while(it != identities.end())
361
if((*it)->getName() == name)
369
// no self()->matching identity found, return default identity
370
return identities.first();
373
const IdentityPtr Preferences::identityById(int id)
375
QList<IdentityPtr> identList = identityList();
376
for(QList<IdentityPtr>::iterator it = identList.begin(); it != identList.end(); ++it)
378
if((*it)->id() == id)
384
return identList.first();
387
QStringList Preferences::defaultAliasList()
389
// Auto-alias scripts
390
const QStringList scripts = KGlobal::dirs()->findAllResources("data","konversation/scripts/*");
391
QFileInfo* fileInfo = new QFileInfo();
392
QStringList aliasList;
395
for (QStringList::ConstIterator it = scripts.constBegin(); it != scripts.constEnd(); ++it)
397
fileInfo->setFile(*it);
398
if (fileInfo->isExecutable())
400
newAlias = (*it).section('/',-1)+' '+"/exec "+(*it).section('/', -1 );
401
aliasList.append(newAlias);
403
// FIXME: Historically, defaultAliasList() is primarily used to dynamically
404
// compile a list of installed scripts and generate appropriate aliases for
405
// them. It's not only used when the alias preferences are reset or initia-
406
// lized, but also on application start. The following crudely adds two
407
// aliases when the 'media' script is found, to provide easy access to its
408
// capability to differentiate between audio and video media. This method
409
// needs at the very least to be split up in two, or scripts may in the
410
// future determine what aliases they want to add.
411
if ((*it).section('/',-1) == "media")
413
aliasList.append("audio /exec media audio");
414
aliasList.append("video /exec media video");
425
const QString Preferences::realName() { return self()->mIdentityList[0]->getRealName(); }
426
void Preferences::setRealName(const QString &name) { self()->mIdentityList[0]->setRealName(name); }
428
const QString Preferences::ident() { return self()->mIdentityList[0]->getIdent(); }
429
void Preferences::setIdent(const QString &ident) { self()->mIdentityList[0]->setIdent(ident); }
431
const QString Preferences::partReason() { return self()->mIdentityList[0]->getPartReason(); }
432
void Preferences::setPartReason(const QString &newReason) { self()->mIdentityList[0]->setPartReason(newReason); }
434
const QString Preferences::kickReason() { return self()->mIdentityList[0]->getKickReason(); }
435
void Preferences::setKickReason(const QString &newReason) { self()->mIdentityList[0]->setKickReason(newReason); }
437
bool Preferences::showAwayMessage() { return self()->mIdentityList[0]->getShowAwayMessage(); }
438
void Preferences::setShowAwayMessage(bool state) { self()->mIdentityList[0]->setShowAwayMessage(state); }
440
const QString Preferences::awayMessage() { return self()->mIdentityList[0]->getAwayMessage(); }
441
void Preferences::setAwayMessage(const QString &newMessage) { self()->mIdentityList[0]->setAwayMessage(newMessage); }
442
const QString Preferences::unAwayMessage() { return self()->mIdentityList[0]->getReturnMessage(); }
443
void Preferences::setUnAwayMessage(const QString &newMessage) { self()->mIdentityList[0]->setReturnMessage(newMessage); }
445
void Preferences::clearIgnoreList() { qDeleteAll(self()->mIgnoreList); self()->mIgnoreList.clear(); }
446
const QList<Ignore*> Preferences::ignoreList() { return self()->mIgnoreList; }
448
const QString Preferences::nickname(int index) { return self()->mIdentityList[0]->getNickname(index); }
449
const QStringList Preferences::nicknameList() { return self()->mIdentityList[0]->getNicknameList(); }
450
void Preferences::setNickname(int index,const QString &newName) { self()->mIdentityList[0]->setNickname(index,newName); }
451
void Preferences::setNicknameList(const QStringList &newList) { self()->mIdentityList[0]->setNicknameList(newList); }
453
void Preferences::setShowTrayIcon(bool state)
455
self()->PreferencesBase::setShowTrayIcon(state);
456
emit self()->updateTrayIcon();
459
void Preferences::setTrayNotify(bool state)
461
self()->PreferencesBase::setTrayNotify(state);
462
emit self()->updateTrayIcon();
466
void Preferences::setAutoUserhost(bool state)
468
self()->PreferencesBase::setAutoUserhost(state);
471
bool Preferences::dialogFlag(const QString& flagName)
473
KConfigGroup config(KGlobal::config()->group("Notification self()->Messages"));
475
if (!config.readEntry(flagName).isEmpty())
481
void Preferences::setDialogFlag(const QString& flagName,bool state)
483
KConfigGroup config(KGlobal::config()->group("Notification self()->Messages"));
486
config.deleteEntry(flagName);
489
if (config.readEntry(flagName).isEmpty())
490
config.writeEntry(flagName,"no");
498
const QString Preferences::channelEncoding(const QString& server,const QString& channel)
500
//check all matching server's encodings
501
//TODO when we rewrite dbus, allow for multiple encodings to be returned
502
//for true 'duplicity compatibility'
503
QList<int> serverIds = serverGroupIdsByName(server);
505
if(serverIds.count() > 1)
507
for (int i=0; i < serverIds.count(); i++)
509
codec = channelEncoding(serverIds.at(i),channel);
516
return channelEncoding(serverIds.first(),channel);
519
const QString Preferences::channelEncoding(int serverGroupId,const QString& channel)
521
if(self()->mChannelEncodingsMap.contains(serverGroupId))
522
if(self()->mChannelEncodingsMap[serverGroupId].contains(channel.toLower()))
523
return self()->mChannelEncodingsMap[serverGroupId][channel.toLower()];
527
void Preferences::setChannelEncoding(const QString& server,const QString& channel,const QString& encoding)
529
//set channel encoding for ALL matching servergroups
530
QList<int> serverIds = serverGroupIdsByName(server);
531
if(serverIds.count() > 1)
533
for(int i=0; i < serverIds.count(); i++)
534
setChannelEncoding(serverIds.at(i),channel,encoding);
537
setChannelEncoding(serverIds.first(),channel,encoding);
540
void Preferences::setChannelEncoding(int serverGroupId,const QString& channel,const QString& encoding)
542
self()->mChannelEncodingsMap[serverGroupId][channel.toLower()]=encoding;
545
const QList<int> Preferences::channelEncodingsServerGroupIdList()
547
return self()->mChannelEncodingsMap.keys();
550
const QStringList Preferences::channelEncodingsChannelList(int serverGroupId)
552
return self()->mChannelEncodingsMap[serverGroupId].keys();
555
const QString Preferences::defaultNicknameSortingOrder()
560
// override to add %u if needed
561
QString Preferences::webBrowserCmd()
563
// add %u to command if it's not in there
564
QString cmd=self()->mWebBrowserCmd;
565
if (!cmd.contains("%u"))
570
void Preferences::saveColumnState(QTreeView *treeView, QString name)
572
KConfigGroup group(KGlobal::config(), name);
574
QList<int> columnWidths;
575
for (int i = 0; i < treeView->header()->count(); ++i)
576
columnWidths.append(treeView->columnWidth(i));
577
// save column widths
578
group.writeEntry("ColumnWidths", columnWidths);
579
group.writeEntry("ColumnSorted", treeView->header()->sortIndicatorSection());
580
group.writeEntry("ColumnSortDescending", treeView->header()->sortIndicatorOrder() == Qt::DescendingOrder ? true : false );
583
void Preferences::restoreColumnState(QTreeView *treeView, QString name)
585
KConfigGroup group(KGlobal::config(), name);
587
QList<int> columnWidths = group.readEntry("ColumnWidths", QList<int>());
588
for (int i = 0; i < columnWidths.count(); ++i)
589
if (columnWidths.at(i))
590
treeView->setColumnWidth(i, columnWidths.at(i));
592
if (group.readEntry("ColumnSortDescending", false))
593
treeView->header()->setSortIndicator(group.readEntry("ColumnSorted", 0), Qt::DescendingOrder);
595
treeView->header()->setSortIndicator(group.readEntry("ColumnSorted", 0), Qt::AscendingOrder);
598
#include "preferences.moc"