OSCON/Main.js
changeset 7 97dcd250e5be
child 11 aaba47256eea
equal deleted inserted replaced
6:5e0dece09f96 7:97dcd250e5be
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // Symbian Foundation Example Code
       
     3 //
       
     4 // This software is in the public domain. No copyright is claimed, and you 
       
     5 // may use it for any purpose without license from the Symbian Foundation.
       
     6 // No warranty for any purpose is expressed or implied by the authors or
       
     7 // the Symbian Foundation. 
       
     8 // ////////////////////////////////////////////////////////////////////////////
       
     9 
       
    10 var currentFontSize = 14;
       
    11 var uiManager;
       
    12 var home;
       
    13 var mainView;
       
    14 var osconIcalUrl = new Array();
       
    15 var icalData = new Array();
       
    16 var icalReader = new Array();
       
    17 var osconDays = new Array();
       
    18 var http;
       
    19 var downloadDayIndex = -1;
       
    20 
       
    21 // Called from the onload event handler to initialize the widget.
       
    22 function init() {
       
    23 	
       
    24 	for (var i = 0; i < 5; i++) {
       
    25 		var osconDay = new Date()
       
    26 		osconDay.setFullYear(2009, 6, 20 + i);
       
    27 		osconDays.push(osconDay);
       
    28 		osconIcalUrl.push("OSCON200907" + (20+i) + ".ics");
       
    29 		icalData.push(null);
       
    30 		icalReader.push(null);
       
    31 	}
       
    32 	
       
    33     // set tab-navigation mode and show softkeys
       
    34     // (only if we are in the WRT environment)
       
    35     if (window.widget) {
       
    36 		//create about menu
       
    37 	
       
    38         widget.setNavigationEnabled(false);
       
    39         menu.showSoftkeys();
       
    40     }
       
    41 	
       
    42 	// create UI manager
       
    43 	uiManager = new UIManager();
       
    44 
       
    45 	home = new ListView(null,null);
       
    46 	
       
    47 	var homeViewImage = new ImageLabel(null, null, "oscon-home.png");
       
    48 	// hack to center image
       
    49 	homeViewImage.contentElement.style.textAlign = "center";
       
    50 	home.addControl(homeViewImage);
       
    51 
       
    52 	var homeViewImage2 = new ImageLabel(null, null, "logo.png");
       
    53 	// hack to center image
       
    54 	homeViewImage2.contentElement.style.textAlign = "center";
       
    55 	home.addControl(homeViewImage2);
       
    56 	
       
    57 	mainView = new ListView(null, "<img src=oscon-home.png>");
       
    58 	
       
    59 	
       
    60 	for (var i = 0; i < osconDays.length; i++) {
       
    61 		var button = new NavigationButton(i, "day"+(i+1)+".png", dateToString(osconDays[i]));
       
    62 		mainView.addControl(button);
       
    63 		button.addEventListener("ActionPerformed", function(event){
       
    64 			var clickedButton = event.source;
       
    65 			var clickedId = clickedButton.id;
       
    66 			showDay(clickedId, osconDays[clickedId], null);
       
    67 			});
       
    68 	}	
       
    69 	
       
    70 	home.previousView=mainView;
       
    71 	home.show();
       
    72 	
       
    73 	uiManager.showNotification(-1, "wait", "Please wait...", -1);
       
    74 	setTimeout(function(){	uiManager.hideNotification();mainView.show();}, 1000);
       
    75 }
       
    76 
       
    77 
       
    78 function showDay(dayIndex, date) {
       
    79 	downloadDayIndex = dayIndex;
       
    80 	if ( icalReader[dayIndex] == null ) {
       
    81 		downloadIcalData(dayIndex);
       
    82 	} else {
       
    83 		showList(date, null);
       
    84 	}
       
    85 }
       
    86 
       
    87 function downloadIcalData(dayIndex) {
       
    88 	downloadDayIndex = dayIndex;
       
    89 	uiManager.showNotification(-1, "wait", "Please wait...", -1);
       
    90 
       
    91 	http = new Ajax();
       
    92     http.onreadystatechange = function() { downloadStateChanged(); };
       
    93 
       
    94     // initiate the request
       
    95     http.open("GET", osconIcalUrl[downloadDayIndex], true);
       
    96     http.send(null);
       
    97 }
       
    98 
       
    99 function downloadStateChanged(){
       
   100 	// complete request?
       
   101     if (http.readyState == 4) {
       
   102         // attempt to get response status
       
   103         var responseStatus = null;
       
   104         try {
       
   105             responseStatus = http.status;
       
   106         } catch (noStatusException) {}
       
   107         
       
   108 		// are we being prompted for login?
       
   109 		icalData[downloadDayIndex] = http.responseText;
       
   110 		try {
       
   111 			dataAvailable(downloadDayIndex);
       
   112 //			savePreferences();
       
   113 		}catch(x) {
       
   114 			uiManager.showNotification(5000, "warning", "Error processing feed");
       
   115 		}
       
   116 		downloadInProgress = false;
       
   117     }
       
   118 }
       
   119 
       
   120 function dataAvailable(downloadDayIndex){
       
   121 	uiManager.showNotification(-1, "wait", "Parsing info...", -1);
       
   122 	// parse iCal
       
   123 	var reader = new iCalReader(); // Construction of the reader object.
       
   124 	reader.prepareData(icalData[downloadDayIndex]); // Prepare and set the data for the parser.
       
   125 	reader.parse(); // Parse the data.
       
   126 	reader.sort(); // Sort the data.
       
   127 	icalReader[downloadDayIndex] =  reader;
       
   128 	showList(osconDays[downloadDayIndex], null);
       
   129 	uiManager.hideNotification();
       
   130 }
       
   131 
       
   132 function showList(day, session) {
       
   133 	var list = new ListView(null, "<img src=oscon-home.png>");
       
   134 	if (day) {
       
   135 		var button = new ImageLabel(null, dateToString(day), "day"+(downloadDayIndex+1)+".png");
       
   136 		list.addControl(button);
       
   137 	}
       
   138 	if (session) {
       
   139 		var button = new ImageLabel(null, sessionTimeToString(session) + ", " + dateToString(session) , "session.png");
       
   140 		list.addControl(button);
       
   141 	}
       
   142 	
       
   143 	var myCalReader = icalReader[downloadDayIndex];
       
   144 	var events = myCalReader.getCalendar().getEvents(); // Get all events.
       
   145 	var num = myCalReader.getCalendar().getNrOfEvents();
       
   146 
       
   147 	var addedSessions = new Array();
       
   148 
       
   149 	
       
   150 	for(var i=0; i<num; i++) { // Loop through all events.
       
   151 	
       
   152 		var event = myCalReader.getCalendar().getEventAtIndex(i); // A single event.        
       
   153 		
       
   154 		// Get Javascript date for start and end time.
       
   155 		var startDate = event.getStartDate();
       
   156 		var altStartDate = null;
       
   157 		try {
       
   158 			altStartDate = event.getAltStartDate();
       
   159 		} catch(z) {
       
   160 			altStartDate = startDate; 
       
   161 		}
       
   162 		var timeZone = event.getTimeZone();
       
   163 		
       
   164 		if ( day ) {
       
   165 			if ( ! dayMatches(day, startDate) ) {
       
   166 				continue;
       
   167 			} else {
       
   168 				var haveit = false;
       
   169 				// if session is not in already, add it
       
   170 				for (var j = 0 ; j < addedSessions.length ; j++ ) {
       
   171 					if ( sessionMatches(addedSessions[j], altStartDate ) ) {
       
   172 						haveit = true;
       
   173 						break;
       
   174 					}
       
   175 				}
       
   176 				if (!haveit) {
       
   177 					// add it
       
   178 					addedSessions.push(altStartDate);
       
   179 					var button = new NavigationButton(altStartDate.getTime(), "session.png", "Session @ "+ sessionTimeToString(altStartDate));
       
   180 					button.addEventListener("ActionPerformed", function(event){
       
   181 						var clickedButton = event.source;
       
   182 						var clickedId = clickedButton.id;
       
   183 						var ed = new Date();
       
   184 						ed.setTime(clickedId);
       
   185 						showList(null, ed);
       
   186 					});
       
   187 					list.addControl(button);
       
   188 				}
       
   189 				continue;
       
   190 			}
       
   191 		}
       
   192 		else if ( ! sessionMatches(session, startDate) ) {
       
   193 			continue;
       
   194 		}
       
   195 
       
   196 
       
   197 		// add events
       
   198 		var endDate = event.getEndDate();
       
   199 		var location = event.getProperty("LOCATION");
       
   200 		var summary = event.getProperty("SUMMARY");
       
   201 		var description = event.getProperty("DESCRIPTION");
       
   202 		var url = event.getProperty("URL");
       
   203 
       
   204 		var buf = "";
       
   205 		buf += "<div class=\"FeedItemDate\">" ;
       
   206 		if ( location != null ) {
       
   207 			buf += location + ", ";
       
   208 		}
       
   209 		buf += sessionTimeToString(startDate) +"-" + sessionTimeToString(endDate) + " " + timeZone + "</div>";
       
   210 		buf += "<div class=\"FeedItemDescription\">" + description + "</div>";
       
   211 		if (url != null) {
       
   212 	        buf += "<div class=\"FeedItemLink\">";
       
   213             buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + url + "'); return false;\">";
       
   214             buf += "Read more...";
       
   215             buf += "</a>";
       
   216 	        buf += "</div>";
       
   217 		}
       
   218 		
       
   219 		var cp = new ContentPanel(null, null, null, true);
       
   220 
       
   221 		// initialize feed item control
       
   222 		cp.setCaption(summary);
       
   223 		cp.setContent(buf);
       
   224 		cp.setExpanded(false);
       
   225 		list.addControl(cp); 	
       
   226 	} // End for each event.
       
   227 	list.previousView = uiManager.currentView;
       
   228 	list.show();
       
   229 }
       
   230 
       
   231 
       
   232 // Loads widget preferences.
       
   233 function loadPreferences() {
       
   234     if (window.widget) {
       
   235         // load settings from widget preferences store
       
   236         icalData = widget.preferenceForKey("icalData");
       
   237     }
       
   238 }
       
   239 
       
   240 // Loads widget preferences.
       
   241 function savePreferences() {
       
   242     if (window.widget) {
       
   243         // save settings in widget preferences store
       
   244         widget.setPreferenceForKey(icalData, "icalData");
       
   245     }
       
   246 }
       
   247 
       
   248 
       
   249 
       
   250 function setDefaultFontSizeForScreenSize(){
       
   251 	// first check if there is a preference present
       
   252     if (window.widget) {
       
   253 		var saved = widget.preferenceForKey("fontsize");
       
   254 		if ( widget.preferenceForKey("fontsize") ) {
       
   255 			setCssBodyFontSize(parseInt(saved));
       
   256 		}
       
   257 		else {
       
   258 			// no preference available, check screen size
       
   259 			if (window.screen.width > 400 || window.screen.height > 400) {
       
   260 				// hi res screen, use large font
       
   261 				setCssBodyFontSize(18);
       
   262 			}
       
   263 			else {
       
   264 				// lo res screen, use small font
       
   265 				setCssBodyFontSize(14);
       
   266 			}
       
   267 		}
       
   268 	}
       
   269 }
       
   270 
       
   271 function increaseFontSize(){
       
   272     if (window.widget) {
       
   273 		setCssBodyFontSize(currentFontSize + 2);
       
   274 	}
       
   275 }
       
   276 
       
   277 function decreaseFontSize(){
       
   278     if (window.widget) {
       
   279 		if (currentFontSize > 4) {
       
   280 			setCssBodyFontSize(currentFontSize - 2);
       
   281 		}
       
   282 	}
       
   283 }
       
   284 
       
   285 function setCssBodyFontSize(size) {
       
   286     if (window.widget) {
       
   287 		currentFontSize = size;
       
   288 		var sizestring = "" + size;
       
   289 		document.body.style.fontSize = sizestring + "px";
       
   290 		widget.setPreferenceForKey(sizestring, "fontsize");
       
   291 	}
       
   292 }
       
   293 
       
   294 function nocache(url) {
       
   295     if (url.indexOf("?") == -1) {
       
   296         url += "?";
       
   297     } else {
       
   298         url += "&";
       
   299     }
       
   300     url += "nocache=" + (new Date().getTime());
       
   301 	return url;
       
   302 }
       
   303 
       
   304 
       
   305 function sessionMatches(session, startDate) {
       
   306 	var m_date = session.getDate()==startDate.getDate();
       
   307 	var m_year = session.getFullYear()==startDate.getFullYear();
       
   308 	var m_month = session.getMonth()==startDate.getMonth();
       
   309 	var m_hour = session.getHours()==startDate.getHours();
       
   310 	var m_minute = session.getMinutes()==startDate.getMinutes();
       
   311 	return m_date && m_month && m_year && m_hour && m_minute; 
       
   312 }
       
   313 
       
   314 function dayToString(day) {
       
   315 	return day.toDateString(); 	
       
   316 }
       
   317 
       
   318 function dayMatches(day, startDate){
       
   319 	var m_date = day.getDate()==startDate.getDate();
       
   320 	var m_year = day.getFullYear()==startDate.getFullYear();
       
   321 	var m_month = day.getMonth()==startDate.getMonth();
       
   322 	return m_date && m_month && m_year; 
       
   323 }
       
   324 
       
   325 function sessionTimeToString(session) {
       
   326 	return ""+session.getHours()+":"+pad(session.getMinutes(),2); 	
       
   327 }
       
   328 
       
   329 function dateToString(day) {
       
   330 	var full = day.toDateString();
       
   331 	// remove year as it doesn't fit on small screens
       
   332 	return full.substring(0, full.length-4); 	
       
   333 }
       
   334 
       
   335 function pad(num, digits) {
       
   336 	var str = "" + num;
       
   337 	while ( str.length < digits ) {
       
   338 		str = "0" + str;
       
   339 	}
       
   340 	return str;
       
   341 }
       
   342 
       
   343 // Opens a URL in a separate browser window
       
   344 function openURL(url) {
       
   345     if (window.widget) {
       
   346         // in WRT
       
   347         widget.openURL(url);
       
   348     } else {
       
   349         // outside WRT
       
   350         window.open(url, "NewWindow");
       
   351     }
       
   352 }