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

« back to all changes in this revision

Viewing changes to src/jabcon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2005-01-10 17:41:43 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110174143-ltocv5zapl6blf5d
Tags: 0.9.3-1
* New upstream release
* Cleaned up debian/rules (some things are done by upstream Makefiles now)
* Fixed some lintian warnings:
  - removed executable bit from some .png files
  - moved psi.desktop to /usr/share/applications
* Updated menu files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
** jabcon.cpp - core control class for Psi
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<qapplication.h>
22
 
#include<qstring.h>
23
 
#include<qsettings.h>
24
 
#include<qstylesheet.h>
25
 
 
26
 
#include<qfile.h>
27
 
#include<qmessagebox.h>
28
 
#include<qtimer.h>
29
 
#include<qsound.h>
30
 
#include<qprocess.h>
31
 
#include<qdesktopwidget.h>
32
 
 
33
 
#include"jabcon.h"
34
 
#include"common.h"
35
 
 
36
 
#include"eventdlg.h"
37
 
#include"historydlg.h"
38
 
#include"statusdlg.h"
39
 
#include"optionsdlg.h"
40
 
#include"servicesdlg.h"
41
 
#include"infodlg.h"
42
 
#include"info.h"
43
 
#include"searchdlg.h"
44
 
#include"offermaindlg.h"
45
 
 
46
 
#include"anim.h"
47
 
 
48
 
 
49
 
jabcon::jabcon(QObject *par)
50
 
:QObject(par)
51
 
{
52
 
        pdb(DEBUG_JABCON, QString("%1 v%2\n By Justin Karneges\n    infiniti@affinix.com\n\n").arg(PROG_NAME).arg(PROG_VERSION));
53
 
 
54
 
        lastStatusString = "";
55
 
        mainwin = 0;
56
 
 
57
 
        // load the profile
58
 
        pro.reset();
59
 
        pro.fromFile(pathToProfileConfig(activeProfile));
60
 
 
61
 
        // if no accounts, add a blank account
62
 
        if(pro.acc.isEmpty()) {
63
 
                pro.acc.append(UserAccount());
64
 
        }
65
 
 
66
 
        // set the "global" profile data
67
 
        mwgeom = pro.mwgeom;
68
 
        lastStatusString = pro.lastStatusString;
69
 
        useSound = pro.useSound;
70
 
 
71
 
        // set the preferences
72
 
        option = pro.prefs;
73
 
}
74
 
 
75
 
bool jabcon::init()
76
 
{
77
 
        // first thing, try to load the iconset
78
 
        if(!loadPsiIconSet(option.iconset)) {
79
 
                option.iconset = "stellar";
80
 
                if(!loadPsiIconSet(option.iconset)) {
81
 
                        QMessageBox::critical(0, tr("Error"), tr("Unable to load iconset!  Please make sure Psi is properly installed."));
82
 
                        return FALSE;
83
 
                }
84
 
        }
85
 
 
86
 
        // create a new session
87
 
        s = new JabSession(&jsm);
88
 
        jsm.add(s);
89
 
        debug_jabber = s->serv();
90
 
 
91
 
        // setup the main window
92
 
        mainwin = new MainWin(option.alwaysOnTop, 0, "psimain");
93
 
        mainwin->userlist = &s->userlist;
94
 
        mainwin->setUseDock(option.useDock);
95
 
 
96
 
        // if the coordinates are out of the desktop bounds, reset to the top left
97
 
        QRect r = QApplication::desktop()->screenGeometry();
98
 
        if(mwgeom.x() >= r.right() || mwgeom.y() >= r.bottom()) {
99
 
                mwgeom.setX(r.left() + 32);
100
 
                mwgeom.setY(r.top() + 32);
101
 
        }
102
 
        mainwin->move(mwgeom.x(), mwgeom.y());
103
 
        mainwin->resize(mwgeom.width(), mwgeom.height());
104
 
 
105
 
        cvp = mainwin->cvlist->createProfile("justin@orbit.tenpura.org", "Orbit", TRUE);
106
 
 
107
 
        mainwin->setUsingSSL(FALSE);
108
 
 
109
 
        fileserv = new FileServer;
110
 
        //fileserv->setServer("127.0.0.1", 12346);
111
 
        //FileServerItem *i = fileserv->addFile("/opt/kde", "", "");
112
 
        //if(i) {
113
 
        //      printf("offering: [%s]\n", i->url.latin1());
114
 
        //}
115
 
 
116
 
        // jsm
117
 
        connect(&jsm, SIGNAL(accountSettingsChanged()), SLOT(slotApplyAccounts()));
118
 
        connect(&jsm, SIGNAL(jab_connected(JabSession *)),                                           SLOT(jab_connected(JabSession *)));
119
 
        connect(&jsm, SIGNAL(jab_disconnected(JabSession *)),                                        SLOT(jab_disconnected(JabSession *)));
120
 
        connect(&jsm, SIGNAL(jab_statusUpdate(JabSession *, JabUpdate *)),                           SLOT(jab_statusUpdate(JabSession *, JabUpdate *)) );
121
 
        connect(&jsm, SIGNAL(jab_error(JabSession *, JabError *)),                                   SLOT(jab_error(JabSession *, JabError *)) );
122
 
        connect(&jsm, SIGNAL(jab_messageReceived(JabSession *, const JabMessage &)),                 SLOT(jab_messageReceived(JabSession *, const JabMessage &)) );
123
 
        connect(&jsm, SIGNAL(jab_resourceAvailable(JabSession *, const Jid &, const JabResource &)), SLOT(jab_resourceAvailable(JabSession *, const Jid &, const JabResource &)));
124
 
        connect(&jsm, SIGNAL(jab_resourceUnavailable(JabSession *, const Jid &)),                    SLOT(jab_resourceUnavailable(JabSession *, const Jid &)));
125
 
        connect(&jsm, SIGNAL(jab_contactChanged(JabSession *, JabRosterEntry *)),                    SLOT(jab_contactChanged(JabSession *, JabRosterEntry *)) );
126
 
        connect(&jsm, SIGNAL(jab_contactNew(JabSession *, JabRosterEntry *)),                        SLOT(jab_contactNew(JabSession *, JabRosterEntry *)) );
127
 
        connect(&jsm, SIGNAL(jab_contactRemove(JabSession *, JabRosterEntry *)),                     SLOT(jab_contactRemove(JabSession *, JabRosterEntry *)) );
128
 
        connect(&jsm, SIGNAL(jab_authRequest(JabSession *, const Jid &)),                            SLOT(jab_authRequest(JabSession *, const Jid &)) );
129
 
        connect(&jsm, SIGNAL(jab_authGrant(JabSession *, const Jid &)),                              SLOT(jab_authGrant(JabSession *, const Jid &)) );
130
 
        connect(&jsm, SIGNAL(jab_authRemove(JabSession *, const Jid &)),                             SLOT(jab_authRemove(JabSession *, const Jid &)) );
131
 
 
132
 
        // jabber
133
 
        /*connect(serv, SIGNAL(connected()),                         SLOT(servConnected()));
134
 
        connect(serv, SIGNAL(disconnected()),                      SLOT(servDisconnected()));
135
 
        connect(serv, SIGNAL(statusUpdate(JabUpdate *)),           SLOT(statusUpdate(JabUpdate *)) );
136
 
        connect(serv, SIGNAL(error(JabError *)),                   SLOT(servError(JabError *)) );
137
 
        connect(serv, SIGNAL(messageReceived(const JabMessage &)), SLOT(messageReceived(const JabMessage &)) );
138
 
 
139
 
        connect(serv, SIGNAL(resourceAvailable(const Jid &, const JabResource &)), SLOT(resourceAvailable(const Jid &, const JabResource &)));
140
 
        connect(serv, SIGNAL(resourceUnavailable(const Jid &)), SLOT(resourceUnavailable(const Jid &)));
141
 
 
142
 
        connect(serv, SIGNAL(contactChanged(JabRosterEntry *)),    SLOT(contactChanged(JabRosterEntry *)) );
143
 
        connect(serv, SIGNAL(contactNew(JabRosterEntry *)),        SLOT(contactNew(JabRosterEntry *)) );
144
 
        connect(serv, SIGNAL(contactRemove(JabRosterEntry *)),     SLOT(contactRemove(JabRosterEntry *)) );
145
 
 
146
 
        connect(serv, SIGNAL(authRequest(const Jid &)),            SLOT(authRequest(const Jid &)) );
147
 
        connect(serv, SIGNAL(authGrant(const Jid &)),              SLOT(authGrant(const Jid &)) );
148
 
        connect(serv, SIGNAL(authRemove(const Jid &)),             SLOT(authRemove(const Jid &)) );*/
149
 
 
150
 
        // actions
151
 
        connect(this, SIGNAL(emitOptionsUpdate()), mainwin->cvlist, SLOT(optionsUpdate()) );
152
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), cvp, SLOT(localUpdate(const JabRosterEntry &)));
153
 
        connect(cvp, SIGNAL(actionDefault(ContactViewItem *)),SLOT(actionDefault(ContactViewItem *)) );
154
 
        connect(cvp, SIGNAL(actionRecvEvent(ContactViewItem *)),SLOT(actionRecvEvent(ContactViewItem *)) );
155
 
        connect(cvp, SIGNAL(actionSendMessage(ContactViewItem *)),SLOT(actionSendMessage(ContactViewItem *)) );
156
 
        connect(cvp, SIGNAL(actionSendMessage(const QStringList &)),SLOT(actionSendMessage(const QStringList &)) );
157
 
        connect(cvp, SIGNAL(actionSendUrl(const QString &)),SLOT(actionSendUrl(const QString &)) );
158
 
        connect(cvp, SIGNAL(actionAuthorize(ContactViewItem *)),SLOT(actionAuthorize(ContactViewItem *)) );
159
 
        connect(cvp, SIGNAL(actionAdd(ContactViewItem *)),SLOT(actionAdd(ContactViewItem *)) );
160
 
        connect(cvp, SIGNAL(actionRemove(const QString &)),SLOT(actionRemove(const QString &)) );
161
 
        connect(cvp, SIGNAL(actionRename(ContactViewItem *, const QString &)),SLOT(actionRename(ContactViewItem *, const QString &)) );
162
 
        connect(cvp, SIGNAL(actionGroupChange(const QString &, const QString &)),SLOT(actionGroupChange(const QString &, const QString &)) );
163
 
        connect(cvp, SIGNAL(actionGroupRename(const QString &, const QString &)),SLOT(actionGroupRename(const QString &, const QString &)) );
164
 
        connect(cvp, SIGNAL(actionHistory(const QString &)),SLOT(actionHistory(const QString &)) );
165
 
        connect(cvp, SIGNAL(actionStatusShow(const QString &)),SLOT(actionStatusShow(const QString &)) );
166
 
        connect(cvp, SIGNAL(actionOpenChat(const QString &)), SLOT(actionOpenChat(const QString &)) );
167
 
        connect(cvp, SIGNAL(actionAgentSetStatus(const QString &, int)), SLOT(agentSetStatus(const QString &, int)));
168
 
        connect(cvp, SIGNAL(actionInfo(const QString &)), SLOT(actionInfo(const QString &)));
169
 
        connect(cvp, SIGNAL(actionOfferFile(const QString &)), SLOT(actionOfferFile(const QString &)));
170
 
        connect(mainwin, SIGNAL(doOptions()), SLOT(doOptions()) );
171
 
        connect(mainwin, SIGNAL(doManageServices()), SLOT(doManageServices()) );
172
 
        connect(mainwin, SIGNAL(doManageAccounts()), SLOT(doManageAccounts()) );
173
 
        connect(mainwin, SIGNAL(doFileSharing()), SLOT(doFileSharing()) );
174
 
        connect(mainwin, SIGNAL(accountInfo()), SLOT(openAccountInfo()) );
175
 
        connect(mainwin, SIGNAL(blankMessage()), SLOT(composeBlankMessage()) );
176
 
        connect(mainwin, SIGNAL(statusChanged(int)),SLOT(statusMenuChanged(int)) );
177
 
        connect(mainwin, SIGNAL(closeProgram()), SLOT(closeProgram()));
178
 
        connect(mainwin, SIGNAL(changeProfile()), SLOT(changeProfile()));
179
 
        connect(mainwin, SIGNAL(addUser()), SLOT(askAddUser()));
