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

« back to all changes in this revision

Viewing changes to .pc/fix-and-workaround-graphical-artifacts.diff/applet/nmpopup.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-02-11 20:44:55 UTC
  • Revision ID: package-import@ubuntu.com-20120211204455-se2bx8iwlyhnp89u
Tags: 0.9.0~rc4-0ubuntu2
Cherry pick upstream commit 6a5e30e94ee539b7765eb5ad054bf9e1a6785703
as fix-and-workaround-graphical-artifacts.diff
to fix graphical artifacts in buttons with the default plasma theme.

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-2010 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
// Own
 
23
#include "nmpopup.h"
 
24
 
 
25
// Qt
 
26
#include <QGraphicsLinearLayout>
 
27
#include <QGraphicsGridLayout>
 
28
#include <QTimer>
 
29
#include <QDBusConnection>
 
30
#include <QCheckBox>
 
31
 
 
32
// KDE
 
33
#include <KDebug>
 
34
#include <KIcon>
 
35
#include <KIconLoader>
 
36
#include <KToolInvocation>
 
37
 
 
38
// Plasma
 
39
#include <Plasma/Label>
 
40
#include <Plasma/Separator>
 
41
 
 
42
// Solid::Control
 
43
#include <solid/control/networkmanager.h>
 
44
#include <solid/control/wirelessnetworkinterface.h>
 
45
#include <solid/control/wirednetworkinterface.h>
 
46
 
 
47
// client lib
 
48
#include "activatableitem.h"
 
49
#include "remoteactivatable.h"
 
50
#include "remoteactivatablelist.h"
 
51
#include "remotewirelessinterfaceconnection.h"
 
52
 
 
53
// More own includes
 
54
#include "interfaceitem.h"
 
55
#include "wirelessinterfaceitem.h"
 
56
#include "vpninterfaceitem.h"
 
57
#include "activatablelistwidget.h"
 
58
#include "interfacedetailswidget.h"
 
59
#include "uiutils.h"
 
60
#include "paths.h"
 
61
 
 
62
NMPopup::NMPopup(RemoteActivatableList * activatableList, QGraphicsWidget* parent)
 
63
: QGraphicsWidget(parent),
 
64
    m_activatables(activatableList),
 
65
    m_hasWirelessInterface(false),
 
66
    m_widget(0),
 
67
    m_mainLayout(0),
 
68
    m_leftWidget(0),
 
69
    m_leftLayout(0),
 
70
    m_interfaceLayout(0),
 
71
    m_connectionList(0),
 
72
    m_vpnItem(0)
 
73
{
 
74
    init();
 
75
}
 
76
 
 
77
NMPopup::~NMPopup()
 
78
{
 
79
}
 
80
 
 
81
void NMPopup::init()
 
82
{
 
83
    m_mainLayout = new QGraphicsGridLayout(this);
 
84
 
 
85
    m_leftLabel = new Plasma::Label(this);
 
86
    m_leftLabel->setMaximumHeight(24);
 
87
    m_leftLabel->setMinimumHeight(24);
 
88
    m_leftLabel->setText(i18nc("title on the LHS of the plasmoid", "<h3>Interfaces</h3>"));
 
89
    m_mainLayout->addItem(m_leftLabel, 0, 0);
 
90
 
 
91
    m_rightLabel = new Plasma::Label(this);
 
92
    m_rightLabel->setMaximumHeight(24);
 
93
    m_rightLabel->setMinimumHeight(24);
 
94
    m_rightLabel->setText(i18nc("title on the RHS of the plasmoid", "<h3>Connections</h3>"));
 
95
    m_mainLayout->addItem(m_rightLabel, 0, 2);
 
96
 
 
97
    Plasma::Separator* sep = new Plasma::Separator(this);
 
98
    sep->setOrientation(Qt::Vertical);
 
99
    m_mainLayout->addItem(sep, 0, 1, 2, 1, Qt::AlignRight);
 
100
    m_mainLayout->setRowFixedHeight(0, 24);
 
101
 
 
102
    m_leftWidget = new Plasma::TabBar(this);
 
103
    m_leftWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 
104
    m_leftLayout = new QGraphicsLinearLayout;
 
105
    m_leftLayout->setOrientation(Qt::Vertical);
 
106
 
 
107
    m_interfaceLayout = new QGraphicsLinearLayout;
 
108
    m_interfaceLayout->setOrientation(Qt::Vertical);
 
109
    m_interfaceLayout->setSpacing(0);
 
110
 
 
111
    m_leftLayout->addItem(m_interfaceLayout);
 
112
    m_leftLayout->addStretch(5);
 
113
 
 
114
    Plasma::Separator* sep2 = new Plasma::Separator(this);
 
115
    sep->setOrientation(Qt::Vertical);
 
116
    m_leftLayout->addItem(sep2);
 
117
 
 
118
    QGraphicsWidget* checkboxWidget = new QGraphicsWidget(this);
 
119
    QGraphicsGridLayout* checkboxLayout = new QGraphicsGridLayout(checkboxWidget);
 
120
    // flight-mode checkbox
 
121
    m_networkingCheckBox = new Plasma::CheckBox(m_leftWidget);
 
122
    m_networkingCheckBox->setText(i18nc("CheckBox to enable or disable networking completely", "Enable networking"));
 
123
    checkboxLayout->addItem(m_networkingCheckBox, 0, 0);
 
124
    connect(m_networkingCheckBox, SIGNAL(toggled(bool)),
 
125
            this, SLOT(networkingEnabledToggled(bool)));
 
126
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(networkingEnabledChanged(bool)),
 
127
            this, SLOT(managerNetworkingEnabledChanged(bool)));
 
