Merge branch 'develop'
[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_nomodel(){
44     var db = get_db();
45     optsGlobal = [];
46
47     function readEntry(json) {
48         var parsed = JSON.parse(json);
49         optsGlobal.push(parsed);
50     }
51
52     db.transaction( function(tx) {
53                        try {
54                            var rs = tx.executeSql('SELECT * FROM Settings');
55                            for (var i = 0; i < rs.rows.length; i++) {
56                                readEntry(rs.rows.item(i).json)
57                            }
58                        } catch (error) {
59                            init_nomodel();
60                        }
61                    } );
62 }
63
64 function restore(objmodel){
65     var db = get_db();
66     objmodel.clear();
67
68     optsGlobal = [];
69
70     function readEntry(json) {
71         var parsed = JSON.parse(json);
72         optsGlobal.push(parsed);
73         objmodel.append(parsed);
74     }
75
76     db.transaction( function(tx) {
77                        try {
78                            var rs = tx.executeSql('SELECT * FROM Settings');
79                            for (var i = 0; i < rs.rows.length; i++) {
80                                readEntry(rs.rows.item(i).json)
81                            }
82                        } catch (error) {
83                            init(objmodel);
84                        }
85                    } );
86 }
87
88 function store(lopt){
89     var db = get_db();
90     db.transaction( function(tx) {
91                        try{tx.executeSql('DROP TABLE Settings;');}catch(e){};
92                               tx.executeSql('CREATE TABLE IF NOT EXISTS Settings(id INT, json TEXT)');
93                    });
94     for (var i=0;i<lopt.length;i++){
95         db.transaction( function(tx) {
96                            tx.executeSql('INSERT INTO Settings VALUES(?, ?)', [i+1, lopt[i]]);
97                        });
98     }
99 }
100
101 function save(obj){
102     var lopts = [];
103     optsGlobal = [];
104     for (var i=0; i < obj.count; i++) {
105         var newopt = "{";
106         var firstpartopt = obj.get(i);
107
108         for (var elem in firstpartopt) {
109              if (!elem.match("^attributes")) {
110                  newopt += "\""+elem+"\": \"" + firstpartopt[elem] + "\",";
111              }
112         }
113         if (firstpartopt.attributes){
114              newopt += "\"attributes\":[";
115               for (var j=0; j < firstpartopt.attributes.count; j++) {
116                     if (j > 0) newopt += ",";
117                     var attr = firstpartopt.attributes.get(j);
118                      newopt += JSON.stringify(attr);
119               }
120               newopt += "]";
121         }else{
122             newopt = newopt.slice(0, -1);
123         }
124          newopt += "}";
125         lopts.push(newopt);
126         optsGlobal.push( JSON.parse(newopt));
127     }
128     store(lopts);
129 }
130
131 function init_nomodel(){
132     var listopts = init_default();
133     optsGlobal = [];
134     for(var i=0;i< listopts.length;i++){
135         optsGlobal.push(listopts[i]);
136     }
137 }
138
139 function init(obj){
140     var listopts = init_default();
141     optsGlobal = [];
142     for(var i=0;i< listopts.length;i++){
143         obj.append(listopts[i]);
144         optsGlobal.push(listopts[i]);
145     }
146 }
147
148 function pressandhold(){
149     return get_opt(4);
150 }
151
152 function doubleclicked(){
153     return get_opt(3);
154 }
155
156 function chedoxoay(){
157     console.log(optsGlobal)
158     return get_opt(1);
159 }
160
161 function get_opt(idm){
162     for(var i=0;i<optsGlobal.length;i++){
163         if (optsGlobal[i].idm == idm){
164                 if(optsGlobal[i].selected == "true" || optsGlobal[i].selected === true)
165                     return 1;
166         }
167     }
168    return 0;
169 }