180
 
        connect(mainwin, SIGNAL(recvNextEvent()), SLOT(recvNextEvent()));
181
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), mainwin, SLOT(localUpdate(const JabRosterEntry &)));
182
 
        connect(this, SIGNAL(emitOptionsUpdate()), mainwin, SLOT(optionsUpdate()) );
183
 
 
184
 
        s->setContactProfile(cvp);
185
 
        s->setAccount(pro.acc.first());
186
 
 
187
 
        mainwin->cvlist->setShowOffline(s->acc()->tog_offline);
188
 
        mainwin->cvlist->setShowAway(s->acc()->tog_away);
189
 
        mainwin->cvlist->setShowAgents(s->acc()->tog_agents);
190
 
 
191
 
        mainwin->show();
192
 
 
193
 
        JabUpdate ju = s->serv()->getJU();
194
 
        jab_statusUpdate(s, &ju);
195
 
 
196
 
        emitLocalUpdate(*s->serv()->userEntry());
197
 
 
198
 
        idle.start();
199
 
        connect(&idle, SIGNAL(secondsIdle(int)), s, SLOT(secondsIdle(int)));
200
 
 
201
 
        // empty?
202
 
        if(s->acc()->host.isEmpty()) {
203
 
                jsm.modify(s);
204
 
        }
205
 
        else {
206
 
                if(s->acc()->opt_auto)
207
 
                        statusMenuChanged(STATUS_ONLINE);
208
 
        }
209
 
 
210
 
        return TRUE;
211
 
}
212
 
 
213
 
jabcon::~jabcon()
214
 
{
215
 
        // serv not created yet?  don't bother destructing
216
 
        if(!s->serv())
217
 
                return;
218
 
 
219
 
        delete fileserv;
220
 
 
221
 
        //isDisconnecting = TRUE;
222
 
        ///serv->disc();
223
 
        s->acc()->roster = *s->serv()->getCurrentRoster();
224
 
        s->acc()->olr_string = s->serv()->getOLR();
225
 
 
226
 
        mwgeom.setX(mainwin->x());
227
 
        mwgeom.setY(mainwin->y());
228
 
        mwgeom.setWidth(mainwin->width());
229
 
        mwgeom.setHeight(mainwin->height());
230
 
        s->acc()->tog_offline = mainwin->cvlist->isShowOffline();
231
 
        s->acc()->tog_away = mainwin->cvlist->isShowAway();
232
 
        s->acc()->tog_agents = mainwin->cvlist->isShowAgents();
233
 
 
234
 
        delete mainwin;
235
 
 
236
 
        // delete all toplevel windows except log window (and QDesktopWidget..)
237
 
        QWidgetList *list = QApplication::topLevelWidgets();
238
 
        QWidgetListIt it(*list);
239
 
        for(QWidget *w; (w = it.current()); ++it) {
240
 
                if(w->isA("LogWindow") && (LogWindow *)w == debug_window)
241
 
                        continue;
242
 
                if(w->isA("QDesktopWidget") || w->isA("QWidget"))
243
 
                        continue;
244
 
 
245
 
                w->deleteLater();
246
 
        }
247
 
        delete list;
248
 
 
249
 
        pro.mwgeom = mwgeom;
250
 
        pro.lastStatusString = lastStatusString;
251
 
        pro.useSound = useSound;
252
 
 
253
 
        pro.acc.first() = *s->acc();
254
 
 
255
 
        pro.prefs = option;
256
 
        pro.toFile(pathToProfileConfig(activeProfile));
257
 
 
258
 
        // delete sessions
259
 
        jsm.clear();
260
 
 
261
 
        unloadPsiIconSet();
262
 
 
263
 
        debug_jabber = 0;
264
 
}
265
 
 
266
 
void jabcon::jab_connected(JabSession *)
267
 
{
268
 
        mainwin->setUsingSSL(s->serv()->isSSLEnabled());
269
 
}
270
 
 
271
 
void jabcon::jab_disconnected(JabSession *)
272
 
{
273
 
}
274
 
 
275
 
void jabcon::jab_statusUpdate(JabSession *, JabUpdate *x)
276
 
{
277
 
        // make a status string
278
 
        QString str = "";
279
 
        if(x->queuePending > 0)
280
 
                str.append(QString("[%1] - ").arg(x->queuePending));
281
 
        str.append(x->str);
282
 
 
283
 
        // show status in the gui
284
 
        mainwin->setInfo(str);
285
 
 
286
 
        pdb(DEBUG_JABCON, QString("[jabcon: status update: %1]\n").arg(str));
287
 
}
288
 
 
289
 
void jabcon::jab_error(JabSession *, JabError *err)
290
 
{
291
 
        if(err->type == JABERR_CONNECT) {
292
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("There was an error communicating with the Jabber server.\nReason: %1")).arg(err->msg));
293
 
                //delayedAskLogin();
294
 
        }
295
 
        else if(err->type == JABERR_AUTH) {
296
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("Authorization failed.\nReason: %1")).arg(err->msg));
297
 
                //delayedAskLogin();
298
 
        }
299
 
        else if(err->type == JABERR_CREATE) {
300
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("Failed to create the new account.\nThe server gave this reason: \"%1\"")).arg(err->msg));
301
 
                //delayedAskLogin();
302
 
        }
303
 
        else if(err->type == JABERR_DISCONNECT) {
304
 
                QMessageBox::critical(0, CAP(tr("Error")), tr("You have been disconnected from the Jabber server."));
305
 
        }
306
 
        else {
307
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("An unknown error occurred.\nType: %1\nReason: %2")).arg(err->type).arg(err->msg));
308
 
        }
309
 
}
310
 
 
311
 
void jabcon::jab_messageReceived(JabSession *, const JabMessage &_msg)
312
 
{
313
 
        // skip headlines?
314
 
        if(_msg.type == JABMESSAGE_HEADLINE && option.ignoreHeadline)
315
 
                return;
316
 
 
317
 
        Message msg;
318
 
        msg.from = _msg.from.full();
319
 
        if(_msg.type == JABMESSAGE_CHAT)
320
 
                msg.type = MESSAGE_CHAT;
321
 
        else if(_msg.type == JABMESSAGE_ERROR)
322
 
                msg.type = MESSAGE_ERROR;
323
 
        else if(_msg.type == JABMESSAGE_HEADLINE)
324
 
                msg.type = MESSAGE_HEADLINE;
325
 
        else
326
 
                msg.type = MESSAGE_NORM;
327
 
        msg.text = _msg.body;
328
 
        msg.subject = _msg.subject;
329
 
        msg.url = _msg.url;
330
 
        msg.url_desc = _msg.url_desc;
331
 
        msg.timeStamp = _msg.timeStamp;
332
 
        msg.late = _msg.late;
333
 
        msg.originLocal = FALSE;
334
 
 
335
 
        pdb(DEBUG_JABCON, QString("[jabcon: message received from %1]\n").arg(msg.from));
336
 
 
337
 
 
338
 
        if(option.incomingAs == 1)
339
 
                msg.type = MESSAGE_NORM;
340
 
        else if(option.incomingAs == 2)
341
 
                msg.type = MESSAGE_CHAT;
342
 
 
343
 
        if(msg.type == MESSAGE_CHAT) {
344
 
                ChatDlg *c = ChatDlg::find(msg.from);
345
 
                if(c && !c->isHidden()) {
346
 
                        c->incomingMessage(msg);
347
 
                        doOnEvent(option.onevent[eChat2]);
348
 
 
349
 
                        // since it never goes into the queue in this case, we need to log here
350
 
                        if(s->acc()->opt_log)
351
 
                                logMessage(msg);
352
 
                        return;
353
 
                }
354
 
        }
355
 
 
356
 
        //msg.unread = TRUE;
357
 
        if(msg.type == MESSAGE_ERROR) {
358
 
                msg.text = QString(tr("<big>[Error Message]</big><br>%1").arg(plain2rich(msg.text)));
359
 
        }
360
 
 
361
 
        insertMessage(msg);
362
 
}
363
 
 
364
 
void jabcon::jab_resourceAvailable(JabSession *s, const Jid &j, const JabResource &r)
365
 
{
366
 
        QString dstr = QString("[jabcon: resource available: jid=[%1], name=[%2], status=[%3] ]\n").arg(j.full()).arg(r.name).arg(status2txt(r.status));
367
 
        pdb(DEBUG_JABCON, dstr);
368
 
 
369
 
        JabRosterEntry *me = s->serv()->userEntry();
370
 
        if(jidcmp(me->jid, j.s())) {
371
 
                int oldStatus = s->localStatus;
372
 
 
373
 
                s->localStatus = me->local()->status;
374
 
                s->localStatusString = me->local()->statusString;
375
 
 
376
 
                pdb(DEBUG_JABCON, QString("presence: [%1]  total=%2\n").arg(status2txt(s->localStatus)).arg(me->res.count()));
377
 
 
378
 
                mainwin->decorateButton(s->localStatus);
379
 
                emitLocalUpdate(*me);
380
 
 
381
 
                // coming from offline to online
382
 
                if(oldStatus == STATUS_OFFLINE && s->localStatus != STATUS_OFFLINE) {
383
 
                        /*if(doNewUser) {
384
 
                                QMessageBox::information(0, CAP(tr("Success")), tr("New account was created and logged in successfully.  Enjoy."));
385
 
                                doNewUser = FALSE;
386
 
 
387
 
                                // allow the new user to set account info
388
 
                                openAccountInfo();
389
 
                        }*/
390
 
 
391
 
                        QTimer::singleShot(15000, this, SLOT(enableOnEventOnline()));
392
 
 
393
 
                        JT_VCard *j = new JT_VCard(s->serv()->ioUser());
394
 
                        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotCheckVCard(JabTask *)));
395
 
                        j->get(s->acc()->jid());
396
 
                        j->go();
397
 
                }
398
 
        }
399
 
        else {
400
 
                UserListItem *item = s->userlist.findAnyByJid(j.s());
401
 
                if(!item)
402
 
                        return;
403
 
 
404
 
                // if the contact was offline before this, then do things
405
 
                bool doThings = FALSE;
406
 
                if(item->state == STATUS_OFFLINE && s->onEventOnlineOk)
407
 
                        doThings = TRUE;
408
 
 
409
 
                //item->state = r.status;
410
 
                //item->statusString = r.statusString;
411
 
 
412
 
                // add/update resource
413
 
                JabResource *r2 = item->res.find(r.name);
414
 
                if(!r2) {
415
 
                        r2 = new JabResource(r);
416
 
                        item->res.append(r2);
417
 
                }
418
 
                else {
419
 
                        *r2 = r;
420
 
                }
421
 
 
422
 
                // contact view should reflect the priority status
423
 
                item->state = item->res.priority()->status;
424
 
                item->statusString = item->res.priority()->statusString;
425
 
 
426
 
                // update gui
427
 
                cvp->updateEntry(*item);
428
 
                //mainwin->cvlist->updateEntry(item->jid, jidnick(item->jid, item->nick), item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
429
 
 
430
 
                if(doThings) {
431
 
                        doOnEvent(option.onevent[eOnline]);
432
 
 
433
 
                        if(option.rosterAnim)
434
 
                                cvp->animateNick(item->jid);
435
 
                }
436
 
 
437
 
                // tell the world
438
 
                emitContact(item);
439
 
        }
440
 
}
441
 
 
442
 
void jabcon::jab_resourceUnavailable(JabSession *s, const Jid &j)
443
 
