core.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // ---------------------------------------------------------------------------
  2. //Copyright (C) 2008-2011 Nokia Corporation and/or its subsidiary(-ies).
  3. //All rights reserved.
  4. //Contact: Nokia Corporation (qt-info@nokia.com)
  5. //You may use the files in this folder under the terms of the BSD
  6. //license as follows:
  7. //Redistribution and use in source and binary forms, with or without
  8. //modification, are permitted provided that the following conditions are
  9. //met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright
  13. // notice, this list of conditions and the following disclaimer in
  14. // the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
  17. // the names of its contributors may be used to endorse or promote
  18. // products derived from this software without specific prior written
  19. // permission.
  20. //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. //A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. //OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. //SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. //LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. //DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. //THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // ---------------------------------------------------------------------------
  31. .pragma library
  32. var _db;
  33. function openDB() {
  34. _db = openDatabaseSync("KarmaDB","1.0","The Karma Way Database",1000000);
  35. createTableKarma();
  36. createTableUser();
  37. if (countUsers()==0){
  38. createDefaultUser();
  39. return 0
  40. }else
  41. return 1
  42. }
  43. function createTableKarma(){
  44. _db.transaction(
  45. function(tx){
  46. tx.executeSql("CREATE TABLE IF NOT EXISTS karma (id INTEGER PRIMARY KEY AUTOINCREMENT, points INTEGER, title TEXT, description TEXT, modified TEXT)");
  47. }
  48. )
  49. }
  50. function dropKamraTable()
  51. {
  52. _db.transaction(
  53. function(tx){
  54. tx.executeSql("DROP TABLE IF EXISTS karma");
  55. }
  56. )
  57. }
  58. function readKarmas()
  59. {
  60. var data = [];
  61. _db.readTransaction(
  62. function(tx){
  63. var rs = tx.executeSql("SELECT * FROM karma ORDER BY modified DESC");
  64. for (var i=0; i< rs.rows.length; i++) {
  65. data[i] = rs.rows.item(i);
  66. }
  67. }
  68. )
  69. return data;
  70. }
  71. function updateKarma(karmaItem)
  72. {
  73. _db.transaction(
  74. function(tx){
  75. tx.executeSql("UPDATE karma SET points = ? , \
  76. title = ?, description = ?, modified = ? \
  77. WHERE id = ?", [karmaItem.points, karmaItem.title, karmaItem.description,
  78. karmaItem.modified, karmaItem.id]);
  79. }
  80. )
  81. }
  82. function deleteKarma(id)
  83. {
  84. _db.transaction(
  85. function(tx){
  86. tx.executeSql("DELETE FROM karma WHERE id = ?", id);
  87. }
  88. )
  89. }
  90. function createKarma(karmaItem)
  91. {
  92. print ('creating karma record:' + karmaItem.title + ' ' +karmaItem.points + ' '+ karmaItem.description)
  93. _db.transaction(
  94. function(tx){
  95. tx.executeSql("INSERT INTO karma (points,title,description, modified) VALUES(?,?,?,?)",[ karmaItem.points, karmaItem.title, karmaItem.description, karmaItem.modified]);
  96. }
  97. )
  98. }
  99. function readKarmaList(model)
  100. {
  101. model.clear()
  102. //print ('reading karma list')
  103. _db.readTransaction(
  104. function(tx){
  105. var rs = tx.executeSql("SELECT * FROM karma ORDER BY modified DESC");
  106. for (var i=0; i< rs.rows.length; i++) {
  107. model.append(rs.rows.item(i))
  108. }
  109. }
  110. )
  111. }
  112. function readPeopleList(model)
  113. {
  114. model.clear()
  115. print ('reading people list')
  116. _db.readTransaction(
  117. function(tx){
  118. var rs = tx.executeSql("SELECT * FROM user ORDER BY name ASC");
  119. for (var i=0; i< rs.rows.length; i++) {
  120. model.append(rs.rows.item(i))
  121. }
  122. }
  123. )
  124. }
  125. function getGoodKarma()
  126. {
  127. var data=0
  128. _db.readTransaction(
  129. function(tx){
  130. var rs = tx.executeSql("SELECT SUM(points) AS good FROM karma WHERE points>0");
  131. if(rs.rows.length === 1 && rs.rows.item(0).good>0) {
  132. data = rs.rows.item(0).good
  133. print("Good: "+data)
  134. }
  135. }
  136. )
  137. return data;
  138. }
  139. function getBadKarma()
  140. {
  141. var data=0
  142. _db.readTransaction(
  143. function(tx){
  144. var rs = tx.executeSql("SELECT SUM(points) AS bad FROM karma WHERE points<0");
  145. if(rs.rows.length === 1 && rs.rows.item(0).bad>0) {
  146. data = rs.rows.item(0).bad
  147. print("Bad: "+data)
  148. }
  149. }
  150. )
  151. return data;
  152. }
  153. function readKarmaItem(karmaId) {
  154. var data
  155. _db.readTransaction(
  156. function(tx){
  157. var rs = tx.executeSql("SELECT * FROM karma WHERE id=?", [karmaId])
  158. if(rs.rows.length === 1) {
  159. data = rs.rows.item(0)
  160. }
  161. }
  162. )
  163. return data;
  164. }
  165. function createTableUser(){
  166. _db.transaction(
  167. function(tx){
  168. tx.executeSql("CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, image TEXT, description TEXT)");
  169. }
  170. )
  171. }
  172. function createDefaultUser()
  173. {
  174. //print ('creating karma record:' + karmaItem.title + ' ' +karmaItem.points)
  175. _db.transaction(
  176. function(tx){
  177. tx.executeSql("INSERT INTO user (name, image, description) VALUES(\"Your name\",\"qrc:/violet/face\",\"I'm a karmic person\")");
  178. }
  179. )
  180. }
  181. function updateUser(UserItem)
  182. {
  183. _db.transaction(
  184. function(tx){
  185. tx.executeSql("UPDATE user SET name = ? , image = ? , description=? \
  186. WHERE id = ?", [UserItem.name, UserItem.image, UserItem.description, UserItem.id]);
  187. }
  188. )
  189. }
  190. function createPlayer(UserItem)
  191. {
  192. _db.transaction(
  193. function(tx){
  194. tx.executeSql("INSERT INTO user (name, image, description) VALUES(?,?,?)", [UserItem.name, UserItem.image, UserItem.description]);
  195. }
  196. )
  197. }
  198. function readUserItem(userId) {
  199. var data
  200. _db.readTransaction(
  201. function(tx){
  202. var rs = tx.executeSql("SELECT * FROM user WHERE id=?", [userId])
  203. if(rs.rows.length === 1) {
  204. data = rs.rows.item(0)
  205. }
  206. }
  207. )
  208. return data;
  209. }
  210. function countUsers() {
  211. var data=0
  212. _db.readTransaction(
  213. function(tx){
  214. var rs = tx.executeSql("SELECT COUNT(*) AS number FROM user")
  215. if(rs.rows.length === 1) {
  216. data = rs.rows.item(0).number
  217. }
  218. }
  219. )
  220. return data;
  221. }
  222. function printObject(o)
  223. {
  224. print('---')
  225. print(Object.keys(o))
  226. for(var key in o) {
  227. print(" " + key +"="+o[key])
  228. }
  229. }
  230. /*
  231. function countBox(boxId)
  232. {
  233. var data = 0;
  234. _db.readTransaction(
  235. function(tx){
  236. var rs = tx.executeSql("SELECT count(*) FROM todo WHERE box = ?",[boxId]);
  237. if(rs.rows.length === 1) {
  238. data = rs.rows.item(0)["count(*)"]
  239. }
  240. }
  241. )
  242. return data;
  243. }
  244. function createBatchTodos(data)
  245. {
  246. _db.transaction(
  247. function(tx){
  248. for(var i=0; i<data.length; i++) {
  249. var item = data[i]
  250. tx.executeSql("INSERT INTO todo (box, done, title, note, modified) VALUES(?,?,?,?,?)",[item.box, item.done, item.title, item.note, item.modified]);
  251. }
  252. }
  253. )
  254. }
  255. function clearArchive(model)
  256. {
  257. model.clear()
  258. _db.transaction(
  259. function(tx){
  260. tx.executeSql("DELETE FROM karma WHERE box = 4");
  261. }
  262. )
  263. }
  264. function updateArchive(box)
  265. {
  266. _db.transaction(
  267. function(tx){
  268. tx.executeSql("UPDATE todo SET box = 4 WHERE done = 'true' AND box=?",box);
  269. }
  270. )
  271. }
  272. */
  273. function defaultItem()
  274. {
  275. return {points: 0, title: "", modified: new Date()}
  276. }
  277. function defaultUserItem()
  278. {
  279. return {id: 1, name: "", image: ""}
  280. }