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