~ubuntu-branches/ubuntu/oneiric/knetworkmanager/oneiric

« back to all changes in this revision

Viewing changes to knetworkmanager/src/knetworkmanager-notify.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-07-14 14:05:44 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080714140544-yjhxgrdwartk3kx7
Tags: 1:0.7svn830754-0ubuntu1
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * knetworkmanager-notify.cpp - A NetworkManager frontend for KDE
4
 
 *
5
 
 * Copyright (C) 2005, 2006 Novell, Inc.
6
 
 *
7
 
 * Author: Timo Hoenig <thoenig@suse.de>, <thoenig@nouse.net>
8
 
 *         Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
9
 
 *
10
 
 * This program is free software; you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation; either version 2 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program; if not, write to the Free Software
22
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 
 *
24
 
 **************************************************************************/
25
 
 
26
 
#include <qpushbutton.h>
27
 
#include <kprogress.h>
28
 
#include <qtooltip.h>
29
 
#include <klocale.h>
30
 
#include <kpushbutton.h>
31
 
#include <kglobalsettings.h>
32
 
#include <qpointarray.h>
33
 
#include <qpainter.h>
34
 
#include <qbitmap.h>
35
 
#include <kiconloader.h>
36
 
#include <qpoint.h>
37
 
 
38
 
#include "knetworkmanager-notify.h"
39
 
#include "activation.h"
40
 
#include "errormsg.h"
41
 
 
42
 
class DeviceStore;
43
 
 
44
 
/* ActivationStageNotifyVPN */
45
 
 
46
 
void
47
 
ActivationStageNotifyVPN::updateActivationStage ()
48
 
{
49
 
        State*  state = _ctx->getState ();
50
 
        int     progress;
51
 
        QString stage;
52
 
        /* close dialog if NetworkManager is not running*/
53
 
        if (state->isNetworkManagerRunning () == false)
54
 
                goto done;
55
 
 
56
 
        /* if we have an unclosed error let it unchanged */
57
 
        if (_errorWidget != NULL)
58
 
                goto stage_pending;
59
 
 
60
 
        switch (progress = _vpnConnection->getActivationStage ()) {
61
 
                case NM_VPN_ACT_STAGE_PREPARE:
62
 
                {
63
 
                        stage = i18n("Preparing...");
64
 
                        break;
65
 
                }
66
 
                case NM_VPN_ACT_STAGE_CONNECT:
67
 
                {
68
 
                        stage = i18n("Connection in progress...");
69
 
                        break;
70
 
                }
71
 
                case NM_VPN_ACT_STAGE_IP_CONFIG_GET:
72
 
                {
73
 
                        stage = i18n("Getting IP configuration...");
74
 
                        break;
75
 
                }
76
 
                case NM_VPN_ACT_STAGE_ACTIVATED:
77
 
                {
78
 
                        stage = i18n("Successfully connected.");
79
 
                        break;
80
 
                }
81
 
                case NM_VPN_ACT_STAGE_DISCONNECTED:
82
 
                case NM_VPN_ACT_STAGE_FAILED:
83
 
                        stage = i18n("Failed.");
84
 
                        break;
85
 
        
86
 
                case NM_VPN_ACT_STAGE_CANCELED:
87
 
                default:
88
 
                {
89
 
                        goto done;
90
 
                }
91
 
        }
92
 
 
93
 
        if (progress == NM_VPN_ACT_STAGE_FAILED || progress == NM_VPN_ACT_STAGE_DISCONNECTED)
94
 
        {
95
 
                QString temp("");
96
 
                connectionFailure(temp, temp);
97
 
                goto stage_pending;
98
 
        }
99
 
 
100
 
        _activationWidget->pbarActivationStage->setProgress (progress);
101
 
        _activationWidget->lblActivationStage->setText (i18n("Activation stage: %1.").arg(stage));
102
 
 
103
 
        this->resize(minimumSizeHint());
104
 
 
105
 
        if (progress < NM_VPN_ACT_STAGE_ACTIVATED)
106
 
                goto stage_pending;
107
 
 
108
 
        if (progress == NM_VPN_ACT_STAGE_ACTIVATED) {
109
 
                // close us in 1 sec
110
 
                QTimer* timer = new QTimer(this);
111
 
                connect(timer, SIGNAL(timeout()), this, SLOT(done()));
112
 
                timer->start(1000, true);
113
 
                goto stage_pending;
114
 
        }
115
 
 
116
 
done:
117
 
        close(TRUE);
118
 
 
119
 
stage_pending:
120
 
 
121
 
        return;
122
 
}
123
 
 
124
 
