]>
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
= document
.getElementById("FEEDR-" + feed_id
);
96 if (feedr
&& !feedr
.className
.match("Selected")) {
97 feedr
.className
= feedr
.className
+ "Selected";
100 var feedr
= document
.getElementById("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
= document
.getElementById("headlines-frame");
115 if (reply
.offset
== 0) {
116 debug("resetting headlines scrollTop");
124 var feed_title
= get_local_feed_title(feed_id
);
129 tmp
+= "<div id=\"headlinesContainer\">";
131 tmp
+= "<div class=\"headlinesSubToolbar\">";
132 tmp
+= "<div id=\"subtoolbar_ftitle\">";
141 if (document
.getElementById("content-frame")) {
142 sel_all_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
143 sel_unread_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
144 sel_none_link
= "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
145 sel_inv_link
= "javascript:invertHeadlineSelection()";
147 sel_all_link
= "javascript:cdmSelectArticles('all')";
148 sel_unread_link
= "javascript:cdmSelectArticles('unread')";
149 sel_none_link
= "javascript:cdmSelectArticles('none')";
150 sel_inv_link
= "javascript:invertHeadlineSelection()";
153 tmp
+= __('Select:')+
154 " <a href=\""+sel_all_link
+"\">"+__('All')+"</a>, "+
155 "<a href=\""+sel_unread_link
+"\">"+__('Unread')+"</a>, "+
156 "<a href=\""+sel_inv_link
+"\">"+__('Invert')+"</a>, "+
157 "<a href=\""+sel_none_link
+"\">"+__('None')+"</a>";
159 tmp
+= " ";
163 tmp
+= "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
164 if (document
.getElementById("content-frame")) {
165 tmp
+= "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
172 var toolbar_form
= document
.forms
["main_toolbar_form"];
174 var limit
= toolbar_form
.limit
[toolbar_form
.limit
.selectedIndex
].value
;
175 var view_mode
= toolbar_form
.view_mode
[toolbar_form
.view_mode
.selectedIndex
].value
;
177 var limit_qpart
= "";
178 var strategy_qpart
= "";
180 var offset_qpart
= "";
183 limit_qpart
= "LIMIT " + limit
;
186 if (view_mode
== "all_articles") {
188 } else if (view_mode
== "adaptive") {
189 if (get_local_feed_unread(feed_id
) > 0) {
190 mode_qpart
= "unread = 1";
194 } else if (view_mode
== "marked") {
195 mode_qpart
= "marked = 1";
196 } else if (view_mode
== "unread") {
197 mode_qpart
= "unread = 1";
202 var ext_tables_qpart
= "";
205 strategy_qpart
= "feed_id = " + feed_id
;
206 } else if (feed_id
== -1) {
207 strategy_qpart
= "marked = 1";
208 } else if (feed_id
== -4) {
209 strategy_qpart
= "1";
210 } else if (feed_id
< -10) {
211 var label_id
= -11 - feed_id
;
212 strategy_qpart
= "article_labels.id = articles.id AND label_id = " + label_id
;
213 ext_tables_qpart
= ",article_labels";
217 offset_qpart
= "OFFSET " + (offset
*30);
222 var query
= "SELECT *,feeds.title AS feed_title "+
223 "FROM articles,feeds"+ext_tables_qpart
+" "+
225 "feed_id = feeds.id AND " +
227 " AND " + mode_qpart
+
228 " ORDER BY updated DESC "+
232 var rs
= db
.execute(query
);
234 var line_num
= offset
*30;
236 var real_feed_id
= feed_id
;
238 while (rs
.isValidRow()) {
240 var id
= rs
.fieldByName("id");
241 var feed_id
= rs
.fieldByName("feed_id");
243 var entry_feed_title
= false;
245 if (real_feed_id
< 0) {
246 entry_feed_title
= rs
.fieldByName("feed_title");
251 var row_class
= (line_num
% 2) ? "even" : "odd";
253 if (rs
.fieldByName("unread") == "1") {
254 row_class
+= "Unread";
257 var labels
= get_local_article_labels(id
);
259 var labels_str
= "<span id=\"HLLCTR-"+id
+"\">";
260 labels_str
+= format_article_labels(labels
, id
);
261 labels_str
+= "</span>";
263 if (rs
.fieldByName("marked") == "1") {
264 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
265 "src=\"images/mark_set.png\" class=\"markedPic\""+
266 "alt=\"Unstar article\" onclick='javascript:tMark("+id
+")'>";
268 marked_pic
= "<img id=\"FMPIC-"+id
+"\" "+
269 "src=\"images/mark_unset.png\" class=\"markedPic\""+
270 "alt=\"Star article\" onclick='javascript:tMark("+id
+")'>";
273 var mouseover_attrs
= "onmouseover='postMouseIn($id)' "+
274 "onmouseout='postMouseOut($id)'";
276 var content_preview
= truncate_string(strip_tags(rs
.fieldByName("content")),
279 if (document
.getElementById("content-frame")) {
281 tmp
+= "<tr class='"+row_class
+"' id='RROW-"+id
+"' "+mouseover_attrs
+">";
283 tmp
+= "<td class='hlUpdPic'> </td>";
285 tmp
+= "<td class='hlSelectRow'>"+
286 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id
+"\"></td>";
288 tmp
+= "<td class='hlMarkedPic'>"+marked_pic
+"</td>";
290 tmp
+= "<td onclick='view("+id
+","+feed_id
+")' "+
291 "class='hlContent' valign='middle'>";
293 tmp
+= "<a target=\"_blank\" id=\"RTITLE-"+id
+"\" href=\"" +
294 rs
.fieldByName("link") + "\"" +
295 "onclick=\"return view("+id
+","+feed_id
+");\">"+
296 rs
.fieldByName("title");
298 tmp
+= "<span class=\"contentPreview\"> - "+content_preview
+"</span>";
304 if (entry_feed_title
) {
305 tmp
+= " <span class=\"hlFeed\">"+
306 "(<a href='javascript:viewfeed("+feed_id
+
307 ")'>"+entry_feed_title
+"</a>)</span>";
312 tmp
+= "<td class=\"hlUpdated\" onclick='view("+id
+","+feed_id
+")'>"+
313 "<nobr>"+rs
.fieldByName("updated").substring(0,16)+
321 if (rs
.fieldByName("unread") == "1") {
322 add_class
= "Unread";
325 tmp
+= "<div class=\"cdmArticle"+add_class
+"\" id=\"RROW-"+id
+"\" "+
326 mouseover_attrs
+"'>";
328 feed_icon_img
= "<img class=\"tinyFeedIcon\" src=\""+
329 getInitParam("icons_url")+"/"+feed_id
+".ico\" alt=\"\">";
330 cdm_feed_icon
= "<span style=\"cursor : pointer\" "+
331 "onclick=\"viewfeed("+feed_id
+")\">"+feed_icon_img
+"</span>";
333 tmp
+= "<div class=\"cdmHeader\">";
334 tmp
+= "<div class=\"articleUpdated\">"+
335 rs
.fieldByName("updated").substring(0,16)+
336 " "+cdm_feed_icon
+"</div>";
338 tmp
+= "<span id=\"RTITLE-"+id
+"\" class=\"titleWrap\">"+
339 "<a class=\"title\" onclick=\"javascript:toggleUnread("+id
+", 0)\""+
340 "target=\"_blank\" href=\""+rs
.fieldByName("link")+
341 "\">"+rs
.fieldByName("title")+"</a>";
345 if (entry_feed_title
) {
346 tmp
+= " (<a href='javascript:viewfeed("+feed_id
+
347 ")'>"+entry_feed_title
+"</a>)";
350 tmp
+= "</span></div>";
352 tmp
+= "<div class=\"cdmContent\" onclick=\"cdmClicked("+id
+")\""+
353 "id=\"CICD-"+id
+"\">";
354 tmp
+= rs
.fieldByName("content");
355 tmp
+= "<br clear='both'>"
358 tmp
+= "<div class=\"cdmFooter\"><span class='s0'>";
359 tmp
+= __("Select:")+
360 " <input type=\"checkbox\" "+
361 "onclick=\"toggleSelectRowById(this, 'RROW-"+id
+"')\" "+
362 "class=\"feedCheckBox\" id=\"RCHK-"+id
+"\">";
364 tmp
+= "</span><span class='s1'>"+marked_pic
+"</span> ";
366 /* tmp += "<span class='s1'>"+
367 "<img class='tagsPic' src='images/tag.png' alt='Tags' title='Tags'>"+
368 "<span id=\"ATSTR-"+id+"\">"+rs.fieldByName("tags")+"</span>"+
371 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
372 "href=\"javascript:toggleUnread("+id+")\">"+
383 if (line_num - offset*30 < 30) {
384 _infscroll_disable = 1;
392 if (line_num - offset*30 == 0) {
393 tmp += "<div class='whiteBox'>" +
394 __("No articles found to display.") +
397 tmp += "</div></div>";
401 var container = document.getElementById("headlines-frame");
402 container.innerHTML = tmp;
404 var ids = getSelectedArticleIds2();
406 var container = document.getElementById("headlinesList");
407 container.innerHTML = container.innerHTML + tmp;
409 for (var i = 0; i < ids.length; i++) {
410 markHeadline(ids[i]);
417 _infscroll_request_sent = 0;
420 exception_error("viewfeed_offline", e);
424 function render_offline_feedlist() {
426 var cats_enabled = getInitParam("enable_feed_cats") == "1";
428 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
430 var unread = get_local_feed_unread(-4);
432 global_unread = unread;
436 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
439 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
442 var unread = get_local_feed_unread(-1);
444 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
445 "images/mark_set.png");
450 tmp += "<li><hr/></li>";
454 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
457 var rs = db.execute("SELECT id,caption "+
461 while (rs.isValidRow()) {
462 var id = -11 - parseInt(rs.field(0));
463 var caption = rs.field(1);
464 var unread = get_local_feed_unread(id);
466 tmp += printFeedEntry(id, caption, "feed", unread,
477 tmp += "<li><hr/></li>";
480 /* var rs = db.execute("SELECT feeds.id,feeds.title,has_icon,COUNT(articles.id) "+
481 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
482 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
483 "ORDER BY feeds.title"); */
485 var order_by
= "feeds.title";
487 if (cats_enabled
) order_by
= "categories.title," + order_by
;
489 var rs
= db
.execute("SELECT "+
490 "feeds.id,feeds.title,has_icon,cat_id,collapsed "+
491 "FROM feeds,categories WHERE cat_id = categories.id "+
492 "ORDER BY "+order_by
);
496 while (rs
.isValidRow()) {
498 var id
= rs
.field(0);
499 var title
= rs
.field(1);
500 var has_icon
= rs
.field(2);
501 var unread
= get_local_feed_unread(id
);
502 var cat_id
= rs
.field(3);
503 var cat_hidden
= rs
.field(4);
505 if (cat_id
!= tmp_cat_id
&& cats_enabled
) {
506 if (tmp_cat_id
!= -1) {
509 tmp
+= printCategoryHeader(cat_id
, cat_hidden
, false);
516 icon
= "icons/" + id
+ ".ico";
521 var row_class
= "feed";
524 row_class
+= "Unread";
525 fctr_class
= "feedCtrHasUnread";
527 fctr_class
= "feedCtrNoUnread";
530 tmp
+= printFeedEntry(id
, title
, "feed", unread
, icon
);
543 render_feedlist(tmp
);
545 exception_error("render_offline_feedlist", e
);
549 function init_offline() {
553 Element
.hide("dispSwitchPrompt");
554 Element
.hide("feedBrowserPrompt");
556 Element
.hide("topLinksOnline");
557 Element
.show("topLinksOffline");
559 var tb_form
= document
.getElementById("main_toolbar_form");
560 Element
.hide(tb_form
.update
);
562 var chooser
= document
.getElementById("quickMenuChooser");
563 chooser
.disabled
= true;
565 var rs
= db
.execute("SELECT key, value FROM init_params");
567 while (rs
.isValidRow()) {
568 init_params
[rs
.field(0)] = rs
.field(1);
574 var rs
= db
.execute("SELECT COUNT(*) FROM feeds");
578 if (rs
.isValidRow()) {
579 num_feeds
= rs
.field(0);
584 if (num_feeds
== 0) {
587 __("Data for offline browsing has not been downloaded yet."));
590 render_offline_feedlist();
592 window
.setTimeout("viewfeed(-4)", 50);
595 exception_error("init_offline", e
);
599 function offline_download_parse(stage
, transport
) {
601 if (transport
.responseXML
) {
603 var sync_ok
= transport
.responseXML
.getElementsByTagName("sync-ok");
605 if (sync_ok
.length
> 0) {
606 for (var i
= 0; i
< sync_ok
.length
; i
++) {
607 var id
= sync_ok
[i
].getAttribute("id");
609 debug("synced offline info for id " + id
);
610 db
.execute("UPDATE articles SET modified = '' WHERE id = ?", [id
]);
617 var feeds
= transport
.responseXML
.getElementsByTagName("feed");
619 if (feeds
.length
> 0) {
620 db
.execute("DELETE FROM feeds");
623 for (var i
= 0; i
< feeds
.length
; i
++) {
624 var id
= feeds
[i
].getAttribute("id");
625 var has_icon
= feeds
[i
].getAttribute("has_icon");
626 var title
= feeds
[i
].firstChild
.nodeValue
;
627 var cat_id
= feeds
[i
].getAttribute("cat_id");
629 db
.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
631 [id
, title
, has_icon
, cat_id
]);
634 var cats
= transport
.responseXML
.getElementsByTagName("category");
636 if (feeds
.length
> 0) {
637 db
.execute("DELETE FROM categories");
640 for (var i
= 0; i
< cats
.length
; i
++) {
641 var id
= cats
[i
].getAttribute("id");
642 var collapsed
= cats
[i
].getAttribute("collapsed");
643 var title
= cats
[i
].firstChild
.nodeValue
;
645 db
.execute("INSERT INTO categories (id,title,collapsed)"+
647 [id
, title
, collapsed
]);
650 var labels
= transport
.responseXML
.getElementsByTagName("label");
652 if (labels
.length
> 0) {
653 db
.execute("DELETE FROM labels");
656 for (var i
= 0; i
< labels
.length
; i
++) {
657 var id
= labels
[i
].getAttribute("id");
658 var fg_color
= labels
[i
].getAttribute("fg_color");
659 var bg_color
= labels
[i
].getAttribute("bg_color");
660 var caption
= labels
[i
].firstChild
.nodeValue
;
662 db
.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
664 [id
, caption
, fg_color
, bg_color
]);
667 window
.setTimeout("update_offline_data("+(stage
+1)+")", 10*1000);
670 var articles
= transport
.responseXML
.getElementsByTagName("article");
672 var limit
= transport
.responseXML
.getElementsByTagName("limit")[0];
675 limit
= limit
.getAttribute("value");
680 var articles_found
= 0;
682 for (var i
= 0; i
< articles
.length
; i
++) {
683 var a
= eval("("+articles
[i
].firstChild
.nodeValue
+")");
687 db
.execute("DELETE FROM articles WHERE id = ?", [a
.id
]);
689 db
.execute("INSERT INTO articles "+
690 "(id, feed_id, title, link, guid, updated, content, "+
691 "unread, marked, tags, comments) "+
692 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
693 [a
.id
, a
.feed_id
, a
.title
, a
.link
, a
.guid
, a
.updated
,
694 a
.content
, a
.unread
, a
.marked
, a
.tags
,
697 if (a
.labels
.length
> 0) {
698 for (var j
= 0; j
< a
.labels
.length
; j
++) {
699 label_local_add_article(a
.id
, a
.labels
[j
][0]);
706 debug("downloaded articles: " + articles_found
+ " limit: " + limit
);
708 var has_sync_data
= has_local_sync_data();
710 if (articles_found
>= limit
|| has_sync_data
) {
711 window
.setTimeout("update_offline_data("+(stage
+1)+")", 10*1000);
712 debug("<b>update_offline_data: done " + stage
+ " HSD: " +
713 has_sync_data
+ "</b>");
715 window
.setTimeout("update_offline_data(0)", 1800*1000);
716 debug("update_offline_data: finished");
718 db
.execute("DELETE FROM articles WHERE "+
719 "updated < DATETIME('NOW', 'localtime', '-31 days')");
723 update_local_sync_data();
730 exception_error("offline_download_parse", e
);
734 function update_offline_data(stage
) {
737 if (!stage
) stage
= 0;
738 if (offline_mode
) return;
740 debug("update_offline_data: stage " + stage
);
742 // notify_progress("Updating offline data... (" + stage +")", true);
744 var query
= "backend.php?op=rpc&subop=download&stage=" + stage
;
746 var rs
= db
.execute("SELECT MAX(id), MIN(id) FROM articles");
748 if (rs
.isValidRow() && rs
.field(0)) {
749 var offline_dl_max_id
= rs
.field(0);
750 var offline_dl_min_id
= rs
.field(1);
752 query
= query
+ "&cidt=" + offline_dl_max_id
;
753 query
= query
+ "&cidb=" + offline_dl_min_id
;
758 var to_sync
= prepare_local_sync_data();
761 to_sync
= "?sync=" + param_escape(to_sync
);
764 debug(query
+ "/" + to_sync
);
766 new Ajax
.Request(query
, {
768 onComplete: function(transport
) {
769 offline_download_parse(stage
, transport
);
773 exception_error("initiate_offline_download", e
);
777 function set_feedlist_counter(id
, ctr
, is_cat
) {
780 var feedctr
= document
.getElementById("FEEDCTR-" + id
);
781 var feedu
= document
.getElementById("FEEDU-" + id
);
782 var feedr
= document
.getElementById("FEEDR-" + id
);
785 var catctr
= document
.getElementById("FCATCTR-" + id
);
787 catctr
.innerHTML
= "(" + ctr
+ ")";
789 catctr
.className
= "catCtrHasUnread";
791 catctr
.className
= "catCtrNoUnread";
794 } else if (feedctr
&& feedu
&& feedr
) {
796 var row_needs_hl
= (ctr
> 0 && ctr
> parseInt(feedu
.innerHTML
));
798 feedu
.innerHTML
= ctr
;
801 feedctr
.className
= "feedCtrHasUnread";
802 if (!feedr
.className
.match("Unread")) {
803 var is_selected
= feedr
.className
.match("Selected");
805 feedr
.className
= feedr
.className
.replace("Selected", "");
806 feedr
.className
= feedr
.className
.replace("Unread", "");
808 feedr
.className
= feedr
.className
+ "Unread";
811 feedr
.className
= feedr
.className
+ "Selected";
817 new Effect
.Highlight(feedr
, {duration
: 1, startcolor
: "#fff7d5",
818 queue
: { position
:'end', scope
: 'EFQ-' + id
, limit
: 1 } } );
821 feedctr
.className
= "feedCtrNoUnread";
822 feedr
.className
= feedr
.className
.replace("Unread", "");
827 exception_error("set_feedlist_counter", e
);
831 function update_local_feedlist_counters() {
833 if (!offline_mode
|| !db
) return;
835 /* var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
836 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
837 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
838 "ORDER BY feeds.title"); */
840 var rs
= db
.execute("SELECT id FROM feeds "+
843 while (rs
.isValidRow()) {
844 var id
= rs
.field(0);
845 var ctr
= get_local_feed_unread(id
);
846 set_feedlist_counter(id
, ctr
, false);
852 var rs
= db
.execute("SELECT cat_id,SUM(unread) "+
853 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
855 while (rs
.isValidRow()) {
856 var id
= rs
.field(0);
857 var ctr
= rs
.field(1);
858 set_feedlist_counter(id
, ctr
, true);
864 set_feedlist_counter(-2, get_local_category_unread(-2), true);
866 set_feedlist_counter(-4, get_local_feed_unread(-4));
867 set_feedlist_counter(-1, get_local_feed_unread(-1));
869 var rs
= db
.execute("SELECT id FROM labels");
871 while (rs
.isValidRow()) {
872 var id
= -11 - rs
.field(0);
873 var ctr
= get_local_feed_unread(id
);
874 set_feedlist_counter(id
, ctr
, false);
880 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
882 global_unread
= get_local_feed_unread(-4);
886 exception_error("update_local_feedlist_counters", e
);
890 function get_local_feed_unread(id
) {
895 rs
= db
.execute("SELECT SUM(unread) FROM articles");
896 } else if (id
== -1) {
897 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE marked = 1");
899 rs
= db
.execute("SELECT SUM(unread) FROM articles WHERE feed_id = ?", [id
]);
900 } else if (id
< -10) {
901 var label_id
= -11 - id
;
902 rs
= db
.execute("SELECT SUM(unread) FROM articles,article_labels "+
903 "WHERE article_labels.id = articles.id AND label_id = ?", [label_id
]);
908 if (rs
.isValidRow()) {
919 exception_error("get_local_feed_unread", e
);
923 function init_gears() {
926 if (window
.google
&& google
.gears
) {
927 localServer
= google
.gears
.factory
.create("beta.localserver");
928 store
= localServer
.createManagedStore("tt-rss");
929 db
= google
.gears
.factory
.create('beta.database');
932 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
934 var rs
= db
.execute("SELECT schema_version FROM version");
938 if (rs
.isValidRow()) {
939 version
= rs
.field(0);
944 if (version
!= SCHEMA_VERSION
) {
945 db
.execute("DROP TABLE IF EXISTS init_params");
946 db
.execute("DROP TABLE IF EXISTS cache");
947 db
.execute("DROP TABLE IF EXISTS feeds");
948 db
.execute("DROP TABLE IF EXISTS categories");
949 db
.execute("DROP TABLE IF EXISTS labels");
950 db
.execute("DROP TABLE IF EXISTS article_labels");
951 db
.execute("DROP TABLE IF EXISTS articles");
952 db
.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
953 db
.execute("DROP INDEX IF EXISTS articles_unread_idx");
954 db
.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
955 db
.execute("DROP INDEX IF EXISTS articles_id_idx");
956 db
.execute("DROP INDEX IF EXISTS article_labels_id_idx");
957 db
.execute("DROP TABLE IF EXISTS version");
958 db
.execute("DROP TRIGGER IF EXISTS articles_update_unread");
959 db
.execute("DROP TRIGGER IF EXISTS articles_update_marked");
960 db
.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
961 db
.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
962 db
.execute("DROP TABLE IF EXISTS syncdata");
963 db
.execute("INSERT INTO version (schema_version) VALUES (?)",
967 db
.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
969 db
.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
970 db
.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
971 db
.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
972 db
.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
973 db
.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
974 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)");
976 db
.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
977 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
978 db
.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
979 db
.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
980 db
.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
982 db
.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
984 db
.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
986 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
987 "UPDATE OF unread ON articles "+
989 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
990 "WHERE id = OLD.id AND "+
991 "OLD.unread != NEW.unread;"+
994 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
995 "UPDATE OF marked ON articles "+
997 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
998 "WHERE id = OLD.id;"+
1001 db
.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1002 "DELETE ON articles "+
1004 "DELETE FROM article_labels WHERE id = OLD.id; "+
1007 init_local_sync_data();
1009 Element
.show("restartOfflinePic");
1016 exception_error("init_gears", e
);
1020 function gotoOffline() {
1021 window
.location
.href
= "tt-rss.php?offline=1";
1024 function gotoOnline() {
1025 window
.location
.href
= "tt-rss.php";
1028 function local_collapse_cat(id
) {
1031 db
.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1035 exception_error("local_collapse_cat", e
);
1039 function get_local_category_title(id
) {
1042 var rs
= db
.execute("SELECT title FROM categories WHERE id = ?", [id
]);
1045 if (rs
.isValidRow()) {
1054 exception_error("get_local_category_title", e
);
1058 function get_local_category_unread(id
) {
1063 rs
= db
.execute("SELECT SUM(unread) FROM articles, feeds "+
1064 "WHERE feeds.id = feed_id AND cat_id = ?", [id
]);
1065 } else if (id
== -2) {
1066 rs
= db
.execute("SELECT SUM(unread) FROM article_labels, articles "+
1067 "where article_labels.id = articles.id");
1074 if (rs
.isValidRow()) {
1083 exception_error("get_local_category_unread", e
);
1087 function printCategoryHeader(cat_id
, hidden
, can_browse
) {
1089 if (hidden
== undefined) hidden
= false;
1090 if (can_browse
== undefined) can_browse
= false;
1092 var tmp_category
= get_local_category_title(cat_id
);
1095 var cat_unread
= get_local_category_unread(cat_id
);
1097 var holder_style
= "";
1101 holder_style
= "display:none;";
1105 var catctr_class
= (cat_unread
> 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1107 var browse_cat_link
= "";
1108 var inner_title_class
= "catTitleNL";
1111 browse_cat_link
= "onclick=\"javascript:viewCategory($cat_id)\"";
1112 inner_title_class
= "catTitle";
1115 var cat_class
= "feedCat";
1117 tmp
+= "<li class=\""+cat_class
+"\" id=\"FCAT-"+cat_id
+"\">"+
1118 "<img onclick=\"toggleCollapseCat("+cat_id
+")\" class=\"catCollapse\""+
1119 " title=\""+__('Click to collapse category')+"\""+
1120 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class
+"\" "+
1121 " id=\"FCATN-"+cat_id
+"\" "+browse_cat_link
+
1122 "\">"+tmp_category
+"</span>";
1124 tmp
+= "<span id=\"FCAP-"+cat_id
+"\">";
1126 tmp
+= " <span id=\"FCATCTR-"+cat_id
+"\" "+
1127 "class=\""+catctr_class
+"\">("+cat_unread
+")</span> "+ellipsis
;
1131 tmp
+= "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id
+"\" "+
1132 "style='"+holder_style
+"'>";
1136 exception_error("printCategoryHeader", e
);
1140 function is_local_cat_collapsed(id
) {
1143 var rs
= db
.execute("SELECT collapsed FROM categories WHERE id = ?", [id
]);
1146 if (rs
.isValidRow()) {
1147 cat_hidden
= rs
.field(0);
1152 return cat_hidden
== "1";
1155 exception_error("is_local_cat_collapsed", e
);
1159 function get_local_article_labels(id
) {
1161 var rs
= db
.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1162 "FROM labels, article_labels "+
1163 "WHERE labels.id = label_id AND article_labels.id = ?", [id
]);
1165 var tmp
= new Array();
1167 while (rs
.isValidRow()) {
1168 var e
= new Array();
1183 exception_error("get_local_article_labels", e
);
1187 function label_local_add_article(id
, label_id
) {
1189 //debug("label_local_add_article " + id + " => " + label_id);
1191 var rs
= db
.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1192 "id = ? AND label_id = ?", [id
, label_id
]);
1193 var check
= rs
.field(0);
1195 if (rs
.isValidRow()) {
1196 var check
= rs
.field(0);
1201 db
.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1202 "(?,?)", [id
, label_id
]);
1206 exception_error("label_local_add_article", e
);
1210 function get_local_feed_title(id
) {
1213 var feed_title
= "Unknown feed: " + id
;
1216 var rs
= db
.execute("SELECT title FROM feeds WHERE id = ?", [id
]);
1218 if (rs
.isValidRow()) {
1219 feed_title
= rs
.field(0);
1223 } else if (id
== -1) {
1224 feed_title
= __("Starred articles");
1225 } else if (id
== -4) {
1226 feed_title
= __("All articles");
1227 } else if (id
< -10) {
1229 var label_id
= -11 - id
;
1231 var rs
= db
.execute("SELECT caption FROM labels WHERE id = ?", [label_id
]);
1233 if (rs
.isValidRow()) {
1234 feed_title
= rs
.field(0);
1243 exception_error("get_local_feed_title", e
);
1247 function format_article_labels(labels
, id
) {
1250 var labels_str
= "";
1252 if (!labels
) return "";
1254 for (var i
= 0; i
< labels
.length
; i
++) {
1257 labels_str
+= "<span class='hlLabelRef' "+
1258 "style='color : "+l
[2]+"; background-color : "+l
[3]+"'>"+l
[1]+"</span>";
1264 exception_error("format_article_labels", e
);
1268 function init_local_sync_data() {
1270 var rs
= db
.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1271 var has_last_online
= 0;
1273 if (rs
.isValidRow()) {
1274 has_last_online
= rs
.field(0);
1279 if (!has_last_online
) {
1280 db
.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1284 exception_error("init_local_sync_data", e
);
1289 function has_local_sync_data() {
1292 var rs
= db
.execute("SELECT id FROM articles "+
1293 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1298 if (rs
.isValidRow()) {
1307 exception_error("has_local_sync_data", e
);
1311 function prepare_local_sync_data() {
1313 var rs
= db
.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1315 var last_online
= "";
1317 if (rs
.isValidRow()) {
1318 last_online
= rs
.field(0);
1323 var rs
= db
.execute("SELECT id,unread,marked FROM articles "+
1324 "WHERE modified > ? LIMIT 200", [last_online
]);
1326 var tmp
= last_online
+ ";";
1330 while (rs
.isValidRow()) {
1331 var e
= new Array();
1333 tmp
= tmp
+ rs
.field(0) + "," + rs
.field(1) + "," + rs
.field(2) + ";";
1348 exception_error("prepare_local_sync_data", e
);
1352 function update_local_sync_data() {
1354 if (db
&& !offline_mode
) {
1356 var rs
= db
.execute("SELECT id FROM articles "+
1357 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1358 "key = 'last_online') LIMIT 1")
1362 if (rs
.isValidRow()) {
1368 /* no pending articles to sync */
1371 db
.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1372 "WHERE key = 'last_online'");
1377 exception_error("update_local_sync_data", e
);
1381 function catchup_local_feed(id
, is_cat
) {
1387 db
.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id
]);
1388 } else if (id
== -1) {
1389 db
.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1390 } else if (id
== -4) {
1391 db
.execute("UPDATE articles SET unread = 0");
1392 } else if (id
< -10) {
1393 var label_id
= -11-id
;
1395 db
.execute("UPDATE articles SET unread = 0 WHERE "+
1396 "(SELECT COUNT(*) FROM article_labels WHERE "+
1397 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id
]);
1401 update_local_feedlist_counters();
1404 exception_error("catchup_local_feed", e
);