]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
check update daemon timestamp
[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 daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17 var sanity_check_done = false;
18
19 var xmlhttp = Ajax.getTransport();
20 var xmlhttp_ctr = Ajax.getTransport();
21
22 var init_params = new Object();
23
24 function tagsAreDisplayed() {
25 return display_tags;
26 }
27
28 function toggleTags(show_all) {
29
30 var p = document.getElementById("dispSwitchPrompt");
31
32 if (!show_all && !display_tags) {
33 displayDlg("printTagCloud");
34 } else if (show_all) {
35 closeInfoBox();
36 display_tags = true;
37 p.innerHTML = __("display feeds");
38 notify_progress("Loading, please wait...", true);
39 updateFeedList();
40 } else if (display_tags) {
41 display_tags = false;
42 p.innerHTML = __("tag cloud");
43 notify_progress("Loading, please wait...", true);
44 updateFeedList();
45 }
46 }
47
48 function dlg_frefresh_callback() {
49 if (xmlhttp.readyState == 4) {
50 // notify(xmlhttp.responseText);
51
52 if (getActiveFeedId() == _qfd_deleted_feed) {
53 var h = document.getElementById("headlines-frame");
54 if (h) {
55 h.innerHTML = "<div class='whiteBox'>" + __('No feed selected.') + "</div>";
56 }
57 }
58
59 setTimeout('updateFeedList(false, false)', 50);
60 closeInfoBox();
61 }
62 }
63
64 function refetch_callback() {
65 if (xmlhttp_ctr.readyState == 4) {
66 try {
67
68 var date = new Date();
69
70 last_refetch = date.getTime() / 1000;
71
72 parse_counters_reply(xmlhttp_ctr, true);
73
74 debug("refetch_callback: done");
75
76 if (!daemon_enabled && !daemon_refresh_only) {
77 notify_info("All feeds updated.");
78 updateTitle("");
79 } else {
80 //notify("");
81 }
82 } catch (e) {
83 exception_error("refetch_callback", e);
84 updateTitle("");
85 }
86 }
87 }
88
89 function backend_sanity_check_callback() {
90
91 if (xmlhttp.readyState == 4) {
92
93 try {
94
95 if (sanity_check_done) {
96 fatalError(11, "Sanity check request received twice. This can indicate "+
97 "presence of Firebug or some other disrupting extension. "+
98 "Please disable it and try again.");
99 return;
100 }
101
102 if (!xmlhttp.responseXML) {
103 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
104 return;
105 }
106
107 var reply = xmlhttp.responseXML.firstChild.firstChild;
108
109 if (!reply) {
110 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
111 return;
112 }
113
114 var error_code = reply.getAttribute("error-code");
115
116 if (error_code && error_code != 0) {
117 return fatalError(error_code, reply.getAttribute("error-msg"));
118 }
119
120 debug("sanity check ok");
121
122 var params = reply.nextSibling;
123
124 if (params) {
125 debug('reading init-params...');
126 var param = params.firstChild;
127
128 while (param) {
129 var k = param.getAttribute("key");
130 var v = param.getAttribute("value");
131 debug(k + " => " + v);
132 init_params[k] = v;
133 param = param.nextSibling;
134 }
135 }
136
137 sanity_check_done = true;
138
139 init_second_stage();
140
141 } catch (e) {
142 exception_error("backend_sanity_check_callback", e);
143 }
144 }
145 }
146
147 function scheduleFeedUpdate(force) {
148
149 if (!daemon_enabled && !daemon_refresh_only) {
150 notify_progress("Updating feeds, please wait.", true);
151 updateTitle("Updating");
152 }
153
154 var query_str = "backend.php?op=rpc&subop=";
155
156 if (force) {
157 query_str = query_str + "forceUpdateAllFeeds";
158 } else {
159 query_str = query_str + "updateAllFeeds";
160 }
161
162 var omode;
163
164 if (firsttime_update && !navigator.userAgent.match("Opera")) {
165 firsttime_update = false;
166 omode = "T";
167 } else {
168 if (display_tags) {
169 omode = "tl";
170 } else {
171 omode = "flc";
172 }
173 }
174
175 query_str = query_str + "&omode=" + omode;
176 query_str = query_str + "&uctr=" + global_unread;
177
178 debug("in scheduleFeedUpdate");
179
180 var date = new Date();
181
182 var timestamp = Math.round(date.getTime() / 1000);
183 query_str = query_str + "&ts=" + timestamp
184
185 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
186 debug("<b>xmlhttp seems to be stuck, aborting</b>");
187 xmlhttp_ctr.abort();
188 if (is_safari()) {
189 xmlhttp_ctr = Ajax.getTransport();
190 }
191 }
192
193 debug("REFETCH query: " + query_str);
194
195 if (xmlhttp_ready(xmlhttp_ctr)) {
196 xmlhttp_ctr.open("GET", query_str, true);
197 xmlhttp_ctr.onreadystatechange=refetch_callback;
198 xmlhttp_ctr.send(null);
199 } else {
200 debug("xmlhttp_ctr busy");
201 //printLockingError();
202 }
203 }
204
205 function updateFeedList(silent, fetch) {
206
207 // if (silent != true) {
208 // notify("Loading feed list...");
209 // }
210
211 debug("<b>updateFeedList</b>");
212
213 var query_str = "backend.php?op=feeds";
214
215 if (display_tags) {
216 query_str = query_str + "&tags=1";
217 }
218
219 if (getActiveFeedId() && !activeFeedIsCat()) {
220 query_str = query_str + "&actid=" + getActiveFeedId();
221 }
222
223 var date = new Date();
224 var timestamp = Math.round(date.getTime() / 1000);
225 query_str = query_str + "&ts=" + timestamp
226
227 if (fetch) query_str = query_str + "&fetch=yes";
228
229 // var feeds_frame = document.getElementById("feeds-frame");
230 // feeds_frame.src = query_str;
231
232 debug("updateFeedList Q=" + query_str);
233
234 if (xmlhttp_ready(xmlhttp)) {
235 xmlhttp.open("GET", query_str, true);
236 xmlhttp.onreadystatechange=feedlist_callback;
237 xmlhttp.send(null);
238 } else {
239 debug("xmlhttp busy");
240 //printLockingError();
241 }
242
243 }
244
245 function catchupAllFeeds() {
246
247 var query_str = "backend.php?op=feeds&subop=catchupAll";
248
249 notify_progress("Marking all feeds as read...");
250
251 debug("catchupAllFeeds Q=" + query_str);
252
253 if (xmlhttp_ready(xmlhttp)) {
254 xmlhttp.open("GET", query_str, true);
255 xmlhttp.onreadystatechange=feedlist_callback;
256 xmlhttp.send(null);
257 } else {
258 debug("xmlhttp busy");
259 //printLockingError();
260 }
261
262 global_unread = 0;
263 updateTitle("");
264
265 }
266
267 function viewCurrentFeed(subop) {
268
269 // if (getActiveFeedId()) {
270 if (getActiveFeedId() != undefined) {
271 viewfeed(getActiveFeedId(), subop);
272 } else {
273 disableContainerChildren("headlinesToolbar", false, document);
274 // viewfeed(-1, subop); // FIXME
275 }
276 return false; // block unneeded form submits
277 }
278
279 function viewfeed(feed, subop) {
280 var f = window.frames["feeds-frame"];
281 f.viewfeed(feed, subop);
282 }
283
284 function timeout() {
285 scheduleFeedUpdate(false);
286
287 var refresh_time = getInitParam("feeds_frame_refresh");
288
289 if (!refresh_time) refresh_time = 600;
290
291 setTimeout("timeout()", refresh_time*1000);
292 }
293
294 function resetSearch() {
295 var searchbox = document.getElementById("searchbox")
296
297 if (searchbox.value != "" && getActiveFeedId()) {
298 searchbox.value = "";
299 viewfeed(getActiveFeedId(), "");
300 }
301 }
302
303 function searchCancel() {
304 closeInfoBox(true);
305 }
306
307 function search() {
308 closeInfoBox();
309 viewCurrentFeed(0, "");
310 }
311
312 function localPiggieFunction(enable) {
313 if (enable) {
314 var query_str = "backend.php?op=feeds&subop=piggie";
315
316 if (xmlhttp_ready(xmlhttp)) {
317
318 xmlhttp.open("GET", query_str, true);
319 xmlhttp.onreadystatechange=feedlist_callback;
320 xmlhttp.send(null);
321 }
322 }
323 }
324
325 // if argument is undefined, current subtitle is not updated
326 // use blank string to clear subtitle
327 function updateTitle(s) {
328 var tmp = "Tiny Tiny RSS";
329
330 if (s != undefined) {
331 current_subtitle = s;
332 }
333
334 if (global_unread > 0) {
335 tmp = tmp + " (" + global_unread + ")";
336 }
337
338 if (current_subtitle) {
339 tmp = tmp + " - " + current_subtitle;
340 }
341
342 if (active_title_text.length > 0) {
343 tmp = tmp + " > " + active_title_text;
344 }
345
346 document.title = tmp;
347 }
348
349 function genericSanityCheck() {
350
351 if (!xmlhttp) fatalError(1);
352
353 setCookie("ttrss_vf_test", "TEST");
354
355 if (getCookie("ttrss_vf_test") != "TEST") {
356 fatalError(2);
357 }
358
359 return true;
360 }
361
362 function init() {
363
364 try {
365
366 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
367
368 if (arguments.callee.done) return;
369 arguments.callee.done = true;
370
371 disableContainerChildren("headlinesToolbar", true);
372
373 Form.disable("main_toolbar_form");
374
375 if (!genericSanityCheck())
376 return;
377
378 if (getURLParam('debug')) {
379 document.getElementById('debug_output').style.display = 'block';
380 debug('debug mode activated');
381 }
382
383 var params = "&ua=" + param_escape(navigator.userAgent);
384
385 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
386 xmlhttp.onreadystatechange=backend_sanity_check_callback;
387 xmlhttp.send(null);
388
389 } catch (e) {
390 exception_error("init", e);
391 }
392 }
393
394 function resize_headlines() {
395
396 var h_frame = document.getElementById("headlines-frame");
397 var c_frame = document.getElementById("content-frame");
398 var f_frame = document.getElementById("footer");
399
400 if (!c_frame || !h_frame) return;
401
402 debug("resize_headlines");
403
404 if (!is_msie()) {
405 h_frame.style.height = 30 + "%";
406 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
407 h_frame.style.height = h_frame.offsetHeight + "px";
408 } else {
409 h_frame.style.height = document.documentElement.clientHeight * 0.3 + "px";
410 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
411
412 var c_bottom = document.documentElement.clientHeight;
413
414 if (f_frame) {
415 c_bottom = f_frame.offsetTop;
416 }
417
418 c_frame.style.height = c_bottom - (h_frame.offsetTop +
419 h_frame.offsetHeight + 1) + "px";
420 h_frame.style.height = h_frame.offsetHeight + "px";
421
422 }
423 }
424
425 function init_second_stage() {
426
427 try {
428
429 cookie_lifetime = getCookie("ttrss_cltime");
430
431 delCookie("ttrss_vf_test");
432
433 // document.onresize = resize_headlines;
434 resize_headlines();
435
436 var toolbar = document.forms["main_toolbar_form"];
437
438 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
439 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
440
441 daemon_enabled = getInitParam("daemon_enabled") == 1;
442 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
443
444 setTimeout('updateFeedList(false, false)', 50);
445
446 debug("second stage ok");
447
448 } catch (e) {
449 exception_error("init_second_stage", e);
450 }
451 }
452
453 function quickMenuChange() {
454 var chooser = document.getElementById("quickMenuChooser");
455 var opid = chooser[chooser.selectedIndex].value;
456
457 chooser.selectedIndex = 0;
458 quickMenuGo(opid);
459 }
460
461 function quickMenuGo(opid) {
462 try {
463
464 if (opid == "qmcPrefs") {
465 gotoPreferences();
466 }
467
468 if (opid == "qmcSearch") {
469 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
470 return;
471 }
472
473 if (opid == "qmcAddFeed") {
474 displayDlg("quickAddFeed");
475 return;
476 }
477
478 if (opid == "qmcEditFeed") {
479 editFeedDlg(getActiveFeedId());
480 }
481
482 if (opid == "qmcRemoveFeed") {
483 var actid = getActiveFeedId();
484
485 if (activeFeedIsCat()) {
486 alert("You can't unsubscribe from the category.");
487 return;
488 }
489
490 if (!actid) {
491 alert("Please select some feed first.");
492 return;
493 }
494
495 var fn = getFeedName(actid);
496
497 if (confirm("Unsubscribe from " + fn + "?")) {
498 qfdDelete(actid);
499 }
500
501 return;
502 }
503
504 if (opid == "qmcUpdateFeeds") {
505 scheduleFeedUpdate(true);
506 return;
507 }
508
509 if (opid == "qmcCatchupAll") {
510 catchupAllFeeds();
511 return;
512 }
513
514 if (opid == "qmcShowOnlyUnread") {
515 toggleDispRead();
516 return;
517 }
518
519 if (opid == "qmcAddFilter") {
520 displayDlg("quickAddFilter", getActiveFeedId());
521 }
522
523 } catch (e) {
524 exception_error("quickMenuGo", e);
525 }
526 }
527
528 function qfdDelete(feed_id) {
529
530 notify_progress("Removing feed...");
531
532 if (!xmlhttp_ready(xmlhttp)) {
533 printLockingError();
534 return
535 }
536
537 _qfd_deleted_feed = feed_id;
538
539 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
540 xmlhttp.onreadystatechange=dlg_frefresh_callback;
541 xmlhttp.send(null);
542
543 return false;
544 }
545
546
547 function updateFeedTitle(t) {
548 active_title_text = t;
549 updateTitle();
550 }
551
552 function toggleDispRead() {
553 try {
554
555 if (!xmlhttp_ready(xmlhttp)) {
556 printLockingError();
557 return
558 }
559
560 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
561
562 hide_read_feeds = !hide_read_feeds;
563
564 debug("toggle_disp_read => " + hide_read_feeds);
565
566 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
567
568 storeInitParam("hide_read_feeds", hide_read_feeds, true);
569
570 /* var query = "backend.php?op=rpc&subop=setpref" +
571 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
572
573 new Ajax.Request(query); */
574
575 } catch (e) {
576 exception_error("toggleDispRead", e);
577 }
578 }
579
580 function parse_runtime_info(elem) {
581 if (!elem) {
582 debug("parse_runtime_info: elem is null, aborting");
583 return;
584 }
585
586 var param = elem.firstChild;
587
588 debug("parse_runtime_info: " + param);
589
590 while (param) {
591 var k = param.getAttribute("key");
592 var v = param.getAttribute("value");
593
594 debug("RI: " + k + " => " + v);
595
596 if (k == "new_version_available") {
597 var icon = document.getElementById("newVersionIcon");
598 if (icon) {
599 if (v == "1") {
600 icon.style.display = "inline";
601 } else {
602 icon.style.display = "none";
603 }
604 }
605 }
606
607 var error_flag;
608
609 if (k == "daemon_is_running" && v != 1) {
610 notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
611 error_flag = true;
612 }
613
614 if (k == "daemon_stamp_ok" && v != 1) {
615 notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
616 error_flag = true;
617 }
618
619 if (!error_flag) {
620 notify('');
621 }
622
623 /* var w = document.getElementById("noDaemonWarning");
624
625 if (w) {
626 if (k == "daemon_is_running" && v != 1) {
627 w.style.display = "block";
628 } else {
629 w.style.display = "none";
630 }
631 } */
632 param = param.nextSibling;
633 }
634 }
635
636 function catchupCurrentFeed() {
637
638 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
639
640 var str = "Mark all articles in " + fn + " as read?";
641
642 /* if (active_feed_is_cat) {
643 str = "Mark all articles in this category as read?";
644 } */
645
646 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
647 return viewCurrentFeed('MarkAllRead')
648 }
649 }
650
651 function userSwitch() {
652 var chooser = document.getElementById("userSwitch");
653 var user = chooser[chooser.selectedIndex].value;
654 window.location = "tt-rss.php?swu=" + user;
655 }
656
657 function editFeedDlg(feed) {
658
659 disableHotkeys();
660
661 if (!feed) {
662 alert("Please select some feed first.");
663 return;
664 }
665
666 if (feed <= 0 || activeFeedIsCat() || tagsAreDisplayed()) {
667 alert("You can't edit this kind of feed.");
668 return;
669 }
670
671 if (xmlhttp_ready(xmlhttp)) {
672 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
673 param_escape(feed), true);
674 xmlhttp.onreadystatechange=infobox_callback;
675 xmlhttp.send(null);
676 } else {
677 printLockingError();
678 }
679 }
680
681 /* this functions duplicate those of prefs.js feed editor, with
682 some differences because there is no feedlist */
683
684 function feedEditCancel() {
685 closeInfoBox();
686 return false;
687 }
688
689 function feedEditSave() {
690
691 try {
692
693 if (!xmlhttp_ready(xmlhttp)) {
694 printLockingError();
695 return
696 }
697
698 // FIXME: add parameter validation
699
700 var query = Form.serialize("edit_feed_form");
701
702 notify_progress("Saving feed...");
703
704 xmlhttp.open("POST", "backend.php", true);
705 xmlhttp.onreadystatechange=dlg_frefresh_callback;
706 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
707 xmlhttp.send(query);
708
709 closeInfoBox();
710
711 return false;
712
713 } catch (e) {
714 exception_error("feedEditSave (main)", e);
715 }
716 }
717