Debian Codegraph @

qml/js/db.js

raw
1 /*
2 * uNav http://launchpad.net/unav
3 * Copyright (C) 2015 JkB https://launchpad.net/~joergberroth
4 * Copyright (C) 2015 Marcos Alvarez Costales https://launchpad.net/~costales
5 *
6 * uNav is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * uNav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16 // Thanks http://askubuntu.com/questions/352157/how-to-use-a-sqlite-database-from-qml
17
18
19 function openDB() {
20 var db = LocalStorage.openDatabaseSync("storedkeys_db", "0.1", "Stored Authenticator Keys", 1000);
21 try {
22 db.transaction(function(tx) {
23 tx.executeSql('CREATE TABLE IF NOT EXISTS storedkeys( added DATE, identifier INTEGER PRIMARY KEY, keyJSON TEXT, description TEXT )');
24 });
25 } catch (err) {
26 console.log("Error creating table in database: " + err)
27 } return db
28 }
29
30 function storeKey(added, keyJSON, description) {
31 var db = openDB();
32 db.transaction(function(tx){
33 tx.executeSql('INSERT OR REPLACE INTO storedkeys (added, keyJSON, description) VALUES(?, ?, ?)', [added, keyJSON, description]);
34 });
35 }
36
37 function updateStoredKey(id, modified, url, desc) {
38 var db = openDB();
39
40 db.transaction(function(tx){
41 tx.executeSql('UPDATE storedkeys SET added=?, keyJSON=?, description=? WHERE identifier=?;', [modified, url, desc, id]);
42 });
43 }
44
45 function getKeys() {
46 var db = openDB();
47 var rs;
48 db.transaction(function(tx) {
49 rs = tx.executeSql('SELECT * FROM storedkeys');
50 });
51
52 return rs
53 }
54
55 function findKey(key) {
56 var db = openDB();
57 var rs;
58 db.transaction(function(tx) {
59 rs = tx.executeSql('SELECT * FROM storedkeys WHERE keyJSON like ?;', [key]);
60 });
61
62 if (rs.rows.length > 0) {
63 return true;
64 } else {
65 return false;
66 }
67 }
68
69 function deleteKey(id){
70 console.log("Deleting register id: " + id);
71 var db = openDB();
72 db.transaction(function(tx){
73 tx.executeSql('DELETE FROM storedkeys WHERE identifier=?;', [id]);
74 });
75 }
76
77 function updateCounter(id, difference){
78 var db = openDB();
79 var countString = "";
80 var updatedUrl = "";
81
82 db.transaction(function(tx){
83 var rs = tx.executeSql('SELECT * FROM storedkeys WHERE identifier=?;', [id]);
84 if (rs.rows.length > 0) {
85 var url = rs.rows.item(0).keyJSON;
86 countString = url.match(/counter=(.*?)\&/);
87 if (!countString) countString = url.match(/counter=(.*?)$/)[0];
88
89 //Check if last char is &
90 if (countString.slice(-1) == "&") countString = countString.slice(0,-1);
91
92 var counterValue = Number(countString.slice(8)) + difference;
93 url = url.replace(countString, "counter=" + counterValue.toString());
94 updatedUrl = url;
95
96 tx.executeSql('UPDATE storedkeys SET keyJSON=? WHERE identifier=?;', [url, id]);
97
98 } else console.log("Error: Key url db is empty");
99 });
100
101 return updatedUrl;
102 }