128
 
 
129
    // flight-mode checkbox
 
130
    m_wwanCheckBox = new Plasma::CheckBox(m_leftWidget);
 
131
    m_wwanCheckBox->setText(i18nc("CheckBox to enable or disable wwan (mobile broadband) interface)", "Enable mobile broadband"));
 
132
    m_wwanCheckBox->hide();
 
133
    m_wwanCheckBox->nativeWidget()->setTristate(true);
 
134
    checkboxLayout->addItem(m_wwanCheckBox, 0, 1);
 
135
 
 
136
    connect(m_wwanCheckBox, SIGNAL(toggled(bool)), SLOT(wwanEnabledToggled(bool)));
 
137
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(wwanEnabledChanged(bool)),
 
138
            this, SLOT(managerWwanEnabledChanged(bool)));
 
139
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(wwanHardwareEnabledChanged(bool)),
 
140
            this, SLOT(managerWwanHardwareEnabledChanged(bool)));
 
141
 
 
142
    // flight-mode checkbox
 
143
    m_wifiCheckBox = new Plasma::CheckBox(m_leftWidget);
 
144
    m_wifiCheckBox->setText(i18nc("CheckBox to enable or disable wireless interface (rfkill)", "Enable wireless"));
 
145
    m_wifiCheckBox->hide();
 
146
    checkboxLayout->addItem(m_wifiCheckBox, 1, 0);
 
147
 
 
148
    connect(m_wifiCheckBox, SIGNAL(toggled(bool)), SLOT(wirelessEnabledToggled(bool)));
 
149
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(wirelessEnabledChanged(bool)),
 
150
            this, SLOT(managerWirelessEnabledChanged(bool)));
 
151
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(wirelessHardwareEnabledChanged(bool)),
 
152
            this, SLOT(managerWirelessHardwareEnabledChanged(bool)));
 
153
 
 
154
    m_leftLayout->addItem(checkboxWidget);
 
155
 
 
156
    m_leftWidget->addTab(i18nc("tabbar on the left side", "Interfaces"), m_leftLayout);
 
157
    m_leftWidget->setTabBarShown(false); // TODO: enable
 
158
 
 
159
    m_interfaceDetailsWidget = new InterfaceDetailsWidget(m_leftWidget);
 
160
    m_interfaceDetailsWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 
161
    connect(m_interfaceDetailsWidget, SIGNAL(back()), this, SLOT(toggleInterfaceTab()));
 
162
 
 
163
    // Hack to prevent graphical artifacts during tab transition.
 
164
    connect(m_leftWidget, SIGNAL(currentChanged(int)), SLOT(refresh()));
 
165
 
 
166
    m_leftWidget->addTab(i18nc("details for the interface", "Details"), m_interfaceDetailsWidget);
 
167
 
 
168
    m_mainLayout->addItem(m_leftWidget, 1, 0);
 
169
 
 
170
    m_rightWidget = new QGraphicsWidget(this);
 
171
    m_rightWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
 
172
    m_rightLayout = new QGraphicsLinearLayout(m_rightWidget);
 
173
    m_rightLayout->setOrientation(Qt::Vertical);
 
174
 
 
175
    // List with activatables
 
176
    m_connectionList = new ActivatableListWidget(m_activatables, m_rightWidget);
 
177
    m_connectionList->addType(Knm::Activatable::InterfaceConnection);
 
178
    m_connectionList->addType(Knm::Activatable::WirelessInterfaceConnection);
 
179
    m_connectionList->addType(Knm::Activatable::VpnInterfaceConnection);
 
180
    m_connectionList->addType(Knm::Activatable::GsmInterfaceConnection);
 
181
    m_connectionList->init();
 
182
 
 
183
    m_connectionList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
184
    m_connectionList->setPreferredHeight(240);
 
185
    m_connectionList->setShowAllTypes(true, true);
 
186
    connect(m_connectionList, SIGNAL(showInterfaceDetails(QString)), SLOT(showInterfaceDetails(QString)));
 
187
 
 
188
    m_rightLayout->addItem(m_connectionList);
 
189
 
 
190
    m_connectionsButton = new Plasma::PushButton(m_rightWidget);
 
191
    m_connectionsButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
 
192
    m_connectionsButton->setIcon(KIcon("configure"));
 
193
    m_connectionsButton->setText(i18nc("manage connections button in the applet's popup", "Manage Connections..."));
 
194
    m_connectionsButton->setMinimumHeight(28);
 
195
    m_connectionsButton->setMaximumHeight(28);
 
196
    connect(m_connectionsButton, SIGNAL(clicked()), this, SLOT(manageConnections()));
 
197
 
 
198
    m_showMoreButton = new Plasma::PushButton(m_rightWidget);
 
