]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
toggle no daemon warning @ runtime on refetch
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var _qfd_deleted_feed = 0;
10 var firsttime_update = true;
11 var last_refetch = 0;
12 var cookie_lifetime = 0;
13 var active_feed_id = 0;
14
15 var xmlhttp = Ajax.getTransport();
16
17 var init_params = new Array();
18
19 function toggleTags() {
20         display_tags = !display_tags;
21
22         var p = document.getElementById("dispSwitchPrompt");
23
24         if (display_tags) {
25                 p.innerHTML = "display feeds";
26         } else {
27                 p.innerHTML = "display tags";
28         }
29         
30         updateFeedList();
31 }
32
33 function dlg_frefresh_callback() {
34         if (xmlhttp.readyState == 4) {
35                 notify(xmlhttp.responseText);
36                 updateFeedList(false, false);
37                 if (_qfd_deleted_feed) {
38                         var hframe = document.getElementById("headlines-frame");
39                         if (hframe) {
40                                 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
41                         }
42                 }
43                 closeInfoBox();
44         } 
45 }
46
47 function refetch_callback() {
48         if (xmlhttp.readyState == 4) {
49                 try {
50
51                         var date = new Date();
52
53                         last_refetch = date.getTime() / 1000;
54
55                         if (!xmlhttp.responseXML) {
56                                 notify("refetch_callback: backend did not return valid XML", true, true);
57                                 return;
58                         }
59                 
60                         var reply = xmlhttp.responseXML.firstChild;
61         
62                         if (!reply) {
63                                 notify("refetch_callback: backend did not return expected XML object", true, true);
64                                 updateTitle("");
65                                 return;
66                         } 
67         
68                         var error_code = reply.getAttribute("error-code");
69                 
70                         if (error_code && error_code != 0) {
71                                 return fatalError(error_code, reply.getAttribute("error-msg"));
72                         }
73
74                         var counters = reply.firstChild;
75         
76                         parse_counters(counters, true);
77
78                         var runtime_info = counters.nextSibling;
79
80                         parse_runtime_info(runtime_info);
81
82                         debug("refetch_callback: done");
83
84                         if (!daemon_enabled) {
85                                 notify("All feeds updated.");
86                                 updateTitle("");
87                         } else {
88                                 notify("");
89                         }
90                 } catch (e) {
91                         exception_error("refetch_callback", e);
92                         updateTitle("");
93                 }
94         }
95 }
96
97 function backend_sanity_check_callback() {
98
99         if (xmlhttp.readyState == 4) {
100
101                 try {
102                 
103                         if (!xmlhttp.responseXML) {
104                                 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
105                                 return;
106                         }
107         
108                         var reply = xmlhttp.responseXML.firstChild.firstChild;
109         
110                         if (!reply) {
111                                 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
112                                 return;
113                         }
114         
115                         var error_code = reply.getAttribute("error-code");
116                 
117                         if (error_code && error_code != 0) {
118                                 return fatalError(error_code, reply.getAttribute("error-msg"));
119                         }
120         
121                         debug("sanity check ok");
122
123                         var params = reply.nextSibling;
124
125                         if (params) {
126                                 debug('reading init-params...');
127                                 var param = params.firstChild;
128
129                                 while (param) {
130                                         var k = param.getAttribute("key");
131                                         var v = param.getAttribute("value");
132                                         debug(k + " => " + v);
133                                         init_params[k] = v;                                     
134                                         param = param.nextSibling;
135                                 }
136                         }
137
138                         init_second_stage();
139
140                 } catch (e) {
141                         exception_error("backend_sanity_check_callback", e);
142                 }
143         } 
144 }
145
146 function scheduleFeedUpdate(force) {
147
148         if (!daemon_enabled) {
149                 notify("Updating feeds, please wait.", true);
150                 updateTitle("Updating");
151         }
152
153         var query_str = "backend.php?op=rpc&subop=";
154
155         if (force) {
156                 query_str = query_str + "forceUpdateAllFeeds";
157         } else {
158                 query_str = query_str + "updateAllFeeds";
159         }
160
161         var omode;
162
163         if (firsttime_update && !navigator.userAgent.match("Opera")) {
164                 firsttime_update = false;
165                 omode = "T";
166         } else {
167                 if (display_tags) {
168                         omode = "t";
169                 } else {
170                         omode = "flc";
171                 }
172         }
173         
174         query_str = query_str + "&omode=" + omode;
175         query_str = query_str + "&uctr=" + global_unread;
176
177         debug("in scheduleFeedUpdate");
178
179         var date = new Date();
180
181         if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
182                 debug("<b>xmlhttp seems to be stuck, aborting</b>");
183                 xmlhttp.abort();
184         }
185
186         if (xmlhttp_ready(xmlhttp)) {
187                 xmlhttp.open("GET", query_str, true);
188                 xmlhttp.onreadystatechange=refetch_callback;
189                 xmlhttp.send(null);
190         } else {
191                 debug("xmlhttp busy");
192                 printLockingError();
193         }   
194 }
195
196 function updateFeedList(silent, fetch) {
197
198 //      if (silent != true) {
199 //              notify("Loading feed list...");
200 //      }
201
202         var query_str = "backend.php?op=feeds";
203
204         if (display_tags) {
205                 query_str = query_str + "&tags=1";
206         }
207
208         if (getActiveFeedId()) {
209                 query_str = query_str + "&actid=" + getActiveFeedId();
210         }
211
212         if (navigator.userAgent.match("Opera")) {
213                 var date = new Date();
214                 var timestamp = Math.round(date.getTime() / 1000);
215                 query_str = query_str + "&ts=" + timestamp
216         }
217         
218         if (fetch) query_str = query_str + "&fetch=yes";
219
220         var feeds_frame = document.getElementById("feeds-frame");
221
222         feeds_frame.src = query_str;
223 }
224
225 function catchupAllFeeds() {
226
227         var query_str = "backend.php?op=feeds&subop=catchupAll";
228
229         notify("Marking all feeds as read...");
230
231         var feeds_frame = document.getElementById("feeds-frame");
232
233         feeds_frame.src = query_str;
234
235         global_unread = 0;
236         updateTitle("");
237
238 }
239
240 function viewCurrentFeed(skip, subop) {
241
242         if (getActiveFeedId()) {
243                 viewfeed(getActiveFeedId(), skip, subop);
244         } else {
245                 disableContainerChildren("headlinesToolbar", false, document);
246                 viewfeed(-1, skip, subop); // FIXME
247         }
248         return false; // block unneeded form submits
249 }
250
251 function viewfeed(feed, skip, subop) {
252         var f = window.frames["feeds-frame"];
253         f.viewfeed(feed, skip, subop);
254 }
255
256 function timeout() {
257         scheduleFeedUpdate(false);
258
259         var refresh_time = getInitParam("feeds_frame_refresh");
260
261         if (!refresh_time) refresh_time = 600; 
262
263         setTimeout("timeout()", refresh_time*1000);
264 }
265
266 function resetSearch() {
267         var searchbox = document.getElementById("searchbox")
268
269         if (searchbox.value != "" && getActiveFeedId()) {       
270                 searchbox.value = "";
271                 viewfeed(getActiveFeedId(), 0, "");
272         }
273 }
274
275 function searchCancel() {
276         closeInfoBox(true);
277 }
278
279 function search() {
280         closeInfoBox(); 
281         viewCurrentFeed(0, "");
282 }
283
284 function localPiggieFunction(enable) {
285         if (enable) {
286                 var query_str = "backend.php?op=feeds&subop=piggie";
287
288                 if (xmlhttp_ready(xmlhttp)) {
289
290                         xmlhttp.open("GET", query_str, true);
291                         xmlhttp.onreadystatechange=feedlist_callback;
292                         xmlhttp.send(null);
293                 }
294         }
295 }
296
297 // if argument is undefined, current subtitle is not updated
298 // use blank string to clear subtitle
299 function updateTitle(s) {
300         var tmp = "Tiny Tiny RSS";
301
302         if (s != undefined) {
303                 current_subtitle = s;
304         }
305
306         if (global_unread > 0) {
307                 tmp = tmp + " (" + global_unread + ")";
308         }
309
310         if (current_subtitle) {
311                 tmp = tmp + " - " + current_subtitle;
312         }
313
314         if (active_title_text.length > 0) {
315                 tmp = tmp + " > " + active_title_text;
316         }
317
318         document.title = tmp;
319 }
320
321 function genericSanityCheck() {
322
323         if (!xmlhttp) fatalError(1);
324
325         setCookie("ttrss_vf_test", "TEST");
326         
327         if (getCookie("ttrss_vf_test") != "TEST") {
328                 fatalError(2);
329         }
330
331         return true;
332 }
333
334 function init() {
335
336         try {
337
338                 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
339
340                 if (arguments.callee.done) return;
341                 arguments.callee.done = true;           
342
343                 disableContainerChildren("headlinesToolbar", true);
344
345                 Form.disable("main_toolbar_form");
346
347                 if (!genericSanityCheck()) 
348                         return;
349
350                 if (getURLParam('debug')) {
351                         document.getElementById('debug_output').style.display = 'block';
352                         debug('debug mode activated');
353                 }
354
355                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
356                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
357                 xmlhttp.send(null);
358
359         } catch (e) {
360                 exception_error("init", e);
361         }
362 }
363
364 function resize_feeds_frame() {
365         var f = document.getElementById("feeds-frame");
366         var tf = document.getElementById("mainFooter");
367         var th = document.getElementById("mainHeader");
368                  
369         f.style.height = document.body.scrollHeight - tf.scrollHeight - 
370                 th.scrollHeight - 50 + "px";
371 }
372
373 function init_second_stage() {
374
375         try {
376
377                 cookie_lifetime = getCookie("ttrss_cltime");
378
379                 delCookie("ttrss_vf_test");
380         
381                 updateFeedList(false, false);
382                 document.onkeydown = hotkey_handler;
383
384                 var tb = parent.document.forms["main_toolbar_form"];
385
386                 dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
387                 dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
388
389                 daemon_enabled = getInitParam("daemon_enabled");
390
391                 // FIXME should be callled after window resize
392
393                 var h = document.getElementById("headlines");
394                 var c = document.getElementById("content");
395
396                 if (navigator.userAgent.match("Opera")) {
397                         resize_feeds_frame();
398                 }
399
400                 debug("second stage ok");
401         
402         } catch (e) {
403                 exception_error("init_second_stage", e);
404         }
405 }
406
407 function quickMenuChange() {
408         var chooser = document.getElementById("quickMenuChooser");
409         var opid = chooser[chooser.selectedIndex].value;
410
411         chooser.selectedIndex = 0;
412         quickMenuGo(opid);
413 }
414
415 function quickMenuGo(opid) {
416         try {
417
418                 if (opid == "qmcPrefs") {
419                         gotoPreferences();
420                 }
421         
422                 if (opid == "qmcSearch") {
423                         displayDlg("search", getActiveFeedId());
424                         return;
425                 }
426         
427                 if (opid == "qmcAddFeed") {
428                         displayDlg("quickAddFeed");
429                         return;
430                 }
431         
432                 if (opid == "qmcRemoveFeed") {
433                         var actid = getActiveFeedId();
434         
435                         if (!actid) {
436                                 alert("Please select some feed first.");
437                                 return;
438                         }
439         
440                         if (confirm("Unsubscribe from current feed?")) {
441                                 qfdDelete(actid);
442                         }
443                 
444                         return;
445                 }
446         
447                 if (opid == "qmcUpdateFeeds") {
448                         scheduleFeedUpdate(true);
449                         return;
450                 }
451         
452                 if (opid == "qmcCatchupAll") {
453                         catchupAllFeeds();
454                         return;
455                 }
456         
457                 if (opid == "qmcShowOnlyUnread") {
458                         toggleDispRead();
459                         return;
460                 }
461         
462                 if (opid == "qmcAddFilter") {
463                         displayDlg("quickAddFilter", getActiveFeedId());
464                 }
465         } catch (e) {
466                 exception_error("quickMenuGo", e);
467         }
468 }
469
470 function qfdDelete(feed_id) {
471
472         notify("Removing feed...");
473
474         if (!xmlhttp_ready(xmlhttp)) {
475                 printLockingError();
476                 return
477         }
478
479         _qfd_deleted_feed = feed_id;
480
481         xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
482         xmlhttp.onreadystatechange=dlg_frefresh_callback;
483         xmlhttp.send(null);
484 }
485
486
487 function updateFeedTitle(t) {
488         active_title_text = t;
489         updateTitle();
490 }
491
492 function toggleDispRead() {
493         try {
494
495                 if (!xmlhttp_ready(xmlhttp)) {
496                         printLockingError();
497                         return
498                 }
499
500                 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
501
502                 hide_read_feeds = !hide_read_feeds;
503
504                 debug("toggle_disp_read => " + hide_read_feeds);
505
506                 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
507
508                 var query = "backend.php?op=rpc&subop=setpref" +
509                         "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
510
511                 storeInitParam("hide_read_feeds", hide_read_feeds, true);
512
513                 new Ajax.Request(query);
514                 
515         } catch (e) {
516                 exception_error("toggleDispRead", e);
517         }
518 }
519
520 function parse_runtime_info(elem) {
521         var param = elem.firstChild;
522
523         while (param) {
524                 var k = param.getAttribute("key");
525                 var v = param.getAttribute("value");
526
527                 var w = document.getElementById("noDaemonWarning");
528                 
529                 if (w) {
530                         if (k == "daemon_is_running" && v != 1) {
531                                 w.style.display = "block";
532                         } else {
533                                 w.style.display = "none";
534                         }
535                 }
536                 param = param.nextSibling;
537         }
538 }