~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to src/proxy.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2010-02-19 09:37:12 UTC
  • mfrom: (6.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100219093712-e225xvm1wjcf1cgi
Tags: 0.14-2
* comment out only function which uses va_list to work around build
  problems on armel
* Set Standards-Version to 3.8.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <qlineedit.h>
25
25
#include <qcheckbox.h>
26
26
#include <qlayout.h>
27
 
#include <q3grid.h>
28
 
#include <q3hbox.h>
29
 
#include <q3vbox.h>
30
27
#include <qcombobox.h>
31
28
#include <qpushbutton.h>
32
 
#include <q3listbox.h>
33
29
#include <qdom.h>
34
30
#include <qpointer.h>
35
31
#include <qapplication.h>
36
32
//Added by qt3to4:
37
33
#include <QHBoxLayout>
38
34
#include <QList>
39
 
#include <QDebug>
 
35
 
40
36
#include "common.h"
41
37
#include "iconwidget.h"
42
38
#include "psioptions.h"
43
 
 
44
 
static QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
45
 
{
46
 
        QDomElement tag = doc.createElement(name);
47
 
        QDomText text = doc.createTextNode(content);
48
 
        tag.appendChild(text);
49
 
 
50
 
        return tag;
51
 
}
52
 
 
53
 
static QDomElement textTag(QDomDocument &doc, const QString &name, bool content)
54
 
{
55
 
        QDomElement tag = doc.createElement(name);
56
 
        QDomText text = doc.createTextNode(content ? "true" : "false");
57
 
        tag.appendChild(text);
58
 
 
59
 
        return tag;
60
 
}
61
 
 
62
 
static QDomElement findSubTag(const QDomElement &e, const QString &name)
63
 
{
64
 
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
65
 
                QDomElement i = n.toElement();
66
 
                if(i.isNull())
67
 
                        continue;
68
 
                if(i.tagName() == name)
69
 
                        return i;
70
 
        }
71
 
 
72
 
        return QDomElement();
73
 
}
74
 
 
75
 
//----------------------------------------------------------------------------
76
 
// HostPortEdit
77
 
//----------------------------------------------------------------------------
78
 
class HostPortEdit::Private
79
 
{
80
 
public:
81
 
        Private() {}
82
 
 
83
 
        QLineEdit *le_host, *le_port;
84
 
};
85
 
 
86
 
HostPortEdit::HostPortEdit(QWidget *parent, const char *name)
87
 
:QWidget(parent, name)
88
 
{
89
 
        d = new Private;
90
 
 
91
 
        QLabel *l;
92
 
        QHBoxLayout *hb = new QHBoxLayout(this, 0, 4);
93
 
        l = new QLabel(tr("Host:"), this);
94
 
        hb->addWidget(l);
95
 
        d->le_host = new QLineEdit(this);
96
 
        d->le_host->setMinimumWidth(128);
97
 
        hb->addWidget(d->le_host);
98
 
        l = new QLabel(tr("Port:"), this);
99
 
        hb->addWidget(l);
100
 
        d->le_port = new QLineEdit(this);
101
 
        d->le_port->setFixedWidth(64);
102
 
        hb->addWidget(d->le_port);
103
 
        setPort(0);
104
 
}
105
 
 
106
 
HostPortEdit::~HostPortEdit()
107
 
{
108
 
        delete d;
109
 
}
110
 
 
111
 
QString HostPortEdit::host() const
112
 
{
113
 
        return d->le_host->text();
114
 
}
115
 
 
116
 
int HostPortEdit::port() const
117
 
{
118
 
        return d->le_port->text().toInt();
119
 
}
120
 
 
121
 
void HostPortEdit::setHost(const QString &s)
122
 
{
123
 
        d->le_host->setText(s);
124
 
}
125
 
 
126
 
void HostPortEdit::setPort(int x)
127
 
{
128
 
        d->le_port->setText(QString::number(x));
129
 
}
130
 
 
131
 
void HostPortEdit::fixTabbing(QWidget *a, QWidget *b)
132
 
{
133
 
        setTabOrder(a, d->le_host);
134
 
        setTabOrder(d->le_host, d->le_port);
135
 
        setTabOrder(d->le_port, b);
136
 
}
137
 
 
 
39
#include "xmpp_xmlcommon.h"
138
40
 
139
41
//----------------------------------------------------------------------------
140
42
// ProxySettings
166
68
        user = o->getOption(base + ".user").toString();
167
69
        pass = decodePassword(o->getOption(base + ".pass").toString(), PASSWORDKEY);
