~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

Viewing changes to src/accountmanagedlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * accountmanagedlg.cpp - dialogs for manipulating PsiAccounts
 
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 library; 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 <QtCrypto>
 
22
#include <QInputDialog>
 
23
#include <QMessageBox>
 
24
#include <QPushButton>
 
25
#include <QLineEdit>
 
26
#include <QLabel>
 
27
#include <q3listview.h>
 
28
#include <q3buttongroup.h>
 
29
#include "psicon.h"
 
30
#include "psiaccount.h"
 
31
#include "common.h"
 
32
#include "xmpp_tasks.h"
 
33
#include "pgputil.h"
 
34
#include "proxy.h"
 
35
#include "miniclient.h"
 
36
#include "accountadddlg.h"
 
37
#include "accountmanagedlg.h"
 
38
#include "ui_accountremove.h"
 
39
#include "psicontactlist.h"
 
40
 
 
41
using namespace XMPP;
 
42
 
 
43
 
 
44
//----------------------------------------------------------------------------
 
45
// AccountRemoveDlg
 
46
//----------------------------------------------------------------------------
 
47
 
 
48
class AccountRemoveDlg : public QDialog, public Ui::AccountRemove
 
49
{
 
50
        Q_OBJECT
 
51
public:
 
52
        AccountRemoveDlg(ProxyManager *, const UserAccount &, QWidget *parent=0);
 
53
        ~AccountRemoveDlg();
 
54
 
 
55
protected:
 
56
        // reimplemented
 
57
        //void closeEvent(QCloseEvent *);
 
58
 
 
59
public slots:
 
60
        void done(int);
 
61
 
 
62
private slots:
 
63
        void remove();
 
64
        void bg_clicked(int);
 
65
 
 
66
        void client_handshaken();
 
67
        void client_error();
 
68
        void client_disconnected();
 
69
        void unreg_finished();
 
70
 
 
71
private:
 
72
        class Private;
 
73
        Private *d;
 
74
 
 
75
        MiniClient *client;
 
76
};
 
77
 
 
78
class AccountRemoveDlg::Private
 
79
{
 
80
public:
 
81
        Private() {}
 
82
 
 
83
        UserAccount acc;
 
84
        QButtonGroup *bg;
 
85
        ProxyManager *proxyman;
 
86
};
 
87
 
 
88
AccountRemoveDlg::AccountRemoveDlg(ProxyManager *proxyman, const UserAccount &acc, QWidget *parent)
 
89
:QDialog(parent)
 
90
{
 
91
        setupUi(this);
 
92
        setModal(false);
 
93
        d = new Private;
 
94
        d->acc = acc;
 
95
        d->proxyman = proxyman;
 
96
 
 
97
        setWindowTitle(CAP(caption()));
 
98
 
 
99
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
 
100
        connect(pb_remove, SIGNAL(clicked()), SLOT(remove()));
 
101
 
 
102
        d->bg = new QButtonGroup(0);
 
103
        d->bg->addButton(rb_remove, 0);
 
104
        d->bg->addButton(rb_removeAndUnreg, 1);
 
105
        connect(d->bg, SIGNAL(buttonClicked(int)), SLOT(bg_clicked(int)));
 
106
        rb_remove->setChecked(true);
 
107
        bg_clicked(0);
 
108
 
 
109
        pb_close->setFocus();
 
110
 
 
111
        client = new MiniClient;
 
112
        connect(client, SIGNAL(handshaken()), SLOT(client_handshaken()));
 
113
        connect(client, SIGNAL(error()), SLOT(client_error()));
 
114
        connect(client, SIGNAL(disconnected()), SLOT(client_disconnected()));
 
115
}
 
116
 
 
117
AccountRemoveDlg::~AccountRemoveDlg()
 
118
{
 
119
        delete client;
 
120
 
 
121
        delete d->bg;
 
122
        delete d;
 
123
}
 
124
 
 
125
/*void AccountRemoveDlg::closeEvent(QCloseEvent *e)
 
126
{
 
127
        e->ignore();
 
128
        reject();
 
129
}*/
 
130
 
 
131
void AccountRemoveDlg::done(int r)
 
132
{
 
133
        if(busy->isActive()) {
 
134
                int n = QMessageBox::information(this, tr("Warning"), tr("Are you sure you want to cancel the unregistration?"), tr("&Yes"), tr("&No"));
 
135
                if(n != 0)
 
136
                        return;
 
137
        }
 
138
        QDialog::done(r);
 
139
}
 
140
 
 
141
void AccountRemoveDlg::bg_clicked(int x)
 
142
{
 
143
        if(x == 0) {
 
144
                lb_pass->setEnabled(false);
 
145
                le_pass->setEnabled(false);
 
146
        }
 
147
        else if(x == 1) {
 
148
                lb_pass->setEnabled(true);
 
149
                le_pass->setEnabled(true);
 
150
                le_pass->setFocus();
 
151
        }
 
152
}
 
153
 
 
154
void AccountRemoveDlg::remove()
 
