File: wicdengine.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 (173 lines) | stat: -rw-r--r-- 5,805 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/****************************************************************************
 *  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 "wicdengine.h"
#include "wicdservice.h"

WicdEngine::WicdEngine(QObject* parent, const QVariantList& args)
    : Plasma::DataEngine(parent)
{
    Q_UNUSED(args)

    setMinimumPollingInterval(1000);

    connect(DBusHandler::instance(), SIGNAL(statusChange(Status)), this, SLOT(updateStatus(Status)));
    connect(DBusHandler::instance(), SIGNAL(scanStarted()), this, SLOT(scanStarted()));
    connect(DBusHandler::instance(), SIGNAL(scanEnded()), this, SLOT(scanEnded()));
    connect(DBusHandler::instance(), SIGNAL(launchChooser()), this, SLOT(profileNeeded()));
    connect(DBusHandler::instance(), SIGNAL(chooserLaunched()), this, SLOT(profileNotNeeded()));
    connect(DBusHandler::instance(), SIGNAL(connectionResultSend(QString)), this, SLOT(resultReceived(QString)));
}

void WicdEngine::init()
{
    m_scanning = false;
    m_profileNeeded = false;

    //we need a current profile
    QString profile = DBusHandler::instance()->callWired("GetDefaultWiredNetwork").toString();
    if (profile.isEmpty())
        profile = DBusHandler::instance()->callWired("GetWiredProfileList").toStringList().at(0);
    DBusHandler::instance()->setCurrentProfile(profile);

    //check if the profile manager is needed
    if (DBusHandler::instance()->callDaemon("GetNeedWiredProfileChooser").toBool()) {
        profileNeeded();
    }

    //force first status update
    forceUpdateStatus();
}

Plasma::Service *WicdEngine::serviceForSource(const QString &source)
{
    Q_UNUSED(source)
    return new WicdService(this, DBusHandler::instance());
}

QStringList WicdEngine::sources() const
{
    QStringList sources;
    sources<<"networks";
    sources<<"status";
    sources<<"daemon";
    return sources;
}

bool WicdEngine::sourceRequestEvent(const QString &source)
{
    if (source == "networks") {
        updateSourceEvent(source);
        return true;
    }
    if (source == "status") {
        updateSourceEvent(source);
        return true;
    }
    if (source == "daemon") {
        updateSourceEvent(source);
        return true;
    }
    return false;
}

bool WicdEngine::updateSourceEvent(const QString &source)
{
    if (source == "networks") {
        //reset
        removeAllData(source);
        //populate new data
        QMap<int, NetworkInfo> list = DBusHandler::instance()->networksList();
        QMap<int, NetworkInfo>::const_iterator it = list.constBegin();
        while (it != list.constEnd()) {
            setData(source, QString::number(it.key()), it.value());
            ++it;
        }
        return true;
    }
    if (source == "status") {
        setData(source, "state", m_status.State);
        setData(source, "info", m_status.Info);
        setData(source, "message", m_message);
        setData(source, "interface", m_interface);
        return true;
    }
    if (source == "daemon") {
        setData(source, "profileNeeded", m_profileNeeded);
        setData(source, "scanning", m_scanning);
        setData(source, "connectionResult", m_connectionResult);
        //to simulate a "signal-like" behaviour
        m_connectionResult = "";
        return true;
    }
    return false;
}

void WicdEngine::updateStatus(Status status)
{
    m_interface = DBusHandler::instance()->callDaemon("GetCurrentInterface").toString();
    if (status.State == WicdState::CONNECTING) {
        bool wired = (status.Info.at(0)=="wired");
        if (wired) {
            m_message = DBusHandler::instance()->callWired("CheckWiredConnectingMessage").toString();
        } else {
            m_message = DBusHandler::instance()->callWireless("CheckWirelessConnectingMessage").toString();
        }
        QTimer::singleShot(500, this, SLOT(forceUpdateStatus()));
    }
    m_status = status;
    updateSourceEvent("status");
}

void WicdEngine::forceUpdateStatus()
{
    updateStatus(DBusHandler::instance()->status());
}

void WicdEngine::profileNeeded()
{
    m_profileNeeded = true;
    updateSourceEvent("daemon");
}

void WicdEngine::profileNotNeeded()
{
    m_profileNeeded = false;
    updateSourceEvent("daemon");
}

void WicdEngine::scanStarted()
{
    m_scanning = true;
    updateSourceEvent("daemon");
}

void WicdEngine::scanEnded()
{
    m_scanning = false;
    updateSourceEvent("daemon");
}

void WicdEngine::resultReceived(const QString& result)
{
    m_connectionResult = result;
    updateSourceEvent("daemon");
}

#include "wicdengine.moc"