168
70
}
169
 
        
170
 
        
 
71
 
171
72
QDomElement ProxySettings::toXml(QDomDocument *doc) const
172
73
{
173
74
        QDomElement e = doc->createElement("proxySettings");
174
 
        e.appendChild(textTag(*doc, "host", host));
175
 
        e.appendChild(textTag(*doc, "port", QString::number(port)));
176
 
        e.appendChild(textTag(*doc, "url", url));
177
 
        e.appendChild(textTag(*doc, "useAuth", useAuth));
178
 
        e.appendChild(textTag(*doc, "user", user));
179
 
        e.appendChild(textTag(*doc, "pass", pass));
 
75
        e.appendChild(XMLHelper::textTag(*doc, "host", host));
 
76
        e.appendChild(XMLHelper::textTag(*doc, "port", QString::number(port)));
 
77
        e.appendChild(XMLHelper::textTag(*doc, "url", url));
 
78
        e.appendChild(XMLHelper::textTag(*doc, "useAuth", useAuth));
 
79
        e.appendChild(XMLHelper::textTag(*doc, "user", user));
 
80
        e.appendChild(XMLHelper::textTag(*doc, "pass", pass));
180
81
        return e;
181
82
}
182
83
 
183
84
bool ProxySettings::fromXml(const QDomElement &e)
184
85
{
185
 
        host = findSubTag(e, "host").text();
186
 
        port = findSubTag(e, "port").text().toInt();
187
 
        url = findSubTag(e, "url").text();
188
 
        useAuth = (findSubTag(e, "useAuth").text() == "true") ? true: false;
189
 
        user = findSubTag(e, "user").text();
190
 
        pass = findSubTag(e, "pass").text();
 
86
        host = findSubTag(e, "host", 0).text();
 
87
        port = findSubTag(e, "port", 0).text().toInt();
 
88
        url = findSubTag(e, "url", 0).text();
 
89
        useAuth = (findSubTag(e, "useAuth", 0).text() == "true") ? true: false;
 
90
        user = findSubTag(e, "user", 0).text();
 
91
        pass = findSubTag(e, "pass", 0).text();
191
92
        return true;
192
93
}
193
94
 
194
 
 
195
 
//----------------------------------------------------------------------------
196
 
// ProxyEdit
197
 
//----------------------------------------------------------------------------
198
 
class ProxyEdit::Private
199
 
{
200
 
public:
201
 
        Private() {}
202
 
 
203
 
        HostPortEdit *hp_host;
204
 
        QCheckBox *ck_auth;
205
 
        QLabel *lb_url;
206
 
        QLineEdit *le_url, *le_user, *le_pass;
207
 
        Q3Grid *gr_auth;
208
 
};
209
 
 
210
 
ProxyEdit::ProxyEdit(QWidget *parent, const char *name)
211
 
:Q3GroupBox(1, Qt::Horizontal, tr("Settings"), parent, name)
212
 
{
213
 
        d = new Private;
214
 
 
215
 
        //QVBoxLayout *vb = new QVBoxLayout(this);
216
 
        //QVBox *gb = new QVBox(this);
217
 
        //gb->setSpacing(4);
218
 
        //vb->addWidget(gb);
219
 
 
220
 
        QLabel *l;
221
 
 
222
 
        d->hp_host = new HostPortEdit(this);
223
 
 
224
 
        Q3HBox *hb = new Q3HBox(this);
225
 
        d->lb_url = new QLabel(tr("Polling URL:"), hb);
226
 
        d->le_url = new QLineEdit(hb);
227
 
 
228
 
        d->ck_auth = new QCheckBox(tr("Use authentication"), this);
229
 
        connect(d->ck_auth, SIGNAL(toggled(bool)), SLOT(ck_toggled(bool)));
230
 
 
231
 
        d->gr_auth = new Q3Grid(2, Qt::Horizontal, this);
232
 
        d->gr_auth->setSpacing(4);
233
 
        l = new QLabel(tr("Username:"), d->gr_auth);
234
 
        d->le_user = new QLineEdit(d->gr_auth);
235
 
        l = new QLabel(tr("Password:"), d->gr_auth);
236
 
        d->le_pass = new QLineEdit(d->gr_auth);
237
 
        d->le_pass->setEchoMode(QLineEdit::Password);
238
 
        d->gr_auth->setEnabled(false);
239
 
 
240
 
        d->hp_host->setWhatsThis(
241
 
                tr("Enter the hostname and port of your proxy server.") + "  " +
242
 
                tr("Consult your network administrator if necessary."));
243
 
        d->le_user->setWhatsThis(
244
 
                tr("Enter your proxy server login (username) "
245
 
                "or leave this field blank if the proxy server does not require it.") + "  " +
246
 
                tr("Consult your network administrator if necessary."));
247
 
        d->le_pass->setWhatsThis(
248
 
                tr("Enter your proxy server password "
249
 
                "or leave this field blank if the proxy server does not require it.") + "  " +
250
 
                tr("Consult your network administrator if necessary."));
251
 
}
252
 
 
253
 
