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