]> git.wh0rd.org - tt-rss.git/blobdiff - offline.js
automatically extract i18n data from .js files
[tt-rss.git] / offline.js
index 7ffbc39fa8565b5ed493441b119943ccdd8edb0a..010bbe05e2b94e00425face108b46890e497bb79 100644 (file)
@@ -4,6 +4,9 @@ var offline_mode = false;
 var store = false;
 var localServer = false;
 var db = false;
+var articles_synced = 0;
+var sync_in_progress = false;
+var sync_timer = false;
 
 function view_offline(id, feed_id) {
        try {
@@ -143,16 +146,29 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                                var sel_none_link;
                                var sel_inv_link;
 
+                               var catchup_feed_link = "javascript:catchupCurrentFeed()";
+                               var catchup_sel_link = "javascript:catchupSelection()";
+
+                               var tog_unread_link;
+                               var tog_marked_link;
+
                                if ($("content-frame")) {
                                        sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
                                        sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
                                        sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
                                        sel_inv_link = "javascript:invertHeadlineSelection()";
+
+                                       tog_unread_link = "javascript:selectionToggleUnread()";
+                                       tog_marked_link = "javascript:selectionToggleMarked()";
+
                                } else {
                                        sel_all_link = "javascript:cdmSelectArticles('all')";
                                        sel_unread_link = "javascript:cdmSelectArticles('unread')";
                                        sel_none_link = "javascript:cdmSelectArticles('none')";
                                        sel_inv_link = "javascript:invertHeadlineSelection()";
+
+                                       tog_unread_link = "javascript:selectionToggleUnread(true)";
+                                       tog_marked_link = "javascript:selectionToggleMarked(true)";
                                }
 
                                tmp += __('Select:')+
@@ -163,6 +179,25 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
        
                                tmp += "  ";
        
+                               tmp += "<span onmouseover=\"enable_selection(false)\" "+
+                                       "onmouseout=\"enable_selection(true) \"" +
+                                       "onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">" +
+                                       __("Actions...") + "&nbsp;&nbsp;<img src=\"images/down_arrow.png\">" +
+                               "</span>";
+
+                               tmp += "<ul id=\"headlineActionsBody\" style=\"display : none\">";
+
+                               tmp += "<li class=\"insensitive\">"+__('Selection toggle:')+"</li>"+
+                               "<li onclick=\""+tog_unread_link+"\">&nbsp;&nbsp;"+__('Unread')+"</li>"+
+                               "<li onclick=\""+tog_marked_link+"\">&nbsp;&nbsp;"+__('Starred')+"</li>"+
+                               "<li class=\"insensitive\">"+__('Mark as read:')+"</li>"+
+                               "<li onclick=\""+catchup_sel_link+"\">&nbsp;&nbsp;"+__('Selection')+"</li>";
+
+                               tmp += "<li onclick=\""+catchup_feed_link+"\">&nbsp;&nbsp;"+__('Entire feed')+
+                                       "</li>";
+
+                               tmp += "</ul>";
+
                                tmp += "</div>";
        
                                tmp += "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
@@ -564,6 +599,7 @@ function render_offline_feedlist() {
 function init_offline() {
        try {
                offline_mode = true;
+               store.enabled = true;
 
                Element.hide("dispSwitchPrompt");
                Element.hide("feedBrowserPrompt");
@@ -615,13 +651,24 @@ function offline_download_parse(stage, transport) {
        try {
                if (transport.responseXML) {
 
+                       if (!sync_in_progress) return;
+
                        var sync_ok = transport.responseXML.getElementsByTagName("sync-ok");
 
                        if (sync_ok.length > 0) {
                                for (var i = 0; i < sync_ok.length; i++) {
                                        var id = sync_ok[i].getAttribute("id");
+                                       var unread = sync_ok[i].getAttribute("unread");
+                                       var marked = sync_ok[i].getAttribute("marked");
+
                                        if (id) {
                                                debug("synced offline info for id " + id);
+
+                                               if (unread != undefined && marked != undefined) {
+                                                       db.execute("UPDATE articles SET "+
+                                                               "unread = ?, marked = ? WHERE id = ?", [unread, marked, id]);
+
+                                               }
                                                db.execute("UPDATE articles SET modified = '' WHERE id = ?", [id]);
                                        }
                                }
@@ -629,6 +676,8 @@ function offline_download_parse(stage, transport) {
 
                        if (stage == 0) {
 
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing feeds...");
+
                                var feeds = transport.responseXML.getElementsByTagName("feed");
 
                                if (feeds.length > 0) {
@@ -646,6 +695,8 @@ function offline_download_parse(stage, transport) {
                                                [id, title, has_icon, cat_id]);
                                }
 
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing categories...");
+
                                var cats = transport.responseXML.getElementsByTagName("category");
 
                                if (feeds.length > 0) {
@@ -662,6 +713,8 @@ function offline_download_parse(stage, transport) {
                                                [id, title, collapsed]);
                                }
 
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing labels...");
+
                                var labels = transport.responseXML.getElementsByTagName("label");
 
                                if (labels.length > 0) {
@@ -679,7 +732,9 @@ function offline_download_parse(stage, transport) {
                                                [id, caption, fg_color, bg_color]);
                                }
 
-                               window.setTimeout("update_offline_data("+(stage+1)+")", 5*1000);
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing articles...");
+
+                               sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")", 2*1000);
                        } else {
 
                                var articles = transport.responseXML.getElementsByTagName("article");
@@ -720,23 +775,47 @@ function offline_download_parse(stage, transport) {
 
                                debug("downloaded articles: " + articles_found + " limit: " + limit);
 
+                               articles_synced += articles_found;
+
+                               var msg =__("Synchronizing articles (%d)...").replace("%d", articles_synced);
+
+                               $("offlineModeSyncMsg").innerHTML = msg;
+
                                var has_sync_data = has_local_sync_data();
 
                                if (articles_found >= limit || has_sync_data) {
-                                       window.setTimeout("update_offline_data("+(stage+1)+")", 5*1000);
+                                       sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")", 
+                                               3*1000);
                                        debug("<b>update_offline_data: done " + stage + " HSD: " + 
                                                has_sync_data + "</b>");
                                } else {
-                                       window.setTimeout("update_offline_data(0)", 180*1000);
+                                       window.setTimeout("offlineDownloadStart()", 180*1000);
                                        debug("update_offline_data: finished");
 
-                                       var pic = $("restartOfflinePic");
+                                       var pic = $("offlineModePic");
 
                                        if (pic) { 
                                                pic.src = "images/offline.png";
-                                               pic.title = __("Restart in offline mode");
+
+                                               var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
+                                               var last_sync = "";
+
+                                               if (rs.isValidRow()) {
+                                                       last_sync = rs.field(0).substring(0,16);
+                                               }
+                                               rs.close();
+
+                                               var msg = __("Last sync: %s").replace("%s", last_sync);
+
+                                               articles_synced = 0;
+
+                                               $("offlineModeSyncMsg").innerHTML = msg;                                        
                                        }                        
 
+                                       offlineSyncShowHideElems(false);
+
+                                       sync_in_progress = false;
+
                                        db.execute("DELETE FROM articles WHERE "+
                                                "updated < DATETIME('NOW', 'localtime', '-31 days')");
 
@@ -748,7 +827,21 @@ function offline_download_parse(stage, transport) {
 
 //                     notify('');
 
+               } else {
+                       sync_in_progress = false;
+
+                       var pic = $("offlineModePic");
+
+                       if (pic) { 
+                               pic.src = "images/offline.png";
+                               var msg = __("Last sync: Error receiving data.");
+                               articles_synced = 0;
+                               $("offlineModeSyncMsg").innerHTML = msg;                                        
+                       }                        
+
+                       offlineSyncShowHideElems(false);
                }
+
        } catch (e) {
                exception_error("offline_download_parse", e);
        }
@@ -758,9 +851,8 @@ function update_offline_data(stage) {
        try {
 
                if (!stage) stage = 0;
-               if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
 
-//             notify_progress("Updating offline data... (" + stage +")", true);
+               if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
 
                var query = "backend.php?op=rpc&subop=download";
                
@@ -773,7 +865,7 @@ function update_offline_data(stage) {
                        query = query + "&cidt=" + offline_dl_max_id;
                        query = query + "&cidb=" + offline_dl_min_id;
 
-                       stage = 1;
+                       if (stage > 0) stage = 1;
                }
 
                rs.close();
@@ -790,13 +882,20 @@ function update_offline_data(stage) {
 
                debug(query + "/" + to_sync);
 
-               var pic = $("restartOfflinePic");
+               var pic = $("offlineModePic");
 
                if (pic) {
                        pic.src = "images/offline-sync.gif";
-                       pic.title = __("Synchronizing offline data...");
+                       if (articles_synced == 0) {
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing...");
+                       }
                }
 
+               offlineSyncShowHideElems(true);
+               offlineUpdateStore();
+
+               sync_in_progress = true;
+
                new Ajax.Request(query, {
                        parameters: to_sync,
                        onComplete: function(transport) { 
@@ -959,8 +1058,8 @@ function enable_offline_reading() {
 
                if (db && getInitParam("offline_enabled") == "1") {
                        init_local_sync_data();
-                       Element.show("restartOfflinePic");
-                       window.setTimeout("update_offline_data(0)", 100);
+                       Element.show("offlineModePic");
+                       offlineDownloadStart();
                }
 
        } catch (e) {
@@ -972,8 +1071,17 @@ function init_gears() {
        try {
 
                if (window.google && google.gears) {
-                       localServer = google.gears.factory.create("beta.localserver");
+
+                       try {
+                               localServer = google.gears.factory.create("beta.localserver");
+                       } catch (e) {
+                               return;
+                       }
+
                        store = localServer.createManagedStore("tt-rss");
+                       store.manifestUrl = "manifest.json.php";
+                       store.enabled = false;
+
                        db = google.gears.factory.create('beta.database');
                        db.open('tt-rss');
 
@@ -1061,12 +1169,61 @@ function init_gears() {
        }
 }
 
+function offlineArticlesStored() {
+
+       var rs = db.execute("SELECT COUNT(*) FROM articles");
+       var count = 0;
+       if (rs.isValidRow()) {
+               count = rs.field(0);
+       }
+
+       rs.close();
+
+       return count;
+}
+
 function gotoOffline() {
-       window.location.href = "tt-rss.php?offline=1";
+
+//     debug("[Local store] currentVersion = " + store.currentVersion);
+
+
+       if (offlineArticlesStored() == 0) {
+               notify_error("You have to synchronize some articles before going into offline mode.");
+               return;
+       }
+
+       if (confirm(__("Switch Tiny Tiny RSS into offline mode?"))) {
+
+               store.enabled = true;
+               store.checkForUpdate();
+       
+               notify_progress("Preparing offline mode...", true);
+       
+               var timerId = window.setInterval(function() {
+                       if (store.currentVersion) {
+                               window.clearInterval(timerId);
+                               debug("[Local store] sync complete: " + store.currentVersion);
+
+                               //window.location.href = "tt-rss.php";
+
+                               offlineDownloadStop();
+                               init_offline();
+
+                               notify_info("Tiny Tiny RSS is in offline mode.");
+
+                       } else if (store.updateStatus == 3) {
+                               debug("[Local store] sync error: " + store.lastErrorMessage);
+                               notify_error(store.lastErrorMessage, true);
+                       } }, 500);
+       }
 }
 
 function gotoOnline() {
-       window.location.href = "tt-rss.php";
+//     if (confirm(__("You won't be able to access offline version of Tiny Tiny RSS until you switch it into offline mode again. Go online?"))) {
+       if (confirm(__("Tiny Tiny RSS will reload. Go online?"))) {
+               //localServer.removeManagedStore("tt-rss");
+               window.location.href = "tt-rss.php";
+       }
 }
 
 function local_collapse_cat(id) {
@@ -1451,3 +1608,146 @@ function catchup_local_feed(id, is_cat) {
                exception_error("catchup_local_feed", e);
        }
 }
+
+function toggleOfflineModeInfo() {
+       try {
+               var e = $('offlineModeDrop');
+               var p = $('offlineModePic');
+               
+               if (Element.visible(e)) {
+                       Element.hide(e);
+               } else {
+                       Element.show(e);
+               }
+
+       } catch (e) {
+               exception_error("toggleOfflineModeInfo", e);
+       }
+}
+
+function offlineDownloadStart(stage) {
+       try {
+               if (!stage) stage = 0;
+
+               if (db && !sync_in_progress && getInitParam("offline_enabled") == "1") {
+                       window.setTimeout("update_offline_data("+stage+")", 100);
+               }
+       } catch (e) {
+               exception_error("offlineDownloadStart", e);
+       }
+}
+
+function offlineDownloadStop() {
+       try {
+               if (db && sync_in_progress && getInitParam("offline_enabled") == "1") {
+
+                       sync_in_progress = false;
+
+                       if (sync_timer) {
+                               window.clearTimeout(sync_timer);
+                               sync_timer = false;
+                       }
+
+                       var pic = $("offlineModePic");
+       
+                       if (pic) { 
+                               pic.src = "images/offline.png";
+                               var msg = __("Last sync: Cancelled.");
+                               articles_synced = 0;
+                               $("offlineModeSyncMsg").innerHTML = msg;                                        
+                       }                        
+
+                       offlineSyncShowHideElems(false);
+
+               }
+       } catch (e) {
+               exception_error("offlineDownloadStart", e);
+       }
+}
+
+function offlineClearData() {
+       try {
+               if (db) {
+
+                       if (confirm(__("This will remove all offline data stored by Tiny Tiny RSS on this computer. Continue?"))) {
+
+                               notify_progress("Removing offline data...");
+
+                               localServer.removeManagedStore("tt-rss");
+
+                               db.execute("DELETE FROM articles");
+                               db.execute("DELETE FROM article_labels");
+                               db.execute("DELETE FROM labels");
+                               db.execute("DELETE FROM feeds");
+                               db.execute("DELETE FROM cache");
+
+                               notify_info("Local data removed.");
+                       }
+               }
+       } catch (e) {
+               exception_error("offlineClearData", e);
+       }
+}
+
+function offlineUpdateStore() {
+       try {
+               if (offline_mode || !store) return;
+
+               store.checkForUpdate();
+       
+               var timerId = window.setInterval(function() {
+                       if (store.currentVersion) {
+                               window.clearInterval(timerId);
+                               debug("[Local store] sync complete: " + store.currentVersion);
+                       } else if (store.updateStatus == 3) {
+                               debug("[Local store] sync error: " + store.lastErrorMessage);
+                       } }, 500);
+
+       } catch (e) {
+               exception_error("offlineUpdateStore", e);
+       }
+}
+
+function offlineSyncShowHideElems(syncing) {
+       try {
+
+               var elems = $$("div.hideWhenSyncing");
+       
+               for (var j = 0; j < elems.length; j++) {
+                       if (syncing) {
+                               Element.hide(elems[j]);
+                       } else {
+                               Element.show(elems[j]);
+                       }
+               }
+
+               var elems = $$("div.showWhenSyncing");
+       
+               for (var j = 0; j < elems.length; j++) {
+                       if (syncing) {
+                               Element.show(elems[j]);
+                       } else {
+                               Element.hide(elems[j]);
+                       }
+               }
+
+       } catch (e) {
+               exception_error("offlineSyncShowHideElems", e);
+       }
+}
+
+function offlineConfirmModeChange() {
+       try {
+               
+               if (db && getInitParam("offline_enabled") == "1" && offlineArticlesStored() > 0) {
+                       var ok = confirm(__("Tiny Tiny RSS has trouble accessing its server. Would you like to go offline?")); 
+
+                       if (ok) {
+                               init_offline();
+                       }
+               }
+
+       } catch (e) {
+               exception_error("offlineConfirmModeChange", e);
+       }
+}