ProxyEdit::~ProxyEdit()
254
 
{
255
 
        delete d;
256
 
}
257
 
 
258
 
void ProxyEdit::reset()
259
 
{
260
 
        d->hp_host->setHost("");
261
 
        d->hp_host->setPort(0);
262
 
        d->le_url->setText("");
263
 
        d->ck_auth->setChecked(false);
264
 
        d->le_user->setText("");
265
 
        d->le_pass->setText("");
266
 
}
267
 
 
268
 
void ProxyEdit::setType(const QString &s)
269
 
{
270
 
        if(s == "poll") {
271
 
                d->lb_url->setEnabled(true);
272
 
                d->le_url->setEnabled(true);
273
 
        }
274
 
        else {
275
 
                d->lb_url->setEnabled(false);
276
 
                d->le_url->setEnabled(false);
277
 
        }
278
 
}
279
 
 
280
 
ProxySettings ProxyEdit::proxySettings() const
281
 
{
282
 
        ProxySettings s;
283
 
        s.host = d->hp_host->host();
284
 
        s.port = d->hp_host->port();
285
 
        if(d->le_url->isEnabled())
286
 
                s.url = d->le_url->text();
287
 
        s.useAuth = d->ck_auth->isChecked();
288
 
        s.user = d->le_user->text();
289
 
        s.pass = d->le_pass->text();
290
 
        return s;
291
 
}
292
 
 
293
 
void ProxyEdit::setProxySettings(const ProxySettings &s)
294
 
{
295
 
        d->hp_host->setHost(s.host);
296
 
        d->hp_host->setPort(s.port);
297
 
        d->le_url->setText(s.url);
298
 
        d->ck_auth->setChecked(s.useAuth);
299
 
        d->le_user->setText(s.user);
300
 
        d->le_pass->setText(s.pass);
301
 
}
302
 
 
303
 
void ProxyEdit::ck_toggled(bool b)
304
 
{
305
 
        d->gr_auth->setEnabled(b);
306
 
}
307
 
 
308
 
void ProxyEdit::fixTabbing(QWidget *a, QWidget *b)
309
 
{
310
 
        d->hp_host->fixTabbing(a, d->le_url);
311
 
        setTabOrder(d->le_url, d->ck_auth);
312
 
        setTabOrder(d->ck_auth, d->le_user);
313
 
        setTabOrder(d->le_user, d->le_pass);
314
 
        setTabOrder(d->le_pass, b);
315
 
}
316
 
 
317
 
 
318
95
//----------------------------------------------------------------------------
319
96
// ProxyDlg
320
97
//----------------------------------------------------------------------------
321
 
class ProxyDlg::Private
 
98
class ProxyDlg::Private : public QObject
322
99
{
 
100
        Q_OBJECT
323
101
public:
324
 
        Private() {}
 
102
        enum Data {
 
103
                HostRole = Qt::UserRole + 1,
 
104
                PortRole = Qt::UserRole + 2,
 
105
                UrlRole  = Qt::UserRole + 3,
 
106
                AuthRole = Qt::UserRole + 4,
 
107
                UserRole = Qt::UserRole + 5,
 
108
                PassRole = Qt::UserRole + 6,
 
109
                IdRole   = Qt::UserRole + 7,
 
110
                TypeRole = Qt::UserRole + 8
 
111
        };
325
112
 
326
 
        ProxyEdit *pe_settings;
 
113
        ProxyDlg* q;
327
114
        ProxyItemList list;
328
 
        int last;
 
115
 
 
116
        Private(ProxyDlg* dialog)
 
117
                : QObject(dialog)
 
118
                , q(dialog)
 
119
        {
 
120
                Q_ASSERT(q);
 
121
 
 
122
                connect(q->ui_.lbx_proxy, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(currentItemChanged(QListWidgetItem*, QListWidgetItem*)));
 
123
 
 
124
                connect(q->ui_.pb_new, SIGNAL(clicked()), SLOT(addProxy()));
 
125
                connect(q->ui_.pb_remove, SIGNAL(clicked()), SLOT(removeProxy()));
 
126
 
 
127
                connect(q->ui_.buttonBox->button(QDialogButtonBox::Save),   SIGNAL(clicked()), this, SLOT(accept()));
 
128
                connect(q->ui_.buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), q,    SLOT(reject()));
 
129
                q->ui_.buttonBox->button(QDialogButtonBox::Save)->setDefault(true);
 
130
 
 
131
                connect(q->ui_.lbx_proxy->itemDelegate(), SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), SLOT(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)));
 