199
    m_showMoreButton->setToolTip(i18nc("@info:tooltip tooltip for the 'Show More' button", "Show all available networks"));
 
200
    // Do not use this according to KDE HIG. Bug #272492
 
201
    //m_showMoreButton->setCheckable(true);
 
202
    m_showMoreButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
203
    m_showMoreButton->setIcon(KIcon("list-add"));
 
204
    m_showMoreButton->setText(i18nc("show more button in the applet's popup", "Show More..."));
 
205
    m_showMoreButton->setMinimumHeight(28);
 
206
    m_showMoreButton->setMaximumHeight(28);
 
207
    QSizeF sMore = m_showMoreButton->size();
 
208
    m_showMoreButton->setText(i18nc("show more button in the applet's popup", "Show Less..."));
 
209
    QSizeF sLess = m_showMoreButton->size();
 
210
    QSizeF sMax = sMore.width() > sLess.width() ? sMore : sLess;
 
211
    m_showMoreButton->setMinimumSize(sMax);
 
212
    m_showMoreButton->setMaximumSize(sMax);
 
213
    connect(m_showMoreButton, SIGNAL(clicked()), this, SLOT(showMore()));
 
214
    connect(m_activatables, SIGNAL(activatableAdded(RemoteActivatable *, int)), this, SLOT(uncheckShowMore(RemoteActivatable *)));
 
215
    connect(m_activatables, SIGNAL(activatableRemoved(RemoteActivatable *)), this, SLOT(checkShowMore(RemoteActivatable *)));
 
216
 
 
217
    QGraphicsLinearLayout* connectionLayout = new QGraphicsLinearLayout;
 
218
    connectionLayout->addItem(m_showMoreButton);
 
219
    connectionLayout->addItem(m_connectionsButton);
 
220
 
 
221
    m_rightLayout->addItem(connectionLayout);
 
222
 
 
223
    m_mainLayout->addItem(m_rightWidget, 1, 2);
 
224
    m_mainLayout->setColumnFixedWidth(2, m_showMoreButton->size().rwidth() + m_connectionsButton->size().rwidth());
 
225
 
 
226
    //createTab(Knm::Activatable::WirelessInterfaceConnection);
 
227
    kDebug() << "Adding interfaces initially";
 
228
    foreach (Solid::Control::NetworkInterfaceNm09 * iface, Solid::Control::NetworkManagerNm09::networkInterfaces()) {
 
229
        addInterfaceInternal(iface);
 
230
        kDebug() << "Network Interface:" << iface->interfaceName() << iface->driver() << iface->designSpeed();
 
231
    }
 
232
    addVpnInterface();
 
233
    // hook up signals to allow us to change the connection list depending on APs present, etc
 
234
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(networkInterfaceAdded(const QString&)),
 
235
            SLOT(interfaceAdded(const QString&)));
 
236
    connect(Solid::Control::NetworkManagerNm09::notifier(), SIGNAL(networkInterfaceRemoved(const QString&)),
 
237
            SLOT(interfaceRemoved(const QString&)));
 
238
 
 
239
    m_showMoreChecked = false;
 
240
    m_oldShowMoreChecked = true;
 
241
    wicCount = 0; // number of wireless networks which user explicitly configured using the kcm module.
 
242
 
 
243
    KNetworkManagerServicePrefs::instance(Knm::NETWORKMANAGEMENT_RCFILE);
 
244
    KConfigGroup config(KNetworkManagerServicePrefs::self()->config(), QLatin1String("General"));
 
245
    m_oldShowMoreChecked = config.readEntry(QLatin1String("ShowAllConnections"), true);
 
246
    showMore(m_oldShowMoreChecked);
 
247
 
 
248
    readConfig();
 
249
 
 
250
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
251
    dbus.connect("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement", "org.kde.Solid.PowerManagement", "resumingFromSuspend", this, SLOT(readConfig()));
 
252
    dbus.connect("org.kde.kded", "/org/kde/networkmanagement", "org.kde.networkmanagement", "ReloadConfig", this, SLOT(readConfig()));
 
253
 
 
254
    adjustSize();
 
255
}
 
256
 
 
257
static int compareVersions(const QString & version1, const QString & version2)
 
258
{
 
259
    QStringList sl1 = version1.split('.');
 
260
    QStringList sl2 = version2.split('.');
 
261
 
 
262
    if (sl1.size() > 2 && sl2.size() > 2) {
 
263
        int v1[3] = { sl1[0].toInt(), sl1[1].toInt(), sl1[2].toInt() };
 
264
        int v2[3] = { sl2[0].toInt(), sl2[1].toInt(), sl2[2].toInt() };
 
265
 
 
266
        if (v1[0] > v2[0]) {
 
267
            return 1;
 
268
        } else if (v1[0] < v2[0]) {
 
269
            return -1;
 
270
        } else if (v1[1] > v2[1]) {
 
271
            return 1;
 
272
        } else if (v1[1] < v2[1]) {
 
273
            return -1;
 
274
        } else if (v1[2] > v2[2]) {
 
275
            return 1;
 
276
        } else if (v1[2] < v2[2]) {
 
277
            return -1;
 
278
        } else {
 
279
            return 0;
 
280
        }
 
281
    }
 
282
    return 0;
 
283
}
 
