2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "postcapturemodel.h"
22 #include <QSparqlConnection>
23 #include <QSparqlQuery>
24 #include <QSparqlResult>
25 #include <QSparqlError>
26 #include <QDeclarativeInfo>
28 #include <QDBusConnection>
29 #include <QStringList>
30 #include <QDBusMetaType>
31 #include <QDBusArgument>
33 #define DRIVER "QTRACKER_DIRECT"
34 #define QUERY "SELECT rdf:type(?urn) AS ?type nie:url(?urn) AS ?url nie:contentCreated(?urn) AS ?created nie:title(?urn) AS ?title nfo:fileName(?urn) AS ?filename nie:mimeType(?urn) AS ?mimetype tracker:available(?urn) AS ?available nfo:fileLastModified(?urn) as ?lastmod tracker:id(?urn) AS ?trackerid (EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite }) AS ?favorite WHERE { ?urn nfo:equipment ?:equipment . {?urn a nfo:Video} UNION {?urn a nfo:Image}} ORDER BY DESC(?created)"
36 #define UPDATE_QUERY "SELECT rdf:type(?urn) AS ?type nie:url(?urn) AS ?url nie:contentCreated(?urn) AS ?created nie:title(?urn) AS ?title nfo:fileName(?urn) AS ?filename nie:mimeType(?urn) AS ?mimetype tracker:available(?urn) AS ?available nfo:fileLastModified(?urn) as ?lastmod tracker:id(?urn) AS ?trackerid (EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite }) AS ?favorite WHERE {?urn a nfo:Visual . FILTER (tracker:id(?urn) IN (%1)) }"
38 #define TRACKER_SERVICE "org.freedesktop.Tracker1"
39 #define TRACKER_RESOURCE_PATH "/org/freedesktop/Tracker1/Resources"
40 #define TRACKER_RESOURCE_INTERFACE "org.freedesktop.Tracker1.Resources"
41 #define TRACKER_RESOURCE_SIGNAL "GraphUpdated"
42 #define PHOTO_CLASS "http://www.tracker-project.org/temp/nmm#Photo"
43 #define VIDEO_CLASS "http://www.tracker-project.org/temp/nmm#Video"
44 #define TRACKER_RESOURCE_SIGNAL_SIGNATURE "sa(iiii)a(iiii)"
54 Q_DECLARE_METATYPE(Quad);
55 Q_DECLARE_METATYPE(QList<Quad>);
57 QDBusArgument& operator<<(QDBusArgument& argument, const Quad& t) {
58 argument.beginStructure();
59 argument << t.graph << t.subject << t.predicate << t.object;
60 argument.endStructure();
64 const QDBusArgument& operator>>(const QDBusArgument& argument, Quad& t) {
65 argument.beginStructure();
66 argument >> t.graph >> t.subject >> t.predicate >> t.object;
67 argument.endStructure();
71 PostCaptureModel::PostCaptureModel(QObject *parent) :
72 QAbstractListModel(parent),
76 QHash<int, QByteArray> roles;
77 roles.insert(Item, "item");
80 qDBusRegisterMetaType<Quad>();
81 qDBusRegisterMetaType<QList<Quad> >();
84 PostCaptureModel::~PostCaptureModel() {
88 delete m_connection; m_connection = 0;
91 void PostCaptureModel::reload() {
92 delete m_connection; m_connection = 0;
94 if (!m_items.isEmpty()) {
95 beginRemoveRows(QModelIndex(), 0, m_items.size() - 1);
101 m_connection = new QSparqlConnection(DRIVER, QSparqlConnectionOptions(), this);
102 if (!m_connection->isValid()) {
103 emit error(tr("Failed to create SPARQL connection"));
107 QString equipment = QString("urn:equipment:%1:%2:").arg(m_manufacturer).arg(m_model);
109 QSparqlQuery q(QUERY, QSparqlQuery::SelectStatement);
110 q.bindValue("equipment", equipment);
114 const char *slot = SLOT(graphUpdated(const QString&,
116 const QList<Quad>&));
117 m_connected = QDBusConnection::sessionBus().connect(TRACKER_SERVICE, TRACKER_RESOURCE_PATH,
118 TRACKER_RESOURCE_INTERFACE,
119 TRACKER_RESOURCE_SIGNAL,
120 TRACKER_RESOURCE_SIGNAL_SIGNATURE,
125 qmlInfo(this) << "Failed to connect to tracker " << TRACKER_RESOURCE_SIGNAL;
129 void PostCaptureModel::exec(QSparqlQuery& query) {
131 qWarning() << "No connection to query";
135 QSparqlResult *result = m_connection->exec(query);
137 if (result->hasError()) {
138 QSparqlError error = result->lastError();
139 qmlInfo(this) << "Error executing SPARQL query" << error.message();
143 emit PostCaptureModel::error(tr("Failed to query tracker"));
147 QObject::connect(result, SIGNAL(dataReady(int)), this, SLOT(dataReady(int)));
148 QObject::connect(result, SIGNAL(finished()), result, SLOT(deleteLater()));
152 int PostCaptureModel::rowCount(const QModelIndex& parent) const {
153 if (parent.isValid()) {
157 return m_items.size();
160 QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
161 if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
166 return QVariant::fromValue(dynamic_cast<QObject *>(m_items[index.row()]));
172 QString PostCaptureModel::manufacturer() const {
173 return m_manufacturer;
176 void PostCaptureModel::setManufacturer(const QString& manufacturer) {
177 if (m_manufacturer != manufacturer) {
178 m_manufacturer = manufacturer;
179 emit manufacturerChanged();
183 QString PostCaptureModel::model() const {
187 void PostCaptureModel::setModel(const QString& model) {
188 if (m_model != model) {
194 void PostCaptureModel::dataReady(int totalCount) {
195 Q_UNUSED(totalCount);
197 QSparqlResult *result = dynamic_cast<QSparqlResult *>(sender());
202 while (result->next()) {
203 addRow(new PostCaptureModelItem(result->current(), this));
209 void PostCaptureModel::addRow(PostCaptureModelItem *item) {
210 if (m_hash.contains(item->trackerId())) {
211 m_hash[item->trackerId()]->update(item);
216 beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
218 m_hash.insert(item->trackerId(), item);
223 void PostCaptureModel::remove(QObject *item) {
224 PostCaptureModelItem *i = dynamic_cast<PostCaptureModelItem *>(item);
226 qmlInfo(this) << "Invalid item to remove";
230 int index = m_items.indexOf(i);
232 qmlInfo(this) << "Item" << i->trackerId() << "not found in model";
236 beginRemoveRows(QModelIndex(), index, index);
237 m_items.takeAt(index);
238 m_hash.remove(i->trackerId());
244 void PostCaptureModel::graphUpdated(const QString& className, const QList<Quad>& deleted,
245 const QList<Quad>& inserted) {
247 // We will just assume tracker:available has changed and that's it.
248 // We are not really interested in the rest of properties.
250 if (!(className == QLatin1String(PHOTO_CLASS) || className == QLatin1String(VIDEO_CLASS))) {
256 for (int x = 0; x < deleted.size(); x++) {
258 if (m_hash.contains(q.subject) && items.indexOf(q.subject) == -1) {
263 for (int x = 0; x < inserted.size(); x++) {
264 Quad q = inserted[x];
265 if (m_hash.contains(q.subject) && items.indexOf(q.subject) == -1) {
270 for (int x = 0; x < items.size(); x++) {
271 QString query = QString(UPDATE_QUERY).arg(items[x]);
272 QSparqlQuery q(query, QSparqlQuery::SelectStatement);
278 PostCaptureModelItem::PostCaptureModelItem(const QSparqlResultRow& row, QObject *parent) :
281 for (int x = 0; x < row.count(); x++) {
282 QSparqlBinding b = row.binding(x);
283 m_data.insert(b.name(), b.value());
287 QString PostCaptureModelItem::type() const {
288 return value("type").toString();
291 QUrl PostCaptureModelItem::url() const {
292 return value("url").toUrl();
295 QString PostCaptureModelItem::created() const {
296 return formatDateTime(value("created").toDateTime());
299 QString PostCaptureModelItem::title() const {
300 return value("title").toString();
303 QString PostCaptureModelItem::fileName() const {
304 return value("filename").toString();
307 QString PostCaptureModelItem::mimeType() const {
308 return value("mimetype").toString();
311 bool PostCaptureModelItem::available() const {
312 return value("available", false).toBool();
315 QString PostCaptureModelItem::lastModified() const {
316 return formatDateTime(value("lastmod").toDateTime());
319 unsigned PostCaptureModelItem::trackerId() const {
320 return value("trackerid").toUInt();
323 bool PostCaptureModelItem::favorite() const {
324 return value("favorite", false).toBool();
327 void PostCaptureModelItem::setFavorite(bool add) {
328 if (favorite() != add) {
329 m_data["favorite"] = add;
331 emit favoriteChanged();
335 QString PostCaptureModelItem::formatDateTime(const QDateTime& dt) const {
336 return dt.toString();
339 void PostCaptureModelItem::update(PostCaptureModelItem *other) {
340 // We will only update available, favorite and title:
342 qDebug() << "i" << trackerId() << other->trackerId() << "\n"
343 << "a" << available() << other->available() << "\n"
344 << "t" << title() << other->title() << "\n"
345 << "f" << favorite() << other->favorite();
348 if (available() != other->available()) {
349 m_data["available"] = other->available();
350 emit availableChanged();
353 if (title() != other->title()) {
354 m_data["title"] = other->title();
358 if (favorite() != other->favorite()) {
359 m_data["favorite"] = other->favorite();
360 emit favoriteChanged();
364 QVariant PostCaptureModelItem::value(const QString& id, const QVariant& def) const {
365 return m_data.contains(id) ? m_data[id] : def;