~pete-woods/libqtdbustest/valgrind-present-1263925

« back to all changes in this revision

Viewing changes to src/NetworkPrompt.cpp

  • Committer: Pete Woods
  • Date: 2013-08-01 15:17:32 UTC
  • Revision ID: pete.woods@canonical.com-20130801151732-ee1g57uzg8ycaikg
Add code to construct gmenumodels

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (C) 2013 Canonical, Ltd.
3
3
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; version 3.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 *
16
16
 * Author: Pete Woods <pete.woods@canonical.com>
17
17
 */
18
18
 
19
 
#include <NetworkManagerDeviceInterface.h>
20
19
#include <NetworkManagerDeviceWirelessInterface.h>
 
20
#include <NetworkManagerAccessPointInterface.h>
21
21
#include <NetworkPrompt.h>
 
22
#include <WiFiMenu.h>
22
23
 
23
24
#include <NetworkManager.h>
24
25
 
26
27
                const QDBusConnection &systemConnection, QObject *parent) :
27
28
                QObject(parent), m_sessionConnection(sessionConnection), m_systemConnection(
28
29
                                systemConnection), m_networkManager(NM_DBUS_SERVICE,
29
 
                                NM_DBUS_PATH, m_systemConnection), m_systemDialog(
30
 
                                "com.canonical.Unity.SystemDialog",
31
 
                                "/com/canonical/Unity/SystemDialog", m_sessionConnection) {
 
30
                                NM_DBUS_PATH, m_systemConnection) {
32
31
}
33
32
 
34
33
NetworkPrompt::~NetworkPrompt() {
35
34
}
36
35
 
 
36
QDBusObjectPath NetworkPrompt::activateAccessPoint(
 
37
                const QDBusObjectPath& accessPointPath,
 
38
                const OrgFreedesktopNetworkManagerDeviceInterface& device,
 
39
                const QDBusObjectPath& devicePath) {
 
40
 
 
41
        qDebug() << "access point: " << accessPointPath.path();
 
42
        QList<QDBusObjectPath> connectionPaths(device.availableConnections());
 
43
 
 
44
        if (connectionPaths.empty()) {
 
45
                // if no existing connections, then add a new one
 
46
                return m_networkManager.AddAndActivateConnection(QVariantMap(),
 
47
                                devicePath, accessPointPath);
 
48
        } else {
 
49
                // we already have a connection, so just activate it
 
50
                return m_networkManager.ActivateConnection(connectionPaths.first(),
 
51
                                devicePath, accessPointPath);
 
52
        }
 
53
}
 
54
 
37
55
void NetworkPrompt::check() {
38
56
        // not on internet
39
57
        // and wi-fi is on
51
69
 
52
70
                QList<QDBusObjectPath> devicePaths(m_networkManager.GetDevices());
53
71
 
 
72
                WiFiMenuPtr wifiMenu(new WiFiMenu());
 
73
 
54
74
                for (const QDBusObjectPath &devicePath : devicePaths) {
55
 
                        OrgFreedesktopNetworkManagerDeviceInterface device(NM_DBUS_SERVICE,
56
 
                                        devicePath.path(), m_systemConnection);
57
 
                        if (device.deviceType() == NM_DEVICE_TYPE_WIFI) {
 
75
                        OrgFreedesktopNetworkManagerDeviceInterface deviceInterface(
 
76
                                        NM_DBUS_SERVICE, devicePath.path(), m_systemConnection);
 
77
                        if (deviceInterface.deviceType() == NM_DEVICE_TYPE_WIFI) {
58
78
 
59
 
                                OrgFreedesktopNetworkManagerDeviceWirelessInterface wifiDevice(
 
79
                                OrgFreedesktopNetworkManagerDeviceWirelessInterface wifiDeviceInterface(
60
80
                                                NM_DBUS_SERVICE, devicePath.path(), m_systemConnection);
61
81
 
 
82
                                DevicePtr device(new Device());
 
83
                                device->setPath(devicePath.path().toStdString());
 
84
                                wifiMenu->addDevice(device);
 
85
 
62
86
                                QDBusPendingReply<void> reply(
63
 
                                                wifiDevice.RequestScan(QVariantMap()));
 
87
                                                wifiDeviceInterface.RequestScan(QVariantMap()));
 
88
                                reply.waitForFinished();
 
89
 
64
90
                                QList<QDBusObjectPath> accessPointPaths(
65
 
                                                wifiDevice.GetAccessPoints());
 
91
                                                wifiDeviceInterface.GetAccessPoints());
66
92
 
67
93
                                for (const QDBusObjectPath &accessPointPath : accessPointPaths) {
68
 
                                        qDebug() << "access point: " << accessPointPath.path();
69
 
                                        QList<QDBusObjectPath> connectionPaths(
70
 
                                                        device.availableConnections());
71
 
                                        if (connectionPaths.empty()) {
72
 
                                                // if no existing connections, then add a new one
73
 
                                                QDBusObjectPath networkConnection(
74
 
                                                                m_networkManager.AddAndActivateConnection(
75
 
                                                                                QVariantMap(), devicePath,
76
 
                                                                                accessPointPath));
77
 
                                        } else {
78
 
                                                // we already have a connection, so just activate it
79
 
                                                QDBusObjectPath networkConnection(
80
 
                                                                m_networkManager.ActivateConnection(
81
 
                                                                                connectionPaths.first(), devicePath,
82
 
                                                                                accessPointPath));
83
 
                                        }
 
94
                                        OrgFreedesktopNetworkManagerAccessPointInterface accessPointInterface(
 
95
                                                        NM_DBUS_SERVICE, accessPointPath.path(),
 
96
                                                        m_systemConnection);
 
97
 
 
98
                                        AccessPointPtr accessPoint(new AccessPoint());
 
99
                                        accessPoint->setSsid(accessPointInterface.ssid().data());
 
100
                                        accessPoint->setAdhoc(
 
101
                                                        accessPointInterface.mode()
 
102
                                                                        == NM_802_11_MODE_ADHOC);
 
103
                                        accessPoint->setSecure(
 
104
                                                        accessPointInterface.flags()
 
105
                                                                        == NM_802_11_AP_FLAGS_PRIVACY);
 
106
                                        accessPoint->setBssid(
 
107
                                                        accessPointInterface.hwAddress().toStdString());
 
108
                                        accessPoint->setPath(accessPointPath.path().toStdString());
 
109
                                        device->addAccessPoint(accessPoint);
84
110
                                }
 
111
 
85
112
                        }
86
113
                }
 
114
 
 
115
                MenuExporter exporter(wifiMenu);
 
116
//              activateAccessPoint(accessPointPath, device, devicePath);
87
117
        }
88
118
}
89
119