]> git.wh0rd.org - tt-rss.git/blob - offline.js
offline: automatically set version of manifest.json (using filemtime)
[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 = $("FEEDR-" + feed_id);
96 if (feedr && !feedr.className.match("Selected")) {
97 feedr.className = feedr.className + "Selected";
98 }
99 } else {
100 var feedr = $("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 = $("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 var feed_title = "";
124
125 if (is_cat) {
126 feed_title = get_local_category_title(feed_id);
127 } else {
128 feed_title = get_local_feed_title(feed_id);
129 }
130
131 if (feed_title) {
132
133 if (offset == 0) {
134 tmp += "<div id=\"headlinesContainer\">";
135
136 tmp += "<div class=\"headlinesSubToolbar\">";
137 tmp += "<div id=\"subtoolbar_ftitle\">";
138 tmp += feed_title;
139 tmp += "</div>";
140
141 var sel_all_link;
142 var sel_unread_link;
143 var sel_none_link;
144 var sel_inv_link;
145
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()";
151 } else {
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()";
156 }
157
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>";
163
164 tmp += "&nbsp;&nbsp;";
165
166 tmp += "</div>";
167
168 tmp += "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
169 if ($("content-frame")) {
170 tmp += "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
171 }
172
173 }
174
175 var limit = 30;
176
177 var toolbar_form = document.forms["main_toolbar_form"];
178
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;
181
182 var limit_qpart = "";
183 var strategy_qpart = "";
184 var mode_qpart = "";
185 var offset_qpart = "";
186
187 if (limit != 0) {
188 limit_qpart = "LIMIT " + limit;
189 }
190
191 if (view_mode == "all_articles") {
192 mode_qpart = "1";
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";
197 } else {
198 mode_qpart = "1";
199 }
200
201 } else if (view_mode == "marked") {
202 mode_qpart = "marked = 1";
203 } else if (view_mode == "unread") {
204 mode_qpart = "unread = 1";
205 } else {
206 mode_qpart = "1";
207 }
208
209 var ext_tables_qpart = "";
210
211 if (is_cat) {
212 if (feed_id >= 0) {
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";
217 }
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";
228 }
229
230 if (offset > 0) {
231 offset_qpart = "OFFSET " + (offset*30);
232 } else {
233 offset_qpart = "";
234 }
235
236 var query = "SELECT *,feeds.title AS feed_title "+
237 "FROM articles,feeds,categories"+ext_tables_qpart+" "+
238 "WHERE " +
239 "cat_id = categories.id AND " +
240 "feed_id = feeds.id AND " +
241 strategy_qpart +
242 " AND " + mode_qpart +
243 " ORDER BY updated DESC "+
244 limit_qpart + " " +
245 offset_qpart;
246
247 var rs = db.execute(query);
248
249 var line_num = offset*30;
250
251 var real_feed_id = feed_id;
252
253 while (rs.isValidRow()) {
254
255 var id = rs.fieldByName("id");
256 var feed_id = rs.fieldByName("feed_id");
257
258 var entry_feed_title = false;
259
260 if (real_feed_id < 0 || is_cat) {
261 entry_feed_title = rs.fieldByName("feed_title");
262 }
263
264 var marked_pic;
265
266 var row_class = (line_num % 2) ? "even" : "odd";
267
268 if (rs.fieldByName("unread") == "1") {
269 row_class += "Unread";
270 }
271
272 var labels = get_local_article_labels(id);
273
274 var labels_str = "<span id=\"HLLCTR-"+id+"\">";
275 labels_str += format_article_labels(labels, id);
276 labels_str += "</span>";
277
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+")'>";
282 } else {
283 marked_pic = "<img id=\"FMPIC-"+id+"\" "+
284 "src=\"images/mark_unset.png\" class=\"markedPic\""+
285 "alt=\"Star article\" onclick='javascript:tMark("+id+")'>";
286 }
287
288 var mouseover_attrs = "onmouseover='postMouseIn($id)' "+
289 "onmouseout='postMouseOut($id)'";
290
291 var content_preview = truncate_string(strip_tags(rs.fieldByName("content")),
292 100);
293
294 if ($("content-frame")) {
295
296 tmp += "<tr class='"+row_class+"' id='RROW-"+id+"' "+mouseover_attrs+">";
297
298 tmp += "<td class='hlUpdPic'> </td>";
299
300 tmp += "<td class='hlSelectRow'>"+
301 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id+"\"></td>";
302
303 tmp += "<td class='hlMarkedPic'>"+marked_pic+"</td>";
304
305 tmp += "<td onclick='view("+id+","+feed_id+")' "+
306 "class='hlContent' valign='middle'>";
307
308 tmp += "<a target=\"_blank\" id=\"RTITLE-"+id+"\" href=\"" +
309 rs.fieldByName("link") + "\"" +
310 "onclick=\"return view("+id+","+feed_id+");\">"+
311 rs.fieldByName("title");
312
313 tmp += "<span class=\"contentPreview\"> - "+content_preview+"</span>";
314
315 tmp += "</a>";
316
317 tmp += labels_str;
318
319 if (entry_feed_title) {
320 tmp += " <span class=\"hlFeed\">"+
321 "(<a href='javascript:viewfeed("+feed_id+
322 ")'>"+entry_feed_title+"</a>)</span>";
323 }
324
325 tmp += "</td>";
326
327 tmp += "<td class=\"hlUpdated\" onclick='view("+id+","+feed_id+")'>"+
328 "<nobr>"+rs.fieldByName("updated").substring(0,16)+
329 "</nobr></td>";
330
331 tmp += "</tr>";
332 } else {
333
334 var add_class = "";
335
336 if (rs.fieldByName("unread") == "1") {
337 add_class = "Unread";
338 }
339
340 tmp += "<div class=\"cdmArticle"+add_class+"\" id=\"RROW-"+id+"\" "+
341 mouseover_attrs+"'>";
342
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>";
347
348 tmp += "<div class=\"cdmHeader\">";
349 tmp += "<div class=\"articleUpdated\">"+
350 rs.fieldByName("updated").substring(0,16)+
351 " "+cdm_feed_icon+"</div>";
352
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>";
357
358 tmp += labels_str;
359
360 if (entry_feed_title) {
361 tmp += "&nbsp;(<a href='javascript:viewfeed("+feed_id+
362 ")'>"+entry_feed_title+"</a>)";
363 }
364
365 tmp += "</span></div>";
366
367 tmp += "<div class=\"cdmContent\" onclick=\"cdmClicked("+id+")\""+
368 "id=\"CICD-"+id+"\">";
369 tmp += rs.fieldByName("content");
370 tmp += "<br clear='both'>"
371 tmp += "</div>";
372
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+"\">";
378
379 tmp += "</span><span class='s1'>"+marked_pic+"</span> ";
380
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>"+
384 "</span>"; */
385
386 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
387 "href=\"javascript:toggleUnread("+id+")\">"+
388 "Unread</a></span>";
389 tmp += "</div>";
390
391 tmp += "</div>";
392 }
393
394 rs.next();
395 line_num++;
396 }
397
398 if (line_num - offset*30 < 30) {
399 _infscroll_disable = 1;
400 }
401
402 rs.close();
403
404 if (offset == 0) {
405 tmp += "</table>";
406
407 if (line_num - offset*30 == 0) {
408 tmp += "<div class='whiteBox'>" +
409 __("No articles found to display.") +
410 "</div>";
411 }
412 tmp += "</div></div>";
413 }
414
415 if (offset == 0) {
416 var container = $("headlines-frame");
417 container.innerHTML = tmp;
418 } else {
419 var ids = getSelectedArticleIds2();
420
421 var container = $("headlinesList");
422 container.innerHTML = container.innerHTML + tmp;
423
424 for (var i = 0; i < ids.length; i++) {
425 markHeadline(ids[i]);
426 }
427 }
428 }
429
430 remove_splash();
431
432 _infscroll_request_sent = 0;
433
434 } catch (e) {
435 exception_error("viewfeed_offline", e);
436 }
437 }
438
439 function render_offline_feedlist() {
440 try {
441 var cats_enabled = getInitParam("enable_feed_cats") == "1";
442
443 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
444
445 var unread = get_local_feed_unread(-4);
446
447 global_unread = unread;
448 updateTitle();
449
450 if (cats_enabled) {
451 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
452 }
453
454 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
455 "images/tag.png");
456
457 var unread = get_local_feed_unread(-1);
458
459 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
460 "images/mark_set.png");
461
462 if (cats_enabled) {
463 tmp += "</ul></li>";
464 } else {
465 tmp += "<li><hr/></li>";
466 }
467
468 if (cats_enabled) {
469 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
470 }
471
472 var rs = db.execute("SELECT id,caption "+
473 "FROM labels "+
474 "ORDER BY caption");
475
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);
480
481 tmp += printFeedEntry(id, caption, "feed", unread,
482 "images/label.png");
483
484 rs.next();
485 }
486
487 rs.close();
488
489 if (cats_enabled) {
490 tmp += "</ul></li>";
491 } else {
492 tmp += "<li><hr/></li>";
493 }
494
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"); */
499
500 var order_by = "feeds.title";
501
502 if (cats_enabled) order_by = "categories.title," + order_by;
503
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);
508
509 var tmp_cat_id = -1;
510
511 while (rs.isValidRow()) {
512
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);
519
520 if (cat_id != tmp_cat_id && cats_enabled) {
521 if (tmp_cat_id != -1) {
522 tmp += "</ul></li>";
523 }
524 tmp += printCategoryHeader(cat_id, cat_hidden, true);
525 tmp_cat_id = cat_id;
526 }
527
528 var icon = "";
529
530 if (has_icon) {
531 icon = getInitParam("icons_url") + "/" + id + ".ico";
532 }
533
534 var feed_icon = "";
535
536 var row_class = "feed";
537
538 if (unread > 0) {
539 row_class += "Unread";
540 fctr_class = "feedCtrHasUnread";
541 } else {
542 fctr_class = "feedCtrNoUnread";
543 }
544
545 tmp += printFeedEntry(id, title, "feed", unread, icon);
546
547 rs.next();
548 }
549
550 rs.close();
551
552 if (cats_enabled) {
553 tmp += "</ul>";
554 }
555
556 tmp += "</ul>";
557
558 render_feedlist(tmp);
559 } catch (e) {
560 exception_error("render_offline_feedlist", e);
561 }
562 }
563
564 function init_offline() {
565 try {
566 offline_mode = true;
567
568 Element.hide("dispSwitchPrompt");
569 Element.hide("feedBrowserPrompt");
570
571 Element.hide("topLinksOnline");
572 Element.show("topLinksOffline");
573
574 var tb_form = $("main_toolbar_form");
575 Element.hide(tb_form.update);
576
577 var chooser = $("quickMenuChooser");
578 chooser.disabled = true;
579
580 var rs = db.execute("SELECT key, value FROM init_params");
581
582 while (rs.isValidRow()) {
583 init_params[rs.field(0)] = rs.field(1);
584 rs.next();
585 }
586
587 rs.close();
588
589 var rs = db.execute("SELECT COUNT(*) FROM feeds");
590
591 var num_feeds = 0;
592
593 if (rs.isValidRow()) {
594 num_feeds = rs.field(0);
595 }
596
597 rs.close();
598
599 if (num_feeds == 0) {
600 remove_splash();
601 return fatalError(0,
602 __("Data for offline browsing has not been downloaded yet."));
603 }
604
605 render_offline_feedlist();
606 init_second_stage();
607 window.setTimeout("viewfeed(-4)", 50);
608
609 } catch (e) {
610 exception_error("init_offline", e);
611 }
612 }
613
614 function offline_download_parse(stage, transport) {
615 try {
616 if (transport.responseXML) {
617
618 var sync_ok = transport.responseXML.getElementsByTagName("sync-ok");
619
620 if (sync_ok.length > 0) {
621 for (var i = 0; i < sync_ok.length; i++) {
622 var id = sync_ok[i].getAttribute("id");
623 if (id) {
624 debug("synced offline info for id " + id);
625 db.execute("UPDATE articles SET modified = '' WHERE id = ?", [id]);
626 }
627 }
628 }
629
630 if (stage == 0) {
631
632 var feeds = transport.responseXML.getElementsByTagName("feed");
633
634 if (feeds.length > 0) {
635 db.execute("DELETE FROM feeds");
636 }
637
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");
643
644 db.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
645 "VALUES (?,?,?,?)",
646 [id, title, has_icon, cat_id]);
647 }
648
649 var cats = transport.responseXML.getElementsByTagName("category");
650
651 if (feeds.length > 0) {
652 db.execute("DELETE FROM categories");
653 }
654
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;
659
660 db.execute("INSERT INTO categories (id,title,collapsed)"+
661 "VALUES (?,?,?)",
662 [id, title, collapsed]);
663 }
664
665 var labels = transport.responseXML.getElementsByTagName("label");
666
667 if (labels.length > 0) {
668 db.execute("DELETE FROM labels");
669 }
670
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;
676
677 db.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
678 "VALUES (?,?,?,?)",
679 [id, caption, fg_color, bg_color]);
680 }
681
682 window.setTimeout("update_offline_data("+(stage+1)+")", 5*1000);
683 } else {
684
685 var articles = transport.responseXML.getElementsByTagName("article");
686
687 var limit = transport.responseXML.getElementsByTagName("limit")[0];
688
689 if (limit) {
690 limit = limit.getAttribute("value");
691 } else {
692 limit = 0;
693 }
694
695 var articles_found = 0;
696
697 for (var i = 0; i < articles.length; i++) {
698 var a = eval("("+articles[i].firstChild.nodeValue+")");
699 articles_found++;
700 if (a) {
701
702 db.execute("DELETE FROM articles WHERE id = ?", [a.id]);
703
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,
710 a.comments]);
711
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]);
715 }
716 }
717
718 }
719 }
720
721 debug("downloaded articles: " + articles_found + " limit: " + limit);
722
723 var has_sync_data = has_local_sync_data();
724
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>");
729 } else {
730 window.setTimeout("update_offline_data(0)", 180*1000);
731 debug("update_offline_data: finished");
732
733 var pic = $("restartOfflinePic");
734
735 if (pic) {
736 pic.src = "images/offline.png";
737 pic.title = __("Restart in offline mode");
738 }
739
740 db.execute("DELETE FROM articles WHERE "+
741 "updated < DATETIME('NOW', 'localtime', '-31 days')");
742
743 }
744 }
745
746 update_local_sync_data();
747
748
749 // notify('');
750
751 }
752 } catch (e) {
753 exception_error("offline_download_parse", e);
754 }
755 }
756
757 function update_offline_data(stage) {
758 try {
759
760 if (!stage) stage = 0;
761 if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
762
763 // notify_progress("Updating offline data... (" + stage +")", true);
764
765 var query = "backend.php?op=rpc&subop=download";
766
767 var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
768
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);
772
773 query = query + "&cidt=" + offline_dl_max_id;
774 query = query + "&cidb=" + offline_dl_min_id;
775
776 stage = 1;
777 }
778
779 rs.close();
780
781 debug("update_offline_data: stage " + stage);
782
783 query = query + "&stage=" + stage;
784
785 var to_sync = prepare_local_sync_data();
786
787 if (to_sync != "") {
788 to_sync = "?sync=" + param_escape(to_sync);
789 }
790
791 debug(query + "/" + to_sync);
792
793 var pic = $("restartOfflinePic");
794
795 if (pic) {
796 pic.src = "images/offline-sync.gif";
797 pic.title = __("Synchronizing offline data...");
798 }
799
800 new Ajax.Request(query, {
801 parameters: to_sync,
802 onComplete: function(transport) {
803 offline_download_parse(stage, transport);
804 } });
805
806 } catch (e) {
807 exception_error("initiate_offline_download", e);
808 }
809 }
810
811 function set_feedlist_counter(id, ctr, is_cat) {
812 try {
813
814 var feedctr = $("FEEDCTR-" + id);
815 var feedu = $("FEEDU-" + id);
816 var feedr = $("FEEDR-" + id);
817
818 if (is_cat) {
819 var catctr = $("FCATCTR-" + id);
820 if (catctr) {
821 catctr.innerHTML = "(" + ctr + ")";
822 if (ctr > 0) {
823 catctr.className = "catCtrHasUnread";
824 } else {
825 catctr.className = "catCtrNoUnread";
826 }
827 }
828 } else if (feedctr && feedu && feedr) {
829
830 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
831
832 feedu.innerHTML = ctr;
833
834 if (ctr > 0) {
835 feedctr.className = "feedCtrHasUnread";
836 if (!feedr.className.match("Unread")) {
837 var is_selected = feedr.className.match("Selected");
838
839 feedr.className = feedr.className.replace("Selected", "");
840 feedr.className = feedr.className.replace("Unread", "");
841
842 feedr.className = feedr.className + "Unread";
843
844 if (is_selected) {
845 feedr.className = feedr.className + "Selected";
846 }
847
848 }
849
850 if (row_needs_hl) {
851 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
852 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
853 }
854 } else {
855 feedctr.className = "feedCtrNoUnread";
856 feedr.className = feedr.className.replace("Unread", "");
857 }
858 }
859
860 } catch (e) {
861 exception_error("set_feedlist_counter", e);
862 }
863 }
864
865 function update_local_feedlist_counters() {
866 try {
867 if (!offline_mode || !db) return;
868
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"); */
873
874 var rs = db.execute("SELECT id FROM feeds "+
875 "ORDER BY title");
876
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);
881 rs.next();
882 }
883
884 rs.close();
885
886 var rs = db.execute("SELECT cat_id,SUM(unread) "+
887 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
888
889 while (rs.isValidRow()) {
890 var id = rs.field(0);
891 var ctr = rs.field(1);
892 set_feedlist_counter(id, ctr, true);
893 rs.next();
894 }
895
896 rs.close();
897
898 set_feedlist_counter(-2, get_local_category_unread(-2), true);
899
900 set_feedlist_counter(-4, get_local_feed_unread(-4));
901 set_feedlist_counter(-1, get_local_feed_unread(-1));
902
903 var rs = db.execute("SELECT id FROM labels");
904
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);
909 rs.next();
910 }
911
912 rs.close();
913
914 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
915
916 global_unread = get_local_feed_unread(-4);
917 updateTitle();
918
919 } catch (e) {
920 exception_error("update_local_feedlist_counters", e);
921 }
922 }
923
924 function get_local_feed_unread(id) {
925 try {
926 var rs;
927
928 if (id == -4) {
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");
932 } else if (id > 0) {
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]);
938 }
939
940 var a = false;
941
942 if (rs.isValidRow()) {
943 a = rs.field(0);
944 } else {
945 a = 0;
946 }
947
948 rs.close();
949
950 return a;
951
952 } catch (e) {
953 exception_error("get_local_feed_unread", e);
954 }
955 }
956
957 function enable_offline_reading() {
958 try {
959
960 if (db && getInitParam("offline_enabled") == "1") {
961 init_local_sync_data();
962 Element.show("restartOfflinePic");
963 window.setTimeout("update_offline_data(0)", 100);
964 }
965
966 } catch (e) {
967 exception_error("enable_offline_reading", e);
968 }
969 }
970
971 function init_gears() {
972 try {
973
974 if (window.google && google.gears) {
975 localServer = google.gears.factory.create("beta.localserver");
976 store = localServer.createManagedStore("tt-rss");
977 store.manifestUrl = "manifest.json.php";
978 store.checkForUpdate();
979
980 db = google.gears.factory.create('beta.database');
981 db.open('tt-rss');
982
983 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
984
985 var rs = db.execute("SELECT schema_version FROM version");
986
987 var version = "";
988
989 if (rs.isValidRow()) {
990 version = rs.field(0);
991 }
992
993 rs.close();
994
995 if (version != SCHEMA_VERSION) {
996 db.execute("DROP TABLE IF EXISTS init_params");
997 db.execute("DROP TABLE IF EXISTS cache");
998 db.execute("DROP TABLE IF EXISTS feeds");
999 db.execute("DROP TABLE IF EXISTS categories");
1000 db.execute("DROP TABLE IF EXISTS labels");
1001 db.execute("DROP TABLE IF EXISTS article_labels");
1002 db.execute("DROP TABLE IF EXISTS articles");
1003 db.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
1004 db.execute("DROP INDEX IF EXISTS articles_unread_idx");
1005 db.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
1006 db.execute("DROP INDEX IF EXISTS articles_id_idx");
1007 db.execute("DROP INDEX IF EXISTS article_labels_id_idx");
1008 db.execute("DROP TABLE IF EXISTS version");
1009 db.execute("DROP TRIGGER IF EXISTS articles_update_unread");
1010 db.execute("DROP TRIGGER IF EXISTS articles_update_marked");
1011 db.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
1012 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1013 db.execute("DROP TABLE IF EXISTS syncdata");
1014 db.execute("INSERT INTO version (schema_version) VALUES (?)",
1015 [SCHEMA_VERSION]);
1016 }
1017
1018 db.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
1019
1020 db.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
1021 db.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
1022 db.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
1023 db.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
1024 db.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
1025 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)");
1026
1027 db.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
1028 db.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
1029 db.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
1030 db.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
1031 db.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
1032
1033 db.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
1034
1035 db.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
1036
1037 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
1038 "UPDATE OF unread ON articles "+
1039 "BEGIN "+
1040 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1041 "WHERE id = OLD.id AND "+
1042 "OLD.unread != NEW.unread;"+
1043 "END;");
1044
1045 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
1046 "UPDATE OF marked ON articles "+
1047 "BEGIN "+
1048 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1049 "WHERE id = OLD.id;"+
1050 "END;");
1051
1052 db.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1053 "DELETE ON articles "+
1054 "BEGIN "+
1055 "DELETE FROM article_labels WHERE id = OLD.id; "+
1056 "END; ");
1057
1058 }
1059
1060 cache_expire();
1061
1062 } catch (e) {
1063 exception_error("init_gears", e);
1064 }
1065 }
1066
1067 function gotoOffline() {
1068 window.location.href = "tt-rss.php?offline=1";
1069 }
1070
1071 function gotoOnline() {
1072 window.location.href = "tt-rss.php";
1073 }
1074
1075 function local_collapse_cat(id) {
1076 try {
1077 if (db) {
1078 db.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1079 [id]);
1080 }
1081 } catch (e) {
1082 exception_error("local_collapse_cat", e);
1083 }
1084 }
1085
1086 function get_local_category_title(id) {
1087 try {
1088
1089 var rs = db.execute("SELECT title FROM categories WHERE id = ?", [id]);
1090 var tmp = "";
1091
1092 if (rs.isValidRow()) {
1093 tmp = rs.field(0);
1094 }
1095
1096 rs.close();
1097
1098 return tmp;
1099
1100 } catch (e) {
1101 exception_error("get_local_category_title", e);
1102 }
1103 }
1104
1105 function get_local_category_unread(id) {
1106 try {
1107 var rs = false;
1108
1109 if (id >= 0) {
1110 rs = db.execute("SELECT SUM(unread) FROM articles, feeds "+
1111 "WHERE feeds.id = feed_id AND cat_id = ?", [id]);
1112 } else if (id == -2) {
1113 rs = db.execute("SELECT SUM(unread) FROM article_labels, articles "+
1114 "where article_labels.id = articles.id");
1115 } else {
1116 return 0;
1117 }
1118
1119 var tmp = 0;
1120
1121 if (rs.isValidRow()) {
1122 tmp = rs.field(0);
1123 }
1124
1125 rs.close();
1126
1127 return tmp;
1128
1129 } catch (e) {
1130 exception_error("get_local_category_unread", e);
1131 }
1132 }
1133
1134 function printCategoryHeader(cat_id, hidden, can_browse) {
1135 try {
1136 if (hidden == undefined) hidden = false;
1137 if (can_browse == undefined) can_browse = false;
1138
1139 var tmp_category = get_local_category_title(cat_id);
1140 var tmp = "";
1141
1142 var cat_unread = get_local_category_unread(cat_id);
1143
1144 var holder_style = "";
1145 var ellipsis = "";
1146
1147 if (hidden) {
1148 holder_style = "display:none;";
1149 ellipsis = "…";
1150 }
1151
1152 var catctr_class = (cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1153
1154 var browse_cat_link = "";
1155 var inner_title_class = "catTitleNL";
1156
1157 if (can_browse) {
1158 browse_cat_link = "onclick=\"javascript:viewCategory("+cat_id+")\"";
1159 inner_title_class = "catTitle";
1160 }
1161
1162 var cat_class = "feedCat";
1163
1164 tmp += "<li class=\""+cat_class+"\" id=\"FCAT-"+cat_id+"\">"+
1165 "<img onclick=\"toggleCollapseCat("+cat_id+")\" class=\"catCollapse\""+
1166 " title=\""+__('Click to collapse category')+"\""+
1167 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class+"\" "+
1168 " id=\"FCATN-"+cat_id+"\" "+browse_cat_link+
1169 "\">"+tmp_category+"</span>";
1170
1171 tmp += "<span id=\"FCAP-"+cat_id+"\">";
1172
1173 tmp += " <span id=\"FCATCTR-"+cat_id+"\" "+
1174 "class=\""+catctr_class+"\">("+cat_unread+")</span> "+ellipsis;
1175
1176 tmp += "</span>";
1177
1178 tmp += "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id+"\" "+
1179 "style='"+holder_style+"'>";
1180
1181 return tmp;
1182 } catch (e) {
1183 exception_error("printCategoryHeader", e);
1184 }
1185 }
1186
1187 function is_local_cat_collapsed(id) {
1188 try {
1189
1190 var rs = db.execute("SELECT collapsed FROM categories WHERE id = ?", [id]);
1191 var cat_hidden = 0;
1192
1193 if (rs.isValidRow()) {
1194 cat_hidden = rs.field(0);
1195 }
1196
1197 rs.close();
1198
1199 return cat_hidden == "1";
1200
1201 } catch (e) {
1202 exception_error("is_local_cat_collapsed", e);
1203 }
1204 }
1205
1206 function get_local_article_labels(id) {
1207 try {
1208 var rs = db.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1209 "FROM labels, article_labels "+
1210 "WHERE labels.id = label_id AND article_labels.id = ?", [id]);
1211
1212 var tmp = new Array();
1213
1214 while (rs.isValidRow()) {
1215 var e = new Array();
1216
1217 e[0] = rs.field(0);
1218 e[1] = rs.field(1);
1219 e[2] = rs.field(2);
1220 e[3] = rs.field(3);
1221
1222 tmp.push(e);
1223
1224 rs.next();
1225 }
1226
1227 return tmp;
1228
1229 } catch (e) {
1230 exception_error("get_local_article_labels", e);
1231 }
1232 }
1233
1234 function label_local_add_article(id, label_id) {
1235 try {
1236 //debug("label_local_add_article " + id + " => " + label_id);
1237
1238 var rs = db.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1239 "id = ? AND label_id = ?", [id, label_id]);
1240 var check = rs.field(0);
1241
1242 if (rs.isValidRow()) {
1243 var check = rs.field(0);
1244 }
1245 rs.close();
1246
1247 if (check == 0) {
1248 db.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1249 "(?,?)", [id, label_id]);
1250 }
1251
1252 } catch (e) {
1253 exception_error("label_local_add_article", e);
1254 }
1255 }
1256
1257 function get_local_feed_title(id) {
1258 try {
1259
1260 var feed_title = "Unknown feed: " + id;
1261
1262 if (id > 0) {
1263 var rs = db.execute("SELECT title FROM feeds WHERE id = ?", [id]);
1264
1265 if (rs.isValidRow()) {
1266 feed_title = rs.field(0);
1267 }
1268
1269 rs.close();
1270 } else if (id == -1) {
1271 feed_title = __("Starred articles");
1272 } else if (id == -4) {
1273 feed_title = __("All articles");
1274 } else if (id < -10) {
1275
1276 var label_id = -11 - id;
1277
1278 var rs = db.execute("SELECT caption FROM labels WHERE id = ?", [label_id]);
1279
1280 if (rs.isValidRow()) {
1281 feed_title = rs.field(0);
1282 }
1283
1284 rs.close();
1285 }
1286
1287 return feed_title;
1288
1289 } catch (e) {
1290 exception_error("get_local_feed_title", e);
1291 }
1292 }
1293
1294 function format_article_labels(labels, id) {
1295 try {
1296
1297 var labels_str = "";
1298
1299 if (!labels) return "";
1300
1301 for (var i = 0; i < labels.length; i++) {
1302 var l = labels[i];
1303
1304 labels_str += "<span class='hlLabelRef' "+
1305 "style='color : "+l[2]+"; background-color : "+l[3]+"'>"+l[1]+"</span>";
1306 }
1307
1308 return labels_str;
1309
1310 } catch (e) {
1311 exception_error("format_article_labels", e);
1312 }
1313 }
1314
1315 function init_local_sync_data() {
1316 try {
1317
1318 if (!db) return;
1319
1320 var rs = db.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1321 var has_last_online = 0;
1322
1323 if (rs.isValidRow()) {
1324 has_last_online = rs.field(0);
1325 }
1326
1327 rs.close();
1328
1329 if (!has_last_online) {
1330 db.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1331 }
1332
1333 } catch (e) {
1334 exception_error("init_local_sync_data", e);
1335
1336 }
1337 }
1338
1339 function has_local_sync_data() {
1340 try {
1341
1342 var rs = db.execute("SELECT id FROM articles "+
1343 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1344 "LIMIT 1");
1345
1346 var tmp = 0;
1347
1348 if (rs.isValidRow()) {
1349 tmp = rs.field(0);
1350 }
1351
1352 rs.close();
1353
1354 return tmp != 0;
1355
1356 } catch (e) {
1357 exception_error("has_local_sync_data", e);
1358 }
1359 }
1360
1361 function prepare_local_sync_data() {
1362 try {
1363 var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1364
1365 var last_online = "";
1366
1367 if (rs.isValidRow()) {
1368 last_online = rs.field(0);
1369 }
1370
1371 rs.close();
1372
1373 var rs = db.execute("SELECT id,unread,marked FROM articles "+
1374 "WHERE modified > ? LIMIT 200", [last_online]);
1375
1376 var tmp = last_online + ";";
1377
1378 var entries = 0;
1379
1380 while (rs.isValidRow()) {
1381 var e = new Array();
1382
1383 tmp = tmp + rs.field(0) + "," + rs.field(1) + "," + rs.field(2) + ";";
1384 entries++;
1385
1386 rs.next();
1387 }
1388
1389 rs.close();
1390
1391 if (entries > 0) {
1392 return tmp;
1393 } else {
1394 return '';
1395 }
1396
1397 } catch (e) {
1398 exception_error("prepare_local_sync_data", e);
1399 }
1400 }
1401
1402 function update_local_sync_data() {
1403 try {
1404 if (db && !offline_mode) {
1405
1406 var rs = db.execute("SELECT id FROM articles "+
1407 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1408 "key = 'last_online') LIMIT 1")
1409
1410 var f_id = 0;
1411
1412 if (rs.isValidRow()) {
1413 f_id = rs.field(0);
1414 }
1415
1416 rs.close();
1417
1418 /* no pending articles to sync */
1419
1420 if (f_id == 0) {
1421 db.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1422 "WHERE key = 'last_online'");
1423 }
1424
1425 }
1426 } catch (e) {
1427 exception_error("update_local_sync_data", e);
1428 }
1429 }
1430
1431 function catchup_local_feed(id, is_cat) {
1432 try {
1433 if (!db) return;
1434
1435 if (!is_cat) {
1436 if (id >= 0) {
1437 db.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id]);
1438 } else if (id == -1) {
1439 db.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1440 } else if (id == -4) {
1441 db.execute("UPDATE articles SET unread = 0");
1442 } else if (id < -10) {
1443 var label_id = -11-id;
1444
1445 db.execute("UPDATE articles SET unread = 0 WHERE "+
1446 "(SELECT COUNT(*) FROM article_labels WHERE "+
1447 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id]);
1448 }
1449 }
1450
1451 update_local_feedlist_counters();
1452
1453 } catch (e) {
1454 exception_error("catchup_local_feed", e);
1455 }
1456 }