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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ken VanDine, Robert Bruce Park, Guenter Schwann, Ubuntu daily release
  • Date: 2013-10-11 05:41:10 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20131011054110-6ncavqsr1vu5ad3u
Tags: 0.0+13.10.20131011-0ubuntu1
[ Ken VanDine ]
* Make libcontent-hub0 recommend content-hub .
* invoke the destination when the transfer is charged .
* Added a click hook, which iterates installed known peers, removing
  them when they are no longer installed and adding peers when needed.

[ Robert Bruce Park ]
* New component for the importer to indicate an ongoing transfer.

[ Guenter Schwann ]
* New component for the importer to indicate an ongoing transfer.
* Quit the source app on charged/abort when it was started by the
  content hub.

[ Ubuntu daily release ]
* Automatic snapshot from revision 60

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <QStandardPaths>
25
25
#include <QDebug>
26
26
#include <QTimer>
 
27
#include <com/ubuntu/content/peer.h>
27
28
 
28
29
#include "hook.h"
29
30
 
30
 
Hook::Hook(QString app_id, QObject *parent) :
 
31
Hook::Hook(QObject *parent) :
31
32
    QObject(parent),
32
 
    app_id(app_id),
33
33
    registry(new Registry())
34
34
{
35
 
    qDebug() << Q_FUNC_INFO;
36
35
    QTimer::singleShot(200, this, SLOT(run()));
37
36
}
38
37
 
 
38
 
 
39
Hook::Hook(com::ubuntu::content::detail::PeerRegistry *registry, QObject *parent) :
 
40
    QObject(parent),
 
41
    registry(registry)
 
42
{
 
43
}
 
44
 
39
45
void Hook::run()
40
46
{
41
47
    qDebug() << Q_FUNC_INFO;
42
 
 
43
 
    /* FIXME: we should do a sanity check on this before installing */
44
 
    auto peer = cuc::Peer(app_id);
 
48
    /* Looks for files in ${HOME}/.local/share/content-hub/${id} installed
 
49
     * by click packages.  These files are JSON, for example:
 
50
     *
 
51
     * {
 
52
     *     "source": [
 
53
     *         "pictures",
 
54
     *         "music"
 
55
     *     ]
 
56
     * }
 
57
     *
 
58
     * The hook also iterates known peers and removes them if there is
 
59
     * no JSON file installed in this path.
 
60
     */
45
61
 
46
62
    QDir contentDir(
47
 
        QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)
 
63
        QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
48
64
        + QString("/")
49
65
        + QString("content-hub"));
50
66
 
51
 
    qDebug() << contentDir.absolutePath();
52
67
    if (not contentDir.exists())
53
68
        return_error();
54
69
 
55
 
    const QString file = contentDir.filePath(peer.id() + QString("-content.json"));
56
 
 
57
 
    QFile contentJson(file);
 
70
    QStringList all_peers;
 
71
    registry->enumerate_known_peers([&all_peers](const com::ubuntu::content::Peer& peer)
 
72
                                    {
 
73
                                        all_peers.append(peer.id());
 
74
                                    });
 
75
 
 
76
    Q_FOREACH(QString p, all_peers)
 
77
    {
 
78
        qDebug() << Q_FUNC_INFO << "Looking for" << p;
 
79
        QStringList pp = contentDir.entryList(QStringList("*"+ p));
 
80
        if (pp.isEmpty())
 
81
            registry->remove_peer(com::ubuntu::content::Peer{p});
 
82
    }
 
83
 
 
84
    Q_FOREACH(QFileInfo f, contentDir.entryInfoList(QDir::Files))
 
85
        add_peer(f);
 
86
 
 
87
    QCoreApplication::instance()->quit();
 
88
}
 
89
 
 
90
bool Hook::add_peer(QFileInfo result)
 
91
{
 
92
    qDebug() << Q_FUNC_INFO << "Hook:" << result.filePath();
 
93
 
 
94
    QString app_id = result.fileName();
 
95
    auto peer = cuc::Peer(app_id);
 
96
 
 
97
    QFile contentJson(result.absoluteFilePath());
58
98
    if (!contentJson.open(QIODevice::ReadOnly | QIODevice::Text))
59
 
        return_error("couldn't open " + QString(file));
 
99
        return_error("couldn't open " + result.absoluteFilePath());
60
100
 
61
101
    QJsonParseError *e = new QJsonParseError();
62
102
    QJsonDocument contentDoc = QJsonDocument::fromJson(contentJson.readAll(), e);
63
103
 
64
104
    if (e->error != 0)
65
 
        return_error(e->errorString());
 
105
        return return_error(e->errorString());
66
106
 
67
107
    if (not contentDoc.isObject())
68
 
        return_error("invalid JSON object");
69
 
 
 
108
        return return_error("invalid JSON object");
70
109
 
71
110
    QJsonObject contentObj = contentDoc.object();
72
111
    QVariant sources = contentObj.toVariantMap()["source"];
73
112
    Q_FOREACH(QString source, sources.toStringList())
74
113
    {
 
114
        /* FIXME: we should iterate known types, but there isn't
 
115
         * really a good way to do that right now */
75
116
        if (source == "pictures")
76
117
        {
77
118
            if (not registry->install_peer_for_type(cuc::Type::Known::pictures(), peer))
88
129
                qWarning() << "Failed to install peer for" << source;
89
130
        }
90
131
    }
91
 
 
92
 
    QCoreApplication::instance()->quit();
 
132
    return true;
93
133
}
94
134
 
95
 
void Hook::return_error(QString err)
 
135
bool Hook::return_error(QString err)
96
136
{
97
137
    qWarning() << "Failed to install peer" << err;
98
 
    QCoreApplication::instance()->exit(1);
 
138
    return false;
99
139
}