132
 
 
133
                connect(q->ui_.le_host, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
 
134
                connect(q->ui_.le_port, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
 
135
                connect(q->ui_.le_url, SIGNAL(textEdited(const QString&)),  SLOT(updateCurrentItem()));
 
136
                connect(q->ui_.le_user, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
 
137
                connect(q->ui_.le_pass, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
 
138
                connect(q->ui_.gr_auth, SIGNAL(toggled(bool)),              SLOT(updateCurrentItem()));
 
139
                connect(q->ui_.cb_type, SIGNAL(activated (int)),            SLOT(updateCurrentItem()));
 
140
        }
 
141
 
 
142
public slots:
 
143
        void loadProxies(const QString& currentProxy)
 
144
        {
 
145
                q->ui_.lbx_proxy->clear();
 
146
 
 
147
                QListWidgetItem* firstItem = 0;
 
148
                QListWidgetItem* currentItem = 0;
 
149
                foreach(ProxyItem i, list) {
 
150
                        QListWidgetItem* item = new QListWidgetItem(i.name);
 
151
                        addItem(item);
 
152
 
 
153
                        item->setData(IdRole,   QVariant(i.id));
 
154
                        item->setData(TypeRole, QVariant(i.type));
 
155
                        item->setData(HostRole, QVariant(i.settings.host));
 
156
                        item->setData(PortRole, QVariant(i.settings.port));
 
157
                        item->setData(UserRole, QVariant(i.settings.user));
 
158
                        item->setData(PassRole, QVariant(i.settings.pass));
 
159
                        item->setData(AuthRole, QVariant(i.settings.useAuth));
 
160
                        item->setData(UrlRole,  QVariant(i.settings.url));
 
161
 
 
162
                        if (!firstItem) {
 
163
                                firstItem = item;
 
164
                        }
 
165
 
 
166
                        if (i.id == currentProxy) {
 
167
                                currentItem = item;
 
168
                        }
 
169
                }
 
170
 
 
171
                if (currentItem) {
 
172
                        firstItem = currentItem;
 
173
                }
 
174
 
 
175
                if (firstItem) {
 
176
                        q->ui_.lbx_proxy->setCurrentItem(firstItem);
 
177
                        q->ui_.lbx_proxy->scrollToItem(firstItem);
 
178
                }
 
179
        }
 
180
 
 
181
        void saveProxies()
 
182
        {
 
183
                list.clear();
 
184
                for (int i = 0; i < q->ui_.lbx_proxy->count(); ++i) {
 
185
                        QListWidgetItem* item = q->ui_.lbx_proxy->item(i);
 
186
 
 
187
                        ProxyItem pi;
 
188
                        pi.name             = item->data(Qt::DisplayRole).toString();
 
189
                        pi.id               = item->data(IdRole).toString();
 
190
                        pi.type             = item->data(TypeRole).toString();
 
191
                        pi.settings.host    = item->data(HostRole).toString();
 
192
                        pi.settings.port    = item->data(PortRole).toInt();
 
193
                        pi.settings.user    = item->data(UserRole).toString();
 
194
                        pi.settings.pass    = item->data(PassRole).toString();
 
195
                        pi.settings.useAuth = item->data(AuthRole).toBool();
 
196
                        pi.settings.url     = item->data(UrlRole).toString();
 
197
                        list << pi;
 
198
                }
 
199
        }
 
200
 
 
201
        void accept()
 
202
        {
 
203
                saveProxies();
 
204
                emit q->applyList(list, q->ui_.lbx_proxy->currentRow());
 
205
                q->accept();
 
206
        }
 
207
 
 
208
        void currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous)
 
209
        {
 
210
                Q_UNUSED(previous);
 
211
                q->ui_.pb_remove->setEnabled(current);
 
212
 
 
213
                QList<QWidget*> editors = QList<QWidget*>()
 
214
                        << q->ui_.cb_type
 
215
                        << q->ui_.le_host
 
216
                        << q->ui_.le_port
 
217
                        << q->ui_.le_user
 
218
                        << q->ui_.le_pass
 
219
                        << q->ui_.le_url
 
220
                        << q->ui_.gr_auth;
 
221
                foreach(QWidget* w, editors) {
 
222
                        w->blockSignals(true);
 
223
                        w->setEnabled(current);
 
224
                        if (!current) {
 
225
                                if (dynamic_cast<QLineEdit*>(w))
 
226
                                        dynamic_cast<QLineEdit*>(w)->setText(QString());
 
227
                        }
 
228
                }
 
229
 
 
230
                if (current) {
 
231
                        int type = q->ui_.cb_type->findData(current->data(TypeRole).toString());
 
232
                        q->ui_.cb_type->setCurrentIndex(type == -1 ? 0 : type);
 
233
                        q->ui_.le_host->setText(current->data(HostRole).toString());
 
234
                        q->ui_.le_port->setText(current->data(PortRole).toString());
 
235
                        q->ui_.le_user->setText(current->data(UserRole).toString());
 
236
                        q->ui_.le_pass->setText(current->data(PassRole).toString());
 
237
                        q->ui_.le_url->setText(current->data(UrlRole).toString());
 
238
                        q->ui_.gr_auth->setChecked(true); // make sure gr_auth disables its children if AuthRole is false
 
239
                        q->ui_.gr_auth->setChecked(current->data(AuthRole).toBool());
 
240
                }
 
241
 
 
242
                foreach(QWidget* w, editors) {
 
243
                        w->blockSignals(false);
 
244
                }
 
245
 
 
246
                updateCurrentItem();
 
247
        }
 
248
 
 
249
        void updateCurrentItem()
 
250
        {
 
251
                QListWidgetItem* item = q->ui_.lbx_proxy->currentItem();
 
252
                if (!item)
 
253
                        return;
 
254
 
 
255
                QString type = q->ui_.cb_type->itemData(q->ui_.cb_type->currentIndex()).toString();
 
256
                item->setData(TypeRole, type);
 
257
                item->setData(HostRole, q->ui_.le_host->text());
 
258
                item->setData(PortRole, q->ui_.le_port->text());
 
259
                item->setData(UserRole, q->ui_.le_user->text());
 
260
                item->setData(PassRole, q->ui_.le_pass->text());
 
261
                item->setData(UrlRole, q->ui_.le_url->text());
 
262
                item->setData(AuthRole, q->ui_.gr_auth->isChecked());
 
263
 
 
264
                q->ui_.le_url->setEnabled(type == "poll");
 
265
        }
 
266
 
 
267
        void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
 
268
        {
 
269
                Q_UNUSED(editor);
 
270
 
 
271
                if (hint == QAbstractItemDelegate::SubmitModelCache) {
 
272
                        q->ui_.cb_type->setFocus();
 
273
                }
 
274
        }
 
275
 
 
276
        void addItem(QListWidgetItem* item)
 
277
        {
 
278
                Q_ASSERT(item);
 
279
                item->setFlags(item->flags() | Qt::ItemIsEditable);
 
280
                q->ui_.lbx_proxy->addItem(item);
 
281
        }
 
282
 
 
283
        void addProxy()
 
284
        {
 
285
                QListWidgetItem* item = new QListWidgetItem(tr("Unnamed"));
 
286
                addItem(item);
 
287
                q->ui_.lbx_proxy->reset(); // ensure that open editors won't get in our way
 
288
                q->ui_.lbx_proxy->setCurrentItem(item);
 
289
                q->ui_.lbx_proxy->editItem(item);
 
290
        }
 
291
 
 
292
        void removeProxy()
 
293
        {
 
294
                QListWidgetItem* item = q->ui_.lbx_proxy->takeItem(q->ui_.lbx_proxy->currentRow());
 
295
                delete item;
 
296
        }
329
297
};
330
298
 
331
 
ProxyDlg::ProxyDlg(const ProxyItemList &list, const QStringList &methods, const QString &def, QWidget *parent)
 
299
ProxyDlg::ProxyDlg(const ProxyItemList &list, const QString &def, QWidget *parent)
332
300
        : QDialog(parent)
333
301
{
 
302
        ui_.setupUi(this);
 
303
 
334
304
        setAttribute(Qt::WA_DeleteOnClose);
335
 
        d = new Private;
336
 
        setupUi(this);
337
 
        setWindowTitle(CAP(caption()));
338
 
        setModal(QApplication::activeModalWidget() ? true: false);
339
 
 
 
305
        d = new Private(this);
340
306
        d->list = list;
341
 
        d->last = -1;
342
 
        d->pe_settings = new ProxyEdit(gb_prop);
343
 
        replaceWidget(lb_proxyedit, d->pe_settings);
344
 
        d->pe_settings->fixTabbing(cb_type, pb_close);
345
 
 
346
 
        hookEdit();
347
 
 
348
 
        connect(pb_new, SIGNAL(clicked()), SLOT(proxy_new()));
349
 
        connect(pb_remove, SIGNAL(clicked()), SLOT(proxy_remove()));
350
 
        connect(pb_save, SIGNAL(clicked()), SLOT(doSave()));
351
 
        connect(pb_close, SIGNAL(clicked()), SLOT(reject()));
352
 
        gb_prop->setEnabled(false);
353
 
        pb_remove->setEnabled(false);
354
 
        pb_save->setDefault(true);
355
 
 
356
 
        cb_type->insertStringList(methods);
357
 
        connect(cb_type, SIGNAL(activated(int)), SLOT(cb_activated(int)));
358
 
 
359
 
        int defIdx = -1;
360
 
        int i = 0;
361
 
        for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it) {
362
 
                lbx_proxy->insertItem((*it).name);
363
 
                if ((*it).id == def) defIdx= i;
364
 
                ++i;
365
 
        }
366
 
        if(!list.isEmpty()) {
367
 
                if(defIdx < 0)
368
 
                        defIdx = 0;
369
 
                lbx_proxy->setCurrentItem(defIdx);
370
 
                selectCurrent();
371
 
        }
372
 
 
373
 
        cb_type->setWhatsThis(
 
307
 
 
308
        setWindowTitle(CAP(windowTitle()));
 
309
        setModal(true);
 
310
 
 
311
        ui_.cb_type->addItem("HTTP \"Connect\"", "http");
 
312
        ui_.cb_type->addItem("SOCKS Version 5",  "socks");
 
313
        ui_.cb_type->addItem("HTTP Polling",     "poll");
 
314
 
 
315
        ui_.le_host->setWhatsThis(
 
316
                tr("Enter the hostname and port of your proxy server.") + "  " +
 
317
                tr("Consult your network administrator if necessary."));
 
318
        ui_.le_port->setWhatsThis(ui_.le_host->whatsThis());
 
319
        ui_.le_user->setWhatsThis(
 
320
                tr("Enter your proxy server login (username) "
 
321
                "or leave this field blank if the proxy server does not require it.") + "  " +
 
322
                tr("Consult your network administrator if necessary."));
 
323
        ui_.le_pass->setWhatsThis(
 
324
                tr("Enter your proxy server password "
 
325
                "or leave this field blank if the proxy server does not require it.") + "  " +
 
326
                tr("Consult your network administrator if necessary."));
 
327
        ui_.cb_type->setWhatsThis(
374
328
                tr("If you require a proxy server to connect, select the type of proxy here.") + "  " +
375
329
                tr("Consult your network administrator if necessary."));
376
 
        // TODO: whatsthis for name
 
330
 
 
331
        d->loadProxies(def);
377
332
}
378
333
 
379
334
ProxyDlg::~ProxyDlg()
381
336
        delete d;
382
337
}
383
338
 
