~ubuntu-branches/ubuntu/precise/networkmanagement/precise

« back to all changes in this revision

Viewing changes to .pc/015_git-785023bf778b7cabbb482cac74b20896a07cb82f.patch/applet/networkmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-10-23 14:00:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20111023140013-e38hdzybcg6zndrk
Tags: 0.9~svngit.nm09.20111023.ff842e-0ubuntu1
* New upstream snapshot.
* Drop all patches, merged upstream.
* Add kubuntu_add_subdirectory_po.diff to build the translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright 2008, 2009 Will Stephenson <wstephenson@kde.org>
3
 
Copyright 2008, 2009 Sebastian Kügler <sebas@kde.org>
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 as
7
 
published by the Free Software Foundation; either version 2 of
8
 
the License or (at your option) version 3 or any later version
9
 
accepted by the membership of KDE e.V. (or its successor approved
10
 
by the membership of KDE e.V.), which shall act as a proxy
11
 
defined in Section 14 of version 3 of the license.
12
 
 
13
 
This program is distributed in the hope that it will be useful,
14
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
GNU General Public License for more details.
17
 
 
18
 
You should have received a copy of the GNU General Public License
19
 
along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
*/
21
 
 
22
 
#include "networkmanager.h"
23
 
 
24
 
#include <QAction>
25
 
#include <QIcon>
26
 
#include <QPaintEngine>
27
 
#include <QPainter>
28
 
#include <QDesktopWidget>
29
 
#include <QGraphicsPixmapItem>
30
 
#include <QTimeLine>
31
 
 
32
 
#include <QGraphicsBlurEffect>
33
 
 
34
 
#include <KCModuleProxy>
35
 
#include <KCModuleInfo>
36
 
#include <KIcon>
37
 
#include <KIconLoader>
38
 
#include <KToolInvocation>
39
 
#include <KConfigDialog>
40
 
#include <KDebug>
41
 
#include <KLocale>
42
 
#include <KNotification>
43
 
#include <KPushButton>
44
 
#include <kdeversion.h>
45
 
 
46
 
#include <solid/device.h>
47
 
#include <solid/networking.h>
48
 
#include <solid/control/networking.h>
49
 
#include <solid/control/networkinterface.h>
50
 
#include <solid/control/wirednetworkinterface.h>
51
 
#include <solid/control/wirelessnetworkinterface.h>
52
 
#include <solid/control/networkmanager.h>
53
 
 
54
 
#include <Plasma/Animator>
55
 
#include <Plasma/CheckBox>
56
 
#include <Plasma/Theme>
57
 
 
58
 
#include "../libs/types.h"
59
 
#include "knmserviceprefs.h"
60
 
#include "remoteactivatablelist.h"
61
 
#include "nmpopup.h"
62
 
#include "uiutils.h"
63
 
 
64
 
 
65
 
 
66
 
K_EXPORT_PLASMA_APPLET(networkmanagement, NetworkManagerApplet)
67
 
 
68
 
/* for qSort()ing */
69
 
bool networkInterfaceLessThan(Solid::Control::NetworkInterfaceNm09 * if1, Solid::Control::NetworkInterfaceNm09 * if2);
70
 
bool networkInterfaceSameConnectionStateLessThan(Solid::Control::NetworkInterfaceNm09 * if1, Solid::Control::NetworkInterfaceNm09 * if2);
71
 
 
72
 
NetworkManagerApplet::NetworkManagerApplet(QObject * parent, const QVariantList & args)
73
 
    : Plasma::PopupApplet(parent, args),
74
 
        m_iconPerDevice(false),
75
 
        m_popup(0),
76
 
        m_activeInterface(0)
77
 
{
78
 
    KGlobal::locale()->insertCatalog("libknetworkmanager");
79
 
    KGlobal::locale()->insertCatalog("solidcontrol");
80
 
 
81
 
    setHasConfigurationInterface(true);
82
 
    setPopupIcon(QIcon());
83
 
    //setPassivePopup(true); // FIXME: disable, only true for testing ...
84
 
    m_overlayTimeline.setEasingCurve(QEasingCurve::OutExpo);
85
 
    m_currentState = Solid::Control::NetworkInterfaceNm09::UnknownState;
86
 
    connect(&m_overlayTimeline, SIGNAL(valueChanged(qreal)), this, SLOT(repaint()));
87
 
 
88
 
    Plasma::ToolTipManager::self()->registerWidget(this);
89
 
    setAspectRatioMode(Plasma::ConstrainedSquare);
90
 
 
91
 
    m_svg = new Plasma::Svg(this);
92
 
    m_svg->setImagePath("icons/network");
93
 
    m_svg->setContainsMultipleImages(true);
94
 
    m_meterBgSvg = new Plasma::FrameSvg(this);
95
 
    m_meterBgSvg->setImagePath("widgets/bar_meter_horizontal");
96
 
    m_meterBgSvg->setElementPrefix("bar-inactive");
97
 
    m_meterFgSvg = new Plasma::FrameSvg(this);
98
 
    m_meterFgSvg->setImagePath("widgets/bar_meter_horizontal");
99
 
    m_meterFgSvg->setElementPrefix("bar-active");
100
 
    setStatus(Plasma::ActiveStatus);
101
 
    m_interfaces = Solid::Control::NetworkManagerNm09::networkInterfaces();
102
 
    if (activeInterface()) {
103
 
        m_currentState = static_cast<Solid::Control::NetworkInterfaceNm09::ConnectionState>(activeInterface()->connectionState());
104
 
    }
105
 
    interfaceConnectionStateChanged();
106
 
    m_activatables = new RemoteActivatableList(this);
107
 
    setMinimumSize(16, 16);
108
 
    updatePixmap();
109
 
    //(void)graphicsWidget();
110
 
}
111
 
 
112
 
