~ubuntu-branches/ubuntu/oneiric/knetworkmanager/oneiric

« back to all changes in this revision

Viewing changes to knetworkmanager/src/knetworkmanager-nminfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-07-14 14:05:44 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080714140544-yjhxgrdwartk3kx7
Tags: 1:0.7svn830754-0ubuntu1
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * knetworkmanager-nminfo.cpp - A NetworkManager frontend for KDE 
4
 
 *
5
 
 * Copyright (C) 2005, 2006 Novell, Inc.
6
 
 *
7
 
 * Author: Timo Hoenig        <thoenig@suse.de>, <thoenig@nouse.net>
8
 
 *         Valentine Sinitsyn <e_val@inbox.ru>
9
 
 *
10
 
 * This program is free software; you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation; either version 2 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 * 
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program; if not, write to the Free Software
22
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 
 *
24
 
 **************************************************************************/
25
 
 
26
 
#include <kdebug.h>
27
 
#include <klocale.h>
28
 
 
29
 
#include "knetworkmanager.h"
30
 
#include "knetworkmanager-nminfo_dbus.h"
31
 
#include "knetworkmanager-dialogfab.h"
32
 
#include "knetworkmanager-encryption.h"
33
 
#include "knetworkmanager-storage.h"
34
 
#include "knetworkmanager-synchronizer.h"
35
 
#include "knetworkmanager-vpn.h"
36
 
 
37
 
#include "knetworkmanager-nminfo.h"
38
 
 
39
 
PassphraseRequest::PassphraseRequest(KNetworkManager* ctx, QString obj_path, QString net_path, QString essid, bool new_key, DBusMessage* msg)
40
 
        : QObject(ctx)
41
 
{
42
 
        _ctx = ctx;
43
 
        _obj_path = obj_path;
44
 
        _net_path = net_path;
45
 
        _essid = essid;
46
 
        _new_key = new_key;
47
 
        _msg = msg;
48
 
        _canceled = false;
49
 
        _dlg = NULL;
50
 
}
51
 
 
52
 
PassphraseRequest::~PassphraseRequest()
53
 
{
54
 
 
55
 
}
56
 
 
57
 
void 
58
 
PassphraseRequest::slotKeyEntered(Network* /*net*/)
59
 
{
60
 
        DeviceStore *store = _ctx->getDeviceStore ();
61
 
        Device *dev = store->getDevice (_obj_path);
62
 
        Network* net = dev->getNetwork(_net_path);
63
 
 
64
 
        _ctx->getNetworkManagerInfo()->sendPassphrase(net, _msg);
65
 
}
66
 
 
67
 
void
68
 
PassphraseRequest::slotCancelRequest()
69
 
{
70
 
        _canceled = true;
71
 
        _ctx->getNetworkManagerInfo()->sendPassphraseError(_msg);
72
 
}
73
 
 
74
 
void
75
 
PassphraseRequest::acquireKeyFromDialog()
76
 
{
77
 
        QString msg = QString::null;
78
 
        if (_new_key)
79
 
                msg = i18n("The connection could not be established. Please verify your settings and try again.");
80
 
        _dlg = new AcquirePasswordDialog (_ctx->getTray (), "PassphraseDialog", true, 0, _ctx, _obj_path, _net_path, _essid, msg);
81
 
        connect(_dlg, SIGNAL(sendPassphrase(Network*)), this, SLOT(slotKeyEntered(Network*)));
82
 
        connect(_dlg, SIGNAL(cancelClicked()), this, SLOT(slotCancelRequest()));
83
 
        _dlg->show ();
84
 
}
85
 
 
86
 
void
87
 
PassphraseRequest::slotKeyRestored(bool result, bool canceled)
88
 
{
89
 
        DeviceStore *store = _ctx->getDeviceStore ();
90
 
        Device *dev = store->getDevice (_obj_path);
91
 
        Network* net = dev->getNetwork(_net_path);
92
 
        bool handled = false;
93
 
 
94
 
        if (_canceled)
95
 
                return;
96
 
 
97
 
        // if the user decided to use an other device throw the retrieved key away
98
 
        if (canceled)
99
 
        {
100
 
                // fake a canceled activation stage and send a reply to NM
101
 
                dev->setActivationStage(NM_ACT_STAGE_CANCELLED);
102
 
                this->slotCancelRequest();
103
 
                return;
104
 
        }
105
 
 
106
 
        // if the key was successful restored we can proceed
107
 
        if (result)
108
 
        {
109
 
                Encryption *enc = net->getEncryption ();
110
 
                // check if encryption is now valid
111
 
                if (enc->isValid(_essid))
112
 
                {
113
 
                        // ok, lets send the new key
114
 
                        handled = true;
115
 
                        _ctx->getNetworkManagerInfo()->sendPassphrase(net, _msg);
116
 
                }
117
 
        }
118
 
 
119
 
        // not handled -> ask the user
120
 
        if (!handled)
121
 
        {
122
 
                acquireKeyFromDialog();
123
 
        }
124
 
}
125
 
 
126
 
void
127
 
PassphraseRequest::request()
128
 