384
 
void ProxyDlg::proxy_new()
385
 
{
386
 
        ProxyItem s;
387
 
        s.id = ""; // id of "" means 'new'
388
 
        s.name = getUniqueName();
389
 
        d->list += s;
390
 
 
391
 
        lbx_proxy->insertItem(s.name);
392
 
        lbx_proxy->setCurrentItem(lbx_proxy->count()-1);
393
 
        selectCurrent();
394
 
}
395
 
 
396
 
void ProxyDlg::proxy_remove()
397
 
{
398
 
        int x = lbx_proxy->currentItem();
399
 
        if(x != -1) {
400
 
                ProxyItemList::Iterator it = d->list.begin();
401
 
                for(int n = 0; n < x; ++n)
402
 
                        ++it;
403
 
                d->list.remove(it);
404
 
 
405
 
                d->last = -1;
406
 
                lbx_proxy->removeItem(x);
407
 
                selectCurrent();
408
 
        }
409
 
}
410
 
 
411
 
void ProxyDlg::cb_activated(int x)
412
 
{
413
 
        if(x == 0)
414
 
                d->pe_settings->setType("http");
415
 
        else if(x == 1)
416
 
                d->pe_settings->setType("socks");
417
 
        else if(x == 2)
418
 
                d->pe_settings->setType("poll");
419
 
}
420
 
 
421
 
