~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/profiles.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2002-04-19 02:28:44 UTC
  • Revision ID: james.westby@ubuntu.com-20020419022844-za7xgai5qyfd9xv6
Tags: upstream-0.8.5
ImportĀ upstreamĀ versionĀ 0.8.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** profiles.cpp - deal with profiles
 
3
** Copyright (C) 2001, 2002  Justin Karneges
 
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 program; if not, write to the Free Software
 
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 
18
**
 
19
****************************************************************************/
 
20
 
 
21
#include"profiles.h"
 
22
#include"common.h"
 
23
#include<qdir.h>
 
24
#include<qfileinfo.h>
 
25
#include<qdom.h>
 
26
 
 
27
#include<qapplication.h>
 
28
#include"eventdlg.h"
 
29
#include"chatdlg.h"
 
30
 
 
31
 
 
32
static QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
 
33
{
 
34
        QDomElement tag = doc.createElement(name);
 
35
        QDomText text = doc.createTextNode(content);
 
36
        tag.appendChild(text);
 
37
 
 
38
        return tag;
 
39
}
 
40
 
 
41
static QDomElement textTag(QDomDocument &doc, const QString &name, int content)
 
42
{
 
43
        QDomElement tag = doc.createElement(name);
 
44
        QDomText text = doc.createTextNode(QString::number(content));
 
45
        tag.appendChild(text);
 
46
 
 
47
        return tag;
 
48
}
 
49
 
 
50
static QDomElement textTag(QDomDocument &doc, const QString &name, bool content)
 
51
{
 
52
        QDomElement tag = doc.createElement(name);
 
53
        QDomText text = doc.createTextNode(content ? "true" : "false");
 
54
        tag.appendChild(text);
 
55
 
 
56
        return tag;
 
57
}
 
58
 
 
59
static QDomElement textTag(QDomDocument &doc, const QString &name, QSize s)
 
60
{
 
61
        QString str;
 
62
        str.sprintf("%d,%d", s.width(), s.height());
 
63
 
 
64
        QDomElement tag = doc.createElement(name);
 
65
        QDomText text = doc.createTextNode(str);
 
66
        tag.appendChild(text);
 
67
 
 
68
        return tag;
 
69
}
 
70
 
 
71
static QDomElement textTag(QDomDocument &doc, const QString &name, QRect r)
 
72
{
 
73
        QString str;
 
74
        str.sprintf("%d,%d,%d,%d", r.x(), r.y(), r.width(), r.height());
 
75
 
 
76
        QDomElement tag = doc.createElement(name);
 
77
        QDomText text = doc.createTextNode(str);
 
78
        tag.appendChild(text);
 
79
 
 
80
        return tag;
 
81
}
 
82
 
 
83
static QString tagContent(const QDomElement &e)
 
84
{
 
85
        // look for some tag content
 
86
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
87
                QDomText i = n.toText();
 
88
                if(i.isNull())
 
89
                        continue;
 
90
                return i.data();
 
91
        }
 
92
 
 
93
        return "";
 
94
}
 
95
 
 
96
static QDomElement findSubTag(const QDomElement &e, const QString &name, bool *found)
 
97
{
 
98
        if(found)
 
99
                *found = FALSE;
 
100
 
 
101
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
102
                QDomElement i = n.toElement();
 
103
                if(i.isNull())
 
104
                        continue;
 
105
                if(i.tagName() == name) {
 
106
                        if(found)
 
107
                                *found = TRUE;
 
108
                        return i;
 
109
                }
 
110
        }
 
111
 
 
112
        QDomElement tmp;
 
113
        return tmp;
 
114
}
 
115
 
 
116
static void readEntry(const QDomElement &e, const QString &name, QString *v)
 
117
{
 
118
        bool found = FALSE;
 
119
        QDomElement tag = findSubTag(e, name, &found);
 
120
        if(!found)
 
121
                return;
 
122
        *v = tagContent(tag);
 
123
}
 
124
 
 
125
static void readNumEntry(const QDomElement &e, const QString &name, int *v)
 
126
{
 
127
        bool found = FALSE;
 
128
        QDomElement tag = findSubTag(e, name, &found);
 
129
        if(!found)
 
130
                return;
 
131
        *v = tagContent(tag).toInt();
 
132
}
 
133
 
 
134
static void readBoolEntry(const QDomElement &e, const QString &name, bool *v)
 
135
{
 
136
        bool found = FALSE;
 
137
        QDomElement tag = findSubTag(e, name, &found);
 
138
        if(!found)
 
139
                return;
 
140
        *v = (tagContent(tag) == "true") ? TRUE: FALSE;
 
141
}
 
142
 
 
143
static void readSizeEntry(const QDomElement &e, const QString &name, QSize *v)
 