284
 
 
285
void NMPopup::readConfig()
 
286
{
 
287
    kDebug();
 
288
    KNetworkManagerServicePrefs::self()->readConfig();
 
289
 
 
290
    m_networkingCheckBox->setChecked(Solid::Control::NetworkManagerNm09::isNetworkingEnabled());
 
291
    m_wifiCheckBox->setChecked(Solid::Control::NetworkManagerNm09::isWirelessEnabled());
 
292
 
 
293
    /* There is a bug in Solid < 4.6.2 where it does not emit the wirelessHardwareEnabledChanged signal.
 
294
     * So we always enable the wireless checkbox for versions below 4.6.2. */
 
295
#if KDE_IS_VERSION(4,6,2)
 
296
    m_wifiCheckBox->setEnabled(Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled());
 
297
#else
 
298
    m_wifiCheckBox->setEnabled(true);
 
299
#endif
 
300
 
 
301
    m_showMoreButton->setEnabled(Solid::Control::NetworkManagerNm09::isNetworkingEnabled() &&
 
302
                                 Solid::Control::NetworkManagerNm09::isWirelessEnabled());
 
303
 
 
304
    m_wwanCheckBox->nativeWidget()->setCheckState(Solid::Control::NetworkManagerNm09::isWwanEnabled() ? Qt::Checked : Qt::Unchecked);
 
305
    m_wwanCheckBox->setEnabled(Solid::Control::NetworkManagerNm09::isWwanHardwareEnabled());
 
306
 
 
307
    foreach(InterfaceItem * i, m_interfaces) {
 
308
        i->setNameDisplayMode(InterfaceItem::InterfaceName);
 
309
    }
 
310
 
 
311
    QString version = Solid::Control::NetworkManagerNm09::version();
 
312
    if (version.isEmpty()) {
 
313
        Plasma::Label * warning = new Plasma::Label(this);
 
314
        warning->setText(i18nc("Warning about wrong NetworkManager version", "NetworkManager is not running. Please start it."));
 
315
        m_interfaceLayout->addItem(warning);
 
316
    } else if (compareVersions(version, QString(MINIMUM_NM_VERSION_REQUIRED)) < 0) {
 
317
        Plasma::Label * warning = new Plasma::Label(this);
 
318
        warning->setText(i18nc("Warning about wrong NetworkManager version", "We need at least NetworkManager-%1 to work properly, found '%2'. Please upgrade to a newer version.", QString(MINIMUM_NM_VERSION_REQUIRED), version));
 
319
        m_interfaceLayout->addItem(warning);
 
320
    }
 
321
}
 
322
 
 
323
// Interfaces
 
324
void NMPopup::interfaceAdded(const QString& uni)
 
325
{
 
326
    if (m_interfaces.contains(uni)) {
 
327
        return;
 
328
    }
 
329
    Solid::Control::NetworkInterfaceNm09 * iface = Solid::Control::NetworkManagerNm09::findNetworkInterface(uni);
 
330
    if (iface) {
 
331
        kDebug() << "Interface Added:" << iface->interfaceName() << iface->driver() << iface->designSpeed();
 
332
        addInterfaceInternal(iface);
 
333
    }
 
334
}
 
335
 
 
336
void NMPopup::interfaceRemoved(const QString& uni)
 
337
{
 
338
    if (m_interfaces.contains(uni)) {
 
339
        // To prevent crashes when the interface removed is the one in interfaceDetailsWidget.
 
340
        // the m_iface pointer in interfaceDetailsWidget become invalid in this case.
 
341
        if (uni == m_interfaceDetailsWidget->getLastIfaceUni()) {
 
342
            m_interfaceDetailsWidget->setInterface(0, false);
 
343
            // Since it is invalid go back to "main" window.
 
344
            m_leftWidget->setCurrentIndex(0);
 
345
        }
 
346
 
 
347
        InterfaceItem* item = m_interfaces.take(uni);
 
348
        connect(item, SIGNAL(disappearAnimationFinished()), this, SLOT(deleteInterfaceItem()));
 
349
        item->disappear();
 
350
        updateHasWireless();
 
351
        updateHasWwan();
 
352
    }
 
353
}
 
354
 
 
355
void NMPopup::deleteInterfaceItem()
 
356
{
 
357
    // slot is called from animation's finished()
 
358
    InterfaceItem* item = dynamic_cast<InterfaceItem*>(sender());
 
359
    m_interfaceLayout->removeItem(item);
 
360
    delete item;
 
361
}
 
362
 
 
363
Solid::Control::NetworkInterfaceNm09* NMPopup::defaultInterface()
 
