]> git.wh0rd.org - tt-rss.git/blobdiff - tt-rss.js
toggle no daemon warning @ runtime on refetch
[tt-rss.git] / tt-rss.js
index 14e4b4ceeb21b92b526ee9a3864ce285c62570f3..0b00737af290645d30b095b92b6a69b70dab218e 100644 (file)
--- a/tt-rss.js
+++ b/tt-rss.js
@@ -1,39 +1,20 @@
-/*
-       This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>              
-       Licensed under GPL v.2 or (at your preference) any later version.
-*/
-
 var xmlhttp = false;
-
 var total_unread = 0;
 var first_run = true;
-
 var display_tags = false;
-
-var global_unread = 0;
-
+var global_unread = -1;
 var active_title_text = "";
-
 var current_subtitle = "";
+var daemon_enabled = false;
+var _qfd_deleted_feed = 0;
+var firsttime_update = true;
+var last_refetch = 0;
+var cookie_lifetime = 0;
+var active_feed_id = 0;
 
-/*@cc_on @*/
-/*@if (@_jscript_version >= 5)
-// JScript gives us Conditional compilation, we can cope with old IE versions.
-// and security blocked creation of the objects.
-try {
-       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
-} catch (e) {
-       try {
-               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
-       } catch (E) {
-               xmlhttp = false;
-       }
-}
-@end @*/
+var xmlhttp = Ajax.getTransport();
 