155
{
 
156
        bool unreg = rb_removeAndUnreg->isChecked();
 
157
 
 
158
        if(unreg) {
 
159
                if(!d->acc.pass.isEmpty() && le_pass->text() != d->acc.pass) {
 
160
                        QMessageBox::information(this, tr("Error"), tr("Password does not match account.  Please try again."));
 
161
                        le_pass->setFocus();
 
162
                        return;
 
163
                }
 
164
        }
 
165
 
 
166
        int n = QMessageBox::information(this, tr("Warning"), tr("Are you sure you want to remove <b>%1</b> ?").arg(d->acc.name), tr("&Yes"), tr("&No"));
 
167
        if(n != 0)
 
168
                return;
 
169
 
 
170
        if(!unreg) {
 
171
                accept();
 
172
                return;
 
173
        }
 
174
 
 
175
        busy->start();
 
176
        gb_account->setEnabled(false);
 
177
        pb_remove->setEnabled(false);
 
178
 
 
179
        Jid j = d->acc.jid;
 
180
        QString pass = le_pass->text();
 
181
        j.setResource(d->acc.resource);
 
182
        client->connectToServer(j, d->acc.legacy_ssl_probe, d->acc.ssl == UserAccount::SSL_Legacy, d->acc.ssl == UserAccount::SSL_Yes, d->acc.opt_host ? d->acc.host : QString(), d->acc.port, d->proxyman, d->acc.proxy_index, &pass);
 
183
}
 
184
 
 
185
void AccountRemoveDlg::client_handshaken()
 
186
{
 
187
        // Workaround for servers that do not send a response to the remove request
 
188
        client->setErrorOnDisconnect(false);
 
189
 
 
190
        // try to unregister an account
 
191
        JT_Register *reg = new JT_Register(client->client()->rootTask());
 
192
        connect(reg, SIGNAL(finished()), SLOT(unreg_finished()));
 
193
        reg->unreg();
 
194
        reg->go(true);
 
195
}
 
196
 
 
197
void AccountRemoveDlg::client_error()
 
198
{
 
199
        busy->stop();
 
200
        gb_account->setEnabled(true);
 
201
        pb_remove->setEnabled(true);
 
202
}
 
203
 
 
204
void AccountRemoveDlg::unreg_finished()
 
205
{
 
206
        JT_Register *reg = (JT_Register *)sender();
 
207
 
 
208
        client->close();
 
209
        busy->stop();
 
210
 
 
211
        if(reg->success()) {
 
212
                QMessageBox::information(this, tr("Success"), tr("The account was unregistered successfully."));
 
213
                accept();
 
214
                return;
 
215
        }
 
216
        else if(reg->statusCode() != Task::ErrDisc) {
 
217
                gb_account->setEnabled(true);
 
218
                pb_remove->setEnabled(true);
 
219
                QMessageBox::critical(this, tr("Error"), QString(tr("There was an error unregistering the account.\nReason: %1")).arg(reg->statusString()));
 
220
        }
 
221
}
 
222
 
 
223
void AccountRemoveDlg::client_disconnected()
 
224
{
 
225
        // Workaround for servers that do not send a response to the remove request
 
226
        busy->stop();
 
227
        QMessageBox::information(this, tr("Success"), tr("The account was unregistered successfully."));
 
228
        accept();
 
229
}
 
230
 
 
231
//----------------------------------------------------------------------------
 
232
// AccountManageDlg
 
233
//----------------------------------------------------------------------------
 
234
class AccountManageItem : public Q3CheckListItem
 
235
{
 
236
public:
 
237
        AccountManageItem(Q3ListView *par, PsiAccount *_pa)
 
238
        :Q3CheckListItem(par,"",CheckBox)
 
239
        {
 
240
                pa = _pa;
 
241
                updateInfo();
 
242
        }
 
243
 
 
244
        void updateInfo()
 
245
        {
 
246
                const UserAccount &acc = pa->userAccount();
 
247
                setText(0, pa->name());
 
248
                //setPixmap(0, IconsetFactory::icon("psi/account"));
 
249
                Jid j = acc.jid;
 
250
                setText(1, acc.opt_host && acc.host.length() ? acc.host : j.host());
 
251
                setText(2, pa->isActive() ? AccountManageDlg::tr("Active") : AccountManageDlg::tr("Not active"));
 
252
                setOn(pa->enabled());
 
253
        }
 
254
 
 
255
        void stateChange( bool s)
 
256
        {
 
257
                if (pa->enabled()!=s)
 
258
                        pa->setEnabled(s);
 
259
                updateInfo();
 
260
        }
 
261
 
 
262
        int rtti() const
 
263
        {
 
264
                return 8109;
 
265
        }
 
266
 
 
267
        PsiAccount *pa;
 
268
};
 
269
 
 
270
AccountManageDlg::AccountManageDlg(PsiCon *_psi)
 
271
:QDialog(0)
 
