~ubuntu-branches/ubuntu/edgy/psi/edgy

« back to all changes in this revision

Viewing changes to src/servicesdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
** servicesdlg.cpp - a dialog for browsing Jabber services/agents/transports
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
 
 
 
1
/*
 
2
 * servicesdlg.cpp - a dialog for browsing Jabber services
 
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"servicesdlg.h"
 
22
 
 
23
#include<qpushbutton.h>
 
24
#include<qcombobox.h>
 
25
#include<qstringlist.h>
 
26
#include<qlistbox.h>
21
27
#include<qlayout.h>
22
 
#include<qpushbutton.h>
23
 
#include<qframe.h>
24
 
 
25
 
#include"servicesdlg.h"
 
28
#include<qlineedit.h>
 
29
#include<qlabel.h>
 
30
#include<qgrid.h>
 
31
#include<qguardedptr.h>
 
32
#include<qlistview.h>
 
33
#include<qgroupbox.h>
 
34
#include<qapplication.h>
 
35
#include<qdesktopwidget.h>
 
36
#include"psicon.h"
 
37
#include"psiaccount.h"
 
38
#include"im.h"
 
39
#include"xmpp_tasks.h"
26
40
#include"common.h"
27
 
 
28
 
 
29
 
static ServicesDlg *services_ptr = 0;
30
 
 
31
 
ServicesDlg::ServicesDlg(int _localStatus, QWidget *parent, const char *name)
32
 
:ServicesUI(parent, name, FALSE, WDestructiveClose)
33
 
{
 
41
#include"busywidget.h"
 
42
#include"iconwidget.h"
 
43
 
 
44
//----------------------------------------------------------------------------
 
45
// ServicesDlg
 
46
//----------------------------------------------------------------------------
 
47
class ServicesDlg::Private
 
48
{
 
49
public:
 
50
        Private() {}
 
51
 
 
52
        Jid jid;
 
53
        PsiAccount *pa;
 
54
        BusyWidget *busy;
 
55
        JT_GetServices *jt_gs;
 
56
        AgentList agents;
 
57
};
 
58
 
 
59
ServicesDlg::ServicesDlg(const Jid &j, PsiAccount *pa)
 
60
:ServicesUI(0, 0, false, WDestructiveClose)
 
61
{
 
62
        d = new Private;
 
63
        d->pa = pa;
 
64
        d->jid = j;
 
65
        d->pa->dialogRegister(this);
 
66
        d->jt_gs = 0;
 
67
 
34
68
        setCaption(CAP(tr("Manage Services")));
35
 
        setIcon(transport2icon("", STATUS_ONLINE));
36
 
 
37
 
        services_ptr = this;
38
 
 
39
 
        isBusy = FALSE;
40
 
        localStatus = _localStatus;
41
 
 
42
 
        busy = new BusyWidget(this);
43
 
        vb_side->addWidget(busy);
44
 
 
45
 
        pb_register->setEnabled(FALSE);
46
 
        pb_search->setEnabled(FALSE);
47
 
        pb_refresh->setEnabled(FALSE);
48
 
 
49
 
        connect(pb_refresh, SIGNAL(clicked()), SLOT(doRefresh()));
 
69
        setIcon(is->transportStatus("transport", STATUS_ONLINE));
 
70
 
 
71
        d->busy = new BusyWidget(this);
 
72
        replaceWidget(lb_busywidget, d->busy);
 
73
 
 
74
        cb_browse->setInsertionPolicy(QComboBox::NoInsertion);
 
75
        cb_browse->insertStringList(d->pa->psi()->recentBrowseList());
 
76
 
50
77
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
 
78
        connect(pb_browse, SIGNAL(clicked()), SLOT(doBrowse()));
 
79
        connect(cb_browse, SIGNAL(activated(const QString &)), SLOT(doBrowse(const QString &)));
 
80
        connect(li_services, SIGNAL(highlighted(int)), SLOT(serviceSelected(int)));
 
81
        connect(li_services, SIGNAL(doubleClicked(QListBoxItem *)), SLOT(doubleClicked(QListBoxItem *)));
51
82
        connect(pb_register, SIGNAL(clicked()), SLOT(serviceRegister()));
52
83
        connect(pb_search, SIGNAL(clicked()), SLOT(serviceSearch()));
53
 
        connect(li_services, SIGNAL(highlighted(int)), SLOT(serviceSelected(int)));
54
 
        connect(li_services, SIGNAL(doubleClicked(QListBoxItem *)), SLOT(doubleClicked(QListBoxItem *)));
55
 
 
56
 
        resize(400,260);
 
84
        connect(pb_join, SIGNAL(clicked()), SLOT(serviceJoin()));
 
85
 
 
86
        pb_browse->setDefault(true);
 
87
        cb_browse->setCurrentText(d->jid.full());
 
88
 
 
89
        pb_register->setEnabled(false);
 
90
        pb_search->setEnabled(false);
 
91
        pb_join->setEnabled(false);
 
92
        pb_join->hide();
 
93
 
 
94
        cb_browse->setFocus();
 
95
 
 
96
        if(d->pa->loggedIn())
 
97
                doBrowse();
57
98
}
58
99
 
59
100
ServicesDlg::~ServicesDlg()
60
101
{
61
 
        services_ptr = 0;
62
 
}
63
 
 
64
 
/* static */ ServicesDlg *ServicesDlg::find()
65
 
