1
/****************************************************************************
2
** jabsess.cpp - keeps track of Jabber accounts/sessions
3
** Copyright (C) 2001, 2002 Justin Karneges
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.
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.
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.
19
****************************************************************************/
22
#include"accountdlg.h"
23
#include<qinputdialog.h>
26
/****************************************************************************
28
****************************************************************************/
29
JabSession::JabSession(JabSessionManager *par)
32
v_serv.setOfflineEnabled(TRUE);
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 &)));
53
usingAutoStatus = FALSE;
54
isDisconnecting = FALSE;
55
onEventOnlineOk = FALSE;
57
loginAs.type = STATUS_ONLINE;
59
localStatus = STATUS_OFFLINE;
62
JabSession::~JabSession()
66
void JabSession::setAccount(const UserAccount &_acc)
70
serv()->setCurrentRoster(&v_acc.roster);
71
serv()->setOLR(v_acc.olr_string);
72
v_acc.olr_string = "";
77
void JabSession::setContactProfile(ContactProfile *_cp)
82
void JabSession::update(bool newUser)
86
// nuke the offline contact list (this will clear cvlist thru signal-chain)
89
// there might be some "not in list" contacts left, so this gets them too
93
if(v_acc.opt_keepAlive)
94
v_serv.setNoop(180000); // prevent NAT timeouts every 3 minutes
98
v_serv.setSSLEnabled(v_acc.opt_ssl);
100
JabSessionManager *jsm = (JabSessionManager *)parent();
101
jsm->childUpdate(this);
104
void JabSession::conn()
106
if(!serv()->isActive()) {
107
isDisconnecting = FALSE;
108
onEventOnlineOk = FALSE;
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);
116
void JabSession::disc(bool fast)
119
if(serv()->isActive()) {
120
isDisconnecting = TRUE;
122
//mainwin->setUsingSSL(FALSE);
126
void JabSession::setStatus(const StatusInfo &info)
128
usingAutoStatus = FALSE;
130
loginAs.type = info.type;
131
loginAs.str = info.str;
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();
142
if(!acc()->opt_pass) {
144
QString text = QInputDialog::getText(
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())
158
serv()->setPresence(info.type, info.str, acc()->priority);
162
void JabSession::slotDisconnected()
164
isDisconnecting = TRUE;
169
void JabSession::secondsIdle(int x)
171
static int lastIdle = 0;
172
static int lastStatus = STATUS_OFFLINE;
174
if(serv()->isActive()) {
177
if(localStatus != STATUS_ONLINE && usingAutoStatus) {
178
serv()->setPresence(STATUS_ONLINE, "", acc()->priority);
179
lastStatus = STATUS_ONLINE;
180
usingAutoStatus = FALSE;
183
else if( !(localStatus != STATUS_ONLINE && !usingAutoStatus) ) {
184
int minutes = x / 60;
186
if(option.asOffline > 0 && minutes >= option.asOffline) {
187
isDisconnecting = TRUE;
188
lastStatus = STATUS_OFFLINE;
189
usingAutoStatus = FALSE;
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;
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;
215
/****************************************************************************
217
****************************************************************************/
218
JabSessionManager::JabSessionManager(QObject *par)
221
list.setAutoDelete(TRUE);
224
JabSessionManager::~JabSessionManager()
228
void JabSessionManager::add(JabSession *s)
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 &)));
247
void JabSessionManager::clear()
252
void JabSessionManager::slotConnected()
254
JabSession *s = (JabSession *)sender();
258
void JabSessionManager::slotDisconnected()
260
JabSession *s = (JabSession *)sender();
264
void JabSessionManager::slotStatusUpdate(JabUpdate *x)
266
JabSession *s = (JabSession *)sender();
267
jab_statusUpdate(s, x);
272
void JabSessionManager::slotError(JabError *x)
274
JabSession *s = (JabSession *)sender();
278
void JabSessionManager::slotMessageReceived(const JabMessage &msg)
280
JabSession *s = (JabSession *)sender();
281
jab_messageReceived(s, msg);
284
void JabSessionManager::slotResourceAvailable(const Jid &j, const JabResource &r)
286
JabSession *s = (JabSession *)sender();
287
jab_resourceAvailable(s, j, r);
290
void JabSessionManager::slotResourceUnavailable(const Jid &j)
292
JabSession *s = (JabSession *)sender();
293
jab_resourceUnavailable(s, j);
296
void JabSessionManager::slotContactChanged(JabRosterEntry *e)
298
JabSession *s = (JabSession *)sender();
299
jab_contactChanged(s, e);
302
void JabSessionManager::slotContactNew(JabRosterEntry *e)
304
JabSession *s = (JabSession *)sender();
305
jab_contactNew(s, e);
308
void JabSessionManager::slotContactRemove(JabRosterEntry *e)
310
JabSession *s = (JabSession *)sender();
311
jab_contactRemove(s, e);
314
void JabSessionManager::slotAuthRequest(const Jid &j)
316
JabSession *s = (JabSession *)sender();
317
jab_authRequest(s, j);
320
void JabSessionManager::slotAuthGrant(const Jid &j)
322
JabSession *s = (JabSession *)sender();
326
void JabSessionManager::slotAuthRemove(const Jid &j)
328
JabSession *s = (JabSession *)sender();
329
jab_authRemove(s, j);
332
JabSession *JabSessionManager::find(const QString &name)
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)
343
void JabSessionManager::manage()
345
AccountManageDlg *w = AccountManageDlg::find();
349
w = new AccountManageDlg(this);
350
connect(w, SIGNAL(signalModify(const QString &)), SLOT(modify(const QString &)));
355
void JabSessionManager::modify(const QString &accname)
357
JabSession *s = find(accname);
363
void JabSessionManager::modify(JabSession *s)
365
AccountModifyDlg *w = AccountModifyDlg::find(s->name());
369
w = new AccountModifyDlg(s, s->serv()->isSSLSupported(), 0);
374
void JabSessionManager::childUpdate(JabSession *s)
376
// jabcon should respond to this and save settings
377
accountSettingsChanged();
379
// notify the world that this session was updated