-if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
-       xmlhttp = new XMLHttpRequest();
-}
+var init_params = new Array();
 
 function toggleTags() {
        display_tags = !display_tags;
@@ -51,131 +32,123 @@ function toggleTags() {
 
 function dlg_frefresh_callback() {
        if (xmlhttp.readyState == 4) {
+               notify(xmlhttp.responseText);
                updateFeedList(false, false);
-               closeDlg();
-       } 
-}
-
-function dialog_refresh_callback() {
-       if (xmlhttp.readyState == 4) {
-               var dlg = document.getElementById("userDlg");
-
-               dlg.innerHTML = xmlhttp.responseText;
-               dlg.style.display = "block";
+               if (_qfd_deleted_feed) {
+                       var hframe = document.getElementById("headlines-frame");
+                       if (hframe) {
+                               hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
+                       }
+               }
+               closeInfoBox();
        } 
 }
 
 function refetch_callback() {
        if (xmlhttp.readyState == 4) {
+               try {
 
-//             document.title = "Tiny Tiny RSS";
+                       var date = new Date();
 
-               if (!xmlhttp.responseXML) {
-                       notify("refetch_callback: backend did not return valid XML");
-                       return;
-               }
-       
-               var reply = xmlhttp.responseXML.firstChild;
+                       last_refetch = date.getTime() / 1000;
 
-               if (!reply) {
-                       notify("refetch_callback: backend did not return expected XML object");
-                       return;
-               } 
-
-               var error_code = reply.getAttribute("error-code");
+                       if (!xmlhttp.responseXML) {
+                               notify("refetch_callback: backend did not return valid XML", true, true);
+                               return;
+                       }
+               
+                       var reply = xmlhttp.responseXML.firstChild;
        
-               if (error_code && error_code != 0) {
-                       return fatalError(error_code);
-               }
-
-               var f_document = window.frames["feeds-frame"].document;
-
-               for (var l = 0; l < reply.childNodes.length; l++) {
-                       var id = reply.childNodes[l].getAttribute("id");
-                       var ctr = reply.childNodes[l].getAttribute("counter");
+                       if (!reply) {
+                               notify("refetch_callback: backend did not return expected XML object", true, true);
+                               updateTitle("");
+                               return;
+                       } 
+       
+                       var error_code = reply.getAttribute("error-code");
+               
+                       if (error_code && error_code != 0) {
+                               return fatalError(error_code, reply.getAttribute("error-msg"));
+                       }
 
-                       var feedctr = f_document.getElementById("FEEDCTR-" + id);
-                       var feedu = f_document.getElementById("FEEDU-" + id);
-                       var feedr = f_document.getElementById("FEEDR-" + id);
+                       var counters = reply.firstChild;
+       
+                       parse_counters(counters, true);
 
-/*                     TODO figure out how to update this from viewfeed.js->view()
-                       disabled for now...
+                       var runtime_info = counters.nextSibling;
 
-                       if (id == "global-unread") {
-                               global_unread = ctr;
-                       } */
+                       parse_runtime_info(runtime_info);
 
-                       if (feedctr && feedu && feedr) {
+                       debug("refetch_callback: done");
 
-                               feedu.innerHTML = ctr;
-       
-                               if (ctr > 0) {
-                                       feedctr.className = "odd";
-                                       if (!feedr.className.match("Unread")) {
-                                               feedr.className = feedr.className + "Unread";
-                                       }
-                               } else {
-                                       feedctr.className = "invisible";
-                                       feedr.className = feedr.className.replace("Unread", "");
-                               }
+                       if (!daemon_enabled) {
+                               notify("All feeds updated.");
+                               updateTitle("");
+                       } else {
+                               notify("");
                        }
-               }  
-
-               updateTitle("");
-               notify("All feeds updated.");
-
+               } catch (e) {
+                       exception_error("refetch_callback", e);
+                       updateTitle("");
+               }
        }
 }
 
 function backend_sanity_check_callback() {
 
        if (xmlhttp.readyState == 4) {
-               
-               if (!xmlhttp.responseXML) {
-                       fatalError(3);
-                       return;
-               }
-
-               var reply = xmlhttp.responseXML.firstChild;
 
-               if (!reply) {
-                       fatalError(3);
-                       return;
-               }
-
-               var error_code = reply.getAttribute("error-code");
+               try {
+               
+                       if (!xmlhttp.responseXML) {
+                               fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
+                               return;
+                       }
        
-               if (error_code && error_code != 0) {
-                       return fatalError(error_code);
-               }
+                       var reply = xmlhttp.responseXML.firstChild.firstChild;
+       
+                       if (!reply) {
+                               fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
+                               return;
+                       }
+       
+                       var error_code = reply.getAttribute("error-code");
+               
+                       if (error_code && error_code != 0) {
+                               return fatalError(error_code, reply.getAttribute("error-msg"));
+                       }
+       
+                       debug("sanity check ok");
 
-               init_second_stage();
-       } 
-}
+                       var params = reply.nextSibling;
 
-/* wtf this is obsolete
-function updateFeed(feed_id) {
+                       if (params) {
+                               debug('reading init-params...');
+                               var param = params.firstChild;
 
-       var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
+                               while (param) {
+                                       var k = param.getAttribute("key");
+                                       var v = param.getAttribute("value");
+                                       debug(k + " => " + v);
+                                       init_params[k] = v;                                     
+                                       param = param.nextSibling;
+                               }
+                       }
 
-       if (xmlhttp_ready(xmlhttp)) {
-               xmlhttp.open("GET", query_str, true);
-               xmlhttp.onreadystatechange=feed_update_callback;
-               xmlhttp.send(null);
-       } else {
-               printLockingError();
-       }   
+                       init_second_stage();
 
+               } catch (e) {
+                       exception_error("backend_sanity_check_callback", e);
+               }
+       } 
 }
-*/
 
 function scheduleFeedUpdate(force) {
 
-       notify("Updating feeds in background...");
-
-//     document.title = "Tiny Tiny RSS - Updating...";
-
-       updateTitle("Updating");
+       if (!daemon_enabled) {
+               notify("Updating feeds, please wait.", true);
+               updateTitle("Updating");
+       }
 
        var query_str = "backend.php?op=rpc&subop=";
 
@@ -187,19 +160,35 @@ function scheduleFeedUpdate(force) {
 
        var omode;
 
-       if (display_tags) {
-               omode = "t";
+       if (firsttime_update && !navigator.userAgent.match("Opera")) {
+               firsttime_update = false;
+               omode = "T";
        } else {
-               omode = "fl";
+               if (display_tags) {
+                       omode = "t";
+               } else {
+                       omode = "flc";
+               }
        }
-
+       
        query_str = query_str + "&omode=" + omode;
+       query_str = query_str + "&uctr=" + global_unread;
+
+       debug("in scheduleFeedUpdate");
+
+       var date = new Date();
+
+       if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
+               debug("<b>xmlhttp seems to be stuck, aborting</b>");
+               xmlhttp.abort();
+       }
 
        if (xmlhttp_ready(xmlhttp)) {
                xmlhttp.open("GET", query_str, true);
                xmlhttp.onreadystatechange=refetch_callback;
                xmlhttp.send(null);
        } else {
+               debug("xmlhttp busy");
                printLockingError();
        }   
 }
@@ -220,6 +209,12 @@ function updateFeedList(silent, fetch) {
                query_str = query_str + "&actid=" + getActiveFeedId();
        }
 
+       if (navigator.userAgent.match("Opera")) {
+               var date = new Date();
+               var timestamp = Math.round(date.getTime() / 1000);
+               query_str = query_str + "&ts=" + timestamp
+       }
+       
        if (fetch) query_str = query_str + "&fetch=yes";
 
        var feeds_frame = document.getElementById("feeds-frame");
@@ -250,6 +245,7 @@ function viewCurrentFeed(skip, subop) {
                disableContainerChildren("headlinesToolbar", false, document);
                viewfeed(-1, skip, subop); // FIXME
        }
+       return false; // block unneeded form submits
 }
 
 function viewfeed(feed, skip, subop) {
@@ -259,7 +255,12 @@ function viewfeed(feed, skip, subop) {
 
 function timeout() {
        scheduleFeedUpdate(false);
-       setTimeout("timeout()", 1800*1000);
+
+       var refresh_time = getInitParam("feeds_frame_refresh");
+
+       if (!refresh_time) refresh_time = 600; 
+
+       setTimeout("timeout()", refresh_time*1000);
 }
 
 function resetSearch() {
@@ -271,7 +272,12 @@ function resetSearch() {
        }
 }
 
+function searchCancel() {
+       closeInfoBox(true);
+}
+
 function search() {
+       closeInfoBox(); 
        viewCurrentFeed(0, "");
 }
 
@@ -288,38 +294,12 @@ function localPiggieFunction(enable) {
        }
 }
 
-function localHotkeyHandler(keycode) {
-
-/*     if (keycode == 78) {
-               return moveToPost('next');
-       }
-
-       if (keycode == 80) {
-               return moveToPost('prev');
-       } */
-
-       if (keycode == 82) { // r
-               return scheduleFeedUpdate(true);
-       }
-
-       if (keycode == 85) { // u
-               if (getActiveFeedId()) {
-                       return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
-               }
-       }
-
-       if (keycode == 65) { // a
-               return toggleDispRead();
-       }
-
-//     notify("KC: " + keycode);
-
-}
-
+// if argument is undefined, current subtitle is not updated
+// use blank string to clear subtitle
 function updateTitle(s) {
        var tmp = "Tiny Tiny RSS";
 
-       if (s && s.length > 0) {
+       if (s != undefined) {
                current_subtitle = s;
        }
 
@@ -327,7 +307,7 @@ function updateTitle(s) {
                tmp = tmp + " (" + global_unread + ")";
        }
 
-       if (s) {
+       if (current_subtitle) {
                tmp = tmp + " - " + current_subtitle;
        }
 
@@ -348,220 +328,211 @@ function genericSanityCheck() {
                fatalError(2);
        }
 
-/*     if (!xmlhttp) {
-               document.getElementById("headlines").innerHTML = 
-                       "<b>Fatal error:</b> This program requires XmlHttpRequest " + 
-                       "to function properly. Your browser doesn't seem to support it.";
-               return false;
-       }
-
-       setCookie("ttrss_vf_test", "TEST");
-       if (getCookie("ttrss_vf_test") != "TEST") {
-
-               document.getElementById("headlines").innerHTML = 
-                       "<b>Fatal error:</b> This program requires cookies " + 
-                       "to function properly. Your browser doesn't seem to support them.";
-
-               return false;
-       } */
-
        return true;
 }
 
 function init() {
 
-       disableContainerChildren("headlinesToolbar", true);
+       try {
 
-       if (!genericSanityCheck()) 
-               return;
+               // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
 
-       xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
-       xmlhttp.onreadystatechange=backend_sanity_check_callback;
-       xmlhttp.send(null);
+               if (arguments.callee.done) return;
+               arguments.callee.done = true;           
 
-}
+               disableContainerChildren("headlinesToolbar", true);
 
-function init_second_stage() {
-
-       setCookie("ttrss_vf_actfeed", "");
+               Form.disable("main_toolbar_form");
 
-       updateFeedList(false, false);
-       document.onkeydown = hotkey_handler;
+               if (!genericSanityCheck()) 
+                       return;
 
-       var content = document.getElementById("content");
+               if (getURLParam('debug')) {
+                       document.getElementById('debug_output').style.display = 'block';
+                       debug('debug mode activated');
+               }
 
-       if (getCookie("ttrss_vf_vmode")) {
-               var viewbox = document.getElementById("viewbox");
-               viewbox.value = getCookie("ttrss_vf_vmode");
-       }
+               xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
+               xmlhttp.onreadystatechange=backend_sanity_check_callback;
+               xmlhttp.send(null);
 
-       if (getCookie("ttrss_vf_limit")) {
-               var limitbox = document.getElementById("limitbox");
-               limitbox.value = getCookie("ttrss_vf_limit");
+       } catch (e) {
+               exception_error("init", e);
        }
+}
 
-//     if (getCookie("ttrss_vf_actfeed")) {
-//             viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
-//     }
-
-//     setTimeout("timeout()", 2*1000);
-//     scheduleFeedUpdate(true);
+function resize_feeds_frame() {
+       var f = document.getElementById("feeds-frame");
+       var tf = document.getElementById("mainFooter");
+       var th = document.getElementById("mainHeader");
+                
+       f.style.height = document.body.scrollHeight - tf.scrollHeight - 
+               th.scrollHeight - 50 + "px";
+}
 
-       var splash = document.getElementById("splash");
+function init_second_stage() {
 
-       if (splash) {
-               splash.style.display = "none";
-       }
+       try {
 
-}
+               cookie_lifetime = getCookie("ttrss_cltime");
 
-function quickMenuGo() {
+               delCookie("ttrss_vf_test");
+       
+               updateFeedList(false, false);
+               document.onkeydown = hotkey_handler;
 
-       var chooser = document.getElementById("quickMenuChooser");
-       var opid = chooser[chooser.selectedIndex].id;
+               var tb = parent.document.forms["main_toolbar_form"];
 
-       if (opid == "qmcPrefs") {
-               gotoPreferences();
-       }
+               dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
+               dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
 
-       if (opid == "qmcAdvSearch") {
-               displayDlg("search");
-               return;
-       }
+               daemon_enabled = getInitParam("daemon_enabled");
 
-       if (opid == "qmcAddFeed") {
-               displayDlg("quickAddFeed");
-               return;
-       }
+               // FIXME should be callled after window resize
 
-       if (opid == "qmcRemoveFeed") {
-               var actid = getActiveFeedId();
+               var h = document.getElementById("headlines");
+               var c = document.getElementById("content");
 
-               if (!actid) {
-                       notify("Please select some feed first.");
-                       return;
+               if (navigator.userAgent.match("Opera")) {
+                       resize_feeds_frame();
                }
-       
-               displayDlg("quickDelFeed", actid);
-               return;
-       }
-
-       if (opid == "qmcUpdateFeeds") {
-               scheduleFeedUpdate(true);
-               return;
-       }
-
-       if (opid == "qmcCatchupAll") {
-               catchupAllFeeds();
-               return;
-       }
 
-       if (opid == "qmcShowOnlyUnread") {
-               toggleDispRead();
-               return;
+               debug("second stage ok");
+       
+       } catch (e) {
+               exception_error("init_second_stage", e);
        }
-
 }
 
-function qafAdd() {
+function quickMenuChange() {
+       var chooser = document.getElementById("quickMenuChooser");
+       var opid = chooser[chooser.selectedIndex].value;
 
-       if (!xmlhttp_ready(xmlhttp)) {
-               printLockingError();
-               return
-       }
+       chooser.selectedIndex = 0;
+       quickMenuGo(opid);
+}
 
-       var link = document.getElementById("qafInput");
+function quickMenuGo(opid) {
+       try {
 
-       if (link.value.length == 0) {
-               notify("Missing feed URL.");
-       } else {
-               notify("Adding feed...");
+               if (opid == "qmcPrefs") {
+                       gotoPreferences();
+               }
+       
+               if (opid == "qmcSearch") {
+                       displayDlg("search", getActiveFeedId());
+                       return;
+               }
+       
+               if (opid == "qmcAddFeed") {
+                       displayDlg("quickAddFeed");
+                       return;
+               }
+       
+               if (opid == "qmcRemoveFeed") {
+                       var actid = getActiveFeedId();
+       
+                       if (!actid) {
+                               alert("Please select some feed first.");
+                               return;
+                       }
+       
+                       if (confirm("Unsubscribe from current feed?")) {
+                               qfdDelete(actid);
+                       }
                
-               var feeds_doc = window.frames["feeds-frame"].document;
-
-               feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
-
-               xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
-                       param_escape(link.value), true);
-               xmlhttp.onreadystatechange=dlg_frefresh_callback;
-               xmlhttp.send(null);
-
-               link.value = "";
-
+                       return;
+               }
+       
+               if (opid == "qmcUpdateFeeds") {
+                       scheduleFeedUpdate(true);
+                       return;
+               }
+       
+               if (opid == "qmcCatchupAll") {
+                       catchupAllFeeds();
+                       return;
+               }
+       
+               if (opid == "qmcShowOnlyUnread") {
+                       toggleDispRead();
+                       return;
+               }
+       
+               if (opid == "qmcAddFilter") {
+                       displayDlg("quickAddFilter", getActiveFeedId());
+               }
+       } catch (e) {
+               exception_error("quickMenuGo", e);
        }
 }
 
-function displayDlg(id, param) {
-
-       notify("");
-
-       xmlhttp.open("GET", "backend.php?op=dlg&id=" +
-               param_escape(id) + "&param=" + param_escape(param), true);
-       xmlhttp.onreadystatechange=dialog_refresh_callback;
-       xmlhttp.send(null);
-
-}
-
-function closeDlg() {
-       var dlg = document.getElementById("userDlg");
-       dlg.style.display = "none";
-}
-
 function qfdDelete(feed_id) {
 
        notify("Removing feed...");
 
-       var feeds_doc = window.frames["feeds-frame"].document;
-       feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       _qfd_deleted_feed = feed_id;
 
-       xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids=" + feed_id);
+       xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
        xmlhttp.onreadystatechange=dlg_frefresh_callback;
        xmlhttp.send(null);
 }
 
 
-function allFeedsMenuGo() {
-       var chooser = document.getElementById("allFeedsChooser");
-
-       var opname = chooser[chooser.selectedIndex].text;
-
-       if (opname == "Update") {
-               scheduleFeedUpdate(true);
-               return;
-       }
-
-       if (opname == "Mark as read") {
-               catchupAllFeeds();
-               return;
-       }
-
-       if (opname == "Show only unread") {
-               toggleDispRead();
-               return;
-       }
-
-}
-
 function updateFeedTitle(t) {
        active_title_text = t;
        updateTitle();
 }
 
 function toggleDispRead() {
-       var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
+       try {
+
+               if (!xmlhttp_ready(xmlhttp)) {
+                       printLockingError();
+                       return
+               }
 
-       hide_read_feeds = !hide_read_feeds;
+               var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
 
-       var feeds_doc = window.frames["feeds-frame"].document;
+               hide_read_feeds = !hide_read_feeds;
 
-       hideOrShowFeeds(feeds_doc, hide_read_feeds);
+               debug("toggle_disp_read => " + hide_read_feeds);
 
-       if (hide_read_feeds) {
-               setCookie("ttrss_vf_hreadf", 1);
-       } else {
-               setCookie("ttrss_vf_hreadf", 0);
-       }
+               hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
+
+               var query = "backend.php?op=rpc&subop=setpref" +
+                       "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
 
+               storeInitParam("hide_read_feeds", hide_read_feeds, true);
+
+               new Ajax.Request(query);
+               
+       } catch (e) {
+               exception_error("toggleDispRead", e);
+       }
 }
 
+function parse_runtime_info(elem) {
+       var param = elem.firstChild;
 
+       while (param) {
+               var k = param.getAttribute("key");
+               var v = param.getAttribute("value");
+
+               var w = document.getElementById("noDaemonWarning");
+               
+               if (w) {
+                       if (k == "daemon_is_running" && v != 1) {
+                               w.style.display = "block";
+                       } else {
+                               w.style.display = "none";
+                       }
+               }
+               param = param.nextSibling;
+       }
+}