void
125
 
ActivationStageNotifyVPN::connectionFailure(QString& member, QString& err_msg)
126
 
{
127
 
        QString title;
128
 
        QString desc;
129
 
        QString details = err_msg;
130
 
        
131
 
        if (member == NM_DBUS_VPN_SIGNAL_LOGIN_FAILED) {
132
 
                title = i18n("VPN Login Failure");
133
 
                desc = i18n("Could not start the VPN connection '%1' due to a login failure.").arg(_vpnConnection->getName());
134
 
        }
135
 
        else if (member == NM_DBUS_VPN_SIGNAL_LAUNCH_FAILED) {
136
 
                title = i18n("VPN Start Failure");
137
 
                desc = i18n("Could not start the VPN connection '%1' due to a failure launching the VPN program.").arg(_vpnConnection->getName());
138
 
        }
139
 
        else if (member == NM_DBUS_VPN_SIGNAL_CONNECT_FAILED) {
140
 
                title = i18n("VPN Connect Failure");
141
 
                desc = i18n("Could not start the VPN connection '%1' due to a connection error.").arg(_vpnConnection->getName());
142
 
        }
143
 
        else if (member == NM_DBUS_VPN_SIGNAL_VPN_CONFIG_BAD) {
144
 
                title = i18n("VPN Configuration Error");
145
 
                desc = i18n("The VPN connection '%1' was not correctly configured.").arg(_vpnConnection->getName());
146
 
        }
147
 
        else if (member == NM_DBUS_VPN_SIGNAL_IP_CONFIG_BAD) {
148
 
                title = i18n("VPN Connect Failure");
149
 
                desc = i18n("Could not start the VPN connection '%1' because the VPN server did not return an adequate network configuration.").arg(_vpnConnection->getName());
150
 
        }
151
 
        else {
152
 
                title = i18n("VPN Failure");
153
 
                desc = i18n("Could not start the VPN connection %1.").arg(_vpnConnection->getName());
154
 
        }
155
 
 
156
 
        showError(title, desc + "\n" + err_msg);
157
 
}
158
 
 
159
 
ActivationStageNotifyVPN::ActivationStageNotifyVPN (QWidget* parent, const char* name, bool /*modal*/, WFlags fl, KNetworkManager* ctx, VPNConnection* vpnConnection)
160
 
                : ActivationStageNotify (parent, name, fl, ctx)
161
 
{
162
 
        QString info;
163
 
 
164
 
        if (!vpnConnection)
165
 
                return;
166
 
 
167
 
        _vpnConnection = vpnConnection;
168
 
 
169
 
        QWidget* mainWid = this;
170
 
        _activationWidget = new ActivationWidget (mainWid, "activationwidget");
171
 
        _activationWidget->lblActivationCaption->setText (i18n ("Activating VPN Connection"));
172
 
        _activationWidget->lblPixmap->setPixmap(SmallIcon("encrypted", QIconSet::Automatic));
173
 
        _activationWidget->lblActivation->setText (_vpnConnection->getName ());
174
 
        _activationWidget->pbarActivationStage->setTotalSteps (5);
175
 
        _activationWidget->lblActivationStage->setText (QString::null);
176
 
 
177
 
        connect (parent, SIGNAL (destroyActivationStage ()),
178
 
                 this,   SLOT   (destroyActivationStage ()));
179
 
 
180
 
        connect (vpnConnection, SIGNAL (activationStateChanged ()),
181
 
                 this,          SLOT   (updateActivationStage ()));
182
 
 
183
 
        connect (vpnConnection, SIGNAL (connectionFailure(QString&, QString&)), this, SLOT(connectionFailure(QString&, QString&)));
184
 
        this->resize(minimumSizeHint());
185
 
}
186
 
 
187
 
ActivationStageNotifyVPN::~ActivationStageNotifyVPN ()
188
 
{
189
 
 
190
 
}
191
 
 
192
 
 
193
 
/* ActivationStageNotifyNetwork */
194
 
 
195
 
void
196
 
