Licensing GPLv3
[quicknewsreader] / qml / QuickNewsReader / content / js / SettingsStorage.js
1 /***
2 ** Copyright (C) 2012 Christophe CHAPUIS <chris.chapuis _at_ gmail _dot_ com>
3 **
4 ** This package is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This package is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this package; if not, write to the Free Software
16 ** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17 **
18 ***/
19 //SettingsStorage.js
20 .pragma library
21
22 // First, let's create a short helper function to get the database connection
23 function getDatabase() {
24      return openDatabaseSync("QuickNewsReader", "1.0", "SettingsStorageDatabase", 100000);
25 }
26
27 // At the start of the application, we can initialize the tables we need if they haven't been created yet
28 function initialize() {
29     var db = getDatabase();
30     db.transaction(
31         function(tx) {
32             // Create the settings table if it doesn't already exist
33             // If the table exists, this is skipped
34             tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
35       });
36 }
37
38 // This function is used to write a setting into the database
39 function setSetting(setting, value) {
40    // setting: string representing the setting name (eg: “username”)
41    // value: string representing the value of the setting (eg: “myUsername”)
42    var db = getDatabase();
43    var res = "";
44    db.transaction(function(tx) {
45         var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
46               //console.log(rs.rowsAffected)
47               if (rs.rowsAffected > 0) {
48                 res = "OK";
49               } else {
50                 res = "Error";
51               }
52         }
53   );
54   // The function returns “OK” if it was successful, or “Error” if it wasn't
55   return res;
56 }
57
58 // This function is used to retrieve a setting from the database
59 function getSetting(setting) {
60    var db = getDatabase();
61    var res="";
62    db.transaction(function(tx) {
63      var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
64      if (rs.rows.length > 0) {
65           res = rs.rows.item(0).value;
66      } else {
67          res = "Unknown";
68      }
69   })
70   // The function returns “Unknown” if the setting was not found in the database
71   // For more advanced projects, this should probably be handled through error codes
72   return res
73 }
74