364
{
 
365
    // In fact we're returning the first available interface,
 
366
    // and if there is none available just the first one we have
 
367
    // and if we don't have one, 0. Make sure you check though.
 
368
    if (Solid::Control::NetworkManagerNm09::networkInterfaces().isEmpty()) {
 
369
        return 0;
 
370
    }
 
371
    Solid::Control::NetworkInterfaceNm09* iface = Solid::Control::NetworkManagerNm09::networkInterfaces().first();
 
372
    foreach (Solid::Control::NetworkInterfaceNm09 * _iface, Solid::Control::NetworkManagerNm09::networkInterfaces()) {
 
373
        switch (_iface->connectionState()) {
 
374
            case Solid::Control::NetworkInterfaceNm09::Disconnected:
 
375
            case Solid::Control::NetworkInterfaceNm09::Preparing:
 
376
            case Solid::Control::NetworkInterfaceNm09::Configuring:
 
377
            case Solid::Control::NetworkInterfaceNm09::NeedAuth:
 
378
            case Solid::Control::NetworkInterfaceNm09::IPConfig:
 
379
            case Solid::Control::NetworkInterfaceNm09::IPCheck:
 
380
            case Solid::Control::NetworkInterfaceNm09::Secondaries:
 
381
            case Solid::Control::NetworkInterfaceNm09::Activated:
 
382
            case Solid::Control::NetworkInterfaceNm09::Deactivating:
 
383
            case Solid::Control::NetworkInterfaceNm09::Failed:
 
384
                return _iface;
 
385
                break;
 
386
            case Solid::Control::NetworkInterfaceNm09::Unavailable:
 
387
            case Solid::Control::NetworkInterfaceNm09::Unmanaged:
 
388
            case Solid::Control::NetworkInterfaceNm09::UnknownState:
 
389
            default:
 
390
                break;
 
391
        }
 
392
    }
 
393
    return iface;
 
394
}
 
395
 
 
396
void NMPopup::addInterfaceInternal(Solid::Control::NetworkInterfaceNm09* iface)
 
397
{
 
398
    if (!iface) {
 
399
        // the interface might be gone in the meantime...
 
400
        return;
 
401
    }
 
402
    if (!m_interfaces.contains(iface->uni())) {
 
403
        InterfaceItem * ifaceItem = 0;
 
404
        if (iface->type() == Solid::Control::NetworkInterfaceNm09::Wifi) {
 
405
            // Create the wireless interface item
 
406
            WirelessInterfaceItem* wifiItem = 0;
 
407
            wifiItem = new WirelessInterfaceItem(static_cast<Solid::Control::WirelessNetworkInterfaceNm09 *>(iface), m_activatables, InterfaceItem::InterfaceName, this);
 
408
            ifaceItem = wifiItem;
 
409
            //connect(wirelessinterface, SIGNAL(stateChanged()), this, SLOT(updateNetworks()));
 
410
            wifiItem->setEnabled(Solid::Control::NetworkManagerNm09::isWirelessEnabled());
 
411
            kDebug() << "WiFi added";
 
412
            connect(wifiItem, SIGNAL(disconnectInterfaceRequested(const QString&)), m_connectionList, SLOT(deactivateConnection(const QString&)));
 
413
        } else {
 
414
            // Create the interfaceitem
 
415
            ifaceItem = new InterfaceItem(static_cast<Solid::Control::WiredNetworkInterfaceNm09 *>(iface), m_activatables, InterfaceItem::InterfaceName, this);
 
416
            connect(ifaceItem, SIGNAL(disconnectInterfaceRequested(const QString&)), m_connectionList, SLOT(deactivateConnection(const QString&)));
 
417
        }
 
418
        connect(ifaceItem, SIGNAL(clicked()), this, SLOT(toggleInterfaceTab()));
 
419
        connect(ifaceItem, SIGNAL(clicked(Solid::Control::NetworkInterfaceNm09*)),
 
420
                m_connectionList,  SLOT(addInterface(Solid::Control::NetworkInterfaceNm09*)));
 
421
        connect(ifaceItem, SIGNAL(hoverEnter(const QString&)), m_connectionList, SLOT(hoverEnter(const QString&)));
 
422
        connect(ifaceItem, SIGNAL(hoverLeave(const QString&)), m_connectionList, SLOT(hoverLeave(const QString&)));
 
423
 
 
424
        // Catch connection changes
 
425
        connect(iface, SIGNAL(connectionStateChanged(int,int,int)), this, SLOT(handleConnectionStateChange(int,int,int)));
 
426
        m_interfaceLayout->addItem(ifaceItem);
 
427
        m_interfaces.insert(iface->uni(), ifaceItem);
 
428
    }
 
429
    updateHasWireless();
 
430
    updateHasWwan();
 
431
}
 
432
 
 
433
void NMPopup::showInterfaceDetails(const QString & uni)
 
434
{
 
435
    InterfaceItem * ifaceItem = m_interfaces.value(uni, 0);
 
436
    if (!ifaceItem) {
 
437
        if (m_vpnItem) {
 
438
            ifaceItem = m_vpnItem;
 
439
        } else {
 
440
            return;
 
441
        }
 
442
    }
 
443
    QMetaObject::invokeMethod(ifaceItem, "clicked", Qt::QueuedConnection);
 
444
}
 
445
 
 
446
void NMPopup::addVpnInterface()
 
