author | ivanl |
Fri, 19 Mar 2010 11:03:38 +0000 | |
changeset 21 | f7a5a40d5bc4 |
parent 18 | b73e6caf0031 |
permissions | -rw-r--r-- |
0 | 1 |
// //////////////////////////////////////////////////////////////////////////// |
5 | 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. |
|
0 | 8 |
// //////////////////////////////////////////////////////////////////////////// |
9 |
||
10 |
||
11 |
/////////////////////////////////////////////////////////////////////////////// |
|
5 | 12 |
// A widget for accessing developer.symbian.org |
0 | 13 |
|
14 |
||
15 |
// Reference to the WRTKit user interface manager and main view. |
|
16 |
var uiManager; |
|
17 |
||
18 |
// Global settings / URLs and such |
|
19 |
||
20 |
// Symbian.org web site base URL |
|
21 |
//var symbianOrgBaseUrl = "http://staging.foundationhost.org"; |
|
22 |
//var symbianOrgBaseUrlSsl = "https://staging.foundationhost.org"; |
|
23 |
var symbianOrgBaseUrl = "http://developer.symbian.org"; |
|
24 |
var symbianOrgBaseUrlSsl = "https://developer.symbian.org"; |
|
25 |
var registrationURL = symbianOrgBaseUrl + "/main/user_profile/register.php"; |
|
26 |
var blogFeedName = "Symbian Blog"; |
|
21 | 27 |
var blogFeedUrl = "http://blog.symbian.org/feed/"; |
0 | 28 |
|
29 |
// FORUM vars and settings |
|
30 |
var symbianOrgNewThreadUrl = symbianOrgBaseUrl+"/forum/newthread.php?"; |
|
31 |
var symbianOrgNewReplyUrl = symbianOrgBaseUrl+"/forum/newreply.php?"; |
|
32 |
var symbianOrgLoginUrl = symbianOrgBaseUrlSsl+"/main/user_profile/login.php"; |
|
33 |
var symbianOrgLoginUsernameField = "username"; |
|
34 |
var symbianOrgLoginPasswordField = "password"; |
|
18 | 35 |
var forumUsername = null; |
0 | 36 |
var forumPassword = null; |
37 |
||
38 |
// Feed name, URL etc for forums |
|
39 |
var forumFeedName = "Symbian.org Forums"; |
|
2 | 40 |
var forumFeedURL = symbianOrgBaseUrl+ "/forum/external2.php?type=rss2"; //&fulldesc=1&lastpost=1 |
0 | 41 |
var forumsForumSpecQuery = "&forumid="; |
42 |
var forumFeedUpdateFrequency = -1; |
|
43 |
||
44 |
var forumGroupsUrl = symbianOrgBaseUrl+ "/rss/forum.php?type=groups"; |
|
45 |
var forumsListUrl = symbianOrgBaseUrl+ "/rss/forum.php?type=forums&group="; |
|
46 |
var forumThreadUrl = symbianOrgBaseUrl+ "/rss/forum.php?type=threadmsgs&threadid="; |
|
47 |
||
48 |
// Wiki url etc |
|
49 |
var wikiFeedName = "New on Symbian.org Wiki"; |
|
50 |
var wikiFeedUrl = symbianOrgBaseUrl+"/wiki/index.php?title=Special:NewPages&feed=rss"; |
|
51 |
var wikiBaseUrl = symbianOrgBaseUrl+"/wiki/index.php"; |
|
52 |
||
53 |
// Update variables |
|
21 | 54 |
var myversion = "1.0rc17"; |
0 | 55 |
var versionWikiPageUrl = wikiBaseUrl + "/Symbian.org_WRT_Widget"; |
56 |
var versionWikiPageString = "Current widget version is ["; |
|
57 |
var downloadUrl = symbianOrgBaseUrl + "/wiki/images/c/c5/Symbian.org.wgz"; |
|
58 |
||
59 |
// UI elements |
|
60 |
||
61 |
// blog / forum instances |
|
62 |
// all are SOScreen subclasses |
|
63 |
var home; // home screen |
|
64 |
var blog; // RSSReader showing blog |
|
65 |
var wiki; // RSSReader showing wiki |
|
66 |
var bugzila; // RSSReader showing bugzilla feeds |
|
67 |
var forumGroups; // RSSReader showing list of forum groups |
|
68 |
var settings; |
|
69 |
var about; |
|
70 |
||
71 |
// currently showing SOScreen |
|
72 |
var currentScreen = null; |
|
73 |
||
74 |
// Controls for the settings view |
|
75 |
var forumUsernameControl; |
|
76 |
var forumPasswordControl; |
|
77 |
||
78 |
// Constants for menu item identifiers. |
|
18 | 79 |
var MENU_ITEM_HOME = 0; |
80 |
var MENU_ITEM_SETTINGS = 1; |
|
81 |
var MENU_ITEM_REFRESH = 2; |
|
82 |
var MENU_ITEM_ABOUT = 3; |
|
83 |
var MENU_ITEM_CHECK_UPDATE = 4; |
|
84 |
var MENU_ITEM_LARGER_FONT = 5; |
|
85 |
var MENU_ITEM_SMALLER_FONT = 6; |
|
0 | 86 |
// Flag indicating weather the web site login has been initiated |
87 |
var loginInitiated = false; |
|
88 |
var loginInitiatedCallback = null; |
|
89 |
||
90 |
||
91 |
var widgetHomepage = symbianOrgBaseUrl + "/wiki/index.php?title=Symbian.org_WRT_Widget&action=render"; |
|
92 |
var aboutText = "<strong>Symbian.org "+myversion+"</strong><br>" |
|
93 |
+ "Symbian.org WRT Widget is a Web Runtime application which allows mobile " |
|
94 |
+ "access to Symbian Foundation Forums, Blog, Wiki and Bugzilla. <br>" |
|
95 |
+ "For more information and updates check <div class=FeedItemLink>" |
|
96 |
+ "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" |
|
97 |
+ widgetHomepage |
|
98 |
+ "'); return false;\">" |
|
99 |
+" Symbian.org Widget Homepage </a> </div><p>" |
|
100 |
+ "Credits: Ivan Litovski, Ryan Grentz, James Mentz"; |
|
101 |
||
102 |
||
2 | 103 |
var currentFontSize = 14; |
104 |
||
0 | 105 |
// Called from the onload event handler to initialize the widget. |
106 |
function init() { |
|
107 |
||
108 |
// set tab-navigation mode and show softkeys |
|
109 |
// (only if we are in the WRT environment) |
|
110 |
if (window.widget) { |
|
111 |
widget.setNavigationEnabled(false); |
|
112 |
window.menu.showSoftkeys(); |
|
113 |
// create menu |
|
18 | 114 |
var homeMenuItem = new MenuItem("Home", MENU_ITEM_HOME); |
115 |
homeMenuItem.onSelect = menuItemSelected; |
|
116 |
menu.append(homeMenuItem); |
|
0 | 117 |
var refreshMenuItem = new MenuItem("Refresh", MENU_ITEM_REFRESH); |
118 |
refreshMenuItem.onSelect = menuItemSelected; |
|
119 |
menu.append(refreshMenuItem); |
|
2 | 120 |
var settingsMenuItem = new MenuItem("Settings", MENU_ITEM_SETTINGS); |
121 |
settingsMenuItem.onSelect = menuItemSelected; |
|
122 |
menu.append(settingsMenuItem); |
|
123 |
var updateMenuItem = new MenuItem("Check for updates", MENU_ITEM_CHECK_UPDATE); |
|
124 |
updateMenuItem.onSelect = menuItemSelected; |
|
125 |
menu.append(updateMenuItem); |
|
18 | 126 |
var largerFontMenuItem = new MenuItem("Large font", MENU_ITEM_LARGER_FONT); |
2 | 127 |
largerFontMenuItem.onSelect = menuItemSelected; |
128 |
menu.append(largerFontMenuItem); |
|
18 | 129 |
var smallerFontMenuItem = new MenuItem("Small font", MENU_ITEM_SMALLER_FONT); |
2 | 130 |
smallerFontMenuItem.onSelect = menuItemSelected; |
131 |
menu.append(smallerFontMenuItem); |
|
0 | 132 |
var aboutMenuItem = new MenuItem("About", MENU_ITEM_ABOUT); |
133 |
aboutMenuItem.onSelect = menuItemSelected; |
|
134 |
menu.append(aboutMenuItem); |
|
135 |
} |
|
136 |
||
137 |
// load prefs |
|
138 |
if (!forumUsername || !forumPassword) { |
|
139 |
loadPreferences(); |
|
140 |
} |
|
141 |
||
142 |
// create UI manager |
|
143 |
uiManager = new UIManager(); |
|
144 |
||
145 |
// Create the home view |
|
146 |
home = new ListView(null, "<img src=logo.png>"); |
|
147 |
||
148 |
// add forums button |
|
149 |
var forumsButton = new NavigationButton(1, "right.gif", "Forums") |
|
150 |
forumsButton.addEventListener("ActionPerformed", function(){forumGroups.show();}); |
|
151 |
home.addControl(forumsButton); |
|
152 |
||
153 |
// add blogs button |
|
154 |
var blogsButton = new NavigationButton(2, "right.gif", "Blogs") |
|
155 |
blogsButton.addEventListener("ActionPerformed", function(){blog.show();}); |
|
156 |
home.addControl(blogsButton); |
|
157 |
||
158 |
// add wiki button |
|
159 |
var wikiButton = new NavigationButton(3, "right.gif", "Wiki") |
|
160 |
wikiButton.addEventListener("ActionPerformed", function(){wiki.show();}); |
|
161 |
home.addControl(wikiButton); |
|
162 |
||
163 |
// add bugzilla button |
|
164 |
var bugzillaButton = new NavigationButton(3, "right.gif", "Bugzilla") |
|
165 |
bugzillaButton.addEventListener("ActionPerformed", function(){login(function(){bugzilla.show();});}); |
|
16 | 166 |
// bugzillaButton.addEventListener("ActionPerformed", function(){bugzilla.show();}); |
0 | 167 |
home.addControl(bugzillaButton); |
168 |
||
169 |
// soft keys |
|
170 |
home.setupSoftKeys = function() { |
|
171 |
if (window.widget) { |
|
172 |
menu.setRightSoftkeyLabel("Exit", function(){window.close();}); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
// create blog screen |
|
177 |
blog = new RssReader(blogFeedName, blogFeedUrl, null, home, null); |
|
10
07ac2f6a36a9
1.0rc10 Fixes annoyances and buglets in reading forums and blog.
ivanl
parents:
6
diff
changeset
|
178 |
|
0 | 179 |
// create wiki screen |
18 | 180 |
wiki = new WikiHome(home);// new RssReader(wikiFeedName, wikiFeedUrl, new ButtonFeedPresenter(null), home, null); |
0 | 181 |
|
182 |
// wiki feed contains full article text for many articles |
|
183 |
// this takes up a _lot_ of memory. Also we don't |
|
184 |
// really want the full text at the stage when we want a list |
|
185 |
// of recent articles. |
|
186 |
wiki.ignoreContent = true; |
|
187 |
||
188 |
wiki.maxItems = 20; |
|
189 |
||
190 |
bugzilla = new BugzillaSearchPanel(home); |
|
191 |
||
192 |
// create the top level forums screen - list of forum groups |
|
193 |
// forumsGroup, forum and thread screens are dynamically generated by forumGroups |
|
194 |
forumGroups = new RssReader("Forums", forumGroupsUrl, new ForumGroupsFeedPresenter(null), home, forumGroupsResponseParser); |
|
195 |
||
196 |
// create settings view |
|
197 |
settings = new ListView(null, createCaption("Settings")); |
|
198 |
settings.previousView = home; |
|
199 |
||
200 |
var settingsIntroLabel = new Label(null, null, |
|
201 |
"In order to access all site features, you must login. " |
|
202 |
+ "If you have not registered yet, please click the 'Register' button below."); |
|
203 |
||
204 |
settings.addControl(settingsIntroLabel); |
|
205 |
// forum username control |
|
206 |
forumUsernameControl = new TextField('forumUsername', "Symbian.org username", forumUsername?forumUsername:""); |
|
207 |
settings.addControl(forumUsernameControl); |
|
208 |
||
209 |
// forum password control |
|
210 |
forumPasswordControl = new TextField('forumPassword', "Symbian.org password", forumPassword?forumPassword:"", true); |
|
211 |
settings.addControl(forumPasswordControl); |
|
212 |
||
213 |
// save settings button |
|
214 |
settingsSaveButton = new FormButton(null, "Save"); |
|
215 |
settingsSaveButton.addEventListener("ActionPerformed", saveSettingsClicked); |
|
216 |
settings.addControl(settingsSaveButton); |
|
217 |
||
218 |
||
219 |
// cancel settings button |
|
220 |
var settingsRegisterButton = new FormButton(null, "Register"); |
|
221 |
settingsRegisterButton.addEventListener("ActionPerformed", function(){openURL(registrationURL);}); |
|
222 |
settings.addControl(settingsRegisterButton); |
|
223 |
||
224 |
// cancel settings button |
|
225 |
settingsCancelButton = new FormButton(null, "Cancel"); |
|
226 |
settingsCancelButton.addEventListener("ActionPerformed", function(){settings.goBack();}); |
|
227 |
settings.addControl(settingsCancelButton); |
|
228 |
||
229 |
//Create about view |
|
230 |
about = new ListView(null, createCaption("Symbian.org")); |
|
231 |
about.previousView = home; |
|
232 |
// About label control |
|
233 |
aboutLabel = new ContentPanel(null, null, null, true); |
|
234 |
aboutLabel.setCaption("About this Widget"); |
|
235 |
aboutLabel.setContent(aboutText); |
|
236 |
aboutLabel.setExpanded(true); |
|
237 |
about.addControl(aboutLabel); |
|
238 |
||
239 |
home.show(); |
|
2 | 240 |
setDefaultFontSizeForScreenSize(); |
0 | 241 |
} |
242 |
||
243 |
// Callback for when menu items are selected. |
|
244 |
function menuItemSelected(id) { |
|
245 |
var currentView = uiManager.getView(); |
|
246 |
switch (id) { |
|
247 |
case MENU_ITEM_SETTINGS: |
|
248 |
if ( currentView == settings || currentView == about) { |
|
249 |
settings.previousView = home; |
|
250 |
} else { |
|
251 |
settings.previousView = currentView; |
|
252 |
} |
|
253 |
uiManager.hideNotification(); |
|
254 |
settings.show(); |
|
255 |
break; |
|
18 | 256 |
case MENU_ITEM_HOME: |
257 |
currentView = home; |
|
258 |
home.show(); |
|
259 |
break; |
|
0 | 260 |
case MENU_ITEM_REFRESH: |
261 |
currentView.update(true); |
|
262 |
break; |
|
2 | 263 |
case MENU_ITEM_LARGER_FONT: |
18 | 264 |
setLargeView(); |
265 |
// increaseFontSize(); |
|
2 | 266 |
break; |
267 |
case MENU_ITEM_SMALLER_FONT: |
|
18 | 268 |
setSmallView(); |
269 |
// decreaseFontSize(); |
|
2 | 270 |
break; |
0 | 271 |
case MENU_ITEM_CHECK_UPDATE: |
272 |
checkForUpdates(); |
|
273 |
break; |
|
274 |
case MENU_ITEM_ABOUT: |
|
275 |
if ( currentView == settings || currentView == about) { |
|
276 |
about.previousView = home; |
|
277 |
} else { |
|
278 |
about.previousView = currentView; |
|
279 |
} |
|
280 |
about.show(); |
|
281 |
break; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
// Loads widget preferences. |
|
286 |
function loadPreferences() { |
|
287 |
if (window.widget) { |
|
288 |
// load settings from widget preferences store |
|
289 |
forumUsername = widget.preferenceForKey("forumUsername"); |
|
290 |
forumPassword = widget.preferenceForKey("forumPassword"); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
// Loads widget preferences. |
|
295 |
function savePreferences() { |
|
296 |
if (window.widget) { |
|
297 |
// save settings in widget preferences store |
|
298 |
widget.setPreferenceForKey(forumUsername, "forumUsername"); |
|
299 |
widget.setPreferenceForKey(forumPassword, "forumPassword"); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
// Callback for settings view save button. |
|
304 |
function saveSettingsClicked() { |
|
305 |
forumUsername = forumUsernameControl.getText(); |
|
306 |
forumPassword = forumPasswordControl.getText(); |
|
307 |
||
308 |
// save preferences |
|
309 |
savePreferences(); |
|
310 |
||
311 |
settings.goBack(); |
|
312 |
||
313 |
if ( loginInitiated ) { |
|
314 |
login(loginInitiatedCallback); |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
// Opens a URL in a separate browser window |
|
319 |
function openURL(url) { |
|
320 |
if (window.widget) { |
|
321 |
// in WRT |
|
322 |
widget.openURL(url); |
|
323 |
} else { |
|
324 |
// outside WRT |
|
325 |
window.open(url, "NewWindow"); |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
var updatePageAjax = null; |
|
330 |
||
331 |
function checkForUpdates() { |
|
332 |
uiManager.showNotification(-1, "wait", "Checking for updates...", -1); |
|
333 |
updatePageAjax = new Ajax(); |
|
334 |
updatePageAjax.onreadystatechange = checkForUpdatesStage2; |
|
3 | 335 |
updatePageAjax.open('GET', nocache(versionWikiPageUrl), true); |
0 | 336 |
updatePageAjax.send(null); |
337 |
} |
|
338 |
||
339 |
function checkForUpdatesStage2() { |
|
340 |
if (updatePageAjax.readyState == 4) { |
|
341 |
// extract version number |
|
342 |
var content = updatePageAjax.responseText; |
|
343 |
var ind = content.indexOf(versionWikiPageString); |
|
344 |
if ( ind == -1 ) { |
|
345 |
uiManager.showNotification(3000, "warning", "Update failed, check manually."); |
|
346 |
return; |
|
347 |
} |
|
348 |
ind += versionWikiPageString.length; |
|
349 |
var ind2 = content.indexOf("]",ind); |
|
350 |
if ( ind2 == -1 || (ind2-ind) > 10 ) { |
|
351 |
uiManager.showNotification(3000, "warning", "Update failed, check manually."); |
|
352 |
return; |
|
353 |
} |
|
354 |
var version = content.substring(ind,ind2); |
|
355 |
// compare to this version |
|
356 |
if ( version != myversion ) { |
|
357 |
var answer = confirm("Install new version " + version + "?"); |
|
358 |
if (answer) { |
|
359 |
// ok, we have the update |
|
360 |
uiManager.hideNotification(); |
|
3 | 361 |
openURL(nocache(downloadUrl)); |
2 | 362 |
setTimeout(function () {window.close();}, 1000); |
0 | 363 |
} else { |
364 |
uiManager.showNotification(3000, "info", "Update cancelled."); |
|
365 |
} |
|
366 |
} else { |
|
367 |
uiManager.showNotification(3000, "info", "Up to date!"); |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
function createCaption(caption) { |
|
373 |
if (caption.length > 30) { |
|
374 |
caption = caption.substring(0, 30) + "..."; |
|
375 |
} |
|
2 | 376 |
return "<table border=0><tr><td style=\"{vertical-align:middle}\">" |
377 |
+ "<img src=titlebar.png style=\"{vertical-align:middle}\" >" |
|
378 |
+ "</td><td style=\"{vertical-align:middle}\"> " |
|
379 |
+ "<p class=ListViewCaptionText>" + caption +"</p></td></tr></table>"; |
|
0 | 380 |
} |
2 | 381 |
|
382 |
function setDefaultFontSizeForScreenSize(){ |
|
21 | 383 |
// no preference available, check screen size |
384 |
if (window.innerWidth > 400 || window.innerHeight > 400) { |
|
385 |
// hi res screen, use large font |
|
386 |
setLargeView(); |
|
387 |
} |
|
388 |
else { |
|
389 |
// lo res screen, use small font |
|
390 |
setSmallView(); |
|
2 | 391 |
} |
392 |
} |
|
393 |
||
18 | 394 |
function setLargeView(){ |
395 |
document.getElementById('stylesheet').href = 'WRTKit/Resources/UI-large.css'; |
|
396 |
// setCssBodyFontSize(22); |
|
397 |
} |
|
398 |
||
399 |
function setSmallView(){ |
|
400 |
document.getElementById('stylesheet').href = 'WRTKit/Resources/UI.css'; |
|
401 |
// setCssBodyFontSize(14); |
|
402 |
} |
|
403 |
||
2 | 404 |
function increaseFontSize(){ |
405 |
if (window.widget) { |
|
406 |
setCssBodyFontSize(currentFontSize + 2); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
function decreaseFontSize(){ |
|
411 |
if (window.widget) { |
|
412 |
if (currentFontSize > 4) { |
|
413 |
setCssBodyFontSize(currentFontSize - 2); |
|
414 |
} |
|
415 |
} |
|
416 |
} |
|
417 |
||
16 | 418 |
function setCssBodyFontSize(size){ |
2 | 419 |
if (window.widget) { |
420 |
currentFontSize = size; |
|
421 |
var sizestring = "" + size; |
|
422 |
document.body.style.fontSize = sizestring + "px"; |
|
423 |
widget.setPreferenceForKey(sizestring, "fontsize"); |
|
424 |
} |
|
425 |
} |
|
3 | 426 |
|
427 |
function nocache(url) { |
|
428 |
if (url.indexOf("?") == -1) { |
|
429 |
url += "?"; |
|
430 |
} else { |
|
431 |
url += "&"; |
|
432 |
} |
|
18 | 433 |
url += "xnocache=" + (new Date().getTime()); |
3 | 434 |
return url; |
435 |
} |