{
444
 
        QString dstr = QString("[jabcon: resource unavailable: jid=[%1], name=[%2] ]\n").arg(j.s()).arg(j.resource());
445
 
        pdb(DEBUG_JABCON, dstr);
446
 
 
447
 
        JabRosterEntry *me = s->serv()->userEntry();
448
 
        if(jidcmp(me->jid, j.s())) {
449
 
                // only go offline if the local resource goes offline
450
 
                if(!me->local()) {
451
 
                        s->localStatus = STATUS_OFFLINE;
452
 
                        s->localStatusString = "";
453
 
 
454
 
                        pdb(DEBUG_JABCON, QString("presence: [%1]  total=%2\n").arg(status2txt(s->localStatus)).arg(me->res.count()));
455
 
 
456
 
                        mainwin->setUsingSSL(FALSE);
457
 
 
458
 
                        mainwin->decorateButton(s->localStatus);
459
 
                        emitLocalUpdate(*me);
460
 
 
461
 
                        // restore original account and delete contacts if guestMode
462
 
                        /*if(guestMode) {
463
 
                                pdb(DEBUG_JABCON, "Return from GUESTMODE\n");
464
 
                                acc = oldacc;
465
 
                                guestMode = FALSE;
466
 
 
467
 
                                s->serv()->reset();
468
 
                                cvp->clear();
469
 
                        }*/
470
 
                }
471
 
                else
472
 
                        emitLocalUpdate(*me);
473
 
        }
474
 
        else {
475
 
                JabRosterEntry *entry = s->serv()->findByJid(j.s());
476
 
                if(!entry)
477
 
                        return;
478
 
                UserListItem *item = s->userlist.findAnyByJid(j.s());
479
 
                if(!item)
480
 
                        return;
481
 
 
482
 
                // if the contact was not offline before this, then sound
483
 
                if(item->state != STATUS_OFFLINE && !s->isDisconnecting)
484
 
                        doOnEvent(option.onevent[eOffline]);
485
 
 
486
 
                // still available?  use next highest resource
487
 
                if(entry->isAvailable()) {
488
 
                        JabResource *r = entry->res.priority();
489
 
                        item->state = r->status;
490
 
                        item->statusString = r->statusString;
491
 
                }
492
 
                // not available
493
 
                else {
494
 
                        item->state = STATUS_OFFLINE;
495
 
                        item->statusString = entry->unavailableStatusString;
496
 
                }
497
 
 
498
 
                // remove resource
499
 
                JabResource *r = item->res.find(j.resource());
500
 
                if(r)
501
 
                        item->res.remove(r);
502
 
 
503
 
                // update gui
504
 
                cvp->updateEntry(*item);
505
 
                //mainwin->cvlist->updateEntry(item->jid, jidnick(item->jid, item->nick), item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
506
 
 
507
 
                // tell the world
508
 
                emitContact(item);
509
 
        }
510
 
}
511
 
 
512
 
void jabcon::jab_contactChanged(JabSession *, JabRosterEntry *entry)
513
 
{
514
 
        QString dstr = QString("[jabcon: roster changed: %1]\n").arg(entry->jid);
515
 
        pdb(DEBUG_JABCON, dstr);
516
 
 
517
 
        // this should never fail...
518
 
        UserListItem *item = s->userlist.findAnyByJid(cleanJid(entry->jid));
519
 
        if(!item)
520
 
                return;
521
 
 
522
 
        // copy into userlist
523
 
        item->nick = entry->nick;
524
 
        if(entry->groups.isEmpty())
525
 
                item->group = "";
526
 
        else
527
 
                item->group = entry->groups[0];
528
 
        item->sub = entry->sub;
529
 
 
530
 
        QString nick = item->nick.isEmpty() ? item->jid: item->nick;
531
 
 
532
 
        // update gui
533
 
        cvp->updateEntry(*item);
534
 
        //mainwin->cvlist->updateEntry(item->jid, nick, item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
535
 
 
536
 
        // tell the world
537
 
        emitContact(item);
538
 
}
539
 
 
540
 
void jabcon::jab_contactNew(JabSession *, JabRosterEntry *entry)
541
 
{
542
 
        // we probably already have the entry in our list somewhere,
543
 
        //  because of the authorization transaction
544
 
        UserListItem *item = s->userlist.findAnyByJid(cleanJid(entry->jid));
545
 
 
546
 
        if(item) {
547
 
                item->inList = TRUE;
548
 
                cvp->updateEntry(*item);
549
 
                //mainwin->cvlist->updateEntry(item->jid, jidnick(item->jid, item->nick), item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
550
 
        }
551
 
        else {
552
 
                // add to the userlist
553
 
                UserListItem *item = new UserListItem;
554
 
                item->jid = cleanJid(entry->jid);
555
 
                item->nick = entry->nick;
556
 
                if(entry->groups.isEmpty())
557
 
                        item->group = "";
558
 
                else
559
 
                        item->group = entry->groups[0];
560
 
                item->sub = entry->sub;
561
 
 
562
 
                item->state = STATUS_OFFLINE;
563
 
                item->isTransport = jidIsAgent(item->jid);
564
 
 
565
 
                // a new entry that is "push"?  [pushinfo]
566
 
                /*if(entry->push) {
567
 
                        VCard info;
568
 
                        if(readUserInfo(entry->jid, &info) && !info.field[vNickname].isEmpty()) {
569
 
                                item->nick = info.field[vNickname];
570
 
                        }
571
 
                        else {
572
 
                                if(localStatus != STATUS_OFFLINE)
573
 
                                        serv->getVCard(item->jid);
574
 
                        }
575
 
                }*/
576
 
 
577
 
                s->userlist.add(item);
578
 
 
579
 
                // add to the contact gui
580
 
                cvp->updateEntry(*item);
581
 
                //mainwin->cvlist->updateEntry(item->jid, jidnick(item->jid, item->nick), item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
582
 
 
583
 
                // search for unread messages and queue them
584
 
                //queueUnread(item);
585
 
        }
586
 
 
587
 
        pdb(DEBUG_JABCON, QString("[jabcon: %1 has been added to the internal contact list]\n").arg(entry->jid));
588
 
}
589
 
 
590
 
void jabcon::jab_contactRemove(JabSession *, JabRosterEntry *entry)
591
 
{
592
 
        UserListItem *item = s->userlist.findAnyByJid(cleanJid(entry->jid));
593
 
        if(!item)
594
 
                return;
595
 
 
596
 
        // remove all events from the queue (and tell mainwin)
597
 
        events.removeAll(item->jid);
598
 
        int next_type = 0;
599
 
        int next_amount = events.QPtrList<EventItem>::count();
600
 
        if(next_amount > 0)
601
 
                next_type = events.peekNext()->msg.type;
602
 
        mainwin->updateReadNext(next_type, next_amount);
603
 
 
604
 
        // remove from the contact gui
605
 
        cvp->removeEntry(item->jid);
606
 
 
607
 
        // remove from the userlist
608
 
        s->userlist.remove(item->jid);
609
 
 
610
 
        pdb(DEBUG_JABCON, QString("[jabcon: %1 has been removed from the internal contact list]\n").arg(entry->jid));
611
 
}
612
 
 
613
 
void jabcon::jab_authRequest(JabSession *, const Jid &from)
614
 
{
615
 
        QString body(tr("<big>[System Message]</big><br>This user wants to subscribe to your presence.  Click the button labelled \"Add/Auth\" to authorize the subscription.  This will also add the person to your contact list if it is not already there."));
616
 
 
617
 
        Message tmp;
618
 
        tmp.from = from.full();
619
 
        tmp.type = MESSAGE_AUTHREQ;
620
 
        tmp.text = body;
621
 
        tmp.timeStamp = QDateTime::currentDateTime();
622
 
        tmp.late = FALSE;
623
 
        tmp.originLocal = FALSE;
624
 
        insertMessage(tmp);
625
 
 
626
 
        pdb(DEBUG_JABCON, QString("[jabcon: %1 wants to add you to their list]\n").arg(from.full()));
627
 
}
628
 
 
629
 
void jabcon::jab_authGrant(JabSession *, const Jid &from)
630
 
{
631
 
        QString body(tr("<big>[System Message]</big><br>You are now authorized."));
632
 
 
633
 
        Message tmp;
634
 
        tmp.from = from.full();
635
 
        tmp.type = MESSAGE_SYS;
636
 
        tmp.text = body;
637
 
        tmp.timeStamp = QDateTime::currentDateTime();
638
 
        tmp.late = FALSE;
639
 
        tmp.originLocal = FALSE;
640
 
        insertMessage(tmp);
641
 
 
642
 
        pdb(DEBUG_JABCON, QString("[jabcon: %1 has authorized you]\n").arg(from.full()));
643
 
}
644
 
 
645
 
void jabcon::jab_authRemove(JabSession *, const Jid &from)
646
 
{
647
 
        // only send a sysmessage if it's done by the other person
648
 
        //  (because we get this signal when we remove contacts too.  I think...)
649
 
        if(s->userlist.findAnyByJid(from.s())) {
650
 
                QString body(tr("<big>[System Message]</big><br>Your authorization has been removed!"));
651
 
 
652
 
                Message tmp;
653
 
                tmp.from = from.full();
654
 
                tmp.type = MESSAGE_SYS;
655
 
                tmp.text = body;
656
 
                tmp.timeStamp = QDateTime::currentDateTime();
657
 
                tmp.late = FALSE;
658
 
                tmp.originLocal = FALSE;
659
 
                insertMessage(tmp);
660
 
        }
661
 
 
662
 
        pdb(DEBUG_JABCON, QString("[jabcon: %1 has removed you from their contact list]\n").arg(from.full()));
663
 
}
664
 
 
665
 
 
666
 
 
667
 
/*void jabcon::servConnected()
668
 
{
669
 
        mainwin->setUsingSSL(serv->isSSLEnabled());
670
 
}
671
 
 
672
 
void jabcon::servDisconnected()
673
 
{
674
 
        isDisconnecting = TRUE;
675
 
}
676
 
 
677
 
void jabcon::statusUpdate(JabUpdate *x)
678
 
{
679
 
        // make a status string
680
 
        QString str = "";
681
 
        if(x->queuePending > 0)
682
 
                str.append(QString("[%1] - ").arg(x->queuePending));
683
 
        str.append(x->str);
684
 
 
685
 
        // show status in the gui
686
 
        mainwin->setInfo(str);
687
 
 
688
 
        pdb(DEBUG_JABCON, QString("[jabcon: status update: %1]\n").arg(str));
689
 
}
690
 
*/
691
 
 
692
 
void jabcon::insertMessage(const Message &msg, bool doLog)
693
 
{
694
 
        // sound
695
 
        if(msg.type == MESSAGE_CHAT) {
696
 
                // any pending chats from this person?
697
 
                bool found = FALSE;
698
 
                UserListItem *i = s->userlist.findAnyByJid(msg.from);
699
 
                if(i)
700
 
                        found = events.hasChats(i->jid);
701
 
 
702
 
                doOnEvent(option.onevent[found ? eChat2: eChat1]);
703
 
        }
704
 
        else if(msg.type == MESSAGE_NORM)
705
 
                doOnEvent(option.onevent[eMessage]);
706
 
        else if(msg.type == MESSAGE_HEADLINE)
707
 
                doOnEvent(option.onevent[eMessage]);
708
 
        else
709
 
                doOnEvent(option.onevent[eSystem]);
710
 
 
711
 
        queueMessage(msg);
712
 
 
713
 
        if(doLog) {
714
 
                if(s->acc()->opt_log)
715
 
                        logMessage(msg);
716
 
        }
717
 
 
718
 
        // cvlist alert
719
 
        cvp->showAlert(cleanJid(msg.from), msg.type);
720
 
 
721
 
        // update mainwin status
722
 
        mainwin->updateReadNext(events.peekNext()->msg.type, events.QPtrList<EventItem>::count());
723
 
 
724
 
        // messagebox open?
725
 
        EventDlg *e = EventDlg::find(msg.from);
726
 
        if(e) {
727
 
                UserListItem *item = s->userlist.findAnyByJid(msg.from);
728
 
                e->updateReadNext(events.peek(item->jid)->msg.type, events.count(item->jid));
729
 
        }
730
 
 
731
 
        if((msg.type != MESSAGE_CHAT && option.popupMsgs) ||
732
 
           (msg.type == MESSAGE_CHAT && option.popupChats)) {
733
 
                UserListItem *i = s->userlist.findAnyByJid(msg.from);
734
 
                if(i) {
735
 
                        readMessage(i);
736
 
                }
737
 
        }
738
 
        if(option.raise) {
739
 
//#ifdef Q_WS_WIN
740
 
//              win_raiseNoFocus(mainwin);
741
 
//#else
742
 
                mainwin->showNormal();
743
 
                mainwin->raise();
744
 
//#endif
745
 
        }
746
 
}
747
 
 
748
 
void jabcon::actionDefault(ContactViewItem *item)
749
 