{
66
 
        return services_ptr;
67
 
}
68
 
 
69
 
void ServicesDlg::closeEvent(QCloseEvent *e)
70
 
{
71
 
        // cancel active transaction (refresh or getform)
72
 
        if(isBusy) {
73
 
                signalCancelTransaction(id);
74
 
        }
75
 
 
76
 
        // accept the close event
77
 
        e->accept();
 
102
        delete d->jt_gs;
 
103
        d->pa->dialogUnregister(this);
 
104
        delete d;
78
105
}
79
106
 
80
107
void ServicesDlg::restoreWidgets()
81
108
{
82
 
        if(!isBusy) {
83
 
                pb_refresh->setEnabled(TRUE);
84
 
                li_services->setEnabled(TRUE);
85
 
 
86
 
                serviceSelected(li_services->currentItem());
87
 
        }
88
 
}
89
 
 
90
 
void ServicesDlg::doRefresh()
91
 
{
92
 
        if(localStatus == STATUS_OFFLINE) {
93
 
                QMessageBox::warning(this, tr("Warning"), tr("You must be connected to the server to manage services.  Please login first."));
94
 
                return;
95
 
        }
96
 
 
97
 
        li_services->clear();
98
 
 
99
 
        isBusy = TRUE;
100
 
        busy->start();
101
 
        pb_refresh->setEnabled(FALSE);
102
 
        pb_register->setEnabled(FALSE);
103
 
        pb_search->setEnabled(FALSE);
104
 
 
105
 
        signalRefresh(&id);
106
 
}
107
 
 
108
 
void ServicesDlg::loadSuccess(JabRoster *list)
109
 
{
110
 
        servicesList = *list;
111
 
        li_services->clear();
112
 
 
113
 
        QPtrListIterator<JabRosterEntry> it(servicesList.list);
114
 
        JabRosterEntry *entry;
115
 
        for(; (entry = it.current()); ++it) {
116
 
                li_services->insertItem(transport2icon(entry->jid), entry->nick);
117
 
        }
118
 
 
119
 
        if(isBusy) {
120
 
                busy->stop();
121
 
                isBusy = FALSE;
122
 
                restoreWidgets();
123
 
        }
124
 
 
125
 
        if(li_services->count() > 0)
126
 
                li_services->setSelected(0, TRUE);
127
 
}
128
 
 
129
 
void ServicesDlg::loadFail()
130
 
{
131
 
        if(isBusy) {
132
 
                busy->stop();
133
 
                isBusy = FALSE;
134
 
                restoreWidgets();
135
 
 
136
 
                QMessageBox::critical(this, tr("Error"), tr("There was an error retrieving the list of services."));
137
 
        }
138
 
}
139
 
 
140
 
void ServicesDlg::loadFormSuccess()
141
 
{
142
 
        if(isBusy) {
143
 
                busy->stop();
144
 
                isBusy = FALSE;
145
 
                restoreWidgets();
146
 
        }
147
 
}
148
 
 
149
 
void ServicesDlg::loadFormFail()
150
 
{
151
 
        if(isBusy) {
152
 
                busy->stop();
153
 
                isBusy = FALSE;
154
 
                restoreWidgets();
155
 
 
156
 
                if(actionType == SERVICESDLG_ACT_REGISTER)
157
 
                        QMessageBox::critical(this, tr("Error"), tr("There was an error retrieving the registration form for the %1 service.").arg(regname));
158
 
                else
159
 
                        QMessageBox::critical(this, tr("Error"), tr("There was an error retrieving the search form for the %1 service.").arg(regname));
160
 
        }
161
 
}
162
 
 
163
 
void ServicesDlg::localUpdate(const JabRosterEntry &e)
164
 
