on e82caeb help sign in
146 lines 4.9 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Alberto Ruiz <alberto.ruiz@canonical.com>
18 * Renato Araujo Oliveira Filho <renato@canonical.com>
19 */
21#include "networkagent.h"
23#include <QVariant>
24#include <QApplication>
25#include <QDebug>
27class NetworkAgentToken
29public:
30 NMConnection *connection;
31 NMSettingWirelessSecurity *wisec;
32 guint id;
33 QByteArray keyMgmt;
35 NetworkAgentToken(NMConnection *connection,
36 NMSettingWirelessSecurity *wisec,
37 guint id,
38 QByteArray keyMgmt)
39 : connection(connection),
40 wisec(wisec),
41 id(id),
42 keyMgmt(keyMgmt)
43 {
44 if (connection) {
45 g_object_ref(connection);
46 } else {
47 qWarning() << "invalid connection object";
48 }
49 }
51 ~NetworkAgentToken()
52 {
53 if (connection) {
54 g_object_unref(connection);
55 }
56 }
57};
59NetworkAgent::NetworkAgent(QObject *parent)
60 : QObject(parent)
62 m_agent = unity_settings_secret_agent_new();
63 g_signal_connect(G_OBJECT(m_agent),
64 UNITY_SETTINGS_SECRET_AGENT_SECRET_REQUESTED,
65 G_CALLBACK(onSecretRequested),
66 this);
68 g_signal_connect(G_OBJECT(m_agent),
69 UNITY_SETTINGS_SECRET_AGENT_REQUEST_CANCELLED,
70 G_CALLBACK(onSecretRequestCancelled),
71 this);
74NetworkAgent::~NetworkAgent()
76 nm_secret_agent_unregister(NM_SECRET_AGENT(m_agent));
77 g_object_unref(m_agent);
80void NetworkAgent::authenticate(const QVariant &token, const QString &key)
82 NetworkAgentToken *pToken = (NetworkAgentToken *) token.value<void *>();
83 if (pToken == NULL) {
84 return;
85 }
87 if ((pToken->keyMgmt == "wpa-none") || (pToken->keyMgmt == "wpa-psk")) {
88 g_object_set(G_OBJECT(pToken->wisec),
89 NM_SETTING_WIRELESS_SECURITY_PSK, qPrintable(key),
90 NULL);
91 } else if (pToken->keyMgmt == "none") {
92 g_object_set(G_OBJECT(pToken->wisec),
93 NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, qPrintable(key),
94 NULL);
95 }
97 GHashTable *settings = nm_connection_to_hash(pToken->connection,
98 NM_SETTING_HASH_FLAG_ALL);
100 unity_settings_secret_agent_provide_secret(m_agent,
101 pToken->id, settings);
102 g_hash_table_unref(settings);
103 delete pToken;
106void NetworkAgent::cancel(const QVariant &token)
108 NetworkAgentToken *pToken = (NetworkAgentToken *) token.value<void *>();
109 if (pToken != NULL) {
110 unity_settings_secret_agent_cancel_request(m_agent, pToken->id);
111 delete pToken;
112 }
115void NetworkAgent::onSecretRequested(UnitySettingsSecretAgent* /*agent*/,
116 guint id,
117 NMConnection* connection,
118 const char* /*setting_name*/,
119 const char** /*hints*/,
120 NMSecretAgentGetSecretsFlags /*flags*/,
121 NetworkAgent* self)
123 NetworkAgentToken *token = new NetworkAgentToken(connection,
124 NULL,
125 id,
126 NULL);
128 token->wisec = nm_connection_get_setting_wireless_security(connection);
129 if (token->wisec) {
130 token->keyMgmt = nm_setting_wireless_security_get_key_mgmt(token->wisec);
131 }
133 QVariant varToken = qVariantFromValue((void *) token);
134 Q_EMIT self->secretRequested(varToken);
137void NetworkAgent::onSecretRequestCancelled(UnitySettingsSecretAgent* /*agent*/,
138 guint /*id*/,
139 NMConnection* /*connection*/,
140 const char* /*setting_name*/,
141 const char** /*hints*/,
142 NMSecretAgentGetSecretsFlags /*flags*/,
143 NetworkAgent* /*self*/)
145 //Do nothing for now