void ProxyDlg::selectCurrent()
422
 
{
423
 
        int x = lbx_proxy->currentItem();
424
 
        if(x != -1)
425
 
                lbx_proxy->setSelected(x, true);
426
 
}
427
 
 
428
 
QString ProxyDlg::getUniqueName() const
429
 
{
430
 
        QString str;
431
 
        int x = 0;
432
 
        bool found;
433
 
        do {
434
 
                str = QString("Proxy %1").arg(++x);
435
 
                found = false;
436
 
                for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it) {
437
 
                        if(str == (*it).name) {
438
 
                                found = true;
439
 
                                break;
440
 
                        }
441
 
                }
442
 
        } while(found);
443
 
        return str;
444
 
}
445
 
 
446
 
void ProxyDlg::saveIntoItem(int x)
447
 
{
448
 
        ProxyItem &s = d->list[x];
449
 
        s.name = le_name->text();
450
 
        int i = cb_type->currentItem();
451
 
        s.type = "http";
452
 
        if(i == 0)
453
 
                s.type = "http";
454
 
        else if(i == 1)
455
 
                s.type = "socks";
456
 
        else if(i == 2)
457
 
                s.type = "poll";
458
 
        s.settings = d->pe_settings->proxySettings();
459
 
}
460
 
 
461
 