{
165
 
        int oldstate = localStatus;
166
 
        localStatus = e.localStatus();
167
 
 
168
 
        if(localStatus == STATUS_OFFLINE) {
169
 
                if(isBusy) {
170
 
                        busy->stop();
171
 
                        isBusy = FALSE;
172
 
                }
173
 
 
174
 
                pb_register->setEnabled(FALSE);
175
 
                pb_search->setEnabled(FALSE);
176
 
                pb_refresh->setEnabled(FALSE);
177
 
                li_services->clear();
178
 
                servicesList.clear();
 
109
        pb_browse->setEnabled(true);
 
110
        cb_browse->setEnabled(true);
 
111
        li_services->setEnabled(true);
 
112
        serviceSelected(li_services->currentItem());
 
113
}
 
114
 
 
115
void ServicesDlg::doBrowse(const QString &host)
 
116
{
 
117
        if(!d->pa->checkConnected(this))
 
118
                return;
 
119
 
 
120
        Jid j;
 
121
        if(!host.isEmpty())
 
122
                j = host;
 
123
        else
 
124
                j = cb_browse->currentText();
 
125
 
 
126
        if(!j.isValid())
 
127
                return;
 
128
        d->jid = j;
 
129
        d->pa->psi()->recentBrowseAdd(d->jid.full());
 
130
        cb_browse->clear();
 
131
        cb_browse->insertStringList(d->pa->psi()->recentBrowseList());
 
132
 
 
133
        li_services->clear();
 
134
        d->agents.clear();
 
135
        d->busy->start();
 
136
 
 
137
        pb_browse->setEnabled(false);
 
138
        cb_browse->setEnabled(false);
 
139
        pb_register->setEnabled(false);
 
140
        pb_search->setEnabled(false);
 
141
        pb_join->setEnabled(false);
 
142
 
 
143
        d->jt_gs = new JT_GetServices(d->pa->client()->rootTask());
 
144
        connect(d->jt_gs, SIGNAL(finished()), SLOT(jt_gs_finished()));
 
145
        d->jt_gs->get(d->jid);
 
146
        d->jt_gs->go(true);
 
147
}
 
148
 
 
149
void ServicesDlg::jt_gs_finished()
 
150
{
 
151
        d->busy->stop();
 
152
        restoreWidgets();
 
153
 
 
154
        JT_GetServices *jt = d->jt_gs;
 
155
        d->jt_gs = 0;
 
156
 
 
157
        if(jt->success()) {
 
158
                const AgentList & agents = jt->agents();
 
159
                for(AgentList::ConstIterator it = agents.begin(); it != agents.end(); ++it)
 
160
                        li_services->insertItem(is->status((*it).jid(), STATUS_ONLINE), (*it).name());
 
161
                d->agents = agents;
 
162
                if(li_services->count() > 0)
 
163
                        li_services->setSelected(0, true);
179
164
        }
180
165
        else {
181
 
                // coming online?
182
 
                if(oldstate == STATUS_OFFLINE) {
183
 
                        doRefresh(); // this will also turn the refresh button on when finished
184
 
                }
 
166
                QMessageBox::critical(this, tr("Error"), tr("There was an error browsing the service list.\nReason: %1").arg(jt->statusString()));
185
167
        }
186
168
}
187
169
 
188
170
void ServicesDlg::doubleClicked(QListBoxItem *)
189
171
{
190
 
        serviceRegister();
 
172
        int x = li_services->currentItem();
 
173
        if(x < 0 || x >= (int)d->agents.count())
 
174
                return;
 
175
        const Features &f = (*(d->agents.at(x))).features();
 
176
        if(f.canGroupchat())
 
177
                serviceJoin();
 
178
        else if(f.canRegister())
 
179
                serviceRegister();
 
180
        else if(f.canSearch())
 
181
                serviceSearch();
191
182
}
192
183
 
193
184
void ServicesDlg::serviceSelected(int x)
194
185
{
195
 
        if(x == -1)
196
 
                return;
197
 
        JabRosterEntry *entry = servicesList.list.at(x);
198
 
        if(!entry)
199
 
                return;
200
 
 
201
 
        if(localStatus == STATUS_OFFLINE)
202
 
                return;
203
 
 
204
 
        pb_register->setEnabled(entry->isRegisterable);
205
 
        pb_search->setEnabled(entry->isSearchable);
 
186
        if(x < 0 || x >= (int)d->agents.count())
 
187
                return;
 
188
 
 
189
        const Features &f = (*(d->agents.at(x))).features();
 
190
 
 
191
        pb_register->setEnabled(f.canRegister());
 
192
        pb_search->setEnabled(f.canRegister());
 
193
        pb_join->setEnabled(f.canGroupchat());
206
194
}
207
195
 
208
196
void ServicesDlg::serviceRegister()
209
197
{
210
 
        if(!pb_register->isEnabled())
211
 
                return;
212
 
 
213
 
        JabRosterEntry *entry = servicesList.list.at(li_services->currentItem());
214
 
        if(!entry)
215
 
                return;
216
 
 
217
 
        //printf("ServicesDlg: getting registration form: %s\n", entry->nick.latin1());
218
 
        regname = entry->jid;
219
 
        actionType = SERVICESDLG_ACT_REGISTER;
220
 
 
221
 
        // block user input
222
 
        li_services->setEnabled(FALSE);
223
 
        pb_refresh->setEnabled(FALSE);
224
 
        pb_register->setEnabled(FALSE);
225
 
        pb_search->setEnabled(FALSE);
226
 
 
227
 
        isBusy = TRUE;
228
 
        busy->start();
229
 
 
230
 
        signalGetRegForm(entry->jid, &id);
 
198
        int x = li_services->currentItem();
 
199
        if(x < 0 || x >= (int)d->agents.count())
 
200
                return;
 
201
        const AgentItem &a = *(d->agents.at(x));
 
202
        signalRegister(a.jid());
231
203
}
232
204
 
233
205
void ServicesDlg::serviceSearch()
234
206
{
235
 
        JabRosterEntry *entry = servicesList.list.at(li_services->currentItem());
236
 
        if(!entry)
237
 
                return;
238
 
 
239
 
        //printf("ServicesDlg: getting search form: %s\n", entry->nick.latin1());
240
 
        regname = entry->jid;
241
 
        actionType = SERVICESDLG_ACT_SEARCH;
242
 
 
243
 
        // block user input
244
 
        li_services->setEnabled(FALSE);
245
 
        pb_refresh->setEnabled(FALSE);
246
 
        pb_register->setEnabled(FALSE);
247
 
        pb_search->setEnabled(FALSE);
248
 
 
249
 
        isBusy = TRUE;
250
 
        busy->start();
251
 
 
252
 
        signalSearch(entry->jid, &id);
253
 
}
254
 
 
255
 
 
256
 
RegistrationDlg::RegistrationDlg(const JabForm &_form, QWidget *parent, const char *name)
257
 
:QDialog(parent, name, FALSE, WDestructiveClose), UniqueWindow(TRUE, "RegistrationDlg", this, cleanJid(_form.jid.s()))
258
 
{
259
 
        form = _form;
260
 
        isBusy = FALSE;
261
 
 
262
 
        setCaption(CAP(tr("Service Registration")));
263
 
        resize(16, 16);
 
207
        int x = li_services->currentItem();
 
208
        if(x < 0 || x >= (int)d->agents.count())
 
209
                return;
 
210
        const AgentItem &a = *(d->agents.at(x));
 
211
        signalSearch(a.jid());
 
212
}
 
213
 
 
214
void ServicesDlg::serviceJoin()
 
215
{
 
216
        int x = li_services->currentItem();
 
217
        if(x < 0 || x >= (int)d->agents.count())
 
218
                return;
 
219
        const AgentItem &a = *(d->agents.at(x));
 
220
        signalJoin(a.jid());
 
221
}
 
222
 
 
223
 
 
224
//----------------------------------------------------------------------------
 
225
// RegistrationDlg
 
226
//----------------------------------------------------------------------------
 
227
class RegistrationDlg::Private
 
228
{
 
229
public:
 
230
        Private() {}
 
231
 
 
232
        Jid jid;
 
233
        PsiAccount *pa;
 
234
 
 
235
        QPushButton *pb_close, *pb_reg;
 
236
        QGuardedPtr<JT_Register> jt;
 
237
        int type;
 
238
        BusyWidget *busy;
 
239
        QLabel *lb_top;
 
240
        QGrid *gr_form;
 
241
        Form form;
 
242
 
 
243
        QPtrList<QLabel> lb_field;
 
244
        QPtrList<QLineEdit> le_field;
 
245
};
 
246
 
 
247
RegistrationDlg::RegistrationDlg(const Jid &jid, PsiAccount *pa)
 
248
:QDialog(0, 0, false, WDestructiveClose)
 
249
{
 
250
        d = new Private;
 
251
        d->jid = jid;
 
252
        d->pa = pa;
 
253
        d->pa->dialogRegister(this, d->jid);
 
254
        d->jt = 0;
 
255
 
 
256
        d->lb_field.setAutoDelete(true);
 
257
        d->le_field.setAutoDelete(true);
 
258
 
 
259
        setCaption(tr("Registration: %1").arg(d->jid.full()));
264
260
 
265
261
        QVBoxLayout *vb1 = new QVBoxLayout(this, 4);
266
 
        QLabel *l;
267
 
        l = new QLabel(QString(tr("Registration for \"%1\"")).arg(form.jid.s()), this);
268
 
        l->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
269
 
        vb1->addWidget(l);
270
 
        l = new QLabel(QString("<qt>%1</qt>").arg(form.instructions), this);
271
 
        l->setFrameStyle( QFrame::Panel | QFrame::Sunken );
272
 
        vb1->addWidget(l);
273
 
 
274
 
        QGridLayout *gr = new QGridLayout(vb1, 2);
275
 
        int at = 0;
276
 
        lb_field.setAutoDelete(TRUE);
277
 
        le_field.setAutoDelete(TRUE);
278
 
        QPtrListIterator<JabFormField> it(form);
279
 
        JabFormField *f;
280
 
        for(; (f = it.current()); ++it) {
281
 
                QLabel *lb = new QLabel(f->fieldName(), this);
282
 
                QLineEdit *le = new QLineEdit(this);
283
 
                if(f->type() == JabFormField::password)
284
 
                        le->setEchoMode(QLineEdit::Password);
285
 
                le->setText(f->value());
286
 
 
287
 
                lb_field.append(lb);
288
 
                le_field.append(le);
289
 
 
290
 
                gr->addWidget(lb, at, 0);
291
 
                gr->addWidget(le, at, 1);
292
 
                ++at;
293
 
        }
294
 
 
295
 
        QHBoxLayout *hb1 = new QHBoxLayout(vb1);
296
 
        busy = new BusyWidget(this);
297
 
        hb1->addStretch(1);
298
 
        hb1->addWidget(busy);
 
262
        d->lb_top = new QLabel(this);
 
263
        d->lb_top->setFrameStyle( QFrame::Panel | QFrame::Sunken );
 
264
        d->lb_top->hide();
 
265
        vb1->addWidget(d->lb_top);
 
266
 
 
267
        d->gr_form = new QGrid(2, Horizontal, this);
 
268
        d->gr_form->setSpacing(4);
 
269
        vb1->addWidget(d->gr_form);
 
270
        d->gr_form->hide();
299
271
 
300
272
        QFrame *line = new QFrame(this);
301
273
        line->setFixedHeight(2);
302
274
        line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
303
275
        vb1->addWidget(line);
304
276
 
305
 
        QHBoxLayout *hb2 = new QHBoxLayout(vb1);
306
 
        pb_register = new QPushButton(tr("&Register"), this);
307
 
        connect(pb_register, SIGNAL(clicked()), SLOT(doRegSubmit()));
308
 
        hb2->addWidget(pb_register);
309
 
        hb2->addStretch(1);
310
 
        pb_close = new QPushButton(tr("&Close"), this);
 
277
        QHBoxLayout *hb1 = new QHBoxLayout(vb1);
 
278
        d->busy = new BusyWidget(this);
 
279
        hb1->addWidget(d->busy);
 
280
        hb1->addStretch(1);
 
281
        d->pb_reg = new QPushButton(tr("&Register"), this);
 
282
        d->pb_reg->setDefault(true);
 
283
        connect(d->pb_reg, SIGNAL(clicked()), SLOT(doRegSet()));
 
284
        hb1->addWidget(d->pb_reg);
 
285
        d->pb_close = new QPushButton(tr("&Close"), this);
 
286
        connect(d->pb_close, SIGNAL(clicked()), SLOT(close()));
 
287
        hb1->addWidget(d->pb_close);
 
288
 
 
289
        d->pb_reg->hide();
 
290
 
 
291
        doRegGet();
 
292
}
 
293
 
 
294
RegistrationDlg::~RegistrationDlg()
 
295
{
 
296
        delete d->jt;
 
297
        d->pa->dialogUnregister(this);
 
298
        delete d;
 
299
}
 
300
 
 
301
/*void RegistrationDlg::closeEvent(QCloseEvent *e)
 
302
{
 
303
        e->ignore();
 
304
        reject();
 
305
}*/
 
306
 
 
307
void RegistrationDlg::done(int r)
 
308
{
 
309
        if(d->busy->isActive() && d->type == 1) {
 
310
                int n = QMessageBox::information(this, tr("Busy"), tr("<qt>Registration has already been submitted, so closing this window will not prevent the registration from happening.  Do you still wish to close?</qt>"), tr("&Yes"), tr("&No"));
 
311
                if(n != 0)
 
312
                        return;
 
313
        }
 
314
        QDialog::done(r);
 
315
}
 
316
 
 
317
void RegistrationDlg::doRegGet()
 
318
{
 
319
        d->lb_top->setText(tr("Fetching registration form for %1 ...").arg(d->jid.full()));
 
320
        d->lb_top->show();
 
321
        d->busy->start();
 
322
 
 
323
        d->type = 0;
 
324
        d->jt = new JT_Register(d->pa->client()->rootTask());
 
325
        connect(d->jt, SIGNAL(finished()), SLOT(jt_finished()));
 
326
        d->jt->getForm(d->jid);
 
327
        d->jt->go(true);
 
328
}
 
329
 
 
330
void RegistrationDlg::doRegSet()
 
331
{
 
332
        if(!d->pa->checkConnected(this))
 
333
                return;
 
334
 
 
335
        Form submitForm = d->form;
 
336
 
 
337
        // import the changes back into the form.
 
338
        // the QPtrList of QLineEdits should be in the same order
 
339
        QPtrListIterator<QLineEdit> lit(d->le_field);
 
340
        for(Form::Iterator it = submitForm.begin(); it != submitForm.end(); ++it) {
 
341
                FormField &f = *it;
 
342
                QLineEdit *le = lit.current();
 
343
                f.setValue(le->text());
 
344
                ++lit;
 
345
        }
 
346
 
 
347
        d->gr_form->setEnabled(false);
 
348
        d->pb_reg->setEnabled(false);
 
349
        d->busy->start();
 
350
 
 
351
        d->type = 1;
 
352
        d->jt = new JT_Register(d->pa->client()->rootTask());
 
353
        connect(d->jt, SIGNAL(finished()), SLOT(jt_finished()));
 
354
        d->jt->setForm(submitForm);
 
355
        d->jt->go(true);
 
356
}
 
357
 
 
358
void RegistrationDlg::jt_finished()
 
359
{
 
360
        d->busy->stop();
 
361
        d->gr_form->setEnabled(true);
 
362
        d->pb_reg->setEnabled(true);
 
363
        JT_Register *jt = d->jt;
 
364
        d->jt = 0;
 
365
 
 
366
        if(jt->success()) {
 
367
                if(d->type == 0) {
 
368
                        d->form = jt->form();
 
369
                        QString str = tr("<b>Registration for \"%1\":</b><br><br>").arg(d->jid.full());
 
370
                        str += plain2rich(d->form.instructions());
 
371
                        d->lb_top->setText(str);
 
372
                        d->lb_top->setFixedWidth(300);
 
373
 
 
374
                        for(Form::ConstIterator it = d->form.begin(); it != d->form.end(); ++it) {
 
375
                                const FormField &f = *it;
 
376
 
 
377
                                QLabel *lb = new QLabel(f.fieldName(), d->gr_form);
 
378
                                QLineEdit *le = new QLineEdit(d->gr_form);
 
379
                                if(f.isSecret())
 
380
                                        le->setEchoMode(QLineEdit::Password);
 
381
                                le->setText(f.value());
 
382
 
 
383
                                d->lb_field.append(lb);
 
384
                                d->le_field.append(le);
 
385
                        }
 
386
 
 
387
                        if (!d->le_field.isEmpty())
 
388
                                d->le_field.first()->setFocus();
 
389
 
 
390
                        d->gr_form->show();
 
391
                        d->pb_reg->show();
 
392
 
 
393
                        qApp->processEvents();
 
394
                        //QDesktopWidget *w = QApplication::desktop();
 
395
                        //setGeometry((w->width()/2)-(width()/2), (w->height()/2)-(height()/2), minimumWidth(), minimumHeight());
 
396
                        resize(minimumSize());
 
397
                }
 
398
                else {
 
399
                        closeDialogs(this);
 
400
                        QMessageBox::information(this, tr("Success"), tr("Registration successful."));
 
401
                        close();
 
402
                }
 
403
        }
 
404
        else {
 
405
                if(d->type == 0) {
 
406
                        QMessageBox::critical(this, tr("Error"), tr("Unable to retrieve registration form.\nReason: %1").arg(jt->statusString()));
 
407
                        close();
 
408
                }
 
409
                else {
 
410
                        closeDialogs(this);
 
411
                        QMessageBox::critical(this, tr("Error"), tr("Error submitting registration form.\nReason: %1").arg(jt->statusString()));
 
412
                        close();
 
413
                }
 
414
        }
 
415
}
 
416
 
 
417
 
 
418
//----------------------------------------------------------------------------
 
419
// SearchDlg
 
420
//----------------------------------------------------------------------------
 
421
class SearchDlg::Private
 
422
{
 
423
public:
 
424
        Private() {}
 
425
 
 
426
        PsiAccount *pa;
 
427
        Jid jid;
 
428
        Form form;
 
429
        BusyWidget *busy;
 
430
        QGuardedPtr<JT_Search> jt;
 
431
        QGrid *gr_form;
 
432
        int type;
 
433
 
 
434
        QPtrList<QLabel> lb_field;
 
435
        QPtrList<QLineEdit> le_field;
 
436
};
 
437
 
 
438
SearchDlg::SearchDlg(const Jid &jid, PsiAccount *pa)
 
439
:SearchUI(0, 0, WDestructiveClose)
 
440
{
 
441
        d = new Private;
 
442
        d->pa = pa;
 
443
        d->jid = jid;
 
444
        d->pa->dialogRegister(this, d->jid);
 
445
        d->jt = 0;
 
446
 
 
447
        setCaption(caption().arg(d->jid.full()));
 
448
 
 
449
        d->busy = busy;
 
450
 
 
451
        d->gr_form = new QGrid(2, Horizontal, gb_search);
 
452
        d->gr_form->setSpacing(4);
 
453
        replaceWidget(lb_form, d->gr_form);
 
454
        d->gr_form->hide();
 
455
 
 
456
        pb_add->setEnabled(false);
 
457
        pb_info->setEnabled(false);
 
458
        pb_stop->setEnabled(false);
 
459
        pb_search->setEnabled(false);
 
460
 
 
461
        lv_results->setMultiSelection(true);
 
462
        lv_results->setSelectionMode( QListView::Extended );
 
463
        connect(lv_results, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
311
464
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
312
 
        hb2->addWidget(pb_close);
313
 
 
314
 
        vb1->activate();
315
 
 
316
 
        resize(320, height() + 80);
317
 
}
318
 
 
319
 
void RegistrationDlg::closeEvent(QCloseEvent *e)
320
 
{
321
 
        if(isBusy) {
322
 
                //QMessageBox::information(this, tr("Busy"), tr("Registration has already been submitted, it cannot be canceled.  Please wait for the server to respond."));
323
 
                e->ignore();
324
 
                return;
325
 
        }
326
 
 
327
 
        e->accept();
328
 
}
329
 
 
330
 
void RegistrationDlg::putRegFormResponse(bool ok, const QString &err)
331
 
{
332
 
        busy->stop();
333
 
        isBusy = FALSE;
334
 
 
335
 
        if(ok) {
336
 
                QMessageBox::information(this, tr("Success"), tr("Registration was successful."));
337
 
        }
338
 
        else {
339
 
                QMessageBox::critical(this, tr("Failed"), QString(tr("Registration failed.  Reason:\n\"%1\"")).arg(err));
340
 
        }
341
 
 
342
 
        close();
343
 
}
344
 
 
345
 
void RegistrationDlg::doRegSubmit()
346
 
{
347
 
        JabForm submitform = form;
348
 
 
349
 
        // import the changes back into the form.
350
 
        // the QPtrList of QLineEdits should be in the same order
351
 
        QPtrListIterator<JabFormField> it(submitform);
352
 
        QPtrListIterator<QLineEdit> lit(le_field);
353
 
        JabFormField *f;
354
 
        for(; (f = it.current());) {
355
 
                QLineEdit *le = lit.current();
356
 
                f->setValue(le->text());
357
 
                ++it;
358
 
                ++lit;
359
 
        }
360
 
 
361
 
        pb_register->setEnabled(FALSE);
362
 
        pb_close->setEnabled(FALSE);
363
 
        isBusy = TRUE;
364
 
        busy->start();
365
 
 
366
 
        signalSubmitForm(submitform, &id);
367
 
}
368
 
 
369
 
void RegistrationDlg::localUpdate(const JabRosterEntry &e)
370
 
{
371
 
        int localStatus = e.localStatus();
372
 
 
373
 
        // if the user goes offline then cancel the form
 
465
        connect(pb_search, SIGNAL(clicked()), SLOT(doSearchSet()));
 
466
        connect(pb_stop, SIGNAL(clicked()), SLOT(doStop()));
 
467
        connect(pb_add, SIGNAL(clicked()), SLOT(doAdd()));
 
468
        connect(pb_info, SIGNAL(clicked()), SLOT(doInfo()));
 
469
 
 
470
        resize(600,440);
 
471
 
 
472
        doSearchGet();
 
473
}
 
474
 
 
475
SearchDlg::~SearchDlg()
 
476
{
 
477
        delete d->jt;
 
478
        d->pa->dialogUnregister(this);
 
479
        delete d;
 
480
}
 
481
 
 
482
/*void SearchDlg::localUpdate(const JabRosterEntry &e)
 
483
{
 
484
        int oldstate = localStatus;
 
485
        localStatus = e.localStatus();
 
486
 
374
487
        if(localStatus == STATUS_OFFLINE) {
375
488
                if(isBusy) {
376
 
                        putRegFormResponse(FALSE, "Disconnected");  // <-- this will turn off busy also
377
 
                }
378
 
                close();
379
 
        }
380
 
}
381
 
 
382
 
/*static*/ RegistrationDlg * RegistrationDlg::find(const QString &xjid)
383
 
{
384
 
        return (RegistrationDlg *)UniqueWindowBank::find("RegistrationDlg", cleanJid(xjid));
 
489
                        busy->stop();
 
490
                        isBusy = FALSE;
 
491
                }
 
492
 
 
493
                pb_search->setEnabled(FALSE);
 
494
                pb_stop->setEnabled(FALSE);
 
495
                clear();
 
496
        }
 
497
        else {
 
498
                // coming online?
 
499
                if(oldstate == STATUS_OFFLINE) {
 
500
                        pb_search->setEnabled(TRUE);
 
501
                }
 
502
        }
 
503
}*/
 
