Changeset View
Changeset View
Standalone View
Standalone View
plugins/welcomepage/qml/storage.js
- This file was added.
1 | /* KDevelop | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2017 Kevin Funk <kfunk@kde.org> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License | ||||
7 | * as published by the Free Software Foundation; either version 2 | ||||
8 | * of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | * 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | .import QtQuick.LocalStorage 2.0 as Storage | ||||
22 | | ||||
23 | function getDatabase() { | ||||
24 | return Storage.LocalStorage.openDatabaseSync("WelcomePage", "0.1", "HelloAppDatabase", 100); | ||||
25 | } | ||||
26 | | ||||
27 | function createTable(tx) { | ||||
28 | tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)'); | ||||
29 | } | ||||
30 | | ||||
31 | function set(setting, value) { | ||||
32 | var db = getDatabase(); | ||||
33 | var res = false; | ||||
34 | db.transaction(function(tx) { | ||||
35 | createTable(tx); | ||||
36 | var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]); | ||||
37 | if (rs.rowsAffected > 0) { | ||||
38 | res = true; | ||||
39 | } else { | ||||
40 | res = false; | ||||
41 | } | ||||
42 | }); | ||||
43 | return res; | ||||
44 | } | ||||
45 | | ||||
46 | function get(setting, defaultValue) { | ||||
47 | var db = getDatabase(); | ||||
48 | var res = ""; | ||||
49 | try { | ||||
50 | db.transaction(function(tx) { | ||||
51 | createTable(tx); | ||||
52 | var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]); | ||||
53 | if (rs.rows.length > 0) { | ||||
54 | res = rs.rows.item(0).value; | ||||
55 | } else { | ||||
56 | res = defaultValue; | ||||
57 | } | ||||
58 | }); | ||||
59 | } catch (err) { | ||||
60 | console.log("Database error:" + err); | ||||
61 | res = defaultValue; | ||||
62 | }; | ||||
63 | return res | ||||
64 | } |