~ubuntu-branches/ubuntu/dapper/psi/dapper

« back to all changes in this revision

Viewing changes to src/jabsess.cpp

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** jabsess.cpp - keeps track of Jabber accounts/sessions
 
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"jabsess.h"
 
22
#include"accountdlg.h"
 
23
#include<qinputdialog.h>
 
24
 
 
25
 
 
26
/****************************************************************************
 
27
  JabSession
 
28
****************************************************************************/
 
29
JabSession::JabSession(JabSessionManager *par)
 
30
:QObject(par)
 
31
{
 
32
        v_serv.setOfflineEnabled(TRUE);
 
33
 
 
34
        // remap the Jabber signals
 
35
        connect(&v_serv, SIGNAL(connected()), SIGNAL(connected()));
 
36
        connect(&v_serv, SIGNAL(disconnected()), SLOT(slotDisconnected()));
 
37
        connect(&v_serv, SIGNAL(statusUpdate(JabUpdate *)), SIGNAL(statusUpdate(JabUpdate *)));
 
38
        connect(&v_serv, SIGNAL(error(JabError *)), SIGNAL(error(JabError *)));
 
39
        connect(&v_serv, SIGNAL(messageReceived(const JabMessage &)), SIGNAL(messageReceived(const JabMessage &)));
 
40
        connect(&v_serv, SIGNAL(resourceAvailable(const Jid &, const JabResource &)), SIGNAL(resourceAvailable(const Jid &, const JabResource &)));
 
41
        connect(&v_serv, SIGNAL(resourceUnavailable(const Jid &)), SIGNAL(resourceUnavailable(const Jid &)));
 
42
        connect(&v_serv, SIGNAL(contactChanged(JabRosterEntry *)), SIGNAL(contactChanged(JabRosterEntry *)));
 
43
        connect(&v_serv, SIGNAL(contactNew(JabRosterEntry *)), SIGNAL(contactNew(JabRosterEntry *)));
 
44
        connect(&v_serv, SIGNAL(contactRemove(JabRosterEntry *)), SIGNAL(contactRemove(JabRosterEntry *)));
 
45
        connect(&v_serv, SIGNAL(authRequest(const Jid &)), SIGNAL(authRequest(const Jid &)));
 
46
        connect(&v_serv, SIGNAL(authGrant(const Jid &)), SIGNAL(authGrant(const Jid &)));
 
47
        connect(&v_serv, SIGNAL(authRemove(const Jid &)), SIGNAL(authRemove(const Jid &)));
 
48
 
 
49
        // init
 
50
        v_cp = 0;
 
51
 
 
52
        // setup
 
53
        usingAutoStatus = FALSE;
 
54
        isDisconnecting = FALSE;
 
55
        onEventOnlineOk = FALSE;
 
56
 
 
57
        loginAs.type = STATUS_ONLINE;
 
58
        loginAs.str = "";
 
59
        localStatus = STATUS_OFFLINE;
 
60
}
 
61
 
 
62
JabSession::~JabSession()
 
63
{
 
64
}
 
65
 
 
66
void JabSession::setAccount(const UserAccount &_acc)
 
67
{
 
68
        v_acc = _acc;
 
69
 
 
70
        serv()->setCurrentRoster(&v_acc.roster);
 
71
        serv()->setOLR(v_acc.olr_string);
 
72
        v_acc.olr_string = "";
 
73
 
 
74
        update(FALSE);
 
75
}
 
76
 
 
77
void JabSession::setContactProfile(ContactProfile *_cp)
 
78
{
 
79
        v_cp = _cp;
 
80
}
 
81
 
 
82
void JabSession::update(bool newUser)
 
83
{
 
84
        // different user?
 
85
        if(newUser) {
 
86
                // nuke the offline contact list (this will clear cvlist thru signal-chain)
 
87
                serv()->reset();
 
88
 
 
89
                // there might be some "not in list" contacts left, so this gets them too
 
90
                cp()->clear();
 
91
        }
 
92
 
 
93
        if(v_acc.opt_keepAlive)
 
94
                v_serv.setNoop(180000);  // prevent NAT timeouts every 3 minutes
 
95
        else
 
96
                v_serv.setNoop(0);
 
97
 
 
98
        v_serv.setSSLEnabled(v_acc.opt_ssl);
 
99
 
 
100
        JabSessionManager *jsm = (JabSessionManager *)parent();
 
101
        jsm->childUpdate(this);
 
102
}
 