{
750
 
        UserListItem *i = s->userlist.findAnyByJid(item->jid());
751
 
        if(!i)
752
 
                return;
753
 
 
754
 
        //c->erasePrompt();
755
 
        QString dstr; dstr.sprintf("[jabcon: default action: %s]\n", i->jid.latin1());
756
 
        pdb(DEBUG_JABCON, dstr);
757
 
 
758
 
        //c->reshowPrompt();
759
 
 
760
 
        // pending messages from this person?
761
 
        if(events.count(i->jid) > 0) {
762
 
                readMessage(i);
763
 
        }
764
 
        else {
765
 
                if(option.defaultAction == 0)
766
 
                        composeMessage(i, FALSE);
767
 
                else
768
 
                        openChat(i);
769
 
        }
770
 
}
771
 
 
772
 
void jabcon::actionSendMessage(ContactViewItem *item)
773
 
{
774
 
        UserListItem *i = s->userlist.findAnyByJid(item->jid());
775
 
        if(!i)
776
 
                return;
777
 
        composeMessage(i, FALSE);
778
 
}
779
 
 
780
 
void jabcon::actionSendMessage(const QStringList &list)
781
 
{
782
 
        EventDlg *c = new EventDlg(list, s->localStatus);
783
 
        connect(c, SIGNAL(aSend(const Message &)), SLOT(sendMessage(const Message &)));
784
 
        connect(c, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
785
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), c, SLOT(localUpdate(const JabRosterEntry &)));
786
 
        connect(this, SIGNAL(emitOptionsUpdate()), c, SLOT(optionsUpdate()));
787
 
 
788
 
        c->show();
789
 
}
790
 
 
791
 
void jabcon::actionSendUrl(const QString &jid)
792
 
{
793
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
794
 
        if(!i)
795
 
                return;
796
 
        composeMessage(i, TRUE);
797
 
}
798
 
 
799
 
void jabcon::actionRecvEvent(ContactViewItem *item)
800
 
{
801
 
        UserListItem *i = s->userlist.findAnyByJid(item->jid());
802
 
        if(!i)
803
 
                return;
804
 
 
805
 
        if(events.count(i->jid) > 0)
806
 
                readMessage(i);
807
 
}
808
 
 
809
 
void jabcon::actionRemove(const QString &jid)
810
 
{
811
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
812
 
        if(!i)
813
 
                return;
814
 
 
815
 
        userRemove(i);
816
 
}
817
 
 
818
 
void jabcon::actionRename(ContactViewItem *item, const QString &newname)
819
 
{
820
 
        UserListItem *i = s->userlist.findAnyByJid(item->jid());
821
 
        if(!i) {
822
 
                return;
823
 
        }
824
 
 
825
 
        userRename(i, newname);
826
 
}
827
 
 
828
 
void jabcon::actionGroupChange(const QString &jid, const QString &groupname)
829
 
{
830
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
831
 
        if(!i) {
832
 
                return;
833
 
        }
834
 
        if(!i->inList) {
835
 
                return;
836
 
        }
837
 
 
838
 
        userGroupChange(i, groupname);
839
 
}
840
 
 
841
 
void jabcon::actionGroupRename(const QString &oldname, const QString &groupname)
842
 
{
843
 
        // change all users in this group to use the new name
844
 
        UserListItem *i = s->userlist.start(USERLIST_CONTACTS);
845
 
        for(; i ; i = s->userlist.next()) {
846
 
                if(oldname == i->group)
847
 
                        userGroupChange(i, groupname);
848
 
        }
849
 
        s->userlist.end();
850
 
}
851
 
 
852
 
void jabcon::actionHistory(const QString &jid)
853
 
{
854
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
855
 
        if(!i)
856
 
                return;
857
 
 
858
 
        // search for an already opened historydlg
859
 
        HistoryDlg *hist = HistoryDlg::find(i->jid);
860
 
 
861
 
        if(hist) {
862
 
                bringToFront(hist);
863
 
        }
864
 
        else {
865
 
                hist = new HistoryDlg(i, 0, "HistoryDlg");
866
 
                connect(hist, SIGNAL(openMessage(const Message &)), SLOT(actionHistoryBox(const Message &)));
867
 
                hist->show();
868
 
        }
869
 
}
870
 
 
871
 
// quick and dirty
872
 
void jabcon::actionHistoryBox(const Message &msg)
873
 
{
874
 
        UserListItem tmp;
875
 
 
876
 
        tmp.jid = msg.from;
877
 
 
878
 
        // create the messagebox
879
 
        EventDlg *e = new EventDlg(&tmp, s->localStatus, FALSE, FALSE);
880
 
        connect(e, SIGNAL(aReply(const QString &)), SLOT(composeMessage(const QString &)));
881
 
        connect(e, SIGNAL(aReadNext(const QString &)), SLOT(processReadNext(const QString &)));
882
 
        connect(e, SIGNAL(aAuth(const QString &)), SLOT(userAddAuth(const QString &)));
883
 
        connect(e, SIGNAL(aInfo(const QString &)), SLOT(actionInfo(const QString &)));
884
 
        connect(e, SIGNAL(aHistory(const QString &)), SLOT(actionHistory(const QString &)));
885
 
        connect(e, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
886
 
        connect(this, SIGNAL(emitContact(UserListItem *)), e, SLOT(updateContact(UserListItem *)));
887
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), e, SLOT(localUpdate(const JabRosterEntry &)));
888
 
        connect(this, SIGNAL(emitOptionsUpdate()), e, SLOT(optionsUpdate()));
889
 
 
890
 
        // load the message
891
 
        //processReadNext(item);
892
 
        e->updateEvent(msg);
893
 
 
894
 
        e->show();
895
 
}
896
 
 
897
 
void jabcon::actionStatusShow(const QString &jid)
898
 
{
899
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
900
 
        if(!i)
901
 
                return;
902
 
 
903
 
        StatusShowDlg *w = new StatusShowDlg(i);
904
 
        w->show();
905
 
}
906
 
 
907
 
void jabcon::actionAuthorize(ContactViewItem *item)
908
 
{
909
 
        userAuthorize(item->jid());
910
 
}
911
 
 
912
 
void jabcon::actionAdd(ContactViewItem *item)
913
 
{
914
 
        userAdd(item->jid());
915
 
}
916
 
 
917
 
void jabcon::actionOpenChat(const QString &jid)
918
 
{
919
 
        UserListItem *item = s->userlist.findAnyByJid(jid);
920
 
        if(!item)
921
 
                return;
922
 
 
923
 
        openChat(item);
924
 
}
925
 
 
926
 
void jabcon::actionInfo(const QString &jid)
927
 
{
928
 
        InfoDlg *w = InfoDlg::find(jid);
929
 
        if(w) {
930
 
                bringToFront(w);
931
 
        }
932
 
        else {
933
 
                Info *p = InfoBank::get(jid);
934
 
                VCard tmp;
935
 
                if(p)
936
 
                        tmp = p->vcard;
937
 
 
938
 
                w = new InfoDlg(INFODLG_CONTACT, jid, tmp, s->localStatus, 0);
939
 
                connect(w, SIGNAL(signalGetVCard(const QString &, QString *)), SLOT(slotGetVCard(const QString &, QString *)));
940
 
                connect(w, SIGNAL(signalCancelTransaction(const QString &)), SLOT(slotCancelTransaction(const QString &)));
941
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), w, SLOT(localUpdate(const JabRosterEntry &)));
942
 
                w->show();
943
 
 
944
 
                // automatically retrieve info if it doesn't exist
945
 
                if(!p)
946
 
                        w->doRefresh();
947
 
        }
948
 
}
949
 
 
950
 
 
951
 
ChatDlg *jabcon::ensureChatDlg(const QString &jid)
952
 
{
953
 
        UserListItem *item = s->userlist.findAnyByJid(jid);
954
 
        if(!item)
955
 
                return 0;
956
 
 
957
 
        ChatDlg *c = ChatDlg::find(jid);
958
 
        if(!c) {
959
 
                // create the chatbox
960
 
                c = new ChatDlg(item, s->acc()->user, s->localStatus);
961
 
                connect(c, SIGNAL(aSend(const Message &)), SLOT(sendMessage(const Message &)));
962
 
                connect(c, SIGNAL(aInfo(const QString &)), SLOT(actionInfo(const QString &)));
963
 
                connect(c, SIGNAL(aHistory(const QString &)), SLOT(actionHistory(const QString &)));
964
 
                connect(c, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
965
 
                connect(this, SIGNAL(emitContact(UserListItem *)), c, SLOT(updateContact(UserListItem *)));
966
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), c, SLOT(localUpdate(const JabRosterEntry &)));
967
 
                connect(this, SIGNAL(emitOptionsUpdate()), c, SLOT(optionsUpdate()));
968
 
        }
969
 
 
970
 
        return c;
971
 
}
972
 
 
973
 
void jabcon::openChat(UserListItem *item)
974
 
{
975
 
        ChatDlg *c = ensureChatDlg(item->jid);
976
 
        if(!c)
977
 
                return; // not quite sure how this would happen
978
 
 
979
 
        processChats(item);
980
 
 
981
 
        if(c->isHidden())
982
 
                c->show();
983
 
 
984
 
        bringToFront(c);
985
 
}
986
 
 
987
 
void jabcon::readMessage(UserListItem *item)
988
 
{
989
 
        Message *m = &events.peek(item->jid)->msg;
990
 
        if(m->type == MESSAGE_CHAT) {
991
 
                openChat(item);
992
 
        }
993
 
        else {
994
 
                // search for an already opened messagebox
995
 
                EventDlg *e = EventDlg::find(item->jid);
996
 
 
997
 
                if(e) {
998
 
                        bringToFront(e);
999
 
                }
1000
 
                else {
1001
 
                        // create the messagebox
1002
 
                        e = new EventDlg(item, s->localStatus, TRUE, FALSE);
1003
 
                        connect(e, SIGNAL(aReply(const QString &)), SLOT(composeMessage(const QString &)));
1004
 
                        connect(e, SIGNAL(aReadNext(const QString &)), SLOT(processReadNext(const QString &)));
1005
 
                        connect(e, SIGNAL(aAuth(const QString &)), SLOT(userAddAuth(const QString &)));
1006
 
                        connect(e, SIGNAL(aInfo(const QString &)), SLOT(actionInfo(const QString &)));
1007
 
                        connect(e, SIGNAL(aHistory(const QString &)), SLOT(actionHistory(const QString &)));
1008
 
                        connect(e, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
1009
 
 
1010
 
                        connect(this, SIGNAL(emitContact(UserListItem *)), e, SLOT(updateContact(UserListItem *)));
1011
 
                        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), e, SLOT(localUpdate(const JabRosterEntry &)));
1012
 
                        connect(this, SIGNAL(emitOptionsUpdate()), e, SLOT(optionsUpdate()));
1013
 
 
1014
 
                        // load the message
1015
 
                        processReadNext(item);
1016
 
 
1017
 
                        e->show();
1018
 
                        bringToFront(e);
1019
 
                }
1020
 
        }
1021
 
}
1022
 
 
1023
 
void jabcon::composeMessage(const QString &jid)
1024
 
{
1025
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
1026
 
        if(!i) {
1027
 
                QStringList tmp;
1028
 
                tmp += jid;
1029
 
                actionSendMessage(tmp);
1030
 
                return;
1031
 
        }
1032
 
        composeMessage(i, FALSE);
1033
 
}
1034
 
 
1035
 
void jabcon::composeMessage(UserListItem *item, bool urlMode)
1036
 
{
1037
 
        EventDlg *c = new EventDlg(item, s->localStatus, FALSE, TRUE, urlMode);
1038
 
        connect(c, SIGNAL(aSend(const Message &)), SLOT(sendMessage(const Message &)));
1039
 
        connect(c, SIGNAL(aInfo(const QString &)), SLOT(actionInfo(const QString &)));
1040
 
        connect(c, SIGNAL(aHistory(const QString &)), SLOT(actionHistory(const QString &)));
1041
 
        connect(c, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
1042
 
        connect(this, SIGNAL(emitContact(UserListItem *)), c, SLOT(updateContact(UserListItem *)));
1043
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), c, SLOT(localUpdate(const JabRosterEntry &)));
1044
 
        connect(this, SIGNAL(emitOptionsUpdate()), c, SLOT(optionsUpdate()));
1045
 
 
1046
 
        c->show();
1047
 
}
1048
 
 
1049
 
void jabcon::composeBlankMessage()
1050
 