NetworkManagerApplet::~NetworkManagerApplet()
113
 
{
114
 
}
115
 
 
116
 
QString NetworkManagerApplet::svgElement(Solid::Control::NetworkInterfaceNm09 *iface)
117
 
{
118
 
    if (iface->type() != Solid::Control::NetworkInterfaceNm09::Wifi
119
 
        && iface->type() != Solid::Control::NetworkInterfaceNm09::Ethernet) {
120
 
        return QString();
121
 
    }
122
 
    QString icon;
123
 
 
124
 
    int _s = qMin(contentsRect().width(), contentsRect().height());
125
 
    int s;
126
 
    // Figure out the maximum size of the element
127
 
    // those pixelsizes are taken from the svg
128
 
    if (_s >= 19) {
129
 
        s = 19;
130
 
    }
131
 
    if (_s >= 24) {
132
 
        s = 24;
133
 
    }
134
 
    if (_s >= 38) {
135
 
        s = 38;
136
 
    }
137
 
    if (_s >= 50) {
138
 
        s = 50;
139
 
    }
140
 
    if (_s >= 76) {
141
 
        s = 76;
142
 
    }
143
 
    // For our fixed sizes, we want a fixed rect to render into
144
 
    if (_s >= 19 && _s <= 76) {
145
 
        m_contentSquare = QRect(contentsRect().x() + (contentsRect().width() - s) / 2,
146
 
                                contentsRect().y() + (contentsRect().height() - s) / 2,
147
 
                                s, s);
148
 
    } else { // .... otherwise, for free scaling, we just want a square that fits in 
149
 
        m_contentSquare = QRect(contentsRect().x() + (contentsRect().width() - _s) / 2,
150
 
                                contentsRect().y() + (contentsRect().height() - _s) / 2,
151
 
                                _s, _s);
152
 
    }
153
 
 
154
 
    if (iface->type() == Solid::Control::NetworkInterfaceNm09::Ethernet) {
155
 
        if (iface->connectionState() == Solid::Control::NetworkInterfaceNm09::Activated) {
156
 
            icon = "network-wired-activated";
157
 
        } else {
158
 
            icon = "network-wired";
159
 
        }
160
 
        return icon;
161
 
    }
162
 
 
163
 
    // Now figure out which exact element we'll use
164
 
    QString strength = "00";
165
 
    Solid::Control::WirelessNetworkInterfaceNm09 *wiface = qobject_cast<Solid::Control::WirelessNetworkInterfaceNm09*>(iface);
166
 
 
167
 
    if (wiface) {
168
 
        QString uni = wiface->activeAccessPoint();
169
 
        Solid::Control::AccessPointNm09 *ap = wiface->findAccessPoint(uni);
170
 
        if (ap) {
171
 
            int str = ap->signalStrength();
172
 
            if (str < 13) {
173
 
                strength = '0';
174
 
            } else if (str < 30) {
175
 
                strength = "20";
176
 
            } else if (str < 50) {
177
 
                strength = "40";
178
 
            } else if (str < 70) {
179
 
                strength = "60";
180
 
            } else if (str < 90) {
181
 
                strength = "80";
182
 
            } else {
183
 
                strength = "100";
184
 
            }
185
 
        } else {
186
 
                strength = '0';
187
 
        }
188
 
    } else {
189
 
        return QString("dialog-error");
190
 
    }
191
 
    QString w = QString::number(s);
192
 
 
193
 
    // The format in the SVG looks like this: wireless-signal-<strenght>
194
 
    icon = QString("network-wireless-%1").arg(strength);
195
 
    //kDebug() << "Icon:" << icon;
196
 
    return icon;
197
 
}
198
 
 
199
 
void NetworkManagerApplet::setupInterfaceSignals()
200
 
