Plasma5Support

storage.cpp
1/*
2 storage.cpp
3 SPDX-FileCopyrightText: 2010 Brian Pritchett <batenkaitos@gmail.com>
4 SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "private/storage_p.h"
10
11// Qt
12#include <QSqlDriver>
13#include <QSqlError>
14#include <QSqlField>
15#include <QSqlQuery>
16#include <QSqlRecord>
17#include <QThreadStorage>
18
19// KDE
20#include <QDebug>
21
22// Plasma
23#include "dataengine.h"
24#include "storagethread_p.h"
25
26StorageJob::StorageJob(const QString &destination, const QString &operation, const QVariantMap &parameters, QObject *parent)
27 : ServiceJob(destination, operation, parameters, parent)
28 , m_clientName(destination)
29{
30 Plasma5Support::StorageThread::self()->start();
31 connect(Plasma5Support::StorageThread::self(), &Plasma5Support::StorageThread::newResult, this, &StorageJob::resultSlot);
32 qRegisterMetaType<StorageJob *>();
33 qRegisterMetaType<QPointer<StorageJob>>();
34}
35
36StorageJob::~StorageJob()
37{
38}
39
40void StorageJob::setData(const QVariantMap &data)
41{
42 m_data = data;
43}
44
45QVariantMap StorageJob::data() const
46{
47 return m_data;
48}
49
50QString StorageJob::clientName() const
51{
52 return m_clientName;
53}
54
55void StorageJob::start()
56{
57 // FIXME: QHASH
58 QVariantMap params = parameters();
59
60 QString valueGroup = params[QStringLiteral("group")].toString();
61 if (valueGroup.isEmpty()) {
62 valueGroup = QStringLiteral("default");
63 }
64
65 QPointer<StorageJob> me(this);
66 if (operationName() == QLatin1String("save")) {
67 QMetaObject::invokeMethod(Plasma5Support::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(QPointer<StorageJob>, me), Q_ARG(QVariantMap, params));
68 } else if (operationName() == QLatin1String("retrieve")) {
69 QMetaObject::invokeMethod(Plasma5Support::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(QPointer<StorageJob>, me), Q_ARG(QVariantMap, params));
70 } else if (operationName() == QLatin1String("delete")) {
71 QMetaObject::invokeMethod(Plasma5Support::StorageThread::self(),
72 "deleteEntry",
74 Q_ARG(QPointer<StorageJob>, me),
75 Q_ARG(QVariantMap, params));
76 } else if (operationName() == QLatin1String("expire")) {
77 QMetaObject::invokeMethod(Plasma5Support::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(QPointer<StorageJob>, me), Q_ARG(QVariantMap, params));
78 } else {
79 setError(true);
80 setResult(false);
81 }
82}
83
84void StorageJob::resultSlot(StorageJob *job, const QVariant &result)
85{
86 if (job == this) {
87 if (result.userType() == QMetaType::QVariantMap) {
88 m_data = result.toMap();
89 }
90 setResult(result);
91 }
92}
93
94Plasma5Support::ServiceJob *Storage::createJob(const QString &operation, QVariantMap &parameters)
95{
96 if (m_clientName.isEmpty()) {
97 return nullptr;
98 }
99
100 return new StorageJob(m_clientName, operation, parameters, this);
101}
102
103// Storage implementation
104Storage::Storage(QObject *parent)
105 : Plasma5Support::Service(parent)
106 , m_clientName(QStringLiteral("data"))
107{
108 // search among parents for an applet or dataengine: if found call the table as its plugin name
109 QObject *parentObject = this;
110
111 while ((parentObject = parentObject->parent())) {
112 if (parentObject->inherits("Applet")) {
113 KPluginMetaData d = parentObject->property("pluginMetaData").value<KPluginMetaData>();
114 if (d.isValid()) {
115 m_clientName = d.pluginId();
116 break;
117 }
118 }
119
120 Plasma5Support::DataEngine *engine = qobject_cast<Plasma5Support::DataEngine *>(parentObject);
121 if (engine) {
122 m_clientName = engine->metadata().pluginId();
123 break;
124 }
125 }
126
127 m_clientName.replace(QLatin1Char('.'), QLatin1Char('_'));
128 m_clientName.replace(QLatin1Char('-'), QLatin1Char('_'));
129
130 setName(QStringLiteral("storage"));
131}
132
133Storage::~Storage()
134{
135}
136
137#include "moc_storage_p.cpp"
Data provider for plasmoids (Plasma plugins)
Definition dataengine.h:45
This class provides jobs for use with Plasma5Support::Service.
Definition servicejob.h:39
Namespace for everything in libplasma.
Definition datamodel.cpp:15
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QObject * parent() const const
bool isEmpty() const const
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QMap< QString, QVariant > toMap() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.