{
1051
 
        QStringList to;
1052
 
 
1053
 
        EventDlg *c = new EventDlg(to, s->localStatus);
1054
 
        connect(c, SIGNAL(aSend(const Message &)), SLOT(sendMessage(const Message &)));
1055
 
        connect(c, SIGNAL(aOpenURL(const QString &)), SLOT(actionOpenURL(const QString &)));
1056
 
        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), c, SLOT(localUpdate(const JabRosterEntry &)));
1057
 
        connect(this, SIGNAL(emitOptionsUpdate()), c, SLOT(optionsUpdate()));
1058
 
 
1059
 
        c->show();
1060
 
}
1061
 
 
1062
 
void jabcon::sendMessage(const Message &msg)
1063
 
{
1064
 
        JabMessage tmp;
1065
 
        tmp.to = msg.to;
1066
 
        if(msg.type == MESSAGE_CHAT)
1067
 
                tmp.type = JABMESSAGE_CHAT;
1068
 
        else
1069
 
                tmp.type = JABMESSAGE_NORM;
1070
 
        tmp.body = msg.text;
1071
 
        tmp.subject = msg.subject;
1072
 
        tmp.url = msg.url;
1073
 
        tmp.url_desc = msg.url_desc;
1074
 
 
1075
 
        s->serv()->sendMessage(tmp);
1076
 
 
1077
 
        QString dstr; dstr.sprintf("jabcon: message sent to %s.\n", msg.to.latin1());
1078
 
        pdb(DEBUG_JABCON, dstr);
1079
 
 
1080
 
        if(s->acc()->opt_log) {
1081
 
                MessageHistory log(msg.to, HISTORY_WRITE);
1082
 
                log.writeEntry(msg);
1083
 
        }
1084
 
 
1085
 
        doOnEvent(option.onevent[eSend]);
1086
 
 
1087
 
        // auto close an open messagebox (if non-chat)
1088
 
        if(msg.type != MESSAGE_CHAT) {
1089
 
                EventDlg *e = EventDlg::find(msg.to);
1090
 
                if(e) {
1091
 
                        e->closeAfterReply();
1092
 
                }
1093
 
        }
1094
 
}
1095
 
 
1096
 
void jabcon::processReadNext(const QString &jid)
1097
 
{
1098
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
1099
 
        if(!i)
1100
 
                return;
1101
 
        processReadNext(i);
1102
 
}
1103
 
 
1104
 
void jabcon::processReadNext(UserListItem *item)
1105
 
{
1106
 
        EventDlg *e = EventDlg::find(item->jid);
1107
 
        if(!e) {
1108
 
                // this should NEVER happen
1109
 
                return;
1110
 
        }
1111
 
 
1112
 
        // look at the message
1113
 
        Message *m = &events.peek(item->jid)->msg;
1114
 
 
1115
 
        // if it's a chat message, just open the chat window.  there is no need to do
1116
 
        // further processing.  the chat window will remove it from the queue, de-alert
1117
 
        // the cvlist, etc etc.
1118
 
        if(m->type == MESSAGE_CHAT) {
1119
 
                openChat(item);
1120
 
                return;
1121
 
        }
1122
 
 
1123
 
        // remove from queue
1124
 
        EventItem *ei = events.dequeue(item->jid);
1125
 
        //flagAsRead(ei->msg);
1126
 
 
1127
 
 
1128
 
        // update the eventdlg
1129
 
        e->updateEvent(ei->msg);
1130
 
        delete ei;
1131
 
 
1132
 
        // update eventdlg's read-next
1133
 
        int nexttype = 0;
1134
 
        if(events.count(item->jid) > 0)
1135
 
                nexttype = events.peek(item->jid)->msg.type;
1136
 
        e->updateReadNext(nexttype, events.count(item->jid));
1137
 
 
1138
 
        // clear the cvlist alert
1139
 
        cvp->clearAlert(item->jid);
1140
 
 
1141
 
        // mainwin status
1142
 
        int next_type = 0;
1143
 
        int next_amount = events.QPtrList<EventItem>::count();
1144
 
        if(next_amount > 0)
1145
 
                next_type = events.peekNext()->msg.type;
1146
 
        mainwin->updateReadNext(next_type, next_amount);
1147
 
}
1148
 
 
1149
 
void jabcon::processChats(const QString &jid)
1150
 
{
1151
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
1152
 
        if(!i)
1153
 
                return;
1154
 
        processChats(i);
1155
 
}
1156
 
 
1157
 
void jabcon::processChats(UserListItem *item)
1158
 
{
1159
 
        ChatDlg *c = ChatDlg::find(item->jid);
1160
 
        if(!c) {
1161
 
                // this should NEVER happen
1162
 
                return;
1163
 
        }
1164
 
        pdb(DEBUG_JABCON, "jabcon: processChats: processing chats.\n");
1165
 
 
1166
 
        // extract the chats and position list
1167
 
        QPtrList<Message> chatList;
1168
 
        QValueList<int> pos;
1169
 
        events.extractChats(item->jid, &chatList, &pos);
1170
 
 
1171
 
        // dump the chats into the chat window, and remove the related cvlist alerts
1172
 
        chatList.setAutoDelete(TRUE);
1173
 
        QValueList<int>::Iterator it = pos.begin();
1174
 
        int modifier = 0;
1175
 
        for(Message *m = chatList.last(); m;) {
1176
 
                // process the message
1177
 
                //flagAsRead(*m);
1178
 
                c->incomingMessage(*m);
1179
 
 
1180
 
                // delete it
1181
 
                chatList.remove();
1182
 
 
1183
 
                // deleting an item at the end means we need to move up to the new "last"
1184
 
                m = chatList.last();
1185
 
 
1186
 
                // clear the alert
1187
 
                int x = (*it) - modifier;
1188
 
                //printf("deleting entry %d (pos=%d,mod=%d)\n", x, (*it), modifier);
1189
 
                cvp->clearAlert(item->jid, x);
1190
 
                ++it;
1191
 
                ++modifier;
1192
 
        }
1193
 
 
1194
 
        // if there is an associated eventdlg, make sure that gets updated also
1195
 
        EventDlg *e = EventDlg::find(item->jid);
1196
 
        if(e) {
1197
 
                int nexttype = 0;
1198
 
                if(events.count(item->jid) > 0)
1199
 
                        nexttype = events.peek(item->jid)->msg.type;
1200
 
 
1201
 
                e->updateReadNext(nexttype, events.count(item->jid));
1202
 
        }
1203
 
 
1204
 
        // mainwin status
1205
 
        int next_type = 0;
1206
 
        int next_amount = events.QPtrList<EventItem>::count();
1207
 
        if(next_amount > 0)
1208
 
                next_type = events.peekNext()->msg.type;
1209
 
        mainwin->updateReadNext(next_type, next_amount);
1210
 
}
1211
 
 
1212
 
void jabcon::userRemove(UserListItem *i)
1213
 
{
1214
 
        if(!i->inList)
1215
 
                return;
1216
 
 
1217
 
        s->serv()->unsubscribe(i->jid);
1218
 
}
1219
 
 
1220
 
void jabcon::userRename(UserListItem *i, const QString &newname)
1221
 
{
1222
 
        if(i->nick == newname)
1223
 
                return;
1224
 
 
1225
 
        // setting to jidname?  remove nick
1226
 
        if(newname == i->jid)
1227
 
                i->nick = "";
1228
 
        else
1229
 
                i->nick = newname;
1230
 
 
1231
 
        emitContact(i);
1232
 
 
1233
 
        if(!i->inList)
1234
 
                return;
1235
 
 
1236
 
        s->serv()->setRoster(i->jid, i->nick, i->group);
1237
 
}
1238
 
 
1239
 
void jabcon::userGroupChange(UserListItem *i, const QString &groupname)
1240
 
{
1241
 
        i->group = groupname;
1242
 
 
1243
 
        s->serv()->setRoster(i->jid, i->nick, i->group);
1244
 
}
1245
 
 
1246
 
 
1247
 
/****************************************************************************
1248
 
  User functions
1249
 
****************************************************************************/
1250
 
void jabcon::userAdd(const QString &jid)
1251
 
{
1252
 
        // the contact should be in the local list someplace
1253
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
1254
 
        if(!i)
1255
 
                return;
1256
 
 
1257
 
        // put the user in our contact list (don't do this if it's already there)
1258
 
        if(!s->serv()->findByJid(i->jid))
1259
 
                s->serv()->setRoster(i->jid, i->nick, i->group);
1260
 
 
1261
 
        // request subscription
1262
 
        s->serv()->subscribe(i->jid);
1263
 
}
1264
 
 
1265
 
void jabcon::userAuthorize(const QString &jid)
1266
 
{
1267
 
        s->serv()->subscribed(jid);
1268
 
}
1269
 
 
1270
 
void jabcon::userAddAuth(const QString &jid)
1271
 
{
1272
 
        // the contact should be in the local list someplace
1273
 
        UserListItem *i = s->userlist.findAnyByJid(jid);
1274
 
        if(!i)
1275
 
                return;
1276
 
 
1277
 
        // authorize using authoritative jid (argument) instead of local jid (userlist)
1278
 
        userAuthorize(jid);
1279
 
 
1280
 
        // only add if it's _not_ a transport.
1281
 
        // this is a workaround for the AIM transport, which has status change
1282
 
        //  problems if fully subscribed.
1283
 
        if(!i->isTransport)
1284
 
                userAdd(jid);
1285
 
}
1286
 
 
1287
 
 
1288
 
 
1289
 
void jabcon::actionAdd(const QString &jid, const QString &nick, const QString &group)
1290
 
{
1291
 
        s->serv()->subscribe(jid);
1292
 
 
1293
 
        // skip setting the roster if it's already in the server contact list
1294
 
        if(s->serv()->findByJid(jid))
1295
 
                return;
1296
 
 
1297
 
        s->serv()->setRoster(jid, nick, group);
1298
 
}
1299
 
 
1300
 
void jabcon::agentSetStatus(const QString &jid, int status)
1301
 
{
1302
 
        UserListItem *i = s->userlist.findServiceByJid(jid);
1303
 
        if(!i)
1304
 
                return;
1305
 
 
1306
 
        s->serv()->agentSetStatus(jid, status);
1307
 
}
1308
 
 
1309
 
void jabcon::agentRemove(const QString &jid)
1310
 
{
1311
 
        UserListItem *i = s->userlist.findServiceByJid(jid);
1312
 
        if(!i)
1313
 
                return;
1314
 
 
1315
 
        s->serv()->unsubscribe(i->jid);
1316
 
        //userRemove(i);        // FIXME: i say this whole jabcon class needs help
1317
 
}
1318
 
 
1319
 
 
1320
 
void jabcon::queueMessage(const Message &msg)
1321
 
{
1322
 
        UserListItem *item = s->userlist.findAnyByJid(msg.from);
1323
 
 
1324
 
        // if not in list, add a temporary entry
1325
 
        if(!item) {
1326
 
                // add to the userlist
1327
 
                item = new UserListItem;
1328
 
                item->jid = cleanJid(msg.from);
1329
 
                item->nick = "";
1330
 
                item->group = "";
1331
 
                item->sub = "none";
1332
 
                item->state = STATUS_OFFLINE;
1333
 
                item->statusString = "";
1334
 
                item->inList = FALSE;
1335
 
                item->isTransport = jidIsAgent(item->jid);
1336
 
 
1337
 
                // treat it like a push  [pushinfo]
1338
 
                /*VCard info;
1339
 
                if(readUserInfo(item->jid, &info) && !info.field[vNickname].isEmpty())
1340
 
                        item->nick = info.field[vNickname];
1341
 
                else {
1342
 
                        if(localStatus != STATUS_OFFLINE)
1343
 
                                serv->getVCard(item->jid);
1344
 
                }*/
1345
 
 
1346
 
                s->userlist.add(item);
1347
 
        }
1348
 
 
1349
 
        // make sure the contact is displayed
1350
 
        QString nick = item->nick.isEmpty() ? item->jid: item->nick;
1351
 
        cvp->updateEntry(*item);
1352
 
        //mainwin->cvlist->updateEntry(item->jid, nick, item->group, item->sub, item->state, item->statusString, item->inList, item->isTransport, item->res);
1353
 
 
1354
 
        EventItem *ei = new EventItem;
1355
 
        ei->jid = item->jid;
1356
 
        ei->msg = msg;
1357
 
        events.enqueue(ei);
1358
 
 
1359
 
        QString dstr; dstr.sprintf("jabcon: queuing a message from %s.\n", msg.from.latin1());
1360
 
        pdb(DEBUG_JABCON, dstr);
1361
 
}
1362
 
 
1363
 