504
 
 
505
void SearchDlg::addEntry(const QString &jid, const QString &nick, const QString &first, const QString &last, const QString &email)
 
506
{
 
507
        QListViewItem *lvi = new QListViewItem(lv_results);
 
508
        lvi->setText(0, nick);
 
509
        lvi->setText(1, first);
 
510
        lvi->setText(2, last);
 
511
        lvi->setText(3, email);
 
512
        lvi->setText(4, jid);
 
513
}
 
514
 
 
515
void SearchDlg::doSearchGet()
 
516
{
 
517
        lb_instructions->setText(tr("<qt>Fetching search form for %1 ...</qt>").arg(d->jid.full()));
 
518
        d->busy->start();
 
519
 
 
520
        d->type = 0;
 
521
        d->jt = new JT_Search(d->pa->client()->rootTask());
 
522
        connect(d->jt, SIGNAL(finished()), SLOT(jt_finished()));
 
523
        d->jt->get(d->jid);
 
524
        d->jt->go(true);
 
525
}
 
526
 
 
527
void SearchDlg::doSearchSet()
 
528
{
 
529
        if(d->busy->isActive())
 
530
                return;
 
531
 
 
532
        if(!d->pa->checkConnected(this))
 
533
                return;
 
534
 
 
535
        Form submitForm = d->form;
 
536
 
 
537
        // import the changes back into the form.
 
538
        // the QPtrList of QLineEdits should be in the same order
 
539
        QPtrListIterator<QLineEdit> lit(d->le_field);
 
540
        for(Form::Iterator it = submitForm.begin(); it != submitForm.end(); ++it) {
 
541
                FormField &f = *it;
 
542
                QLineEdit *le = lit.current();
 
543
                f.setValue(le->text());
 
544
                ++lit;
 
545
        }
 
546
 
 
547
        clear();
 
548
 
 
549
        pb_search->setEnabled(false);
 
550
        pb_stop->setEnabled(true);
 
551
        d->gr_form->setEnabled(false);
 
552
        d->busy->start();
 
553
 
 
554
        d->type = 1;
 
555
        d->jt = new JT_Search(d->pa->client()->rootTask());
 
556
        connect(d->jt, SIGNAL(finished()), SLOT(jt_finished()));
 
557
        d->jt->set(submitForm);
 
558
        d->jt->go(true);
 
559
}
 
