24
24
#include <QStandardPaths>
27
#include <com/ubuntu/content/peer.h>
30
Hook::Hook(QString app_id, QObject *parent) :
31
Hook::Hook(QObject *parent) :
33
33
registry(new Registry())
35
qDebug() << Q_FUNC_INFO;
36
35
QTimer::singleShot(200, this, SLOT(run()));
39
Hook::Hook(com::ubuntu::content::detail::PeerRegistry *registry, QObject *parent) :
41
47
qDebug() << Q_FUNC_INFO;
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:
58
* The hook also iterates known peers and removes them if there is
59
* no JSON file installed in this path.
47
QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)
63
QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
49
65
+ QString("content-hub"));
51
qDebug() << contentDir.absolutePath();
52
67
if (not contentDir.exists())
55
const QString file = contentDir.filePath(peer.id() + QString("-content.json"));
57
QFile contentJson(file);
70
QStringList all_peers;
71
registry->enumerate_known_peers([&all_peers](const com::ubuntu::content::Peer& peer)
73
all_peers.append(peer.id());
76
Q_FOREACH(QString p, all_peers)
78
qDebug() << Q_FUNC_INFO << "Looking for" << p;
79
QStringList pp = contentDir.entryList(QStringList("*"+ p));
81
registry->remove_peer(com::ubuntu::content::Peer{p});
84
Q_FOREACH(QFileInfo f, contentDir.entryInfoList(QDir::Files))
87
QCoreApplication::instance()->quit();
90
bool Hook::add_peer(QFileInfo result)
92
qDebug() << Q_FUNC_INFO << "Hook:" << result.filePath();
94
QString app_id = result.fileName();
95
auto peer = cuc::Peer(app_id);
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());
61
101
QJsonParseError *e = new QJsonParseError();
62
102
QJsonDocument contentDoc = QJsonDocument::fromJson(contentJson.readAll(), e);
64
104
if (e->error != 0)
65
return_error(e->errorString());
105
return return_error(e->errorString());
67
107
if (not contentDoc.isObject())
68
return_error("invalid JSON object");
108
return return_error("invalid JSON object");
71
110
QJsonObject contentObj = contentDoc.object();
72
111
QVariant sources = contentObj.toVariantMap()["source"];
73
112
Q_FOREACH(QString source, sources.toStringList())
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")
77
118
if (not registry->install_peer_for_type(cuc::Type::Known::pictures(), peer))