~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: 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
** 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
 
 
21
#include<qlayout.h>
 
22
#include<qpushbutton.h>
 
23
#include<qframe.h>
 
24
 
 
25
#include"servicesdlg.h"
 
26
#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
{
 
34
        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()));
 
50
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
 
51
        connect(pb_register, SIGNAL(clicked()), SLOT(serviceRegister()));
 
52
        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);
 
57
}
 
58
 
 
59
ServicesDlg::~ServicesDlg()
 
60
{
 
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();
 
78
}
 
79
 
 
80
void ServicesDlg::restoreWidgets()
 
81
{
 
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();
 
179
        }
 
180
        else {
 
181
                // coming online?
 
182
                if(oldstate == STATUS_OFFLINE) {
 
183
                        doRefresh(); // this will also turn the refresh button on when finished
 
184
                }
 
185
        }
 
186
}
 
187
 
 
188
void ServicesDlg::doubleClicked(QListBoxItem *)
 
189
{
 
190
        serviceRegister();
 
191
}
 
192
 
 
193
void ServicesDlg::serviceSelected(int x)
 
194
{
 
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);
 
206
}
 
207
 
 
208
void ServicesDlg::serviceRegister()
 
209
{
 
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);
 
231
}
 
232
 
 
233
void ServicesDlg::serviceSearch()
 
234
{
 
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);
 
264
 
 
265
        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);
 
299
 
 
300
        QFrame *line = new QFrame(this);
 
301
        line->setFixedHeight(2);
 
302
        line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
 
303
        vb1->addWidget(line);
 
304
 
 
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);
 
311
        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
 
374
        if(localStatus == STATUS_OFFLINE) {
 
375
                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));
 
385
}