560
 
 
561
void SearchDlg::jt_finished()
 
562
{
 
563
        d->busy->stop();
 
564
        JT_Search *jt = d->jt;
 
565
        d->jt = 0;
 
566
 
 
567
        if(d->type == 1) {
 
568
                d->gr_form->setEnabled(true);
 
569
                pb_search->setEnabled(true);
 
570
                pb_stop->setEnabled(false);
 
571
        }
 
572
 
 
573
        if(jt->success()) {
 
574
                if(d->type == 0) {
 
575
                        d->form = jt->form();
 
576
                        QString str = plain2rich(d->form.instructions());
 
577
                        lb_instructions->setText(str);
 
578
 
 
579
                        for(Form::ConstIterator it = d->form.begin(); it != d->form.end(); ++it) {
 
580
                                const FormField &f = *it;
 
581
 
 
582
                                QLabel *lb = new QLabel(f.fieldName(), d->gr_form);
 
583
                                QLineEdit *le = new QLineEdit(d->gr_form);
 
584
                                if(f.isSecret())
 
585
                                        le->setEchoMode(QLineEdit::Password);
 
586
                                le->setText(f.value());
 
587
 
 
588
                                d->lb_field.append(lb);
 
589
                                d->le_field.append(le);
 
590
 
 
591
                                connect(le, SIGNAL(returnPressed()), this, SLOT(doSearchSet()));
 
592
                        }
 
593
 
 
594
                        d->gr_form->show();
 
595
                        pb_search->setEnabled(true);
 
596
                }
 
597
                else {
 
598
                        const QValueList<SearchResult> &list = jt->results();
 
599
                        if(list.isEmpty())
 
600
                                QMessageBox::information(this, tr("Search Results"), tr("Search returned 0 results."));
 
601
                        else {
 
602
                                for(QValueList<SearchResult>::ConstIterator it = list.begin(); it != list.end(); ++it) {
 
603
                                        const SearchResult &r = *it;
 
604
                                        addEntry(r.jid().full(), r.nick(), r.first(), r.last(), r.email());
 
605
                                }
 
606
                        }
 
607
                }
 
608
        }
 
609
        else {
 
610
                if(d->type == 0) {
 
611
                        QMessageBox::critical(this, tr("Error"), tr("Unable to retrieve search form.\nReason: %1").arg(jt->statusString()));
 
612
                        close();
 
613
                }
 
614
                else {
 
615
                        QMessageBox::critical(this, tr("Error"), tr("Error retrieving search results.\nReason: %1").arg(jt->statusString()));
 
616
                }
 
617
        }
 
618
}
 
