Bugfixes on Maemo5 platform
[quicknewsreader] / qml / QuickNewsReader / content / js / SettingsStorage.js
1 //SettingsStorage.js
2 .pragma library
3
4 // First, let's create a short helper function to get the database connection
5 function getDatabase() {
6      return openDatabaseSync("QuickNewsReader", "1.0", "SettingsStorageDatabase", 100000);
7 }
8
9 // At the start of the application, we can initialize the tables we need if they haven't been created yet
10 function initialize() {
11     var db = getDatabase();
12     db.transaction(
13         function(tx) {
14             // Create the settings table if it doesn't already exist
15             // If the table exists, this is skipped
16             tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
17       });
18 }
19
20 // This function is used to write a setting into the database
21 function setSetting(setting, value) {
22    // setting: string representing the setting name (eg: “username”)
23    // value: string representing the value of the setting (eg: “myUsername”)
24    var db = getDatabase();
25    var res = "";
26    db.transaction(function(tx) {
27         var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
28               //console.log(rs.rowsAffected)
29               if (rs.rowsAffected > 0) {
30                 res = "OK";
31               } else {
32                 res = "Error";
33               }
34         }
35   );
36   // The function returns “OK” if it was successful, or “Error” if it wasn't
37   return res;
38 }
39
40 // This function is used to retrieve a setting from the database
41 function getSetting(setting) {
42    var db = getDatabase();
43    var res="";
44    db.transaction(function(tx) {
45      var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
46      if (rs.rows.length > 0) {
47           res = rs.rows.item(0).value;
48      } else {
49          res = "Unknown";
50      }
51   })
52   // The function returns “Unknown” if the setting was not found in the database
53   // For more advanced projects, this should probably be handled through error codes
54   return res
55 }
56