{
201
 
    foreach (Solid::Control::NetworkInterfaceNm09* interface, m_interfaces) {
202
 
        // be aware of state changes
203
 
        QObject::disconnect(interface, SIGNAL(connectionStateChanged(int, int, int)), this, SLOT(interfaceConnectionStateChanged()));
204
 
        QObject::disconnect(interface, SIGNAL(connectionStateChanged(int)), this, SLOT(interfaceConnectionStateChanged()));
205
 
        QObject::disconnect(interface, SIGNAL(linkUpChanged(bool)));
206
 
 
207
 
        //connect(iface, SIGNAL(connectionStateChanged(int,int,int)), this, SLOT(handleConnectionStateChange(int,int,int)));
208
 
        connect(interface, SIGNAL(connectionStateChanged(int,int,int)), this, SLOT(interfaceConnectionStateChanged()));
209
 
        //connect(iface, SIGNAL(linkUpChanged(bool)), this, SLOT(switchToDefaultTab()));
210
 
 
211
 
        QObject::connect(interface, SIGNAL(connectionStateChanged(int)), this, SLOT(interfaceConnectionStateChanged()));
212
 
        QObject::connect(interface, SIGNAL(linkUpChanged(bool)), this, SLOT(interfaceConnectionStateChanged()));
213
 
 
214
 
        // Interface type-specific connections
215
 
        if (interface->type() == Solid::Control::NetworkInterfaceNm09::Ethernet) {
216
 
            Solid::Control::WiredNetworkInterfaceNm09* wirediface =
217
 
                            static_cast<Solid::Control::WiredNetworkInterfaceNm09*>(interface);
218
 
            connect(wirediface, SIGNAL(carrierChanged(bool)), this, SLOT(interfaceConnectionStateChanged()));
219
 
        } else if (interface->type() == Solid::Control::NetworkInterfaceNm09::Wifi) {
220
 
            Solid::Control::WirelessNetworkInterfaceNm09* wirelessiface =
221
 
                            static_cast<Solid::Control::WirelessNetworkInterfaceNm09*>(interface);
222
 
            connect(wirelessiface, SIGNAL(activeAccessPointChanged(const QString&)), SLOT(interfaceConnectionStateChanged()));
223
 
            QString uni = wirelessiface->activeAccessPoint();
224
 
            Solid::Control::AccessPointNm09 *ap = wirelessiface->findAccessPoint(uni);
225
 
            if (ap) {
226
 
                connect(ap, SIGNAL(signalStrengthChanged(int)), SLOT(interfaceConnectionStateChanged()));
227
 
                connect(ap, SIGNAL(destroyed(QObject*)), SLOT(interfaceConnectionStateChanged()));
228
 
            }
229
 
        }
230
 
    }
231
 
}
232
 
 
233
 
void NetworkManagerApplet::init()
234
 
{
235
 
    // bogus, just to make sure we have some remotely sensible value
236
 
    m_contentSquare = contentsRect().toRect();
237
 
    //kDebug();
238
 
    configChanged();
239
 
    QObject::connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(networkInterfaceAdded(const QString&)),
240
 
            this, SLOT(networkInterfaceAdded(const QString&)));
241
 
    QObject::connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(networkInterfaceRemoved(const QString&)),
242
 
            this, SLOT(networkInterfaceRemoved(const QString&)));
243
 
 
244
 
    QObject::connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(statusChanged(Solid::Networking::Status)),
245
 
                     this, SLOT(managerStatusChanged(Solid::Networking::Status)));
246
 
 
247
 
    m_activatables->init();
248
 
    setupInterfaceSignals();
249
 
 
250
 
    // Just to make sure the kded module is loaded.
251
 
    QDBusInterface kded(QLatin1String("org.kde.kded"), QLatin1String("/kded"),
252
 
                        QLatin1String("org.kde.kded"), QDBusConnection::sessionBus());
253
 
 
254
 
    kded.call(QLatin1String("loadModule"), QLatin1String("networkmanagement"));
255
 
 
256
 
    // Import old connection files.
257
 
    QDBusInterface networkmanagement(QLatin1String("org.kde.networkmanagement"), QLatin1String("/org/kde/networkmanagement"),
258
 
                                     QLatin1String("org.kde.networkmanagement"), QDBusConnection::sessionBus());
259
 
 
260
 
    networkmanagement.call(QLatin1String("ImportNm08Connections"));
261
 
}
262
 
 
263
 
void NetworkManagerApplet::configChanged()
264
 
{
265
 
    KConfigGroup cg = config();
266
 
    m_iconPerDevice = cg.readEntry("IconPerDevice", false);
267
 
}
268
 
 
269
 
 
270
 
 
271
 
QGraphicsWidget* NetworkManagerApplet::graphicsWidget()
272
 
{
273
 
    if (!m_popup) {
274
 
        m_popup = new NMPopup(m_activatables, this);
275
 
        connect(m_popup, SIGNAL(configNeedsSaving()), this, SIGNAL(configNeedsSaving()));
276
 
    }
277
 
    return m_popup;
278
 
 
279
 
}
280
 
 
281
 
void NetworkManagerApplet::createConfigurationInterface(KConfigDialog *parent)
282
 
{
283
 
    // Add the networkmanager KCM pages to the applet's configdialog
284
 
    m_kcmNM = new KCModuleProxy("kcm_networkmanagement");
285
 
    m_kcmNMTray = new KCModuleProxy("kcm_networkmanagement_tray");
286
 
    parent->addPage(m_kcmNM, m_kcmNM->moduleInfo().moduleName(),
287
 
                    m_kcmNM->moduleInfo().icon());
288
 
    parent->addPage(m_kcmNMTray, m_kcmNMTray->moduleInfo().moduleName(),
289
 
                    m_kcmNMTray->moduleInfo().icon());
290
 
 
291
 
    connect(parent, SIGNAL(applyClicked()), this, SLOT(saveConfiguration()));
292
 
    connect(parent, SIGNAL(okClicked()), this, SLOT(saveConfiguration()));
293
 
}
294
 
 
295
 