103
 
 
104
void JabSession::conn()
 
105
{
 
106
        if(!serv()->isActive()) {
 
107
                isDisconnecting = FALSE;
 
108
                onEventOnlineOk = FALSE;
 
109
 
 
110
                serv()->setHost(acc()->host, acc()->port);
 
111
                serv()->setAccount(acc()->user, acc()->pass, acc()->resource);
 
112
                serv()->login(loginAs.type, loginAs.str, acc()->priority, acc()->opt_plain);
 
113
        }
 
114
}
 
115
 
 
116
void JabSession::disc(bool fast)
 
117
{
 
118
        // disconnect
 
119
        if(serv()->isActive()) {
 
120
                isDisconnecting = TRUE;
 
121
                serv()->disc(fast);
 
122
                //mainwin->setUsingSSL(FALSE);
 
123
        }
 
124
}
 
125
 
 
126
void JabSession::setStatus(const StatusInfo &info)
 
127
{
 
128
        usingAutoStatus = FALSE;
 
129
 
 
130
        loginAs.type = info.type;
 
131
        loginAs.str = info.str;
 
132
 
 
133
        // if the Jabber (serv) class is not busy, then attempt to login
 
134
        // if it is busy then change the status
 
135
        if(!serv()->isActive()) {
 
136
                if(acc()->user.isEmpty() || acc()->host.isEmpty() || acc()->resource.isEmpty()) {
 
137
                        QMessageBox::information(0, CAP(tr("Error")), tr("Unable to login.  Ensure your account information is filled out."));
 
138
                        JabSessionManager *jsm = (JabSessionManager *)parent();
 
139
                        jsm->modify(this);
 
140
                        return;
 
141
                }
 
142
                if(!acc()->opt_pass) {
 
143
                        bool ok = FALSE;
 
144
                        QString text = QInputDialog::getText(
 
145
                                tr("Need Password"),
 
146
                                QString(tr("Please enter the password for <b>%1</b>")).arg(acc()->jid()),
 
147
                                QLineEdit::Password, QString::null, &ok, 0);
 
148
                        if(ok && !text.isEmpty())
 
149
                                acc()->pass = text;
 
150
                        else
 
151
                                return;
 
152
                }
 
153
 
 
154
                conn();
 
155
        }
 
156
        // change status
 
157
        else {
 
158
                serv()->setPresence(info.type, info.str, acc()->priority);
 
159
        }
 
160
}
 
161
 
 
162
void JabSession::slotDisconnected()
 
163
{
 
164
        isDisconnecting = TRUE;
 
165
 
 
166
        disconnected();
 
167
}
 
168
 
 
169
void JabSession::secondsIdle(int x)
 
170
{
 
171
        static int lastIdle = 0;
 
172
        static int lastStatus = STATUS_OFFLINE;
 
173
 
 
174
        if(serv()->isActive()) {
 
175
                // no longer idle?
 
176
                if(lastIdle > x) {
 
177
                        if(localStatus != STATUS_ONLINE && usingAutoStatus) {
 
178
                                serv()->setPresence(STATUS_ONLINE, "", acc()->priority);
 
179
                                lastStatus = STATUS_ONLINE;
 
180
                                usingAutoStatus = FALSE;
 
181
                        }
 
182
                }
 
183
                else if( !(localStatus != STATUS_ONLINE && !usingAutoStatus) ) {
 
184
                        int minutes = x / 60;
 
185
 
 
186
                        if(option.asOffline > 0 && minutes >= option.asOffline) {
 
187
                                isDisconnecting = TRUE;
 
188
                                lastStatus = STATUS_OFFLINE;
 
189
                                usingAutoStatus = FALSE;
 
190
                                disc();
 
191
                        }
 
192
                        else if(option.asXa > 0 && minutes >= option.asXa) {
 
193
                                if(localStatus != STATUS_XA && lastStatus != STATUS_XA) {
 
194
                                        QString str = tr("Auto Status (idle)");
 
195
                                        serv()->setPresence(STATUS_XA, str, acc()->priority);
 
196
                                        lastStatus = STATUS_XA;
 
197
                                        usingAutoStatus = TRUE;
 
198
                                }
 
199
                        }
 
200
                        else if(option.asAway > 0 && minutes >= option.asAway) {
 
201
                                if(localStatus != STATUS_AWAY && lastStatus != STATUS_AWAY) {
 
202
                                        QString str = tr("Auto Status (idle)");
 
203
                                        serv()->setPresence(STATUS_AWAY, str, acc()->priority);
 
204
                                        lastStatus = STATUS_AWAY;
 
205
                                        usingAutoStatus = TRUE;
 
206
                                }
 
207
                        }
 
208
                }
 
209
        }
 
210
 
 
211
        lastIdle = x;
 
212
}
 
