not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
13 KiB

  1. 
  2. // Settings handling
  3. define(["sugar-web/datastore"], function (datastore) {
  4. var settings = {};
  5. // Default value
  6. settings.init = function() {
  7. this.name = "<No name>";
  8. this.color = 22;
  9. this.colorvalue = null;
  10. this.activities = [];
  11. this.view = 0;
  12. this.language = "en";
  13. var navigatorLanguage = navigator.language;
  14. if (navigatorLanguage) {
  15. if (navigatorLanguage.indexOf("fr") != -1)
  16. this.language = "fr";
  17. else if (navigatorLanguage.indexOf("es") != -1)
  18. this.language = "es";
  19. else if (navigatorLanguage.indexOf("de") != -1)
  20. this.language = "de";
  21. else if (navigatorLanguage.indexOf("ar") != -1)
  22. this.language = "ar";
  23. else if (navigatorLanguage.indexOf("ja") != -1)
  24. this.language = "ja";
  25. else if (navigatorLanguage.indexOf("pl") != -1)
  26. this.language = "pl";
  27. else if (navigatorLanguage.indexOf("pt") != -1)
  28. this.language = "pt";
  29. }
  30. this.options = {stats: true, sync: true};
  31. this.connected = false;
  32. this.server = null;
  33. this.networkId = null;
  34. this.privateJournal = null;
  35. this.sharedJournal = null;
  36. this.genericActivity = {directory: "icons", icon: "application-x-generic.svg"};
  37. this.token = null;
  38. this.histoy = null;
  39. }
  40. settings.init();
  41. // Load settings from local storage
  42. settings.load = function(then) {
  43. // Load settings
  44. var that = this;
  45. datastore.localStorage.load(function() {
  46. that.history = datastore.localStorage.getValue('sugar_history');
  47. var preferences = datastore.localStorage.getValue('sugar_settings');
  48. if (preferences == null || preferences.name === undefined) {
  49. if (then) {
  50. then(false);
  51. }
  52. return;
  53. }
  54. that.name = preferences.name;
  55. that.color = preferences.color;
  56. that.colorvalue = preferences.colorvalue;
  57. that.view = preferences.view;
  58. if (preferences.language !== undefined) {
  59. that.language = preferences.language;
  60. }
  61. if (preferences.connected !== undefined) {
  62. that.connected = preferences.connected;
  63. }
  64. if (preferences.server !== undefined) {
  65. that.server = preferences.server;
  66. }
  67. if (preferences.networkId !== undefined) {
  68. that.networkId = preferences.networkId;
  69. }
  70. if (preferences.privateJournal !== undefined) {
  71. that.privateJournal = preferences.privateJournal;
  72. }
  73. if (preferences.sharedJournal !== undefined) {
  74. that.sharedJournal = preferences.sharedJournal;
  75. }
  76. if (preferences.token !== undefined) {
  77. that.token = preferences.token;
  78. }
  79. if (preferences.options !== undefined) {
  80. that.options = preferences.options;
  81. }
  82. // Set language
  83. l10n.language.code = that.language;
  84. // Load activities
  85. that.activities = preferences.activities;
  86. // Load datastore entries for each activity
  87. that.updateEntries();
  88. if (then) {
  89. then(true);
  90. }
  91. });
  92. };
  93. // Update entries for each activity
  94. settings.updateEntries = function() {
  95. var length = this.activities ? this.activities.length : 0;
  96. for (var i = 0 ; i < length ; i++) {
  97. var activity = this.activities[i];
  98. activity.instances = datastore.find(activity.id);
  99. activity.instances.sort(function(e0, e1) {
  100. return parseInt(e1.metadata.timestamp) - parseInt(e0.metadata.timestamp);
  101. });
  102. }
  103. };
  104. // Save settings to local storage
  105. settings.save = function() {
  106. datastore.localStorage.setValue('sugar_settings', {
  107. name: this.name,
  108. color: this.color,
  109. options: this.options,
  110. colorvalue: xoPalette.colors[this.color],
  111. activities: this.activities,
  112. view: app.getView(),
  113. language: this.language,
  114. connected: this.connected,
  115. server: this.server,
  116. networkId: this.networkId,
  117. privateJournal: this.privateJournal,
  118. sharedJournal: this.sharedJournal,
  119. token: this.token
  120. });
  121. };
  122. // Save settings to the server
  123. settings.saveToServer = function(remoteserver, then, error) {
  124. if (this.networkId != null) {
  125. var that = this;
  126. remoteserver.putUser(
  127. this.networkId,
  128. {
  129. name: that.name,
  130. color: xoPalette.colors[that.color],
  131. language: that.language,
  132. options: that.options,
  133. favorites: that.getFavoritesActivitiesName()
  134. },
  135. function(inSender, inResponse) {
  136. if (then) {
  137. then();
  138. }
  139. },
  140. function(inResponse, inCode) {
  141. console.log("WARNING: Error updating network user");
  142. if (error) {
  143. error(inResponse, inCode);
  144. }
  145. }
  146. );
  147. }
  148. else {
  149. if (then) {
  150. then();
  151. }
  152. }
  153. };
  154. // Merge current settings with the one in parameter
  155. settings.merge = function(preferences) {
  156. var changed = false;
  157. if (preferences.name !== undefined && preferences.name != this.name) { this.name = preferences.name; changed = true; }
  158. if (this.colorvalue == null || preferences.color !== undefined && (preferences.color.fill != this.colorvalue.fill || preferences.color.stroke != this.colorvalue.stroke)) {
  159. this.colorvalue = preferences.color;
  160. this.color = util.getColorIndex(this.colorvalue);
  161. changed = true;
  162. }
  163. if (preferences.language !== undefined && preferences.language != this.language) { this.language = preferences.language; changed = true; }
  164. if (preferences.favorites !== undefined) {
  165. var favoriteCount = 0;
  166. for(var i = 0 ; i < this.activities.length ; i++) {
  167. var wasfavorite = this.activities[i].favorite;
  168. this.activities[i].favorite = false;
  169. for (var j = 0 ; j < preferences.favorites.length ; j++) {
  170. if (preferences.favorites[j] == this.activities[i].id) {
  171. this.activities[i].favorite = true;
  172. }
  173. }
  174. changed = changed || (wasfavorite != this.activities[i].favorite);
  175. }
  176. }
  177. if (preferences.server !== undefined && preferences.server != this.server) { this.server = preferences.server; changed = true; }
  178. if (preferences.networkId !== undefined && preferences.networkId != this.networkId) { this.networkId = preferences.networkId; changed = true; }
  179. if (preferences.private_journal !== undefined && preferences.private_journal != this.privateJournal) { this.privateJournal = preferences.private_journal; }
  180. if (preferences.shared_journal !== undefined && preferences.shared_journal != this.sharedJournal) { this.sharedJournal = preferences.shared_journal; }
  181. if (preferences.options !== undefined && this.options !== undefined) {
  182. for(var prop in preferences.options) {
  183. if (!this.options.hasOwnProperty(prop) || (this.options[prop] != preferences.options[prop])) {
  184. this.options[prop] = preferences.options[prop];
  185. changed = true;
  186. }
  187. }
  188. }
  189. return changed;
  190. };
  191. // Reset settings
  192. settings.reset = function(full) {
  193. datastore.localStorage.removeValue('sugar_settings');
  194. if (full) {
  195. datastore.localStorage.removeValue('sugar_history');
  196. datastore.localStorage.removeValue('sugar_stats');
  197. }
  198. };
  199. // Get properties
  200. settings.getName = function() {
  201. return this.name;
  202. };
  203. settings.getColor = function() {
  204. return xoPalette.colors[this.color];
  205. };
  206. settings.getView = function() {
  207. return this.view;
  208. };
  209. settings.getLanguage = function() {
  210. return this.language;
  211. };
  212. settings.getActivities = function() {
  213. return this.activities;
  214. };
  215. settings.getActivitiesByName = function(name) {
  216. if (name === undefined || name == null || name.length == 0) {
  217. return this.activities;
  218. }
  219. var activities = this.activities;
  220. var matching = [];
  221. for (var i = 0 ; i < activities.length ; i++) {
  222. if (activities[i].name.toLowerCase().indexOf(name) != -1) {
  223. matching.push(activities[i]);
  224. }
  225. }
  226. return matching;
  227. };
  228. settings.getActivity = function(id) {
  229. for(var i = 0 ; i < this.activities.length ; i++) {
  230. if (this.activities[i].id == id) {
  231. return this.activities[i];
  232. }
  233. }
  234. return this.genericActivity;
  235. };
  236. settings.getFavoritesActivities = function() {
  237. var favorites = [];
  238. for(var i = 0 ; i < this.activities.length ; i++) {
  239. if (this.activities[i].favorite) {
  240. favorites.push(this.activities[i]);
  241. }
  242. }
  243. return favorites;
  244. };
  245. settings.getFavoritesActivitiesName = function() {
  246. var favorites = this.getFavoritesActivities();
  247. var names = [];
  248. for(var i = 0 ; i < favorites.length ; i++) {
  249. names.push(favorites[i].id);
  250. }
  251. return names;
  252. };
  253. settings.isConnected = function() {
  254. return this.connected || util.getClientType() == constant.webAppType;
  255. };
  256. settings.getServer = function() {
  257. return this.server;
  258. };
  259. settings.getNetworkId = function() {
  260. var token = this.token;
  261. if (!token || !token.x_key) {
  262. return null;
  263. }
  264. return token.x_key;
  265. };
  266. settings.getPrivateJournal = function() {
  267. return this.privateJournal;
  268. };
  269. settings.getSharedJournal = function() {
  270. return this.sharedJournal;
  271. };
  272. settings.getToken = function() {
  273. return this.token;
  274. };
  275. settings.getHistory = function() {
  276. return this.history;
  277. };
  278. settings.getOptions = function(name) {
  279. if (!this.options) return undefined;
  280. return this.options[name];
  281. }
  282. // Set properties
  283. settings.setName = function(newname) {
  284. this.name = newname;
  285. };
  286. settings.setColor = function(newcolor) {
  287. if (newcolor >= 0 && newcolor < xoPalette.colors.length) {
  288. this.color = newcolor;
  289. }
  290. };
  291. settings.setLanguage = function(newlanguage) {
  292. this.language = newlanguage;
  293. };
  294. settings.setActivities = function(list) {
  295. this.activities = list;
  296. };
  297. settings.setConnected = function(connected) {
  298. this.connected = connected;
  299. };
  300. settings.setServer = function(server) {
  301. this.server = server;
  302. };
  303. settings.setNetworkId = function(nid) {
  304. this.networkId = nid;
  305. };
  306. settings.setPrivateJournal = function(privateJournal) {
  307. this.privateJournal = privateJournal;
  308. };
  309. settings.setSharedJournal = function(sharedJournal) {
  310. this.sharedJournal = sharedJournal;
  311. };
  312. settings.setToken = function(token) {
  313. this.token = token;
  314. };
  315. settings.setOptions = function(optionName, optionValue) {
  316. if (!this.options) { this.options = {} }
  317. this.options[optionName] = optionValue;
  318. };
  319. settings.addUserInHistory = function() {
  320. var user = { name: this.name, color: this.color, server: this.server };
  321. var history = !this.history ? [] : this.history;
  322. for (var i = 0 ; i < history.length ; i++) {
  323. if (this.name.toLowerCase() == history[i].name.toLowerCase() &&
  324. ((this.server == null && history[i].server == null) ||
  325. (this.server && this.server.url && history[i].server && history[i].server.url && this.server.url == history[i].server.url))
  326. ) {
  327. history[i] = history[history.length-1];
  328. history[history.length-1] = user;
  329. datastore.localStorage.setValue('sugar_history', history)
  330. return;
  331. }
  332. }
  333. history.push(user);
  334. if (history.length > constant.maxUserHistory) {
  335. var newHistory = [];
  336. for (var i = 0 ; i < history.length-1 ; i++) {
  337. newHistory.push(history[i+1]);
  338. }
  339. history = newHistory;
  340. }
  341. datastore.localStorage.setValue('sugar_history', history);
  342. };
  343. // Update activities
  344. settings.updateActivities = function(list) {
  345. // Some activities updated, update it
  346. var updated = false;
  347. var seen = 0;
  348. for (var i = 0 ; i < list.length ; i++) {
  349. var found = false;
  350. for (var j = 0 ; !found && j < this.activities.length ; j++) {
  351. if (list[i].id == this.activities[j].id) {
  352. found = true;
  353. seen++;
  354. if (this.activities[j].version != list[i].version) {
  355. this.activities[j].name = list[i].name;
  356. this.activities[j].version = list[i].version;
  357. this.activities[j].directory = list[i].directory;
  358. this.activities[j].icon = list[i].icon;
  359. updated = true;
  360. }
  361. }
  362. }
  363. if (!found) {
  364. this.activities.push(list[i]);
  365. updated = true;
  366. }
  367. }
  368. // Some activities disappeared, remove it
  369. if (seen != this.activities.length) {
  370. updated = true;
  371. var newactivities = [];
  372. for (var i = 0 ; i < this.activities.length ; i++) {
  373. var found = false;
  374. for (var j = 0 ; !found && j < list.length ; j++) {
  375. if (this.activities[i].id == list[j].id || this.activities[i].type == "native") {
  376. found = true;
  377. newactivities.push(this.activities[i]);
  378. }
  379. }
  380. }
  381. this.activities = newactivities;
  382. }
  383. return updated;
  384. };
  385. // Color playing
  386. settings.nextColor = function() {
  387. this.color = (this.color+1)%xoPalette.colors.length;
  388. };
  389. // Activity handling
  390. settings.runActivity = function(activity, objectId, name, sharedId, help) {
  391. if (activity.type != null && activity.type == "native") {
  392. if (sugarizerOS) {
  393. sugarizerOS.runActivity(activity.id);
  394. sugarizerOS.addApplicationToJournal(sugarizerOS.log, activity, datastore);
  395. return;
  396. }
  397. console.log("No sugarizerOS instance found");
  398. }
  399. if (activity.activityId == null) {
  400. activity.activityId = datastore.createUUID();
  401. }
  402. this.save();
  403. var location = activity.directory+"/index.html?aid="+activity.activityId+"&a="+activity.id;
  404. if (objectId === undefined) {
  405. if (activity.instances != null && activity.instances.length > 0) {
  406. objectId = activity.instances[0].objectId;
  407. location = location + "&o="+objectId + "&n="+activity.instances[0].metadata.title;
  408. } else {
  409. location = location + "&n=" +activity.name;
  410. }
  411. } else if (objectId != null) {
  412. location = location + "&o=" + objectId + "&n="+name;
  413. } else {
  414. location = location + "&n=" +activity.name;
  415. }
  416. if (sharedId) {
  417. location = location + "&s=" + sharedId;
  418. }
  419. if (help) {
  420. location = location + "&h=1";
  421. }
  422. stats.trace(constant.viewNames[app.getView()], (objectId ? 're' : '') + 'launch_activity', activity.id, objectId);
  423. window.location = location;
  424. };
  425. settings.switchFavoriteActivity = function(activity) {
  426. activity.favorite = !activity.favorite;
  427. return activity.favorite;
  428. };
  429. return settings;
  430. });