File: networkview.cpp

package info (click to toggle)
wicd-kde 0.3.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,000 kB
  • sloc: cpp: 2,963; sh: 3; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 4,807 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/****************************************************************************
 *  Copyright (c) 2011 Anthony Vital <anthony.vital@gmail.com>              *
 *                                                                          *
 *  This file is part of Wicd Client KDE.                                   *
 *                                                                          *
 *  Wicd Client KDE is free software: you can redistribute it and/or modify *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation, either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  Wicd Client KDE is distributed in the hope that it will be useful,      *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with Wicd Client KDE.  If not, see <http://www.gnu.org/licenses/>.*
 ****************************************************************************/

#include "networkview.h"
#include "wirednetworkitem.h"
#include "wirelessnetworkitem.h"

#include <Plasma/DataEngineManager>

NetworkView::NetworkView( QGraphicsItem *parent )
    : QGraphicsWidget( parent ),
      m_currentNetworkItem(0)
{
    setAcceptHoverEvents(true);
    installEventFilter(this);
    m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
    m_layout->setSpacing(2.0);
    m_layout->setContentsMargins(0, 0, 0, 0);
    
    m_itemBackground = new Plasma::ItemBackground(this);
    m_itemBackground->setTargetItem(0);

    m_controller = engine()->serviceForSource("");
}

NetworkView::~NetworkView()
{
}

void NetworkView::loadNetworks()
{
    //delete old list
    while (m_networkItemList.count() > 0) {
        NetworkItem *oldItem = m_networkItemList.at(0);
        m_layout->removeItem(oldItem);
        m_networkItemList.removeAt(0);
        oldItem->deleteLater();
    }

    //get new data
    Plasma::DataEngine::Data list = engine()->query("networks");
    //Store the data in a QMap with int key
    QMap<int, NetworkInfo> networkMap;
    Plasma::DataEngine::Data::const_iterator i = list.constBegin();
    while (i != list.constEnd()) {
        networkMap.insert(i.key().toInt(), i.value().toHash());
        ++i;
    }

    //populate new list
    QMap<int, NetworkInfo>::const_iterator it = networkMap.constBegin();
    m_currentNetworkItem = 0;
    while (it != networkMap.constEnd()) {
        NetworkItem* item;
        NetworkInfo info = it.value();
        bool isWired = (info.value("networkId").toInt() == -1);
        if (isWired) {
            WiredNetworkItem* wireditem = new WiredNetworkItem(it.value(), this);
            item = wireditem;
        } else {
            WirelessNetworkItem* wirelessitem = new WirelessNetworkItem(it.value(), this);
            item = wirelessitem;
        }
        connect(item, SIGNAL(toggled(int)), this, SLOT(toggleConnection(int)));
        if (info.value("connected").toBool()) {
            m_currentNetworkItem = item;
        }
        item->installEventFilter(this);
        m_networkItemList.append(item);
        m_layout->addItem(item);
        ++it;
    }
    updateGeometry();
}

bool NetworkView::eventFilter(QObject* obj, QEvent *event)
{
    NetworkItem *item = qobject_cast<NetworkItem*>(obj);
    if (item) {
        switch (event->type()) {
        case QEvent::GraphicsSceneHoverEnter:
            highlightItem(item);
            break;
        default:
            break;
        }
    }
    QGraphicsWidget *w = qobject_cast<QGraphicsWidget*>(obj);
    if (w == this && event->type() == QEvent::GraphicsSceneHoverLeave) {
        highlightItem(0);
    }

    return false;
}

void NetworkView::showSignalStrength(bool show)
{
    WirelessNetworkItem::showStrength(show);
}

NetworkItem* NetworkView::currentNetworkItem() const
{
    return m_currentNetworkItem;
}

void NetworkView::toggleConnection(int networkId)
{
    KConfigGroup op = m_controller->operationDescription("toggleConnection");
    op.writeEntry("networkId", networkId);
    m_controller->startOperationCall(op);
}

void NetworkView::highlightItem(QGraphicsItem* item)
{
    m_itemBackground->setTargetItem(item);
}

Plasma::DataEngine* NetworkView::engine()
{
    Plasma::DataEngine *e = Plasma::DataEngineManager::self()->engine("wicd");
    if (e->isValid()) {
        return e;
    } else {
        return 0;
    }
}