]> git.wh0rd.org - tt-rss.git/blob - offline.js
synchronize unread status from offline to online DB
[tt-rss.git] / offline.js
1 var SCHEMA_VERSION = 10;
2
3 var offline_mode = false;
4 var store = false;
5 var localServer = false;
6 var db = false;
7
8 function view_offline(id, feed_id) {
9 try {
10
11 enableHotkeys();
12 showArticleInHeadlines(id);
13
14 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
15
16 var rs = db.execute("SELECT * FROM articles WHERE id = ?", [id]);
17
18 if (rs.isValidRow()) {
19
20 var tmp = "<div class=\"postReply\">";
21
22 tmp += "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" "+
23 "onmouseout=\"enable_resize(false)\">";
24
25 tmp += "<div class=\"postDate\">"+rs.fieldByName("updated")+"</div>";
26
27 if (rs.fieldByName("link") != "") {
28 tmp += "<div clear='both'><a target=\"_blank\" "+
29 "href=\"" + rs.fieldByName("link") + "\">" +
30 rs.fieldByName("title") + "</a></div>";
31 } else {
32 tmp += "<div clear='both'>" + rs.fieldByName("title") + "</div>";
33 }
34
35 /* tmp += "<div style='float : right'> "+
36 "<img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
37 tmp += rs.fieldByName("tags");
38 tmp += "</div>"; */
39
40 /* tmp += "<div clear='both'>"+
41 "<a target=\"_blank\" "+
42 "href=\"" + rs.fieldByName("comments") + "\">" +
43 __("comments") + "</a></div>"; */
44
45 tmp += "</div>";
46
47 tmp += "<div class=\"postContent\">"
48 tmp += rs.fieldByName("content");
49 tmp += "</div>";
50
51 tmp += "</div>";
52
53 render_article(tmp);
54 update_local_feedlist_counters();
55 }
56
57 rs.close();
58
59 return false;
60
61 } catch (e) {
62 exception_error("view_offline", e);
63 }
64 }
65
66 function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, offset) {
67 try {
68 notify('');
69
70 if (!offset) offset = 0;
71
72 if (offset > 0) {
73 _feed_cur_page = parseInt(offset);
74 if (_infscroll_request_sent) {
75 return;
76 }
77 } else {
78 _feed_cur_page = 0;
79 _infscroll_disable = 0;
80 }
81
82 if (getActiveFeedId() != feed_id) {
83 _feed_cur_page = 0;
84 active_post_id = 0;
85 _infscroll_disable = 0;
86 }
87
88 loading_set_progress(100);
89
90 clean_feed_selections();
91
92 setActiveFeedId(feed_id, is_cat);
93
94 if (!is_cat) {
95 var feedr = document.getElementById("FEEDR-" + feed_id);
96 if (feedr && !feedr.className.match("Selected")) {
97 feedr.className = feedr.className + "Selected";
98 }
99 } else {
100 var feedr = document.getElementById("FCAT-" + feed_id);
101 if (feedr && !feedr.className.match("Selected")) {
102 feedr.className = feedr.className + "Selected";
103 }
104 }
105
106 if (subop == "MarkAllRead") {
107 catchup_local_feed(feed_id, is_cat);
108 }
109
110 disableContainerChildren("headlinesToolbar", false);
111 Form.enable("main_toolbar_form");
112
113 var f = document.getElementById("headlines-frame");
114 try {
115 if (reply.offset == 0) {
116 debug("resetting headlines scrollTop");
117 f.scrollTop = 0;
118 }
119 } catch (e) { };
120
121
122 var tmp = "";
123
124 var feed_title = get_local_feed_title(feed_id);
125
126 if (feed_title) {
127
128 if (offset == 0) {
129 tmp += "<div id=\"headlinesContainer\">";
130
131 tmp += "<div class=\"headlinesSubToolbar\">";
132 tmp += "<div id=\"subtoolbar_ftitle\">";
133 tmp += feed_title;
134 tmp += "</div>";
135
136 var sel_all_link;
137 var sel_unread_link;
138 var sel_none_link;
139 var sel_inv_link;
140
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()";
146 } else {
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()";
151 }
152
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>";
158
159 tmp += "&nbsp;&nbsp;";
160
161 tmp += "</div>";
162
163 tmp += "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
164 if (document.getElementById("content-frame")) {
165 tmp += "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
166 }
167
168 }
169
170 var limit = 30;
171
172 var toolbar_form = document.forms["main_toolbar_form"];
173
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;
176
177 var limit_qpart = "";
178 var strategy_qpart = "";
179 var mode_qpart = "";
180 var offset_qpart = "";
181
182 if (limit != 0) {
183 limit_qpart = "LIMIT " + limit;
184 }
185
186 if (view_mode == "all_articles") {
187 mode_qpart = "1";
188 } else if (view_mode == "adaptive") {
189 if (get_local_feed_unread(feed_id) > 0) {
190 mode_qpart = "unread = 1";
191 } else {
192 mode_qpart = "1";
193 }
194 } else if (view_mode == "marked") {
195 mode_qpart = "marked = 1";
196 } else if (view_mode == "unread") {
197 mode_qpart = "unread = 1";
198 } else {
199 mode_qpart = "1";
200 }
201
202 var ext_tables_qpart = "";
203
204 if (feed_id > 0) {
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";
214 }
215
216 if (offset > 0) {
217 offset_qpart = "OFFSET " + (offset*30);
218 } else {
219 offset_qpart = "";
220 }
221
222 var query = "SELECT *,feeds.title AS feed_title "+
223 "FROM articles,feeds"+ext_tables_qpart+" "+
224 "WHERE " +
225 "feed_id = feeds.id AND " +
226 strategy_qpart +
227 " AND " + mode_qpart +
228 " ORDER BY updated DESC "+
229 limit_qpart + " " +
230 offset_qpart;
231
232 var rs = db.execute(query);
233
234 var line_num = offset*30;
235
236 var real_feed_id = feed_id;
237
238 while (rs.isValidRow()) {
239
240 var id = rs.fieldByName("id");
241 var feed_id = rs.fieldByName("feed_id");
242
243 var entry_feed_title = false;
244
245 if (real_feed_id < 0) {
246 entry_feed_title = rs.fieldByName("feed_title");
247 }
248
249 var marked_pic;
250
251 var row_class = (line_num % 2) ? "even" : "odd";
252
253 if (rs.fieldByName("unread") == "1") {
254 row_class += "Unread";
255 }
256
257 var labels = get_local_article_labels(id);
258
259 var labels_str = "<span id=\"HLLCTR-"+id+"\">";
260 labels_str += format_article_labels(labels, id);
261 labels_str += "</span>";
262
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+")'>";
267 } else {
268 marked_pic = "<img id=\"FMPIC-"+id+"\" "+
269 "src=\"images/mark_unset.png\" class=\"markedPic\""+
270 "alt=\"Star article\" onclick='javascript:tMark("+id+")'>";
271 }
272
273 var mouseover_attrs = "onmouseover='postMouseIn($id)' "+
274 "onmouseout='postMouseOut($id)'";
275
276 var content_preview = truncate_string(strip_tags(rs.fieldByName("content")),
277 100);
278
279 if (document.getElementById("content-frame")) {
280
281 tmp += "<tr class='"+row_class+"' id='RROW-"+id+"' "+mouseover_attrs+">";
282
283 tmp += "<td class='hlUpdPic'> </td>";
284
285 tmp += "<td class='hlSelectRow'>"+
286 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id+"\"></td>";
287
288 tmp += "<td class='hlMarkedPic'>"+marked_pic+"</td>";
289
290 tmp += "<td onclick='view("+id+","+feed_id+")' "+
291 "class='hlContent' valign='middle'>";
292
293 tmp += "<a target=\"_blank\" id=\"RTITLE-"+id+"\" href=\"" +
294 rs.fieldByName("link") + "\"" +
295 "onclick=\"return view("+id+","+feed_id+");\">"+
296 rs.fieldByName("title");
297
298 tmp += "<span class=\"contentPreview\"> - "+content_preview+"</span>";
299
300 tmp += "</a>";
301
302 tmp += labels_str;
303
304 if (entry_feed_title) {
305 tmp += " <span class=\"hlFeed\">"+
306 "(<a href='javascript:viewfeed("+feed_id+
307 ")'>"+entry_feed_title+"</a>)</span>";
308 }
309
310 tmp += "</td>";
311
312 tmp += "<td class=\"hlUpdated\" onclick='view("+id+","+feed_id+")'>"+
313 "<nobr>"+rs.fieldByName("updated").substring(0,16)+
314 "</nobr></td>";
315
316 tmp += "</tr>";
317 } else {
318
319 var add_class = "";
320
321 if (rs.fieldByName("unread") == "1") {
322 add_class = "Unread";
323 }
324
325 tmp += "<div class=\"cdmArticle"+add_class+"\" id=\"RROW-"+id+"\" "+
326 mouseover_attrs+"'>";
327
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>";
332
333 tmp += "<div class=\"cdmHeader\">";
334 tmp += "<div class=\"articleUpdated\">"+
335 rs.fieldByName("updated").substring(0,16)+
336 " "+cdm_feed_icon+"</div>";
337
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>";
342
343 tmp += labels_str;
344
345 if (entry_feed_title) {
346 tmp += "&nbsp;(<a href='javascript:viewfeed("+feed_id+
347 ")'>"+entry_feed_title+"</a>)";
348 }
349
350 tmp += "</span></div>";
351
352 tmp += "<div class=\"cdmContent\" onclick=\"cdmClicked("+id+")\""+
353 "id=\"CICD-"+id+"\">";
354 tmp += rs.fieldByName("content");
355 tmp += "<br clear='both'>"
356 tmp += "</div>";
357
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+"\">";
363
364 tmp += "</span><span class='s1'>"+marked_pic+"</span> ";
365
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>"+
369 "</span>"; */
370
371 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
372 "href=\"javascript:toggleUnread("+id+")\">"+
373 "Unread</a></span>";
374 tmp += "</div>";
375
376 tmp += "</div>";
377 }
378
379 rs.next();
380 line_num++;
381 }
382
383 if (line_num - offset*30 < 30) {
384 _infscroll_disable = 1;
385 }
386
387 rs.close();
388
389 if (offset == 0) {
390 tmp += "</table>";
391
392 if (line_num - offset*30 == 0) {
393 tmp += "<div class='whiteBox'>" +
394 __("No articles found to display.") +
395 "</div>";
396 }
397 tmp += "</div></div>";
398 }
399
400 if (offset == 0) {
401 var container = document.getElementById("headlines-frame");
402 container.innerHTML = tmp;
403 } else {
404 var ids = getSelectedArticleIds2();
405
406 var container = document.getElementById("headlinesList");
407 container.innerHTML = container.innerHTML + tmp;
408
409 for (var i = 0; i < ids.length; i++) {
410 markHeadline(ids[i]);
411 }
412 }
413 }
414
415 remove_splash();
416
417 _infscroll_request_sent = 0;
418
419 } catch (e) {
420 exception_error("viewfeed_offline", e);
421 }
422 }
423
424 function render_offline_feedlist() {
425 try {
426 var cats_enabled = getInitParam("enable_feed_cats") == "1";
427
428 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
429
430 var unread = get_local_feed_unread(-4);
431
432 global_unread = unread;
433 updateTitle();
434
435 if (cats_enabled) {
436 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
437 }
438
439 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
440 "images/tag.png");
441
442 var unread = get_local_feed_unread(-1);
443
444 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
445 "images/mark_set.png");
446
447 if (cats_enabled) {
448 tmp += "</ul></li>";
449 } else {
450 tmp += "<li><hr/></li>";
451 }
452
453 if (cats_enabled) {
454 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
455 }
456
457 var rs = db.execute("SELECT id,caption "+
458 "FROM labels "+
459 "ORDER BY caption");
460
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);
465
466 tmp += printFeedEntry(id, caption, "feed", unread,
467 "images/label.png");
468
469 rs.next();
470 }
471
472 rs.close();
473
474 if (cats_enabled) {
475 tmp += "</ul></li>";
476 } else {
477 tmp += "<li><hr/></li>";
478 }
479
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"); */
484
485 var order_by = "feeds.title";
486
487 if (cats_enabled) order_by = "categories.title," + order_by;
488
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);
493
494 var tmp_cat_id = -1;
495
496 while (rs.isValidRow()) {
497
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);
504
505 if (cat_id != tmp_cat_id && cats_enabled) {
506 if (tmp_cat_id != -1) {
507 tmp += "</ul></li>";
508 }
509 tmp += printCategoryHeader(cat_id, cat_hidden, false);
510 tmp_cat_id = cat_id;
511 }
512
513 var icon = "";
514
515 if (has_icon) {
516 icon = "icons/" + id + ".ico";
517 }
518
519 var feed_icon = "";
520
521 var row_class = "feed";
522
523 if (unread > 0) {
524 row_class += "Unread";
525 fctr_class = "feedCtrHasUnread";
526 } else {
527 fctr_class = "feedCtrNoUnread";
528 }
529
530 tmp += printFeedEntry(id, title, "feed", unread, icon);
531
532 rs.next();
533 }
534
535 rs.close();
536
537 if (cats_enabled) {
538 tmp += "</ul>";
539 }
540
541 tmp += "</ul>";
542
543 render_feedlist(tmp);
544 } catch (e) {
545 exception_error("render_offline_feedlist", e);
546 }
547 }
548
549 function init_offline() {
550 try {
551 offline_mode = true;
552
553 Element.hide("dispSwitchPrompt");
554 Element.hide("feedBrowserPrompt");
555
556 Element.hide("topLinksOnline");
557 Element.show("topLinksOffline");
558
559 var tb_form = document.getElementById("main_toolbar_form");
560 Element.hide(tb_form.update);
561
562 var chooser = document.getElementById("quickMenuChooser");
563 chooser.disabled = true;
564
565 var rs = db.execute("SELECT key, value FROM init_params");
566
567 while (rs.isValidRow()) {
568 init_params[rs.field(0)] = rs.field(1);
569 rs.next();
570 }
571
572 rs.close();
573
574 var rs = db.execute("SELECT COUNT(*) FROM feeds");
575
576 var num_feeds = 0;
577
578 if (rs.isValidRow()) {
579 num_feeds = rs.field(0);
580 }
581
582 rs.close();
583
584 if (num_feeds == 0) {
585 remove_splash();
586 return fatalError(0,
587 __("Data for offline browsing has not been downloaded yet."));
588 }
589
590 render_offline_feedlist();
591 init_second_stage();
592 window.setTimeout("viewfeed(-4)", 50);
593
594 } catch (e) {
595 exception_error("init_offline", e);
596 }
597 }
598
599 function offline_download_parse(stage, transport) {
600 try {
601 if (transport.responseXML) {
602
603 var sync_ok = transport.responseXML.getElementsByTagName("sync-ok");
604
605 if (sync_ok.length > 0) {
606 for (var i = 0; i < sync_ok.length; i++) {
607 var id = sync_ok[i].getAttribute("id");
608 if (id) {
609 debug("synced offline info for id " + id);
610 db.execute("UPDATE articles SET modified = '' WHERE id = ?", [id]);
611 }
612 }
613 }
614
615 if (stage == 0) {
616
617 var feeds = transport.responseXML.getElementsByTagName("feed");
618
619 if (feeds.length > 0) {
620 db.execute("DELETE FROM feeds");
621 }
622
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");
628
629 db.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
630 "VALUES (?,?,?,?)",
631 [id, title, has_icon, cat_id]);
632 }
633
634 var cats = transport.responseXML.getElementsByTagName("category");
635
636 if (feeds.length > 0) {
637 db.execute("DELETE FROM categories");
638 }
639
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;
644
645 db.execute("INSERT INTO categories (id,title,collapsed)"+
646 "VALUES (?,?,?)",
647 [id, title, collapsed]);
648 }
649
650 var labels = transport.responseXML.getElementsByTagName("label");
651
652 if (labels.length > 0) {
653 db.execute("DELETE FROM labels");
654 }
655
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;
661
662 db.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
663 "VALUES (?,?,?,?)",
664 [id, caption, fg_color, bg_color]);
665 }
666
667 window.setTimeout("update_offline_data("+(stage+1)+")", 10*1000);
668 } else {
669
670 var articles = transport.responseXML.getElementsByTagName("article");
671
672 var limit = transport.responseXML.getElementsByTagName("limit")[0];
673
674 if (limit) {
675 limit = limit.getAttribute("value");
676 } else {
677 limit = 0;
678 }
679
680 var articles_found = 0;
681
682 for (var i = 0; i < articles.length; i++) {
683 var a = eval("("+articles[i].firstChild.nodeValue+")");
684 articles_found++;
685 if (a) {
686
687 db.execute("DELETE FROM articles WHERE id = ?", [a.id]);
688
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,
695 a.comments]);
696
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]);
700 }
701 }
702
703 }
704 }
705
706 debug("downloaded articles: " + articles_found + " limit: " + limit);
707
708 var has_sync_data = has_local_sync_data();
709
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>");
714 } else {
715 window.setTimeout("update_offline_data(0)", 1800*1000);
716 debug("update_offline_data: finished");
717
718 db.execute("DELETE FROM articles WHERE "+
719 "updated < DATETIME('NOW', 'localtime', '-31 days')");
720
721 }
722
723 update_local_sync_data();
724 }
725
726 // notify('');
727
728 }
729 } catch (e) {
730 exception_error("offline_download_parse", e);
731 }
732 }
733
734 function update_offline_data(stage) {
735 try {
736
737 if (!stage) stage = 0;
738 if (offline_mode) return;
739
740 debug("update_offline_data: stage " + stage);
741
742 // notify_progress("Updating offline data... (" + stage +")", true);
743
744 var query = "backend.php?op=rpc&subop=download&stage=" + stage;
745
746 var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
747
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);
751
752 query = query + "&cidt=" + offline_dl_max_id;
753 query = query + "&cidb=" + offline_dl_min_id;
754 }
755
756 rs.close();
757
758 var to_sync = prepare_local_sync_data();
759
760 if (to_sync != "") {
761 to_sync = "?sync=" + param_escape(to_sync);
762 }
763
764 debug(query + "/" + to_sync);
765
766 new Ajax.Request(query, {
767 parameters: to_sync,
768 onComplete: function(transport) {
769 offline_download_parse(stage, transport);
770 } });
771
772 } catch (e) {
773 exception_error("initiate_offline_download", e);
774 }
775 }
776
777 function set_feedlist_counter(id, ctr, is_cat) {
778 try {
779
780 var feedctr = document.getElementById("FEEDCTR-" + id);
781 var feedu = document.getElementById("FEEDU-" + id);
782 var feedr = document.getElementById("FEEDR-" + id);
783
784 if (is_cat) {
785 var catctr = document.getElementById("FCATCTR-" + id);
786 if (catctr) {
787 catctr.innerHTML = "(" + ctr + ")";
788 if (ctr > 0) {
789 catctr.className = "catCtrHasUnread";
790 } else {
791 catctr.className = "catCtrNoUnread";
792 }
793 }
794 } else if (feedctr && feedu && feedr) {
795
796 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
797
798 feedu.innerHTML = ctr;
799
800 if (ctr > 0) {
801 feedctr.className = "feedCtrHasUnread";
802 if (!feedr.className.match("Unread")) {
803 var is_selected = feedr.className.match("Selected");
804
805 feedr.className = feedr.className.replace("Selected", "");
806 feedr.className = feedr.className.replace("Unread", "");
807
808 feedr.className = feedr.className + "Unread";
809
810 if (is_selected) {
811 feedr.className = feedr.className + "Selected";
812 }
813
814 }
815
816 if (row_needs_hl) {
817 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
818 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
819 }
820 } else {
821 feedctr.className = "feedCtrNoUnread";
822 feedr.className = feedr.className.replace("Unread", "");
823 }
824 }
825
826 } catch (e) {
827 exception_error("set_feedlist_counter", e);
828 }
829 }
830
831 function update_local_feedlist_counters() {
832 try {
833 if (!offline_mode || !db) return;
834
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"); */
839
840 var rs = db.execute("SELECT id FROM feeds "+
841 "ORDER BY title");
842
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);
847 rs.next();
848 }
849
850 rs.close();
851
852 var rs = db.execute("SELECT cat_id,SUM(unread) "+
853 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
854
855 while (rs.isValidRow()) {
856 var id = rs.field(0);
857 var ctr = rs.field(1);
858 set_feedlist_counter(id, ctr, true);
859 rs.next();
860 }
861
862 rs.close();
863
864 set_feedlist_counter(-2, get_local_category_unread(-2), true);
865
866 set_feedlist_counter(-4, get_local_feed_unread(-4));
867 set_feedlist_counter(-1, get_local_feed_unread(-1));
868
869 var rs = db.execute("SELECT id FROM labels");
870
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);
875 rs.next();
876 }
877
878 rs.close();
879
880 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
881
882 global_unread = get_local_feed_unread(-4);
883 updateTitle();
884
885 } catch (e) {
886 exception_error("update_local_feedlist_counters", e);
887 }
888 }
889
890 function get_local_feed_unread(id) {
891 try {
892 var rs;
893
894 if (id == -4) {
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");
898 } else if (id > 0) {
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]);
904 }
905
906 var a = false;
907
908 if (rs.isValidRow()) {
909 a = rs.field(0);
910 } else {
911 a = 0;
912 }
913
914 rs.close();
915
916 return a;
917
918 } catch (e) {
919 exception_error("get_local_feed_unread", e);
920 }
921 }
922
923 function init_gears() {
924 try {
925
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');
930 db.open('tt-rss');
931
932 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
933
934 var rs = db.execute("SELECT schema_version FROM version");
935
936 var version = "";
937
938 if (rs.isValidRow()) {
939 version = rs.field(0);
940 }
941
942 rs.close();
943
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 (?)",
964 [SCHEMA_VERSION]);
965 }
966
967 db.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
968
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)");
975
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)");
981
982 db.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
983
984 db.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
985
986 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
987 "UPDATE OF unread ON articles "+
988 "BEGIN "+
989 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
990 "WHERE id = OLD.id AND "+
991 "OLD.unread != NEW.unread;"+
992 "END;");
993
994 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
995 "UPDATE OF marked ON articles "+
996 "BEGIN "+
997 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
998 "WHERE id = OLD.id;"+
999 "END;");
1000
1001 db.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1002 "DELETE ON articles "+
1003 "BEGIN "+
1004 "DELETE FROM article_labels WHERE id = OLD.id; "+
1005 "END; ");
1006
1007 init_local_sync_data();
1008
1009 Element.show("restartOfflinePic");
1010
1011 }
1012
1013 cache_expire();
1014
1015 } catch (e) {
1016 exception_error("init_gears", e);
1017 }
1018 }
1019
1020 function gotoOffline() {
1021 window.location.href = "tt-rss.php?offline=1";
1022 }
1023
1024 function gotoOnline() {
1025 window.location.href = "tt-rss.php";
1026 }
1027
1028 function local_collapse_cat(id) {
1029 try {
1030 if (db) {
1031 db.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1032 [id]);
1033 }
1034 } catch (e) {
1035 exception_error("local_collapse_cat", e);
1036 }
1037 }
1038
1039 function get_local_category_title(id) {
1040 try {
1041
1042 var rs = db.execute("SELECT title FROM categories WHERE id = ?", [id]);
1043 var tmp = "";
1044
1045 if (rs.isValidRow()) {
1046 tmp = rs.field(0);
1047 }
1048
1049 rs.close();
1050
1051 return tmp;
1052
1053 } catch (e) {
1054 exception_error("get_local_category_title", e);
1055 }
1056 }
1057
1058 function get_local_category_unread(id) {
1059 try {
1060 var rs = false;
1061
1062 if (id >= 0) {
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");
1068 } else {
1069 return 0;
1070 }
1071
1072 var tmp = 0;
1073
1074 if (rs.isValidRow()) {
1075 tmp = rs.field(0);
1076 }
1077
1078 rs.close();
1079
1080 return tmp;
1081
1082 } catch (e) {
1083 exception_error("get_local_category_unread", e);
1084 }
1085 }
1086
1087 function printCategoryHeader(cat_id, hidden, can_browse) {
1088 try {
1089 if (hidden == undefined) hidden = false;
1090 if (can_browse == undefined) can_browse = false;
1091
1092 var tmp_category = get_local_category_title(cat_id);
1093 var tmp = "";
1094
1095 var cat_unread = get_local_category_unread(cat_id);
1096
1097 var holder_style = "";
1098 var ellipsis = "";
1099
1100 if (hidden) {
1101 holder_style = "display:none;";
1102 ellipsis = "…";
1103 }
1104
1105 var catctr_class = (cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1106
1107 var browse_cat_link = "";
1108 var inner_title_class = "catTitleNL";
1109
1110 if (can_browse) {
1111 browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\"";
1112 inner_title_class = "catTitle";
1113 }
1114
1115 var cat_class = "feedCat";
1116
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>";
1123
1124 tmp += "<span id=\"FCAP-"+cat_id+"\">";
1125
1126 tmp += " <span id=\"FCATCTR-"+cat_id+"\" "+
1127 "class=\""+catctr_class+"\">("+cat_unread+")</span> "+ellipsis;
1128
1129 tmp += "</span>";
1130
1131 tmp += "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id+"\" "+
1132 "style='"+holder_style+"'>";
1133
1134 return tmp;
1135 } catch (e) {
1136 exception_error("printCategoryHeader", e);
1137 }
1138 }
1139
1140 function is_local_cat_collapsed(id) {
1141 try {
1142
1143 var rs = db.execute("SELECT collapsed FROM categories WHERE id = ?", [id]);
1144 var cat_hidden = 0;
1145
1146 if (rs.isValidRow()) {
1147 cat_hidden = rs.field(0);
1148 }
1149
1150 rs.close();
1151
1152 return cat_hidden == "1";
1153
1154 } catch (e) {
1155 exception_error("is_local_cat_collapsed", e);
1156 }
1157 }
1158
1159 function get_local_article_labels(id) {
1160 try {
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]);
1164
1165 var tmp = new Array();
1166
1167 while (rs.isValidRow()) {
1168 var e = new Array();
1169
1170 e[0] = rs.field(0);
1171 e[1] = rs.field(1);
1172 e[2] = rs.field(2);
1173 e[3] = rs.field(3);
1174
1175 tmp.push(e);
1176
1177 rs.next();
1178 }
1179
1180 return tmp;
1181
1182 } catch (e) {
1183 exception_error("get_local_article_labels", e);
1184 }
1185 }
1186
1187 function label_local_add_article(id, label_id) {
1188 try {
1189 //debug("label_local_add_article " + id + " => " + label_id);
1190
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);
1194
1195 if (rs.isValidRow()) {
1196 var check = rs.field(0);
1197 }
1198 rs.close();
1199
1200 if (check == 0) {
1201 db.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1202 "(?,?)", [id, label_id]);
1203 }
1204
1205 } catch (e) {
1206 exception_error("label_local_add_article", e);
1207 }
1208 }
1209
1210 function get_local_feed_title(id) {
1211 try {
1212
1213 var feed_title = "Unknown feed: " + id;
1214
1215 if (id > 0) {
1216 var rs = db.execute("SELECT title FROM feeds WHERE id = ?", [id]);
1217
1218 if (rs.isValidRow()) {
1219 feed_title = rs.field(0);
1220 }
1221
1222 rs.close();
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) {
1228
1229 var label_id = -11 - id;
1230
1231 var rs = db.execute("SELECT caption FROM labels WHERE id = ?", [label_id]);
1232
1233 if (rs.isValidRow()) {
1234 feed_title = rs.field(0);
1235 }
1236
1237 rs.close();
1238 }
1239
1240 return feed_title;
1241
1242 } catch (e) {
1243 exception_error("get_local_feed_title", e);
1244 }
1245 }
1246
1247 function format_article_labels(labels, id) {
1248 try {
1249
1250 var labels_str = "";
1251
1252 if (!labels) return "";
1253
1254 for (var i = 0; i < labels.length; i++) {
1255 var l = labels[i];
1256
1257 labels_str += "<span class='hlLabelRef' "+
1258 "style='color : "+l[2]+"; background-color : "+l[3]+"'>"+l[1]+"</span>";
1259 }
1260
1261 return labels_str;
1262
1263 } catch (e) {
1264 exception_error("format_article_labels", e);
1265 }
1266 }
1267
1268 function init_local_sync_data() {
1269 try {
1270 var rs = db.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1271 var has_last_online = 0;
1272
1273 if (rs.isValidRow()) {
1274 has_last_online = rs.field(0);
1275 }
1276
1277 rs.close();
1278
1279 if (!has_last_online) {
1280 db.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1281 }
1282
1283 } catch (e) {
1284 exception_error("init_local_sync_data", e);
1285
1286 }
1287 }
1288
1289 function has_local_sync_data() {
1290 try {
1291
1292 var rs = db.execute("SELECT id FROM articles "+
1293 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1294 "LIMIT 1");
1295
1296 var tmp = 0;
1297
1298 if (rs.isValidRow()) {
1299 tmp = rs.field(0);
1300 }
1301
1302 rs.close();
1303
1304 return tmp != 0;
1305
1306 } catch (e) {
1307 exception_error("has_local_sync_data", e);
1308 }
1309 }
1310
1311 function prepare_local_sync_data() {
1312 try {
1313 var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1314
1315 var last_online = "";
1316
1317 if (rs.isValidRow()) {
1318 last_online = rs.field(0);
1319 }
1320
1321 rs.close();
1322
1323 var rs = db.execute("SELECT id,unread,marked FROM articles "+
1324 "WHERE modified > ? LIMIT 200", [last_online]);
1325
1326 var tmp = last_online + ";";
1327
1328 var entries = 0;
1329
1330 while (rs.isValidRow()) {
1331 var e = new Array();
1332
1333 tmp = tmp + rs.field(0) + "," + rs.field(1) + "," + rs.field(2) + ";";
1334 entries++;
1335
1336 rs.next();
1337 }
1338
1339 rs.close();
1340
1341 if (entries > 0) {
1342 return tmp;
1343 } else {
1344 return '';
1345 }
1346
1347 } catch (e) {
1348 exception_error("prepare_local_sync_data", e);
1349 }
1350 }
1351
1352 function update_local_sync_data() {
1353 try {
1354 if (db && !offline_mode) {
1355
1356 var rs = db.execute("SELECT id FROM articles "+
1357 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1358 "key = 'last_online') LIMIT 1")
1359
1360 var f_id = 0;
1361
1362 if (rs.isValidRow()) {
1363 f_id = rs.field(0);
1364 }
1365
1366 rs.close();
1367
1368 /* no pending articles to sync */
1369
1370 if (f_id == 0) {
1371 db.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1372 "WHERE key = 'last_online'");
1373 }
1374
1375 }
1376 } catch (e) {
1377 exception_error("update_local_sync_data", e);
1378 }
1379 }
1380
1381 function catchup_local_feed(id, is_cat) {
1382 try {
1383 if (!db) return;
1384
1385 if (!is_cat) {
1386 if (id >= 0) {
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;
1394
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]);
1398 }
1399 }
1400
1401 update_local_feedlist_counters();
1402
1403 } catch (e) {
1404 exception_error("catchup_local_feed", e);
1405 }
1406 }