~ubuntu-branches/ubuntu/hoary/psi/hoary

« back to all changes in this revision

Viewing changes to src/searchdlg.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
** searchdlg.cpp - a dialog for searching for Jabber contacts
 
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"searchdlg.h"
 
22
#include"common.h"
 
23
#include<qlayout.h>
 
24
#include<qpushbutton.h>
 
25
#include<qlistview.h>
 
26
#include<qgroupbox.h>
 
27
 
 
28
 
 
29
SearchDlg::SearchDlg(const QString &jid, const JabForm &_form, int _localStatus, QWidget *parent, const char *name)
 
30
:SearchUI(parent, name, WDestructiveClose), UniqueWindow(TRUE, "SearchDlg", this, cleanJid(jid))
 
31
{
 
32
        v_jid = jid;
 
33
        form = _form;
 
34
        localStatus = _localStatus;
 
35
 
 
36
        setCaption(QString(tr("Search: %1")).arg(jid));
 
37
        //setIcon(transport2icon("", STATUS_ONLINE));
 
38
 
 
39
        isBusy = FALSE;
 
40
 
 
41
        QWidget *w;
 
42
        QLabel *lb;
 
43
 
 
44
        w = new QWidget(this);
 
45
        busy = new BusyWidget(w);
 
46
        QHBoxLayout *hb1 = new QHBoxLayout(w);
 
47
        hb1->addStretch(1);
 
48
        hb1->addWidget(busy);
 
49
 
 
50
        int i = SearchUILayout->findWidget(line);
 
51
        SearchUILayout->insertWidget(i, w);
 
52
 
 
53
        // instructions
 
54
        lb = new QLabel(QString("<qt>%1</qt>").arg(form.instructions), gb_search);
 
55
        lb->setFrameStyle( QFrame::Panel | QFrame::Sunken );
 
56
        gb_searchLayout->insertWidget(0, lb);
 
57
 
 
58
        // form
 
59
        w = new QWidget(gb_search);
 
60
        QGridLayout *gr = new QGridLayout(w, 2, 1, 4, 8);
 
61
        int at = 0;
 
62
        lb_field.setAutoDelete(TRUE);
 
63
        le_field.setAutoDelete(TRUE);
 
64
        QPtrListIterator<JabFormField> it(form);
 
65
        JabFormField *f;
 
66
        for(; (f = it.current()); ++it) {
 
67
                QLabel *lb = new QLabel(f->fieldName(), w);
 
68
                QLineEdit *le = new QLineEdit(w);
 
69
                le->setMinimumWidth(120);
 
70
                if(f->type() == JabFormField::password)
 
71
                        le->setEchoMode(QLineEdit::Password);
 
72
                le->setText(f->value());
 
73
 
 
74
                lb_field.append(lb);
 
75
                le_field.append(le);
 
76
 
 
77
                gr->addWidget(lb, at, 0);
 
78
                gr->addWidget(le, at, 1);
 
79
                ++at;
 
80
 
 
81
                connect(le, SIGNAL(returnPressed()), this, SLOT(doSubmitForm()));
 
82
        }
 
83
        gb_searchLayout->insertWidget(1, w);
 
84
 
 
85
 
 
86
        pb_add->setEnabled(FALSE);
 
87
        pb_info->setEnabled(FALSE);
 
88
        pb_stop->setEnabled(FALSE);
 
89
 
 
90
        if(localStatus == STATUS_OFFLINE)
 
91
                pb_search->setEnabled(FALSE);
 
92
 
 
93
        connect(pb_close, SIGNAL(clicked()), SLOT(close()));
 
94
        connect(pb_search, SIGNAL(clicked()), SLOT(doSubmitForm()));
 
95
        connect(pb_stop, SIGNAL(clicked()), SLOT(doStop()));
 
96
        connect(pb_add, SIGNAL(clicked()), SLOT(doAdd()));
 
97
        connect(pb_info, SIGNAL(clicked()), SLOT(doInfo()));
 
98
        connect(lv_results, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
 
99
}
 
100
 
 
101
SearchDlg::~SearchDlg()
 
102
{
 
103
}
 
104
 
 
105
/* static */ SearchDlg *SearchDlg::find(const QString &xjid)
 
106
{
 
107
        return (SearchDlg *)UniqueWindowBank::find("SearchDlg", cleanJid(xjid));
 
108
}
 
109
 
 
110
void SearchDlg::closeEvent(QCloseEvent *e)
 
111
{
 
112
        // cancel active transaction
 
113
        if(isBusy) {
 
114
                signalCancelTransaction(id);
 
115
        }
 
116
 
 
117
        e->accept();
 
118
}
 
119
 
 
120
void SearchDlg::localUpdate(const JabRosterEntry &e)
 
121
{
 
122
        int oldstate = localStatus;
 
123
        localStatus = e.localStatus();
 
124
 
 
125
        if(localStatus == STATUS_OFFLINE) {
 
126
                if(isBusy) {
 
127
                        busy->stop();
 
128
                        isBusy = FALSE;
 
129
                }
 
130
 
 
131
                pb_search->setEnabled(FALSE);
 
132
                pb_stop->setEnabled(FALSE);
 
133
                clear();
 
134
        }
 
135
        else {
 
136
                // coming online?
 
137
                if(oldstate == STATUS_OFFLINE) {
 
138
                        pb_search->setEnabled(TRUE);
 
139
                }
 
140
        }
 
141
}
 
142
 
 
143
void SearchDlg::addEntry(const QString &jid, const QString &nick, const QString &first, const QString &last, const QString &email)
 
144
{
 
145
        QListViewItem *lvi = new QListViewItem(lv_results);
 
146
        lvi->setText(0, nick);
 
147
        //lvi->setPixmap(0, *pix_online);
 
148
        lvi->setText(1, first);
 
149
        lvi->setText(2, last);
 
150
        lvi->setText(3, email);
 
151
        lvi->setText(4, jid);
 
152
}
 
153
 
 
154
void SearchDlg::clear()
 
155
{
 
156
        lv_results->clear();
 
157
 
 
158
        pb_add->setEnabled(FALSE);
 
159
        pb_info->setEnabled(FALSE);
 
160
}
 
161
 
 
162
void SearchDlg::doSubmitForm()
 
163
{
 
164
        if(isBusy || !pb_search->isEnabled())
 
165
                return;
 
166
 
 
167
        clear();
 
168
 
 
169
        JabForm submitform = form;
 
170
 
 
171
        // import the changes back into the form.
 
172
        // the QPtrList of QLineEdits should be in the same order
 
173
        QPtrListIterator<JabFormField> it(submitform);
 
174
        QPtrListIterator<QLineEdit> lit(le_field);
 
175
        JabFormField *f;
 
176
        for(; (f = it.current());) {
 
177
                QLineEdit *le = lit.current();
 
178
                f->setValue(le->text());
 
179
                ++it;
 
180
                ++lit;
 
181
        }
 
182
 
 
183
        pb_search->setEnabled(FALSE);
 
184
        pb_stop->setEnabled(TRUE);
 
185
 
 
186
        isBusy = TRUE;
 
187
        busy->start();
 
188
 
 
189
        signalSubmitForm(submitform, &id);
 
190
}
 
191
 
 
192
void SearchDlg::doStop()
 
193
{
 
194
        if(!isBusy || !pb_stop->isEnabled())
 
195
                return;
 
196
 
 
197
        signalCancelTransaction(id);
 
198
        isBusy = FALSE;
 
199
        busy->stop();
 
200
 
 
201
        pb_search->setEnabled(TRUE);
 
202
        pb_stop->setEnabled(FALSE);
 
203
}
 
204
 
 
205
void SearchDlg::putSearchFormResponse(JabRoster *p)
 
206
{
 
207
        if(isBusy) {
 
208
                isBusy = FALSE;
 
209
                busy->stop();
 
210
        }
 
211
        pb_search->setEnabled(TRUE);
 
212
        pb_stop->setEnabled(FALSE);
 
213
 
 
214
        // success
 
215
        if(p) {
 
216
                // fill out the results
 
217
                for(JabRosterEntry *entry = p->first(); entry; entry = p->next())
 
218
                        addEntry(entry->jid, entry->nick, entry->first, entry->last, entry->email);
 
219
        }
 
220
        else {
 
221
                QMessageBox::information(this, tr("Error"), tr("There was an error retrieving search results."));
 
222
        }
 
223
}
 
224
 
 
225
void SearchDlg::selectionChanged()
 
226
{
 
227
        QListViewItem *i = lv_results->selectedItem();
 
228
        if(!i) {
 
229
                pb_add->setEnabled(FALSE);
 
230
                pb_info->setEnabled(FALSE);
 
231
        }
 
232
 
 
233
        pb_add->setEnabled(TRUE);
 
234
        pb_info->setEnabled(TRUE);
 
235
}
 
236
 
 
237
void SearchDlg::doAdd()
 
238
{
 
239
        QListViewItem *i = lv_results->selectedItem();
 
240
        if(!i)
 
241
                return;
 
242
 
 
243
        aAdd(i->text(4), i->text(0), "");
 
244
 
 
245
        QMessageBox::information(this, tr("Add User"), QString(tr("%1 has been added to your contact list.")).arg(i->text(0)));
 
246
}
 
247
 
 
248
void SearchDlg::doInfo()
 
249
{
 
250
        QListViewItem *i = lv_results->selectedItem();
 
251
        if(!i)
 
252
                return;
 
253
 
 
254
        aInfo(i->text(4));
 
255
}
 
256