From c298170f30756226ad26d965af41bfbf2339f7c3 Mon Sep 17 00:00:00 2001 From: Michael Prager Date: Mon, 22 Mar 2010 16:28:18 +0100 Subject: [PATCH] added persitent settings feature --- Readme.txt | 3 + comingNext/index.html | 166 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 145 insertions(+), 24 deletions(-) diff --git a/Readme.txt b/Readme.txt index 134a4cd..978e0c6 100644 --- a/Readme.txt +++ b/Readme.txt @@ -191,3 +191,6 @@ Changelog: - includes Skin Fetcher 1.2 which now also supports 3 panels 1.27 () by Michael Prager - fixed ToDo events without due date to cause error + - added persistent settings. Settings will no longer be lost when updating + the widget. Also, all widgets now use the same settings, you no longer have + to modify the settings in each widget individually. diff --git a/comingNext/index.html b/comingNext/index.html index 05498ab..5321d5a 100644 --- a/comingNext/index.html +++ b/comingNext/index.html @@ -76,6 +76,10 @@ var orientation = ''; var now = new Date(); var mode = 0; // 0 = homescreen, 1 = fullscreen, 2 = settings, 3 = about, 4 = check for update var reqV = null; +var settingsCalEntryId = null; +var settingsCache = null; +var notificationRequest1; +var notificationRequest2; // vars for daylight saving time var daylightsavingWinter = 0; @@ -277,9 +281,23 @@ function requestNotification() criteria.Type = "CalendarEntry"; try { - var result = calendarService.IDataSource.RequestNotification(criteria, callback); - if (result.ErrorCode) - error('loading Calendar items list'); + notificationRequest1 = calendarService.IDataSource.RequestNotification(criteria, callback); + if (notificationRequest1.ErrorCode) + error('requestNotification failed with error code ' + notificationRequest1.ErrorCode); + } catch (e) { + error("requestNotification: " + e + ', line ' + e.line); + } + + var criteria2 = new Object(); + criteria2.Type = "CalendarEntry"; + criteria2.Filter = new Object(); + criteria2.Filter.LocalIdList = new Array(); + criteria2.Filter.LocalIdList[0] = settingsCalEntryId; + + try { + notificationRequest2 = calendarService.IDataSource.RequestNotification(criteria2, settingsCallback); + if (notificationRequest2.ErrorCode) + error('requestNotification failed with error code ' + notificationRequest2.ErrorCode); } catch (e) { error("requestNotification: " + e + ', line ' + e.line); } @@ -287,9 +305,16 @@ function requestNotification() function callback(transId, eventCode, result) { + console.info("callback(): panelNum: %d transId: %d eventCode: %d result.ErrorCode: %d", panelNum, transId, eventCode, result.ErrorCode); updateData(); } +function settingsCallback(transId, eventCode, result) +{ + console.info("settingsCallback(): panelNum: %d transId: %d eventCode: %d result.ErrorCode: %d", panelNum, transId, eventCode, result.ErrorCode); + loadSettings(); +} + function parseDate(dateString) { /* @@ -713,7 +738,6 @@ function init() } loadSettings(); - updateCssClasses(); collectLocales(); //updateData(); requestNotification(); @@ -829,6 +853,53 @@ function updateCssClasses() } } +function getSettingsCalEntryId() +{ + if (settingsCalEntryId == null) { + // check if entry already exists + var listFiltering = { + Type:'CalendarEntry', + Filter:{ + StartRange: new Date(2000, 0, 1), + EndRange: new Date(2000, 0, 1), + SearchText: 'ComingNext Settings|', + Type: 'DayEvent' + } + } + var result = calendarService.IDataSource.GetList(listFiltering); + if (result.ErrorCode) { + error(result.ErrorMessage); + return; + } + var list = result.ReturnValue; + var entry = list.getNext(); + if (entry != undefined) { + settingsCalEntryId = entry.LocalId; + console.info("settingsCalEntryId=" + settingsCalEntryId); + } + else { // create settings item + var item = new Object(); + item.Type = "DayEvent"; + item.StartTime = new Date(2000, 0, 1); + item.Summary = "ComingNext Settings|"; + + var criteria = new Object(); + criteria.Type = "CalendarEntry"; + criteria.Item = item; + + try { + var result = calendarService.IDataSource.Add(criteria); + if (result.ErrorCode) + error(result.ErrorMessage); + } catch (e) { + error("getSettingsCalEntryId: " + e + ', line ' + e.line); + } + + getSettingsCalEntryId(); + } + } +} + function restoreDefaultSettings() { for (var key in config) @@ -837,38 +908,85 @@ function restoreDefaultSettings() function loadSettings() { - for (var key in config) { - if (widget.preferenceForKey(key)) { - if (config[key].Type == 'Int') - config[key].Value = Number(widget.preferenceForKey(key)); - else if (config[key].Type == 'String') - config[key].Value = widget.preferenceForKey(key); - else if (config[key].Type == 'Bool') - config[key].Value = (widget.preferenceForKey(key) == 'true') - else if (config[key].Type == 'Enum') - config[key].Value = widget.preferenceForKey(key); - else if (config[key].Type == 'UID') - config[key].Value = Number(widget.preferenceForKey(key)); + getSettingsCalEntryId(); + var listFiltering = { + Type:'CalendarEntry', + Filter:{ + LocalId: settingsCalEntryId } - else - config[key].Value = config[key].Default; - console.info('Settings: ' + key + '=\'' + config[key].Value + '\''); + } + var result = calendarService.IDataSource.GetList(listFiltering); + if (result.ErrorCode) { + error(result.ErrorMessage); + return; + } + var entry = result.ReturnValue.getNext(); + if (entry != undefined) { + // only reload settings if they chanced since the last reload + if (settingsCache != entry.Summary) + { + restoreDefaultSettings(); + var stringlist = entry.Summary.split("|"); + // skip the first two entries, those contain header and version info + for(var i = 2; i < stringlist.length - 1; i++) { + var pair = stringlist[i].split('='); + var key = pair[0]; + var value = pair[1]; + console.info('stringlist: ' + key + '=\'' + value + '\''); + if (config[key].Type == 'Int') + config[key].Value = Number(value); + else if (config[key].Type == 'String') + config[key].Value = value; + else if (config[key].Type == 'Bool') + config[key].Value = (value == 'true') + else if (config[key].Type == 'Enum') + config[key].Value = value; + else if (config[key].Type == 'UID') + config[key].Value = Number(value); + } + settingsCache = entry.Summary; + updateCssClasses(); + } + } + else { + error("Failed to load settings, calendar entry could not be found"); } } function saveSettings() { + getSettingsCalEntryId(); + var item = new Object(); + item.Type = "DayEvent"; + item.StartTime = new Date(2000, 0, 1); + item.LocalId = settingsCalEntryId; + item.Summary = "ComingNext Settings|" + version + "|"; + for (var key in config) { if (config[key].Type == 'Int') - widget.setPreferenceForKey(config[key].Value.toString(), key); + item.Summary += key + "=" + config[key].Value.toString() + "|"; else if (config[key].Type == 'String') - widget.setPreferenceForKey(config[key].Value, key); + item.Summary += key + "=" + config[key].Value + "|"; else if (config[key].Type == 'Bool') - widget.setPreferenceForKey(config[key].Value ? 'true' : 'false', key); + item.Summary += key + "=" + (config[key].Value ? 'true' : 'false') + "|"; else if (config[key].Type == 'Enum') - widget.setPreferenceForKey(config[key].Value, key); + item.Summary += key + "=" + config[key].Value + "|"; else if (config[key].Type == 'UID') - widget.setPreferenceForKey(config[key].Value.toString(), key); + item.Summary += key + "=" + config[key].Value.toString() + "|"; + } + settingsCache = item.Summary; + + var criteria = new Object(); + criteria.Type = "CalendarEntry"; + criteria.Item = item; + + console.info("Saving settings to calendar entry: " + item.Summary); + try { + var result = calendarService.IDataSource.Add(criteria); + if (result.ErrorCode) + error(result.ErrorMessage); + } catch (e) { + error("saveSettings: " + e + ', line ' + e.line); } } -- 2.39.2