void jabcon::logMessage(const Message &msg)
1364
 
{
1365
 
        Message m(msg);
1366
 
        MessageHistory log(m.from, HISTORY_WRITE);
1367
 
 
1368
 
        // use a short message for AUTHREQ.   FIXME: this is not a good way to do it
1369
 
        if(msg.type == MESSAGE_AUTHREQ)
1370
 
                m.text = QString(tr("<big>[System Message]</big><br>This user wants to subscribe to your presence."));
1371
 
 
1372
 
        log.writeEntry(m);
1373
 
}
1374
 
 
1375
 
void jabcon::queueUnread(UserListItem *item)
1376
 
{
1377
 
        MessageHistory log(item->jid, HISTORY_FLAG);
1378
 
        Message *msg;
1379
 
        bool inZone = FALSE;
1380
 
 
1381
 
        // collect unread received messages into a QPtrList, save in reverse order
1382
 
        QPtrList<Message> unreadList;
1383
 
        unreadList.setAutoDelete(TRUE);
1384
 
        while(1) {
1385
 
                msg = log.readEntry();
1386
 
                if(!msg)
1387
 
                        break;
1388
 
 
1389
 
                if(!inZone) {
1390
 
                        // skip sent messages
1391
 
                        if(msg->originLocal) {
1392
 
                                delete msg;
1393
 
                                continue;
1394
 
                        }
1395
 
                }
1396
 
 
1397
 
                if(!msg->unread) {
1398
 
                        delete msg;
1399
 
                        break;
1400
 
                }
1401
 
 
1402
 
                unreadList.insert(0, msg);
1403
 
                inZone = TRUE;
1404
 
        }
1405
 
 
1406
 
        // empty?  bail
1407
 
        if(unreadList.isEmpty())
1408
 
                return;
1409
 
 
1410
 
        // now put them in the queue. don't log history
1411
 
        QPtrListIterator<Message> it(unreadList);
1412
 
        for(; (msg = it.current()); ++it)
1413
 
                insertMessage(*msg, FALSE);
1414
 
 
1415
 
        unreadList.clear();
1416
 
}
1417
 
 
1418
 
void jabcon::flagAsRead(const Message &msg)
1419
 
{
1420
 
        // messages must be read in sequence, so this message must be earliest.
1421
 
        // scan backwards until we find the last unread message.
1422
 
 
1423
 
        MessageHistory log(msg.from, HISTORY_FLAG);
1424
 
        Message *m;
1425
 
        bool inZone = FALSE;
1426
 
        int num = 0;
1427
 
 
1428
 
        while(1) {
1429
 
                //printf("reading entry\n");
1430
 
                m = log.readCurrent();
1431
 
                if(!m)
1432
 
                        break;
1433
 
                ++num;
1434
 
                //printf("read entry\n");
1435
 
 
1436
 
                if(!inZone) {
1437
 
                        // skip sent messages
1438
 
                        if(m->originLocal) {
1439
 
                                delete m;
1440
 
                                //printf("local message\n");
1441
 
                                log.stepForward();
1442
 
                                continue;
1443
 
                        }
1444
 
                }
1445
 
 
1446
 
                if(!m->unread) {
1447
 
                        delete m;
1448
 
                        //printf("read message\n");
1449
 
                        break;
1450
 
                }
1451
 
 
1452
 
                delete m;
1453
 
                //printf("unread message, going forward\n");
1454
 
                log.stepForward();
1455
 
                inZone = TRUE;
1456
 
        }
1457
 
 
1458
 
        //printf("num = %d\n", num);
1459
 
 
1460
 
        // no history...
1461
 
        if(num == 0)
1462
 
                return;
1463
 
 
1464
 
        // go back one message
1465
 
        //printf("stepping back\n");
1466
 
        log.stepBack();
1467
 
        log.setFlagsCurrent("**-*");
1468
 
}
1469
 
 
1470
 
// mainwin triggers this when the user alters the status button
1471
 
void jabcon::statusMenuChanged(int x)
1472
 
{
1473
 
        // setting to some type of online status
1474
 
        if(x != STATUS_OFFLINE) {
1475
 
                StatusInfo info;
1476
 
                info.type = x;
1477
 
                bool ok = FALSE;
1478
 
 
1479
 
                if(x == STATUS_ONLINE && !option.askOnline) {
1480
 
                        info.str = "";
1481
 
                        ok = TRUE;
1482
 
                }
1483
 
                else {
1484
 
                        if(lastStatusString.isEmpty())
1485
 
                                info.str = tr("I am away from my computer.  Please leave a message.");
1486
 
                        else
1487
 
                                info.str = lastStatusString; // default to old away message
1488
 
 
1489
 
                        if(StatusSetDlg::getStatus(&info)) {
1490
 
                                ok = TRUE;
1491
 
                                lastStatusString = info.str;
1492
 
                        }
1493
 
                }
1494
 
 
1495
 
                if(ok)
1496
 
                        s->setStatus(info);
1497
 
        }
1498
 
        // setting to offline
1499
 
        else {
1500
 
                s->disc();
1501
 
                mainwin->setUsingSSL(FALSE);
1502
 
        }
1503
 
}
1504
 
 
1505
 
/*void jabcon::conn()
1506
 
{
1507
 
        if(!s->serv()->isActive()) {
1508
 
                s->isDisconnecting = FALSE;
1509
 
                s->onEventOnlineOk = FALSE;
1510
 
 
1511
 
                s->serv()->setHost(s->acc()->host, s->acc()->port);
1512
 
                s->serv()->setAccount(s->acc()->user, s->acc()->pass, s->acc()->resource);
1513
 
                s->serv()->login(loginAs.type, loginAs.str, s->acc()->priority);
1514
 
        }
1515
 
}
1516
 
*/
1517
 
 
1518
 
/*void jabcon::askLogin()
1519
 
{
1520
 
        pdb(DEBUG_JABCON, "askLogin\n");
1521
 
 
1522
 
        if(serv->isActive()) {
1523
 
                QMessageBox::information(0, CAP(tr("Error")), tr("Please disconnect before changing the account."));
1524
 
                return;
1525
 
        }
1526
 
 
1527
 
        // cancel any pending connection
1528
 
        isDisconnecting = TRUE;
1529
 
        serv->disc();
1530
 
 
1531
 
        doNewUser = FALSE;
1532
 
 
1533
 
        LoginInfo info;
1534
 
        info.user = acc.user;
1535
 
        info.pass = acc.pass;
1536
 
        info.host = acc.host;
1537
 
        info.port = acc.port;
1538
 
        info.resource = acc.resource;
1539
 
        info.priority = acc.priority;
1540
 
        info.savepw = opt_pass;
1541
 
        info.autologin = opt_auto;
1542
 
        info.use_ssl = opt_ssl;
1543
 
        info.guest = FALSE;
1544
 
 
1545
 
        LoginDlg *login = new LoginDlg(&info, serv->isSSLSupported());
1546
 
        int n = login->exec();
1547
 
 
1548
 
        // different user?
1549
 
        if(info.user != acc.user) {
1550
 
                // nuke the offline contact list (this will clear cvlist thru signal-chain)
1551
 
                serv->reset();
1552
 
 
1553
 
                // there might be some "not in list" contacts left, so this gets them too
1554
 
                cvp->clear();
1555
 
        }
1556
 
 
1557
 
        opt_pass = info.savepw;
1558
 
        opt_auto = info.autologin;
1559
 
        opt_ssl = info.use_ssl;
1560
 
        guestMode = info.guest;
1561
 
 
1562
 
        serv->setSSLEnabled(opt_ssl);
1563
 
 
1564
 
        // Login or Create
1565
 
        if(n > 0) {
1566
 
                if(guestMode) {
1567
 
                        pdb(DEBUG_JABCON, "Using GUESTMODE\n");
1568
 
                        oldacc = acc;
1569
 
                }
1570
 
 
1571
 
                acc.user = info.user;
1572
 
                acc.pass = info.pass;
1573
 
                acc.host = info.host;
1574
 
                acc.port = info.port;
1575
 
                acc.resource = info.resource;
1576
 
                acc.priority = info.priority;
1577
 
 
1578
 
                // Login
1579
 
                if(n == 1) {
1580
 
                        statusMenuChanged(STATUS_ONLINE);
1581
 
                }
1582
 
                // Create
1583
 
                else if(n == 2) {
1584
 
                        doNewUser = TRUE;
1585
 
                        createAndConn();
1586
 
                }
1587
 
        }
1588
 
}
1589
 
 
1590
 
void jabcon::delayedAskLogin()
1591
 
{
1592
 
        QTimer *t = new QTimer(this);
1593
 
        connect(t, SIGNAL(timeout()), SLOT(askLogin()));
1594
 
        t->start(0, TRUE);
1595
 
}
1596
 
*/
1597
 
 
1598
 
void jabcon::askAddUser()
1599
 
{
1600
 
        if(s->localStatus == STATUS_OFFLINE) {
1601
 
                QMessageBox::information(0, CAP(tr("Error")), tr("You must be connected to the server in order to do this."));
1602
 
                return;
1603
 
        }
1604
 
 
1605
 
        AddUserDlg *w = AddUserDlg::find();
1606
 
        if(w)
1607
 
                bringToFront(w);
1608
 
        else {
1609
 
                QStringList groups;
1610
 
 
1611
 
                // build groups list
1612
 
                UserListItem *item;
1613
 
                for(item = s->userlist.start(USERLIST_CONTACTS); item; item = s->userlist.next()) {
1614
 
                        if(item->group.isEmpty())
1615
 
                                continue;
1616
 
 
1617
 
                        // weed out duplicates
1618
 
                        if(qstringlistmatch(groups, item->group) == -1)
1619
 
                                groups.append(item->group);
1620
 
                }
1621
 
 
1622
 
                // build services list
1623
 
                QStringList services, names;
1624
 
                for(item = s->userlist.start(USERLIST_SERVICES); item; item = s->userlist.next()) {
1625
 
                        services += item->jid;
1626
 
                        if(item->nick.isEmpty())
1627
 
                                names += QString(item->jid);
1628
 
                        else
1629
 
                                names += QString("%1 (%2)").arg(item->nick).arg(item->jid);
1630
 
                }
1631
 
 
1632
 
                AddUserDlg *w = new AddUserDlg(services, names, groups, s->localStatus);
1633
 
                connect(w, SIGNAL(signalGetGateway(const QString &, QString *)), SLOT(slotGetIQGateway(const QString &, QString *)));
1634
 
                connect(w, SIGNAL(signalSetGateway(const QString &, const QString &, QString *)), SLOT(slotSetIQGateway(const QString &, const QString &, QString *)));
1635
 
                connect(w, SIGNAL(signalCancelTransaction(const QString &)), SLOT(slotCancelTransaction(const QString &)));
1636
 
                connect(w, SIGNAL(add(const AddUserDlg::Info &)), SLOT(slotAddUser(const AddUserDlg::Info &)));
1637
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), w, SLOT(localUpdate(const JabRosterEntry &)));
1638
 
                w->show();
1639
 
        }
1640
 
}
1641
 
 
1642
 
void jabcon::slotAddUser(const AddUserDlg::Info &info)
1643
 
{
1644
 
        actionAdd(info.jid, info.nick, info.group);
1645
 
}
1646
 
 
1647
 
/*void jabcon::servError(JabError *err)
1648
 
{
1649
 
        if(err->type == JABERR_CONNECT) {
1650
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("There was an error communicating with the Jabber server.\nReason: %1")).arg(err->msg));
1651
 
                //delayedAskLogin();
1652
 
        }
1653
 
        else if(err->type == JABERR_AUTH) {
1654
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("Authorization failed.\nReason: %1")).arg(err->msg));
1655
 
                //delayedAskLogin();
1656
 
        }
1657
 
        else if(err->type == JABERR_CREATE) {
1658
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("Failed to create the new account.\nThe server gave this reason: \"%1\"")).arg(err->msg));
1659
 
                //delayedAskLogin();
1660
 
        }
1661
 
        else if(err->type == JABERR_DISCONNECT) {
1662
 
                QMessageBox::critical(0, CAP(tr("Error")), tr("You have been disconnected from the Jabber server."));
1663
 
        }
1664
 
        else {
1665
 
                QMessageBox::critical(0, CAP(tr("Error")), QString(tr("An unknown error occurred.\nType: %1\nReason: %2")).arg(err->type).arg(err->msg));
1666
 
        }
1667
 
}
1668
 
*/
1669
 
 
1670
 
