~apachelogger/ubuntuone-client/gsoc

18 by Harald Sitter
Copy dbus interfaces to new syncdaemon class
1
/*
2
  Copyright © 2010 Harald Sitter <apachelogger@ubuntu.com>
3
4
  This program is free software; you can redistribute it and/or
5
  modify it under the terms of the GNU General Public License as
6
  published by the Free Software Foundation; either version 2 of
7
  the License or (at your option) version 3 or any later version
8
  accepted by the membership of KDE e.V. (or its successor approved
9
  by the membership of KDE e.V.), which shall act as a proxy
10
  defined in Section 14 of version 3 of the license.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU General Public License for more details.
16
17
  You should have received a copy of the GNU General Public License
18
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
17 by Harald Sitter
Add syncdaemon lib to wrap around auth and syncdaemon
21
#include "SyncDaemon.h"
22
320 by Harald Sitter
factor file stuff into own class
23
#include <QtCore/QDebug>
24
193 by Harald Sitter
Add u1 dir to the places list
25
#include <KFilePlacesModel>
63.1.16 by Harald Sitter
Add basic stateawareness
26
239 by Harald Sitter
ubuntuone-auth -> ubuntu-sso
27
#include "ComUbuntuSsoInterface.h"
18 by Harald Sitter
Copy dbus interfaces to new syncdaemon class
28
#include "SyncDaemonDBus.h"
29
145 by Harald Sitter
Formatting++
30
namespace UbuntuOne
31
{
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
32
    class SyncDaemonPrivate
33
    {
34
    public:
35
        Q_DECLARE_PUBLIC(SyncDaemon)
36
37
        SyncDaemonPrivate(SyncDaemon *q) :
38
            q_ptr(q),
179 by Harald Sitter
The return of upload indication \o/
39
            m_rootDir(QDir()),
40
            m_queueCount(0)
41
        {
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
42
        }
43
306 by Harald Sitter
prevent possible leak
44
        ~SyncDaemonPrivate()
45
        {
46
            delete auth;
47
            delete daemon;
48
            delete status;
49
        }
50
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
51
        SyncDaemon *const q_ptr;
52
178 by Harald Sitter
cleanup private daemon
53
        QDir m_rootDir;
179 by Harald Sitter
The return of upload indication \o/
54
        int m_queueCount;
55
        QStringList m_uploading;
56
        QStringList m_downloading;
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
57
239 by Harald Sitter
ubuntuone-auth -> ubuntu-sso
58
        ComUbuntuSsoInterface *auth;
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
59
        ComUbuntuoneSyncDaemonSyncDaemonInterface *daemon;
60
        ComUbuntuoneSyncDaemonStatusInterface *status;
61
62
        void updateRootDir();
63
        void setRootDir(const QString &root);
64
        void updateQueues();
65
66
        //        void updateTransferStatus();
67
        //        void updatingCompleted();
68
    };
69
70
71
    void SyncDaemonPrivate::updateRootDir()
72
    {
73
        setRootDir(daemon->get_rootdir());
74
    }
75
76
    void SyncDaemonPrivate::setRootDir(const QString &root)
77
    {
78
        Q_Q(SyncDaemon);
79
80
        // Ifing here is really not worth anything. Just try setting whatever
81
        // we got as root, if the path was crap, it will be acted upon below.
178 by Harald Sitter
cleanup private daemon
82
        m_rootDir.setPath(root);
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
83
178 by Harald Sitter
cleanup private daemon
84
        if (!m_rootDir.exists()) {
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
85
            return;
86
        }
87
193 by Harald Sitter
Add u1 dir to the places list
88
        KFilePlacesModel places;
89
        KUrl url = KUrl(m_rootDir.path());
90
        if (places.url(places.closestItem(url)) != url) {
91
            places.addPlace("Ubuntu One", url, "ubuntuone-kde");
92
        }
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
93
94
        q->statusChanged(status->current_status());
95
    }
96
97
    void SyncDaemonPrivate::updateQueues()
98
    {
99
        status->current_status();
100
    }
145 by Harald Sitter
Formatting++
101
102
////////////////////////////////// Public //////////////////////////////////
103
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
104
    SyncDaemon::SyncDaemon(QObject *parent) :
105
        QObject(parent),
106
        d_ptr(new SyncDaemonPrivate(this))
107
    {
108
        qRegisterMetaType<QStringHash>("QStringHash");
109
        qDBusRegisterMetaType<QStringHash>();
110
        qRegisterMetaType<QStringHashHash>("QStringHashHash");
111
        qDBusRegisterMetaType<QStringHashHash>();
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
112
        Status::registerMetaType();
224 by Harald Sitter
this change is so epic it gave me a headache ... share++ syncdaemonshares++ improve all sorts of stuff and magic and whatnot... actually I lost track megachanges--
113
        Share::registerMetaType();
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
114
115
        Q_D(SyncDaemon);
116
117
        d->auth =
239 by Harald Sitter
ubuntuone-auth -> ubuntu-sso
118
            new ComUbuntuSsoInterface("com.ubuntu.sso",
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
119
                    "/", QDBusConnection::sessionBus(),
120
                    this);
121
122
        d->daemon =
123
            new ComUbuntuoneSyncDaemonSyncDaemonInterface("com.ubuntuone.SyncDaemon",
124
                    "/", QDBusConnection::sessionBus(),
125
                    this);
126
127
        d->status =
128
            new ComUbuntuoneSyncDaemonStatusInterface("com.ubuntuone.SyncDaemon",
129
                    "/status", QDBusConnection::sessionBus(),
130
                    this);
131
132
        connect(d->auth, SIGNAL(NewCredentials(QString, QString)),
133
                this, SLOT(receivedCredentials(QString, QString)));
134
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
135
        connect(d->status, SIGNAL(StatusChanged(UbuntuOne::Status)),
136
                this, SLOT(statusChanged(UbuntuOne::Status)));
171 by Harald Sitter
Behold! The return of transferStarted and contentQueueChanged. Only for debugging as of now.
137
        connect(d->status, SIGNAL(ContentQueueChanged(QStringHashHash)),
138
                this, SLOT(contentQueueChanged(QStringHashHash)));
172 by Harald Sitter
I have absolutely no idea why, but the introspection reported {Download,Upload}Started as variant, even though they are strings... it still does... VERY odd.
139
        connect(d->status, SIGNAL(UploadStarted(QString)),
177 by Harald Sitter
work on upload only for now
140
                this, SLOT(uploadStarted(QString)));
141
        connect(d->status, SIGNAL(UploadFinished(QString,QStringHash)),
142
                this, SLOT(uploadFinished(QString,QStringHash)));
182 by Harald Sitter
EXTERMINATE! - erm - DOWNLOAD! ;)
143
        connect(d->status, SIGNAL(DownloadStarted(QString)),
144
                this, SLOT(downloadStarted(QString)));
145
        connect(d->status, SIGNAL(DownloadFinished(QString,QStringHash)),
146
                this, SLOT(downloadFinished(QString,QStringHash)));
331 by Harald Sitter
this is a proper I-have-no-idea-commit
147
148
        // TODO: revisit this. I am not sure if this should be here, more likely
149
        // it should be added to the connection process.
150
        d->auth->maybe_login("https://one.ubuntu.com", "ubuntuone", true);
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
151
    }
152
290 by Harald Sitter
dtor++
153
    SyncDaemon::~SyncDaemon()
154
    {
155
        delete d_ptr;
156
    }
157
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
158
    void SyncDaemon::updateStatus()
159
    {
160
        Q_D(SyncDaemon);
161
162
        d->updateRootDir(); // calls statusChanged if successful
163
        d->updateQueues();
164
    }
165
238 by Harald Sitter
Improved code sweetness
166
    QString SyncDaemon::rootDir() const
167
    {
168
        Q_D(const SyncDaemon);
169
        return d->daemon->get_rootdir();
170
    }
171
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
172
    void SyncDaemon::connectDaemon() const
173
    {
174
        Q_D(const SyncDaemon);
175
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
176
        Status dstatus = d->status->current_status();
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
177
178
        if (!dstatus.isConnected()) {
179
            qDebug() << "connecting to cloud";
180
            d->daemon->disconnect();
181
            d->daemon->connect();
182
        }
183
    }
184
185
    void SyncDaemon::disconnectDaemon() const
186
    {
187
        Q_D(const SyncDaemon);
188
189
        qDebug() << "disconnecting from cloud";
125 by Harald Sitter
dptr++ for syncdaemon
190
        d->daemon->disconnect();
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
191
    }
192
193
    void SyncDaemon::restartDaemon() const
194
    {
195
        Q_D(const SyncDaemon);
196
197
        d->daemon->quit();
198
        connectDaemon();
199
    }
200
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
201
    void SyncDaemon::statusChanged(const UbuntuOne::Status &status)
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
202
    {
203
        Q_D(SyncDaemon);
204
178 by Harald Sitter
cleanup private daemon
205
        if (d->m_rootDir.path().isEmpty()) {
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
206
            return d->updateRootDir();
207
        }
208
209
        switch (status.state()) {
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
210
        case Status::Error : {
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
211
            switch (status.errorState()) {
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
212
            case Status::AuthFailed : emit authFailed(); break;
213
            case Status::CapabilitiesMismatch : emit capabilitiesMismatch(); break;
214
            case Status::UnkownError : emit unkownErrorOccurred(); break;
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
215
            }
216
        }
326 by Harald Sitter
SyncDaemonStatus becomes Status since it really is a type and not an interface, yet the previous name suggested interface (all messed up I say)
217
        case Status::Disconnected : emit disconnected(); break;
218
        case Status::Connecting : emit connecting(); break;
156 by Harald Sitter
namespace indention ... spec does not define whether namespaces are indented or not, so general rule of indention is supposedly to be applied
219
        default : emit idle(); break;
220
        }
221
    }
222
223
    void SyncDaemon::receivedCredentials(const QString &realm,
224
                                         const QString &consumerKey)
225
    {
226
        Q_UNUSED(realm);
227
        Q_UNUSED(consumerKey);
228
229
        Q_D(SyncDaemon);
230
231
        connectDaemon();
232
        d->updateRootDir();
233
    }
234
171 by Harald Sitter
Behold! The return of transferStarted and contentQueueChanged. Only for debugging as of now.
235
    void SyncDaemon::contentQueueChanged(const QStringHashHash &queue)
236
    {
237
        qDebug() << "contentQueueChanged:" << queue;
179 by Harald Sitter
The return of upload indication \o/
238
239
        Q_D(SyncDaemon);
240
        bool ok;
307 by Harald Sitter
linking--
241
        int count = queue.value("Upload").value("count").toInt(&ok);
179 by Harald Sitter
The return of upload indication \o/
242
        qDebug() << ok;
243
        qDebug() << count;
244
        if (ok) {
245
            d->m_queueCount = count;
246
        } else {
247
            d->m_queueCount = 0;
248
        }
182 by Harald Sitter
EXTERMINATE! - erm - DOWNLOAD! ;)
249
250
        count = queue.value("Download").value("count").toInt(&ok);
251
        if (ok) {
252
            d->m_queueCount = d->m_queueCount + count;
253
        }
254
179 by Harald Sitter
The return of upload indication \o/
255
        emit queueUpdated(d->m_queueCount);
171 by Harald Sitter
Behold! The return of transferStarted and contentQueueChanged. Only for debugging as of now.
256
    }
257
177 by Harald Sitter
work on upload only for now
258
    void SyncDaemon::uploadStarted(const QString &path)
259
    {
260
        qDebug() << "uploadStarted:" << path;
179 by Harald Sitter
The return of upload indication \o/
261
262
        Q_D(SyncDaemon);
263
        d->m_uploading.append(path);
264
265
        qDebug() << d->m_uploading;
266
267
        emit uploading(d->m_uploading, d->m_queueCount);
177 by Harald Sitter
work on upload only for now
268
    }
269
270
    void SyncDaemon::uploadFinished(const QString &path, const QStringHash &info)
271
    {
272
        qDebug() << "uploadFinished:" << path;
273
        qDebug() << "uploadFinished:" << info;
179 by Harald Sitter
The return of upload indication \o/
274
275
        Q_D(SyncDaemon);
276
        d->m_uploading.removeAt(d->m_uploading.indexOf(path));
171 by Harald Sitter
Behold! The return of transferStarted and contentQueueChanged. Only for debugging as of now.
277
    }
278
182 by Harald Sitter
EXTERMINATE! - erm - DOWNLOAD! ;)
279
    void SyncDaemon::downloadStarted(const QString &path)
280
    {
281
        qDebug() << "downloadStarted:" << path;
282
283
        Q_D(SyncDaemon);
284
        d->m_downloading.append(path);
285
286
        qDebug() << d->m_downloading;
287
288
        emit downloading(d->m_downloading, d->m_queueCount);
289
    }
290
291
    void SyncDaemon::downloadFinished(const QString &path, const QStringHash &info)
292
    {
293
        qDebug() << "downloadFinished:" << path;
294
        qDebug() << "downloadFinished:" << info;
295
296
        Q_D(SyncDaemon);
297
        d->m_downloading.removeAt(d->m_downloading.indexOf(path));
298
    }
299
176 by Harald Sitter
add super cool function to help with debugging raw qdbusmessages coming from signals
300
    void SyncDaemon::debugDBusMessage(const QDBusMessage &msg)
301
    {
302
        qDebug() << msg;
303
    }
17 by Harald Sitter
Add syncdaemon lib to wrap around auth and syncdaemon
304
} // namespace UbuntuOne