]> git.wh0rd.org Git - tt-rss.git/commitdiff
automagically updating labels with cute XML RPC
authorAndrew Dolgov <fox@bah.spb.su>
Thu, 8 Sep 2005 12:10:07 +0000 (13:10 +0100)
committerAndrew Dolgov <fox@bah.spb.su>
Thu, 8 Sep 2005 12:10:07 +0000 (13:10 +0100)
backend.php
functions.js
tt-rss.js
view.js [new file with mode: 0644]
viewfeed.js

index 7903dcf55188b3293d7ec2a4e14df1a104158a55..71b7d45742838a25155fa2f8711ffcc0f65a3f53 100644 (file)
@@ -1,5 +1,9 @@
 <?
-//     header("Content-Type: application/xml");
+       $op = $_GET["op"];
+
+       if ($op == "rpc") {
+               header("Content-Type: application/xml");
+       }
 
        require_once "config.php";
        require_once "db.php";
        if (DB_TYPE == "pgsql") {
                pg_query("set client_encoding = 'utf-8'");
        }
-
-       $op = $_GET["op"];
+       
        $fetch = $_GET["fetch"];
 
+       function getLabelCounters($link) {
+
+               $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
+                       WHERE marked = true");
+
+               $count = pg_fetch_result($result, 0, "count");
+
+               print "<label id=\"-1\" counter=\"$count\"/>";
+
+               $result = db_query($link, "SELECT id,sql_exp,description FROM
+                       ttrss_labels ORDER by description");
+       
+               while ($line = db_fetch_assoc($result)) {
+
+                       $id = -$line["id"] - 11;
+
+                       error_reporting (0);
+       
+                       $tmp_result = pg_query("SELECT count(id) as count FROM ttrss_entries
+                               WHERE " . $line["sql_exp"]);
+
+                       $count = pg_fetch_result($tmp_result, 0, "count");
+
+                       print "<feed id=\"$id\" counter=\"$count\"/>";
+
+                       error_reporting (E_ERROR | E_WARNING | E_PARSE);
+       
+               }
+       }
+
+       function getFeedCounters($link) {
+
+               $result = db_query($link, "SELECT id,
+                       (SELECT count(id) FROM ttrss_entries WHERE feed_id = ttrss_feeds.id 
+                               AND unread = true) as count
+                       FROM ttrss_feeds");
+       
+               while ($line = db_fetch_assoc($result)) {
+               
+                       $id = $line["id"];
+                       $count = $line["count"];
+
+                       print "<feed id=\"$id\" counter=\"$count\"/>";
+               }
+       }
+
        function outputFeedList($link) {
 
                print "<html><head>
@@ -37,7 +86,7 @@
 
                if (ENABLE_LABELS) {
 
-                       $result = db_query($link, "SELECT id,description FROM
+                       $result = db_query($link, "SELECT id,sql_exp,description FROM
                                ttrss_labels ORDER by description");
        
                        if (db_num_rows($result) > 0) {
                        }
        
                        while ($line = db_fetch_assoc($result)) {
+
+                               error_reporting (0);
        
+                               $tmp_result = pg_query("SELECT count(id) as count FROM ttrss_entries
+                                       WHERE " . $line["sql_exp"]);
+
+                               $count = pg_fetch_result($tmp_result, 0, "count");
+                               
+                               error_reporting (E_ERROR | E_WARNING | E_PARSE);
+
                                printFeedEntry(-$line["id"]-11, 
-                                       "odd", $line["description"], 0, "images/label.png");
+                                       "odd", $line["description"], $count, "images/label.png");
        
                        }
                }
 
                $subop = $_GET["subop"];
 
+               if ($subop == "getLabelCounters") {
+                       print "<rpc-reply>";
+                       getLabelCounters($link);
+                       print "</rpc-reply>";
+               }
+
+               if ($subop == "getFeedCounters") {
+                       print "<rpc-reply>";
+                       getFeedCounters($link);
+                       print "</rpc-reply>";
+               }
+
+               if ($subop == "getAllCounters") {
+                       print "<rpc-reply>";
+                       getLabelCounters($link);
+                       getFeedCounters($link);
+                       print "</rpc-reply>";
+
+               }
+
                if ($subop == "mark") {
                        $mark = $_GET["mark"];
                        $id = db_escape_string($_GET["id"]);
                        return;
                }
 
-               if ($subop == "forceUpdateAllFeeds") {
+               if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
                        update_all_feeds($link, true);                  
-               }
 
-               if ($subop == "updateAllFeeds") {
-                       update_all_feeds($link, false);
+                       print "<rpc-reply>";
+                       getLabelCounters($link);
+                       getFeedCounters($link);
+                       print "</rpc-reply>";
                }
                
                if ($subop == "catchupPage") {
 
                        print "Marked active page as read.";
                }
-
        }
        
        if ($op == "feeds") {
                        
                        print "</div>";
 
+                       print "<script type=\"text/javascript\">
+                               update_label_counters();
+                       </script>";
                }
 
                if ($addheader) {
 
                } else {
 //                     print "[viewfeed] feed type not implemented<br>";                       
-                       $unread = 0;
+
+                       error_reporting(0);
+                       $result = db_query($link, "SELECT count(id) as unread FROM ttrss_entries
+                               WHERE $query_strategy_part");                   
+
+                       $unread = db_fetch_result($result, 0, "unread");
+
+                       error_reporting (E_ERROR | E_WARNING | E_PARSE);
                }                               
 
+               if (!$unread) $unread = 0;
+
                // update unread/total counters and status for active feed in the feedlist 
                // kludge, because iframe doesn't seem to support onload() 
                
 
                        var feedctr = p_document.getElementById(\"FEEDCTR-\" + $feed);
 
-                       if ($unread > 0 && $feed != -1 && !feedr.className.match(\"Unread\")) {
+                       if ($unread > 0 && $feed >= 0 && !feedr.className.match(\"Unread\")) {
                                        feedr.className = feedr.className + \"Unread\";
                                        feedctr.className = '';
                        } else if ($unread <= 0) {      
                                        feedctr.className = 'invisible';
                        }       
 
+                       update_label_counters();
+
 //                     p_notify(\"\");
 
                </script>";
index 4e758a7d08dd5d5adf840b456cb555237ca93593..0db4ea307205b6f1bf3273d4cbb34d21d40d6c33 100644 (file)
@@ -278,3 +278,57 @@ function getActiveFeedId() {
 function setActiveFeedId(id) {
        return setCookie("ttrss_vf_actfeed", id);
 }
+
+var xmlhttp_rpc = false;
+
+/*@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_rpc = new ActiveXObject("Msxml2.XMLHTTP");
+} catch (e) {
+       try {
+               xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
+       } catch (E) {
+               xmlhttp_rpc = false;
+       }
+}
+@end @*/
+
+if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
+       xmlhttp_rpc = new XMLHttpRequest();
+}
+
+function label_counters_callback() {
+       if (xmlhttp_rpc.readyState == 4) {
+               var reply = xmlhttp_rpc.responseXML.firstChild;
+
+               var f_document = parent.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");
+
+                       var feedctr = f_document.getElementById("FEEDCTR-" + id);
+                       var feedu = f_document.getElementById("FEEDU-" + id);
+
+                       feedu.innerHTML = ctr;
+
+                       if (ctr > 0) {
+                               feedctr.className = "odd";
+                       } else {
+                               feedctr.className = "invisible";
+                       }
+               }
+       }
+}
+
+function update_label_counters() {
+       if (xmlhttp_ready(xmlhttp_rpc)) {
+               var query = "backend.php?op=rpc&subop=getLabelCounters";        
+               xmlhttp_rpc.open("GET", query, true);
+               xmlhttp_rpc.onreadystatechange=label_counters_callback;
+               xmlhttp_rpc.send(null);
+       }
+}
index 4f532d60a194100269341bf740aa259833ad7422..d1bcad69221f9da77ec62b000e981cdb653447dc 100644 (file)
--- a/tt-rss.js
+++ b/tt-rss.js
@@ -49,17 +49,37 @@ function feedlist_callback() {
 */
 
 function refetch_callback() {
-
        if (xmlhttp.readyState == 4) {
 
                document.title = "Tiny Tiny RSS";
                notify("All feeds updated.");
 
-               updateFeedList();
-               
-       } 
-}
+               var reply = xmlhttp.responseXML.firstChild;
+
+               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");
 
+                       var feedctr = f_document.getElementById("FEEDCTR-" + id);
+                       var feedu = f_document.getElementById("FEEDU-" + id);
+                       var feedr = f_document.getElementById("FEEDR-" + id);
+
+                       feedu.innerHTML = ctr;
+
+                       if (ctr > 0) {
+                               feedctr.className = "odd";
+                               if (id > 0 && !feedr.className.match("Unread")) {
+                                       feedr.className = feedr.className + "Unread";
+                               }
+                       } else {
+                               feedctr.className = "invisible";
+                               feedr.className = feedr.className.replace("Unread", "");
+                       }
+               }  
+       }
+}
 
 function updateFeed(feed_id) {
 
diff --git a/view.js b/view.js
new file mode 100644 (file)
index 0000000..6450a0b
--- /dev/null
+++ b/view.js
@@ -0,0 +1,53 @@
+var xmlhttp_rpc = false;
+
+/*@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_rpc = new ActiveXObject("Msxml2.XMLHTTP");
+} catch (e) {
+       try {
+               xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
+       } catch (E) {
+               xmlhttp_rpc = false;
+       }
+}
+@end @*/
+
+if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
+       xmlhttp_rpc = new XMLHttpRequest();
+}
+
+function label_counters_callback() {
+       if (xmlhttp_rpc.readyState == 4) {
+               var reply = xmlhttp_rpc.responseXML.firstChild;
+
+               var f_document = parent.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");
+
+                       var feedctr = f_document.getElementById("FEEDCTR-" + id);
+                       var feedu = f_document.getElementById("FEEDU-" + id);
+
+                       feedu.innerHTML = ctr;
+
+                       if (ctr > 0) {
+                               feedctr.className = "odd";
+                       } else {
+                               feedctr.className = "invisible";
+                       }
+               }
+       }
+}
+
+function update_label_counters() {
+       if (xmlhttp_ready(xmlhttp_rpc)) {
+               var query = "backend.php?op=rpc&subop=getLabelCounters";        
+               xmlhttp_rpc.open("GET", query, true);
+               xmlhttp_rpc.onreadystatechange=label_counters_callback;
+               xmlhttp_rpc.send(null);
+       }
+}
index 5bcb381241763aad1e924d167d200bae05f14980..4bded27625c2fdbc720931dea8fd5454f9c44540 100644 (file)
@@ -8,7 +8,7 @@ var xmlhttp_rpc = false;
 // 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");
+       xmlhttp_rpc = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
        try {
                xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
@@ -26,23 +26,25 @@ function view(id, feed_id) {
 
 //     p_notify("Loading article...");
 
-       enableHotkeys();
+       var f_document = parent.frames["feeds-frame"].document;
+       var h_document = document;
+       var m_document = parent.document;
 
-       var crow = document.getElementById("RROW-" + id);
+       enableHotkeys();
 
-       var f_doc = parent.frames["feeds-frame"].document;
+       var crow = h_document.getElementById("RROW-" + id);
 
        if (crow.className.match("Unread")) {
-               var umark = f_doc.getElementById("FEEDU-" + feed_id);
+               var umark = f_document.getElementById("FEEDU-" + feed_id);
                
                umark.innerHTML = umark.innerHTML - 1;
                crow.className = crow.className.replace("Unread", "");
 
                if (umark.innerHTML == "0") {
-                       var feedr = f_doc.getElementById("FEEDR-" + feed_id);   
+                       var feedr = f_document.getElementById("FEEDR-" + feed_id);      
                        feedr.className = feedr.className.replace("Unread", "");
 
-                       var feedctr = f_doc.getElementById("FEEDCTR-" + feed_id);
+                       var feedctr = f_document.getElementById("FEEDCTR-" + feed_id);
 
                        if (feedctr) {
                                feedctr.className = "invisible";
@@ -55,7 +57,7 @@ function view(id, feed_id) {
 
        cleanSelected("headlinesList");
 
-       var upd_img_pic = document.getElementById("FUPDPIC-" + id);
+       var upd_img_pic = h_document.getElementById("FUPDPIC-" + id);
 
        if (upd_img_pic) {
                upd_img_pic.src = "images/blank_icon.gif";
@@ -64,7 +66,7 @@ function view(id, feed_id) {
        var unread_rows = getVisibleUnreadHeadlines();
 
        if (unread_rows.length == 0) {
-               var button = document.getElementById("btnCatchupPage");
+               var button = h_document.getElementById("btnCatchupPage");
                if (button) {
                        button.className = "disabledButton";
                        button.href = "";
@@ -74,17 +76,20 @@ function view(id, feed_id) {
        active_post_id = id; 
        setActiveFeedId(feed_id);
 
-       var content = parent.document.getElementById("content-frame");
+       var content = m_document.getElementById("content-frame");
 
        if (content) {
                content.src = "backend.php?op=view&addheader=true&id=" + param_escape(id);
                markHeadline(active_post_id);
        }
+
 }
 
 
 function toggleMark(id, toggle) {
 
+       var f_document = parent.frames["feeds-frame"].document;
+
        if (!xmlhttp_ready(xmlhttp_rpc)) {
                printLockingError();
                return;
@@ -94,9 +99,9 @@ function toggleMark(id, toggle) {
 
        var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
 
-       var f_doc = parent.frames["feeds-frame"].document;
+       var vfeedu = f_document.getElementById("FEEDU--1");
 
-       var vfeedu = f_doc.getElementById("FEEDU--1");
+//     alert(vfeedu);
 
        if (toggle == true) {
                mark_img.src = "images/mark_set.png";
@@ -116,7 +121,7 @@ function toggleMark(id, toggle) {
 
        }
 
-       var vfeedctr = f_doc.getElementById("FEEDCTR--1");
+       var vfeedctr = f_document.getElementById("FEEDCTR--1");
 
        if (vfeedu && vfeedctr) {
                if ((+vfeedu.innerHTML) > 0) {