void jabcon::closeProgram()
1671
 
{
1672
 
        s->disc(TRUE);
1673
 
 
1674
 
        // quit
1675
 
        quit(0);
1676
 
}
1677
 
 
1678
 
void jabcon::changeProfile()
1679
 
{
1680
 
        if(s->serv()->isActive()) {
1681
 
                QMessageBox::information(0, CAP(tr("Error")), tr("Please disconnect before changing the profile."));
1682
 
                return;
1683
 
        }
1684
 
 
1685
 
        quit(1);
1686
 
}
1687
 
 
1688
 
void jabcon::doOptions()
1689
 
{
1690
 
        OptionsDlg *w = OptionsDlg::find();
1691
 
        if(w)
1692
 
                bringToFront(w);
1693
 
        else {
1694
 
                w = new OptionsDlg(option);
1695
 
                connect(w, SIGNAL(applyOptions(const Options &)), SLOT(slotApplyOptions(const Options &)));
1696
 
                w->show();
1697
 
        }
1698
 
}
1699
 
 
1700
 
void jabcon::slotApplyOptions(const Options &opt)
1701
 
{
1702
 
        QString oldiconset = option.iconset;
1703
 
        option = opt;
1704
 
 
1705
 
        // change icon set
1706
 
        if(option.iconset != oldiconset) {
1707
 
                unloadPsiIconSet();
1708
 
                if(!loadPsiIconSet(option.iconset)) {
1709
 
                        QMessageBox::critical(0, tr("Error"), QString(tr("Unable to load the \"%1\" iconset.")).arg(option.iconset));
1710
 
                        option.iconset = oldiconset;
1711
 
                        loadPsiIconSet(option.iconset);
1712
 
                }
1713
 
        }
1714
 
 
1715
 
        mainwin->setAlwaysOnTop(option.alwaysOnTop);
1716
 
        mainwin->setUseDock(option.useDock);
1717
 
 
1718
 
        emitOptionsUpdate();
1719
 
 
1720
 
        // save just the options
1721
 
        pro.prefs = option;
1722
 
        pro.toFile(pathToProfileConfig(activeProfile));
1723
 
 
1724
 
        //QSettings settings;
1725
 
        //settings.insertSearchPath(QSettings::Windows, "/Affinix");
1726
 
        //settings.insertSearchPath(QSettings::Unix, g.pathHome);
1727
 
        //configSaveOptions(settings);
1728
 
}
1729
 
 
1730
 
void jabcon::slotApplyAccounts()
1731
 
{
1732
 
        pro.acc.first() = *s->acc();
1733
 
        pro.toFile(pathToProfileConfig(activeProfile));
1734
 
}
1735
 
 
1736
 
void jabcon::doManageServices()
1737
 
{
1738
 
        ServicesDlg *w = ServicesDlg::find();
1739
 
        if(w)
1740
 
                bringToFront(w);
1741
 
        else {
1742
 
                w = new ServicesDlg(s->localStatus, 0);
1743
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), w, SLOT(localUpdate(const JabRosterEntry &)));
1744
 
                connect(w, SIGNAL(signalRefresh(QString *)), SLOT(slotGetServices(QString *)));
1745
 
                connect(w, SIGNAL(signalSearch(const QString &, QString *)), SLOT(slotGetSearchForm(const QString &, QString *)));
1746
 
                connect(w, SIGNAL(signalGetRegForm(const QString &, QString *)), SLOT(slotGetRegForm(const QString &, QString *)));
1747
 
                connect(w, SIGNAL(signalCancelTransaction(const QString &)), SLOT(slotCancelTransaction(const QString &)));
1748
 
                w->show();
1749
 
 
1750
 
                w->doRefresh();
1751
 
        }
1752
 
}
1753
 
 
1754
 
void jabcon::doManageAccounts()
1755
 
{
1756
 
        jsm.manage();
1757
 
}
1758
 
 
1759
 
void jabcon::doFileSharing()
1760
 
{
1761
 
        OfferMainDlg *w = OfferMainDlg::find();
1762
 
        if(w)
1763
 
                bringToFront(w);
1764
 
        else {
1765
 
                w = new OfferMainDlg(0);
1766
 
                connect(this, SIGNAL(emitNewOffer(FileServerItem *)), w, SLOT(slotNewOffer(FileServerItem *)));
1767
 
 
1768
 
                // fill the listing
1769
 
                QPtrList<FileServerItem> list = fileserv->items();
1770
 
                QPtrListIterator<FileServerItem> it(list);
1771
 
                for(FileServerItem *fi; (fi = it.current()); ++it)
1772
 
                        emitNewOffer(fi);
1773
 
 
1774
 
                w->show();
1775
 
        }
1776
 
}
1777
 
 
1778
 
#include<qfiledialog.h>
1779
 
void jabcon::actionOfferFile(const QString &jid)
1780
 
{
1781
 
        QString fname = QFileDialog::getOpenFileName(QDir::homeDirPath(), "(*.*)", 0, "", "Choose a file to offer");
1782
 
        if(!fname.isEmpty())
1783
 
                offerFile(fname, "A cool video", jid);
1784
 
}
1785
 
 
1786
 
void jabcon::actionOpenURL(const QString &jid)
1787
 
{
1788
 
        openURL(jid);
1789
 
}
1790
 
 
1791
 
void jabcon::offerFile(const QString &fname, const QString &desc, const QString &jid)
1792
 
{
1793
 
        FileServerItem *i = fileserv->addFile(fname, desc, jid);
1794
 
        if(i) {
1795
 
                printf("%s\n", QString("Offering: [%1]").arg(i->url).latin1());
1796
 
                emitNewOffer(i);
1797
 
        }
1798
 
}
1799
 
 
1800
 
void jabcon::openAccountInfo()
1801
 
{
1802
 
        InfoDlg *w = InfoDlg::find(s->acc()->jid());
1803
 
        if(w)
1804
 
                bringToFront(w);
1805
 
        else {
1806
 
                /*Info *p = InfoBank::get(jid);
1807
 
                VCard tmp;
1808
 
                if(p)
1809
 
                        tmp = p->vcard;*/
1810
 
 
1811
 
                VCard tmp;
1812
 
                w = new InfoDlg(INFODLG_USER, s->acc()->jid(), tmp, s->localStatus);
1813
 
                connect(w, SIGNAL(signalGetVCard(const QString &, QString *)), SLOT(slotGetVCard(const QString &, QString *)));
1814
 
                connect(w, SIGNAL(signalSetVCard(const VCard &, QString *)), SLOT(slotSetVCard(const VCard &, QString *)));
1815
 
                connect(w, SIGNAL(signalCancelTransaction(const QString &)), SLOT(slotCancelTransaction(const QString &)));
1816
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), w, SLOT(localUpdate(const JabRosterEntry &)));
1817
 
                w->show();
1818
 
 
1819
 
                w->doRefresh();
1820
 
        }
1821
 
}
1822
 
 
1823
 
void jabcon::slotGetServices(QString *id)
1824
 
{
1825
 
        JT_GetServices *j = new JT_GetServices(s->serv()->ioUser(), "");
1826
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
1827
 
        *id = j->id();
1828
 
        j->go();
1829
 
}
1830
 
 
1831
 
void jabcon::slotJTDone(JabTask *t)
1832
 
{
1833
 
        //printf("slotJTDone\n");
1834
 
        if(t->isA("JT_GetServices")) {
1835
 
                JT_GetServices *j = (JT_GetServices *)t;
1836
 
                if(t->success()) {
1837
 
                        slotGetServicesResponse(&j->services);
1838
 
                }
1839
 
                else {
1840
 
                        ServicesDlg *w = ServicesDlg::find();
1841
 
                        if(w)
1842
 
                                w->loadFail();
1843
 
                }
1844
 
        }
1845
 
        else if(t->isA("JT_VCard")) {
1846
 
                JT_VCard *j = (JT_VCard *)t;
1847
 
                QString jid = j->jid;
1848
 
                if(j->type == 0) {
1849
 
                        if(t->success()) {
1850
 
                                slotGetVCardResponse(jid, &j->vcard);
1851
 
                        }
1852
 
                        else {
1853
 
                                slotGetVCardResponse(jid, 0);
1854
 
                        }
1855
 
                }
1856
 
                else {
1857
 
                        slotSetVCardResponse(j->success());
1858
 
                }
1859
 
        }
1860
 
        else if(t->isA("JT_RegForm")) {
1861
 
                JT_RegForm *j = (JT_RegForm *)t;
1862
 
                if(j->type == 0) {
1863
 
                        if(t->success()) {
1864
 
                                slotGetRegFormResponse(j->form);
1865
 
                        }
1866
 
                        else {
1867
 
                                ServicesDlg *w = ServicesDlg::find();
1868
 
                                if(w)
1869
 
                                        w->loadFormFail();
1870
 
                        }
1871
 
                }
1872
 
                else {
1873
 
                        slotPutRegFormResponse(j->jid, j->success(), j->errorString());
1874
 
                }
1875
 
        }
1876
 
        else if(t->isA("JT_Search")) {
1877
 
                JT_Search *j = (JT_Search *)t;
1878
 
                if(j->type == 0) {
1879
 
                        if(t->success())
1880
 
                                slotGetSearchFormResponse(j->jid, &j->form);
1881
 
                        else
1882
 
                                slotGetSearchFormResponse(j->jid, 0);
1883
 
                }
1884
 
                else {
1885
 
                        if(t->success())
1886
 
                                slotPutSearchFormResponse(j->jid, &j->roster);
1887
 
                        else
1888
 
                                slotPutSearchFormResponse(j->jid, 0);
1889
 
                }
1890
 
        }
1891
 
        else if(t->isA("JT_Gateway")) {
1892
 
                JT_Gateway *j = (JT_Gateway *)t;
1893
 
                AddUserDlg *w = AddUserDlg::find();
1894
 
                if(w) {
1895
 
                        if(j->type == 0)
1896
 
                                w->slotGetGatewayResponse(j->id(), j->success(), j->desc);
1897
 
                        else
1898
 
                                w->slotSetGatewayResponse(j->id(), j->success(), j->prompt);
1899
 
                }
1900
 
        }
1901
 
 
1902
 
        t->deleteLater();
1903
 
}
1904
 
 
1905
 
void jabcon::slotGetServicesResponse(JabRoster *services)
1906
 
{
1907
 
        ServicesDlg *w = ServicesDlg::find();
1908
 
        if(w)
1909
 
                w->loadSuccess(services);
1910
 
}
1911
 
 
1912
 
void jabcon::slotGetRegForm(const QString &service, QString *id)
1913
 
{
1914
 
        // do we already have this open?
1915
 
        RegistrationDlg *r = RegistrationDlg::find(service);
1916
 
        if(r) {
1917
 
                // loaded successfully
1918
 
                ServicesDlg *w = ServicesDlg::find();
1919
 
                if(w)
1920
 
                        w->loadFormSuccess();
1921
 
 
1922
 
                bringToFront(r);
1923
 
                return;
1924
 
        }
1925
 
 
1926
 
        JT_RegForm *j = new JT_RegForm(s->serv()->ioUser());
1927
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
1928
 
        j->get(service);
1929
 
        *id = j->id();
1930
 
        j->go();
1931
 
}
1932
 
 
1933
 
void jabcon::slotGetRegFormResponse(const JabForm &form)
1934
 
{
1935
 
        ServicesDlg *w = ServicesDlg::find();
1936
 
        if(w) {
1937
 
                w->loadFormSuccess();
1938
 
 
1939
 
                RegistrationDlg *r = new RegistrationDlg(form);
1940
 
                connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), r, SLOT(localUpdate(const JabRosterEntry &)));
1941
 
                connect(r, SIGNAL(signalSubmitForm(const JabForm &, QString *)), this, SLOT(slotPutRegForm(const JabForm &, QString *)));
1942
 
                r->show();
1943
 
        }
1944
 
}
1945
 
 
1946
 
void jabcon::slotPutRegForm(const JabForm &form, QString *id)
1947
 