144
{
 
145
        bool found = FALSE;
 
146
        QDomElement tag = findSubTag(e, name, &found);
 
147
        if(!found)
 
148
                return;
 
149
        QStringList list = QStringList::split(',', tagContent(tag));
 
150
        if(list.count() != 2)
 
151
                return;
 
152
        QSize s;
 
153
        s.setWidth(list[0].toInt());
 
154
        s.setHeight(list[1].toInt());
 
155
        *v = s;
 
156
}
 
157
 
 
158
static void readRectEntry(const QDomElement &e, const QString &name, QRect *v)
 
159
{
 
160
        bool found = FALSE;
 
161
        QDomElement tag = findSubTag(e, name, &found);
 
162
        if(!found)
 
163
                return;
 
164
        QStringList list = QStringList::split(',', tagContent(tag));
 
165
        if(list.count() != 4)
 
166
                return;
 
167
        QRect r;
 
168
        r.setX(list[0].toInt());
 
169
        r.setY(list[1].toInt());
 
170
        r.setWidth(list[2].toInt());
 
171
        r.setHeight(list[3].toInt());
 
172
        *v = r;
 
173
}
 
174
 
 
175
static void readColorEntry(const QDomElement &e, const QString &name, QColor *v)
 
176
{
 
177
        bool found = FALSE;
 
178
        QDomElement tag = findSubTag(e, name, &found);
 
179
        if(!found)
 
180
                return;
 
181
        QColor c;
 
182
        c.setNamedColor(tagContent(tag));
 
183
        if(c.isValid())
 
184
                *v = c;
 
185
}
 
186
 
 
187
static void setBoolAttribute(QDomElement e, const QString &name, bool b)
 
188
{
 
189
        e.setAttribute(name, b ? "true" : "false");
 
190
}
 
191
 
 
192
static void readBoolAttribute(QDomElement e, const QString &name, bool *v)
 
193
{
 
194
        if(e.hasAttribute(name)) {
 
195
                QString s = e.attribute(name);
 
196
                *v = (s == "true") ? TRUE: FALSE;
 
197
        }
 
198
}
 
199
 
 
200
 
 
201
UserAccount::UserAccount()
 
202
{
 
203
        reset();
 
204
}
 
205
 
 
206
void UserAccount::reset()
 
207
{
 
208
        name = "Default";
 
209
        opt_auto = FALSE;
 
210
        opt_ssl = FALSE;
 
211
        tog_offline = TRUE;
 
212
        tog_away = TRUE;
 
213
        tog_agents = TRUE;
 
214
        user = "";
 
215
        pass = "";
 
216
        host = "";
 
217
        opt_pass = FALSE;
 
218
        port = 5222;
 
219
        resource = "Psi";
 
220
        priority = 5;
 
221
        olr_string = "";
 
222
        opt_keepAlive = TRUE;
 
223
        opt_plain = FALSE;
 
224
        opt_log = TRUE;
 
225
        opt_reconn = FALSE;
 
226
 
 
227
        roster.clear();
 
228
}
 
229
 
 
230
QString UserAccount::jid() const
 
231
{
 
232
        return QString("%1@%2").arg(user).arg(host);
 
233
}
 
234
 
 
235
// FIXME: this should be a const function (as should other ::toXml functions)
 
236
QDomElement UserAccount::toXml(QDomDocument &doc, const QString &tagName)
 
237
{
 
238
        QDomElement a = doc.createElement(tagName);
 
239
 
 
240
        setBoolAttribute(a, "auto", opt_auto);
 
241
        setBoolAttribute(a, "ssl", opt_ssl);
 
242
        setBoolAttribute(a, "showOffline", tog_offline);
 
243
        setBoolAttribute(a, "showAway", tog_away);
 
244
        setBoolAttribute(a, "showAgents", tog_agents);
 
245
        setBoolAttribute(a, "keepAlive", opt_keepAlive);
 
246
        setBoolAttribute(a, "plain", opt_plain);
 
247
        setBoolAttribute(a, "log", opt_log);
 
248
        setBoolAttribute(a, "reconn", opt_reconn);
 
249
 
 
250
        a.appendChild(textTag(doc, "name", name));
 
251
        a.appendChild(textTag(doc, "username", user));
 
252
        if(opt_pass)
 
253
                a.appendChild(textTag(doc, "password", encodePassword(pass, jid()) ));
 
254
        a.appendChild(textTag(doc, "host", host));
 
255
        a.appendChild(textTag(doc, "port", QString::number(port)));
 
256
        a.appendChild(textTag(doc, "resource", resource));
 
257
        a.appendChild(textTag(doc, "priority", QString::number(priority)));
 
258
        a.appendChild(textTag(doc, "OLR", olr_string));
 
259
 
 
260
        QDomElement r = doc.createElement("roster");
 
261
        a.appendChild(r);
 
262
        for(JabRosterEntry *i = roster.first(); i; i = roster.next()) {
 
263
                QDomElement tag = doc.createElement("item");
 
264
                tag.setAttribute("jid", i->jid);
 
265
                tag.setAttribute("name", i->nick);
 
266
                tag.setAttribute("subscription", i->sub);
 
267
                for(QStringList::ConstIterator it = i->groups.begin(); it != i->groups.end(); ++it)
 
268
                        tag.appendChild(textTag(doc, "group", *it));
 
269
                r.appendChild(tag);
 
270
        }
 
271
 
 
272
        return a;
 
273
}
 