619
 
 
620
void SearchDlg::clear()
 
621
{
 
622
        lv_results->clear();
 
623
        pb_add->setEnabled(false);
 
624
        pb_info->setEnabled(false);
 
625
}
 
626
 
 
627
void SearchDlg::doStop()
 
628
{
 
629
        if(!d->busy->isActive())
 
630
                return;
 
631
 
 
632
        delete d->jt;
 
633
        d->jt = 0;
 
634
 
 
635
        d->busy->stop();
 
636
        d->gr_form->setEnabled(true);
 
637
        pb_search->setEnabled(true);
 
638
        pb_stop->setEnabled(false);
 
639
}
 
640
 
 
641
void SearchDlg::selectionChanged()
 
642
{
 
643
        int d = 0;
 
644
        QListViewItem *lastChild = lv_results->firstChild();
 
645
 
 
646
        if(!lastChild) {
 
647
                pb_add->setEnabled(false);
 
648
                pb_info->setEnabled(false);
 
649
                return;
 
650
        }
 
651
 
 
652
        if( lastChild->isSelected() ) {
 
653
                pb_add->setEnabled(true);
 
654
                pb_info->setEnabled(true);
 
655
        }
 
656
        d++;
 
657
 
 
658
        if ( lastChild ) {
 
659
                while ( lastChild->nextSibling() ) {
 
660
                        lastChild = lastChild->nextSibling();
 
661
                        if( lastChild->isSelected() ) {
 
662
                                pb_add->setEnabled(true);
 
663
                                pb_info->setEnabled(true);
 
664
                        }
 
665
                        d++;
 
666
                }
 
667
        }
 
668
}
 
