~ken-vandine/content-hub/fix_pending_check

« back to all changes in this revision

Viewing changes to src/com/ubuntu/content/hub.cpp

  • Committer: Thomas Voß
  • Date: 2013-07-15 11:07:50 UTC
  • Revision ID: thomas.voss@canonical.com-20130715110750-l9395s2sdimj0ccw
Initial checkin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 version 3 as
 
6
 * published by the Free Software Foundation.
 
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
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include "ContentServiceInterface.h"
 
20
 
 
21
#include <com/ubuntu/content/hub.h>
 
22
#include <com/ubuntu/content/import_export_handler.h>
 
23
#include <com/ubuntu/content/peer.h>
 
24
#include <com/ubuntu/content/scope.h>
 
25
#include <com/ubuntu/content/store.h>
 
26
#include <com/ubuntu/content/type.h>
 
27
 
 
28
#include <QStandardPaths>
 
29
 
 
30
#include <map>
 
31
 
 
32
namespace cuc = com::ubuntu::content;
 
33
 
 
34
struct cuc::Hub::Private
 
35
{
 
36
    Private(QObject* parent) : service(
 
37
        new com::ubuntu::content::Service(
 
38
            "com.ubuntu.content.Service",
 
39
            "/",
 
40
            QDBusConnection::sessionBus(),
 
41
            parent))
 
42
    {
 
43
    }
 
44
 
 
45
    com::ubuntu::content::Service* service;
 
46
};
 
47
 
 
48
cuc::Hub::Hub(QObject* parent) : QObject(parent), d{new cuc::Hub::Private{this}}
 
49
{
 
50
}
 
51
 
 
52
cuc::Hub::~Hub()
 
53
{
 
54
}
 
55
 
 
56
cuc::Hub* cuc::Hub::Client::instance()
 
57
{
 
58
    static cuc::Hub* hub = new cuc::Hub(nullptr);
 
59
    return hub;
 
60
}
 
61
 
 
62
void cuc::Hub::register_import_export_handler(cuc::ImportExportHandler*)
 
63
{
 
64
}
 
65
 
 
66
const cuc::Store* cuc::Hub::store_for_scope_and_type(cuc::Scope scope, cuc::Type type)
 
67
{
 
68
    static const std::map<std::pair<cuc::Scope, cuc::Type>, cuc::Store*> lut =
 
69
            {
 
70
                {{cuc::system, cuc::Type::Known::pictures()}, new cuc::Store{"/content/Pictures", this}},
 
71
                {{cuc::system, cuc::Type::Known::music()}, new cuc::Store{"/content/Music", this}},
 
72
                {{cuc::system, cuc::Type::Known::documents()}, new cuc::Store{"/content/Documents", this}},
 
73
                {{cuc::user, cuc::Type::Known::pictures()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), this}},
 
74
                {{cuc::user, cuc::Type::Known::music()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::MusicLocation), this}},
 
75
                {{cuc::user, cuc::Type::Known::documents()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), this}},
 
76
                {{cuc::app, cuc::Type::Known::pictures()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Pictures", this}},
 
77
                {{cuc::app, cuc::Type::Known::music()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Music", this}},
 
78
                {{cuc::app, cuc::Type::Known::documents()}, new cuc::Store{QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Documents", this}},
 
79
            };
 
80
 
 
81
    auto it = lut.find(std::make_pair(scope, type));
 
82
 
 
83
    if (it == lut.end())
 
84
        return nullptr;
 
85
 
 
86
    return it->second;
 
87
}
 
88
 
 
89
cuc::Peer cuc::Hub::default_peer_for_type(cuc::Type t)
 
90
{
 
91
    auto reply = d->service->DefaultPeerForType(t.id());
 
92
    reply.waitForFinished();
 
93
 
 
94
    if (reply.isError())
 
95
        return cuc::Peer::unknown();
 
96
 
 
97
    return cuc::Peer(reply.value(), this);
 
98
}
 
99
 
 
100
QVector<cuc::Peer> cuc::Hub::known_peers_for_type(cuc::Type t)
 
101
{
 
102
    QVector<cuc::Peer> result;
 
103
 
 
104
    auto reply = d->service->KnownPeersForType(t.id());
 
105
    reply.waitForFinished();
 
106
 
 
107
    if (reply.isError())
 
108
        return result;
 
109
    
 
110
    auto ids = reply.value();
 
111
 
 
112
    Q_FOREACH(const QString& id, ids)
 
113
    {
 
114
        result << cuc::Peer(id, this);
 
115
    }
 
116
 
 
117
    return result;
 
118
}
 
119
 
 
120
cuc::Transfer* cuc::Hub::create_import_for_type_from_peer(cuc::Type, cuc::Peer)
 
121
{
 
122
    return nullptr;
 
123
}
 
124
 
 
125
void cuc::Hub::quit()
 
126
{
 
127
    d->service->Quit();
 
128
}