274
 
 
275
void UserAccount::fromXml(const QDomElement &a)
 
276
{
 
277
        reset();
 
278
 
 
279
        readEntry(a, "name", &name);
 
280
        readBoolAttribute(a, "auto", &opt_auto);
 
281
        readBoolAttribute(a, "ssl", &opt_ssl);
 
282
        readBoolAttribute(a, "showOffline", &tog_offline);
 
283
        readBoolAttribute(a, "showAway", &tog_away);
 
284
        readBoolAttribute(a, "showAgents", &tog_agents);
 
285
        readBoolAttribute(a, "keepAlive", &opt_keepAlive);
 
286
        readBoolAttribute(a, "plain", &opt_plain);
 
287
        readBoolAttribute(a, "log", &opt_log);
 
288
        readBoolAttribute(a, "reconn", &opt_reconn);
 
289
 
 
290
        readEntry(a, "username", &user);
 
291
        readEntry(a, "host", &host);
 
292
        readEntry(a, "password", &pass);
 
293
        if(pass.isEmpty())
 
294
                opt_pass = FALSE;
 
295
        else {
 
296
                pass = decodePassword(pass, jid());
 
297
                opt_pass = TRUE;
 
298
        }
 
299
        readNumEntry(a, "port", &port);
 
300
        readEntry(a, "resource", &resource);
 
301
        readNumEntry(a, "priority", &priority);
 
302
        readEntry(a, "OLR", &olr_string);
 
303
 
 
304
        bool found;
 
305
        QDomElement r = findSubTag(a, "roster", &found);
 
306
        if(found) {
 
307
                for(QDomNode n = r.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
308
                        QDomElement i = n.toElement();
 
309
                        if(i.isNull())
 
310
                                continue;
 
311
 
 
312
                        if(i.tagName() == "item") {
 
313
                                JabRosterEntry *entry = new JabRosterEntry;
 
314
 
 
315
                                entry->jid = i.attribute("jid");
 
316
                                entry->sub = i.attribute("subscription");
 
317
                                entry->nick = i.attribute("name");
 
318
 
 
319
                                // grab the groups
 
320
                                for(QDomNode n = i.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
321
                                        QDomElement tag = n.toElement();
 
322
                                        if(tag.isNull())
 
323
                                                continue;
 
324
 
 
325
                                        if(tag.tagName() == "group")
 
326
                                                entry->groups.append(tagContent(tag));
 
327
                                }
 
328
                                roster.add(entry);
 
329
                        }
 
330
                }
 
331
        }
 
332
}
 
333
 
 
334
UserProfile::UserProfile()
 
335
{
 
336
        reset();
 
337
}
 
338
 
 
339
void UserProfile::reset()
 
