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