void NetworkManagerApplet::saveConfiguration()
296
 
{
297
 
    // kcm_networkmanagement implicitly saves connection definition after
298
 
    // editing is finished, so no need to call its save() method
299
 
    // FIXME This just writes out changed values to ini file. kded module
300
 
    // still continues to use old value
301
 
    m_kcmNMTray->save();
302
 
}
303
 
 
304
 
void NetworkManagerApplet::constraintsEvent(Plasma::Constraints constraints)
305
 
{
306
 
    // update the pixmap when a new size from kiconloader fits in, this makes sure the
307
 
    // icon is only displayed in sizes provides by KIconLoader, so we don't get blurry
308
 
    // icons
309
 
    if (constraints & (Plasma::SizeConstraint | Plasma::FormFactorConstraint)) {
310
 
        if (UiUtils::iconSize(contentsRect().size()) != UiUtils::iconSize(m_pixmap.size())) {
311
 
            updatePixmap();
312
 
        }
313
 
    }
314
 
}
315
 
 
316
 
void NetworkManagerApplet::updatePixmap()
317
 
{
318
 
    int s = UiUtils::iconSize(contentsRect().size());
319
 
    m_pixmap = KIcon(UiUtils::iconName(activeInterface())).pixmap(s, s);
320
 
    update();
321
 
}
322
 
 
323
 