340
{
 
341
        bool nix, win, mac;
 
342
        nix = win = mac = FALSE;
 
343
 
 
344
#ifdef Q_WS_X11
 
345
        nix = TRUE;
 
346
#endif
 
347
#ifdef Q_WS_WIN
 
348
        win = TRUE;
 
349
#endif
 
350
#ifdef Q_WS_MAC
 
351
        mac = TRUE;
 
352
#endif
 
353
 
 
354
        progver = PROG_VERSION;
 
355
 
 
356
        // global
 
357
        mwgeom.setRect(64, 64, 150, 360);
 
358
        lastStatusString = "";
 
359
        useSound = TRUE;
 
360
 
 
361
        acc.clear();
 
362
 
 
363
        // prefs
 
364
        prefs.useleft = FALSE;
 
365
        prefs.singleclick = FALSE;
 
366
        prefs.defaultAction = 0;
 
367
        prefs.delChats = dcHour;
 
368
        prefs.browser = 0;
 
369
        prefs.customBrowser = "";
 
370
        prefs.customMailer = "";
 
371
        prefs.alwaysOnTop = win ? TRUE : FALSE;
 
372
        prefs.keepSizes = FALSE;
 
373
        prefs.ignoreHeadline = FALSE;
 
374
        prefs.useDock = win ? TRUE: FALSE;
 
375
        prefs.dockDCstyle = win ? TRUE: FALSE;
 
376
        prefs.alertStyle = 2;
 
377
        prefs.popupMsgs = FALSE;
 
378
        prefs.popupChats = FALSE;
 
379
        prefs.raise = FALSE;
 
380
        prefs.incomingAs = 0;
 
381
        prefs.outgoingAs = 0;
 
382
        prefs.askOnline = FALSE;
 
383
        prefs.rosterAnim = TRUE;
 
384
        prefs.asAway = 10;
 
385
        prefs.asXa = 30;
 
386
        prefs.asOffline = 0;
 
387
        prefs.scrollTo = TRUE;
 
388
 
 
389
        prefs.sp.clear();
 
390
        prefs.sp.set(QObject::tr("Away from desk"), QObject::tr("I am away from my desk.  Leave a message."));
 
391
        prefs.sp.set(QObject::tr("Showering"), QObject::tr("I'm in the shower.  You'll have to wait for me to get out."));
 
392
        prefs.sp.set(QObject::tr("Eating"), QObject::tr("Out eating.  Mmmm.. food."));
 
393
        prefs.sp.set(QObject::tr("Sleep"), QObject::tr("Sleep is good.  Zzzzz"));
 
394
        prefs.sp.set(QObject::tr("Work"), QObject::tr("Can't chat.  Gotta work."));
 
395
        prefs.sp.set(QObject::tr("Air"), QObject::tr("Stepping out to get some fresh air."));
 
396
        prefs.sp.set(QObject::tr("Movie"), QObject::tr("Out to a movie.  Is that OK with you?"));
 
397
        prefs.sp.set(QObject::tr("Secret"), QObject::tr("I'm not available right now and that's all you need to know."));
 
398
        prefs.sp.set(QObject::tr("Out for the night"), QObject::tr("Out for the night."));
 
399
        prefs.sp.set(QObject::tr("Greece"), QObject::tr("I have gone to a far away place.  I will be back someday!"));
 
400
 
 
401
        prefs.iconset = "stellar";
 
402
 
 
403
        prefs.color[cOnline]    = QColor("#0060C0");
 
404
        prefs.color[cListBack]  = QColor("#C0C0C0");
 
405
        prefs.color[cAway]      = QColor("#008080");
 
406
        prefs.color[cDND]       = QColor("#800000");
 
407
        prefs.color[cOffline]   = QColor("#000000");
 
408
        prefs.color[cGroupFore] = QColor("#000000");
 
409
        prefs.color[cGroupBack] = QColor("#FFFFFF");
 
410
 
 
411
        prefs.font[fRoster] = QApplication::font().toString();
 
412
        prefs.font[fMessage] = QApplication::font().toString();
 
413
        prefs.font[fChat] = QApplication::font().toString();
 
414
 
 
415
        prefs.player = "play";
 
416
        prefs.noAwaySound = FALSE;
 
417
 
 
418
        prefs.onevent[eMessage] = g.pathBase + "/sound/chat2.wav";
 
419
        prefs.onevent[eChat1]   = g.pathBase + "/sound/chat1.wav";
 
420
        prefs.onevent[eChat2]   = g.pathBase + "/sound/chat2.wav";
 
421
        prefs.onevent[eSystem]  = g.pathBase + "/sound/chat2.wav";
 
422
        prefs.onevent[eOnline]  = g.pathBase + "/sound/online.wav";
 
423
        prefs.onevent[eOffline] = g.pathBase + "/sound/offline.wav";
 
424
        prefs.onevent[eSend]    = g.pathBase + "/sound/send.wav";
 
425
 
 
426
        prefs.sizeEventDlg = EventDlg::defaultSize();
 
427
        prefs.sizeChatDlg = ChatDlg::defaultSize();
 
428
}
 
429
 
 
430
bool UserProfile::toFile(const QString &fname)
 
