]>
git.wh0rd.org - tt-rss.git/blob - offline.js
1 var SCHEMA_VERSION
= 10;
3 var offline_mode
= false;
5 var localServer
= false;
8 function view_offline(id
, feed_id
) {
12 showArticleInHeadlines(id
);
14 db
.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id
]);
16 var rs
= db
.execute("SELECT * FROM articles WHERE id = ?", [id
]);
18 if (rs
.isValidRow()) {
20 var tmp
= "<div class=\"postReply\">";
22 tmp
+= "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" "+
23 "onmouseout=\"enable_resize(false)\">";
25 tmp
+= "<div class=\"postDate\">"+rs
.fieldByName("updated")+"</div>";
27 if (rs
.fieldByName("link") != "") {
28 tmp
+= "<div clear='both'><a target=\"_blank\" "+
29 "href=\"" + rs
.fieldByName("link") + "\">" +
30 rs
.fieldByName("title") + "</a></div>";
32 tmp
+= "<div clear='both'>" + rs
.fieldByName("title") + "</div>";
35 /* tmp += "<div style='float : right'> "+
36 "<img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
37 tmp += rs.fieldByName("tags");
40 /* tmp += "<div clear='both'>"+
41 "<a target=\"_blank\" "+
42 "href=\"" + rs.fieldByName("comments") + "\">" +
43 __("comments") + "</a></div>"; */
47 tmp
+= "<div class=\"postContent\">"
48 tmp
+= rs
.fieldByName("content");
54 update_local_feedlist_counters();
62 exception_error("view_offline", e
);
66 function viewfeed_offline(feed_id
, subop
, is_cat
, subop_param
, skip_history
, offset
) {
70 if (!offset
) offset
= 0;
73 _feed_cur_page
= parseInt(offset
);
74 if (_infscroll_request_sent
) {
79 _infscroll_disable
= 0;
82 if (getActiveFeedId() != feed_id
) {
85 _infscroll_disable
= 0;
88 loading_set_progress(100);
90 clean_feed_selections();
92 setActiveFeedId(feed_id
, is_cat
);
95 var feedr
= $("FEEDR-" + feed_id
);
96 if (feedr
&& !feedr
.className
.match("Selected")) {
97 feedr
.className
= feedr
.className
+ "Selected";
100 var feedr
= $("FCAT-" + feed_id
);
101 if (feedr
&& !feedr
.className
.match("Selected")) {
102 feedr
.className
= feedr
.className
+ "Selected";
106 if (subop
== "MarkAllRead") {
107 catchup_local_feed(feed_id
, is_cat
);
110 disableContainerChildren("headlinesToolbar", false);
111 Form
.enable("main_toolbar_form");
113 var f
= $("headlines-frame");
115 if (reply
.offset
== 0) {
116 debug("resetting headlines scrollTop");
126 feed_title
= get_local_category_title(feed_id
);
128 feed_title
= get_local_feed_title(feed_id
);
134 tmp
+= "<div id=\"headlinesContainer\">";
136 tmp
+= "<div class=\"headlinesSubToolbar\">";
137 tmp
+= "<div id=\"subtoolbar_ftitle\">";
146 if ($("content-frame")) {
147 sel_all_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
148 sel_unread_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
149 sel_none_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
150 sel_inv_link
= "javascript:invertHeadlineSelection()";
152 sel_all_link
= "javascript:cdmSelectArticles('all')";
153 sel_unread_link
= "javascript:cdmSelectArticles('unread')";
154 sel_none_link
= "javascript:cdmSelectArticles('none')";
155 sel_inv_link
= "javascript:invertHeadlineSelection()";
158 tmp
+= __('Select:')+
159 " <a href=\""+sel_all_link
+"\">"+__('All')+"</a>, "+
160 "<a href=\""+sel_unread_link
+"\">"+__('Unread')+"</a>, "+
161 "<a href=\""+sel_inv_link
+"\">"+__('Invert')+"</a>, "+
162 "<a href=\""+sel_none_link
+"\">"+__('None')+"</a>";
164 tmp
+= " ";
168 tmp
+= "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
169 if ($("content-frame")) {
170 tmp
+= "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
177 var toolbar_form
= document
.forms
["main_toolbar_form"];
179 var limit
= toolbar_form
.limit
[toolbar_form
.limit
.selectedIndex
].value
;
180 var view_mode
= toolbar_form
.view_mode
[toolbar_form
.view_mode
.selectedIndex
].value
;
182 var limit_qpart
= "";
183 var strategy_qpart
= "";
185 var offset_qpart
= "";
188 limit_qpart
= "LIMIT " + limit
;
191 if (view_mode
== "all_articles") {
193 } else if (view_mode
== "adaptive") {
194 if (is_cat
&& get_local_category_unread(feed_id
) ||
195 get_local_feed_unread(feed_id
) > 0) {
196 mode_qpart
= "unread = 1";
201 } else if (view_mode
== "marked") {
202 mode_qpart
= "marked = 1";
203 } else if (view_mode
== "unread") {
204 mode_qpart
= "unread = 1";
209 var ext_tables_qpart
= "";
213 strategy_qpart
= "cat_id = " + feed_id
;
214 } else if (feed_id
== -2) {
215 strategy_qpart
= "article_labels.id = articles.id";
216 ext_tables_qpart
= ",article_labels";
218 } else if (feed_id
> 0) {
219 strategy_qpart
= "feed_id = " + feed_id
;
220 } else if (feed_id
== -1) {
221 strategy_qpart
= "marked = 1";
222 } else if (feed_id
== -4) {
223 strategy_qpart
= "1";
224 } else if (feed_id
< -10) {
225 var label_id
= -11 - feed_id
;
226 strategy_qpart
= "article_labels.id = articles.id AND label_id = " + label_id
;
227 ext_tables_qpart
= ",article_labels";
231 offset_qpart
= "OFFSET " + (offset
*30);
236 var query
= "SELECT *,feeds.title AS feed_title "+
237 "FROM articles,feeds,categories"+ext_tables_qpart
+" "+
239 "cat_id = categories.id AND " +
240 "feed_id = feeds.id AND " +
242 " AND " + mode_qpart
+
243 " ORDER BY updated DESC "+
247 var rs
= db
.execute(query
);
249 var line_num
= offset
*30;
251 var real_feed_id
= feed_id
;
253 while (rs
.isValidRow()) {
255 var id
= rs
.fieldByName("id");
256 var feed_id
= rs
.fieldByName("feed_id");
258 var entry_feed_title
= false;
260 if (real_feed_id
< 0 || is_cat
) {
261 entry_feed_title
= rs
.fieldByName("feed_title");
266 var row_class
= (line_num
% 2) ? "even" : "odd";
268 if (rs
.fieldByName("unread") == "1") {
269 row_class
+= "Unread";
272 var labels
= get_local_article_labels(id
);
274 var labels_str
= "<span id=\"HLLCTR-"+id
+"\">";
275 labels_str
+= format_article_labels(labels
, id
);
276 labels_str
+= "</span>";
278 if (rs
.fieldByName("marked") == "1") {
279 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
280 "src=\"images/mark_set.png\" class=\"markedPic\""+
281 "alt=\"Unstar article\" onclick='javascript:tMark("+id
+")'>";
283 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
284 "src=\"images/mark_unset.png\" class=\"markedPic\""+
285 "alt=\"Star article\" onclick='javascript:tMark("+id
+")'>";
288 var mouseover_attrs
= "onmouseover='postMouseIn($id)' "+
289 "onmouseout='postMouseOut($id)'";
291 var content_preview
= truncate_string(strip_tags(rs
.fieldByName("content")),
294 if ($("content-frame")) {
296 tmp
+= "<tr class='"+row_class
+"' id='RROW-"+id
+"' "+mouseover_attrs
+">";
298 tmp
+= "<td class='hlUpdPic'> </td>";
300 tmp
+= "<td class='hlSelectRow'>"+
301 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id
+"\"></td>";
303 tmp
+= "<td class='hlMarkedPic'>"+marked_pic
+"</td>";
305 tmp
+= "<td onclick='view("+id
+","+feed_id
+")' "+
306 "class='hlContent' valign='middle'>";
308 tmp
+= "<a target=\"_blank\" id=\"RTITLE-"+id
+"\" href=\"" +
309 rs
.fieldByName("link") + "\"" +
310 "onclick=\"return view("+id
+","+feed_id
+");\">"+
311 rs
.fieldByName("title");
313 tmp
+= "<span class=\"contentPreview\"> - "+content_preview
+"</span>";
319 if (entry_feed_title
) {
320 tmp
+= " <span class=\"hlFeed\">"+
321 "(<a href='javascript:viewfeed("+feed_id
+
322 ")'>"+entry_feed_title
+"</a>)</span>";
327 tmp
+= "<td class=\"hlUpdated\" onclick='view("+id
+","+feed_id
+")'>"+
328 "<nobr>"+rs
.fieldByName("updated").substring(0,16)+
336 if (rs
.fieldByName("unread") == "1") {
337 add_class
= "Unread";
340 tmp
+= "<div class=\"cdmArticle"+add_class
+"\" id=\"RROW-"+id
+"\" "+
341 mouseover_attrs
+"'>";
343 feed_icon_img
= "<img class=\"tinyFeedIcon\" src=\""+
344 getInitParam("icons_url")+"/"+feed_id
+".ico\" alt=\"\">";
345 cdm_feed_icon
= "<span style=\"cursor : pointer\" "+
346 "onclick=\"viewfeed("+feed_id
+")\">"+feed_icon_img
+"</span>";
348 tmp
+= "<div class=\"cdmHeader\">";
349 tmp
+= "<div class=\"articleUpdated\">"+
350 rs
.fieldByName("updated").substring(0,16)+
351 " "+cdm_feed_icon
+"</div>";
353 tmp
+= "<span id=\"RTITLE-"+id
+"\" class=\"titleWrap\">"+
354 "<a class=\"title\" onclick=\"javascript:toggleUnread("+id
+", 0)\""+
355 "target=\"_blank\" href=\""+rs
.fieldByName("link")+
356 "\">"+rs
.fieldByName("title")+"</a>";
360 if (entry_feed_title
) {
361 tmp
+= " (<a href='javascript:viewfeed("+feed_id
+
362 ")'>"+entry_feed_title
+"</a>)";
365 tmp
+= "</span></div>";
367 tmp
+= "<div class=\"cdmContent\" onclick=\"cdmClicked("+id
+")\""+
368 "id=\"CICD-"+id
+"\">";
369 tmp
+= rs
.fieldByName("content");
370 tmp
+= "<br clear='both'>"
373 tmp
+= "<div class=\"cdmFooter\"><span class='s0'>";
374 tmp
+= __("Select:")+
375 " <input type=\"checkbox\" "+
376 "onclick=\"toggleSelectRowById(this, 'RROW-"+id
+"')\" "+
377 "class=\"feedCheckBox\" id=\"RCHK-"+id
+"\">";
379 tmp
+= "</span><span class='s1'>"+marked_pic
+"</span> ";
381 /* tmp += "<span class='s1'>"+
382 "<img class='tagsPic' src='images/tag.png' alt='Tags' title='Tags'>"+
383 "<span id=\"ATSTR-"+id+"\">"+rs.fieldByName("tags")+"</span>"+
386 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
387 "href=\"javascript:toggleUnread("+id+")\">"+
398 if (line_num - offset*30 < 30) {
399 _infscroll_disable = 1;
407 if (line_num - offset*30 == 0) {
408 tmp += "<div class='whiteBox'>" +
409 __("No articles found to display.") +
412 tmp += "</div></div>";
416 var container = $("headlines-frame");
417 container.innerHTML = tmp;
419 var ids = getSelectedArticleIds2();
421 var container = $("headlinesList");
422 container.innerHTML = container.innerHTML + tmp;
424 for (var i = 0; i < ids.length; i++) {
425 markHeadline(ids[i]);
432 _infscroll_request_sent = 0;
435 exception_error("viewfeed_offline", e);
439 function render_offline_feedlist() {
441 var cats_enabled = getInitParam("enable_feed_cats") == "1";
443 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
445 var unread = get_local_feed_unread(-4);
447 global_unread = unread;
451 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
454 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
457 var unread = get_local_feed_unread(-1);
459 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
460 "images/mark_set.png");
465 tmp += "<li><hr/></li>";
469 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
472 var rs = db.execute("SELECT id,caption "+
476 while (rs.isValidRow()) {
477 var id = -11 - parseInt(rs.field(0));
478 var caption = rs.field(1);
479 var unread = get_local_feed_unread(id);
481 tmp += printFeedEntry(id, caption, "feed", unread,
492 tmp += "<li><hr/></li>";
495 /* var rs = db.execute("SELECT feeds.id,feeds.title,has_icon,COUNT(articles.id) "+
496 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
497 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
498 "ORDER BY feeds.title"); */
500 var order_by
= "feeds.title";
502 if (cats_enabled
) order_by
= "categories.title," + order_by
;
504 var rs
= db
.execute("SELECT "+
505 "feeds.id,feeds.title,has_icon,cat_id,collapsed "+
506 "FROM feeds,categories WHERE cat_id = categories.id "+
507 "ORDER BY "+order_by
);
511 while (rs
.isValidRow()) {
513 var id
= rs
.field(0);
514 var title
= rs
.field(1);
515 var has_icon
= rs
.field(2);
516 var unread
= get_local_feed_unread(id
);
517 var cat_id
= rs
.field(3);
518 var cat_hidden
= rs
.field(4);
520 if (cat_id
!= tmp_cat_id
&& cats_enabled
) {
521 if (tmp_cat_id
!= -1) {
524 tmp
+= printCategoryHeader(cat_id
, cat_hidden
, true);
531 icon
= getInitParam("icons_url") + "/" + id
+ ".ico";
536 var row_class
= "feed";
539 row_class
+= "Unread";
540 fctr_class
= "feedCtrHasUnread";
542 fctr_class
= "feedCtrNoUnread";
545 tmp
+= printFeedEntry(id
, title
, "feed", unread
, icon
);
558 render_feedlist(tmp
);
560 exception_error("render_offline_feedlist", e
);
564 function init_offline() {
568 Element
.hide("dispSwitchPrompt");
569 Element
.hide("feedBrowserPrompt");
571 Element
.hide("topLinksOnline");
572 Element
.show("topLinksOffline");
574 var tb_form
= $("main_toolbar_form");
575 Element
.hide(tb_form
.update
);
577 var chooser
= $("quickMenuChooser");
578 chooser
.disabled
= true;
580 var rs
= db
.execute("SELECT key, value FROM init_params");
582 while (rs
.isValidRow()) {
583 init_params
[rs
.field(0)] = rs
.field(1);
589 var rs
= db
.execute("SELECT COUNT(*) FROM feeds");
593 if (rs
.isValidRow()) {
594 num_feeds
= rs
.field(0);
599 if (num_feeds
== 0) {
602 __("Data for offline browsing has not been downloaded yet."));
605 render_offline_feedlist();
607 window
.setTimeout("viewfeed(-4)", 50);
610 exception_error("init_offline", e
);
614 function offline_download_parse(stage
, transport
) {
616 if (transport
.responseXML
) {
618 var sync_ok
= transport
.responseXML
.getElementsByTagName("sync-ok");
620 if (sync_ok
.length
> 0) {
621 for (var i
= 0; i
< sync_ok
.length
; i
++) {
622 var id
= sync_ok
[i
].getAttribute("id");
624 debug("synced offline info for id " + id
);
625 db
.execute("UPDATE articles SET modified = '' WHERE id = ?", [id
]);
632 var feeds
= transport
.responseXML
.getElementsByTagName("feed");
634 if (feeds
.length
> 0) {
635 db
.execute("DELETE FROM feeds");
638 for (var i
= 0; i
< feeds
.length
; i
++) {
639 var id
= feeds
[i
].getAttribute("id");
640 var has_icon
= feeds
[i
].getAttribute("has_icon");
641 var title
= feeds
[i
].firstChild
.nodeValue
;
642 var cat_id
= feeds
[i
].getAttribute("cat_id");
644 db
.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
646 [id
, title
, has_icon
, cat_id
]);
649 var cats
= transport
.responseXML
.getElementsByTagName("category");
651 if (feeds
.length
> 0) {
652 db
.execute("DELETE FROM categories");
655 for (var i
= 0; i
< cats
.length
; i
++) {
656 var id
= cats
[i
].getAttribute("id");
657 var collapsed
= cats
[i
].getAttribute("collapsed");
658 var title
= cats
[i
].firstChild
.nodeValue
;
660 db
.execute("INSERT INTO categories (id,title,collapsed)"+
662 [id
, title
, collapsed
]);
665 var labels
= transport
.responseXML
.getElementsByTagName("label");
667 if (labels
.length
> 0) {
668 db
.execute("DELETE FROM labels");
671 for (var i
= 0; i
< labels
.length
; i
++) {
672 var id
= labels
[i
].getAttribute("id");
673 var fg_color
= labels
[i
].getAttribute("fg_color");
674 var bg_color
= labels
[i
].getAttribute("bg_color");
675 var caption
= labels
[i
].firstChild
.nodeValue
;
677 db
.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
679 [id
, caption
, fg_color
, bg_color
]);
682 window
.setTimeout("update_offline_data("+(stage
+1)+")", 5*1000);
685 var articles
= transport
.responseXML
.getElementsByTagName("article");
687 var limit
= transport
.responseXML
.getElementsByTagName("limit")[0];
690 limit
= limit
.getAttribute("value");
695 var articles_found
= 0;
697 for (var i
= 0; i
< articles
.length
; i
++) {
698 var a
= eval("("+articles
[i
].firstChild
.nodeValue
+")");
702 db
.execute("DELETE FROM articles WHERE id = ?", [a
.id
]);
704 db
.execute("INSERT INTO articles "+
705 "(id, feed_id, title, link, guid, updated, content, "+
706 "unread, marked, tags, comments) "+
707 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
708 [a
.id
, a
.feed_id
, a
.title
, a
.link
, a
.guid
, a
.updated
,
709 a
.content
, a
.unread
, a
.marked
, a
.tags
,
712 if (a
.labels
.length
> 0) {
713 for (var j
= 0; j
< a
.labels
.length
; j
++) {
714 label_local_add_article(a
.id
, a
.labels
[j
][0]);
721 debug("downloaded articles: " + articles_found
+ " limit: " + limit
);
723 var has_sync_data
= has_local_sync_data();
725 if (articles_found
>= limit
|| has_sync_data
) {
726 window
.setTimeout("update_offline_data("+(stage
+1)+")", 5*1000);
727 debug("<b>update_offline_data: done " + stage
+ " HSD: " +
728 has_sync_data
+ "</b>");
730 window
.setTimeout("update_offline_data(0)", 180*1000);
731 debug("update_offline_data: finished");
733 var pic
= $("restartOfflinePic");
736 pic
.src
= "images/offline.png";
737 pic
.title
= __("Restart in offline mode");
740 db
.execute("DELETE FROM articles WHERE "+
741 "updated < DATETIME('NOW', 'localtime', '-31 days')");
746 update_local_sync_data();
753 exception_error("offline_download_parse", e
);
757 function update_offline_data(stage
) {
760 if (!stage
) stage
= 0;
761 if (!db
|| offline_mode
|| getInitParam("offline_enabled") != "1") return;
763 // notify_progress("Updating offline data... (" + stage +")", true);
765 var query
= "backend.php?op=rpc&subop=download";
767 var rs
= db
.execute("SELECT MAX(id), MIN(id) FROM articles");
769 if (rs
.isValidRow() && rs
.field(0)) {
770 var offline_dl_max_id
= rs
.field(0);
771 var offline_dl_min_id
= rs
.field(1);
773 query
= query
+ "&cidt=" + offline_dl_max_id
;
774 query
= query
+ "&cidb=" + offline_dl_min_id
;
781 debug("update_offline_data: stage " + stage
);
783 query
= query
+ "&stage=" + stage
;
785 var to_sync
= prepare_local_sync_data();
788 to_sync
= "?sync=" + param_escape(to_sync
);
791 debug(query
+ "/" + to_sync
);
793 var pic
= $("restartOfflinePic");
796 pic
.src
= "images/offline-sync.gif";
797 pic
.title
= __("Synchronizing offline data...");
800 new Ajax
.Request(query
, {
802 onComplete: function(transport
) {
803 offline_download_parse(stage
, transport
);
807 exception_error("initiate_offline_download", e
);
811 function set_feedlist_counter(id
, ctr
, is_cat
) {
814 var feedctr
= $("FEEDCTR-" + id
);
815 var feedu
= $("FEEDU-" + id
);
816 var feedr
= $("FEEDR-" + id
);
819 var catctr
= $("FCATCTR-" + id
);
821 catctr
.innerHTML
= "(" + ctr
+ ")";
823 catctr
.className
= "catCtrHasUnread";
825 catctr
.className
= "catCtrNoUnread";
828 } else if (feedctr
&& feedu
&& feedr
) {
830 var row_needs_hl
= (ctr
> 0 && ctr
> parseInt(feedu
.innerHTML
));
832 feedu
.innerHTML
= ctr
;
835 feedctr
.className
= "feedCtrHasUnread";
836 if (!feedr
.className
.match("Unread")) {
837 var is_selected
= feedr
.className
.match("Selected");
839 feedr
.className
= feedr
.className
.replace("Selected", "");
840 feedr
.className
= feedr
.className
.replace("Unread", "");
842 feedr
.className
= feedr
.className
+ "Unread";
845 feedr
.className
= feedr
.className
+ "Selected";
851 new Effect
.Highlight(feedr
, {duration
: 1, startcolor
: "#fff7d5",
852 queue
: { position
:'end', scope
: 'EFQ-' + id
, limit
: 1 } } );
855 feedctr
.className
= "feedCtrNoUnread";
856 feedr
.className
= feedr
.className
.replace("Unread", "");
861 exception_error("set_feedlist_counter", e
);
865 function update_local_feedlist_counters() {
867 if (!offline_mode
|| !db
) return;
869 /* var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
870 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
871 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
872 "ORDER BY feeds.title"); */
874 var rs
= db
.execute("SELECT id FROM feeds "+
877 while (rs
.isValidRow()) {
878 var id
= rs
.field(0);
879 var ctr
= get_local_feed_unread(id
);
880 set_feedlist_counter(id
, ctr
, false);
886 var rs
= db
.execute("SELECT cat_id,SUM(unread) "+
887 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
889 while (rs
.isValidRow()) {
890 var id
= rs
.field(0);
891 var ctr
= rs
.field(1);
892 set_feedlist_counter(id
, ctr
, true);
898 set_feedlist_counter(-2, get_local_category_unread(-2), true);
900 set_feedlist_counter(-4, get_local_feed_unread(-4));
901 set_feedlist_counter(-1, get_local_feed_unread(-1));
903 var rs
= db
.execute("SELECT id FROM labels");
905 while (rs
.isValidRow()) {
906 var id
= -11 - rs
.field(0);
907 var ctr
= get_local_feed_unread(id
);
908 set_feedlist_counter(id
, ctr
, false);
914 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
916 global_unread
= get_local_feed_unread(-4);
920 exception_error("update_local_feedlist_counters", e
);
924 function get_local_feed_unread(id
) {
929 rs
= db
.execute("SELECT SUM(unread) FROM articles");
930 } else if (id
== -1) {
931 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE marked = 1");
933 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE feed_id = ?", [id
]);
934 } else if (id
< -10) {
935 var label_id
= -11 - id
;
936 rs
= db
.execute("SELECT SUM(unread) FROM articles,article_labels "+
937 "WHERE article_labels.id = articles.id AND label_id = ?", [label_id
]);
942 if (rs
.isValidRow()) {
953 exception_error("get_local_feed_unread", e
);
957 function enable_offline_reading() {
960 if (db
&& getInitParam("offline_enabled") == "1") {
961 init_local_sync_data();
962 Element
.show("restartOfflinePic");
963 window
.setTimeout("update_offline_data(0)", 100);
967 exception_error("enable_offline_reading", e
);
971 function init_gears() {
974 if (window
.google
&& google
.gears
) {
975 localServer
= google
.gears
.factory
.create("beta.localserver");
976 store
= localServer
.createManagedStore("tt-rss");
977 db
= google
.gears
.factory
.create('beta.database');
980 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
982 var rs
= db
.execute("SELECT schema_version FROM version");
986 if (rs
.isValidRow()) {
987 version
= rs
.field(0);
992 if (version
!= SCHEMA_VERSION
) {
993 db
.execute("DROP TABLE IF EXISTS init_params");
994 db
.execute("DROP TABLE IF EXISTS cache");
995 db
.execute("DROP TABLE IF EXISTS feeds");
996 db
.execute("DROP TABLE IF EXISTS categories");
997 db
.execute("DROP TABLE IF EXISTS labels");
998 db
.execute("DROP TABLE IF EXISTS article_labels");
999 db
.execute("DROP TABLE IF EXISTS articles");
1000 db
.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
1001 db
.execute("DROP INDEX IF EXISTS articles_unread_idx");
1002 db
.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
1003 db
.execute("DROP INDEX IF EXISTS articles_id_idx");
1004 db
.execute("DROP INDEX IF EXISTS article_labels_id_idx");
1005 db
.execute("DROP TABLE IF EXISTS version");
1006 db
.execute("DROP TRIGGER IF EXISTS articles_update_unread");
1007 db
.execute("DROP TRIGGER IF EXISTS articles_update_marked");
1008 db
.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
1009 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1010 db
.execute("DROP TABLE IF EXISTS syncdata");
1011 db
.execute("INSERT INTO version (schema_version) VALUES (?)",
1015 db
.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
1017 db
.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
1018 db
.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
1019 db
.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
1020 db
.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
1021 db
.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
1022 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)");
1024 db
.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
1025 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
1026 db
.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
1027 db
.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
1028 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
1030 db
.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
1032 db
.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
1034 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
1035 "UPDATE OF unread ON articles "+
1037 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1038 "WHERE id = OLD.id AND "+
1039 "OLD.unread != NEW.unread;"+
1042 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
1043 "UPDATE OF marked ON articles "+
1045 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1046 "WHERE id = OLD.id;"+
1049 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1050 "DELETE ON articles "+
1052 "DELETE FROM article_labels WHERE id = OLD.id; "+
1060 exception_error("init_gears", e
);
1064 function gotoOffline() {
1065 window
.location
.href
= "tt-rss.php?offline=1";
1068 function gotoOnline() {
1069 window
.location
.href
= "tt-rss.php";
1072 function local_collapse_cat(id
) {
1075 db
.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1079 exception_error("local_collapse_cat", e
);
1083 function get_local_category_title(id
) {
1086 var rs
= db
.execute("SELECT title FROM categories WHERE id = ?", [id
]);
1089 if (rs
.isValidRow()) {
1098 exception_error("get_local_category_title", e
);
1102 function get_local_category_unread(id
) {
1107 rs
= db
.execute("SELECT SUM(unread) FROM articles, feeds "+
1108 "WHERE feeds.id = feed_id AND cat_id = ?", [id
]);
1109 } else if (id
== -2) {
1110 rs
= db
.execute("SELECT SUM(unread) FROM article_labels, articles "+
1111 "where article_labels.id = articles.id");
1118 if (rs
.isValidRow()) {
1127 exception_error("get_local_category_unread", e
);
1131 function printCategoryHeader(cat_id
, hidden
, can_browse
) {
1133 if (hidden
== undefined) hidden
= false;
1134 if (can_browse
== undefined) can_browse
= false;
1136 var tmp_category
= get_local_category_title(cat_id
);
1139 var cat_unread
= get_local_category_unread(cat_id
);
1141 var holder_style
= "";
1145 holder_style
= "display:none;";
1149 var catctr_class
= (cat_unread
> 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1151 var browse_cat_link
= "";
1152 var inner_title_class
= "catTitleNL";
1155 browse_cat_link
= "onclick=\"javascript:viewCategory("+cat_id
+")\"";
1156 inner_title_class
= "catTitle";
1159 var cat_class
= "feedCat";
1161 tmp
+= "<li class=\""+cat_class
+"\" id=\"FCAT-"+cat_id
+"\">"+
1162 "<img onclick=\"toggleCollapseCat("+cat_id
+")\" class=\"catCollapse\""+
1163 " title=\""+__('Click to collapse category')+"\""+
1164 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class
+"\" "+
1165 " id=\"FCATN-"+cat_id
+"\" "+browse_cat_link
+
1166 "\">"+tmp_category
+"</span>";
1168 tmp
+= "<span id=\"FCAP-"+cat_id
+"\">";
1170 tmp
+= " <span id=\"FCATCTR-"+cat_id
+"\" "+
1171 "class=\""+catctr_class
+"\">("+cat_unread
+")</span> "+ellipsis
;
1175 tmp
+= "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id
+"\" "+
1176 "style='"+holder_style
+"'>";
1180 exception_error("printCategoryHeader", e
);
1184 function is_local_cat_collapsed(id
) {
1187 var rs
= db
.execute("SELECT collapsed FROM categories WHERE id = ?", [id
]);
1190 if (rs
.isValidRow()) {
1191 cat_hidden
= rs
.field(0);
1196 return cat_hidden
== "1";
1199 exception_error("is_local_cat_collapsed", e
);
1203 function get_local_article_labels(id
) {
1205 var rs
= db
.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1206 "FROM labels, article_labels "+
1207 "WHERE labels.id = label_id AND article_labels.id = ?", [id
]);
1209 var tmp
= new Array();
1211 while (rs
.isValidRow()) {
1212 var e
= new Array();
1227 exception_error("get_local_article_labels", e
);
1231 function label_local_add_article(id
, label_id
) {
1233 //debug("label_local_add_article " + id + " => " + label_id);
1235 var rs
= db
.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1236 "id = ? AND label_id = ?", [id
, label_id
]);
1237 var check
= rs
.field(0);
1239 if (rs
.isValidRow()) {
1240 var check
= rs
.field(0);
1245 db
.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1246 "(?,?)", [id
, label_id
]);
1250 exception_error("label_local_add_article", e
);
1254 function get_local_feed_title(id
) {
1257 var feed_title
= "Unknown feed: " + id
;
1260 var rs
= db
.execute("SELECT title FROM feeds WHERE id = ?", [id
]);
1262 if (rs
.isValidRow()) {
1263 feed_title
= rs
.field(0);
1267 } else if (id
== -1) {
1268 feed_title
= __("Starred articles");
1269 } else if (id
== -4) {
1270 feed_title
= __("All articles");
1271 } else if (id
< -10) {
1273 var label_id
= -11 - id
;
1275 var rs
= db
.execute("SELECT caption FROM labels WHERE id = ?", [label_id
]);
1277 if (rs
.isValidRow()) {
1278 feed_title
= rs
.field(0);
1287 exception_error("get_local_feed_title", e
);
1291 function format_article_labels(labels
, id
) {
1294 var labels_str
= "";
1296 if (!labels
) return "";
1298 for (var i
= 0; i
< labels
.length
; i
++) {
1301 labels_str
+= "<span class='hlLabelRef' "+
1302 "style='color : "+l
[2]+"; background-color : "+l
[3]+"'>"+l
[1]+"</span>";
1308 exception_error("format_article_labels", e
);
1312 function init_local_sync_data() {
1317 var rs
= db
.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1318 var has_last_online
= 0;
1320 if (rs
.isValidRow()) {
1321 has_last_online
= rs
.field(0);
1326 if (!has_last_online
) {
1327 db
.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1331 exception_error("init_local_sync_data", e
);
1336 function has_local_sync_data() {
1339 var rs
= db
.execute("SELECT id FROM articles "+
1340 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1345 if (rs
.isValidRow()) {
1354 exception_error("has_local_sync_data", e
);
1358 function prepare_local_sync_data() {
1360 var rs
= db
.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1362 var last_online
= "";
1364 if (rs
.isValidRow()) {
1365 last_online
= rs
.field(0);
1370 var rs
= db
.execute("SELECT id,unread,marked FROM articles "+
1371 "WHERE modified > ? LIMIT 200", [last_online
]);
1373 var tmp
= last_online
+ ";";
1377 while (rs
.isValidRow()) {
1378 var e
= new Array();
1380 tmp
= tmp
+ rs
.field(0) + "," + rs
.field(1) + "," + rs
.field(2) + ";";
1395 exception_error("prepare_local_sync_data", e
);
1399 function update_local_sync_data() {
1401 if (db
&& !offline_mode
) {
1403 var rs
= db
.execute("SELECT id FROM articles "+
1404 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1405 "key = 'last_online') LIMIT 1")
1409 if (rs
.isValidRow()) {
1415 /* no pending articles to sync */
1418 db
.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1419 "WHERE key = 'last_online'");
1424 exception_error("update_local_sync_data", e
);
1428 function catchup_local_feed(id
, is_cat
) {
1434 db
.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id
]);
1435 } else if (id
== -1) {
1436 db
.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1437 } else if (id
== -4) {
1438 db
.execute("UPDATE articles SET unread = 0");
1439 } else if (id
< -10) {
1440 var label_id
= -11-id
;
1442 db
.execute("UPDATE articles SET unread = 0 WHERE "+
1443 "(SELECT COUNT(*) FROM article_labels WHERE "+
1444 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id
]);
1448 update_local_feedlist_counters();
1451 exception_error("catchup_local_feed", e
);