242c91de32b96fa0f0795969f8456e126f8f9726
[lichviet] / qml / LichViet / Settings.js
1 /*
2 Copyright (C) 2011  by Cuong Le <metacuong@gmail.com>
3
4 This program 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 program 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 program.  If not, see <http://www.gnu.org/licenses/>
16 */
17
18 .pragma library
19
20 var optsGlobal = [];
21
22 function get_db(){
23     return openDatabaseSync("LichVietDB", "1.0", "LichVietDB SQL", 5000);
24 }
25
26 function delete_old_db(){
27     var db = get_db();
28     db.transaction( function(tx) { try {
29                            tx.executeSql('DROP TABLE Settings;');
30                        } catch(error) {} }
31                    );
32 }
33
34 function init_default(){
35     return [
36                 {idm:1,name:"Chế Độ Xoay",selected:true},
37                 {idm:2,name:"Lịch Hệ Thống",selected:false},
38                 {idm:3,name:"Nhấn 2 Lần Liên Tiếp<br>Tới Ngày Dương",selected:true},
39                 {idm:4,name:"Nhấn Giữ<br>Xem Ngày Tốt/Xấu ...",selected:true}
40             ];
41 }
42
43 function restore(objmodel){
44     var db = get_db();
45     objmodel.clear();
46
47     optsGlobal = [];
48
49     function readEntry(json) {
50         var parsed = JSON.parse(json);
51         optsGlobal.push(parsed);
52         objmodel.append(parsed);
53     }
54
55     db.transaction( function(tx) {
56                        try {
57                            var rs = tx.executeSql('SELECT * FROM Settings');
58                            for (var i = 0; i < rs.rows.length; i++) {
59                                readEntry(rs.rows.item(i).json)
60                            }
61                        } catch (error) {
62                            init(objmodel);
63                        }
64                    } );
65 }
66
67 function store(lopt){
68     var db = get_db();
69     db.transaction( function(tx) {
70                        try{tx.executeSql('DROP TABLE Settings;');}catch(e){};
71                               tx.executeSql('CREATE TABLE IF NOT EXISTS Settings(id INT, json TEXT)');
72                    });
73     for (var i=0;i<lopt.length;i++){
74         db.transaction( function(tx) {
75                            tx.executeSql('INSERT INTO Settings VALUES(?, ?)', [i+1, lopt[i]]);
76                        });
77     }
78 }
79
80 function save(obj){
81     var lopts = [];
82     optsGlobal = [];
83     for (var i=0; i < obj.count; i++) {
84         var newopt = "{";
85         var firstpartopt = obj.get(i);
86
87         for (var elem in firstpartopt) {
88              if (!elem.match("^attributes")) {
89                  newopt += "\""+elem+"\": \"" + firstpartopt[elem] + "\",";
90              }
91         }
92         if (firstpartopt.attributes){
93              newopt += "\"attributes\":[";
94               for (var j=0; j < firstpartopt.attributes.count; j++) {
95                     if (j > 0) newopt += ",";
96                     var attr = firstpartopt.attributes.get(j);
97                      newopt += JSON.stringify(attr);
98               }
99               newopt += "]";
100         }else{
101             newopt = newopt.slice(0, -1);
102         }
103          newopt += "}";
104         lopts.push(newopt);
105         optsGlobal.push( JSON.parse(newopt));
106     }
107     store(lopts);
108 }
109
110 function init(obj){
111     var listopts = init_default();
112     optsGlobal = [];
113     for(var i=0;i< listopts.length;i++){
114         obj.append(listopts[i]);
115         optsGlobal.push(listopts[i]);
116     }
117 }
118
119 function pressandhold(){
120     return get_opt(4);
121 }
122
123 function doubleclicked(){
124     return get_opt(3);
125 }
126
127 function chedoxoay(){
128     console.log(optsGlobal)
129     return get_opt(1);
130 }
131
132 function get_opt(idm){
133     for(var i=0;i<optsGlobal.length;i++){
134         if (optsGlobal[i].idm == idm){
135                 if(optsGlobal[i].selected == "true" || optsGlobal[i].selected == true)
136                     return 1;
137         }
138     }
139    return 0;
140 }