431
{
 
432
//      FILE *f = fopen("c:\\psi.txt", "w");
 
433
//      fprintf(f, "would have opened [%s]\n", fname.latin1());
 
434
//      fclose(f);
 
435
//      return;
 
436
 
 
437
        QDomDocument doc;
 
438
 
 
439
        QDomElement base = doc.createElement("psiconf");
 
440
        base.setAttribute("version", "1.0");
 
441
        doc.appendChild(base);
 
442
 
 
443
        base.appendChild(textTag(doc, "progver", PROG_VERSION));
 
444
        base.appendChild(textTag(doc, "geom", mwgeom));
 
445
        base.appendChild(textTag(doc, "lastStatusString", lastStatusString));
 
446
        base.appendChild(textTag(doc, "useSound", useSound));
 
447
 
 
448
        QDomElement accs = doc.createElement("accounts");
 
449
        base.appendChild(accs);
 
450
        for(UserAccountList::Iterator ai = acc.begin(); ai != acc.end(); ++ai)
 
451
                accs.appendChild((*ai).toXml(doc, "account"));
 
452
 
 
453
        QDomElement p = doc.createElement("preferences");
 
454
        base.appendChild(p);
 
455
 
 
456
        {
 
457
                QDomElement p_general = doc.createElement("general");
 
458
                p.appendChild(p_general);
 
459
 
 
460
                {
 
461
                        QDomElement p_roster = doc.createElement("roster");
 
462
                        p_general.appendChild(p_roster);
 
463
 
 
464
                        p_roster.appendChild(textTag(doc, "useleft", prefs.useleft));
 
465
                        p_roster.appendChild(textTag(doc, "singleclick", prefs.singleclick));
 
466
                        p_roster.appendChild(textTag(doc, "defaultAction", prefs.defaultAction));
 
467
                }
 
468
                {
 
469
                        QDomElement p_misc = doc.createElement("misc");
 
470
                        p_general.appendChild(p_misc);
 
471
 
 
472
                        p_misc.appendChild(textTag(doc, "delChats", prefs.delChats));
 
473
                        p_misc.appendChild(textTag(doc, "browser", prefs.browser));
 
474
                        p_misc.appendChild(textTag(doc, "customBrowser", prefs.customBrowser));
 
475
                        p_misc.appendChild(textTag(doc, "customMailer", prefs.customMailer));
 
476
                        p_misc.appendChild(textTag(doc, "alwaysOnTop", prefs.alwaysOnTop));
 
477
                        p_misc.appendChild(textTag(doc, "keepSizes", prefs.keepSizes));
 
478
                        p_misc.appendChild(textTag(doc, "ignoreHeadline", prefs.ignoreHeadline));
 
479
                        p_misc.appendChild(textTag(doc, "scrollTo", prefs.scrollTo));
 
480
                }
 
481
                {
 
482
                        QDomElement p_dock = doc.createElement("dock");
 
483
                        p_general.appendChild(p_dock);
 
484
 
 
485
                        p_dock.appendChild(textTag(doc, "useDock", prefs.useDock));
 
486
                        p_dock.appendChild(textTag(doc, "dockDCstyle", prefs.dockDCstyle));
 
487
                }
 
488
        }
 
489
 
 
490
        {
 
491
                QDomElement p_events = doc.createElement("events");
 
492
                p.appendChild(p_events);
 
493
 
 
494
                p_events.appendChild(textTag(doc, "alertstyle", prefs.alertStyle));
 
495
 
 
496
                {
 
497
                        QDomElement p_receive = doc.createElement("receive");
 
498
                        p_events.appendChild(p_receive);
 
499
 
 
500
                        p_receive.appendChild(textTag(doc, "popupMsgs", prefs.popupMsgs));
 
501
                        p_receive.appendChild(textTag(doc, "popupChats", prefs.popupChats));
 
502
                        p_receive.appendChild(textTag(doc, "raise", prefs.raise));
 
503
                        p_receive.appendChild(textTag(doc, "incomingAs", prefs.incomingAs));
 
504
                }
 
505
                {
 
506
                        QDomElement p_send = doc.createElement("send");
 
507
                        p_events.appendChild(p_send);
 
508
 
 
509
                        p_send.appendChild(textTag(doc, "outgoingAs", prefs.outgoingAs));
 
510
                }
 
511
        }
 
512
 
 
513
        {
 
514
                QDomElement p_pres = doc.createElement("presence");
 
515
                p.appendChild(p_pres);
 
516
 
 
517
                {
 
518
                        QDomElement tag = doc.createElement("misc");
 
519
                        p_pres.appendChild(tag);
 
520
 
 
521
                        tag.appendChild(textTag(doc, "askOnline", prefs.askOnline));
 
522
                        tag.appendChild(textTag(doc, "rosterAnim", prefs.rosterAnim));
 
523
                }
 
524
                {
 
525
                        QDomElement tag = doc.createElement("autostatus");
 
526
                        p_pres.appendChild(tag);
 
527
 
 
528
                        tag.appendChild(textTag(doc, "away", prefs.asAway));
 
529
                        tag.appendChild(textTag(doc, "xa", prefs.asXa));
 
530
                        tag.appendChild(textTag(doc, "offline", prefs.asOffline));
 
531
                }
 
532
                {
 
533
                        p_pres.appendChild(prefs.sp.toXml(doc, "statuspresets"));
 
534
                }
 
535
        }
 
536
 
 
537
        {
 
538
                QDomElement p_lnf = doc.createElement("lookandfeel");
 
539
                p.appendChild(p_lnf);
 
540
 
 
541
                p_lnf.appendChild(textTag(doc, "iconset", prefs.iconset));
 
542
 
 
543
                {
 
544
                        QDomElement tag = doc.createElement("colors");
 
545
                        p_lnf.appendChild(tag);
 
546
 
 
547
                        tag.appendChild(textTag(doc, "online", prefs.color[cOnline].name() ));
 
548
                        tag.appendChild(textTag(doc, "listback", prefs.color[cListBack].name() ));
 
549
                        tag.appendChild(textTag(doc, "away", prefs.color[cAway].name() ));
 
550
                        tag.appendChild(textTag(doc, "dnd", prefs.color[cDND].name() ));
 
551
                        tag.appendChild(textTag(doc, "offline", prefs.color[cOffline].name() ));
 
552
                        tag.appendChild(textTag(doc, "groupfore", prefs.color[cGroupFore].name() ));
 
553
                        tag.appendChild(textTag(doc, "groupback", prefs.color[cGroupBack].name() ));
 
554
                }
 
555
 
 
556
                {
 
557
                        QDomElement tag = doc.createElement("fonts");
 
558
                        p_lnf.appendChild(tag);
 
559
 
 
560
                        tag.appendChild(textTag(doc, "roster", prefs.font[fRoster] ));
 
561
                        tag.appendChild(textTag(doc, "message", prefs.font[fMessage] ));
 
562
                        tag.appendChild(textTag(doc, "chat", prefs.font[fChat] ));
 
563
                }
 
564
        }
 
565
 
 
566
        {
 
567
                QDomElement p_sound = doc.createElement("sound");
 
568
                p.appendChild(p_sound);
 
569
 
 
570
                p_sound.appendChild(textTag(doc, "player", prefs.player));
 
571
                p_sound.appendChild(textTag(doc, "noawaysound", prefs.noAwaySound));
 
572
 
 
573
                {
 
574
                        QDomElement p_onevent = doc.createElement("onevent");
 
575
                        p_sound.appendChild(p_onevent);
 
576
 
 
577
                        p_onevent.appendChild(textTag(doc, "message", prefs.onevent[eMessage]));
 
578
                        p_onevent.appendChild(textTag(doc, "chat1", prefs.onevent[eChat1]));
 
579
                        p_onevent.appendChild(textTag(doc, "chat2", prefs.onevent[eChat2]));
 
580
                        p_onevent.appendChild(textTag(doc, "system", prefs.onevent[eSystem]));
 
581
                        p_onevent.appendChild(textTag(doc, "online", prefs.onevent[eOnline]));
 
582
                        p_onevent.appendChild(textTag(doc, "offline", prefs.onevent[eOffline]));
 
583
                        p_onevent.appendChild(textTag(doc, "send", prefs.onevent[eSend]));
 
584
                }
 
585
        }
 
586
 
 
587
        {
 
588
                QDomElement p_sizes = doc.createElement("sizes");
 
589
                p.appendChild(p_sizes);
 
590
 
 
591
                p_sizes.appendChild(textTag(doc, "eventdlg", prefs.sizeEventDlg));
 
592
                p_sizes.appendChild(textTag(doc, "chatdlg", prefs.sizeChatDlg));
 
593
        }
 
594
 
 
595
        QFile f(fname);
 
596
        if(!f.open(IO_WriteOnly))
 
597
                return FALSE;
 
598
        QTextStream t;
 
599
        t.setDevice(&f);
 
600
        t.setEncoding(QTextStream::UnicodeUTF8);
 
601
        t << doc.toString();
 
602
        t.unsetDevice();
 
603
        f.close();
 
604
 
 
605
        return TRUE;
 
606
}
 
