summaryrefslogtreecommitdiff
path: root/daemon/jskitobjects.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/jskitobjects.cpp')
-rw-r--r--daemon/jskitobjects.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/daemon/jskitobjects.cpp b/daemon/jskitobjects.cpp
new file mode 100644
index 0000000..fc9506d
--- /dev/null
+++ b/daemon/jskitobjects.cpp
@@ -0,0 +1,69 @@
+#include <QStandardPaths>
+#include <QDir>
+#include "jskitobjects.h"
+
+JSKitPebble::JSKitPebble(JSKitManager *mgr)
+ : QObject(mgr)
+{
+}
+
+void JSKitPebble::addEventListener(const QString &value, QJSValue callback)
+{
+ _callbacks[value].append(callback);
+}
+
+JSKitLocalStorage::JSKitLocalStorage(const QUuid &uuid, JSKitManager *mgr)
+ : QObject(mgr), _storage(new QSettings(getStorageFileFor(uuid), QSettings::IniFormat, this))
+{
+ _len = _storage->allKeys().size();
+}
+
+int JSKitLocalStorage::length() const
+{
+ return _len;
+}
+
+QJSValue JSKitLocalStorage::getItem(const QString &key) const
+{
+ QVariant value = _storage->value(key);
+ if (value.isValid()) {
+ return QJSValue(value.toString());
+ } else {
+ return QJSValue(QJSValue::NullValue);
+ }
+}
+
+void JSKitLocalStorage::setItem(const QString &key, const QString &value)
+{
+ _storage->setValue(key, QVariant::fromValue(value));
+ checkLengthChanged();
+}
+
+void JSKitLocalStorage::removeItem(const QString &key)
+{
+ _storage->remove(key);
+ checkLengthChanged();
+}
+
+void JSKitLocalStorage::clear()
+{
+ _storage->clear();
+ _len = 0;
+ emit lengthChanged();
+}
+
+void JSKitLocalStorage::checkLengthChanged()
+{
+ int curLen = _storage->allKeys().size();
+ if (_len != curLen) {
+ _len = curLen;
+ emit lengthChanged();
+ }
+}
+
+QString JSKitLocalStorage::getStorageFileFor(const QUuid &uuid)
+{
+ QDir dataDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
+ dataDir.mkdir("js-storage");
+ return dataDir.absoluteFilePath("js-storage/" + uuid.toString() + ".ini");
+}