447
{
 
448
    m_vpnItem = new VpnInterfaceItem(0, m_activatables, InterfaceItem::InterfaceName, this);
 
449
    connect(m_vpnItem, SIGNAL(clicked()), this, SLOT(toggleInterfaceTab()));
 
450
    connect(m_vpnItem, SIGNAL(clicked(Solid::Control::NetworkInterfaceNm09*)),
 
451
            m_connectionList,  SLOT(addInterface(Solid::Control::NetworkInterfaceNm09*)));
 
452
    connect(m_vpnItem, SIGNAL(hoverEnter()), m_connectionList, SLOT(vpnHoverEnter()));
 
453
    connect(m_vpnItem, SIGNAL(hoverLeave()), m_connectionList, SLOT(vpnHoverLeave()));
 
454
 
 
455
    connect(m_vpnItem, SIGNAL(clicked()), m_connectionList, SLOT(toggleVpn()));
 
456
 
 
457
    m_leftLayout->insertItem(2, m_vpnItem);
 
458
}
 
459
 
 
460
void NMPopup::handleConnectionStateChange(int new_state, int old_state, int reason)
 
461
{
 
462
    Q_UNUSED( reason );
 
463
    // Switch to default tab if an interface has become available, or unavailable
 
464
    if (available(new_state) != available(old_state)) {
 
465
        m_connectionList->clearInterfaces();
 
466
    }
 
467
}
 
468
 
 
469
bool NMPopup::hasWireless() {
 
470
    return m_hasWirelessInterface;
 
471
}
 
472
 
 
473
bool NMPopup::available(int state)
 
474
{
 
475
    // Can an interface be used?
 
476
    switch (state) {
 
477
        case Solid::Control::NetworkInterfaceNm09::Disconnected:
 
478
        case Solid::Control::NetworkInterfaceNm09::Preparing:
 
479
        case Solid::Control::NetworkInterfaceNm09::Configuring:
 
480
        case Solid::Control::NetworkInterfaceNm09::NeedAuth:
 
481
        case Solid::Control::NetworkInterfaceNm09::IPConfig:
 
482
        case Solid::Control::NetworkInterfaceNm09::IPCheck:
 
483
        case Solid::Control::NetworkInterfaceNm09::Secondaries:
 
484
        case Solid::Control::NetworkInterfaceNm09::Activated:
 
485
        case Solid::Control::NetworkInterfaceNm09::Deactivating:
 
486
        case Solid::Control::NetworkInterfaceNm09::Failed:
 
487
            return true;
 
488
            break;
 
489
        case Solid::Control::NetworkInterfaceNm09::Unavailable:
 
490
        case Solid::Control::NetworkInterfaceNm09::Unmanaged:
 
491
        case Solid::Control::NetworkInterfaceNm09::UnknownState:
 
492
        default:
 
493
            return false;
 
494
            break;
 
495
    }
 
496
    return false;
 
497
}
 
498
 
 
499
void NMPopup::wirelessEnabledToggled(bool checked)
 
500
{
 
501
    kDebug() << "Applet wireless enable switch toggled" << checked;
 
502
    if (Solid::Control::NetworkManagerNm09::isWirelessEnabled() != checked) {
 
503
        Solid::Control::NetworkManagerNm09::setWirelessEnabled(checked);
 
504
    }
 
505
    updateHasWireless(checked);
 
506
}
 
507
 
 
508
void NMPopup::wwanEnabledToggled(bool checked)
 
509
{
 
510
    kDebug() << "Applet wwan enable switch toggled" << checked;
 
511
    if (Solid::Control::NetworkManagerNm09::isWwanEnabled() != checked) {
 
512
        Solid::Control::NetworkManagerNm09::setWwanEnabled(checked);
 
513
    }
 
514
}
 
515
 
 
516
void NMPopup::networkingEnabledToggled(bool checked)
 
517
{
 
518
    // Switch networking on / off
 
519
    if (Solid::Control::NetworkManagerNm09::isNetworkingEnabled() != checked) {
 
520
        Solid::Control::NetworkManagerNm09::setNetworkingEnabled(checked);
 
521
    }
 
522
    // Update wireless checkbox
 
523
    m_wifiCheckBox->setChecked(Solid::Control::NetworkManagerNm09::isWirelessEnabled());
 
524
 
 
525
kDebug() << "Wireless hardware enabled ==" << Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled();
 
526
 
 
527
#if KDE_IS_VERSION(4,6,2)
 
528
    m_wifiCheckBox->setEnabled(Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled() || Solid::Control::NetworkManagerNm09::isWirelessEnabled());
 
529
#endif
 
530
 
 
531
    m_wwanCheckBox->nativeWidget()->setCheckState(Solid::Control::NetworkManagerNm09::isWwanEnabled() ? Qt::Checked : Qt::Unchecked);
 
532
    m_wwanCheckBox->setEnabled(Solid::Control::NetworkManagerNm09::isWwanHardwareEnabled() || Solid::Control::NetworkManagerNm09::isWwanEnabled());
 
533
 
 
534
    updateHasWireless(checked);
 
535
}
 
536
 
 
537
void NMPopup::updateHasWireless(bool checked)
 