void ProxyDlg::qlbx_highlighted(int x)
462
 
{
463
 
        // if we are moving off of another item, save its content
464
 
        if(d->last != -1)
465
 
                saveIntoItem(d->last);
466
 
        d->last = x;
467
 
 
468
 
        // display the new item's content
469
 
        if(x == -1) {
470
 
                le_name->setText("");
471
 
                cb_type->setCurrentItem(0);
472
 
                d->pe_settings->reset();
473
 
                gb_prop->setEnabled(false);
474
 
                pb_remove->setEnabled(false);
475
 
        }
476
 
        else {
477
 
                ProxyItem &s = d->list[x];
478
 
                le_name->setText(s.name);
479
 
                int i = 0;
480
 
                if(s.type == "http")
481
 
                        i = 0;
482
 
                else if(s.type == "socks")
483
 
                        i = 1;
484
 
                else if(s.type == "poll")
485
 
                        i = 2;
486
 
                cb_type->setCurrentItem(i);
487
 
                d->pe_settings->setProxySettings(s.settings);
488
 
                cb_activated(i);
489
 
                gb_prop->setEnabled(true);
490
 
                pb_remove->setEnabled(true);
491
 
        }
492
 
}
493
 
 
494
 
void ProxyDlg::qle_textChanged(const QString &s)
495
 
{
496
 
        int x = lbx_proxy->currentItem();
497
 
        if(x != -1) {
498
 
                unhookEdit();
499
 
                lbx_proxy->changeItem(s, x);
500
 
                hookEdit();
501
 
        }
502
 
}
503
 
 
504
 
// hookEdit / unhookEdit - disconnect some signals to prevent recursion
505
 
void ProxyDlg::hookEdit()
506
 
{
507
 
        connect(lbx_proxy, SIGNAL(highlighted(int)), this, SLOT(qlbx_highlighted(int)));
508
 
        connect(le_name, SIGNAL(textChanged(const QString &)), this, SLOT(qle_textChanged(const QString &)));
509
 
}
510
 
 
511
 
void ProxyDlg::unhookEdit()
512
 
{
513
 
        disconnect(lbx_proxy, SIGNAL(highlighted(int)), this, SLOT(qlbx_highlighted(int)));
514
 
        disconnect(le_name, SIGNAL(textChanged(const QString &)), this, SLOT(qle_textChanged(const QString &)));
515
 
}
516
 
 
517
 
void ProxyDlg::doSave()
518
 
{
519
 
        int x = lbx_proxy->currentItem();
520
 
        if(x != -1)
521
 
                saveIntoItem(x);
522
 
        applyList(d->list, x);
523
 
        accept();
524
 
}
525
 
 
526
 
 
527
339
//----------------------------------------------------------------------------
528
340
// ProxyChooser
529
341
//----------------------------------------------------------------------------
537
349
        ProxyManager *m;
538
350
};
539
351
 
540
 
ProxyChooser::ProxyChooser(ProxyManager *m, QWidget *parent, const char *name)
541
 
:QWidget(parent, name)
 
352
ProxyChooser::ProxyChooser(ProxyManager* m, QWidget* parent)
 
353
        : QWidget(parent)
