]> git.wh0rd.org - tt-rss.git/blobdiff - offline.js
automatically extract i18n data from .js files
[tt-rss.git] / offline.js
index 172aea3143446757a0faf7f3900369fa3d6aa431..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 {
@@ -92,21 +95,25 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                setActiveFeedId(feed_id, is_cat);
 
                if (!is_cat) {
-                       var feedr = document.getElementById("FEEDR-" + feed_id);
+                       var feedr = $("FEEDR-" + feed_id);
                        if (feedr && !feedr.className.match("Selected")) {      
                                feedr.className = feedr.className + "Selected";
                        } 
                } else {
-                       var feedr = document.getElementById("FCAT-" + feed_id);
+                       var feedr = $("FCAT-" + feed_id);
                        if (feedr && !feedr.className.match("Selected")) {      
                                feedr.className = feedr.className + "Selected";
                        } 
                }
 
+               if (subop == "MarkAllRead") {
+                       catchup_local_feed(feed_id, is_cat);
+               }
+
                disableContainerChildren("headlinesToolbar", false);
                Form.enable("main_toolbar_form");
 
-               var f = document.getElementById("headlines-frame");
+               var f = $("headlines-frame");
                try {
                        if (reply.offset == 0) { 
                                debug("resetting headlines scrollTop");
@@ -116,8 +123,13 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
 
 
                var tmp = "";
+               var feed_title = "";
 
-               var feed_title = get_local_feed_title(feed_id);
+               if (is_cat) {
+                       feed_title = get_local_category_title(feed_id);
+               } else {                
+                       feed_title = get_local_feed_title(feed_id);
+               }
 
                if (feed_title) {
 
@@ -134,16 +146,29 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                                var sel_none_link;
                                var sel_inv_link;
 
-                               if (document.getElementById("content-frame")) {
+                               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:')+
@@ -154,10 +179,29 @@ 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()\">";
-                               if (document.getElementById("content-frame")) {
+                               if ($("content-frame")) {
                                        tmp += "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
                                }
                        
@@ -182,11 +226,13 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                        if (view_mode == "all_articles") {
                                mode_qpart = "1";
                        } else if (view_mode == "adaptive") {
-                               if (get_local_feed_unread(feed_id) > 0) {
-                                       mode_qpart = "unread = 1";
+                               if (is_cat && get_local_category_unread(feed_id) ||
+                                       get_local_feed_unread(feed_id) > 0) {
+                                               mode_qpart = "unread = 1";
                                } else {
                                        mode_qpart = "1";
                                }
+                               
                        } else if (view_mode == "marked") {
                                mode_qpart = "marked = 1";
                        } else if (view_mode == "unread") {
@@ -197,7 +243,14 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
 
                        var ext_tables_qpart = "";
 
-                       if (feed_id > 0) {
+                       if (is_cat) {
+                               if (feed_id >= 0) {
+                                       strategy_qpart = "cat_id = " + feed_id;
+                               } else if (feed_id == -2) {
+                                       strategy_qpart = "article_labels.id = articles.id";
+                                       ext_tables_qpart = ",article_labels";
+                               }
+                       } else if (feed_id > 0) {
                                strategy_qpart = "feed_id = " + feed_id;
                        } else if (feed_id == -1) {
                                strategy_qpart = "marked = 1";
@@ -216,8 +269,9 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                        }
 
                        var query = "SELECT *,feeds.title AS feed_title "+
-                               "FROM articles,feeds"+ext_tables_qpart+" "+
+                               "FROM articles,feeds,categories"+ext_tables_qpart+" "+
                                "WHERE " +
+                               "cat_id = categories.id AND " +
                                "feed_id = feeds.id AND " +
                                strategy_qpart +
                                " AND " + mode_qpart + 
@@ -238,7 +292,7 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
 
                                var entry_feed_title = false;
 
-                               if (real_feed_id < 0) {
+                               if (real_feed_id < 0 || is_cat) {
                                        entry_feed_title = rs.fieldByName("feed_title");
                                }
 
@@ -252,8 +306,6 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
 
                                var labels = get_local_article_labels(id);
 
-                               debug(labels);
-
                                var labels_str = "<span id=\"HLLCTR-"+id+"\">";
                                labels_str += format_article_labels(labels, id);
                                labels_str += "</span>";
@@ -274,7 +326,7 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                                var content_preview = truncate_string(strip_tags(rs.fieldByName("content")), 
                                                100);
        
-                               if (document.getElementById("content-frame")) {
+                               if ($("content-frame")) {
 
                                        tmp += "<tr class='"+row_class+"' id='RROW-"+id+"' "+mouseover_attrs+">";
                                        
@@ -396,12 +448,12 @@ function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, off
                        }
        
                        if (offset == 0) {
-                               var container = document.getElementById("headlines-frame");
+                               var container = $("headlines-frame");
                                container.innerHTML = tmp;
                        } else {
                                var ids = getSelectedArticleIds2();
                
-                               var container = document.getElementById("headlinesList");
+                               var container = $("headlinesList");
                                container.innerHTML = container.innerHTML + tmp;
        
                                for (var i = 0; i < ids.length; i++) {
@@ -504,14 +556,14 @@ function render_offline_feedlist() {
                                if (tmp_cat_id != -1) {
                                        tmp += "</ul></li>";
                                }
-                               tmp += printCategoryHeader(cat_id, cat_hidden, false);
+                               tmp += printCategoryHeader(cat_id, cat_hidden, true);
                                tmp_cat_id = cat_id;
                        }
 
                        var icon = "";
 
                        if (has_icon) {
-                               icon = "icons/" + id + ".ico";
+                               icon = getInitParam("icons_url") + "/" + id + ".ico";
                        }
 
                        var feed_icon = "";
@@ -547,6 +599,7 @@ function render_offline_feedlist() {
 function init_offline() {
        try {
                offline_mode = true;
+               store.enabled = true;
 
                Element.hide("dispSwitchPrompt");
                Element.hide("feedBrowserPrompt");
@@ -554,10 +607,10 @@ function init_offline() {
                Element.hide("topLinksOnline");
                Element.show("topLinksOffline");
 
-               var tb_form = document.getElementById("main_toolbar_form");
+               var tb_form = $("main_toolbar_form");
                Element.hide(tb_form.update);
 
-               var chooser = document.getElementById("quickMenuChooser");
+               var chooser = $("quickMenuChooser");
                chooser.disabled = true;
 
                var rs = db.execute("SELECT key, value FROM init_params");
@@ -598,8 +651,33 @@ 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]);
+                                       }
+                               }
+                       }
+
                        if (stage == 0) {
 
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing feeds...");
+
                                var feeds = transport.responseXML.getElementsByTagName("feed");
 
                                if (feeds.length > 0) {
@@ -617,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) {
@@ -633,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) {
@@ -650,7 +732,9 @@ function offline_download_parse(stage, transport) {
                                                [id, caption, fg_color, bg_color]);
                                }
 
-                               window.setTimeout("update_offline_data("+(stage+1)+")", 10*1000);
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing articles...");
+
+                               sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")", 2*1000);
                        } else {
 
                                var articles = transport.responseXML.getElementsByTagName("article");
@@ -671,7 +755,6 @@ function offline_download_parse(stage, transport) {
                                        if (a) {
 
                                                db.execute("DELETE FROM articles WHERE id = ?", [a.id]);
-                                               db.execute("DELETE FROM article_labels WHERE id = ?", [a.id]);
 
                                                db.execute("INSERT INTO articles "+
                                                "(id, feed_id, title, link, guid, updated, content, "+
@@ -692,24 +775,73 @@ function offline_download_parse(stage, transport) {
 
                                debug("downloaded articles: " + articles_found + " limit: " + limit);
 
-                               if (articles_found >= limit) {
-                                       window.setTimeout("update_offline_data("+(stage+1)+")", 10*1000);
-                                       debug("update_offline_data: done " + stage);
+                               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) {
+                                       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)", 1800*1000);
+                                       window.setTimeout("offlineDownloadStart()", 180*1000);
                                        debug("update_offline_data: finished");
 
-                                       var date = new Date();
-                                       var ts = Math.round(date.getTime() / 1000);
+                                       var pic = $("offlineModePic");
+
+                                       if (pic) { 
+                                               pic.src = "images/offline.png";
+
+                                               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);
 
-                                       db.execute("DELETE FROM articles WHERE added < ? - 2592000", [ts]);
+                                       sync_in_progress = false;
+
+                                       db.execute("DELETE FROM articles WHERE "+
+                                               "updated < DATETIME('NOW', 'localtime', '-31 days')");
 
                                }
                        }
 
+                       update_local_sync_data();
+               
+
 //                     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);
        }
@@ -719,14 +851,11 @@ function update_offline_data(stage) {
        try {
 
                if (!stage) stage = 0;
-               if (offline_mode) return;
 
-               debug("update_offline_data: stage " + stage);
-
-//             notify_progress("Updating offline data... (" + stage +")", true);
-
-               var query = "backend.php?op=rpc&subop=download&stage=" + stage;
+               if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
 
+               var query = "backend.php?op=rpc&subop=download";
+               
                var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
 
                if (rs.isValidRow() && rs.field(0)) {
@@ -735,11 +864,40 @@ function update_offline_data(stage) {
 
                        query = query + "&cidt=" + offline_dl_max_id;
                        query = query + "&cidb=" + offline_dl_min_id;
+
+                       if (stage > 0) stage = 1;
                }
 
                rs.close();
 
+               debug("update_offline_data: stage " + stage);
+
+               query = query + "&stage=" + stage;
+
+               var to_sync = prepare_local_sync_data();
+
+               if (to_sync != "") {
+                       to_sync = "?sync=" + param_escape(to_sync);
+               }
+
+               debug(query + "/" + to_sync);
+
+               var pic = $("offlineModePic");
+
+               if (pic) {
+                       pic.src = "images/offline-sync.gif";
+                       if (articles_synced == 0) {
+                               $("offlineModeSyncMsg").innerHTML = __("Synchronizing...");
+                       }
+               }
+
+               offlineSyncShowHideElems(true);
+               offlineUpdateStore();
+
+               sync_in_progress = true;
+
                new Ajax.Request(query, {
+                       parameters: to_sync,
                        onComplete: function(transport) { 
                                offline_download_parse(stage, transport);                               
                        } });
@@ -752,12 +910,12 @@ function update_offline_data(stage) {
 function set_feedlist_counter(id, ctr, is_cat) {
        try {
 
-               var feedctr = document.getElementById("FEEDCTR-" + id);
-               var feedu = document.getElementById("FEEDU-" + id);
-               var feedr = document.getElementById("FEEDR-" + id);
+               var feedctr = $("FEEDCTR-" + id);
+               var feedu = $("FEEDU-" + id);
+               var feedr = $("FEEDR-" + id);
 
                if (is_cat) {
-                       var catctr = document.getElementById("FCATCTR-" + id);
+                       var catctr = $("FCATCTR-" + id);
                        if (catctr) {
                                catctr.innerHTML = "(" + ctr + ")";
                                if (ctr > 0) {
@@ -805,7 +963,7 @@ function set_feedlist_counter(id, ctr, is_cat) {
 
 function update_local_feedlist_counters() {
        try {
-               if (!offline_mode) return;
+               if (!offline_mode || !db) return;
 
 /*             var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
                        "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
@@ -895,12 +1053,35 @@ function get_local_feed_unread(id) {
        }
 }
 
+function enable_offline_reading() {
+       try {
+
+               if (db && getInitParam("offline_enabled") == "1") {
+                       init_local_sync_data();
+                       Element.show("offlineModePic");
+                       offlineDownloadStart();
+               }
+
+       } catch (e) {
+               exception_error("enable_offline_reading", e);
+       }
+}
+
 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');
 
@@ -927,16 +1108,21 @@ function init_gears() {
                                db.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
                                db.execute("DROP INDEX IF EXISTS articles_unread_idx");
                                db.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
+                               db.execute("DROP INDEX IF EXISTS articles_id_idx");
+                               db.execute("DROP INDEX IF EXISTS article_labels_id_idx");
                                db.execute("DROP TABLE IF EXISTS version");
-                               db.execute("DROP TRIGGER IF EXISTS articles_update_modified");
+                               db.execute("DROP TRIGGER IF EXISTS articles_update_unread");
+                               db.execute("DROP TRIGGER IF EXISTS articles_update_marked");
+                               db.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
                                db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
+                               db.execute("DROP TABLE IF EXISTS syncdata");
                                db.execute("INSERT INTO version (schema_version) VALUES (?)", 
                                        [SCHEMA_VERSION]);
                        }
 
                        db.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
 
-                       db.execute("CREATE TABLE IF NOT EXISTS cache (id text, article text, param text, added text)");
+                       db.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
                        db.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
                        db.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
                        db.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
@@ -946,16 +1132,33 @@ function init_gears() {
                        db.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
                        db.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
                        db.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
+                       db.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
+                       db.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
+
+                       db.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
 
                        db.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
 
-                       db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_modified "+
+                       db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
                                "UPDATE OF unread ON articles "+
                                "BEGIN "+
-                               "UPDATE articles SET modified = DATETIME('NOW') WHERE id = old.id; "+
+                               "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
+                               "WHERE id = OLD.id AND "+
+                               "OLD.unread != NEW.unread;"+
+                               "END;");
+
+                       db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
+                               "UPDATE OF marked ON articles "+
+                               "BEGIN "+
+                               "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
+                               "WHERE id = OLD.id;"+
                                "END;");
 
-                       Element.show("restartOfflinePic");
+                       db.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
+                               "DELETE ON articles "+
+                               "BEGIN "+
+                               "DELETE FROM article_labels WHERE id = OLD.id; "+
+                               "END; ");
 
                }       
        
@@ -966,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) {
@@ -987,6 +1239,7 @@ function local_collapse_cat(id) {
 
 function get_local_category_title(id) {
        try {
+       
                var rs = db.execute("SELECT title FROM categories WHERE id = ?", [id]);
                var tmp = "";
 
@@ -1056,7 +1309,7 @@ function printCategoryHeader(cat_id, hidden, can_browse) {
                        var inner_title_class = "catTitleNL";
 
                        if (can_browse) {
-                               browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\"";
+                               browse_cat_link = "onclick=\"javascript:viewCategory("+cat_id+")\"";
                                inner_title_class = "catTitle";
                        }
 
@@ -1134,7 +1387,7 @@ function get_local_article_labels(id) {
 
 function label_local_add_article(id, label_id) {
        try {
-               debug("label_local_add_article " + id + " => " + label_id);
+               //debug("label_local_add_article " + id + " => " + label_id);
 
                var rs = db.execute("SELECT COUNT(id) FROM article_labels WHERE "+
                        "id = ? AND label_id = ?", [id, label_id]);
@@ -1213,3 +1466,288 @@ function format_article_labels(labels, id) {
        }
 }
 
+function init_local_sync_data() {
+       try {
+
+               if (!db) return;
+
+               var rs = db.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
+               var has_last_online = 0;
+
+               if (rs.isValidRow()) {
+                       has_last_online = rs.field(0);
+               }
+
+               rs.close();
+
+               if (!has_last_online) {
+                       db.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
+               }
+
+       } catch (e) {
+               exception_error("init_local_sync_data", e);
+
+       }
+}
+
+function has_local_sync_data() {
+       try {
+
+               var rs = db.execute("SELECT id FROM articles "+
+                       "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
+                       "LIMIT 1");
+
+               var tmp = 0;
+
+               if (rs.isValidRow()) {
+                       tmp = rs.field(0);
+               }
+
+               rs.close();
+
+               return tmp != 0;
+
+       } catch (e) {
+               exception_error("has_local_sync_data", e);
+       }
+}
+
+function prepare_local_sync_data() {
+       try {
+               var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
+
+               var last_online = "";
+               
+               if (rs.isValidRow()) {
+                       last_online = rs.field(0);
+               }
+
+               rs.close();
+
+               var rs = db.execute("SELECT id,unread,marked FROM articles "+
+                       "WHERE modified > ? LIMIT 200", [last_online]);
+
+               var tmp = last_online + ";";
+
+               var entries = 0;
+
+               while (rs.isValidRow()) {
+                       var e = new Array();
+
+                       tmp = tmp + rs.field(0) + "," + rs.field(1) + "," + rs.field(2) + ";";
+                       entries++;
+
+                       rs.next();
+               }
+
+               rs.close();
+
+               if (entries > 0) {
+                       return tmp;
+               } else {
+                       return '';
+               }
+
+       } catch (e) {
+               exception_error("prepare_local_sync_data", e);
+       }
+}
+
+function update_local_sync_data() {
+       try {
+               if (db && !offline_mode) {
+
+                       var rs = db.execute("SELECT id FROM articles "+
+                               "WHERE modified > (SELECT value FROM syncdata WHERE "+
+                                       "key = 'last_online') LIMIT 1")
+
+                       var f_id = 0;
+
+                       if (rs.isValidRow()) {
+                               f_id = rs.field(0);
+                       }
+
+                       rs.close();
+
+                       /* no pending articles to sync */
+
+                       if (f_id == 0) {
+                               db.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
+                                       "WHERE key = 'last_online'");
+                       }
+
+               }
+       } catch (e) {
+               exception_error("update_local_sync_data", e);
+       }
+}
+
+function catchup_local_feed(id, is_cat) {
+       try {
+               if (!db) return;
+
+               if (!is_cat) {
+                       if (id >= 0) {
+                               db.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id]);
+                       } else if (id == -1) {
+                               db.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
+                       } else if (id == -4) {
+                               db.execute("UPDATE articles SET unread = 0");
+                       } else if (id < -10) {
+                               var label_id = -11-id;
+
+                               db.execute("UPDATE articles SET unread = 0 WHERE "+
+                                       "(SELECT COUNT(*) FROM article_labels WHERE "+
+                                       "article_labels.id = articles.id AND label_id = ?) > 0", [label_id]);
+                       }
+               }
+
+               update_local_feedlist_counters();
+
+       } catch (e) {
+               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);
+       }
+}