538
{
 
539
    //kDebug() << "UPDATE!!!!!!!!!!!!";
 
540
    bool hasWireless = true;
 
541
    if (!Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled() ||
 
542
        !Solid::Control::NetworkManagerNm09::isNetworkingEnabled() ||
 
543
        !Solid::Control::NetworkManagerNm09::isWirelessEnabled() || !checked) {
 
544
 
 
545
        //kDebug () << "networking enabled?" << Solid::Control::NetworkManagerNm09::isNetworkingEnabled();
 
546
        //kDebug () << "wireless hardware enabled?" << Solid::Control::NetworkManagerNm09::isWirelessHardwareEnabled();
 
547
        //kDebug () << "wireless enabled?" << Solid::Control::NetworkManagerNm09::isWirelessEnabled();
 
548
 
 
549
        // either networking is disabled, or wireless is disabled
 
550
        hasWireless = false;
 
551
    }
 
552
    //solid is too slow, we need to see if the checkbox was checked by the user
 
553
    if (checked)
 
554
        hasWireless = true;
 
555
    kDebug() << "After chckboxn" << hasWireless;
 
556
 
 
557
    foreach (InterfaceItem* ifaceitem, m_interfaces) {
 
558
        Solid::Control::NetworkInterfaceNm09* iface = ifaceitem->interface();
 
559
        if (iface && iface->type() == Solid::Control::NetworkInterfaceNm09::Wifi) {
 
560
            //kDebug() << "there's a wifi iface" << ifaceitem->connectionName() << iface->interfaceName();
 
561
            m_hasWirelessInterface = true; // at least one interface is wireless. We're happy.
 
562
            m_wifiCheckBox->show();
 
563
            break;
 
564
        }
 
565
    }
 
566
    if (!m_hasWirelessInterface) {
 
567
        kDebug() << "no ifaces";
 
568
        hasWireless = false;
 
569
        m_wifiCheckBox->hide();
 
570
    }
 
571
    m_connectionList->setHasWireless(hasWireless);
 
572
}
 
573
 
 
574
void NMPopup::updateHasWwan()
 
575
{
 
576
    bool hasWwan = false;
 
577
    foreach (InterfaceItem* ifaceitem, m_interfaces) {
 
578
        Solid::Control::ModemNetworkInterfaceNm09* iface = qobject_cast<Solid::Control::ModemNetworkInterfaceNm09 *>(ifaceitem->interface());
 
579
        if (iface) {
 
580
            hasWwan = true;
 
581
            break;
 
582
        }
 
583
    }
 
584
    if (hasWwan) {
 
585
        m_wwanCheckBox->show();
 
586
        hasWwan = m_wwanCheckBox->isChecked();
 
587
    } else {
 
588
        m_wwanCheckBox->hide();
 
589
    }
 
590
}
 
591
 
 
592
void NMPopup::managerWirelessEnabledChanged(bool enabled)
 
593
{
 
594
    kDebug() << "NM daemon changed wireless enable state" << enabled;
 
595
    // it might have changed because we toggled the switch,
 
596
    // but it might have been changed externally, so set it anyway
 
597
    m_wifiCheckBox->setChecked(enabled);
 
598
    if (enabled) {
 
599
        m_wifiCheckBox->setEnabled(enabled);
 
600
    }
 
601
 
 
602
    m_showMoreButton->setEnabled(enabled && Solid::Control::NetworkManagerNm09::isNetworkingEnabled());
 
603
}
 
604
 
 
605
void NMPopup::managerWirelessHardwareEnabledChanged(bool enabled)
 
606
{
 
607
    kDebug() << "Hardware wireless enable switch state changed" << enabled;
 
608
    m_wifiCheckBox->setEnabled(enabled);
 
609
    updateHasWireless(enabled);
 
610
    m_showMoreButton->setEnabled(enabled && Solid::Control::NetworkManagerNm09::isNetworkingEnabled());
 
611
}
 
612
 
 
613
void NMPopup::managerNetworkingEnabledChanged(bool enabled)
 
614
{
 
615
    kDebug() << "NM daemon changed networking enable state" << enabled;
 
616
    m_networkingCheckBox->setChecked(enabled);
 
617
    m_showMoreButton->setEnabled(enabled);
 
618
}
 
619
 
 
620
void NMPopup::managerWwanEnabledChanged(bool enabled)
 
621
{
 
622
    kDebug() << "NM daemon changed wwan enable state" << enabled;
 
623
    m_wwanCheckBox->nativeWidget()->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
 
624
    if (enabled) {
 
625
        m_wwanCheckBox->setEnabled(enabled);
 
626
    }
 
627
}
 
628
 
 
629
void NMPopup::managerWwanHardwareEnabledChanged(bool enabled)
 
630
{
 
631
    kDebug() << "Hardware wwan enable switch state changed" << enabled;
 
632
    m_wwanCheckBox->setEnabled(enabled);
 
633
}
 
634
 
 
635
void NMPopup::showMore()
 
636
{
 
637
    // Simulate button toggling.
 
638
    m_showMoreChecked = !m_showMoreChecked;
 
639
    m_oldShowMoreChecked = m_showMoreChecked;
 
640
    showMore(m_oldShowMoreChecked);
 
641
 
 
642
    KConfigGroup config(KNetworkManagerServicePrefs::self()->config(), QLatin1String("General"));
 
643
    config.writeEntry(QLatin1String("ShowAllConnections"), m_oldShowMoreChecked);
 
644
    config.sync();
 
645
}
 
646
 
 
647
void NMPopup::showMore(bool more)
 