213
 
 
214
 
 
215
/****************************************************************************
 
216
  JabSessionManager
 
217
****************************************************************************/
 
218
JabSessionManager::JabSessionManager(QObject *par)
 
219
:QObject(par)
 
220
{
 
221
        list.setAutoDelete(TRUE);
 
222
}
 
223
 
 
224
JabSessionManager::~JabSessionManager()
 
225
{
 
226
}
 
227
 
 
228
void JabSessionManager::add(JabSession *s)
 
229
{
 
230
        list.append(s);
 
231
 
 
232
        connect(s, SIGNAL(connected()), SLOT(slotConnected()));
 
233
        connect(s, SIGNAL(disconnected()), SLOT(slotDisconnected()));
 
234
        connect(s, SIGNAL(statusUpdate(JabUpdate *)), SLOT(slotStatusUpdate(JabUpdate *)));
 
235
        connect(s, SIGNAL(error(JabError *)), SLOT(slotError(JabError *)));
 
236
        connect(s, SIGNAL(messageReceived(const JabMessage &)), SLOT(slotMessageReceived(const JabMessage &)));
 
237
        connect(s, SIGNAL(resourceAvailable(const Jid &, const JabResource &)), SLOT(slotResourceAvailable(const Jid &, const JabResource &)));
 
238
        connect(s, SIGNAL(resourceUnavailable(const Jid &)), SLOT(slotResourceUnavailable(const Jid &)));
 
239
        connect(s, SIGNAL(contactChanged(JabRosterEntry *)), SLOT(slotContactChanged(JabRosterEntry *)));
 
240
        connect(s, SIGNAL(contactNew(JabRosterEntry *)), SLOT(slotContactNew(JabRosterEntry *)));
 
241
        connect(s, SIGNAL(contactRemove(JabRosterEntry *)), SLOT(slotContactRemove(JabRosterEntry *)));
 
242
        connect(s, SIGNAL(authRequest(const Jid &)), SLOT(slotAuthRequest(const Jid &)));
 
243
        connect(s, SIGNAL(authGrant(const Jid &)), SLOT(slotAuthGrant(const Jid &)));
 
244
        connect(s, SIGNAL(authRemove(const Jid &)), SLOT(slotAuthRemove(const Jid &)));
 
245
}
 
246
 
 
247
void JabSessionManager::clear()
 
248
{
 
249
        list.clear();
 
250
}
 
251
 
 
252
void JabSessionManager::slotConnected()
 
253
{
 
254
        JabSession *s = (JabSession *)sender();
 
255
        jab_connected(s);
 
256
}
 
257
 
 
258
void JabSessionManager::slotDisconnected()
 
259
{
 
260
        JabSession *s = (JabSession *)sender();
 
261
        jab_disconnected(s);
 
262
}
 
263
 
 
264
void JabSessionManager::slotStatusUpdate(JabUpdate *x)
 
265
{
 
266
        JabSession *s = (JabSession *)sender();
 
267
        jab_statusUpdate(s, x);
 
268
 
 
269
        sessionUpdate(s);
 
270
}
 