{
1948
 
        pdb(DEBUG_JABCON, "submitting form\n");
1949
 
 
1950
 
        JT_RegForm *j = new JT_RegForm(s->serv()->ioUser());
1951
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
1952
 
        j->set(form);
1953
 
        *id = j->id();
1954
 
        j->go();
1955
 
}
1956
 
 
1957
 
void jabcon::slotPutRegFormResponse(const QString &service, bool ok, const QString &err)
1958
 
{
1959
 
        pdb(DEBUG_JABCON, QString("Registration result from %1: %2\n").arg(service).arg(ok ? "Success" : "Failed"));
1960
 
 
1961
 
        RegistrationDlg *r = RegistrationDlg::find(service);
1962
 
        if(r)
1963
 
                r->putRegFormResponse(ok, err);
1964
 
}
1965
 
 
1966
 
void jabcon::slotGetVCard(const QString &jid, QString *id)
1967
 
{
1968
 
        JT_VCard *j = new JT_VCard(s->serv()->ioUser());
1969
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
1970
 
        j->get(jid);
1971
 
        *id = j->id();
1972
 
        j->go();
1973
 
}
1974
 
 
1975
 
void jabcon::slotGetVCardResponse(const QString &jid, VCard *vcard)
1976
 
{
1977
 
        if(vcard) {
1978
 
                Info *p = new Info;
1979
 
                p->jid = jid;
1980
 
                p->vcard = *vcard;
1981
 
                //InfoBank::put(p);
1982
 
 
1983
 
                // rename the contact in list [pushinfo]
1984
 
                //UserListItem *i = userlist.findAnyByJid(jid);
1985
 
                //if(i && !info->field[vNickname].isEmpty())
1986
 
                //      userRename(i, info->field[vNickname]);
1987
 
        }
1988
 
 
1989
 
        InfoDlg *w = InfoDlg::find(jid);
1990
 
        if(!w)
1991
 
                return;
1992
 
 
1993
 
        if(vcard) {
1994
 
                w->updateVCard(*vcard);
1995
 
        }
1996
 
        else
1997
 
                w->error();
1998
 
}
1999
 
 
2000
 
void jabcon::slotSetVCard(const VCard &vcard, QString *id)
2001
 
{
2002
 
        JT_VCard *j = new JT_VCard(s->serv()->ioUser());
2003
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
2004
 
        VCard x = vcard;
2005
 
        j->set(x);
2006
 
        *id = j->id();
2007
 
        j->go();
2008
 
}
2009
 
 
2010
 
void jabcon::slotSetVCardResponse(bool x)
2011
 
{
2012
 
        InfoDlg *w = InfoDlg::find(s->acc()->jid());
2013
 
        if(!w)
2014
 
                return;
2015
 
 
2016
 
        w->result(x);
2017
 
}
2018
 
 
2019
 
void jabcon::slotGetSearchForm(const QString &service, QString *id)
2020
 
{
2021
 
        // do we already have this open?
2022
 
        SearchDlg *sd = SearchDlg::find(service);
2023
 
        if(sd) {
2024
 
                // loaded successfully
2025
 
                ServicesDlg *w = ServicesDlg::find();
2026
 
                if(w)
2027
 
                        w->loadFormSuccess();
2028
 
 
2029
 
                bringToFront(sd);
2030
 
                return;
2031
 
        }
2032
 
 
2033
 
        JT_Search *j = new JT_Search(s->serv()->ioUser());
2034
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
2035
 
        j->get(service);
2036
 
        *id = j->id();
2037
 
        j->go();
2038
 
}
2039
 
 
2040
 
void jabcon::slotGetSearchFormResponse(const QString &service, JabForm *p)
2041
 
{
2042
 
        ServicesDlg *w = ServicesDlg::find();
2043
 
        if(w) {
2044
 
                if(p) {
2045
 
                        w->loadFormSuccess();
2046
 
 
2047
 
                        SearchDlg *sd = new SearchDlg(service, *p, s->localStatus);
2048
 
                        connect(this, SIGNAL(emitLocalUpdate(const JabRosterEntry &)), sd, SLOT(localUpdate(const JabRosterEntry &)));
2049
 
                        connect(sd, SIGNAL(signalSubmitForm(const JabForm &, QString *)), this, SLOT(slotPutSearchForm(const JabForm &, QString *)));
2050
 
                        connect(sd, SIGNAL(signalCancelTransaction(const QString &)), SLOT(slotCancelTransaction(const QString &)));
2051
 
                        connect(sd, SIGNAL(aInfo(const QString &)), SLOT(actionInfo(const QString &)));
2052
 
                        connect(sd, SIGNAL(aAdd(const QString &, const QString &, const QString &)), SLOT(actionAdd(const QString &, const QString &, const QString &)));
2053
 
                        sd->show();
2054
 
                }
2055
 
                else
2056
 
                        w->loadFormFail();
2057
 
        }
2058
 
}
2059
 
 
2060
 
void jabcon::slotPutSearchForm(const JabForm &form, QString *id)
2061
 
{
2062
 
        pdb(DEBUG_JABCON, "submitting search form\n");
2063
 
 
2064
 
        JT_Search *j = new JT_Search(s->serv()->ioUser());
2065
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
2066
 
        j->set(form);
2067
 
        *id = j->id();
2068
 
        j->go();
2069
 
}
2070
 
 
2071
 
void jabcon::slotPutSearchFormResponse(const QString &service, JabRoster *p)
2072
 
{
2073
 
        pdb(DEBUG_JABCON, QString("Search result from %1: %2\n").arg(service).arg(p ? "Success" : "Failed"));
2074
 
 
2075
 
        SearchDlg *s = SearchDlg::find(service);
2076
 
        if(s)
2077
 
                s->putSearchFormResponse(p);
2078
 
}
2079
 
 
2080
 
 
2081
 
void jabcon::slotGetIQGateway(const QString &service, QString *id)
2082
 
{
2083
 
        JT_Gateway *j = new JT_Gateway(s->serv()->ioUser());
2084
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
2085
 
        j->get(service);
2086
 
        *id = j->id();
2087
 
        j->go();
2088
 
}
2089
 
 
2090
 
void jabcon::slotSetIQGateway(const QString &service, const QString &prompt, QString *id)
2091
 
{
2092
 
        JT_Gateway *j = new JT_Gateway(s->serv()->ioUser());
2093
 
        connect(j, SIGNAL(finished(JabTask *)), SLOT(slotJTDone(JabTask *)));
2094
 
        j->set(service, prompt);
2095
 
        *id = j->id();
2096
 
        j->go();
2097
 
}
2098
 
 
2099
 
 
2100
 
void jabcon::slotCancelTransaction(const QString &id)
2101
 
{
2102
 
        pdb(DEBUG_JABCON, QString("Cancelling transaction: [%1]\n").arg(id));
2103
 
        s->serv()->cancelTransaction(id);
2104
 
}
2105
 
 
2106
 
 
2107
 
void jabcon::slotCheckVCard(JabTask *t)
2108
 
{
2109
 
        JT_VCard *j = (JT_VCard *)t;
2110
 
        if(! (j->success() && !j->vcard.isIncomplete()) )
2111
 
                openAccountInfo();
2112
 
}
2113
 
 
2114
 
 
2115
 
void jabcon::doOnEvent(const QString &str)
2116
 
{
2117
 
        if(str.isEmpty())
2118
 
                return;
2119
 
 
2120
 
        // no sound?
2121
 
        if(!useSound)
2122
 
                return;
2123
 
 
2124
 
        // no away sounds?
2125
 
        if(option.noAwaySound && (s->localStatus == STATUS_AWAY || s->localStatus == STATUS_XA || s->localStatus == STATUS_DND))
2126
 
                return;
2127
 
 
2128
 
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
2129
 
        QSound::play(str);
2130
 
#else
2131
 
        if(!option.player.isEmpty()) {
2132
 
                QStringList args;
2133
 
                args += option.player;
2134
 
                args += str;
2135
 
                QProcess cmd(args);
2136
 
                cmd.start();
2137
 
        }
2138
 
#endif
2139
 
}
2140
 
 
2141
 
void jabcon::enableOnEventOnline()
2142
 
{
2143
 
        s->onEventOnlineOk = TRUE;
2144
 
}
2145
 
 
2146
 
void jabcon::recvNextEvent()
2147
 
{
2148
 
        EventItem *ei = events.peekNext();
2149
 
        if(!ei)
2150
 
                return;
2151
 
        UserListItem *i = s->userlist.findAnyByJid(ei->jid);
2152
 
        if(!i)
2153
 
                return;
2154
 
 
2155
 
        readMessage(i);
2156
 
}
2157
 
 
2158
 
 
2159
 
/****************************************************************************
2160
 
  EventQueue
2161
 
****************************************************************************/
2162
 
EventQueue::EventQueue()
2163
 
:QPtrList<EventItem>()
2164
 
{
2165
 
}
2166
 
 
2167
 
int EventQueue::count(const QString &jid)
2168
 
{
2169
 
        int total = 0;
2170
 
 
2171
 
        for(EventItem *i = last(); i; i = prev()) {
2172
 
                if(i->jid == jid)
2173
 
                        ++total;
2174
 
        }
2175
 
 
2176
 
        return total;
2177
 
}
2178
 
 
2179
 
void EventQueue::enqueue(EventItem *i)
2180
 
{
2181
 
        insert(0, i);
2182
 
}
2183
 
 
2184
 
EventItem *EventQueue::dequeue(const QString &jid)
2185
 
{
2186
 
        if(isEmpty())
2187
 
                return 0;
2188
 
 
2189
 
        for(EventItem *i = last(); i; i = prev()) {
2190
 
                if(i->jid == jid) {
2191
 
                        remove();
2192
 
                        return i;
2193
 
                }
2194
 
        }
2195
 
        return 0;
2196
 
}
2197
 
 
2198
 
EventItem *EventQueue::peek(const QString &jid)
2199
 
{
2200
 
        if(isEmpty())
2201
 
                return 0;
2202
 
 
2203
 
        for(EventItem *i = last(); i; i = prev()) {
2204
 
                if(i->jid == jid) {
2205
 
                        return i;
2206
 
                }
2207
 
        }
2208
 
        return 0;
2209
 
}
2210
 
 
2211
 
EventItem *EventQueue::dequeueNext()
2212
 
{
2213
 
        if(isEmpty())
2214
 
                return 0;
2215
 
 
2216
 
        EventItem *i = last();
2217
 
        remove();
2218
 
 
2219
 
        return i;
2220
 
}
2221
 
 
2222
 
EventItem *EventQueue::peekNext()
2223
 
{
2224
 
        return last();
2225
 
}
2226
 
 
2227
 
bool EventQueue::hasChats(const QString &jid)
2228
 
{
2229
 
        if(isEmpty())
2230
 
                return FALSE;
2231
 
 
2232
 
        for(EventItem *i = last(); i; i = prev()) {
2233
 
                if(i->jid == jid) {
2234
 
                        if(i->msg.type == MESSAGE_CHAT)
2235
 
                                return TRUE;
2236
 
                }
2237
 
        }
2238
 
 
2239
 
        return FALSE;
2240
 
}
2241
 
 
2242
 
// this function extracts all chats from the queue, and returns a list of queue positions
2243
 
void EventQueue::extractChats(const QString &jid, QPtrList<Message> *list, QValueList<int> *pos)
2244
 
{
2245
 
        if(isEmpty())
2246
 
                return;
2247
 
 
2248
 
        int at = 0;
2249
 
        for(EventItem *i = last(); (i = current());) {
2250
 
                if(i->jid == jid) {
2251
 
                        if(i->msg.type == MESSAGE_CHAT) {
2252
 
                                Message *m = new Message(i->msg);
2253
 
                                list->insert(0, m);
2254
 
                                pos->append(at);
2255
 
 
2256
 
                                bool isLast = (i == getLast());
2257
 
                                remove();
2258
 
                                if(isLast) {
2259
 
                                        ++at;
2260
 
                                        continue;
2261
 
                                }
2262
 
                        }
2263
 
                        ++at;
2264
 
                }
2265
 
                i = prev();
2266
 
        }
2267
 
}
2268
 
 
2269
 
// this function removes all events associated with the input jid
2270
 
void EventQueue::removeAll(const QString &jid)
2271
 
{
2272
 
        for(EventItem *i = first(); (i = current());) {
2273
 
                if(i->jid == jid)
2274
 
                        remove();
2275
 
                else
2276
 
                        i = next();
2277
 
        }
2278
 
}