summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/jskit/jskitlocalstorage.cpp
blob: d69b6adf43c528a86e63d33932fcfff0e73566c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <QDesktopServices>
#include <QDir>
#include <QDebug>

#include "jskitlocalstorage.h"

JSKitLocalStorage::JSKitLocalStorage(QJSEngine *engine, const QString &storagePath, const QUuid &uuid):
    QObject(engine),
    m_engine(engine),
    m_storage(new QSettings(getStorageFileFor(storagePath, uuid), QSettings::IniFormat, this))
{
}

int JSKitLocalStorage::length() const
{
    return m_storage->allKeys().size();
}

QJSValue JSKitLocalStorage::getItem(const QJSValue &key) const
{
    QVariant value = m_storage->value(key.toString());

    if (value.isValid()) {
        return QJSValue(value.toString());
    } else {
        return QJSValue(QJSValue::NullValue);
    }
}

bool JSKitLocalStorage::setItem(const QJSValue &key, const QJSValue &value)
{
    m_storage->setValue(key.toString(), QVariant::fromValue(value.toString()));
    return true;
}

bool JSKitLocalStorage::removeItem(const QJSValue &key)
{
    if (m_storage->contains(key.toString())) {
        m_storage->remove(key.toString());
        return true;
    } else {
        return false;
    }
}

void JSKitLocalStorage::clear()
{
    m_storage->clear();
}

QJSValue JSKitLocalStorage::key(int index)
{
    QStringList allKeys = m_storage->allKeys();
    QJSValue key(QJSValue::NullValue);

    if (allKeys.size() > index) {
        key = QJSValue(allKeys[index]);
    }

    return key;
}

QJSValue JSKitLocalStorage::get(const QJSValue &proxy, const QJSValue &key) const
{
    Q_UNUSED(proxy);
    return getItem(key);
}

bool JSKitLocalStorage::set(const QJSValue &proxy, const QJSValue &key, const QJSValue &value)
{
    Q_UNUSED(proxy);
    return setItem(key, value);
}

bool JSKitLocalStorage::has(const QJSValue &proxy, const QJSValue &key)
{
    Q_UNUSED(proxy);
    return m_storage->contains(key.toString());
}

bool JSKitLocalStorage::deleteProperty(const QJSValue &proxy, const QJSValue &key)
{
    Q_UNUSED(proxy);
    return removeItem(key);
}

QJSValue JSKitLocalStorage::keys(const QJSValue &proxy)
{
    Q_UNUSED(proxy);

    QStringList allKeys = m_storage->allKeys();
    QJSValue keyArray = m_engine->newArray(allKeys.size());
    for (int i = 0; i < allKeys.size(); i++) {
        keyArray.setProperty(i, allKeys[i]);
    }

    return keyArray;
}

QJSValue JSKitLocalStorage::enumerate()
{
    return keys(0);
}

QString JSKitLocalStorage::getStorageFileFor(const QString &storageDir, const QUuid &uuid)
{
    QDir dataDir(storageDir + "/js-storage");
    if (!dataDir.exists() && !dataDir.mkpath(dataDir.absolutePath())) {
        qWarning() << "Error creating jskit storage dir";
        return QString();
    }

    QString fileName = uuid.toString();
    fileName.remove('{');
    fileName.remove('}');
    return dataDir.absoluteFilePath(fileName + ".ini");
}