ActivationStageNotifyNetwork::updateActivationStage ()
197
 
{
198
 
        State*  state = _ctx->getState ();
199
 
        int     progress;
200
 
        QString stage;
201
 
 
202
 
        /* close dialog if NetworkManager is not running*/
203
 
        if (state->isNetworkManagerRunning () == false)
204
 
                goto done;
205
 
 
206
 
        switch (progress = _dev->getActivationStage ()) {
207
 
                case NM_ACT_STAGE_DEVICE_PREPARE:
208
 
                {
209
 
                        stage = i18n("Preparing device");
210
 
                        break;
211
 
                }
212
 
                case NM_ACT_STAGE_DEVICE_CONFIG:
213
 
                {
214
 
                        stage = i18n("Configuring device");
215
 
                        break;
216
 
                }
217
 
                case NM_ACT_STAGE_NEED_USER_KEY:
218
 
                {
219
 
                        stage = i18n("Waiting for passphrase from user");
220
 
                        break;
221
 
                }
222
 
                case NM_ACT_STAGE_IP_CONFIG_START:
223
 
                {
224
 
                        stage = i18n("IP configuration started");
225
 
                        break;
226
 
                }
227
 
                case NM_ACT_STAGE_IP_CONFIG_GET:
228
 
                {
229
 
                        stage = i18n("IP configuration requested");
230
 
                        break;
231
 
                }
232
 
                case NM_ACT_STAGE_IP_CONFIG_COMMIT:
233
 
                {
234
 
                        stage = i18n("Commit IP configuration");
235
 
                        break;
236
 
                }
237
 
                case NM_ACT_STAGE_ACTIVATED:
238
 
                {
239
 
                        stage = i18n("Device activated");
240
 
                        break;
241
 
                }
242
 
                case NM_ACT_STAGE_FAILED:
243
 
                {
244
 
                        stage = i18n("Device activation failed");
245
 
                        break;
246
 
                }
247
 
                case NM_ACT_STAGE_CANCELLED:
248
 
                {
249
 
                        stage = i18n("Device activation canceled");
250
 
                        break;
251
 
                }
252
 
                case NM_ACT_STAGE_UNKNOWN:
253
 
                default:
254
 
                {
255
 
                        stage = i18n("Unknown");
256
 
                }
257
 
        }
258
 
 
259
 
        _activationWidget->pbarActivationStage->setProgress (progress);
260
 
        _activationWidget->lblActivationStage->setText (i18n("Activation stage: %1.").arg(stage));
261
 
 
262
 
        if (progress == NM_ACT_STAGE_NEED_USER_KEY)
263
 
                goto done;
264
 
 
265
 
        if (progress < NM_ACT_STAGE_ACTIVATED)
266
 
                goto stage_pending;
267
 
        
268
 
        if (progress == NM_ACT_STAGE_ACTIVATED)
269
 
        {
270
 
                // close us in 1 second
271
 
                QTimer* timer = new QTimer(this);
272
 
                connect(timer, SIGNAL(timeout()), this, SLOT(close()));
273
 
                timer->start(1000, true);
274
 
                goto stage_pending;
275
 
        }
276
 
 
277
 
        if (progress == NM_ACT_STAGE_FAILED)
278
 
        {
279
 
                connectionFailure();
280
 
                goto stage_pending;
281
 
        }
282
 
 
283
 
        if (progress == (int) NM_ACT_STAGE_CANCELLED) {
284
 
//               TODO some warning 
285
 
//              printf ("notify::NM_ACT_STAGE_CANCELLED\n");
286
 
        }
287
 
 
288
 
done:
289
 
        close(TRUE);
290
 
 
291
 
stage_pending:
292
 
 
293
 
        return;
294
 
}
295
 
 
296
 
void
297
 
ActivationStageNotifyNetwork::connectionFailure(const QString& /*member*/, const QString& /*err_msg*/)
298
 
{
299
 
        QString title;
300
 
        QString desc;
301
 
        
302
 
        title = i18n("Connection Failure");
303
 
        if (_dev )
304
 
        {
305
 
                if ( _dev->isWired() ) {
306
 
                        desc = i18n("Could not connect to the network using device %1").arg(_dev->getInterface());
307
 
                } else {
308
 
                        desc = i18n("Could not connect to the network %1").arg(_essid);
309
 
                }
310
 
        }
311
 
 
312
 
        showError(title, desc);
313
 
}
314
 
 
315
 
