root/trunk/uploadr/MacUploadr.app/Contents/Resources/chrome/content/uploadr/ui.js

Revision 493, 11.7 kB (checked in by jdecq, 11 months ago)

add timestamps in log message

Line 
1 /*
2  * Flickr Uploadr
3  *
4  * Copyright (c) 2007-2008 Yahoo! Inc.  All rights reserved.  This library is
5  * free software; you can redistribute it and/or modify it under the terms of
6  * the GNU General Public License (GPL), version 2 only.  This library is
7  * distributed WITHOUT ANY WARRANTY, whether express or implied. See the GNU
8  * GPL for more details (http://www.gnu.org/licenses/gpl.html)
9  */
10
11 // A bit of a catch-all, but better than it was before
12 var ui = {
13
14     cancel: false,
15    
16         // Called at app startup
17         init: function() {
18
19                 // Default the initial prompt to the free user case
20                 document.getElementById('photos_init_prompt').firstChild.nodeValue =
21                         locale.getString('photos.init.free');
22
23
24                 // The meta fields with no selection should refer to a photo
25                 document.getElementById('no_who').firstChild.nodeValue =
26                         locale.getString('meta.single.who.photo');
27
28                 // Sneaky reformatting of help text
29                 for each (var id in ['help_offline', 'help_drag']) {
30                         var node = document.getElementById(id);
31                         var parts = node.firstChild.nodeValue.split('^^');
32                         node.removeChild(node.firstChild);
33                         node.appendChild(document.createTextNode(parts[0]));
34                         var span = document.createElementNS(NS_HTML, 'span');
35                         span.style.color = '#ff0084';
36                         span.appendChild(document.createTextNode(parts[1]));
37                         node.appendChild(span);
38                         node.appendChild(document.createTextNode(parts[2]));
39                 }
40                 var node = document.getElementById('help_faq');
41                 var parts = node.firstChild.nodeValue.split('^^');
42                 node.removeChild(node.firstChild);
43                 node.appendChild(document.createTextNode(parts[0]));
44                 var span = document.createElementNS(NS_HTML, 'span');
45                 span.className = 'link';
46                 span.onclick = menus.help.faq;
47                 span.appendChild(document.createTextNode(parts[1]));
48                 node.appendChild(span);
49                 node.appendChild(document.createTextNode(parts[2]));
50
51         },
52
53         // Called after user info is changed, for example by API calls done
54         // at login time
55         users_updated: function() {
56                 ui.bandwidth_updated();
57
58                 // Notes in the empty photo pane
59                 var notes = document.getElementById('photos_init_notes');
60                 while (notes.hasChildNodes()) {
61                         notes.removeChild(notes.firstChild);
62                 }
63                 var li = document.createElementNS(NS_HTML, 'li');
64                 li.appendChild(document.createTextNode(locale.getFormattedString(
65                         'photos.init.note.photo_size',
66                         [Math.max(5, users.filesize >> 10)])));
67                 notes.appendChild(li);
68                 if (users.is_pro) {
69
70                         // Additional notes for pro users regarding videos
71                         li = document.createElementNS(NS_HTML, 'li');
72                         li.appendChild(document.createTextNode(locale.getFormattedString(
73                                 'photos.init.note.video_size', [(null == users.videosize
74                                 ? conf.videosize : users.videosize) >> 10])));
75                         notes.appendChild(li);
76                         li = document.createElementNS(NS_HTML, 'li');
77                         li.appendChild(document.createTextNode(locale.getString(
78                                 'photos.init.note.video_length')));
79                         notes.appendChild(li);
80
81                         // Replace the top prompt for pro users to mention videos
82                         document.getElementById('photos_init_prompt')
83                                 .firstChild.nodeValue = locale.getString('photos.init.pro');
84
85                 } else {
86
87                         // Replace the top prompt for free users to mention only photos
88                         document.getElementById('photos_init_prompt')
89                                 .firstChild.nodeValue = locale.getString('photos.init.free');
90                        
91                 }
92
93                 // No extra notes for offline users since we have no idea what
94                 // to show them
95                 if (users.nsid) {
96                         document.getElementById('photos_init_note').style.display =
97                                 '-moz-box';
98                 }
99
100         },
101
102         // Update the counters showing remaining bandwidth and batch size
103         bandwidth_updated: function() {
104
105                 // Counter for remaining bandwidth
106                 if (users.bandwidth && !users.is_pro) {
107                         var remaining = document.getElementById('bw_remaining_mb');
108                         remaining.firstChild.nodeValue =
109                                 locale.getFormattedString('mb', [Math.max(0,
110                                 Math.round(users.bandwidth.remaining / 102.4) / 10)]);
111                         if (0 >= users.bandwidth.remaining) {
112                                 remaining.className = 'exhausted';
113                         } else if (6 << 10 > users.bandwidth.remaining) {
114                                 remaining.className = 'almost';
115                         } else {
116                                 remaining.className = '';
117                         }
118                         document.getElementById('bw_remaining')
119                                 .style.display = '-moz-box';
120                 }
121
122                 // Counter for current batch size
123                 var batch = document.getElementById('bw_batch_mb');
124                 batch.firstChild.nodeValue =
125                         locale.getFormattedString('mb', [Math.round(
126                         photos.batch_size / 102.4) / 10]);
127                 if (users.bandwidth) {
128                         if (photos.batch_size > users.bandwidth.remaining) {
129                                 batch.className = 'exhausted';
130                         } else if (photos.batch_size + (6 << 10)
131                                 > users.bandwidth.remaining) {
132                                 batch.className = 'almost';
133                         } else {
134                                 batch.className = '';
135                         }
136                 } else {
137                         batch.className = '';
138                 }
139
140         }
141
142 };
143
144 // Full-screen pages in the UI
145 var pages = {
146
147         _list: ['photos', 'auth', 'help'],
148         _current: 0,
149
150         // Go to a specified page
151         go: function(id) {
152
153                 // Load the page
154                 var ii = pages._list.length;
155                 for (var i = 0; i < ii; ++i) {
156                         var display = '';
157                         if (pages._list[i] == id) {
158                                 display = '-moz-box';
159                                 pages._last = pages._current;
160                                 pages._current = i;
161                         } else {
162                                 display = 'none';
163                         }
164                         document.getElementById('page_' + pages._list[i])
165                                 .style.display = display;
166                 }
167
168                 // Only the photos page has the toolbar
169                 if ('photos' == id) {
170                         document.getElementById('tools').style.display = '-moz-box';
171                         document.getElementById('bw_batch').style.display = '-moz-box';
172                 } else {
173                         document.getElementById('tools').style.display = 'none';
174                         document.getElementById('bw_batch').style.display = 'none';
175                 }
176
177         },
178
179         // Return the current page name
180         current: function() {
181                 return pages._list[pages._current];
182         }
183
184 };
185
186 // The menus
187 var menus = {
188
189         tools: {
190
191                 addons: function() {
192                         var wm = Cc['@mozilla.org/appshell/window-mediator;1']
193                                 .getService(Ci.nsIWindowMediator);
194                         var em = wm.getMostRecentWindow('Extension:Manager');
195                         if (em) {
196                                 em.focus();
197                                 return;
198                         }
199                         window.openDialog(
200                                 'chrome://mozapps/content/extensions/extensions.xul',
201                                 '',
202                                 'chrome,menubar,extra-chrome,toolbar,dialog=no,resizable'
203                         );
204                 },
205
206                 console: function() {
207                         window.open('chrome://global/content/console.xul', '_blank',
208                         'chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar');
209                 },
210                
211                 venkman: function() {
212                     start_venkman();
213                 }
214
215         },
216
217         help: {
218
219                 about: function() {
220                         window.openDialog('chrome://uploadr/content/about.xul',
221                                 'about-dialog', 'chrome,modal,centerscreen',
222                                 locale.getFormattedString('dialog.about.version',
223                                 [conf.version]));
224                 },
225
226                 tips: function() {
227                         mouse.show_photos();
228                         pages.go('help');
229                 },
230
231                 faq: function() {
232                         launch_browser('http://flickr.com/help/tools/');
233                 }
234
235         }
236
237 };
238
239 // Progress bars
240 var ProgressBar = function(id, width) {
241         this.id = id;
242         this.width = null == width ? document.getElementById(id).parentNode
243                 .boxObject.width : width;
244 };
245 ProgressBar.prototype = {
246         update: function(percent) {
247                 var bar = document.getElementById(this.id);
248                 if (null != bar) {
249                         bar.width = Math.round(this.width * Math.max(0,
250                                 Math.min(1, percent)));
251                 }
252         },
253         clear: function() {
254                 this.update(0);
255         },
256
257         // Do something special when the bar is finished
258         done: function(success) {
259                 this.update(1);
260                 document.getElementById(this.id).className = 'done';
261         },
262
263         // Generate DOM nodes for this progress bar
264         create: function() {
265                 var inner = document.createElement('box');
266                 inner.id = this.id;
267                 var outer = document.createElement('box');
268                 outer.className = 'progress_bar';
269                 outer.style.width = this.width + 'px';
270                 outer.setAttribute('flex', 1);
271                 outer.appendChild(inner);
272                 return outer;
273         }
274
275 };
276
277 // Change the status bar text
278 var status = {
279
280         // Set a message in the status bar
281         set: function(str) {
282                 document.getElementById('status').label = str;
283         },
284
285         // Clear the status bar
286         clear: function() {
287                 var status = document.getElementById('status');
288                 status.label = '';
289         }
290
291 };
292
293 var logStringMessage = function(msg) {
294     Cc['@mozilla.org/consoleservice;1']
295                                 .getService(Ci.nsIConsoleService)
296                                 .logStringMessage(new Date().toString() + ': ' + msg);
297 };
298
299
300 // Override the alert, confirm and prompt functions to take a title and
301 // text for OK/Cancel buttons
302 var alert = function(msg, title, ok) {
303         window.openDialog('chrome://uploadr/content/alert.xul', 'dialog_alert',
304                 'chrome,modal', msg, title, ok);
305 };
306 var confirm = function(msg, title, ok, cancel) {
307         var result = {result: false};
308         window.openDialog('chrome://uploadr/content/confirm.xul',
309                 'dialog_confirm', 'chrome,modal', msg, title, ok, cancel, result);
310         return result.result;
311 };
312 var prompt = function(msg, title, ok, cancel) {
313         var result = {result: false};
314         window.openDialog('chrome://uploadr/content/prompt.xul', 'dialog_prompt',
315                 'chrome,modal', msg, title, ok, cancel, result);
316         return result.result;
317 };
318
319 // Open a browser window to the given URL
320 var launch_browser = function(url) {
321         try {
322                 var io = Cc['@mozilla.org/network/io-service;1']
323                         .getService(Ci.nsIIOService);
324                 var uri = io.newURI(url, null, null);
325                 var eps = Cc['@mozilla.org/uriloader/external-protocol-service;1']
326                         .getService(Ci.nsIExternalProtocolService);
327                 var launcher = eps.getProtocolHandlerInfo('http');
328                 launcher.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
329                 launcher.launchWithURI(uri, null);
330         } catch (err) {}
331         return url;
332 };
333
334 // Now hack locale.getFormattedString to work like it should
335 //   Apparently the docs were just wrong and it *must* be a capital S
336 //   So, this function will go away eventually
337 var locale = document.getElementById('locale');
338 locale.getFormattedString = function(id, args) {
339         var str = locale.getString(id);
340         var ii = args.length;
341         for (var i = 0; i < ii; ++i) {
342                 var regex = new RegExp('%' + (i + 1) + '\\$[dDsS]');
343                 str = str.replace(regex, args[i]);
344         }
345         return str;
346 };
347
348 // Stacks to block removing photos, sorting photos, normalizing the
349 // photos list and exiting Uploadr
350 var _block_remove = 0;
351 var block_remove = function() {
352         if (0 == _block_remove) {
353                 buttons.remove.disable();
354         }
355         ++_block_remove;
356 };
357 var unblock_remove = function() {
358         --_block_remove;
359         if (0 == _block_remove && photos.selected.length) {
360                 buttons.remove.enable();
361         }
362 };
363 var _block_sort = 0;
364 var block_sort = function() {
365         ++_block_sort;
366 };
367 var unblock_sort = function() {
368         --_block_sort;
369 };
370 var _block_normalize = 0;
371 var block_normalize = function() {
372         ++_block_normalize;
373 };
374 var unblock_normalize = function() {
375         --_block_normalize;
376 };
377 var _block_exit = 0;
378 var block_exit = function() {
379         ++_block_exit;
380 };
381 var unblock_exit = function() {
382         --_block_exit;
383 };
384
385 // Exit Uploadr, asking for confirmation if necessary
386 var exit = function(force) {
387         if (null == force) {
388                 force = false;
389         }
390
391         // Don't exit if exit is blocked
392         if (!force && 0 < _block_exit && !confirm(
393                 locale.getString('dialog.exit.text'),
394                 locale.getString('dialog.exit.title'),
395                 locale.getString('dialog.exit.ok'),
396                 locale.getString('dialog.exit.cancel'))) {
397                 return false;
398         }
399
400         // Save state
401         photos.save();
402         settings.save();
403         users.save();
404
405         // Remove the images and TEMP directories if there are no photos left
406         if (0 == photos.count) {
407                 try {
408                         var profile = Cc['@mozilla.org/file/directory_service;1']
409                                 .getService(Ci.nsIProperties).get('ProfD', Ci.nsIFile);
410                         profile.append('images');
411                         profile.remove(true);
412                 } catch (err) {}
413                 try {
414                         var temp = Cc['@mozilla.org/file/local;1']
415                                 .createInstance(Ci.nsILocalFile);
416                         temp.initWithPath('C:\\temp');
417                         if (temp.isDirectory()) {
418                                 var files = temp.directoryEntries;
419                                 while (files.hasMoreElements()) {
420                                         var f = files.getNext().QueryInterface(Ci.nsILocalFile);
421                                         if (f.leafName.match(/^original/)) {
422                                                 try {
423                                                         f.remove(false);
424                                                 } catch (err2) {}
425                                         }
426                                 }
427                         }
428                 } catch (err) {}
429         }
430
431         // Shutdown threads
432         photos.thumb_cancel = true;
433         ui.cancel = true;
434         upload.cancel = true;
435         threads.worker.shutdown();
436         threads.uploadr.shutdown();
437
438         // Finally exit
439         var e = Cc['@mozilla.org/toolkit/app-startup;1']
440                 .getService(Ci.nsIAppStartup);
441         e.quit(Ci.nsIAppStartup.eForceQuit);
442
443 };
Note: See TracBrowser for help on using the browser.