{
129
 
        bool handled = false;
130
 
        DeviceStore *store = _ctx->getDeviceStore ();
131
 
        Device *dev = store->getDevice (_obj_path);
132
 
        Network* net = dev->getNetwork(_net_path);
133
 
 
134
 
        /* Steps for obtaining the key:
135
 
         *  First, look in the Encryption object - maybe it was already loaded
136
 
         *  Second, look in the wallet - maybe it is here
137
 
         *  Third, ask the user
138
 
         */
139
 
        if (!_new_key) {
140
 
                Encryption *enc = net->getEncryption ();
141
 
                if (enc->isValid (_essid) ) {
142
 
                        // the encryption is valid -> send it immediatly
143
 
                        handled = true;
144
 
                        _ctx->getNetworkManagerInfo()->sendPassphrase(net, _msg);
145
 
                }
146
 
                else if (enc->hasStoredKey()) {
147
 
                        // the key is stored in the wallet -> try to restore and wait for restoration
148
 
                        connect(enc, SIGNAL(keyRestored(bool, bool)), this, SLOT(slotKeyRestored(bool, bool)));
149
 
                        enc->restoreKeyAsync();
150
 
                        handled = true;
151
 
                }
152
 
        }
153
 
 
154
 
        if (!handled) {
155
 
                // the encryption is not valid and no key exists in kwallet -> ask the user
156
 
                acquireKeyFromDialog();
157
 
        }
158
 
}
159
 
 
160
 
void
161
 
NetworkManagerInfo::userInteraction (void)
162
 
{
163
 
        NetworkManagerInfoDBus::userInteraction ();
164
 
}
165
 
 
166
 
void
167
 
NetworkManagerInfo::sendPassphrase (Network* net, DBusMessage* msg)
168
 
{
169
 
        NetworkManagerInfoDBus::sendKeyForNetwork (net, msg);
170
 
}
171
 
 
172
 
void
173
 
NetworkManagerInfo::sendPassphraseError (DBusMessage* msg)
174
 
{
175
 
        NetworkManagerInfoDBus::sendGetKeyError (msg);
176
 
}
177
 
 
178
 
void
179
 
NetworkManagerInfo::acquirePassphrase (QString obj_path, QString net_path, QString essid, bool new_key, DBusMessage* msg)
180
 
{
181
 
        kdDebug () << k_funcinfo << " fork ahead: user or storage" << endl;
182
 
 
183
 
        DeviceStore *store = _ctx->getDeviceStore ();
184
 
        Device *dev = store->getDevice (obj_path);
185
 
        Synchronizer sync(dev);
186
 
        sync.setSources(Synchronizer::Storage | Synchronizer::New);
187
 
        sync.synchronize(essid, net_path);
188
 
 
189
 
        /*
190
 
          drop the current pending passphrase request without sending an answer to NM 
191
 
          because NM cant handle multiple passphrase replies for now.
192
 
        */
193
 
        if (_currentRequest)
194
 
                delete _currentRequest; 
195
 
 
196
 
        _currentRequest = new PassphraseRequest(_ctx, obj_path, net_path, essid, new_key, msg);
197
 
        _currentRequest->request();
198
 
}
199
 
 
200
 
QStringList
201
 
NetworkManagerInfo::getNetworks ()
202
 
{
203
 
        return KNetworkManagerStorage::getInstance ()->networks ();
204
 
}
205
 
 
206
 
Network*
207
 
NetworkManagerInfo::getNetworkProperties (const QString & essid)
208
 
{
209
 
        return KNetworkManagerStorage::getInstance ()->networkProperties (essid);
210
 
}
211
 
 
212
 
void 
213
 
NetworkManagerInfo::emitNetworkUpdated (Network* net, bool automatic)
214
 
{
215
 
        emit networkUpdated (net, automatic);
216
 
}
217
 
 
218
 
VPNConnection*
219
 
NetworkManagerInfo::getVPNConnection (const QString & name)
220
 
{
221
 
        VPN*           vpn           = _ctx->getVPN ();
222
 
        VPNConnection* vpnConnection = NULL;
223
 
 
224
 
        if (vpn && vpn->isAvailable ()) {
225
 
                VPNList* vpnList = vpn->getVPNList ();
226
 
 
227
 
                for (VPNList::iterator i = vpnList->begin (); i != vpnList->end (); ++i) {
228
 
                        if ((*i)->getName () == name)
229
 
                                vpnConnection = *i;
230
 
                }
231
 
        }
232
 
 
233
 
        return vpnConnection;
234
 
}
235
 
 
236
 
QStringList
237
 
NetworkManagerInfo::getVPNConnectionNames ()
238
 
{
239
 
        VPN*        vpn = _ctx->getVPN ();
240
 
        QStringList vpnConnectionNames;
241
 
 
242
 
        if (vpn && vpn->isAvailable ()) {
243
 
                VPNList* vpnList = vpn->getVPNList ();
244
 
                
245
 
                for (VPNList::iterator i = vpnList->begin (); i != vpnList->end (); ++i) {
246
 
                        vpnConnectionNames.append ((*i)->getName ());
247
 
                }
248
 
        }
249
 
 
250
 
        return vpnConnectionNames;
251
 
}
252
 
 
253
 
void
254
 
NetworkManagerInfo::push (KNetworkManager* ctx)
255
 
{
256
 
        NetworkManagerInfoDBus::push (ctx);
257
 
        _ctx = ctx;
258
 
}
259
 
 
260
 
NetworkManagerInfo::NetworkManagerInfo ()
261
 
{
262
 
        _currentRequest = NULL;
263
 
}
264
 
 
265
 
NetworkManagerInfo::~NetworkManagerInfo ()
266
 
{
267
 
 
268
 
}
269
 
 
270
 
#include "knetworkmanager-nminfo.moc"