void NetworkManagerApplet::paintInterface(QPainter * p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
324
 
{
325
 
    Q_UNUSED( option );
326
 
 
327
 
    Solid::Control::NetworkInterfaceNm09* interface = activeInterface();
328
 
    bool useSvg = false;
329
 
    if (interface) {
330
 
        useSvg = interface->type() == Solid::Control::NetworkInterfaceNm09::Wifi || interface->type() == Solid::Control::NetworkInterfaceNm09::Ethernet;
331
 
    }
332
 
 
333
 
    if (useSvg) {
334
 
        QString el = svgElement(interface);
335
 
        m_svg->paint(p, m_contentSquare, el);
336
 
    } else {
337
 
        paintPixmap(p, m_pixmap, contentsRect);
338
 
    }
339
 
    paintStatusOverlay(p);
340
 
    paintNeedAuthOverlay(p);
341
 
}
342
 
 
343
 
void NetworkManagerApplet::paintNeedAuthOverlay(QPainter *p)
344
 
{
345
 
    // Needs authentication, show this in the panel
346
 
    if (!activeInterface()) {
347
 
        kDebug() << "No active interface";
348
 
        return;
349
 
    }
350
 
    /*
351
 
    kDebug() << "Painting overlay ...>" << activeInterface()->connectionState();
352
 
    */
353
 
    if (activeInterface() && activeInterface()->connectionState() == Solid::Control::NetworkInterfaceNm09::NeedAuth) {
354
 
        //kDebug() << "Needing auth ...>";
355
 
        int i_s = (int)contentsRect().width()/4;
356
 
        int iconsize = qMax(UiUtils::iconSize(QSizeF(i_s, i_s)), 8);
357
 
 
358
 
        //kDebug() << "Security:iconsize" << iconsize;
359
 
        QPixmap icon = KIcon("dialog-password").pixmap(iconsize);
360
 
        QPoint pos = QPoint(contentsRect().right() - iconsize,
361
 
                            contentsRect().bottom() - iconsize);
362
 
 
363
 
        p->drawPixmap(pos, icon);
364
 
    }
365
 
}
366
 
 
367
 
void NetworkManagerApplet::paintStatusOverlay(QPainter *p)
368
 
{
369
 
    int oldOpacity = p->opacity();
370
 
    qreal opacity = m_overlayTimeline.currentValue();
371
 
    if (!qFuzzyCompare(opacity, 1) && !m_previousStatusOverlay.isNull()) {
372
 
        p->setOpacity(1 - opacity);
373
 
        p->drawPixmap(contentsRect().left(), contentsRect().bottom() - m_previousStatusOverlay.height(), m_previousStatusOverlay);
374
 
    }
375
 
    if (!m_statusOverlay.isNull()) {
376
 
        p->setOpacity(opacity);
377
 
        p->drawPixmap(contentsRect().left(), contentsRect().bottom() - m_statusOverlay.height(), m_statusOverlay);
378
 
    }
379
 
    p->setOpacity(oldOpacity);
380
 
}
381
 
 
382
 
void NetworkManagerApplet::paintPixmap(QPainter *painter, QPixmap pixmap, const QRectF &rect, qreal opacity)
383
 
{
384
 
    int size = pixmap.size().width();
385
 
    QPointF iconOrigin = QPointF(rect.left() + (rect.width() - size) / 2,
386
 
                                 rect.top() + (rect.height() - size) / 2);
387
 
 
388
 
    painter->setRenderHint(QPainter::SmoothPixmapTransform);
389
 
    painter->setRenderHint(QPainter::Antialiasing);
390
 
 
391
 
    if (painter->paintEngine()->hasFeature(QPaintEngine::ConstantOpacity)) {
392
 
        // NOTE: Works, but makes hw acceleration impossible, use below code path
393
 
        //kWarning() << "You don't really want to hit this path, it means slow painting. Your paintengine is not good enough.";
394
 
        qreal old = painter->opacity();
395
 
        painter->setOpacity(opacity);
396
 
        painter->drawPixmap(iconOrigin, pixmap);
397
 
        painter->setOpacity(old);
398
 
    } else {
399
 
        QPixmap temp(QSize(size, size));
400
 
        temp.fill(Qt::transparent);
401
 
 
402
 
        QPainter p;
403
 
        p.begin(&temp);
404
 
 
405
 
        p.setCompositionMode(QPainter::CompositionMode_Source);
406
 
        p.drawPixmap(QPoint(0,0), pixmap);
407
 
 
408
 
        p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
409
 
        p.fillRect(pixmap.rect(), QColor(0, 0, 0, opacity * 254));
410
 
        p.end();
411
 
 
412
 
        // draw the pixmap
413
 
        painter->drawPixmap(iconOrigin, temp);
414
 
    }
415
 
}
416
 
 
417
 
void NetworkManagerApplet::repaint()
418
 
{
419
 
    update();
420
 
}
421
 
 
422
 
/* Slots to react to changes from the daemon */
423
 
void NetworkManagerApplet::networkInterfaceAdded(const QString & uni)
424
 
{
425
 
    Q_UNUSED(uni);
426
 
    // update the tray icon
427
 
    m_interfaces = Solid::Control::NetworkManagerNm09::networkInterfaces();
428
 
 
429
 
    setupInterfaceSignals();
430
 
    interfaceConnectionStateChanged();
431
 
    updatePixmap();
432
 
}
433
 
 
434
 
void NetworkManagerApplet::networkInterfaceRemoved(const QString & uni)
435
 
{
436
 
    Q_UNUSED(uni);
437
 
    // update the tray icon
438
 
    m_interfaces = Solid::Control::NetworkManagerNm09::networkInterfaces();
439
 
    KConfigGroup cg = config();
440
 
 
441
 
    setupInterfaceSignals();
442
 
    interfaceConnectionStateChanged();
443
 
    updatePixmap();
444
 
    // kill any animations involving this interface
445
 
}
446
 
 
447
 
Solid::Control::NetworkInterfaceNm09* NetworkManagerApplet::activeInterface()
448
 
{
449
 
    if (!m_interfaces.isEmpty()) {
450
 
        qSort(m_interfaces.begin(), m_interfaces.end(), networkInterfaceLessThan);
451
 
        return m_interfaces.first();
452
 
    } else {
453
 
        return 0;
454
 
    }
455
 
 
456
 
}
457
 
 
458
 
void NetworkManagerApplet::interfaceConnectionStateChanged()
459
 
{
460
 
    //kDebug() << " +++ +++ +++ Connection State Changed +++ +++ +++";
461
 
    if (activeInterface()) {
462
 
        //kDebug() << "busy ... ?";
463
 
        Solid::Control::NetworkInterfaceNm09::ConnectionState state = static_cast<Solid::Control::NetworkInterfaceNm09::ConnectionState>(activeInterface()->connectionState());
464
 
        switch (state) {
465
 
            case Solid::Control::NetworkInterfaceNm09::Preparing:
466
 
            case Solid::Control::NetworkInterfaceNm09::Configuring:
467
 
            case Solid::Control::NetworkInterfaceNm09::IPConfig:
468
 
            case Solid::Control::NetworkInterfaceNm09::IPCheck:
469
 
            case Solid::Control::NetworkInterfaceNm09::Secondaries:
470
 
                if (m_currentState != state) {
471
 
                    setStatusOverlay(generateProgressStatusOverlay());
472
 
                }
473
 
                //setBusy(true);
474
 
                break;
475
 
            case Solid::Control::NetworkInterfaceNm09::NeedAuth:
476
 
                //setBusy(false);
477
 
                break;
478
 
            case Solid::Control::NetworkInterfaceNm09::Activated:
479
 
                //setBusy(false);
480
 
                if (m_currentState != state) {
481
 
                    // We want to show the full circle a bit
482
 
                    setStatusOverlay(generateProgressStatusOverlay());
483
 
                    setStatusOverlay("task-complete");
484
 
                    QTimer::singleShot(4000, this, SLOT(clearActivatedOverlay()));
485
 
                }
486
 
                break;
487
 
            case Solid::Control::NetworkInterfaceNm09::UnknownState:
488
 
                kDebug() << "UnknownState! should this happen?";
489
 
            case Solid::Control::NetworkInterfaceNm09::Unmanaged:
490
 
            case Solid::Control::NetworkInterfaceNm09::Unavailable:
491
 
            case Solid::Control::NetworkInterfaceNm09::Failed:
492
 
                if (m_currentState != state) {
493
 
                    setStatusOverlay("dialog-error");
494
 
                }
495
 
                break;
496
 
            case Solid::Control::NetworkInterfaceNm09::Disconnected:
497
 
                if (m_currentState != state) {
498
 
                    setStatusOverlay("dialog-cancel");
499
 
                }
500
 
                break;
501
 
        }
502
 
        m_currentState = state;
503
 
    }
504
 
    updatePixmap();
505
 
}
506
 
 
507
 
void NetworkManagerApplet::toolTipAboutToShow()
508
 
{
509
 
    Solid::Control::NetworkInterfaceNm09List interfaces
510
 
        = Solid::Control::NetworkManagerNm09::networkInterfaces();
511
 
    if (interfaces.isEmpty()) {
512
 
        m_toolTip = Plasma::ToolTipContent(QString(),
513
 
                                        i18nc("Tooltip sub text", "No network interfaces"),
514
 
                                        KIcon("networkmanager").pixmap(IconSize(KIconLoader::Desktop))
515
 
                                        );
516
 
    } else {
517
 
        qSort(interfaces.begin(), interfaces.end(), networkInterfaceLessThan);
518
 
        bool hasActive = false;
519
 
        bool iconChanged = false;
520
 
        QString icon = "networkmanager";
521
 
        QStringList lines;
522
 
        foreach (Solid::Control::NetworkInterfaceNm09 *iface, interfaces) {
523
 
            if (iface->connectionState() != Solid::Control::NetworkInterfaceNm09::Unavailable) {
524
 
                if (!lines.isEmpty()) {
525
 
                    lines << QString();
526
 
                }
527
 
                hasActive = true;
528
 
 
529
 
                QString deviceText = UiUtils::interfaceNameLabel(iface->uni());
530
 
 
531
 
                QString ifaceName = iface->interfaceName();
532
 
                lines << QString::fromLatin1("<b>%1</b>").arg(deviceText);
533
 
                QString connectionName;
534
 
                RemoteInterfaceConnection *conn = m_activatables->connectionForInterface(iface);
535
 
                if (conn) {
536
 
                    connectionName = conn->connectionName();
537
 
                }
538
 
 
539
 
                lines << QString("%1").arg(UiUtils::connectionStateToString(static_cast<Solid::Control::NetworkInterfaceNm09::ConnectionState>(iface->connectionState()), connectionName));
540
 
                /*
541
 
                Solid::Control::IPv4Config ip4Config = iface->ipV4Config();
542
 
                QList<Solid::Control::IPv4Address> addresses = ip4Config.addresses();
543
 
                if (!addresses.isEmpty()) {
544
 
                    QHostAddress addr(addresses.first().address());
545
 
                    QString currentIp = addr.toString();
546
 
                    lines << i18nc("Display of the IP (network) address in the tooltip", "<font size=\"-1\">Address: %1</font>", currentIp);
547
 
                }
548
 
                */
549
 
                // Show the first active connection's icon, otherwise the networkmanager icon
550
 
                if (!iconChanged && iface->connectionState() == Solid::Control::NetworkInterfaceNm09::Activated) {
551
 
                    icon = UiUtils::iconName(iface);
552
 
                    iconChanged = true; // we only want the first one
553
 
                }
554
 
            }
555
 
        }
556
 
        QString subText;
557
 
        QString text;
558
 
        if (hasActive) {
559
 
            subText = lines.join(QLatin1String("<br>"));
560
 
        } else {
561
 
            text = i18nc("tooltip, all interfaces are down", "Disconnected");
562
 
 
563
 
            if (m_popup->hasWireless() && !Solid::Control::NetworkManagerNm09::isWirelessEnabled()) {
564
 
                subText = i18nc("tooltip, wireless is disabled in software", "Wireless disabled in software");
565
 
            }
566
 
            if (!Solid::Control::NetworkManagerNm09::isNetworkingEnabled()) {
567
 
                subText = i18nc("tooltip, all interfaces are down", "Networking disabled");
568
 
            }
569
 
            if (m_popup->hasWireless() && !Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled()) {
570
 
                subText = i18nc("tooltip, wireless is disabled by hardware", "Wireless disabled by hardware");
571
 
            }
572
 
 
573
 
        }
574
 
        m_toolTip = Plasma::ToolTipContent(text,
575
 
                                           subText,
576
 
                                           KIcon(icon).pixmap(IconSize(KIconLoader::Desktop))
577
 
                                           );
578
 
    }
579
 
    Plasma::ToolTipManager::self()->setContent(this, m_toolTip);
580
 
}
581
 
 
582
 
 
583
 
bool networkInterfaceLessThan(Solid::Control::NetworkInterfaceNm09 *if1, Solid::Control::NetworkInterfaceNm09 * if2)
584
 
{
585
 
    /*
586
 
     * status merging algorithm
587
 
     * In descending order of importance:
588
 
     * - Connecting devices
589
 
     *   - Cellular devices (because of cost)
590
 
     *   - = PPP devices
591
 
     *   - Ethernet devices
592
 
     *   - Wireless devices
593
 
     * - Connected devices
594
 
     *   - order as above
595
 
     * - Disconnected devices
596
 
     *   - order as above
597
 
     */
598
 
    enum {  Connecting,
599
 
            Connected,
600
 
            Disconnected,
601
 
            Unavailable }
602
 
        if2status = Unavailable,
603
 
        if1status = Unavailable;
604
 
 
605
 
    switch (if1->connectionState()) {
606
 
        case Solid::Control::NetworkInterfaceNm09::Preparing:
607
 
        case Solid::Control::NetworkInterfaceNm09::Configuring:
608
 
        case Solid::Control::NetworkInterfaceNm09::NeedAuth:
609
 
        case Solid::Control::NetworkInterfaceNm09::IPConfig:
610
 
        case Solid::Control::NetworkInterfaceNm09::IPCheck:
611
 
        case Solid::Control::NetworkInterfaceNm09::Secondaries:
612
 
            if1status = Connecting;
613
 
            break;
614
 
        case Solid::Control::NetworkInterfaceNm09::Activated:
615
 
            if1status = Connected;
616
 
            break;
617
 
        case Solid::Control::NetworkInterfaceNm09::Disconnected:
618
 
            if1status = Disconnected;
619
 
            break;
620
 
        default: // all kind of unavailable
621
 
            break;
622
 
    }
623
 
    switch (if2->connectionState()) {
624
 
        case Solid::Control::NetworkInterfaceNm09::Preparing:
625
 
        case Solid::Control::NetworkInterfaceNm09::Configuring:
626
 
        case Solid::Control::NetworkInterfaceNm09::NeedAuth:
627
 
        case Solid::Control::NetworkInterfaceNm09::IPConfig:
628
 
        case Solid::Control::NetworkInterfaceNm09::IPCheck:
629
 
        case Solid::Control::NetworkInterfaceNm09::Secondaries:
630
 
            if2status = Connecting;
631
 
            break;
632
 
        case Solid::Control::NetworkInterfaceNm09::Activated:
633
 
            if2status = Connected;
634
 
            break;
635
 
        case Solid::Control::NetworkInterfaceNm09::Disconnected:
636
 
            if2status = Disconnected;
637
 
            break;
638
 
        default: // all kind of disconnected
639
 
            break;
640
 
    }
641
 
 
642
 
    bool lessThan = false;
643
 
    switch (if1status) {
644
 
        case Connecting:
645
 
            lessThan = (if2status != Connecting || networkInterfaceSameConnectionStateLessThan(if1, if2));
646
 
            //return true;//
647
 
            break;
648
 
        case Connected:
649
 
            if ( if2status == Connecting) {
650
 
               return false;
651
 
            }
652
 
            lessThan = ((if2status != Connected) || networkInterfaceSameConnectionStateLessThan(if1, if2));
653
 
            break;
654
 
        case Disconnected:
655
 
            lessThan = false;
656
 
            if ( if2status == Disconnected) {
657
 
                lessThan = networkInterfaceSameConnectionStateLessThan(if1, if2);
658
 
            }
659
 
            if (if2status == Unavailable) {
660
 
                lessThan = true;
661
 
            }
662
 
            break;
663
 
        case Unavailable:
664
 
            lessThan = false;
665
 
            if ( if2status == Unavailable) {
666
 
                lessThan = networkInterfaceSameConnectionStateLessThan(if1, if2);
667
 
            }
668
 
            break;
669
 
    }
670
 
 
671
 
    return lessThan;
672
 
}
673
 
 
674
 
bool networkInterfaceSameConnectionStateLessThan(Solid::Control::NetworkInterfaceNm09 * if1, Solid::Control::NetworkInterfaceNm09 * if2)
675
 
{
676
 
    bool lessThan = false;
677
 
    switch (if1->type() ) {
678
 
        case Solid::Control::NetworkInterfaceNm09::Ethernet:
679
 
            switch (if2->type()) {
680
 
                case Solid::Control::NetworkInterfaceNm09::Ethernet:
681
 
                    lessThan = if1->uni() < if2->uni();
682
 
                    break;
683
 
                case Solid::Control::NetworkInterfaceNm09::Wifi:
684
 
                    lessThan = true;
685
 
                    break;
686
 
                case Solid::Control::NetworkInterfaceNm09::Modem:
687
 
                default:
688
 
                    lessThan = false;
689
 
                    break;
690
 
            }
691
 
            break;
692
 
        case Solid::Control::NetworkInterfaceNm09::Wifi:
693
 
            switch (if2->type()) {
694
 
                case Solid::Control::NetworkInterfaceNm09::Ethernet:
695
 
                    lessThan = false;
696
 
                    break;
697
 
                case Solid::Control::NetworkInterfaceNm09::Wifi:
698
 
                    lessThan = if1->uni() < if2->uni();
699
 
                    break;
700
 
                case Solid::Control::NetworkInterfaceNm09::Modem:
701
 
                    lessThan = false;
702
 
                    break;
703
 
                default:
704
 
                    lessThan = true;
705
 
                    break;
706
 
            }
707
 
            break;
708
 
        case Solid::Control::NetworkInterfaceNm09::Modem:
709
 
            switch (if2->type()) {
710
 
                case Solid::Control::NetworkInterfaceNm09::Ethernet:
711
 
                case Solid::Control::NetworkInterfaceNm09::Wifi:
712
 
                    lessThan = true;
713
 
                    break;
714
 
                case Solid::Control::NetworkInterfaceNm09::Modem:
715
 
                    lessThan = if1->uni() < if2->uni();
716
 
                    break;
717
 
                default:
718
 
                    lessThan = true;
719
 
                    break;
720
 
            }
721
 
            break;
722
 
        default:
723
 
            lessThan = false;
724
 
        }
725
 
    return lessThan;
726
 
}
727
 
 
728
 
void NetworkManagerApplet::managerWirelessEnabledChanged(bool)
729
 
{
730
 
    setupInterfaceSignals();
731
 
}
732
 
 
733
 
void NetworkManagerApplet::managerWirelessHardwareEnabledChanged(bool enabled)
734
 
{
735
 
    Q_UNUSED( enabled );
736
 
    setupInterfaceSignals();
737
 
    updatePixmap();
738
 
}
739
 
 
740
 
void NetworkManagerApplet::userNetworkingEnabledChanged(bool enabled)
741
 
{
742
 
    kDebug() << enabled;
743
 
    Solid::Control::NetworkManagerNm09::setNetworkingEnabled(enabled);
744
 
    setupInterfaceSignals();
745
 
}
746
 
 
747
 
void NetworkManagerApplet::userWirelessEnabledChanged(bool enabled)
748
 
{
749
 
    kDebug() << enabled;
750
 
    Solid::Control::NetworkManagerNm09::setWirelessEnabled(enabled);
751
 
    setupInterfaceSignals();
752
 
}
753
 
 
754
 
void NetworkManagerApplet::managerStatusChanged(Solid::Networking::Status status)
755
 
{
756
 
    //kDebug() << "managerstatuschanged";
757
 
    if (Solid::Networking::Unknown == status ) {
758
 
        // FIXME: Do something smart
759
 
    } else {
760
 
        // ...
761
 
    }
762
 
    setupInterfaceSignals();
763
 
    updatePixmap();
764
 
}
765
 
 
766
 
bool NetworkManagerApplet::hasInterfaceOfType(Solid::Control::NetworkInterfaceNm09::Type type)
767
 
{
768
 
    foreach (Solid::Control::NetworkInterfaceNm09 * interface, m_interfaces) {
769
 
        if (interface->type() == type) {
770
 
            return true;
771
 
        }
772
 
    }
773
 
    return false;
774
 
}
775
 
 
776
 
void NetworkManagerApplet::setStatusOverlay(const QPixmap& pix)
777
 
{
778
 
    m_previousStatusOverlay = m_statusOverlay;
779
 
    m_statusOverlay = pix;
780
 
    if (m_overlayTimeline.state() == QTimeLine::Running) {
781
 
        m_overlayTimeline.stop();
782
 
    }
783
 
    m_overlayTimeline.start();
784
 
}
785
 
 
786
 
void NetworkManagerApplet::setStatusOverlay(const QString& name)
787
 
{
788
 
    int i_s = (int)contentsRect().width()/4;
789
 
    int size = qMax(UiUtils::iconSize(QSizeF(i_s, i_s)), 8);
790
 
    QPixmap pix = KIcon(name).pixmap(size);
791
 
    setStatusOverlay(pix);
792
 
}
793
 
 
794
 
QPixmap NetworkManagerApplet::generateProgressStatusOverlay()
795
 
{
796
 
    int width = contentsRect().width();
797
 
    int height = qMax(width / 4, 4);
798
 
 
799
 
    QPixmap pix(width, height);
800
 
    pix.fill(Qt::transparent);
801
 
    qreal state = UiUtils::interfaceState(activeInterface());
802
 
 
803
 
    QPainter p(&pix);
804
 
    p.setRenderHint(QPainter::Antialiasing);
805
 
    m_meterBgSvg->resizeFrame(pix.size());
806
 
    m_meterBgSvg->paintFrame(&p, pix.rect());
807
 
 
808
 
    QRectF innerRect = pix.rect();
809
 
    innerRect.setWidth(innerRect.width() * state);
810
 
    m_meterFgSvg->resizeFrame(innerRect.size());
811
 
    m_meterFgSvg->paintFrame(&p, innerRect);
812
 
 
813
 
    return pix;
814
 
}
815
 
 
816
 
void NetworkManagerApplet::clearActivatedOverlay()
817
 
{
818
 
    if (activeInterface() && static_cast<Solid::Control::NetworkInterfaceNm09::ConnectionState>(activeInterface()->connectionState()) == Solid::Control::NetworkInterfaceNm09::Activated) {
819
 
        // Clear the overlay, but only if we are still activated
820
 
        setStatusOverlay(QPixmap());
821
 
    }
822
 
}
823
 
 
824
 
#include "networkmanager.moc"