See09/schedule.js
changeset 19 f3521a11d878
equal deleted inserted replaced
18:b73e6caf0031 19:f3521a11d878
       
     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 // The Schedule class is used to load, keep and filter sessions   
       
    11 function Schedule() {
       
    12 	this.callBackFunction = null;
       
    13 	this.sessions = null;
       
    14 	this.topics = new Array( "Main Stage", "Application Development", "Open Source Business Models", "Device Creation and Contributing to Symbian", "Hands-on Lab 1", "Hands-on Lab 2", "Birds of a Feather" );
       
    15 	this.scheduleFileName = null;
       
    16 	this.http = null;
       
    17 	this.parseCounter = 0;
       
    18 	this.rawData = null;
       
    19 }
       
    20 
       
    21 // Kick off loading
       
    22 Schedule.prototype.Init = function(csvfile, cbfunc){
       
    23 	this.callBackFunction = cbfunc;
       
    24 	this.scheduleFileName = csvfile;
       
    25 	var self = this;
       
    26 	setTimeout( function() {self.Load();}, 100);
       
    27 }
       
    28 
       
    29 // 'public' functions 
       
    30 Schedule.prototype.GetTopics = function() {
       
    31 	return this.topics;
       
    32 }
       
    33 
       
    34 Schedule.prototype.GetSessions = function(topic, day) {
       
    35 	var cnt = this.sessions.length;
       
    36 	var ret = new Array();
       
    37 	for( var i = 0; i < cnt ; i++ ) {
       
    38 		var session = this.sessions[i];
       
    39 		if ( session.topic == topic && day == session.date ) {
       
    40 			ret.push(session);
       
    41 		}
       
    42 	}
       
    43 	return ret;
       
    44 }
       
    45 
       
    46 Schedule.prototype.GetCurrentSessions = function() {
       
    47 	var now = GetUtcTime(new Date());
       
    48 	if ( ! this.sessions || this.sessions == null ) {
       
    49 		return;
       
    50 	}
       
    51 	var cnt = this.sessions.length;
       
    52 	var ret = new Array();
       
    53 	for( var i = 0; i < cnt ; i++ ) {
       
    54 		var session = this.sessions[i];
       
    55 		var sessionStart = session.GetStartUtc();
       
    56 		var sessionEnd = session.GetEndUtc();
       
    57 		if( now > sessionStart && now < sessionEnd ) {
       
    58 			ret.push(session);
       
    59 		}
       
    60 	}
       
    61 	return ret;
       
    62 }
       
    63 
       
    64 
       
    65 // 'private' functions
       
    66 
       
    67 // Loading and parsing
       
    68 Schedule.prototype.Load = function(){
       
    69 	// Prepare asynchronous download
       
    70 	this.http = new Ajax();
       
    71 	var self = this;
       
    72 	this.http.onreadystatechange = function() { self.LoadComplete(); };
       
    73 	this.http.open("GET", this.scheduleFileName, true); // false means synchronous
       
    74 	this.http.send(null);
       
    75 }
       
    76 	
       
    77 Schedule.prototype.LoadComplete = function() {
       
    78 	// request complete?
       
    79     if (this.http.readyState == 4) {
       
    80 		try {
       
    81 			this.rawData = this.http.responseText;
       
    82 			this.http = null; 
       
    83 			// parse data
       
    84 			this.sessions = new Array();
       
    85 			this.Parse(); // Prepare and set the data for the parser.
       
    86 		} 
       
    87 		catch (x) {
       
    88 			uiManager.showNotification(5000, "warning", "Error processing feed");
       
    89 		}
       
    90 	}
       
    91 }
       
    92 
       
    93 Schedule.prototype.Parse = function() {
       
    94 	var session = new Session();
       
    95 	var fieldCounter = 0;
       
    96 	var fieldBuf = "";
       
    97 	var quoted = false;
       
    98 	for(; this.parseCounter < this.rawData.length; this.parseCounter++ ) {
       
    99 		var ch = this.rawData.charAt(this.parseCounter);
       
   100 		if ( !quoted && ( ch == ',' || ch == '\n') ) {
       
   101 			session.SetFieldByOrdinal(fieldCounter++, fieldBuf);
       
   102 			if (fieldCounter == session.GetNumberOfFields()) {
       
   103 				this.AddSession(session);
       
   104 				this.parseCounter++;
       
   105 				var self = this;
       
   106 				setTimeout(function(){self.Parse();}, 1);
       
   107 				return;
       
   108 			}
       
   109 			fieldBuf = "";
       
   110 		} else if ( ch == '"' ) {
       
   111 			if (quoted) {
       
   112 				if (this.parseCounter < this.rawData.length - 1 && this.rawData.charAt(this.parseCounter + 1) == '"') {
       
   113 					// escaped quote, ignore this and next
       
   114 					this.parseCounter++;
       
   115 				} else {
       
   116 					quoted = false;
       
   117 				}
       
   118 			} else {
       
   119 				quoted = true;
       
   120 			}
       
   121 		} else if ( ch == '\r' ) {
       
   122 			// ignore carriage return 
       
   123 		} else if ( ch == '\n' ) { // quoted = true
       
   124 			// replace newline with <br> 
       
   125 			fieldBuf += "<br>";
       
   126 		} else {
       
   127 			fieldBuf += ch;
       
   128 		}
       
   129 	}
       
   130 	this.Sort();
       
   131 	if (this.callBackFunction) {
       
   132 		this.callBackFunction.call();
       
   133 	}
       
   134 	uiManager.hideNotification();
       
   135 }
       
   136 
       
   137 Schedule.prototype.Sort = function (){
       
   138 	// we get the schedule all messy so here we sort things out
       
   139 	var cnt = this.sessions.length;
       
   140 	for( var i = 0 ; i < cnt ; i++ ) {
       
   141 		for( var j = i+1 ; j < cnt ; j++ ) {
       
   142 			var si = this.sessions[i];
       
   143 			var sj = this.sessions[j];
       
   144 			if ( si.startTime > sj.startTime ) {
       
   145 				// swap
       
   146 				this.sessions[i] = sj;
       
   147 				this.sessions[j] = si;
       
   148 			}
       
   149 		}	 
       
   150 	}	 
       
   151 }
       
   152 
       
   153 Schedule.prototype.AddSession = function (session) {
       
   154 	if ( ! session.topic || session.topic == null || session.topic.length == 0 ) {
       
   155 		return;
       
   156 	}
       
   157 	// add to list of sessions
       
   158 	this.sessions.push(session);
       
   159 }
       
   160 
       
   161 function trim(text) {
       
   162 	return text;
       
   163 }
       
   164 
       
   165 
       
   166 function GetUtcTime(d){
       
   167 	// convert to msec since Jan 1 1970
       
   168 	var localTime = d.getTime();
       
   169 	// obtain local UTC offset and convert to msec
       
   170 	var localOffset = d.getTimezoneOffset() * 60000;
       
   171 	// obtain UTC time in msec
       
   172 	return localTime + localOffset;
       
   173 }