File: busconnection.cpp

package info (click to toggle)
plasma-widget-networkmanagement 0.1~svn1141976-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,812 kB
  • ctags: 3,700
  • sloc: cpp: 28,962; xml: 471; makefile: 21; sh: 16; python: 9
file content (155 lines) | stat: -rw-r--r-- 5,883 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
/*  This file is part of the KDE project
    Copyright (C) 2008 Christopher Blauvelt <cblauvelt@gmail.com>
    Copyright (C) 2008,2009 Will Stephenson <wstephenson@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "busconnection.h"

#include <QDBusMetaType>
#include <KDebug>

#include "connectionsecretsjob.h"

#include "connection.h"
#include "connectiondbus.h"

typedef QMap<QString,QVariantMap> QVariantMapMap;

BusConnection::BusConnection(Knm::Connection * connection, QObject *parent)
    : QObject(parent), m_connection(connection), m_job(0)
{
    qDBusRegisterMetaType<QVariantMapMap>();
    qDBusRegisterMetaType<QList<uint> >();
    qDBusRegisterMetaType<QStringMap>();
}

BusConnection::~BusConnection()
{
    emit Removed();
}

Knm::Connection * BusConnection::connection() const
{
    return m_connection;
}

void BusConnection::Update(QVariantMapMap updates)
{
    kDebug() << "TODO: validate incoming settings";
    kDebug() << "TODO: implement fromDbusMap for all settings!";
    kDebug() << "TODO: replace existing connection with one specified in updates";
    ConnectionDbus cd(m_connection);
    cd.fromDbusMap(updates);
    emit Updated(cd.toDbusMap());
}


void BusConnection::updateInternal(Knm::Connection * connection)
{
    if (m_job) {
        // the user updated the connection using the KCM, _while_ a CSJ
        // for another GetSecrets was running
        ConnectionSecretsJob * newJob = new ConnectionSecretsJob(connection, m_job->settingName(), m_job->secrets().keys(), false /*request_new*/, m_job->requestMessage());
        m_job->kill(KJob::Quietly);

        m_job = newJob;
        connect(m_job, SIGNAL(finished(KJob*)), this, SLOT(gotSecrets(KJob*)));
        m_job->start();
    }
    m_connection = connection;
    ConnectionDbus cd(m_connection);
    QVariantMapMap map = cd.toDbusMap();
    kDebug() << "emitting Updated" << map;
    emit Updated(cd.toDbusMap());
}

void BusConnection::Delete()
{
    kDebug();
    if (m_job) {
        m_job->kill(KJob::Quietly);
    }
    deleteLater();
}

QVariantMapMap BusConnection::GetSettings() const
{
    ConnectionDbus cd(m_connection);
    QVariantMapMap map = cd.toDbusMap();
    return map;
}

QVariantMapMap BusConnection::GetSecrets(const QString &setting_name, const QStringList &hints, bool request_new, const QDBusMessage& message)
{
    if (m_job) {
        kDebug() << "existing job, ignoring";
        return QVariantMapMap();
    } else {
        kDebug() << m_connection->uuid() << setting_name << hints << request_new;
        if (!request_new && !m_connection->hasSecrets()) {
            ConnectionDbus cd(m_connection);
            return cd.toDbusSecretsMap();
        }
        message.setDelayedReply(true);
        m_job = new ConnectionSecretsJob(m_connection, setting_name, hints, request_new, message);
        connect(m_job, SIGNAL(finished(KJob*)), this, SLOT(gotSecrets(KJob*)));
        m_job->start();

        return QVariantMapMap();
    } 
}

void BusConnection::gotSecrets(KJob *job)
{
    ConnectionSecretsJob * csj = static_cast<ConnectionSecretsJob*>(job);
    if (csj == m_job) {
        if (m_job->error() == ConnectionSecretsJob::NoError) {
            ConnectionDbus db(m_connection);
            QVariantMapMap secrets = db.toDbusSecretsMap();

            QDBusMessage reply = m_job->requestMessage().createReply();

            QVariant arg = QVariant::fromValue(secrets);
            reply << arg;
            QDBusConnection::systemBus().send(reply);
        } else if (m_job->error() == ConnectionSecretsJob::EnumError::WalletDisabled ) {
            kDebug() << "ERROR: The KDE wallet is disabled";
            QDBusMessage reply = m_job->requestMessage().createErrorReply(QLatin1String("org.freedesktop.NetworkManager.SettingError"), "The wallet was disabled");
            QDBusConnection::systemBus().send(reply);
        } else if (m_job->error() == ConnectionSecretsJob::EnumError::WalletNotFound ) {
            kDebug() << "ERROR: The wallet used by KDE Network Management was not found";
            QDBusMessage reply = m_job->requestMessage().createErrorReply(QLatin1String("org.freedesktop.NetworkManager.SettingError"), "The wallet was not found");
            QDBusConnection::systemBus().send(reply);
        } else if (m_job->error() == ConnectionSecretsJob::EnumError::WalletOpenRefused ) {
            kDebug() << "ERROR: The user refused KDE Network Management (plasma) permission to open the wallet";
            QDBusMessage reply = m_job->requestMessage().createErrorReply(QLatin1String("org.freedesktop.NetworkManager.SecretsRefused"), "User refused to supply secrets");
            QDBusConnection::systemBus().send(reply);
        } else if (m_job->error() == ConnectionSecretsJob::EnumError::UserInputCancelled ) {
            kDebug() << "ERROR: The user cancelled the get secrets dialog";
            QDBusMessage reply = m_job->requestMessage().createErrorReply(QLatin1String("org.freedesktop.NetworkManager.SecretsRefused"), "User refused to supply secrets");
            QDBusConnection::systemBus().send(reply);
        }
        m_job = 0;
    }
}

QString BusConnection::uuid() const
{
    return m_connection->uuid();
}