607
 
 
608
bool UserProfile::fromFile(const QString &fname)
 
609
{
 
610
        QString confver;
 
611
        QDomDocument doc;
 
612
 
 
613
        QFile f(fname);
 
614
        if(!f.open(IO_ReadOnly))
 
615
                return FALSE;
 
616
        if(!doc.setContent(&f))
 
617
                return FALSE;
 
618
        f.close();
 
619
 
 
620
        QDomElement base = doc.documentElement();
 
621
        if(base.tagName() != "psiconf")
 
622
                return FALSE;
 
623
        confver = base.attribute("version");
 
624
        if(confver != "1.0")
 
625
                return FALSE;
 
626
 
 
627
        readEntry(base, "progver", &progver);
 
628
 
 
629
        readRectEntry(base, "geom", &mwgeom);
 
630
        readEntry(base, "lastStatusString", &lastStatusString);
 
631
        readBoolEntry(base, "usesound", &useSound);
 
632
 
 
633
        bool found;
 
634
        QDomElement accs = findSubTag(base, "accounts", &found);
 
635
        if(found) {
 
636
                for(QDomNode n = accs.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
637
                        QDomElement a = n.toElement();
 
638
                        if(a.isNull())
 
639
                                continue;
 
640
 
 
641
                        if(a.tagName() == "account") {
 
642
                                UserAccount ua;
 
643
                                ua.fromXml(a);
 
644
                                acc.append(ua);
 
645
                        }
 
646
                }
 
647
        }
 
648
 
 
649
        QDomElement p = findSubTag(base, "preferences", &found);
 
650
        if(found) {
 
651
                bool found;
 
652
 
 
653
                QDomElement p_general = findSubTag(p, "general", &found);
 
654
                if(found) {
 
655
                        bool found;
 
656
 
 
657
                        QDomElement p_roster = findSubTag(p_general, "roster", &found);
 
658
                        if(found) {
 
659
                                readBoolEntry(p_roster, "useleft", &prefs.useleft);
 
660
                                readBoolEntry(p_roster, "singleclick", &prefs.singleclick);
 
661
                                readNumEntry(p_roster, "defaultAction", &prefs.defaultAction);
 
662
                        }
 
663
 
 
664
                        QDomElement tag = findSubTag(p_general, "misc", &found);
 
665
                        if(found) {
 
666
                                readNumEntry(tag, "delChats", &prefs.delChats);
 
667
                                readNumEntry(tag, "browser", &prefs.browser);
 
668
                                readEntry(tag, "customBrowser", &prefs.customBrowser);
 
669
                                readEntry(tag, "customMailer", &prefs.customMailer);
 
670
                                readBoolEntry(tag, "alwaysOnTop", &prefs.alwaysOnTop);
 
671
                                readBoolEntry(tag, "keepSizes", &prefs.keepSizes);
 
672
                                readBoolEntry(tag, "ignoreHeadline", &prefs.ignoreHeadline);
 
673
                                readBoolEntry(tag, "scrollTo", &prefs.scrollTo);
 
674
                        }
 
675
 
 
676
                        tag = findSubTag(p_general, "dock", &found);
 
677
                        if(found) {
 
678
                                readBoolEntry(tag, "useDock", &prefs.useDock);
 
679
                                readBoolEntry(tag, "dockDCstyle", &prefs.dockDCstyle);
 
680
                        }
 
681
                }
 
682
 
 
683
                QDomElement p_events = findSubTag(p, "events", &found);
 
684
                if(found) {
 
685
                        bool found;
 
686
 
 
687
                        readNumEntry(p_events, "alertstyle", &prefs.alertStyle);
 
688
 
 
689
                        QDomElement tag = findSubTag(p_events, "receive", &found);
 
690
                        if(found) {
 
691
                                readBoolEntry(tag, "popupMsgs", &prefs.popupMsgs);
 
692
                                readBoolEntry(tag, "popupChats", &prefs.popupChats);
 
693
                                readBoolEntry(tag, "raise", &prefs.raise);
 
694
                                readNumEntry(tag, "incomingAs", &prefs.incomingAs);
 
695
                        }
 
696
 
 
697
                        tag = findSubTag(p_events, "send", &found);
 
698
                        if(found) {
 
699
                                readNumEntry(tag, "outgoingAs", &prefs.outgoingAs);
 
700
                        }
 
701
                }
 
702
 
 
703
                QDomElement p_pres = findSubTag(p, "presence", &found);
 
704
                if(found) {
 
705
                        bool found;
 
706
 
 
707
                        QDomElement tag = findSubTag(p_pres, "misc", &found);
 
708
                        if(found) {
 
709
                                readBoolEntry(tag, "askOnline", &prefs.askOnline);
 
710
                                readBoolEntry(tag, "rosterAnim", &prefs.rosterAnim);
 
711
                        }
 
712
 
 
713
                        tag = findSubTag(p_pres, "autostatus", &found);
 
714
                        if(found) {
 
715
                                readNumEntry(tag, "away", &prefs.asAway);
 
716
                                readNumEntry(tag, "xa", &prefs.asXa);
 
717
                                readNumEntry(tag, "offline", &prefs.asOffline);
 
718
                        }
 
719
 
 
720
                        tag = findSubTag(p_pres, "statuspresets", &found);
 
721
                        if(found)
 
722
                                prefs.sp.fromXml(tag);
 
723
                }
 
724
 
 
725
                QDomElement p_lnf = findSubTag(p, "lookandfeel", &found);
 
726
                if(found) {
 
727
                        bool found;
 
728
 
 
729
                        readEntry(p_lnf, "iconset", &prefs.iconset);
 
730
 
 
731
                        QDomElement tag = findSubTag(p_lnf, "colors", &found);
 
732
                        if(found) {
 
733
                                readColorEntry(tag, "online", &prefs.color[cOnline]);
 
734
                                readColorEntry(tag, "listback", &prefs.color[cListBack]);
 
735
                                readColorEntry(tag, "away", &prefs.color[cAway]);
 
736
                                readColorEntry(tag, "dnd", &prefs.color[cDND]);
 
737
                                readColorEntry(tag, "offline", &prefs.color[cOffline]);
 
738
                                readColorEntry(tag, "groupfore", &prefs.color[cGroupFore]);
 
739
                                readColorEntry(tag, "groupback", &prefs.color[cGroupBack]);
 
740
                        }
 
741
 
 
742
                        tag = findSubTag(p_lnf, "fonts", &found);
 
743
                        if(found) {
 
744
                                readEntry(tag, "roster", &prefs.font[fRoster]);
 
745
                                readEntry(tag, "message", &prefs.font[fMessage]);
 
746
                                readEntry(tag, "chat", &prefs.font[fChat]);
 
747
                        }
 
748
                }
 
749
 
 
750
                QDomElement p_sound = findSubTag(p, "sound", &found);
 
751
                if(found) {
 
752
                        bool found;
 
753
 
 
754
                        readEntry(p_sound, "player", &prefs.player);
 
755
                        readBoolEntry(p_sound, "noawaysound", &prefs.noAwaySound);
 
756
 
 
757
                        QDomElement tag = findSubTag(p_sound, "onevent", &found);
 
758
                        if(found) {
 
759
                                readEntry(tag, "message", &prefs.onevent[eMessage]);
 
760
                                readEntry(tag, "chat1", &prefs.onevent[eChat1]);
 
761
                                readEntry(tag, "chat2", &prefs.onevent[eChat2]);
 
762
                                readEntry(tag, "system", &prefs.onevent[eSystem]);
 
763
                                readEntry(tag, "online", &prefs.onevent[eOnline]);
 
764
                                readEntry(tag, "offline", &prefs.onevent[eOffline]);
 
765
                                readEntry(tag, "send", &prefs.onevent[eSend]);
 
766
                        }
 
767
                }
 
768
 
 
769
                QDomElement p_sizes = findSubTag(p, "sizes", &found);
 
770
                if(found) {
 
771
                        readSizeEntry(p_sizes, "eventdlg", &prefs.sizeEventDlg);
 
772
                        readSizeEntry(p_sizes, "chatdlg", &prefs.sizeChatDlg);
 
773
                }
 
774
        }
 
775
 
 
776
        return TRUE;
 
777
}
 