542
354
{
543
355
        d = new Private;
544
356
        d->m = m;
545
357
        connect(m, SIGNAL(settingsChanged()), SLOT(pm_settingsChanged()));
546
 
        QHBoxLayout *hb = new QHBoxLayout(this, 0, 4);
 
358
        QHBoxLayout *hb = new QHBoxLayout(this);
 
359
        hb->setMargin(0);
 
360
        hb->setSpacing(4);
547
361
        d->cb_proxy = new QComboBox(this);
548
362
        QSizePolicy sp = d->cb_proxy->sizePolicy();
549
 
        d->cb_proxy->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, sp.verData()) );
 
363
        d->cb_proxy->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, d->cb_proxy->sizePolicy().verticalPolicy()) );
550
364
        hb->addWidget(d->cb_proxy);
551
365
        d->pb_edit = new QPushButton(tr("Edit..."), this);
552
366
        connect(d->pb_edit, SIGNAL(clicked()), SLOT(doOpen()));
562
376
 
563
377
QString ProxyChooser::currentItem() const
564
378
{
565
 
        return d->cb_proxy->itemData(d->cb_proxy->currentItem()).toString();
 
379
        return d->cb_proxy->itemData(d->cb_proxy->currentIndex()).toString();
566
380
}
567
381
 
568
382
void ProxyChooser::setCurrentItem(const QString &id)
569
383
{
570
 
        d->cb_proxy->setCurrentItem(d->cb_proxy->findData(id));
 
384
        int index = d->cb_proxy->findData(id);
 
385
        d->cb_proxy->setCurrentIndex(index == -1 ? 0 : index);
571
386
}
572
387
 
573
388
void ProxyChooser::pm_settingsChangedApply()
577
392
        buildComboBox();
578
393
        prev = d->m->lastEdited();
579
394
        int x = d->cb_proxy->findData(prev);
580
 
        if(x == -1) {
581
 
                d->cb_proxy->setCurrentItem(0);
582
 
        } else {
583
 
                d->cb_proxy->setCurrentItem(x);
584
 
        }
 
395
        d->cb_proxy->setCurrentIndex(x == -1 ? 0 : x);
585
396
}
586
397
 
587
 
 
588
398
void ProxyChooser::pm_settingsChanged()
589
399
{
590
400
        QString prev = currentItem();
591
401
        buildComboBox();
592
402
        int x = d->cb_proxy->findData(prev);
593
403
        if(x == -1) {
594
 
                d->cb_proxy->setCurrentItem(0);
 
404
                d->cb_proxy->setCurrentIndex(0);
595
405
        } else {
596
 
                d->cb_proxy->setCurrentItem(x);
 
406
                d->cb_proxy->setCurrentIndex(x);
597
407
        }
598
408
}
599
409
 
609
419
 
610
420
void ProxyChooser::doOpen()
611
421
{
612
 
        QString x = d->cb_proxy->itemData(d->cb_proxy->currentItem()).toString();
 
422
        QString x = d->cb_proxy->itemData(d->cb_proxy->currentIndex()).toString();
613
423
        connect(d->m, SIGNAL(settingsChanged()), SLOT(pm_settingsChangedApply()));
614
424
        d->m->openDialog(x);
615
425
}
616
426
 
617
 
void ProxyChooser::fixTabbing(QWidget *a, QWidget *b)
618
 
{
619
 
        setTabOrder(a, d->cb_proxy);
620
 
        setTabOrder(d->cb_proxy, d->pb_edit);
621
 
        setTabOrder(d->pb_edit, b);
622
 
}
623
 
 
624
 
 
625
427
//----------------------------------------------------------------------------
626
428
// ProxyManager
627
429
//----------------------------------------------------------------------------
694
496
        }
695
497
}
696
498
 
697
 
QStringList ProxyManager::methodList() const
698
 
{
699
 
        QStringList list;
700
 
        list += "HTTP \"Connect\"";
701
 
        list += "SOCKS Version 5";
702
 
        list += "HTTP Polling";
703
 
        return list;
704
 
}
705
 
 
706
499
void ProxyManager::openDialog(QString def)
707
500
{
708
501
        if(d->pd)
709
502
                bringToFront(d->pd);
710
503
        else {
711
 
                d->pd = new ProxyDlg(itemList(), methodList(), def, 0);
 
504
                d->pd = new ProxyDlg(itemList(), def, 0);
712
505
                connect(d->pd, SIGNAL(applyList(const ProxyItemList &, int)), SLOT(pd_applyList(const ProxyItemList &, int)));
713
506
                d->pd->show();
714
507
        }
747
540
        
748
541
        settingsChanged();
749
542
}
 
543
 
 
544
#include "proxy.moc"