648
{
 
649
    if (!Solid::Control::NetworkManagerNm09::isWirelessEnabled()) {
 
650
        more = false;
 
651
    }
 
652
 
 
653
    if (more) {
 
654
        m_showMoreButton->setText(i18nc("pressed show more button", "Show Less..."));
 
655
        m_showMoreButton->setIcon(KIcon("list-remove"));
 
656
        m_showMoreChecked = true;
 
657
        m_connectionList->setShowAllTypes(true, true); // also refresh list
 
658
    } else {
 
659
        m_showMoreButton->setText(i18nc("unpressed show more button", "Show More..."));
 
660
        m_showMoreChecked = false;
 
661
        m_connectionList->setShowAllTypes(false, true); // also refresh list
 
662
        m_showMoreButton->setIcon(KIcon("list-add"));
 
663
    }
 
664
    kDebug() << m_showMoreButton->text();
 
665
}
 
666
 
 
667
void NMPopup::checkShowMore(RemoteActivatable * ra)
 
668
{
 
669
    RemoteWirelessInterfaceConnection * wic = qobject_cast<RemoteWirelessInterfaceConnection*>(ra);
 
670
    if (wic) {
 
671
        if (wic->operationMode() == Solid::Control::WirelessNetworkInterfaceNm09::Adhoc &&
 
672
            wic->activationState() == Knm::InterfaceConnection::Unknown) {
 
673
            return;
 
674
        }
 
675
        if (wicCount > 0) {
 
676
            wicCount--;
 
677
        }
 
678
        if (wicCount == 0 && !m_showMoreChecked) {
 
679
            // There is no wireless network around which the user has explicitly configured
 
680
            // so temporaly show all wifi available networks.
 
681
            showMore(true);
 
682
        }
 
683
    }
 
684
}
 
685
 
 
686
void NMPopup::uncheckShowMore(RemoteActivatable *ra)
 
687
{
 
688
    RemoteWirelessInterfaceConnection * wic = qobject_cast<RemoteWirelessInterfaceConnection*>(ra);
 
689
    if (wic) {
 
690
        if (wic->operationMode() == Solid::Control::WirelessNetworkInterfaceNm09::Adhoc &&
 
691
            wic->activationState() == Knm::InterfaceConnection::Unknown) {
 
692
            return;
 
693
        }
 
694
        wicCount++;
 
695
        if (m_oldShowMoreChecked != m_showMoreChecked) {
 
696
            // One wireless network explicitly configured by the user appeared, reset "Show More" button
 
697
            // state to the value before the checkShowMore method above took action.
 
698
            showMore(m_oldShowMoreChecked);
 
699
        }
 
700
    }
 
701
}
 
702
 
 
703
void NMPopup::manageConnections()
 
704
{
 
705
    //kDebug() << "opening connection management dialog";
 
706
    QStringList args;
 
707
    args << "--icon" << "networkmanager" << "kcm_networkmanagement" << "kcm_networkmanagement_tray";
 
708
    KToolInvocation::kdeinitExec("kcmshell4", args);
 
709
}
 
710
 
 
711
void NMPopup::toggleInterfaceTab()
 
712
{
 
713
    InterfaceItem* item = qobject_cast<InterfaceItem*>(sender());
 
714
    if (item) {
 
715
        m_interfaceDetailsWidget->setInterface(item->interface());
 
716
    }
 
717
 
 
718
    if (m_leftWidget->currentIndex() == 0) {
 
719
        // Enable / disable updating of the details widget
 
720
        m_interfaceDetailsWidget->setUpdateEnabled(true);
 
721
 
 
722
        if (item) {
 
723
            if (item->interface()) {
 
724
                // Temporaly disables hightlight for all connections of this interface.
 
725
                QMetaObject::invokeMethod(item, "hoverLeave", Qt::QueuedConnection,
 
726
                                          Q_ARG(QString, item->interface()->uni()));
 
727
 
 
728
                m_leftLabel->setText(QString("<h3>%1</h3>").arg(
 
729
                                    UiUtils::interfaceNameLabel(item->interface()->uni())));
 
730
            } else {
 
731
                // Temporaly disables hightlight for all VPN connections.
 
732
                QMetaObject::invokeMethod(item, "hoverLeave", Qt::QueuedConnection);
 
733
            }
 
734
        }
 
735
        showMore(true);
 
736
 
 
737
        // Hack to prevent graphical artifact during tab transition.
 
738
        // m_interfaceDetailsWidget will be shown again when transition finishes.
 
739
        m_interfaceDetailsWidget->hide();
 
740
        m_leftWidget->setCurrentIndex(1);
 
741
    } else {
 
742
        m_leftLabel->setText(i18nc("title on the LHS of the plasmoid", "<h3>Interfaces</h3>"));
 
743
        m_connectionList->clearInterfaces();
 
744
        showMore(m_oldShowMoreChecked);
 
745
        m_interfaceDetailsWidget->setUpdateEnabled(false);
 
746
        m_leftWidget->setCurrentIndex(0);
 
747
    }
 
748
}
 
749
 
 
750
void NMPopup::refresh()
 
751
{
 
752
    m_interfaceDetailsWidget->show();
 
753
}
 
754
// vim: sw=4 sts=4 et tw=100
 
755