778
 
 
779
 
 
780
QString pathToProfile(const QString &name)
 
781
{
 
782
        return g.pathProfiles + "/" + name;
 
783
}
 
784
 
 
785
QString pathToProfileConfig(const QString &name)
 
786
{
 
787
        return pathToProfile(name) + "/config.xml";
 
788
}
 
789
 
 
790
QStringList getProfilesList()
 
791
{
 
792
        QStringList list;
 
793
 
 
794
        QDir d(g.pathProfiles);
 
795
        if(!d.exists())
 
796
                return list;
 
797
 
 
798
        QStringList entries = d.entryList();
 
799
        for(QStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
 
800
                if(*it == "." || *it == "..")
 
801
                        continue;
 
802
                QFileInfo info(d, *it);
 
803
                if(!info.isDir())
 
804
                        continue;
 
805
 
 
806
                list.append(*it);
 
807
        }
 
808
 
 
809
        list.sort();
 
810
 
 
811
        return list;
 
812
}
 
813
 
 
814
bool profileExists(const QString &_name)
 
815
{
 
816
        QString name = _name.lower();
 
817
 
 
818
        QStringList list = getProfilesList();
 
819
        for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
 
820
                if((*it).lower() == name)
 
821
                        return TRUE;
 
822
        }
 