ActivationStageNotifyNetwork::ActivationStageNotifyNetwork  (const QString & essid, QWidget* parent, const char* name, bool /*modal*/, WFlags fl, KNetworkManager* ctx, Device* dev )
316
 
                : ActivationStageNotify (parent, name, fl, ctx), _essid( essid )
317
 
{
318
 
        QString info;
319
 
 
320
 
        _dev = dev;
321
 
 
322
 
        QWidget* mainWid = this;
323
 
        _activationWidget = new ActivationWidget (mainWid, "activationwidget");
324
 
        if (_dev )
325
 
        {
326
 
                if ( _dev->isWired() ) {
327
 
                        _activationWidget->lblActivationCaption->setText (i18n ("Activating Network Connection"));
328
 
                        _activationWidget->lblPixmap->setPixmap(SmallIcon("wired", QIconSet::Automatic));
329
 
                        if ((info = _dev->getVendor () + " " + _dev->getProduct ()) == " ")
330
 
                                info = _dev->getInterface ();
331
 
                } else {
332
 
                        _activationWidget->lblActivationCaption->setText (i18n ("Activating Wireless Network Connection"));
333
 
                        _activationWidget->lblPixmap->setPixmap(SmallIcon("wireless", QIconSet::Automatic));
334
 
                        info = _essid;
335
 
                        info += " (" +  _dev->getInterface () + ")";
336
 
                }
337
 
        }
338
 
 
339
 
        _activationWidget->lblActivation->setText (info);
340
 
        _activationWidget->pbarActivationStage->setTotalSteps (7);
341
 
        _activationWidget->lblActivationStage->setText (QString::null);
342
 
 
343
 
        connect (parent, SIGNAL (destroyActivationStage ()),
344
 
                 this,   SLOT   (destroyActivationStage ()));
345
 
 
346
 
        connect (_ctx->getDeviceStore (), SIGNAL (deviceStoreChanged (DeviceStore*)),
347
 
                 this,                    SLOT   (updateActivationStage ()));
348
 
 
349
 
        resize(minimumSizeHint());
350
 
}
351
 
 
352
 
ActivationStageNotifyNetwork::~ActivationStageNotifyNetwork ()
353
 
{
354
 
 
355
 
}
356
 
 
357
 
 
358
 
 
359
 
void
360
 
ActivationStageNotify::destroyActivationStage ()
361
 
{
362
 
        close ();
363
 
}
364
 
 
365
 
ActivationStageNotify::ActivationStageNotify (QWidget* parent, const char* name/*, bool modal,*/, WFlags /*fl*/ , KNetworkManager* ctx)
366
 
        : QWidget (NULL, name, WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM | Qt::WDestructiveClose)
367
 
{
368
 
        _ctx = ctx;
369
 
        _errorWidget = NULL;
370
 
        // we do not want the parent as our parent-widget but we want this deleted at last when our parent is deleted
371
 
        if (parent)
372
 
                parent->insertChild(this);
373
 
        if (!name)
374
 
                setName ("ActivationStageNotify");
375
 
 
376
 
        setCaption (i18n("Activation"));
377
 
 
378
 
        QHBoxLayout* layout = new QHBoxLayout( this, 20 );
379
 
        layout->setAutoAdd(true);
380
 
        clearWState (WState_Polished);
381
 
        setPalette(QToolTip::palette());
382
 
        setAutoMask(true);
383
 
 
384
 
        resize(minimumSizeHint());
385
 
}
386
 
 
387
 
ActivationStageNotify::~ActivationStageNotify ()
388
 
{
389
 
 
390
 
}
391
 
 
392
 
void ActivationStageNotify::showError(const QString& title, const QString& details)
393
 
