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