823
        return FALSE;
 
824
}
 
825
 
 
826
bool profileNew(const QString &name)
 
827
{
 
828
        if(name.isEmpty())
 
829
                return FALSE;
 
830
 
 
831
        // verify the string is sane
 
832
        for(int n = 0; n < (int)name.length(); ++n) {
 
833
                if(!name.at(n).isLetterOrNumber())
 
834
                        return FALSE;
 
835
        }
 
836
 
 
837
        // make it
 
838
        QDir d(g.pathProfiles);
 
839
        if(!d.exists())
 
840
                return FALSE;
 
841
        QDir p(g.pathProfiles + "/" + name);
 
842
        if(!p.exists()) {
 
843
                d.mkdir(name);
 
844
                if(!p.exists())
 
845
                        return FALSE;
 
846
        }
 
847
 
 
848
        p.mkdir("history");
 
849
        p.mkdir("info");
 
850
 
 
851
        return TRUE;
 
852
}
 
853
 
 
854
bool profileRename(const QString &oldname, const QString &name)
 
855
{
 
856
        // verify the string is sane
 
857
        for(int n = 0; n < (int)name.length(); ++n) {
 
858
                if(!name.at(n).isLetterOrNumber())
 
859
                        return FALSE;
 
860
        }
 
861
 
 
862
        // locate the folder
 
863
        QDir d(g.pathProfiles);
 
864
        if(!d.exists())
 
865
                return FALSE;
 
866
        if(!d.rename(oldname, name))
 
867
                return FALSE;
 
868
 
 
869
        return TRUE;
 
870
}
 
871
 
 
872
static bool folderRemove(const QDir &_d)
 
873
{
 
874
        QDir d = _d;
 
875
 
 
876
        QStringList entries = d.entryList();
 
877
        for(QStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
 
878
                if(*it == "." || *it == "..")
 
879
                        continue;
 
880
                QFileInfo info(d, *it);
 
881
                if(info.isDir()) {
 
882
                        if(!folderRemove(QDir(info.filePath())))
 
883
                                return FALSE;
 
884
                }
 
885
                else {
 
886
                        //printf("deleting [%s]\n", info.filePath().latin1());
 
887
                        d.remove(info.filePath());
 
888
                }
 
889
        }
 
890
        QString name = d.dirName();
 
891
        if(!d.cdUp())
 
892
                return FALSE;
 
893
        //printf("removing folder [%s]\n", d.filePath(name).latin1());
 
894
        d.rmdir(name);
 
895
 
 
896
        return TRUE;
 
897
}
 
898
 
 
899
bool profileDelete(const QString &path)
 
900
{
 
901
        QDir d(path);
 
902
        if(!d.exists())
 
903
                return TRUE;
 
904
 
 
905
        return folderRemove(QDir(path));
 
906
}