Commits

Stefan Scherfke committed 149fa8b

Version 0.3 with basic timer and auto update functionality finished.

  • Participants
  • Parent commits a08ec85
  • Tags 0.3

Comments (0)

Files changed (5)

 	<key>CFBundleDisplayName</key>
 	<string>Tea Timer</string>
 	<key>CFBundleVersion</key>
-	<string>0.1</string>
+	<string>0.3</string>
 	<key>MainHTML</key>
 	<string>TeaTimer.html</string>
 	<key>CloseBoxInsetX</key>
 	<string>120</string>
 	<key>AllowSystem</key>
 	<true/>
+	<key>AllowNetworkAccess</key>
+	<true/>
 </dict>
 </plist>

File Scripts/Front.js

+var front = null;
+
 /**
  * This class encapsules all functions for the front side of the widget.
  */
 		if (i > 0)
 			$('#' + val + 'Input').keypress(this.raiseOrLowerValue);
 	}
-}
+};
 	
 /**
  * Handles the onMouseDown and onMouseUp events for the start/pause button.
 	pressed = (event.type == 'mouseover' || event.type == 'mousedown');
 	play = timer.timerInterval ? false : true	;
 	front.setStartButton(play, pressed);
-}
+};
 
 /**;
  * Sets either the play or the pause button to the widget.
 	img += pressed ? '_pressed' : '';
 	img += '.png';
 	$('#start').attr('src', img);
-}
+};
 
 /**
  * Show the text input field to set an event or the time.
 	display.css('visiblity', 'hidden');
 	editor.get(0).focus();
 	editor.get(0).select();
-}
+};
 
 /**
  * Set the text from the input field to the display container and hide
 		display.css('visibility', 'visible');
 		editor.css('visibility', 'hidden');
 	}
-}
+};
 
 /**
  * Raise or lower the value in the hours, minutes or seconds field by one if
 		e.value = val + 1;
 	else if (event.keyCode == 40 && val > 0)
 		e.value = val -1;
-}
+};
 
 /**
  * Cycles through the four input elements if the user presses tab or shift-tab.
 	else
 		$('#' + event.data.nxt).click()
 	$(event.target).blur();
-}
+};
 
 /**
  * Update the timer display with the currently remaining time.
 	$('#hours').text(front.formatTwoDigits(timer.remainingHours));
 	$('#minutes').text(front.formatTwoDigits(timer.remainingMinutes));
 	$('#seconds').text(front.formatTwoDigits(timer.remainingSeconds));
-}
+};
 
 /**
  * Formats a number to to digits with leading zero.
 	if (number.length == 1)
 		number = '0' + number;
 	return number;
-}
+};
 
 /**
  * Resets the display to 00:00:00.
 	$('#hours').text('00');
 	$('#minutes').text('00');
 	$('#seconds').text('00');
-}
+};

File Scripts/Timer.js

+var timer = null;
+
+/**
+ * This class implements the countdown timer and the alarm function.
+ */
 function Timer() {
 	this._targetTime = null;
 	this._isTargetReached = false;
 			front.setStartButton(true, false);
 		}
 	}
-}
+};
 
 /**
  * The run timer action first calculates the remaining time, then updates
     timer.calculateRemainingTime();
     front.updateTimerDisplay();
     timer.checkForAlarm();
-}
+};
 
 /**
  * Calculate the remaining hours, minutes and seconds and write them to their
 	timer.remainingHours = parseInt(timer.remainingMinutes / 60);
 	timer.remainingSeconds %= 60;
 	timer.remainingMinutes %= 60;
-}
+};
 
 /**
  * Check if the target time is reach and alarm the user.
 		front.setStartButton(true, false);
         timer.alarm();
 	}
-}
+};
 
 /**
  * Alarm the user according to his preferences.
 		widget.system('/usr/bin/osascript -e \'say "' + message
 						+ '" using "Victoria"\'', null);
 	}
-}
+};

File Scripts/Updater.js

+var updater = null;
+
 /**
  * Everything needed for the auto update function and the update panel.
  */
-function Updater() {	
+function Updater() {
+	this.clientVersion = null;
+	this.updateCheck = true;
+	this.lastChecked = null;
+	this.url = 'http://xerxes.sofa-rockers.org/TeaTimer.txt';
+	
 	$('#no').bind('mouseover mousedown', function(event) {
 		$(event.target).attr('src', 'Images/cancelUpdate_pressed.png');});
 	$('#no').bind('mouseout mouseup', function(event) {
 		$('#update').css('display', 'none');
 	});
 }
+
+/**
+ * Check for updates.
+ *
+ * Currently checks once a day, I have to change it in a later release ...
+ */
+Updater.prototype.check = function() {
+	if (!updater.clientVersion)
+		updater.obtainVersion();
+
+	if (updater.updateCheck && (!updater.lastChecked
+			|| (new Date()).getDate() != updater.lastChecked.getDate())) {		
+		$.ajax({
+			url: updater.url,
+			type: 'GET',
+		 	dataType: 'string',
+		  	success: function(remoteVersion) {
+				if (remoteVersion > updater.clientVersion)
+					$('#update').css('display', 'block');
+			}
+		});
+				
+		updater.lastChecked = new Date();
+	}
+};
+
+/**
+ * Obtain the current client version from the Info.plist file and set the
+ * value to updater.clientVersion.
+ */
+Updater.prototype.obtainVersion = function() {
+	$.ajax({
+		type: "GET",
+		url: "Info.plist",
+		dataType: "xml",
+		success: function(data) {
+			updater.clientVersion =
+				$('plist > dict > key:contains("CFBundleVersion")', 
+					data).next('string').text();
+		}
+	});
+};
+
+
-var front = null;
-var updater = null;
-var timer = null;
 /**
  * Add all event listeners when the window is loaded.
  */
 	timer = new Timer();
 	front = new Front();
 	updater = new Updater();
+	
+	if (window.widget) {
+		widget.onremove = remove;
+		widget.onhide = hide;
+		widget.onshow = show;
+	}
 });
+
+/**
+ * Called when the widget has been removed from the Dashboard.
+ */
+function remove() {
+    // Stop any timers to prevent CPU usage
+    // Remove any preferences as needed
+    // widget.setPreferenceForKey(null, dashcode.createInstancePreferenceKey("your-key"));
+}
+
+/**
+ * Called when the widget has been hidden.
+ */
+function hide() {
+	// Don't stop the timer or you'll forget your tea!
+}
+
+/**
+ * Called when the widget has been shown.
+ */
+function show() {
+	updater.check();
+}
+
+/**
+ * Called when the info button is clicked to show the back of the widget.
+ *
+ * @param event: onClick event from the info button
+ */
+function showBack(event) {
+    var front = document.getElementById("front");
+    var back = document.getElementById("back");
+
+    if (window.widget) {
+        widget.prepareForTransition("ToBack");
+    }
+
+    front.style.display = "none";
+    back.style.display = "block";
+
+    if (window.widget) {
+        setTimeout('widget.performTransition();', 0);
+    }
+}
+
+/**
+ * Called when the done button is clicked from the back of the widget.
+ *
+ * @param event: onClick event from the done button
+ */
+function showFront(event) {
+    var front = document.getElementById("front");
+    var back = document.getElementById("back");
+
+    if (window.widget) {
+        widget.prepareForTransition("ToFront");
+    }
+
+    front.style.display="block";
+    back.style.display="none";
+
+    if (window.widget) {
+        setTimeout('widget.performTransition();', 0);
+    }
+}