{
394
 
        // hide the activation widget
395
 
        if (_activationWidget)  
396
 
                _activationWidget->hide();
397
 
        
398
 
        // we need an errorwidget to show the error message
399
 
        if (_errorWidget == NULL)
400
 
                _errorWidget = new ErrorMessageWidget(this);
401
 
        
402
 
        _errorWidget->setCaption(title);
403
 
        _errorWidget->labelTitle->setText(title);
404
 
        _errorWidget->labelMessage->setText(details);
405
 
        _errorWidget->labelPixmap->setPixmap(KGlobal::instance()->iconLoader()->loadIcon("messagebox_critical", KIcon::Small, 32));
406
 
        _errorWidget->pushOK->setIconSet(SmallIcon("button_ok", QIconSet::Automatic));
407
 
 
408
 
        // close us when ok is pressed
409
 
        connect(_errorWidget->pushOK, SIGNAL(clicked()), this, SLOT(done()));
410
 
        _errorWidget->show();
411
 
 
412
 
        // resize so the message fits in our widget
413
 
        this->resize(minimumSizeHint());
414
 
 
415
 
        // show if we are hidden
416
 
        this->show();
417
 
}
418
 
 
419
 
void
420
 
ActivationStageNotify::done()
421
 
{
422
 
        close(TRUE);
423
 
}
424
 
 
425
 
void ActivationStageNotify::mousePressEvent(QMouseEvent* /*me*/)
426
 
{
427
 
        if (_errorWidget != NULL)
428
 
                /* if we display an error and the user clicked us he wants
429
 
                   the error to go away -> delete */
430
 
                close(TRUE);
431
 
        else
432
 
                /* if we have only normal state reporting and the user
433
 
                   clicked us he doesnt want to see the state changes but
434
 
                   maybe he wants o be notified when an error occurs */
435
 
                this->hide();
436
 
}
437
 
 
438
 
void ActivationStageNotify::getArrowPoints(QPointArray& arrow)
439
 
{
440
 
        arrow.resize(3);
441
 
        arrow.setPoint(0, QPoint(_right ? width() - 10 : 10,
442
 
                       _bottom ? height() - 20 : 20));
443
 
        arrow.setPoint(1, QPoint(_right ? width() : 0, _bottom ? height() : 0));
444
 
        arrow.setPoint(2, QPoint(_right ? width() - 20 : 20,
445
 
                       _bottom ? height() - 10 : 10));
446
 
}
447
 
 
448
 
void ActivationStageNotify::paintEvent(QPaintEvent* /*pe*/)
449
 
{
450
 
        QPainter paint(this);
451
 
        paint.drawRoundRect(10,10,width()-20, height()-20, 1600/width(), 1600/height());
452
 
 
453
 
        QPointArray arrow;
454
 
        getArrowPoints(arrow);
455
 
 
456
 
        paint.setPen(this->backgroundColor());
457
 
        paint.setBrush(this->backgroundColor());
458
 
        paint.drawPolygon(arrow);
459
 
 
460
 
        paint.setPen(Qt::black);
461
 
        paint.drawPolyline(arrow);
462
 
}
463
 
 
464
 
void ActivationStageNotify::setAnchor(const QPoint &anchor)
465
 
{
466
 
        _anchor = anchor;
467
 
        updateMask();
468
 
}
469
 
 
470
 
// Based on work of Kopete (http://kopete.kde.org)
471
 
void ActivationStageNotify::updateMask()
472
 
{
473
 
        QRect deskRect = KGlobalSettings::desktopGeometry(_anchor);
474
 
        _bottom = (_anchor.y() + height()) > ((deskRect.y() + deskRect.height()-48));
475
 
        _right = (_anchor.x() + width()) > ((deskRect.x() + deskRect.width()-48));
476
 
 
477
 
        QPointArray arrow;
478
 
        getArrowPoints(arrow);
479
 
 
480
 
        QBitmap mask(width(), height());
481
 
        QPainter maskPainter(&mask);
482
 
        mask.fill(Qt::black);
483
 
        maskPainter.setBrush(Qt::white);
484
 
        maskPainter.setPen(Qt::white);
485
 
        maskPainter.drawRoundRect(10, 10, mask.rect().width()- 20, mask.rect().height()-20, 1600 / mask.rect().width(), 1600 / mask.rect().height());
486
 
        maskPainter.drawPolygon(arrow);
487
 
        setMask(mask);
488
 
 
489
 
        move( _right ? _anchor.x() - width() - 10 : ( _anchor.x() + 10 < 0 ? 0 : _anchor.x() + 10 ),
490
 
              _bottom ? _anchor.y() - height() - 10 : ( _anchor.y() + 10 < 0 ? 0 : _anchor.y() + 10 )  );
491
 
}
492
 
 
493
 
#include "knetworkmanager-notify.moc"