]>
git.wh0rd.org - tt-rss.git/blob - offline.js
1 var SCHEMA_VERSION
= 10;
3 var offline_mode
= false;
5 var localServer
= false;
7 var articles_synced
= 0;
8 var sync_in_progress
= false;
9 var sync_timer
= false;
11 function view_offline(id
, feed_id
) {
15 showArticleInHeadlines(id
);
17 db
.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id
]);
19 var rs
= db
.execute("SELECT * FROM articles WHERE id = ?", [id
]);
21 if (rs
.isValidRow()) {
23 var tmp
= "<div class=\"postReply\">";
25 tmp
+= "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" "+
26 "onmouseout=\"enable_resize(false)\">";
28 tmp
+= "<div class=\"postDate\">"+rs
.fieldByName("updated")+"</div>";
30 if (rs
.fieldByName("link") != "") {
31 tmp
+= "<div clear='both'><a target=\"_blank\" "+
32 "href=\"" + rs
.fieldByName("link") + "\">" +
33 rs
.fieldByName("title") + "</a></div>";
35 tmp
+= "<div clear='both'>" + rs
.fieldByName("title") + "</div>";
38 /* tmp += "<div style='float : right'> "+
39 "<img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
40 tmp += rs.fieldByName("tags");
43 /* tmp += "<div clear='both'>"+
44 "<a target=\"_blank\" "+
45 "href=\"" + rs.fieldByName("comments") + "\">" +
46 __("comments") + "</a></div>"; */
50 tmp
+= "<div class=\"postContent\">"
51 tmp
+= rs
.fieldByName("content");
57 update_local_feedlist_counters();
65 exception_error("view_offline", e
);
69 function viewfeed_offline(feed_id
, subop
, is_cat
, subop_param
, skip_history
, offset
) {
73 if (!offset
) offset
= 0;
76 _feed_cur_page
= parseInt(offset
);
77 if (_infscroll_request_sent
) {
82 _infscroll_disable
= 0;
85 if (getActiveFeedId() != feed_id
) {
88 _infscroll_disable
= 0;
91 loading_set_progress(100);
93 clean_feed_selections();
95 setActiveFeedId(feed_id
, is_cat
);
98 var feedr
= $("FEEDR-" + feed_id
);
99 if (feedr
&& !feedr
.className
.match("Selected")) {
100 feedr
.className
= feedr
.className
+ "Selected";
103 var feedr
= $("FCAT-" + feed_id
);
104 if (feedr
&& !feedr
.className
.match("Selected")) {
105 feedr
.className
= feedr
.className
+ "Selected";
109 if (subop
== "MarkAllRead") {
110 catchup_local_feed(feed_id
, is_cat
);
113 disableContainerChildren("headlinesToolbar", false);
114 Form
.enable("main_toolbar_form");
116 var f
= $("headlines-frame");
118 if (reply
.offset
== 0) {
119 debug("resetting headlines scrollTop");
129 feed_title
= get_local_category_title(feed_id
);
131 feed_title
= get_local_feed_title(feed_id
);
137 tmp
+= "<div id=\"headlinesContainer\">";
139 tmp
+= "<div class=\"headlinesSubToolbar\">";
140 tmp
+= "<div id=\"subtoolbar_ftitle\">";
149 var catchup_feed_link
= "javascript:catchupCurrentFeed()";
150 var catchup_sel_link
= "javascript:catchupSelection()";
155 if ($("content-frame")) {
156 sel_all_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
157 sel_unread_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
158 sel_none_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
159 sel_inv_link
= "javascript:invertHeadlineSelection()";
161 tog_unread_link
= "javascript:selectionToggleUnread()";
162 tog_marked_link
= "javascript:selectionToggleMarked()";
165 sel_all_link
= "javascript:cdmSelectArticles('all')";
166 sel_unread_link
= "javascript:cdmSelectArticles('unread')";
167 sel_none_link
= "javascript:cdmSelectArticles('none')";
168 sel_inv_link
= "javascript:invertHeadlineSelection()";
170 tog_unread_link
= "javascript:selectionToggleUnread(true)";
171 tog_marked_link
= "javascript:selectionToggleMarked(true)";
174 tmp
+= __('Select:')+
175 " <a href=\""+sel_all_link
+"\">"+__('All')+"</a>, "+
176 "<a href=\""+sel_unread_link
+"\">"+__('Unread')+"</a>, "+
177 "<a href=\""+sel_inv_link
+"\">"+__('Invert')+"</a>, "+
178 "<a href=\""+sel_none_link
+"\">"+__('None')+"</a>";
180 tmp
+= " ";
182 tmp
+= "<span onmouseover=\"enable_selection(false)\" "+
183 "onmouseout=\"enable_selection(true) \"" +
184 "onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">" +
185 __("Actions...") + " <img src=\"images/down_arrow.png\">" +
188 tmp
+= "<ul id=\"headlineActionsBody\" style=\"display : none\">";
190 tmp
+= "<li class=\"insensitive\">"+__('Selection toggle:')+"</li>"+
191 "<li onclick=\""+tog_unread_link
+"\"> "+__('Unread')+"</li>"+
192 "<li onclick=\""+tog_marked_link
+"\"> "+__('Starred')+"</li>"+
193 "<li class=\"insensitive\">"+__('Mark as read:')+"</li>"+
194 "<li onclick=\""+catchup_sel_link
+"\"> "+__('Selection')+"</li>";
196 tmp
+= "<li onclick=\""+catchup_feed_link
+"\"> "+__('Entire feed')+
203 tmp
+= "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
204 if ($("content-frame")) {
205 tmp
+= "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
212 var toolbar_form
= document
.forms
["main_toolbar_form"];
214 var limit
= toolbar_form
.limit
[toolbar_form
.limit
.selectedIndex
].value
;
215 var view_mode
= toolbar_form
.view_mode
[toolbar_form
.view_mode
.selectedIndex
].value
;
217 var limit_qpart
= "";
218 var strategy_qpart
= "";
220 var offset_qpart
= "";
223 limit_qpart
= "LIMIT " + limit
;
226 if (view_mode
== "all_articles") {
228 } else if (view_mode
== "adaptive") {
229 if (is_cat
&& get_local_category_unread(feed_id
) ||
230 get_local_feed_unread(feed_id
) > 0) {
231 mode_qpart
= "unread = 1";
236 } else if (view_mode
== "marked") {
237 mode_qpart
= "marked = 1";
238 } else if (view_mode
== "unread") {
239 mode_qpart
= "unread = 1";
244 var ext_tables_qpart
= "";
248 strategy_qpart
= "cat_id = " + feed_id
;
249 } else if (feed_id
== -2) {
250 strategy_qpart
= "article_labels.id = articles.id";
251 ext_tables_qpart
= ",article_labels";
253 } else if (feed_id
> 0) {
254 strategy_qpart
= "feed_id = " + feed_id
;
255 } else if (feed_id
== -1) {
256 strategy_qpart
= "marked = 1";
257 } else if (feed_id
== -4) {
258 strategy_qpart
= "1";
259 } else if (feed_id
< -10) {
260 var label_id
= -11 - feed_id
;
261 strategy_qpart
= "article_labels.id = articles.id AND label_id = " + label_id
;
262 ext_tables_qpart
= ",article_labels";
266 offset_qpart
= "OFFSET " + (offset
*30);
271 var query
= "SELECT *,feeds.title AS feed_title "+
272 "FROM articles,feeds,categories"+ext_tables_qpart
+" "+
274 "cat_id = categories.id AND " +
275 "feed_id = feeds.id AND " +
277 " AND " + mode_qpart
+
278 " ORDER BY updated DESC "+
282 var rs
= db
.execute(query
);
284 var line_num
= offset
*30;
286 var real_feed_id
= feed_id
;
288 while (rs
.isValidRow()) {
290 var id
= rs
.fieldByName("id");
291 var feed_id
= rs
.fieldByName("feed_id");
293 var entry_feed_title
= false;
295 if (real_feed_id
< 0 || is_cat
) {
296 entry_feed_title
= rs
.fieldByName("feed_title");
301 var row_class
= (line_num
% 2) ? "even" : "odd";
303 if (rs
.fieldByName("unread") == "1") {
304 row_class
+= "Unread";
307 var labels
= get_local_article_labels(id
);
309 var labels_str
= "<span id=\"HLLCTR-"+id
+"\">";
310 labels_str
+= format_article_labels(labels
, id
);
311 labels_str
+= "</span>";
313 if (rs
.fieldByName("marked") == "1") {
314 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
315 "src=\"images/mark_set.png\" class=\"markedPic\""+
316 "alt=\"Unstar article\" onclick='javascript:tMark("+id
+")'>";
318 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
319 "src=\"images/mark_unset.png\" class=\"markedPic\""+
320 "alt=\"Star article\" onclick='javascript:tMark("+id
+")'>";
323 var mouseover_attrs
= "onmouseover='postMouseIn($id)' "+
324 "onmouseout='postMouseOut($id)'";
326 var content_preview
= truncate_string(strip_tags(rs
.fieldByName("content")),
329 if ($("content-frame")) {
331 tmp
+= "<tr class='"+row_class
+"' id='RROW-"+id
+"' "+mouseover_attrs
+">";
333 tmp
+= "<td class='hlUpdPic'> </td>";
335 tmp
+= "<td class='hlSelectRow'>"+
336 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id
+"\"></td>";
338 tmp
+= "<td class='hlMarkedPic'>"+marked_pic
+"</td>";
340 tmp
+= "<td onclick='view("+id
+","+feed_id
+")' "+
341 "class='hlContent' valign='middle'>";
343 tmp
+= "<a target=\"_blank\" id=\"RTITLE-"+id
+"\" href=\"" +
344 rs
.fieldByName("link") + "\"" +
345 "onclick=\"return view("+id
+","+feed_id
+");\">"+
346 rs
.fieldByName("title");
348 tmp
+= "<span class=\"contentPreview\"> - "+content_preview
+"</span>";
354 if (entry_feed_title
) {
355 tmp
+= " <span class=\"hlFeed\">"+
356 "(<a href='javascript:viewfeed("+feed_id
+
357 ")'>"+entry_feed_title
+"</a>)</span>";
362 tmp
+= "<td class=\"hlUpdated\" onclick='view("+id
+","+feed_id
+")'>"+
363 "<nobr>"+rs
.fieldByName("updated").substring(0,16)+
371 if (rs
.fieldByName("unread") == "1") {
372 add_class
= "Unread";
375 tmp
+= "<div class=\"cdmArticle"+add_class
+"\" id=\"RROW-"+id
+"\" "+
376 mouseover_attrs
+"'>";
378 feed_icon_img
= "<img class=\"tinyFeedIcon\" src=\""+
379 getInitParam("icons_url")+"/"+feed_id
+".ico\" alt=\"\">";
380 cdm_feed_icon
= "<span style=\"cursor : pointer\" "+
381 "onclick=\"viewfeed("+feed_id
+")\">"+feed_icon_img
+"</span>";
383 tmp
+= "<div class=\"cdmHeader\">";
384 tmp
+= "<div class=\"articleUpdated\">"+
385 rs
.fieldByName("updated").substring(0,16)+
386 " "+cdm_feed_icon
+"</div>";
388 tmp
+= "<span id=\"RTITLE-"+id
+"\" class=\"titleWrap\">"+
389 "<a class=\"title\" onclick=\"javascript:toggleUnread("+id
+", 0)\""+
390 "target=\"_blank\" href=\""+rs
.fieldByName("link")+
391 "\">"+rs
.fieldByName("title")+"</a>";
395 if (entry_feed_title
) {
396 tmp
+= " (<a href='javascript:viewfeed("+feed_id
+
397 ")'>"+entry_feed_title
+"</a>)";
400 tmp
+= "</span></div>";
402 tmp
+= "<div class=\"cdmContent\" onclick=\"cdmClicked("+id
+")\""+
403 "id=\"CICD-"+id
+"\">";
404 tmp
+= rs
.fieldByName("content");
405 tmp
+= "<br clear='both'>"
408 tmp
+= "<div class=\"cdmFooter\"><span class='s0'>";
409 tmp
+= __("Select:")+
410 " <input type=\"checkbox\" "+
411 "onclick=\"toggleSelectRowById(this, 'RROW-"+id
+"')\" "+
412 "class=\"feedCheckBox\" id=\"RCHK-"+id
+"\">";
414 tmp
+= "</span><span class='s1'>"+marked_pic
+"</span> ";
416 /* tmp += "<span class='s1'>"+
417 "<img class='tagsPic' src='images/tag.png' alt='Tags' title='Tags'>"+
418 "<span id=\"ATSTR-"+id+"\">"+rs.fieldByName("tags")+"</span>"+
421 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
422 "href=\"javascript:toggleUnread("+id+")\">"+
433 if (line_num - offset*30 < 30) {
434 _infscroll_disable = 1;
442 if (line_num - offset*30 == 0) {
443 tmp += "<div class='whiteBox'>" +
444 __("No articles found to display.") +
447 tmp += "</div></div>";
451 var container = $("headlines-frame");
452 container.innerHTML = tmp;
454 var ids = getSelectedArticleIds2();
456 var container = $("headlinesList");
457 container.innerHTML = container.innerHTML + tmp;
459 for (var i = 0; i < ids.length; i++) {
460 markHeadline(ids[i]);
467 _infscroll_request_sent = 0;
470 exception_error("viewfeed_offline", e);
474 function render_offline_feedlist() {
476 var cats_enabled = getInitParam("enable_feed_cats") == "1";
478 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
480 var unread = get_local_feed_unread(-4);
482 global_unread = unread;
486 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
489 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
492 var unread = get_local_feed_unread(-1);
494 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
495 "images/mark_set.png");
500 tmp += "<li><hr/></li>";
504 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
507 var rs = db.execute("SELECT id,caption "+
511 while (rs.isValidRow()) {
512 var id = -11 - parseInt(rs.field(0));
513 var caption = rs.field(1);
514 var unread = get_local_feed_unread(id);
516 tmp += printFeedEntry(id, caption, "feed", unread,
527 tmp += "<li><hr/></li>";
530 /* var rs = db.execute("SELECT feeds.id,feeds.title,has_icon,COUNT(articles.id) "+
531 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
532 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
533 "ORDER BY feeds.title"); */
535 var order_by
= "feeds.title";
537 if (cats_enabled
) order_by
= "categories.title," + order_by
;
539 var rs
= db
.execute("SELECT "+
540 "feeds.id,feeds.title,has_icon,cat_id,collapsed "+
541 "FROM feeds,categories WHERE cat_id = categories.id "+
542 "ORDER BY "+order_by
);
546 while (rs
.isValidRow()) {
548 var id
= rs
.field(0);
549 var title
= rs
.field(1);
550 var has_icon
= rs
.field(2);
551 var unread
= get_local_feed_unread(id
);
552 var cat_id
= rs
.field(3);
553 var cat_hidden
= rs
.field(4);
555 if (cat_id
!= tmp_cat_id
&& cats_enabled
) {
556 if (tmp_cat_id
!= -1) {
559 tmp
+= printCategoryHeader(cat_id
, cat_hidden
, true);
566 icon
= getInitParam("icons_url") + "/" + id
+ ".ico";
571 var row_class
= "feed";
574 row_class
+= "Unread";
575 fctr_class
= "feedCtrHasUnread";
577 fctr_class
= "feedCtrNoUnread";
580 tmp
+= printFeedEntry(id
, title
, "feed", unread
, icon
);
593 render_feedlist(tmp
);
595 exception_error("render_offline_feedlist", e
);
599 function init_offline() {
602 store
.enabled
= true;
604 Element
.hide("dispSwitchPrompt");
605 Element
.hide("feedBrowserPrompt");
607 Element
.hide("topLinksOnline");
608 Element
.show("topLinksOffline");
610 var tb_form
= $("main_toolbar_form");
611 Element
.hide(tb_form
.update
);
613 var chooser
= $("quickMenuChooser");
614 chooser
.disabled
= true;
616 var rs
= db
.execute("SELECT key, value FROM init_params");
618 while (rs
.isValidRow()) {
619 init_params
[rs
.field(0)] = rs
.field(1);
625 var rs
= db
.execute("SELECT COUNT(*) FROM feeds");
629 if (rs
.isValidRow()) {
630 num_feeds
= rs
.field(0);
635 if (num_feeds
== 0) {
638 __("Data for offline browsing has not been downloaded yet."));
641 render_offline_feedlist();
643 window
.setTimeout("viewfeed(-4)", 50);
646 exception_error("init_offline", e
);
650 function offline_download_parse(stage
, transport
) {
652 if (transport
.responseXML
) {
654 if (!sync_in_progress
) return;
656 var sync_ok
= transport
.responseXML
.getElementsByTagName("sync-ok");
658 if (sync_ok
.length
> 0) {
659 for (var i
= 0; i
< sync_ok
.length
; i
++) {
660 var id
= sync_ok
[i
].getAttribute("id");
661 var unread
= sync_ok
[i
].getAttribute("unread");
662 var marked
= sync_ok
[i
].getAttribute("marked");
665 debug("synced offline info for id " + id
);
667 if (unread
!= undefined && marked
!= undefined) {
668 db
.execute("UPDATE articles SET "+
669 "unread = ?, marked = ? WHERE id = ?", [unread
, marked
, id
]);
672 db
.execute("UPDATE articles SET modified = '' WHERE id = ?", [id
]);
679 $("offlineModeSyncMsg").innerHTML
= __("Synchronizing feeds...");
681 var feeds
= transport
.responseXML
.getElementsByTagName("feed");
683 if (feeds
.length
> 0) {
684 db
.execute("DELETE FROM feeds");
687 for (var i
= 0; i
< feeds
.length
; i
++) {
688 var id
= feeds
[i
].getAttribute("id");
689 var has_icon
= feeds
[i
].getAttribute("has_icon");
690 var title
= feeds
[i
].firstChild
.nodeValue
;
691 var cat_id
= feeds
[i
].getAttribute("cat_id");
693 db
.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
695 [id
, title
, has_icon
, cat_id
]);
698 $("offlineModeSyncMsg").innerHTML
= __("Synchronizing categories...");
700 var cats
= transport
.responseXML
.getElementsByTagName("category");
702 if (feeds
.length
> 0) {
703 db
.execute("DELETE FROM categories");
706 for (var i
= 0; i
< cats
.length
; i
++) {
707 var id
= cats
[i
].getAttribute("id");
708 var collapsed
= cats
[i
].getAttribute("collapsed");
709 var title
= cats
[i
].firstChild
.nodeValue
;
711 db
.execute("INSERT INTO categories (id,title,collapsed)"+
713 [id
, title
, collapsed
]);
716 $("offlineModeSyncMsg").innerHTML
= __("Synchronizing labels...");
718 var labels
= transport
.responseXML
.getElementsByTagName("label");
720 if (labels
.length
> 0) {
721 db
.execute("DELETE FROM labels");
724 for (var i
= 0; i
< labels
.length
; i
++) {
725 var id
= labels
[i
].getAttribute("id");
726 var fg_color
= labels
[i
].getAttribute("fg_color");
727 var bg_color
= labels
[i
].getAttribute("bg_color");
728 var caption
= labels
[i
].firstChild
.nodeValue
;
730 db
.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
732 [id
, caption
, fg_color
, bg_color
]);
735 $("offlineModeSyncMsg").innerHTML
= __("Synchronizing articles...");
737 sync_timer
= window
.setTimeout("update_offline_data("+(stage
+1)+")", 2*1000);
740 var articles
= transport
.responseXML
.getElementsByTagName("article");
742 var limit
= transport
.responseXML
.getElementsByTagName("limit")[0];
745 limit
= limit
.getAttribute("value");
750 var articles_found
= 0;
752 for (var i
= 0; i
< articles
.length
; i
++) {
753 var a
= eval("("+articles
[i
].firstChild
.nodeValue
+")");
757 db
.execute("DELETE FROM articles WHERE id = ?", [a
.id
]);
759 db
.execute("INSERT INTO articles "+
760 "(id, feed_id, title, link, guid, updated, content, "+
761 "unread, marked, tags, comments) "+
762 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
763 [a
.id
, a
.feed_id
, a
.title
, a
.link
, a
.guid
, a
.updated
,
764 a
.content
, a
.unread
, a
.marked
, a
.tags
,
767 if (a
.labels
.length
> 0) {
768 for (var j
= 0; j
< a
.labels
.length
; j
++) {
769 label_local_add_article(a
.id
, a
.labels
[j
][0]);
776 debug("downloaded articles: " + articles_found
+ " limit: " + limit
);
778 articles_synced
+= articles_found
;
780 var msg
=__("Synchronizing articles (%d)...").replace("%d", articles_synced
);
782 $("offlineModeSyncMsg").innerHTML
= msg
;
784 var has_sync_data
= has_local_sync_data();
786 if (articles_found
>= limit
|| has_sync_data
) {
787 sync_timer
= window
.setTimeout("update_offline_data("+(stage
+1)+")",
789 debug("<b>update_offline_data: done " + stage
+ " HSD: " +
790 has_sync_data
+ "</b>");
792 window
.setTimeout("offlineDownloadStart()", 180*1000);
793 debug("update_offline_data: finished");
795 var pic
= $("offlineModePic");
798 pic
.src
= "images/offline.png";
800 var rs
= db
.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
803 if (rs
.isValidRow()) {
804 last_sync
= rs
.field(0).substring(0,16);
808 var msg
= __("Last sync: %s").replace("%s", last_sync
);
812 $("offlineModeSyncMsg").innerHTML
= msg
;
815 offlineSyncShowHideElems(false);
817 sync_in_progress
= false;
819 db
.execute("DELETE FROM articles WHERE "+
820 "updated < DATETIME('NOW', 'localtime', '-31 days')");
825 update_local_sync_data();
831 sync_in_progress
= false;
833 var pic
= $("offlineModePic");
836 pic
.src
= "images/offline.png";
837 var msg
= __("Last sync: Error receiving data.");
839 $("offlineModeSyncMsg").innerHTML
= msg
;
842 offlineSyncShowHideElems(false);
846 exception_error("offline_download_parse", e
);
850 function update_offline_data(stage
) {
853 if (!stage
) stage
= 0;
855 if (!db
|| offline_mode
|| getInitParam("offline_enabled") != "1") return;
857 var query
= "backend.php?op=rpc&subop=download";
859 var rs
= db
.execute("SELECT MAX(id), MIN(id) FROM articles");
861 if (rs
.isValidRow() && rs
.field(0)) {
862 var offline_dl_max_id
= rs
.field(0);
863 var offline_dl_min_id
= rs
.field(1);
865 query
= query
+ "&cidt=" + offline_dl_max_id
;
866 query
= query
+ "&cidb=" + offline_dl_min_id
;
868 if (stage
> 0) stage
= 1;
873 debug("update_offline_data: stage " + stage
);
875 query
= query
+ "&stage=" + stage
;
877 var to_sync
= prepare_local_sync_data();
880 to_sync
= "?sync=" + param_escape(to_sync
);
883 debug(query
+ "/" + to_sync
);
885 var pic
= $("offlineModePic");
888 pic
.src
= "images/offline-sync.gif";
889 if (articles_synced
== 0) {
890 $("offlineModeSyncMsg").innerHTML
= __("Synchronizing...");
894 offlineSyncShowHideElems(true);
895 offlineUpdateStore();
897 sync_in_progress
= true;
899 new Ajax
.Request(query
, {
901 onComplete: function(transport
) {
902 offline_download_parse(stage
, transport
);
906 exception_error("initiate_offline_download", e
);
910 function set_feedlist_counter(id
, ctr
, is_cat
) {
913 var feedctr
= $("FEEDCTR-" + id
);
914 var feedu
= $("FEEDU-" + id
);
915 var feedr
= $("FEEDR-" + id
);
918 var catctr
= $("FCATCTR-" + id
);
920 catctr
.innerHTML
= "(" + ctr
+ ")";
922 catctr
.className
= "catCtrHasUnread";
924 catctr
.className
= "catCtrNoUnread";
927 } else if (feedctr
&& feedu
&& feedr
) {
929 var row_needs_hl
= (ctr
> 0 && ctr
> parseInt(feedu
.innerHTML
));
931 feedu
.innerHTML
= ctr
;
934 feedctr
.className
= "feedCtrHasUnread";
935 if (!feedr
.className
.match("Unread")) {
936 var is_selected
= feedr
.className
.match("Selected");
938 feedr
.className
= feedr
.className
.replace("Selected", "");
939 feedr
.className
= feedr
.className
.replace("Unread", "");
941 feedr
.className
= feedr
.className
+ "Unread";
944 feedr
.className
= feedr
.className
+ "Selected";
950 new Effect
.Highlight(feedr
, {duration
: 1, startcolor
: "#fff7d5",
951 queue
: { position
:'end', scope
: 'EFQ-' + id
, limit
: 1 } } );
954 feedctr
.className
= "feedCtrNoUnread";
955 feedr
.className
= feedr
.className
.replace("Unread", "");
960 exception_error("set_feedlist_counter", e
);
964 function update_local_feedlist_counters() {
966 if (!offline_mode
|| !db
) return;
968 /* var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
969 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
970 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
971 "ORDER BY feeds.title"); */
973 var rs
= db
.execute("SELECT id FROM feeds "+
976 while (rs
.isValidRow()) {
977 var id
= rs
.field(0);
978 var ctr
= get_local_feed_unread(id
);
979 set_feedlist_counter(id
, ctr
, false);
985 var rs
= db
.execute("SELECT cat_id,SUM(unread) "+
986 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
988 while (rs
.isValidRow()) {
989 var id
= rs
.field(0);
990 var ctr
= rs
.field(1);
991 set_feedlist_counter(id
, ctr
, true);
997 set_feedlist_counter(-2, get_local_category_unread(-2), true);
999 set_feedlist_counter(-4, get_local_feed_unread(-4));
1000 set_feedlist_counter(-1, get_local_feed_unread(-1));
1002 var rs
= db
.execute("SELECT id FROM labels");
1004 while (rs
.isValidRow()) {
1005 var id
= -11 - rs
.field(0);
1006 var ctr
= get_local_feed_unread(id
);
1007 set_feedlist_counter(id
, ctr
, false);
1013 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
1015 global_unread
= get_local_feed_unread(-4);
1019 exception_error("update_local_feedlist_counters", e
);
1023 function get_local_feed_unread(id
) {
1028 rs
= db
.execute("SELECT SUM(unread) FROM articles");
1029 } else if (id
== -1) {
1030 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE marked = 1");
1031 } else if (id
> 0) {
1032 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE feed_id = ?", [id
]);
1033 } else if (id
< -10) {
1034 var label_id
= -11 - id
;
1035 rs
= db
.execute("SELECT SUM(unread) FROM articles,article_labels "+
1036 "WHERE article_labels.id = articles.id AND label_id = ?", [label_id
]);
1041 if (rs
.isValidRow()) {
1052 exception_error("get_local_feed_unread", e
);
1056 function enable_offline_reading() {
1059 if (db
&& getInitParam("offline_enabled") == "1") {
1060 init_local_sync_data();
1061 Element
.show("offlineModePic");
1062 offlineDownloadStart();
1066 exception_error("enable_offline_reading", e
);
1070 function init_gears() {
1073 if (window
.google
&& google
.gears
) {
1076 localServer
= google
.gears
.factory
.create("beta.localserver");
1081 store
= localServer
.createManagedStore("tt-rss");
1082 store
.manifestUrl
= "manifest.json.php";
1083 store
.enabled
= false;
1085 db
= google
.gears
.factory
.create('beta.database');
1088 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1090 var rs
= db
.execute("SELECT schema_version FROM version");
1094 if (rs
.isValidRow()) {
1095 version
= rs
.field(0);
1100 if (version
!= SCHEMA_VERSION
) {
1101 db
.execute("DROP TABLE IF EXISTS init_params");
1102 db
.execute("DROP TABLE IF EXISTS cache");
1103 db
.execute("DROP TABLE IF EXISTS feeds");
1104 db
.execute("DROP TABLE IF EXISTS categories");
1105 db
.execute("DROP TABLE IF EXISTS labels");
1106 db
.execute("DROP TABLE IF EXISTS article_labels");
1107 db
.execute("DROP TABLE IF EXISTS articles");
1108 db
.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
1109 db
.execute("DROP INDEX IF EXISTS articles_unread_idx");
1110 db
.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
1111 db
.execute("DROP INDEX IF EXISTS articles_id_idx");
1112 db
.execute("DROP INDEX IF EXISTS article_labels_id_idx");
1113 db
.execute("DROP TABLE IF EXISTS version");
1114 db
.execute("DROP TRIGGER IF EXISTS articles_update_unread");
1115 db
.execute("DROP TRIGGER IF EXISTS articles_update_marked");
1116 db
.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
1117 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1118 db
.execute("DROP TABLE IF EXISTS syncdata");
1119 db
.execute("INSERT INTO version (schema_version) VALUES (?)",
1123 db
.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
1125 db
.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
1126 db
.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
1127 db
.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
1128 db
.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
1129 db
.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
1130 db
.execute("CREATE TABLE IF NOT EXISTS articles (id integer, feed_id integer, title text, link text, guid text, updated timestamp, content text, tags text, unread integer, marked integer, added text, modified timestamp, comments text)");
1132 db
.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
1133 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
1134 db
.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
1135 db
.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
1136 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
1138 db
.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
1140 db
.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
1142 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
1143 "UPDATE OF unread ON articles "+
1145 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1146 "WHERE id = OLD.id AND "+
1147 "OLD.unread != NEW.unread;"+
1150 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
1151 "UPDATE OF marked ON articles "+
1153 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1154 "WHERE id = OLD.id;"+
1157 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1158 "DELETE ON articles "+
1160 "DELETE FROM article_labels WHERE id = OLD.id; "+
1168 exception_error("init_gears", e
);
1172 function offlineArticlesStored() {
1174 var rs
= db
.execute("SELECT COUNT(*) FROM articles");
1176 if (rs
.isValidRow()) {
1177 count
= rs
.field(0);
1185 function gotoOffline() {
1187 // debug("[Local store] currentVersion = " + store.currentVersion);
1190 if (offlineArticlesStored() == 0) {
1191 notify_error("You have to synchronize some articles before going into offline mode.");
1195 if (confirm(__("Switch Tiny Tiny RSS into offline mode?"))) {
1197 store
.enabled
= true;
1198 store
.checkForUpdate();
1200 notify_progress("Preparing offline mode...", true);
1202 var timerId
= window
.setInterval(function() {
1203 if (store
.currentVersion
) {
1204 window
.clearInterval(timerId
);
1205 debug("[Local store] sync complete: " + store
.currentVersion
);
1207 //window.location.href = "tt-rss.php";
1209 offlineDownloadStop();
1212 notify_info("Tiny Tiny RSS is in offline mode.");
1214 } else if (store
.updateStatus
== 3) {
1215 debug("[Local store] sync error: " + store
.lastErrorMessage
);
1216 notify_error(store
.lastErrorMessage
, true);
1221 function gotoOnline() {
1222 // 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?"))) {
1223 if (confirm(__("Tiny Tiny RSS will reload. Go online?"))) {
1224 //localServer.removeManagedStore("tt-rss");
1225 window
.location
.href
= "tt-rss.php";
1229 function local_collapse_cat(id
) {
1232 db
.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1236 exception_error("local_collapse_cat", e
);
1240 function get_local_category_title(id
) {
1243 var rs
= db
.execute("SELECT title FROM categories WHERE id = ?", [id
]);
1246 if (rs
.isValidRow()) {
1255 exception_error("get_local_category_title", e
);
1259 function get_local_category_unread(id
) {
1264 rs
= db
.execute("SELECT SUM(unread) FROM articles, feeds "+
1265 "WHERE feeds.id = feed_id AND cat_id = ?", [id
]);
1266 } else if (id
== -2) {
1267 rs
= db
.execute("SELECT SUM(unread) FROM article_labels, articles "+
1268 "where article_labels.id = articles.id");
1275 if (rs
.isValidRow()) {
1284 exception_error("get_local_category_unread", e
);
1288 function printCategoryHeader(cat_id
, hidden
, can_browse
) {
1290 if (hidden
== undefined) hidden
= false;
1291 if (can_browse
== undefined) can_browse
= false;
1293 var tmp_category
= get_local_category_title(cat_id
);
1296 var cat_unread
= get_local_category_unread(cat_id
);
1298 var holder_style
= "";
1302 holder_style
= "display:none;";
1306 var catctr_class
= (cat_unread
> 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1308 var browse_cat_link
= "";
1309 var inner_title_class
= "catTitleNL";
1312 browse_cat_link
= "onclick=\"javascript:viewCategory("+cat_id
+")\"";
1313 inner_title_class
= "catTitle";
1316 var cat_class
= "feedCat";
1318 tmp
+= "<li class=\""+cat_class
+"\" id=\"FCAT-"+cat_id
+"\">"+
1319 "<img onclick=\"toggleCollapseCat("+cat_id
+")\" class=\"catCollapse\""+
1320 " title=\""+__('Click to collapse category')+"\""+
1321 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class
+"\" "+
1322 " id=\"FCATN-"+cat_id
+"\" "+browse_cat_link
+
1323 "\">"+tmp_category
+"</span>";
1325 tmp
+= "<span id=\"FCAP-"+cat_id
+"\">";
1327 tmp
+= " <span id=\"FCATCTR-"+cat_id
+"\" "+
1328 "class=\""+catctr_class
+"\">("+cat_unread
+")</span> "+ellipsis
;
1332 tmp
+= "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id
+"\" "+
1333 "style='"+holder_style
+"'>";
1337 exception_error("printCategoryHeader", e
);
1341 function is_local_cat_collapsed(id
) {
1344 var rs
= db
.execute("SELECT collapsed FROM categories WHERE id = ?", [id
]);
1347 if (rs
.isValidRow()) {
1348 cat_hidden
= rs
.field(0);
1353 return cat_hidden
== "1";
1356 exception_error("is_local_cat_collapsed", e
);
1360 function get_local_article_labels(id
) {
1362 var rs
= db
.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1363 "FROM labels, article_labels "+
1364 "WHERE labels.id = label_id AND article_labels.id = ?", [id
]);
1366 var tmp
= new Array();
1368 while (rs
.isValidRow()) {
1369 var e
= new Array();
1384 exception_error("get_local_article_labels", e
);
1388 function label_local_add_article(id
, label_id
) {
1390 //debug("label_local_add_article " + id + " => " + label_id);
1392 var rs
= db
.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1393 "id = ? AND label_id = ?", [id
, label_id
]);
1394 var check
= rs
.field(0);
1396 if (rs
.isValidRow()) {
1397 var check
= rs
.field(0);
1402 db
.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1403 "(?,?)", [id
, label_id
]);
1407 exception_error("label_local_add_article", e
);
1411 function get_local_feed_title(id
) {
1414 var feed_title
= "Unknown feed: " + id
;
1417 var rs
= db
.execute("SELECT title FROM feeds WHERE id = ?", [id
]);
1419 if (rs
.isValidRow()) {
1420 feed_title
= rs
.field(0);
1424 } else if (id
== -1) {
1425 feed_title
= __("Starred articles");
1426 } else if (id
== -4) {
1427 feed_title
= __("All articles");
1428 } else if (id
< -10) {
1430 var label_id
= -11 - id
;
1432 var rs
= db
.execute("SELECT caption FROM labels WHERE id = ?", [label_id
]);
1434 if (rs
.isValidRow()) {
1435 feed_title
= rs
.field(0);
1444 exception_error("get_local_feed_title", e
);
1448 function format_article_labels(labels
, id
) {
1451 var labels_str
= "";
1453 if (!labels
) return "";
1455 for (var i
= 0; i
< labels
.length
; i
++) {
1458 labels_str
+= "<span class='hlLabelRef' "+
1459 "style='color : "+l
[2]+"; background-color : "+l
[3]+"'>"+l
[1]+"</span>";
1465 exception_error("format_article_labels", e
);
1469 function init_local_sync_data() {
1474 var rs
= db
.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1475 var has_last_online
= 0;
1477 if (rs
.isValidRow()) {
1478 has_last_online
= rs
.field(0);
1483 if (!has_last_online
) {
1484 db
.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1488 exception_error("init_local_sync_data", e
);
1493 function has_local_sync_data() {
1496 var rs
= db
.execute("SELECT id FROM articles "+
1497 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1502 if (rs
.isValidRow()) {
1511 exception_error("has_local_sync_data", e
);
1515 function prepare_local_sync_data() {
1517 var rs
= db
.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1519 var last_online
= "";
1521 if (rs
.isValidRow()) {
1522 last_online
= rs
.field(0);
1527 var rs
= db
.execute("SELECT id,unread,marked FROM articles "+
1528 "WHERE modified > ? LIMIT 200", [last_online
]);
1530 var tmp
= last_online
+ ";";
1534 while (rs
.isValidRow()) {
1535 var e
= new Array();
1537 tmp
= tmp
+ rs
.field(0) + "," + rs
.field(1) + "," + rs
.field(2) + ";";
1552 exception_error("prepare_local_sync_data", e
);
1556 function update_local_sync_data() {
1558 if (db
&& !offline_mode
) {
1560 var rs
= db
.execute("SELECT id FROM articles "+
1561 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1562 "key = 'last_online') LIMIT 1")
1566 if (rs
.isValidRow()) {
1572 /* no pending articles to sync */
1575 db
.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1576 "WHERE key = 'last_online'");
1581 exception_error("update_local_sync_data", e
);
1585 function catchup_local_feed(id
, is_cat
) {
1591 db
.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id
]);
1592 } else if (id
== -1) {
1593 db
.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1594 } else if (id
== -4) {
1595 db
.execute("UPDATE articles SET unread = 0");
1596 } else if (id
< -10) {
1597 var label_id
= -11-id
;
1599 db
.execute("UPDATE articles SET unread = 0 WHERE "+
1600 "(SELECT COUNT(*) FROM article_labels WHERE "+
1601 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id
]);
1605 update_local_feedlist_counters();
1608 exception_error("catchup_local_feed", e
);
1612 function toggleOfflineModeInfo() {
1614 var e
= $('offlineModeDrop');
1615 var p
= $('offlineModePic');
1617 if (Element
.visible(e
)) {
1624 exception_error("toggleOfflineModeInfo", e
);
1628 function offlineDownloadStart(stage
) {
1630 if (!stage
) stage
= 0;
1632 if (db
&& !sync_in_progress
&& getInitParam("offline_enabled") == "1") {
1633 window
.setTimeout("update_offline_data("+stage
+")", 100);
1636 exception_error("offlineDownloadStart", e
);
1640 function offlineDownloadStop() {
1642 if (db
&& sync_in_progress
&& getInitParam("offline_enabled") == "1") {
1644 sync_in_progress
= false;
1647 window
.clearTimeout(sync_timer
);
1651 var pic
= $("offlineModePic");
1654 pic
.src
= "images/offline.png";
1655 var msg
= __("Last sync: Cancelled.");
1656 articles_synced
= 0;
1657 $("offlineModeSyncMsg").innerHTML
= msg
;
1660 offlineSyncShowHideElems(false);
1664 exception_error("offlineDownloadStart", e
);
1668 function offlineClearData() {
1672 if (confirm(__("This will remove all offline data stored by Tiny Tiny RSS on this computer. Continue?"))) {
1674 notify_progress("Removing offline data...");
1676 localServer
.removeManagedStore("tt-rss");
1678 db
.execute("DELETE FROM articles");
1679 db
.execute("DELETE FROM article_labels");
1680 db
.execute("DELETE FROM labels");
1681 db
.execute("DELETE FROM feeds");
1682 db
.execute("DELETE FROM cache");
1684 notify_info("Local data removed.");
1688 exception_error("offlineClearData", e
);
1692 function offlineUpdateStore() {
1694 if (offline_mode
|| !store
) return;
1696 store
.checkForUpdate();
1698 var timerId
= window
.setInterval(function() {
1699 if (store
.currentVersion
) {
1700 window
.clearInterval(timerId
);
1701 debug("[Local store] sync complete: " + store
.currentVersion
);
1702 } else if (store
.updateStatus
== 3) {
1703 debug("[Local store] sync error: " + store
.lastErrorMessage
);
1707 exception_error("offlineUpdateStore", e
);
1711 function offlineSyncShowHideElems(syncing
) {
1714 var elems
= $$("div.hideWhenSyncing");
1716 for (var j
= 0; j
< elems
.length
; j
++) {
1718 Element
.hide(elems
[j
]);
1720 Element
.show(elems
[j
]);
1724 var elems
= $$("div.showWhenSyncing");
1726 for (var j
= 0; j
< elems
.length
; j
++) {
1728 Element
.show(elems
[j
]);
1730 Element
.hide(elems
[j
]);
1735 exception_error("offlineSyncShowHideElems", e
);
1739 function offlineConfirmModeChange() {
1742 if (db
&& getInitParam("offline_enabled") == "1" && offlineArticlesStored() > 0) {
1743 var ok
= confirm(__("Tiny Tiny RSS has trouble accessing its server. Would you like to go offline?"));
1751 exception_error("offlineConfirmModeChange", e
);