271
 
 
272
void JabSessionManager::slotError(JabError *x)
 
273
{
 
274
        JabSession *s = (JabSession *)sender();
 
275
        jab_error(s, x);
 
276
}
 
277
 
 
278
void JabSessionManager::slotMessageReceived(const JabMessage &msg)
 
279
{
 
280
        JabSession *s = (JabSession *)sender();
 
281
        jab_messageReceived(s, msg);
 
282
}
 
283
 
 
284
void JabSessionManager::slotResourceAvailable(const Jid &j, const JabResource &r)
 
285
{
 
286
        JabSession *s = (JabSession *)sender();
 
287
        jab_resourceAvailable(s, j, r);
 
288
}
 
289
 
 
290
void JabSessionManager::slotResourceUnavailable(const Jid &j)
 
291
{
 
292
        JabSession *s = (JabSession *)sender();
 
293
        jab_resourceUnavailable(s, j);
 
294
}
 
295
 
 
296
void JabSessionManager::slotContactChanged(JabRosterEntry *e)
 
297
{
 
298
        JabSession *s = (JabSession *)sender();
 
299
        jab_contactChanged(s, e);
 
300
}
 
301
 
 
302
void JabSessionManager::slotContactNew(JabRosterEntry *e)
 
303
{
 
304
        JabSession *s = (JabSession *)sender();
 
305
        jab_contactNew(s, e);
 
306
}
 
307
 
 
308
void JabSessionManager::slotContactRemove(JabRosterEntry *e)
 
309
{
 
310
        JabSession *s = (JabSession *)sender();
 
311
        jab_contactRemove(s, e);
 
312
}
 
313
 
 
314
void JabSessionManager::slotAuthRequest(const Jid &j)
 
315
{
 
316
        JabSession *s = (JabSession *)sender();
 
317
        jab_authRequest(s, j);
 
318
}
 
319
 
 
320
void JabSessionManager::slotAuthGrant(const Jid &j)
 
321
{
 
322
        JabSession *s = (JabSession *)sender();
 
323
        jab_authGrant(s, j);
 
324
}
 
325
 
 
326
void JabSessionManager::slotAuthRemove(const Jid &j)
 
327
{
 
328
        JabSession *s = (JabSession *)sender();
 
329
        jab_authRemove(s, j);
 
330
}
 
331
 
 
332
JabSession *JabSessionManager::find(const QString &name)
 
333
{
 
334
        QString val = name.lower();
 
335
        QPtrListIterator<JabSession> it(list);
 
336
        for(JabSession *s; (s = it.current()); ++it) {
 
337
                if(s->acc()->name.lower() == val)
 
338
                        return s;
 
339
        }
 
340
        return 0;
 
341
}
 
342
 
 
343
void JabSessionManager::manage()
 
344
{
 
345
        AccountManageDlg *w = AccountManageDlg::find();
 
346
        if(w)
 
347
                bringToFront(w);
 
348
        else {
 
349
                w = new AccountManageDlg(this);
 
350
                connect(w, SIGNAL(signalModify(const QString &)), SLOT(modify(const QString &)));
 
351
                w->show();
 
352
        }
 
353
}
 
354
 
 
355
void JabSessionManager::modify(const QString &accname)
 
356
{
 
357
        JabSession *s = find(accname);
 
358
        if(!s)
 
359
                return;
 
360
        modify(s);
 
361
}
 
362
 
 
363
void JabSessionManager::modify(JabSession *s)
 
364
{
 
365
        AccountModifyDlg *w = AccountModifyDlg::find(s->name());
 
366
        if(w)
 
367
                bringToFront(w);
 
368
        else {
 
369
                w = new AccountModifyDlg(s, s->serv()->isSSLSupported(), 0);
 
370
                w->show();
 
371
        }
 
372
}
 
373
 
 
374
void JabSessionManager::childUpdate(JabSession *s)
 
375
{
 
376
        // jabcon should respond to this and save settings
 
377
        accountSettingsChanged();
 
378
 
 
379
        // notify the world that this session was updated
 
380
        sessionUpdate(s);
 
381
}