~ubuntu-branches/ubuntu/trusty/content-hub/trusty-proposed

« back to all changes in this revision

Viewing changes to src/com/ubuntu/content/service/registry.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ken VanDine, Ubuntu daily release
  • Date: 2014-03-21 00:46:57 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20140321004657-5w0p7mj1qn9lrrj3
Tags: 0.0+14.04.20140321-0ubuntu1
[ Ken VanDine ]
* Adds support for multiple handler types: source, destination and
  share. Enforce single transfer per peer, if a second transfer
  request is made from a peer that already has an unfinished transfer,
  cancel the previous one. This is needed as long as apps are required
  to be single instance.
* If the transfer isn't persistent, attempt to hardlink instead of
  copying. If the link fails, fallback to a copy.
* If the default source is set to anything other than a click appId
  triplet, fallback to the legacy APP_ID. Changed default source for
  contacts to the legacy APP_ID for address-book-app

[ Ubuntu daily release ]
* New rebuild forced

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Authored by: Ken VanDine <ken.vandine@canonical.com>
17
17
 */
18
18
 
 
19
#include "debug.h"
19
20
#include "registry.h"
 
21
#include "utils.cpp"
20
22
#include <upstart-app-launch.h>
21
23
 
22
24
Registry::Registry() :
23
 
    m_defaultPeers(new QGSettings("com.ubuntu.content.hub.default",
 
25
    m_defaultSources(new QGSettings("com.ubuntu.content.hub.default",
24
26
                                  "/com/ubuntu/content/hub/peers/")),
25
 
    m_peers(new QGSettings("com.ubuntu.content.hub.all",
26
 
                           "/com/ubuntu/content/hub/peers/"))
 
27
    m_sources(new QGSettings("com.ubuntu.content.hub.source",
 
28
                           "/com/ubuntu/content/hub/source/")),
 
29
    m_dests(new QGSettings("com.ubuntu.content.hub.destination",
 
30
                           "/com/ubuntu/content/hub/destination/")),
 
31
    m_shares(new QGSettings("com.ubuntu.content.hub.share",
 
32
                           "/com/ubuntu/content/hub/share/"))
27
33
{
 
34
    /* ensure all default sources are registered as available sources */
 
35
    QList<cuc::Type> types = known_types();
 
36
    Q_FOREACH (cuc::Type type, types)
 
37
    {
 
38
        if (m_defaultSources->keys().contains(type.id()))
 
39
        {
 
40
            QString peer_id = m_defaultSources->get(type.id()).toString();
 
41
            QStringList as(m_defaultSources->get(type.id()).toStringList());
 
42
            if (!as.isEmpty())
 
43
            {
 
44
                std::string pkg = as[0].toStdString();
 
45
                std::string app = as[1].toStdString();
 
46
                std::string ver = as[2].toStdString();
 
47
                cuc::Peer peer;
 
48
                if (app.empty() || ver.empty())
 
49
                    peer = QString::fromStdString(pkg);
 
50
                else
 
51
                    peer = QString::fromLocal8Bit(upstart_app_launch_triplet_to_app_id(pkg.c_str(), app.c_str(), ver.c_str()));
 
52
                install_source_for_type(type, cuc::Peer{peer.id(), true});
 
53
            }
 
54
        }
 
55
    }
28
56
}
29
57
 
30
58
Registry::~Registry() {}
31
59
 
32
 
cuc::Peer Registry::default_peer_for_type(cuc::Type type)
33
 
{
34
 
    qDebug() << Q_FUNC_INFO << type.id();
35
 
    if (m_defaultPeers->keys().contains(type.id()))
36
 
    {
37
 
        QStringList as(m_defaultPeers->get(type.id()).toStringList());
38
 
        std::string pkg = as[0].toStdString();
39
 
        std::string app = as[1].toStdString();
40
 
        std::string ver = as[2].toStdString();
41
 
        return cuc::Peer(QString::fromLocal8Bit(upstart_app_launch_triplet_to_app_id(pkg.c_str(), app.c_str(), ver.c_str())));
42
 
    }
43
 
    else
44
 
        return cuc::Peer();
45
 
}
46
 
 
47
 
void Registry::enumerate_known_peers_for_type(cuc::Type type, const std::function<void(const cuc::Peer&)>&for_each)
48
 
{
49
 
    qDebug() << Q_FUNC_INFO << type.id();
50
 
 
51
 
    Q_FOREACH (QString k, m_peers->get(type.id()).toStringList())
52
 
    {
53
 
        qDebug() << Q_FUNC_INFO << k;
54
 
        for_each(k);
55
 
    }
 
60
cuc::Peer Registry::default_source_for_type(cuc::Type type)
 
61
{
 
62
    TRACE() << Q_FUNC_INFO << type.id();
 
63
    if (m_defaultSources->keys().contains(type.id()))
 
64
    {
 
65
        QStringList as(m_defaultSources->get(type.id()).toStringList());
 
66
        if (!as.isEmpty())
 
67
        {
 
68
            std::string pkg = as[0].toStdString();
 
69
            std::string app = as[1].toStdString();
 
70
            std::string ver = as[2].toStdString();
 
71
            cuc::Peer peer;
 
72
            if (app.empty() || ver.empty())
 
73
                return cuc::Peer(QString::fromStdString(pkg));
 
74
            return cuc::Peer(QString::fromLocal8Bit(upstart_app_launch_triplet_to_app_id(pkg.c_str(), app.c_str(), ver.c_str())), true);
 
75
        }
 
76
    }
 
77
 
 
78
    return cuc::Peer();
56
79
}
57
80
 
58
81
void Registry::enumerate_known_peers(const std::function<void(const cuc::Peer&)>&for_each)
59
82
{
60
 
    qDebug() << Q_FUNC_INFO;
61
 
 
62
 
    Q_FOREACH (QString type_id, m_peers->keys())
63
 
    {
64
 
        qDebug() << Q_FUNC_INFO << type_id;
65
 
        Q_FOREACH (QString k, m_peers->get(type_id).toStringList())
66
 
        {
67
 
            qDebug() << Q_FUNC_INFO << k;
68
 
            for_each(k);
69
 
        }
70
 
    }
71
 
}
72
 
 
73
 
bool Registry::install_default_peer_for_type(cuc::Type type, cuc::Peer peer)
74
 
{
75
 
    qDebug() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
76
 
    if (m_defaultPeers->keys().contains(type.id()))
77
 
    {
78
 
        qDebug() << Q_FUNC_INFO << "Default peer for" << type.id() << "already installed.";
 
83
    TRACE() << Q_FUNC_INFO;
 
84
 
 
85
    Q_FOREACH (QString type_id, m_sources->keys())
 
86
    {
 
87
        TRACE() << Q_FUNC_INFO << type_id;
 
88
        Q_FOREACH (QString k, m_sources->get(type_id).toStringList())
 
89
        {
 
90
            TRACE() << Q_FUNC_INFO << k;
 
91
            for_each(cuc::Peer{k});
 
92
        }
 
93
    }
 
94
    Q_FOREACH (QString type_id, m_dests->keys())
 
95
    {
 
96
        TRACE() << Q_FUNC_INFO << type_id;
 
97
        Q_FOREACH (QString k, m_dests->get(type_id).toStringList())
 
98
        {
 
99
            TRACE() << Q_FUNC_INFO << k;
 
100
            for_each(cuc::Peer{k});
 
101
        }
 
102
    }
 
103
    Q_FOREACH (QString type_id, m_shares->keys())
 
104
    {
 
105
        TRACE() << Q_FUNC_INFO << type_id;
 
106
        Q_FOREACH (QString k, m_shares->get(type_id).toStringList())
 
107
        {
 
108
            TRACE() << Q_FUNC_INFO << k;
 
109
            for_each(cuc::Peer{k});
 
110
        }
 
111
    }
 
112
}
 
113
 
 
114
void Registry::enumerate_known_sources_for_type(cuc::Type type, const std::function<void(const cuc::Peer&)>&for_each)
 
115
{
 
116
    TRACE() << Q_FUNC_INFO << type.id();
 
117
 
 
118
    if (type == cuc::Type::unknown())
 
119
        return;
 
120
 
 
121
    Q_FOREACH (QString k, m_sources->get(type.id()).toStringList())
 
122
    {
 
123
        TRACE() << Q_FUNC_INFO << k;
 
124
        bool defaultPeer = false;
 
125
        QStringList as(m_defaultSources->get(type.id()).toStringList());
 
126
        if (!as.isEmpty())
 
127
        {   
 
128
            std::string pkg = as[0].toStdString();
 
129
            std::string app = as[1].toStdString();
 
130
            std::string ver = as[2].toStdString();
 
131
            if (app.empty() || ver.empty())
 
132
                defaultPeer = QString::fromStdString(pkg) == k;
 
133
            else
 
134
                defaultPeer = QString::fromLocal8Bit(upstart_app_launch_triplet_to_app_id(pkg.c_str(), app.c_str(), ver.c_str())) == k;
 
135
        }
 
136
        for_each(cuc::Peer{k, defaultPeer});
 
137
    }
 
138
}
 
139
 
 
140
void Registry::enumerate_known_destinations_for_type(cuc::Type type, const std::function<void(const cuc::Peer&)>&for_each)
 
141
{
 
142
    TRACE() << Q_FUNC_INFO << type.id();
 
143
    Q_FOREACH (QString k, m_dests->get(type.id()).toStringList())
 
144
    {
 
145
        TRACE() << Q_FUNC_INFO << k;
 
146
        for_each(cuc::Peer{k});
 
147
    }
 
148
}
 
149
 
 
150
void Registry::enumerate_known_shares_for_type(cuc::Type type, const std::function<void(const cuc::Peer&)>&for_each)
 
151
{
 
152
    TRACE() << Q_FUNC_INFO << type.id();
 
153
 
 
154
    Q_FOREACH (QString k, m_shares->get(type.id()).toStringList())
 
155
    {
 
156
        TRACE() << Q_FUNC_INFO << k;
 
157
        for_each(cuc::Peer{k});
 
158
    }
 
159
}
 
160
 
 
161
bool Registry::install_default_source_for_type(cuc::Type type, cuc::Peer peer)
 
162
{
 
163
    TRACE() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
 
164
    if (m_defaultSources->keys().contains(type.id()))
 
165
    {
 
166
        TRACE() << Q_FUNC_INFO << "Default peer for" << type.id() << "already installed.";
79
167
        return false;
80
168
    }
81
169
 
82
 
    this->install_peer_for_type(type, peer);
83
 
    return m_defaultPeers->trySet(type.id(), QVariant(peer.id()));
84
 
}
85
 
 
86
 
bool Registry::install_peer_for_type(cuc::Type type, cuc::Peer peer)
87
 
{
88
 
    qDebug() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
89
 
    QStringList l = m_peers->get(type.id()).toStringList();
90
 
    if (not l.contains(peer.id()))
91
 
    {
92
 
        l.append(peer.id());
93
 
        return m_peers->trySet(type.id(), QVariant(l));
 
170
    this->install_source_for_type(type, peer);
 
171
    return m_defaultSources->trySet(type.id(), QVariant(peer.id()));
 
172
}
 
173
 
 
174
bool Registry::install_source_for_type(cuc::Type type, cuc::Peer peer)
 
175
{
 
176
    TRACE() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
 
177
    QStringList l = m_sources->get(type.id()).toStringList();
 
178
    if (not l.contains(peer.id()))
 
179
    {
 
180
        l.append(peer.id());
 
181
        return m_sources->trySet(type.id(), QVariant(l));
 
182
    }
 
183
    return false;
 
184
}
 
185
 
 
186
bool Registry::install_destination_for_type(cuc::Type type, cuc::Peer peer)
 
187
{
 
188
    TRACE() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
 
189
    QStringList l = m_dests->get(type.id()).toStringList();
 
190
    if (not l.contains(peer.id()))
 
191
    {
 
192
        l.append(peer.id());
 
193
        return m_dests->trySet(type.id(), QVariant(l));
 
194
    }
 
195
    return false;
 
196
}
 
197
 
 
198
bool Registry::install_share_for_type(cuc::Type type, cuc::Peer peer)
 
199
{
 
200
    TRACE() << Q_FUNC_INFO << "type:" << type.id() << "peer:" << peer.id();
 
201
    QStringList l = m_shares->get(type.id()).toStringList();
 
202
    if (not l.contains(peer.id()))
 
203
    {
 
204
        l.append(peer.id());
 
205
        return m_shares->trySet(type.id(), QVariant(l));
94
206
    }
95
207
    return false;
96
208
}
97
209
 
98
210
bool Registry::remove_peer(cuc::Peer peer)
99
211
{
100
 
    qDebug() << Q_FUNC_INFO << "peer:" << peer.id();
 
212
    TRACE() << Q_FUNC_INFO << "peer:" << peer.id();
101
213
    bool ret = false;
102
 
    Q_FOREACH (QString type_id, m_peers->keys())
103
 
    {
104
 
        QStringList l = m_peers->get(type_id).toStringList();
105
 
        if (l.contains(peer.id()))
106
 
        {
107
 
            l.removeAll(peer.id());
108
 
            ret = m_peers->trySet(type_id, QVariant(l));
 
214
    Q_FOREACH (QString type_id, m_sources->keys())
 
215
    {
 
216
        QStringList l = m_sources->get(type_id).toStringList();
 
217
        if (l.contains(peer.id()))
 
218
        {
 
219
            l.removeAll(peer.id());
 
220
            ret = m_sources->trySet(type_id, QVariant(l));
 
221
        }
 
222
    }
 
223
    Q_FOREACH (QString type_id, m_dests->keys())
 
224
    {
 
225
        QStringList l = m_dests->get(type_id).toStringList();
 
226
        if (l.contains(peer.id()))
 
227
        {
 
228
            l.removeAll(peer.id());
 
229
            ret = m_dests->trySet(type_id, QVariant(l));
 
230
        }
 
231
    }
 
232
    Q_FOREACH (QString type_id, m_shares->keys())
 
233
    {
 
234
        QStringList l = m_shares->get(type_id).toStringList();
 
235
        if (l.contains(peer.id()))
 
236
        {
 
237
            l.removeAll(peer.id());
 
238
            ret = m_shares->trySet(type_id, QVariant(l));
109
239
        }
110
240
    }
111
241
    return ret;