669
 
 
670
void SearchDlg::doAdd()
 
671
{
 
672
        int d = 0;
 
673
        QListViewItem *i = lv_results->firstChild();
 
674
        QString name;
 
675
 
 
676
        if(!i)
 
677
                return;
 
678
 
 
679
        if( i->isSelected() ) {
 
680
                name = jidnick(i->text(4), i->text(0));
 
681
                add(Jid(i->text(4)), i->text(0), QStringList(), true);
 
682
                d++;
 
683
        }
 
684
 
 
685
        if( i ) {
 
686
                while( i->nextSibling() ) {
 
687
                        i = i->nextSibling();
 
688
                        if( i->isSelected() ) {
 
689
                                name = jidnick(i->text(4), i->text(0));
 
690
                                add(Jid(i->text(4)), i->text(0), QStringList(), true);
 
691
                                d++;
 
692
                        }
 
693
                }
 
694
        }
 
695
 
 
696
        if( d==1 )
 
697
                QMessageBox::information(this, tr("Add User: Success"), tr("Added %1 to your roster.").arg(name));
 
698
        else
 
699
                QMessageBox::information(this, tr("Add User: Success"), tr("Added %1 users to your roster.").arg(d));
 
700
}
 
701
 
 
702
void SearchDlg::doInfo()
 
703
{
 
704
        QListViewItem *i = lv_results->firstChild();
 
705
        QString name;
 
706
 
 
707
        if(!i)
 
708
                return;
 
709
 
 
710
        if( i->isSelected() ) {
 
711
                aInfo(Jid(i->text(4)));
 
712
        }
 
713
 
 
714
        if( i ) {
 
715
                while( i->nextSibling() ) {
 
716
                        i = i->nextSibling();
 
717
                        if( i->isSelected() ) {
 
718
                                aInfo(Jid(i->text(4)));
 
719
                        }
 
720
                }
 
721
        }
385
722
}