]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
initial implementation of feedlist based on dijit.Tree
[tt-rss.git] / tt-rss.js
1 var total_unread = 0;
2 var display_tags = false;
3 var global_unread = -1;
4 var firsttime_update = true;
5 var _active_feed_id = 0;
6 var _active_feed_is_cat = false;
7 var number_of_feeds = 0;
8 var hotkey_prefix = false;
9 var hotkey_prefix_pressed = false;
10 var init_params = {};
11 var _force_scheduled_update = false;
12 var last_scheduled_update = false;
13 var treeModel;
14
15 var _rpc_seq = 0;
16
17 function next_seq() {
18         _rpc_seq += 1;
19         return _rpc_seq;
20 }
21
22 function get_seq() {
23         return _rpc_seq;
24 }
25
26 function activeFeedIsCat() {
27         return _active_feed_is_cat;
28 }
29
30 function getActiveFeedId() {
31         try {
32                 //console.log("gAFID: " + _active_feed_id);
33                 return _active_feed_id;
34         } catch (e) {
35                 exception_error("getActiveFeedId", e);
36         }
37 }
38
39 function setActiveFeedId(id, is_cat) {
40         try {
41                 //console.log("sAFID(" + id + ", " + is_cat + ")");
42                 _active_feed_id = id;
43
44                 if (is_cat != undefined) {
45                         _active_feed_is_cat = is_cat;
46                 }
47
48         } catch (e) {
49                 exception_error("setActiveFeedId", e);
50         }
51 }
52
53
54 function isFeedlistSortable() {
55         return feedlist_sortable_enabled;
56 }
57
58 function tagsAreDisplayed() {
59         return display_tags;
60 }
61
62 function toggleTags(show_all) {
63
64         try {
65
66         console.log("toggleTags: " + show_all + "; " + display_tags);
67
68         var p = $("dispSwitchPrompt");
69
70         if (!show_all && !display_tags) {
71                 displayDlg("printTagCloud");
72         } else if (show_all) {
73                 closeInfoBox();
74                 display_tags = true;
75                 p.innerHTML = __("display feeds");
76                 notify_progress("Loading, please wait...", true);
77                 updateFeedList();
78         } else if (display_tags) {
79                 display_tags = false;
80                 p.innerHTML = __("tag cloud");
81                 notify_progress("Loading, please wait...", true);
82                 updateFeedList();
83         }
84
85         } catch (e) {
86                 exception_error("toggleTags", e);
87         }
88 }
89
90 function dlg_frefresh_callback(transport, deleted_feed) {
91         if (getActiveFeedId() == deleted_feed) {
92                 setTimeout("viewfeed(-5)", 100);
93         }
94
95         setTimeout('updateFeedList(false, false)', 50);
96         closeInfoBox();
97 }
98
99 function updateFeedList() {
100         try {
101                 console.log("updateFeedList");
102         
103 /*              var query_str = "backend.php?op=feeds";
104         
105                 if (display_tags) {
106                         query_str = query_str + "&tags=1";
107                 }
108         
109                 if (getActiveFeedId() && !activeFeedIsCat()) {
110                         query_str = query_str + "&actid=" + getActiveFeedId();
111                 }
112                 
113                 new Ajax.Request("backend.php", {
114                         parameters: query_str,
115                         onComplete: function(transport) { 
116                                 render_feedlist(transport.responseText);
117                         } }); */
118
119         } catch (e) {
120                 exception_error("updateFeedList", e);
121         }
122 }
123
124 function catchupAllFeeds() {
125
126         var str = __("Mark all articles as read?");
127
128         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
129
130                 var query_str = "backend.php?op=feeds&subop=catchupAll";
131
132                 notify_progress("Marking all feeds as read...");
133
134                 //console.log("catchupAllFeeds Q=" + query_str);
135
136                 new Ajax.Request("backend.php", {
137                         parameters: query_str,
138                         onComplete: function(transport) { 
139                                 feedlist_callback2(transport); 
140                         } });
141
142                 global_unread = 0;
143                 updateTitle("");
144         }
145 }
146
147 function viewCurrentFeed(subop) {
148
149         if (getActiveFeedId() != undefined) {
150                 viewfeed(getActiveFeedId(), subop, activeFeedIsCat());
151         }
152         return false; // block unneeded form submits
153 }
154
155 function timeout() {
156         if (getInitParam("bw_limit") == "1") return;
157
158         try {
159            var date = new Date();
160       var ts = Math.round(date.getTime() / 1000);
161
162                 if (ts - last_scheduled_update > 10 || _force_scheduled_update) {
163
164                         //console.log("timeout()");
165
166                         window.clearTimeout(counter_timeout_id);
167                 
168                         var query_str = "?op=rpc&subop=getAllCounters&seq=" + next_seq();
169                 
170                         var omode;
171                 
172                         if (firsttime_update && !navigator.userAgent.match("Opera")) {
173                                 firsttime_update = false;
174                                 omode = "T";
175                         } else {
176                                 if (display_tags) {
177                                         omode = "tl";
178                                 } else {
179                                         omode = "flc";
180                                 }
181                         }
182                         
183                         query_str = query_str + "&omode=" + omode;
184
185                         if (!_force_scheduled_update)
186                                 query_str = query_str + "&last_article_id=" + getInitParam("last_article_id");
187                 
188                         //console.log("[timeout]" + query_str);
189                 
190                         new Ajax.Request("backend.php", {
191                                 parameters: query_str,
192                                 onComplete: function(transport) { 
193                                                 handle_rpc_reply(transport, !_force_scheduled_update);
194                                                 _force_scheduled_update = false;
195                                         } });
196
197                         last_scheduled_update = ts;
198                 }
199
200         } catch (e) {
201                 exception_error("timeout", e);
202         }
203
204         setTimeout("timeout()", 3000);
205 }
206
207 function search() {
208         closeInfoBox(); 
209         viewCurrentFeed();
210 }
211
212 function updateTitle() {
213         var tmp = "Tiny Tiny RSS";
214
215         if (global_unread > 0) {
216                 tmp = tmp + " (" + global_unread + ")";
217         }
218
219         if (window.fluid) {
220                 if (global_unread > 0) {
221                         window.fluid.dockBadge = global_unread;
222                 } else {
223                         window.fluid.dockBadge = "";
224                 }
225         }
226
227         document.title = tmp;
228 }
229
230 function genericSanityCheck() {
231         setCookie("ttrss_test", "TEST");
232         
233         if (getCookie("ttrss_test") != "TEST") {
234                 fatalError(2);
235         }
236
237         return true;
238 }
239
240 function init() {
241         try {
242                 Form.disable("main_toolbar_form");
243
244                 dojo.require("dijit.layout.BorderContainer");
245                 dojo.require("dijit.layout.ContentPane");
246                 dojo.require("dijit.Dialog");
247                 dojo.require("dijit.form.Button");
248                 dojo.require("dojo.data.ItemFileReadStore");
249                 dojo.require("dojo.data.ItemFileWriteStore");
250                 dojo.require("dijit.Tree");
251
252                 dojo.addOnLoad(function() {
253
254                         var store = new dojo.data.ItemFileWriteStore({
255           url: "backend.php?op=feeds"});
256
257                         treeModel = new dijit.tree.ForestStoreModel({
258                                 store: store,
259                                 query: {
260                                         "type": "feed"
261                                 },
262                                 rootId: "root",
263                                 rootLabel: "Feeds",
264                                 childrenAttrs: ["items"]
265                         });
266
267                         var tree = new dijit.Tree({
268                                 model: treeModel,
269                                 _createTreeNode: function(args) {
270                                         var tnode = new dijit._TreeNode(args);
271                                         tnode.labelNode.innerHTML = args.label;
272                                         return tnode;
273                                         },
274                                 getLabelClass: function (item, opened) {
275                                         return (item.unread == 0) ? "dijitTreeLabel" : "dijitTreeLabel Unread";
276                                 },
277                                 getLabel: function(item) {
278                                         if (item.unread > 0) {
279                                                 return item.name + " (" + item.unread + ")";
280                                         } else {
281                                                 return item.name;
282                                         }
283                                 },
284                                 onClick: function (item, node) {
285                                         var id = String(item.id);
286                                         var is_cat = id.match("^CAT:");
287                                         var feed = id.substr(id.indexOf(":")+1);
288                                         viewfeed(feed, '', is_cat);                             
289                                 },
290                                 showRoot: false,
291                         }, "feedTree");
292
293                 });
294
295                 if (!genericSanityCheck()) 
296                         return;
297
298                 var params = "&ua=" + param_escape(navigator.userAgent);
299
300                 loading_set_progress(30);
301
302                 new Ajax.Request("backend.php", {
303                         parameters: "backend.php?op=rpc&subop=sanityCheck" + params,
304                         onComplete: function(transport) {
305                                         backend_sanity_check_callback(transport);
306                                 } });
307
308         } catch (e) {
309                 exception_error("init", e);
310         }
311 }
312
313 function init_second_stage() {
314
315         try {
316
317                 delCookie("ttrss_test");
318
319                 var toolbar = document.forms["main_toolbar_form"];
320
321                 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
322                 dropboxSelect(toolbar.order_by, getInitParam("default_view_order_by"));
323
324                 feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
325
326                 remove_splash();
327                 feedlist_init();
328
329                 console.log("second stage ok");
330
331                 loading_set_progress(60);
332
333                 if (has_local_storage())
334                         localStorage.clear();
335
336         } catch (e) {
337                 exception_error("init_second_stage", e);
338         }
339 }
340
341 function quickMenuChange() {
342         var chooser = $("quickMenuChooser");
343         var opid = chooser[chooser.selectedIndex].value;
344
345         chooser.selectedIndex = 0;
346         quickMenuGo(opid);
347 }
348
349 function quickMenuGo(opid) {
350         try {
351
352                 if (opid == "qmcPrefs") {
353                         gotoPreferences();
354                 }
355         
356                 if (opid == "qmcSearch") {
357                         displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat(), 
358                                 function() { 
359                                         document.forms['search_form'].query.focus();
360                                 });
361                         return;
362                 }
363         
364                 if (opid == "qmcAddFeed") {
365                         quickAddFeed();
366                         return;
367                 }
368
369                 if (opid == "qmcEditFeed") {
370                         editFeedDlg(getActiveFeedId());
371                 }
372         
373                 if (opid == "qmcRemoveFeed") {
374                         var actid = getActiveFeedId();
375
376                         if (activeFeedIsCat()) {
377                                 alert(__("You can't unsubscribe from the category."));
378                                 return;
379                         }       
380
381                         if (!actid) {
382                                 alert(__("Please select some feed first."));
383                                 return;
384                         }
385
386                         var fn = getFeedName(actid);
387
388                         var pr = __("Unsubscribe from %s?").replace("%s", fn);
389
390                         if (confirm(pr)) {
391                                 unsubscribeFeed(actid);
392                         }
393                 
394                         return;
395                 }
396
397                 if (opid == "qmcCatchupAll") {
398                         catchupAllFeeds();
399                         return;
400                 }
401         
402                 if (opid == "qmcShowOnlyUnread") {
403                         toggleDispRead();
404                         return;
405                 }
406         
407                 if (opid == "qmcAddFilter") {
408                         displayDlg('quickAddFilter', '',
409                            function () {document.forms['filter_add_form'].reg_exp.focus();});
410                 }
411
412                 if (opid == "qmcAddLabel") {
413                         addLabel();
414                 }
415
416                 if (opid == "qmcRescoreFeed") {
417                         rescoreCurrentFeed();
418                 }
419
420                 if (opid == "qmcHKhelp") {
421                         //Element.show("hotkey_help_overlay");
422                         Effect.Appear("hotkey_help_overlay", {duration : 0.3});
423                 }
424
425                 if (opid == "qmcResetUI") {
426                         alert("Function not implemented");
427                 }
428
429                 if (opid == "qmcToggleReorder") {
430                         feedlist_sortable_enabled = !feedlist_sortable_enabled;
431
432                         if (feedlist_sortable_enabled) {
433                                 notify_info("Category reordering enabled");
434                                 toggle_sortable_feedlist(true);
435                         } else {
436                                 notify_info("Category reordering disabled");
437                                 toggle_sortable_feedlist(false);
438                         }
439                 }
440
441                 if (opid == "qmcResetCats") {
442
443                         if (confirm(__("Reset category order?"))) {
444
445                                 var query = "?op=feeds&subop=catsortreset";
446
447                                 notify_progress("Loading, please wait...", true);
448
449                                 new Ajax.Request("backend.php", {
450                                         parameters: query,
451                                         onComplete: function(transport) { 
452                                                 window.setTimeout('updateFeedList(false, false)', 50);
453                                         } });
454                         }
455                 }
456
457         } catch (e) {
458                 exception_error("quickMenuGo", e);
459         }
460 }
461
462 function toggleDispRead() {
463         try {
464
465                 var hide = !(getInitParam("hide_read_feeds") == "1");
466
467                 hideOrShowFeeds(hide);
468
469                 var query = "?op=rpc&subop=setpref&key=HIDE_READ_FEEDS&value=" + 
470                         param_escape(hide);
471
472                 new Ajax.Request("backend.php", {
473                         parameters: query,
474                         onComplete: function(transport) { 
475                                 setInitParam("hide_read_feeds", hide);
476                         } });
477                                 
478         } catch (e) {
479                 exception_error("toggleDispRead", e);
480         }
481 }
482
483 function parse_runtime_info(elem) {
484
485         if (!elem || !elem.firstChild) {
486                 console.warn("parse_runtime_info: invalid node passed");
487                 return;
488         }
489
490         var data = JSON.parse(elem.firstChild.nodeValue);
491
492         //console.log("parsing runtime info...");
493
494         for (k in data) {
495                 var v = data[k];
496
497                 // console.log("RI: " + k + " => " + v);
498
499                 if (k == "new_version_available") {
500                         var icon = $("newVersionIcon");
501                         if (icon) {
502                                 if (v == "1") {
503                                         icon.style.display = "inline";
504                                 } else {
505                                         icon.style.display = "none";
506                                 }
507                         }
508                         return;
509                 }
510
511                 var error_flag;
512
513                 if (k == "daemon_is_running" && v != 1) {
514                         notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
515                         return;
516                 }
517
518                 if (k == "daemon_stamp_ok" && v != 1) {
519                         notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
520                         return;
521                 }
522
523                 init_params[k] = v;                                     
524                 notify('');
525         }
526 }
527
528 function catchupCurrentFeed() {
529
530         var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
531         
532         var str = __("Mark all articles in %s as read?").replace("%s", fn);
533
534         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
535                 return viewCurrentFeed('MarkAllRead')
536         }
537 }
538
539 function catchupFeedInGroup(id) {
540
541         try {
542
543                 var title = getFeedName(id);
544
545                 var str = __("Mark all articles in %s as read?").replace("%s", title);
546
547                 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
548                         return viewCurrentFeed('MarkAllReadGR:' + id)
549                 }
550
551         } catch (e) {
552                 exception_error("catchupFeedInGroup", e);
553         }
554 }
555
556 function editFeedDlg(feed) {
557         try {
558
559                 if (!feed) {
560                         alert(__("Please select some feed first."));
561                         return;
562                 }
563         
564                 if ((feed <= 0) || activeFeedIsCat() || tagsAreDisplayed()) {
565                         alert(__("You can't edit this kind of feed."));
566                         return;
567                 }
568         
569                 var query = "";
570         
571                 if (feed > 0) {
572                         query = "?op=pref-feeds&subop=editfeed&id=" +   param_escape(feed);
573                 } else {
574                         query = "?op=pref-labels&subop=edit&id=" +      param_escape(-feed-11);
575                 }
576
577                 disableHotkeys();
578
579                 notify_progress("Loading, please wait...", true);
580
581                 new Ajax.Request("backend.php", {
582                         parameters: query,
583                         onComplete: function(transport) { 
584                                 infobox_callback2(transport); 
585                                 document.forms["edit_feed_form"].title.focus();
586                         } });
587
588         } catch (e) {
589                 exception_error("editFeedDlg", e);
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                 // FIXME: add parameter validation
606
607                 var query = Form.serialize("edit_feed_form");
608
609                 notify_progress("Saving feed...");
610
611                 new Ajax.Request("backend.php", {
612                         parameters: query,
613                         onComplete: function(transport) { 
614                                 dlg_frefresh_callback(transport); 
615                         } });
616
617                 cache_flush();
618                 closeInfoBox();
619
620                 return false;
621
622         } catch (e) {
623                 exception_error("feedEditSave (main)", e);
624         } 
625 }
626
627 function collapse_feedlist() {
628         try {
629                 //console.log("collapse_feedlist");
630                 
631 /*              var theme = getInitParam("theme");
632                 if (theme != "" && 
633                                 !getInitParam("theme_options").match("collapse_feedlist")) return;
634
635                 var fl = $("feeds-holder");
636                 var fh = $("headlines-frame");
637                 var fc = $("content-frame");
638                 var ft = $("toolbar");
639                 var ff = $("footer");
640                 var fhdr = $("header");
641                 var fbtn = $("collapse_feeds_btn");
642
643                 if (!Element.visible(fl)) {
644                         Element.show(fl);
645                         fbtn.innerHTML = "&lt;&lt;";
646
647                         if (theme != "graycube") {
648
649                                 fh.style.left = fl.offsetWidth + "px";
650                                 ft.style.left = fl.offsetWidth + "px";
651                                 if (fc) fc.style.left = fl.offsetWidth + "px";
652                                 if (ff && theme != "compat") ff.style.left = (fl.offsetWidth-1) + "px";
653
654                                 if (theme == "compact") fhdr.style.left = (fl.offsetWidth + 10) + "px";
655                         } else {
656                                 fh.style.left = fl.offsetWidth + 40 + "px";
657                                 ft.style.left = fl.offsetWidth + 40 +"px";
658                                 if (fc) fc.style.left = fl.offsetWidth + 40 + "px";
659                         }
660
661                         query = "?op=rpc&subop=setpref&key=_COLLAPSED_FEEDLIST&value=false";
662
663                         new Ajax.Request("backend.php", { parameters: query });
664
665                 } else {
666                         Element.hide(fl);
667                         fbtn.innerHTML = "&gt;&gt;";
668
669                         if (theme != "graycube") {
670
671                                 fh.style.left = "0px";
672                                 ft.style.left = "0px";
673                                 if (fc) fc.style.left = "0px";
674                                 if (ff) ff.style.left = "0px";
675
676                                 if (theme == "compact") fhdr.style.left = "10px";
677
678                         } else {
679                                 fh.style.left = "20px";
680                                 ft.style.left = "20px";
681                                 if (fc) fc.style.left = "20px";
682
683                         }
684
685                         query = "?op=rpc&subop=setpref&key=_COLLAPSED_FEEDLIST&value=true";
686
687                         new Ajax.Request("backend.php", { parameters: query });
688
689                 } */
690
691                 query = "?op=rpc&subop=setpref&key=_COLLAPSED_FEEDLIST&value=true";
692
693         } catch (e) {
694                 exception_error("collapse_feedlist", e);
695         }
696 }
697
698 function viewModeChanged() {
699         cache_flush();
700         return viewCurrentFeed('')
701 }
702
703 function viewLimitChanged() {
704         cache_flush();
705         return viewCurrentFeed('')
706 }
707
708 /* function adjustArticleScore(id, score) {
709         try {
710
711                 var pr = prompt(__("Assign score to article:"), score);
712
713                 if (pr != undefined) {
714                         var query = "?op=rpc&subop=setScore&id=" + id + "&score=" + pr;
715
716                         new Ajax.Request("backend.php", {
717                         parameters: query,
718                         onComplete: function(transport) {
719                                         viewCurrentFeed();
720                                 } });
721
722                 }
723         } catch (e) {
724                 exception_error("adjustArticleScore", e);
725         }
726 } */
727
728 function rescoreCurrentFeed() {
729
730         var actid = getActiveFeedId();
731
732         if (activeFeedIsCat() || actid < 0 || tagsAreDisplayed()) {
733                 alert(__("You can't rescore this kind of feed."));
734                 return;
735         }       
736
737         if (!actid) {
738                 alert(__("Please select some feed first."));
739                 return;
740         }
741
742         var fn = getFeedName(actid);
743         var pr = __("Rescore articles in %s?").replace("%s", fn);
744
745         if (confirm(pr)) {
746                 notify_progress("Rescoring articles...");
747
748                 var query = "?op=pref-feeds&subop=rescore&quiet=1&ids=" + actid;
749
750                 new Ajax.Request("backend.php", {
751                         parameters: query,
752                         onComplete: function(transport) {
753                                 viewCurrentFeed();
754                         } });
755         }
756 }
757
758 function hotkey_handler(e) {
759
760         try {
761
762                 var keycode;
763                 var shift_key = false;
764
765                 var cmdline = $('cmdline');
766
767                 try {
768                         shift_key = e.shiftKey;
769                 } catch (e) {
770
771                 }
772         
773                 if (window.event) {
774                         keycode = window.event.keyCode;
775                 } else if (e) {
776                         keycode = e.which;
777                 }
778
779                 var keychar = String.fromCharCode(keycode);
780
781                 if (keycode == 27) { // escape
782                         if (Element.visible("hotkey_help_overlay")) {
783                                 Element.hide("hotkey_help_overlay");
784                         }
785                         hotkey_prefix = false;
786                         closeInfoBox();
787                 } 
788
789                 if (dialogs.length > 0 || !hotkeys_enabled) {
790                         console.log("hotkeys disabled");
791                         return;
792                 }
793
794                 if (keycode == 16) return; // ignore lone shift
795                 if (keycode == 17) return; // ignore lone ctrl
796
797                 if ((keycode == 70 || keycode == 67 || keycode == 71) 
798                                 && !hotkey_prefix) {
799
800                         var date = new Date();
801                         var ts = Math.round(date.getTime() / 1000);
802
803                         hotkey_prefix = keycode;
804                         hotkey_prefix_pressed = ts;
805
806                         cmdline.innerHTML = keychar;
807                         Element.show(cmdline);
808
809                         console.log("KP: PREFIX=" + keycode + " CHAR=" + keychar + " TS=" + ts);
810                         return true;
811                 }
812
813                 if (Element.visible("hotkey_help_overlay")) {
814                         Element.hide("hotkey_help_overlay");
815                 }
816
817                 /* Global hotkeys */
818
819                 Element.hide(cmdline);
820
821                 if (!hotkey_prefix) {
822
823                         if ((keycode == 191 || keychar == '?') && shift_key) { // ?
824                                 if (!Element.visible("hotkey_help_overlay")) {
825                                         //Element.show("hotkey_help_overlay");
826                                         Effect.Appear("hotkey_help_overlay", {duration : 0.3});
827                                 } else {
828                                         Element.hide("hotkey_help_overlay");
829                                 }
830                                 return false;
831                         }
832
833                         if (keycode == 191 || keychar == '/') { // /
834                                 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat(), 
835                                         function() { 
836                                                 document.forms['search_form'].query.focus();
837                                         });
838                                 return false;
839                         }
840
841 /*                      if (keycode == 82 && shift_key) { // R
842                                 scheduleFeedUpdate(true);
843                                 return;
844                         } */
845
846                         if (keycode == 74) { // j
847                                 var feed = getActiveFeedId();
848                                 var new_feed = getRelativeFeedId2(feed, activeFeedIsCat(), 'prev');
849 //                              alert(feed + " IC: " + activeFeedIsCat() + " => " + new_feed);
850                                 if (new_feed) {
851                                         var is_cat = new_feed.match("CAT:");
852                                         if (is_cat) {
853                                                 new_feed = new_feed.replace("CAT:", "");
854                                                 viewCategory(new_feed);
855                                         } else {
856                                                 viewfeed(new_feed, '', false);
857                                         }
858                                 }
859                                 return;
860                         }
861         
862                         if (keycode == 75) { // k
863                                 var feed = getActiveFeedId();
864                                 var new_feed = getRelativeFeedId2(feed, activeFeedIsCat(), 'next');
865 //                              alert(feed + " IC: " + activeFeedIsCat() + " => " + new_feed);
866                                 if (new_feed) {
867                                         var is_cat = new_feed.match("CAT:");
868                                         if (is_cat == "CAT:") {
869                                                 new_feed = new_feed.replace("CAT:", "");
870                                                 viewCategory(new_feed);
871                                         } else {
872                                                 viewfeed(new_feed, '', false);
873                                         }
874                                 }
875                                 return;
876                         }
877
878                         if (shift_key && keycode == 40) { // shift-down
879                                 catchupRelativeToArticle(1);
880                                 return;
881                         }
882
883                         if (shift_key && keycode == 38) { // shift-up
884                                 catchupRelativeToArticle(0);
885                                 return;
886                         }
887
888                         if (shift_key && keycode == 78) { // N
889                                 scrollArticle(50);      
890                                 return;
891                         }
892
893                         if (shift_key && keycode == 80) { // P
894                                 scrollArticle(-50);     
895                                 return;
896                         }
897
898                         if (keycode == 68 && shift_key) { // shift-D
899                                 dismissSelectedArticles();
900                         }
901
902                         if (keycode == 88 && shift_key) { // shift-X
903                                 dismissReadArticles();
904                         }
905
906                         if (keycode == 78 || keycode == 40) { // n, down
907                                 if (typeof moveToPost != 'undefined') {
908                                         moveToPost('next');
909                                         return false;
910                                 }
911                         }
912         
913                         if (keycode == 80 || keycode == 38) { // p, up
914                                 if (typeof moveToPost != 'undefined') {
915                                         moveToPost('prev');
916                                         return false;
917                                 }
918                         }
919
920                         if (keycode == 83 && shift_key) { // S
921                                 selectionTogglePublished(undefined, false, true);
922                                 return;
923                         }
924
925                         if (keycode == 83) { // s
926                                 selectionToggleMarked(undefined, false, true);
927                                 return;
928                         }
929
930
931                         if (keycode == 85) { // u
932                                 selectionToggleUnread(undefined, false, true)
933                                 return;
934                         }
935
936                         if (keycode == 84 && shift_key) { // T
937                                 var id = getActiveArticleId();
938                                 if (id) {
939                                         editArticleTags(id, getActiveFeedId(), isCdmMode());
940                                         return;
941                                 }
942                         }
943
944                         if (keycode == 9) { // tab
945                                 var id = getArticleUnderPointer();
946                                 if (id) {                               
947                                         var cb = $("RCHK-" + id);
948
949                                         if (cb) {
950                                                 cb.checked = !cb.checked;
951                                                 toggleSelectRowById(cb, "RROW-" + id);
952                                                 return false;
953                                         }
954                                 }
955                         }
956
957                         if (keycode == 79) { // o
958                                 if (getActiveArticleId()) {
959                                         openArticleInNewWindow(getActiveArticleId());
960                                         return;
961                                 }
962                         }
963
964                         if (keycode == 81 && shift_key) { // Q
965                                 if (typeof catchupAllFeeds != 'undefined') {
966                                         catchupAllFeeds();
967                                         return;
968                                 }
969                         }
970
971                         if (keycode == 88) { // x
972                                 if (activeFeedIsCat()) {
973                                         toggleCollapseCat(getActiveFeedId());
974                                 }
975                         }
976                 }
977
978                 /* Prefix f */
979
980                 if (hotkey_prefix == 70) { // f 
981
982                         hotkey_prefix = false;
983
984                         if (keycode == 81) { // q
985                                 if (getActiveFeedId()) {
986                                         catchupCurrentFeed();
987                                         return;
988                                 }
989                         }
990
991                         if (keycode == 82) { // r
992                                 if (getActiveFeedId()) {
993                                         viewfeed(getActiveFeedId(), "ForceUpdate", activeFeedIsCat());
994                                         return;
995                                 }
996                         }
997
998                         if (keycode == 65) { // a
999                                 toggleDispRead();
1000                                 return false;
1001                         }
1002
1003 /*                      if (keycode == 85 && shift_key) { // U
1004                                 scheduleFeedUpdate(true);
1005                                 return false;
1006                         } */
1007
1008                         if (keycode == 85) { // u
1009                                 if (getActiveFeedId()) {
1010                                         viewfeed(getActiveFeedId(), "ForceUpdate");
1011                                         return false;
1012                                 }
1013                         }
1014
1015                         if (keycode == 69) { // e
1016                                 editFeedDlg(getActiveFeedId());
1017                                 return false;
1018                         }
1019
1020                         if (keycode == 83) { // s
1021                                 quickAddFeed();
1022                                 return false;
1023                         }
1024
1025                         if (keycode == 67 && shift_key) { // C
1026                                 if (typeof catchupAllFeeds != 'undefined') {
1027                                         catchupAllFeeds();
1028                                         return false;
1029                                 }
1030                         }
1031
1032                         if (keycode == 67) { // c
1033                                 if (getActiveFeedId()) {
1034                                         catchupCurrentFeed();
1035                                         return false;
1036                                 }
1037                         }
1038
1039                         if (keycode == 87) { // w
1040                                 feeds_sort_by_unread = !feeds_sort_by_unread;
1041                                 return resort_feedlist();
1042                         }
1043
1044                         if (keycode == 88) { // x
1045                                 reverseHeadlineOrder();
1046                                 return;
1047                         }
1048                 }
1049
1050                 /* Prefix c */
1051
1052                 if (hotkey_prefix == 67) { // c
1053                         hotkey_prefix = false;
1054
1055                         if (keycode == 70) { // f
1056                                 displayDlg('quickAddFilter', '',
1057                                    function () {document.forms['filter_add_form'].reg_exp.focus();});
1058                                 return false;
1059                         }
1060
1061                         if (keycode == 76) { // l
1062                                 addLabel();
1063                                 return false;
1064                         }
1065
1066                         if (keycode == 83) { // s
1067                                 if (typeof collapse_feedlist != 'undefined') {
1068                                         collapse_feedlist();
1069                                         return false;
1070                                 }
1071                         }
1072
1073                         if (keycode == 77) { // m
1074                                 feedlist_sortable_enabled = !feedlist_sortable_enabled;
1075                                 if (feedlist_sortable_enabled) {
1076                                         notify_info("Category reordering enabled");
1077                                         toggle_sortable_feedlist(true);
1078                                 } else {
1079                                         notify_info("Category reordering disabled");
1080                                         toggle_sortable_feedlist(false);
1081                                 }
1082                         }
1083
1084                         if (keycode == 78) { // n
1085                                 catchupRelativeToArticle(1);
1086                                 return;
1087                         }
1088
1089                         if (keycode == 80) { // p
1090                                 catchupRelativeToArticle(0);
1091                                 return;
1092                         }
1093
1094
1095                 }
1096
1097                 /* Prefix g */
1098
1099                 if (hotkey_prefix == 71) { // g
1100
1101                         hotkey_prefix = false;
1102
1103
1104                         if (keycode == 65) { // a
1105                                 viewfeed(-4);
1106                                 return false;
1107                         }
1108
1109                         if (keycode == 83) { // s
1110                                 viewfeed(-1);
1111                                 return false;
1112                         }
1113
1114                         if (keycode == 80 && shift_key) { // P
1115                                 gotoPreferences();
1116                                 return false;
1117                         }
1118
1119                         if (keycode == 80) { // p
1120                                 viewfeed(-2);
1121                                 return false;
1122                         }
1123
1124                         if (keycode == 70) { // f
1125                                 viewfeed(-3);
1126                                 return false;
1127                         }
1128
1129                         if (keycode == 84 && shift_key) { // T
1130                                 toggleTags();
1131                                 return false;
1132                         }
1133                 }
1134
1135                 /* Cmd */
1136
1137                 if (hotkey_prefix == 224 || hotkey_prefix == 91) { // f 
1138                         hotkey_prefix = false;
1139                         return;
1140                 }
1141
1142                 if (hotkey_prefix) {
1143                         console.log("KP: PREFIX=" + hotkey_prefix + " CODE=" + keycode + " CHAR=" + keychar);
1144                 } else {
1145                         console.log("KP: CODE=" + keycode + " CHAR=" + keychar);
1146                 }
1147
1148
1149         } catch (e) {
1150                 exception_error("hotkey_handler", e);
1151         }
1152 }
1153
1154 function inPreferences() {
1155         return false;
1156 }
1157
1158 function reverseHeadlineOrder() {
1159         try {
1160
1161                 var query_str = "?op=rpc&subop=togglepref&key=REVERSE_HEADLINES";
1162
1163                 new Ajax.Request("backend.php", {
1164                         parameters: query_str,
1165                         onComplete: function(transport) { 
1166                                         viewCurrentFeed();
1167                                 } });
1168
1169         } catch (e) {
1170                 exception_error("reverseHeadlineOrder", e);
1171         }
1172 }
1173
1174 function showFeedsWithErrors() {
1175         displayDlg('feedUpdateErrors');
1176 }
1177
1178 function handle_rpc_reply(transport, scheduled_call) {
1179         try {
1180                 if (transport.responseXML) {
1181
1182                         if (!transport_error_check(transport)) return false;
1183
1184                         var seq = transport.responseXML.getElementsByTagName("seq")[0];
1185
1186                         if (seq) {
1187                                 seq = seq.firstChild.nodeValue;
1188
1189                                 if (get_seq() != seq) {
1190                                         //console.log("[handle_rpc_reply] sequence mismatch: " + seq);
1191                                         return true;
1192                                 }
1193                         }
1194
1195                         var message = transport.responseXML.getElementsByTagName("message")[0];
1196
1197                         if (message) {
1198                                 message = message.firstChild.nodeValue;
1199
1200                                 if (message == "UPDATE_COUNTERS") {
1201                                         console.log("need to refresh counters...");
1202                                         setInitParam("last_article_id", -1);
1203                                         _force_scheduled_update = true;
1204                                 }
1205                         }
1206
1207                         var counters = transport.responseXML.getElementsByTagName("counters")[0];
1208         
1209                         if (counters)
1210                                 parse_counters(counters, scheduled_call);
1211
1212                         var runtime_info = transport.responseXML.getElementsByTagName("runtime-info")[0];
1213
1214                         if (runtime_info)
1215                                 parse_runtime_info(runtime_info);
1216
1217                         if (feedsSortByUnread())
1218                                 resort_feedlist();
1219
1220                         hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
1221
1222                 } else {
1223                         notify_error("Error communicating with server.");
1224                 }
1225
1226         } catch (e) {
1227                 exception_error("handle_rpc_reply", e, transport);
1228         }
1229
1230         return true;
1231 }
1232
1233 function scheduleFeedUpdate() {
1234         try {
1235
1236                 if (!getActiveFeedId()) {
1237                         alert(__("Please select some feed first."));
1238                         return;
1239                 }
1240
1241                 var query = "?op=rpc&subop=scheduleFeedUpdate&id=" + 
1242                         param_escape(getActiveFeedId()) +
1243                         "&is_cat=" + param_escape(activeFeedIsCat());
1244
1245                 console.log(query);
1246
1247                 new Ajax.Request("backend.php", {
1248                         parameters: query,
1249                         onComplete: function(transport) { 
1250
1251                                 if (transport.responseXML) {
1252                                         var message = transport.responseXML.getElementsByTagName("message")[0];
1253
1254                                         if (message) {
1255                                                 notify_info(message.firstChild.nodeValue);
1256                                                 return;
1257                                         }
1258                                 }
1259
1260                                 notify_error("Error communicating with server.");
1261
1262                         } });
1263
1264
1265         } catch (e) {
1266                 exception_error("scheduleFeedUpdate", e);
1267         }
1268 }