272
{
 
273
        setupUi(this);
 
274
        setModal(false);
 
275
        psi = _psi;
 
276
        psi->dialogRegister(this);
 
277
 
 
278
        setWindowTitle(CAP(caption()));
 
279
 
 
280
        // setup signals
 
281
        connect(pb_add, SIGNAL(clicked()), SLOT(add()));
 
282
        connect(pb_modify, SIGNAL(clicked()), SLOT(modify()));
 
283
        connect(pb_remove, SIGNAL(clicked()), SLOT(remove()));
 
284
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
 
285
 
 
286
        connect(lv_accs, SIGNAL(doubleClicked(Q3ListViewItem *)), SLOT(modify(Q3ListViewItem *)));
 
287
        connect(lv_accs, SIGNAL(selectionChanged(Q3ListViewItem *)), SLOT(qlv_selectionChanged(Q3ListViewItem *)));
 
288
        connect(psi, SIGNAL(accountAdded(PsiAccount *)), SLOT(accountAdded(PsiAccount *)));
 
289
        connect(psi, SIGNAL(accountUpdated(PsiAccount *)), SLOT(accountUpdated(PsiAccount *)));
 
290
        connect(psi, SIGNAL(accountRemoved(PsiAccount *)), SLOT(accountRemoved(PsiAccount *)));
 
291
 
 
292
        lv_accs->setAllColumnsShowFocus(true);
 
293
        lv_accs->setResizeMode(Q3ListView::LastColumn);
 
294
 
 
295
        foreach(PsiAccount* pa, psi->contactList()->accounts())
 
296
                new AccountManageItem(lv_accs, pa);
 
297
 
 
298
        if(lv_accs->childCount() > 0)
 
299
                lv_accs->setSelected(lv_accs->firstChild(), true);
 
300
        else
 
301
                qlv_selectionChanged(0);
 
302
}
 
303
 
 
304
AccountManageDlg::~AccountManageDlg()
 
305
{
 
306
        psi->dialogUnregister(this);
 
307
}
 
308
 
 
309
void AccountManageDlg::qlv_selectionChanged(Q3ListViewItem *lvi)
 
310
{
 
311
        AccountManageItem *i = (AccountManageItem *)lvi;
 
312
        bool ok = i ? true: false;
 
313
 
 
314
        pb_modify->setEnabled(ok);
 
315
        pb_remove->setEnabled(ok);
 
316
}
 
317
 
 
318
void AccountManageDlg::add()
 
319
{
 
320
        AccountAddDlg *w = new AccountAddDlg(psi, 0);
 
321
        w->show();
 
322
}
 
323
 
 
324
void AccountManageDlg::modify()
 
325
{
 
326
        modify(lv_accs->currentItem());
 
327
}
 
328
 
 
329
void AccountManageDlg::modify(Q3ListViewItem *lvi)
 
330
{
 
331
        AccountManageItem *i = (AccountManageItem *)lvi;
 
332
        if(!i)
 
333
                return;
 
334
 
 
335
        i->pa->modify();
 
336
}
 
337
 
 
338
void AccountManageDlg::remove()
 
339
{
 
340
        AccountManageItem *i = (AccountManageItem *)lv_accs->currentItem();
 
341
        if(!i)
 
342
                return;
 
343
 
 
344
        if(i->pa->isActive()) {
 
345
                QMessageBox::information(this, tr("Error"), tr("Unable to remove the account, as it is currently active."));
 
346
                return;
 
347
        }
 
348
 
 
349
        AccountRemoveDlg *w = new AccountRemoveDlg(psi->proxy(), i->pa->userAccount());
 
350
        int n = w->exec();
 
351
        if(n != QDialog::Accepted) {
 
352
                delete w;
 
353
                return;
 
354
        }
 
355
        delete w;
 
356
        psi->removeAccount(i->pa);
 
357
}
 
358
 
 
359
void AccountManageDlg::accountAdded(PsiAccount *pa)
 
360
{
 
361
        new AccountManageItem(lv_accs, pa);
 
362
}
 
363
 
 
364
void AccountManageDlg::accountUpdated(PsiAccount *pa)
 
365
{
 
366
        AccountManageItem *i;
 
367
        Q3ListViewItemIterator it(lv_accs);
 
368
        for(; (i = (AccountManageItem *)it.current()) ; ++it) {
 
369
                if(i->pa == pa)
 
370
                        break;
 
371
        }
 
372
        if(!i)
 
373
                return;
 
374
 
 
375
        i->updateInfo();
 
376
}
 
377
 
 
378
void AccountManageDlg::accountRemoved(PsiAccount *pa)
 
379
{
 
380
        AccountManageItem *i;
 
381
        Q3ListViewItemIterator it(lv_accs);
 
382
        for(; (i = (AccountManageItem *)it.current()) ; ++it) {
 
383
                if(i->pa == pa)
 
384
                        break;
 
385
        }
 
386
        if(!i)
 
387
                return;
 
388
 
 
389
        delete i;
 
390
 
 
391
        qlv_selectionChanged(lv_accs->currentItem());
 
392
}
 
393
 
 
394
#include "accountmanagedlg.moc"
 
395