]> git.wh0rd.org - tt-rss.git/blob - offline.js
offline: only ask for offline mode when possible
[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 var articles_synced = 0;
8 var sync_in_progress = false;
9 var sync_timer = false;
10
11 function view_offline(id, feed_id) {
12 try {
13
14 enableHotkeys();
15 showArticleInHeadlines(id);
16
17 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
18
19 var rs = db.execute("SELECT * FROM articles WHERE id = ?", [id]);
20
21 if (rs.isValidRow()) {
22
23 var tmp = "<div class=\"postReply\">";
24
25 tmp += "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" "+
26 "onmouseout=\"enable_resize(false)\">";
27
28 tmp += "<div class=\"postDate\">"+rs.fieldByName("updated")+"</div>";
29
30 if (rs.fieldByName("link") != "") {
31 tmp += "<div clear='both'><a target=\"_blank\" "+
32 "href=\"" + rs.fieldByName("link") + "\">" +
33 rs.fieldByName("title") + "</a></div>";
34 } else {
35 tmp += "<div clear='both'>" + rs.fieldByName("title") + "</div>";
36 }
37
38 /* tmp += "<div style='float : right'> "+
39 "<img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
40 tmp += rs.fieldByName("tags");
41 tmp += "</div>"; */
42
43 /* tmp += "<div clear='both'>"+
44 "<a target=\"_blank\" "+
45 "href=\"" + rs.fieldByName("comments") + "\">" +
46 __("comments") + "</a></div>"; */
47
48 tmp += "</div>";
49
50 tmp += "<div class=\"postContent\">"
51 tmp += rs.fieldByName("content");
52 tmp += "</div>";
53
54 tmp += "</div>";
55
56 render_article(tmp);
57 update_local_feedlist_counters();
58 }
59
60 rs.close();
61
62 return false;
63
64 } catch (e) {
65 exception_error("view_offline", e);
66 }
67 }
68
69 function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, offset) {
70 try {
71 notify('');
72
73 if (!offset) offset = 0;
74
75 if (offset > 0) {
76 _feed_cur_page = parseInt(offset);
77 if (_infscroll_request_sent) {
78 return;
79 }
80 } else {
81 _feed_cur_page = 0;
82 _infscroll_disable = 0;
83 }
84
85 if (getActiveFeedId() != feed_id) {
86 _feed_cur_page = 0;
87 active_post_id = 0;
88 _infscroll_disable = 0;
89 }
90
91 loading_set_progress(100);
92
93 clean_feed_selections();
94
95 setActiveFeedId(feed_id, is_cat);
96
97 if (!is_cat) {
98 var feedr = $("FEEDR-" + feed_id);
99 if (feedr && !feedr.className.match("Selected")) {
100 feedr.className = feedr.className + "Selected";
101 }
102 } else {
103 var feedr = $("FCAT-" + feed_id);
104 if (feedr && !feedr.className.match("Selected")) {
105 feedr.className = feedr.className + "Selected";
106 }
107 }
108
109 if (subop == "MarkAllRead") {
110 catchup_local_feed(feed_id, is_cat);
111 }
112
113 disableContainerChildren("headlinesToolbar", false);
114 Form.enable("main_toolbar_form");
115
116 var f = $("headlines-frame");
117 try {
118 if (reply.offset == 0) {
119 debug("resetting headlines scrollTop");
120 f.scrollTop = 0;
121 }
122 } catch (e) { };
123
124
125 var tmp = "";
126 var feed_title = "";
127
128 if (is_cat) {
129 feed_title = get_local_category_title(feed_id);
130 } else {
131 feed_title = get_local_feed_title(feed_id);
132 }
133
134 if (feed_title) {
135
136 if (offset == 0) {
137 tmp += "<div id=\"headlinesContainer\">";
138
139 tmp += "<div class=\"headlinesSubToolbar\">";
140 tmp += "<div id=\"subtoolbar_ftitle\">";
141 tmp += feed_title;
142 tmp += "</div>";
143
144 var sel_all_link;
145 var sel_unread_link;
146 var sel_none_link;
147 var sel_inv_link;
148
149 if ($("content-frame")) {
150 sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
151 sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
152 sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
153 sel_inv_link = "javascript:invertHeadlineSelection()";
154 } else {
155 sel_all_link = "javascript:cdmSelectArticles('all')";
156 sel_unread_link = "javascript:cdmSelectArticles('unread')";
157 sel_none_link = "javascript:cdmSelectArticles('none')";
158 sel_inv_link = "javascript:invertHeadlineSelection()";
159 }
160
161 tmp += __('Select:')+
162 " <a href=\""+sel_all_link+"\">"+__('All')+"</a>, "+
163 "<a href=\""+sel_unread_link+"\">"+__('Unread')+"</a>, "+
164 "<a href=\""+sel_inv_link+"\">"+__('Invert')+"</a>, "+
165 "<a href=\""+sel_none_link+"\">"+__('None')+"</a>";
166
167 tmp += "&nbsp;&nbsp;";
168
169 tmp += "</div>";
170
171 tmp += "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
172 if ($("content-frame")) {
173 tmp += "<table class=\"headlinesList\" id=\"headlinesList\" cellspacing=\"0\">";
174 }
175
176 }
177
178 var limit = 30;
179
180 var toolbar_form = document.forms["main_toolbar_form"];
181
182 var limit = toolbar_form.limit[toolbar_form.limit.selectedIndex].value;
183 var view_mode = toolbar_form.view_mode[toolbar_form.view_mode.selectedIndex].value;
184
185 var limit_qpart = "";
186 var strategy_qpart = "";
187 var mode_qpart = "";
188 var offset_qpart = "";
189
190 if (limit != 0) {
191 limit_qpart = "LIMIT " + limit;
192 }
193
194 if (view_mode == "all_articles") {
195 mode_qpart = "1";
196 } else if (view_mode == "adaptive") {
197 if (is_cat && get_local_category_unread(feed_id) ||
198 get_local_feed_unread(feed_id) > 0) {
199 mode_qpart = "unread = 1";
200 } else {
201 mode_qpart = "1";
202 }
203
204 } else if (view_mode == "marked") {
205 mode_qpart = "marked = 1";
206 } else if (view_mode == "unread") {
207 mode_qpart = "unread = 1";
208 } else {
209 mode_qpart = "1";
210 }
211
212 var ext_tables_qpart = "";
213
214 if (is_cat) {
215 if (feed_id >= 0) {
216 strategy_qpart = "cat_id = " + feed_id;
217 } else if (feed_id == -2) {
218 strategy_qpart = "article_labels.id = articles.id";
219 ext_tables_qpart = ",article_labels";
220 }
221 } else if (feed_id > 0) {
222 strategy_qpart = "feed_id = " + feed_id;
223 } else if (feed_id == -1) {
224 strategy_qpart = "marked = 1";
225 } else if (feed_id == -4) {
226 strategy_qpart = "1";
227 } else if (feed_id < -10) {
228 var label_id = -11 - feed_id;
229 strategy_qpart = "article_labels.id = articles.id AND label_id = " + label_id;
230 ext_tables_qpart = ",article_labels";
231 }
232
233 if (offset > 0) {
234 offset_qpart = "OFFSET " + (offset*30);
235 } else {
236 offset_qpart = "";
237 }
238
239 var query = "SELECT *,feeds.title AS feed_title "+
240 "FROM articles,feeds,categories"+ext_tables_qpart+" "+
241 "WHERE " +
242 "cat_id = categories.id AND " +
243 "feed_id = feeds.id AND " +
244 strategy_qpart +
245 " AND " + mode_qpart +
246 " ORDER BY updated DESC "+
247 limit_qpart + " " +
248 offset_qpart;
249
250 var rs = db.execute(query);
251
252 var line_num = offset*30;
253
254 var real_feed_id = feed_id;
255
256 while (rs.isValidRow()) {
257
258 var id = rs.fieldByName("id");
259 var feed_id = rs.fieldByName("feed_id");
260
261 var entry_feed_title = false;
262
263 if (real_feed_id < 0 || is_cat) {
264 entry_feed_title = rs.fieldByName("feed_title");
265 }
266
267 var marked_pic;
268
269 var row_class = (line_num % 2) ? "even" : "odd";
270
271 if (rs.fieldByName("unread") == "1") {
272 row_class += "Unread";
273 }
274
275 var labels = get_local_article_labels(id);
276
277 var labels_str = "<span id=\"HLLCTR-"+id+"\">";
278 labels_str += format_article_labels(labels, id);
279 labels_str += "</span>";
280
281 if (rs.fieldByName("marked") == "1") {
282 marked_pic = "<img id=\"FMPIC-"+id+"\" "+
283 "src=\"images/mark_set.png\" class=\"markedPic\""+
284 "alt=\"Unstar article\" onclick='javascript:tMark("+id+")'>";
285 } else {
286 marked_pic = "<img id=\"FMPIC-"+id+"\" "+
287 "src=\"images/mark_unset.png\" class=\"markedPic\""+
288 "alt=\"Star article\" onclick='javascript:tMark("+id+")'>";
289 }
290
291 var mouseover_attrs = "onmouseover='postMouseIn($id)' "+
292 "onmouseout='postMouseOut($id)'";
293
294 var content_preview = truncate_string(strip_tags(rs.fieldByName("content")),
295 100);
296
297 if ($("content-frame")) {
298
299 tmp += "<tr class='"+row_class+"' id='RROW-"+id+"' "+mouseover_attrs+">";
300
301 tmp += "<td class='hlUpdPic'> </td>";
302
303 tmp += "<td class='hlSelectRow'>"+
304 "<input type=\"checkbox\" onclick=\"tSR(this)\" id=\"RCHK-"+id+"\"></td>";
305
306 tmp += "<td class='hlMarkedPic'>"+marked_pic+"</td>";
307
308 tmp += "<td onclick='view("+id+","+feed_id+")' "+
309 "class='hlContent' valign='middle'>";
310
311 tmp += "<a target=\"_blank\" id=\"RTITLE-"+id+"\" href=\"" +
312 rs.fieldByName("link") + "\"" +
313 "onclick=\"return view("+id+","+feed_id+");\">"+
314 rs.fieldByName("title");
315
316 tmp += "<span class=\"contentPreview\"> - "+content_preview+"</span>";
317
318 tmp += "</a>";
319
320 tmp += labels_str;
321
322 if (entry_feed_title) {
323 tmp += " <span class=\"hlFeed\">"+
324 "(<a href='javascript:viewfeed("+feed_id+
325 ")'>"+entry_feed_title+"</a>)</span>";
326 }
327
328 tmp += "</td>";
329
330 tmp += "<td class=\"hlUpdated\" onclick='view("+id+","+feed_id+")'>"+
331 "<nobr>"+rs.fieldByName("updated").substring(0,16)+
332 "</nobr></td>";
333
334 tmp += "</tr>";
335 } else {
336
337 var add_class = "";
338
339 if (rs.fieldByName("unread") == "1") {
340 add_class = "Unread";
341 }
342
343 tmp += "<div class=\"cdmArticle"+add_class+"\" id=\"RROW-"+id+"\" "+
344 mouseover_attrs+"'>";
345
346 feed_icon_img = "<img class=\"tinyFeedIcon\" src=\""+
347 getInitParam("icons_url")+"/"+feed_id+".ico\" alt=\"\">";
348 cdm_feed_icon = "<span style=\"cursor : pointer\" "+
349 "onclick=\"viewfeed("+feed_id+")\">"+feed_icon_img+"</span>";
350
351 tmp += "<div class=\"cdmHeader\">";
352 tmp += "<div class=\"articleUpdated\">"+
353 rs.fieldByName("updated").substring(0,16)+
354 " "+cdm_feed_icon+"</div>";
355
356 tmp += "<span id=\"RTITLE-"+id+"\" class=\"titleWrap\">"+
357 "<a class=\"title\" onclick=\"javascript:toggleUnread("+id+", 0)\""+
358 "target=\"_blank\" href=\""+rs.fieldByName("link")+
359 "\">"+rs.fieldByName("title")+"</a>";
360
361 tmp += labels_str;
362
363 if (entry_feed_title) {
364 tmp += "&nbsp;(<a href='javascript:viewfeed("+feed_id+
365 ")'>"+entry_feed_title+"</a>)";
366 }
367
368 tmp += "</span></div>";
369
370 tmp += "<div class=\"cdmContent\" onclick=\"cdmClicked("+id+")\""+
371 "id=\"CICD-"+id+"\">";
372 tmp += rs.fieldByName("content");
373 tmp += "<br clear='both'>"
374 tmp += "</div>";
375
376 tmp += "<div class=\"cdmFooter\"><span class='s0'>";
377 tmp += __("Select:")+
378 " <input type=\"checkbox\" "+
379 "onclick=\"toggleSelectRowById(this, 'RROW-"+id+"')\" "+
380 "class=\"feedCheckBox\" id=\"RCHK-"+id+"\">";
381
382 tmp += "</span><span class='s1'>"+marked_pic+"</span> ";
383
384 /* tmp += "<span class='s1'>"+
385 "<img class='tagsPic' src='images/tag.png' alt='Tags' title='Tags'>"+
386 "<span id=\"ATSTR-"+id+"\">"+rs.fieldByName("tags")+"</span>"+
387 "</span>"; */
388
389 tmp += "<span class='s2'>Toggle: <a class=\"cdmToggleLink\""+
390 "href=\"javascript:toggleUnread("+id+")\">"+
391 "Unread</a></span>";
392 tmp += "</div>";
393
394 tmp += "</div>";
395 }
396
397 rs.next();
398 line_num++;
399 }
400
401 if (line_num - offset*30 < 30) {
402 _infscroll_disable = 1;
403 }
404
405 rs.close();
406
407 if (offset == 0) {
408 tmp += "</table>";
409
410 if (line_num - offset*30 == 0) {
411 tmp += "<div class='whiteBox'>" +
412 __("No articles found to display.") +
413 "</div>";
414 }
415 tmp += "</div></div>";
416 }
417
418 if (offset == 0) {
419 var container = $("headlines-frame");
420 container.innerHTML = tmp;
421 } else {
422 var ids = getSelectedArticleIds2();
423
424 var container = $("headlinesList");
425 container.innerHTML = container.innerHTML + tmp;
426
427 for (var i = 0; i < ids.length; i++) {
428 markHeadline(ids[i]);
429 }
430 }
431 }
432
433 remove_splash();
434
435 _infscroll_request_sent = 0;
436
437 } catch (e) {
438 exception_error("viewfeed_offline", e);
439 }
440 }
441
442 function render_offline_feedlist() {
443 try {
444 var cats_enabled = getInitParam("enable_feed_cats") == "1";
445
446 var tmp = "<ul class=\"feedList\" id=\"feedList\">";
447
448 var unread = get_local_feed_unread(-4);
449
450 global_unread = unread;
451 updateTitle();
452
453 if (cats_enabled) {
454 tmp += printCategoryHeader(-1, is_local_cat_collapsed(-1), false);
455 }
456
457 tmp += printFeedEntry(-4, __("All articles"), "feed", unread,
458 "images/tag.png");
459
460 var unread = get_local_feed_unread(-1);
461
462 tmp += printFeedEntry(-1, __("Starred articles"), "feed", unread,
463 "images/mark_set.png");
464
465 if (cats_enabled) {
466 tmp += "</ul></li>";
467 } else {
468 tmp += "<li><hr/></li>";
469 }
470
471 if (cats_enabled) {
472 tmp += printCategoryHeader(-2, is_local_cat_collapsed(-2), false);
473 }
474
475 var rs = db.execute("SELECT id,caption "+
476 "FROM labels "+
477 "ORDER BY caption");
478
479 while (rs.isValidRow()) {
480 var id = -11 - parseInt(rs.field(0));
481 var caption = rs.field(1);
482 var unread = get_local_feed_unread(id);
483
484 tmp += printFeedEntry(id, caption, "feed", unread,
485 "images/label.png");
486
487 rs.next();
488 }
489
490 rs.close();
491
492 if (cats_enabled) {
493 tmp += "</ul></li>";
494 } else {
495 tmp += "<li><hr/></li>";
496 }
497
498 /* var rs = db.execute("SELECT feeds.id,feeds.title,has_icon,COUNT(articles.id) "+
499 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
500 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
501 "ORDER BY feeds.title"); */
502
503 var order_by = "feeds.title";
504
505 if (cats_enabled) order_by = "categories.title," + order_by;
506
507 var rs = db.execute("SELECT "+
508 "feeds.id,feeds.title,has_icon,cat_id,collapsed "+
509 "FROM feeds,categories WHERE cat_id = categories.id "+
510 "ORDER BY "+order_by);
511
512 var tmp_cat_id = -1;
513
514 while (rs.isValidRow()) {
515
516 var id = rs.field(0);
517 var title = rs.field(1);
518 var has_icon = rs.field(2);
519 var unread = get_local_feed_unread(id);
520 var cat_id = rs.field(3);
521 var cat_hidden = rs.field(4);
522
523 if (cat_id != tmp_cat_id && cats_enabled) {
524 if (tmp_cat_id != -1) {
525 tmp += "</ul></li>";
526 }
527 tmp += printCategoryHeader(cat_id, cat_hidden, true);
528 tmp_cat_id = cat_id;
529 }
530
531 var icon = "";
532
533 if (has_icon) {
534 icon = getInitParam("icons_url") + "/" + id + ".ico";
535 }
536
537 var feed_icon = "";
538
539 var row_class = "feed";
540
541 if (unread > 0) {
542 row_class += "Unread";
543 fctr_class = "feedCtrHasUnread";
544 } else {
545 fctr_class = "feedCtrNoUnread";
546 }
547
548 tmp += printFeedEntry(id, title, "feed", unread, icon);
549
550 rs.next();
551 }
552
553 rs.close();
554
555 if (cats_enabled) {
556 tmp += "</ul>";
557 }
558
559 tmp += "</ul>";
560
561 render_feedlist(tmp);
562 } catch (e) {
563 exception_error("render_offline_feedlist", e);
564 }
565 }
566
567 function init_offline() {
568 try {
569 offline_mode = true;
570 store.enabled = true;
571
572 Element.hide("dispSwitchPrompt");
573 Element.hide("feedBrowserPrompt");
574
575 Element.hide("topLinksOnline");
576 Element.show("topLinksOffline");
577
578 var tb_form = $("main_toolbar_form");
579 Element.hide(tb_form.update);
580
581 var chooser = $("quickMenuChooser");
582 chooser.disabled = true;
583
584 var rs = db.execute("SELECT key, value FROM init_params");
585
586 while (rs.isValidRow()) {
587 init_params[rs.field(0)] = rs.field(1);
588 rs.next();
589 }
590
591 rs.close();
592
593 var rs = db.execute("SELECT COUNT(*) FROM feeds");
594
595 var num_feeds = 0;
596
597 if (rs.isValidRow()) {
598 num_feeds = rs.field(0);
599 }
600
601 rs.close();
602
603 if (num_feeds == 0) {
604 remove_splash();
605 return fatalError(0,
606 __("Data for offline browsing has not been downloaded yet."));
607 }
608
609 render_offline_feedlist();
610 init_second_stage();
611 window.setTimeout("viewfeed(-4)", 50);
612
613 } catch (e) {
614 exception_error("init_offline", e);
615 }
616 }
617
618 function offline_download_parse(stage, transport) {
619 try {
620 if (transport.responseXML) {
621
622 if (!sync_in_progress) return;
623
624 var sync_ok = transport.responseXML.getElementsByTagName("sync-ok");
625
626 if (sync_ok.length > 0) {
627 for (var i = 0; i < sync_ok.length; i++) {
628 var id = sync_ok[i].getAttribute("id");
629 if (id) {
630 debug("synced offline info for id " + id);
631 db.execute("UPDATE articles SET modified = '' WHERE id = ?", [id]);
632 }
633 }
634 }
635
636 if (stage == 0) {
637
638 $("offlineModeSyncMsg").innerHTML = __("Synchronizing feeds...");
639
640 var feeds = transport.responseXML.getElementsByTagName("feed");
641
642 if (feeds.length > 0) {
643 db.execute("DELETE FROM feeds");
644 }
645
646 for (var i = 0; i < feeds.length; i++) {
647 var id = feeds[i].getAttribute("id");
648 var has_icon = feeds[i].getAttribute("has_icon");
649 var title = feeds[i].firstChild.nodeValue;
650 var cat_id = feeds[i].getAttribute("cat_id");
651
652 db.execute("INSERT INTO feeds (id,title,has_icon,cat_id)"+
653 "VALUES (?,?,?,?)",
654 [id, title, has_icon, cat_id]);
655 }
656
657 $("offlineModeSyncMsg").innerHTML = __("Synchronizing categories...");
658
659 var cats = transport.responseXML.getElementsByTagName("category");
660
661 if (feeds.length > 0) {
662 db.execute("DELETE FROM categories");
663 }
664
665 for (var i = 0; i < cats.length; i++) {
666 var id = cats[i].getAttribute("id");
667 var collapsed = cats[i].getAttribute("collapsed");
668 var title = cats[i].firstChild.nodeValue;
669
670 db.execute("INSERT INTO categories (id,title,collapsed)"+
671 "VALUES (?,?,?)",
672 [id, title, collapsed]);
673 }
674
675 $("offlineModeSyncMsg").innerHTML = __("Synchronizing labels...");
676
677 var labels = transport.responseXML.getElementsByTagName("label");
678
679 if (labels.length > 0) {
680 db.execute("DELETE FROM labels");
681 }
682
683 for (var i = 0; i < labels.length; i++) {
684 var id = labels[i].getAttribute("id");
685 var fg_color = labels[i].getAttribute("fg_color");
686 var bg_color = labels[i].getAttribute("bg_color");
687 var caption = labels[i].firstChild.nodeValue;
688
689 db.execute("INSERT INTO labels (id,caption,fg_color,bg_color)"+
690 "VALUES (?,?,?,?)",
691 [id, caption, fg_color, bg_color]);
692 }
693
694 $("offlineModeSyncMsg").innerHTML = __("Synchronizing articles...");
695
696 sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")", 2*1000);
697 } else {
698
699 var articles = transport.responseXML.getElementsByTagName("article");
700
701 var limit = transport.responseXML.getElementsByTagName("limit")[0];
702
703 if (limit) {
704 limit = limit.getAttribute("value");
705 } else {
706 limit = 0;
707 }
708
709 var articles_found = 0;
710
711 for (var i = 0; i < articles.length; i++) {
712 var a = eval("("+articles[i].firstChild.nodeValue+")");
713 articles_found++;
714 if (a) {
715
716 db.execute("DELETE FROM articles WHERE id = ?", [a.id]);
717
718 db.execute("INSERT INTO articles "+
719 "(id, feed_id, title, link, guid, updated, content, "+
720 "unread, marked, tags, comments) "+
721 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
722 [a.id, a.feed_id, a.title, a.link, a.guid, a.updated,
723 a.content, a.unread, a.marked, a.tags,
724 a.comments]);
725
726 if (a.labels.length > 0) {
727 for (var j = 0; j < a.labels.length; j++) {
728 label_local_add_article(a.id, a.labels[j][0]);
729 }
730 }
731
732 }
733 }
734
735 debug("downloaded articles: " + articles_found + " limit: " + limit);
736
737 articles_synced += articles_found;
738
739 var msg =__("Synchronizing articles (%d)...").replace("%d", articles_synced);
740
741 $("offlineModeSyncMsg").innerHTML = msg;
742
743 var has_sync_data = has_local_sync_data();
744
745 if (articles_found >= limit || has_sync_data) {
746 sync_timer = window.setTimeout("update_offline_data("+(stage+1)+")",
747 3*1000);
748 debug("<b>update_offline_data: done " + stage + " HSD: " +
749 has_sync_data + "</b>");
750 } else {
751 window.setTimeout("offlineDownloadStart()", 180*1000);
752 debug("update_offline_data: finished");
753
754 var pic = $("offlineModePic");
755
756 if (pic) {
757 pic.src = "images/offline.png";
758
759 var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
760 var last_sync = "";
761
762 if (rs.isValidRow()) {
763 last_sync = rs.field(0).substring(0,16);
764 }
765 rs.close();
766
767 var msg = __("Last sync: %s").replace("%s", last_sync);
768
769 articles_synced = 0;
770
771 $("offlineModeSyncMsg").innerHTML = msg;
772 }
773
774 offlineSyncShowHideElems(false);
775
776 sync_in_progress = false;
777
778 db.execute("DELETE FROM articles WHERE "+
779 "updated < DATETIME('NOW', 'localtime', '-31 days')");
780
781 }
782 }
783
784 update_local_sync_data();
785
786
787 // notify('');
788
789 } else {
790 sync_in_progress = false;
791
792 var pic = $("offlineModePic");
793
794 if (pic) {
795 pic.src = "images/offline.png";
796 var msg = __("Last sync: Error receiving data.");
797 articles_synced = 0;
798 $("offlineModeSyncMsg").innerHTML = msg;
799 }
800
801 offlineSyncShowHideElems(false);
802 }
803
804 } catch (e) {
805 exception_error("offline_download_parse", e);
806 }
807 }
808
809 function update_offline_data(stage) {
810 try {
811
812 if (!stage) stage = 0;
813
814 if (!db || offline_mode || getInitParam("offline_enabled") != "1") return;
815
816 // notify_progress("Updating offline data... (" + stage +")", true);
817
818 var query = "backend.php?op=rpc&subop=download";
819
820 var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
821
822 if (rs.isValidRow() && rs.field(0)) {
823 var offline_dl_max_id = rs.field(0);
824 var offline_dl_min_id = rs.field(1);
825
826 query = query + "&cidt=" + offline_dl_max_id;
827 query = query + "&cidb=" + offline_dl_min_id;
828
829 stage = 1;
830 }
831
832 rs.close();
833
834 debug("update_offline_data: stage " + stage);
835
836 query = query + "&stage=" + stage;
837
838 var to_sync = prepare_local_sync_data();
839
840 if (to_sync != "") {
841 to_sync = "?sync=" + param_escape(to_sync);
842 }
843
844 debug(query + "/" + to_sync);
845
846 var pic = $("offlineModePic");
847
848 if (pic) {
849 pic.src = "images/offline-sync.gif";
850 if (articles_synced == 0) {
851 $("offlineModeSyncMsg").innerHTML = __("Synchronizing...");
852 }
853 }
854
855 offlineSyncShowHideElems(true);
856 offlineUpdateStore();
857
858 sync_in_progress = true;
859
860 new Ajax.Request(query, {
861 parameters: to_sync,
862 onComplete: function(transport) {
863 offline_download_parse(stage, transport);
864 } });
865
866 } catch (e) {
867 exception_error("initiate_offline_download", e);
868 }
869 }
870
871 function set_feedlist_counter(id, ctr, is_cat) {
872 try {
873
874 var feedctr = $("FEEDCTR-" + id);
875 var feedu = $("FEEDU-" + id);
876 var feedr = $("FEEDR-" + id);
877
878 if (is_cat) {
879 var catctr = $("FCATCTR-" + id);
880 if (catctr) {
881 catctr.innerHTML = "(" + ctr + ")";
882 if (ctr > 0) {
883 catctr.className = "catCtrHasUnread";
884 } else {
885 catctr.className = "catCtrNoUnread";
886 }
887 }
888 } else if (feedctr && feedu && feedr) {
889
890 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
891
892 feedu.innerHTML = ctr;
893
894 if (ctr > 0) {
895 feedctr.className = "feedCtrHasUnread";
896 if (!feedr.className.match("Unread")) {
897 var is_selected = feedr.className.match("Selected");
898
899 feedr.className = feedr.className.replace("Selected", "");
900 feedr.className = feedr.className.replace("Unread", "");
901
902 feedr.className = feedr.className + "Unread";
903
904 if (is_selected) {
905 feedr.className = feedr.className + "Selected";
906 }
907
908 }
909
910 if (row_needs_hl) {
911 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
912 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
913 }
914 } else {
915 feedctr.className = "feedCtrNoUnread";
916 feedr.className = feedr.className.replace("Unread", "");
917 }
918 }
919
920 } catch (e) {
921 exception_error("set_feedlist_counter", e);
922 }
923 }
924
925 function update_local_feedlist_counters() {
926 try {
927 if (!offline_mode || !db) return;
928
929 /* var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
930 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
931 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
932 "ORDER BY feeds.title"); */
933
934 var rs = db.execute("SELECT id FROM feeds "+
935 "ORDER BY title");
936
937 while (rs.isValidRow()) {
938 var id = rs.field(0);
939 var ctr = get_local_feed_unread(id);
940 set_feedlist_counter(id, ctr, false);
941 rs.next();
942 }
943
944 rs.close();
945
946 var rs = db.execute("SELECT cat_id,SUM(unread) "+
947 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
948
949 while (rs.isValidRow()) {
950 var id = rs.field(0);
951 var ctr = rs.field(1);
952 set_feedlist_counter(id, ctr, true);
953 rs.next();
954 }
955
956 rs.close();
957
958 set_feedlist_counter(-2, get_local_category_unread(-2), true);
959
960 set_feedlist_counter(-4, get_local_feed_unread(-4));
961 set_feedlist_counter(-1, get_local_feed_unread(-1));
962
963 var rs = db.execute("SELECT id FROM labels");
964
965 while (rs.isValidRow()) {
966 var id = -11 - rs.field(0);
967 var ctr = get_local_feed_unread(id);
968 set_feedlist_counter(id, ctr, false);
969 rs.next();
970 }
971
972 rs.close();
973
974 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
975
976 global_unread = get_local_feed_unread(-4);
977 updateTitle();
978
979 } catch (e) {
980 exception_error("update_local_feedlist_counters", e);
981 }
982 }
983
984 function get_local_feed_unread(id) {
985 try {
986 var rs;
987
988 if (id == -4) {
989 rs = db.execute("SELECT SUM(unread) FROM articles");
990 } else if (id == -1) {
991 rs = db.execute("SELECT SUM(unread) FROM articles WHERE marked = 1");
992 } else if (id > 0) {
993 rs = db.execute("SELECT SUM(unread) FROM articles WHERE feed_id = ?", [id]);
994 } else if (id < -10) {
995 var label_id = -11 - id;
996 rs = db.execute("SELECT SUM(unread) FROM articles,article_labels "+
997 "WHERE article_labels.id = articles.id AND label_id = ?", [label_id]);
998 }
999
1000 var a = false;
1001
1002 if (rs.isValidRow()) {
1003 a = rs.field(0);
1004 } else {
1005 a = 0;
1006 }
1007
1008 rs.close();
1009
1010 return a;
1011
1012 } catch (e) {
1013 exception_error("get_local_feed_unread", e);
1014 }
1015 }
1016
1017 function enable_offline_reading() {
1018 try {
1019
1020 if (db && getInitParam("offline_enabled") == "1") {
1021 init_local_sync_data();
1022 Element.show("offlineModePic");
1023 offlineDownloadStart();
1024 }
1025
1026 } catch (e) {
1027 exception_error("enable_offline_reading", e);
1028 }
1029 }
1030
1031 function init_gears() {
1032 try {
1033
1034 if (window.google && google.gears) {
1035
1036 try {
1037 localServer = google.gears.factory.create("beta.localserver");
1038 } catch (e) {
1039 return;
1040 }
1041
1042 store = localServer.createManagedStore("tt-rss");
1043 store.manifestUrl = "manifest.json.php";
1044 store.enabled = false;
1045
1046 db = google.gears.factory.create('beta.database');
1047 db.open('tt-rss');
1048
1049 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1050
1051 var rs = db.execute("SELECT schema_version FROM version");
1052
1053 var version = "";
1054
1055 if (rs.isValidRow()) {
1056 version = rs.field(0);
1057 }
1058
1059 rs.close();
1060
1061 if (version != SCHEMA_VERSION) {
1062 db.execute("DROP TABLE IF EXISTS init_params");
1063 db.execute("DROP TABLE IF EXISTS cache");
1064 db.execute("DROP TABLE IF EXISTS feeds");
1065 db.execute("DROP TABLE IF EXISTS categories");
1066 db.execute("DROP TABLE IF EXISTS labels");
1067 db.execute("DROP TABLE IF EXISTS article_labels");
1068 db.execute("DROP TABLE IF EXISTS articles");
1069 db.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
1070 db.execute("DROP INDEX IF EXISTS articles_unread_idx");
1071 db.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
1072 db.execute("DROP INDEX IF EXISTS articles_id_idx");
1073 db.execute("DROP INDEX IF EXISTS article_labels_id_idx");
1074 db.execute("DROP TABLE IF EXISTS version");
1075 db.execute("DROP TRIGGER IF EXISTS articles_update_unread");
1076 db.execute("DROP TRIGGER IF EXISTS articles_update_marked");
1077 db.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
1078 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
1079 db.execute("DROP TABLE IF EXISTS syncdata");
1080 db.execute("INSERT INTO version (schema_version) VALUES (?)",
1081 [SCHEMA_VERSION]);
1082 }
1083
1084 db.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
1085
1086 db.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
1087 db.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
1088 db.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
1089 db.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
1090 db.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
1091 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)");
1092
1093 db.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
1094 db.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
1095 db.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
1096 db.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
1097 db.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
1098
1099 db.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
1100
1101 db.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
1102
1103 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
1104 "UPDATE OF unread ON articles "+
1105 "BEGIN "+
1106 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1107 "WHERE id = OLD.id AND "+
1108 "OLD.unread != NEW.unread;"+
1109 "END;");
1110
1111 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
1112 "UPDATE OF marked ON articles "+
1113 "BEGIN "+
1114 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1115 "WHERE id = OLD.id;"+
1116 "END;");
1117
1118 db.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1119 "DELETE ON articles "+
1120 "BEGIN "+
1121 "DELETE FROM article_labels WHERE id = OLD.id; "+
1122 "END; ");
1123
1124 }
1125
1126 cache_expire();
1127
1128 } catch (e) {
1129 exception_error("init_gears", e);
1130 }
1131 }
1132
1133 function offlineArticlesStored() {
1134
1135 var rs = db.execute("SELECT COUNT(*) FROM articles");
1136 var count = 0;
1137 if (rs.isValidRow()) {
1138 count = rs.field(0);
1139 }
1140
1141 rs.close();
1142
1143 return count;
1144 }
1145
1146 function gotoOffline() {
1147
1148 // debug("[Local store] currentVersion = " + store.currentVersion);
1149
1150
1151 if (offlineArticlesStored() == 0) {
1152 notify_error("You have to synchronize some articles before going into offline mode.");
1153 return;
1154 }
1155
1156 if (confirm(__("Switch Tiny Tiny RSS into offline mode?"))) {
1157
1158 store.enabled = true;
1159 store.checkForUpdate();
1160
1161 notify_progress("Preparing offline mode...", true);
1162
1163 var timerId = window.setInterval(function() {
1164 if (store.currentVersion) {
1165 window.clearInterval(timerId);
1166 debug("[Local store] sync complete: " + store.currentVersion);
1167
1168 //window.location.href = "tt-rss.php";
1169
1170 offlineDownloadStop();
1171 init_offline();
1172
1173 notify_info("Tiny Tiny RSS is in offline mode.");
1174
1175 } else if (store.updateStatus == 3) {
1176 debug("[Local store] sync error: " + store.lastErrorMessage);
1177 notify_error(store.lastErrorMessage, true);
1178 } }, 500);
1179 }
1180 }
1181
1182 function gotoOnline() {
1183 // if (confirm(__("You won't be able to access offline version of Tiny Tiny RSS until you switch it into offline mode again. Go online?"))) {
1184 if (confirm(__("Tiny Tiny RSS will reload. Go online?"))) {
1185 //localServer.removeManagedStore("tt-rss");
1186 window.location.href = "tt-rss.php";
1187 }
1188 }
1189
1190 function local_collapse_cat(id) {
1191 try {
1192 if (db) {
1193 db.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1194 [id]);
1195 }
1196 } catch (e) {
1197 exception_error("local_collapse_cat", e);
1198 }
1199 }
1200
1201 function get_local_category_title(id) {
1202 try {
1203
1204 var rs = db.execute("SELECT title FROM categories WHERE id = ?", [id]);
1205 var tmp = "";
1206
1207 if (rs.isValidRow()) {
1208 tmp = rs.field(0);
1209 }
1210
1211 rs.close();
1212
1213 return tmp;
1214
1215 } catch (e) {
1216 exception_error("get_local_category_title", e);
1217 }
1218 }
1219
1220 function get_local_category_unread(id) {
1221 try {
1222 var rs = false;
1223
1224 if (id >= 0) {
1225 rs = db.execute("SELECT SUM(unread) FROM articles, feeds "+
1226 "WHERE feeds.id = feed_id AND cat_id = ?", [id]);
1227 } else if (id == -2) {
1228 rs = db.execute("SELECT SUM(unread) FROM article_labels, articles "+
1229 "where article_labels.id = articles.id");
1230 } else {
1231 return 0;
1232 }
1233
1234 var tmp = 0;
1235
1236 if (rs.isValidRow()) {
1237 tmp = rs.field(0);
1238 }
1239
1240 rs.close();
1241
1242 return tmp;
1243
1244 } catch (e) {
1245 exception_error("get_local_category_unread", e);
1246 }
1247 }
1248
1249 function printCategoryHeader(cat_id, hidden, can_browse) {
1250 try {
1251 if (hidden == undefined) hidden = false;
1252 if (can_browse == undefined) can_browse = false;
1253
1254 var tmp_category = get_local_category_title(cat_id);
1255 var tmp = "";
1256
1257 var cat_unread = get_local_category_unread(cat_id);
1258
1259 var holder_style = "";
1260 var ellipsis = "";
1261
1262 if (hidden) {
1263 holder_style = "display:none;";
1264 ellipsis = "…";
1265 }
1266
1267 var catctr_class = (cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1268
1269 var browse_cat_link = "";
1270 var inner_title_class = "catTitleNL";
1271
1272 if (can_browse) {
1273 browse_cat_link = "onclick=\"javascript:viewCategory("+cat_id+")\"";
1274 inner_title_class = "catTitle";
1275 }
1276
1277 var cat_class = "feedCat";
1278
1279 tmp += "<li class=\""+cat_class+"\" id=\"FCAT-"+cat_id+"\">"+
1280 "<img onclick=\"toggleCollapseCat("+cat_id+")\" class=\"catCollapse\""+
1281 " title=\""+__('Click to collapse category')+"\""+
1282 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class+"\" "+
1283 " id=\"FCATN-"+cat_id+"\" "+browse_cat_link+
1284 "\">"+tmp_category+"</span>";
1285
1286 tmp += "<span id=\"FCAP-"+cat_id+"\">";
1287
1288 tmp += " <span id=\"FCATCTR-"+cat_id+"\" "+
1289 "class=\""+catctr_class+"\">("+cat_unread+")</span> "+ellipsis;
1290
1291 tmp += "</span>";
1292
1293 tmp += "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id+"\" "+
1294 "style='"+holder_style+"'>";
1295
1296 return tmp;
1297 } catch (e) {
1298 exception_error("printCategoryHeader", e);
1299 }
1300 }
1301
1302 function is_local_cat_collapsed(id) {
1303 try {
1304
1305 var rs = db.execute("SELECT collapsed FROM categories WHERE id = ?", [id]);
1306 var cat_hidden = 0;
1307
1308 if (rs.isValidRow()) {
1309 cat_hidden = rs.field(0);
1310 }
1311
1312 rs.close();
1313
1314 return cat_hidden == "1";
1315
1316 } catch (e) {
1317 exception_error("is_local_cat_collapsed", e);
1318 }
1319 }
1320
1321 function get_local_article_labels(id) {
1322 try {
1323 var rs = db.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1324 "FROM labels, article_labels "+
1325 "WHERE labels.id = label_id AND article_labels.id = ?", [id]);
1326
1327 var tmp = new Array();
1328
1329 while (rs.isValidRow()) {
1330 var e = new Array();
1331
1332 e[0] = rs.field(0);
1333 e[1] = rs.field(1);
1334 e[2] = rs.field(2);
1335 e[3] = rs.field(3);
1336
1337 tmp.push(e);
1338
1339 rs.next();
1340 }
1341
1342 return tmp;
1343
1344 } catch (e) {
1345 exception_error("get_local_article_labels", e);
1346 }
1347 }
1348
1349 function label_local_add_article(id, label_id) {
1350 try {
1351 //debug("label_local_add_article " + id + " => " + label_id);
1352
1353 var rs = db.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1354 "id = ? AND label_id = ?", [id, label_id]);
1355 var check = rs.field(0);
1356
1357 if (rs.isValidRow()) {
1358 var check = rs.field(0);
1359 }
1360 rs.close();
1361
1362 if (check == 0) {
1363 db.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1364 "(?,?)", [id, label_id]);
1365 }
1366
1367 } catch (e) {
1368 exception_error("label_local_add_article", e);
1369 }
1370 }
1371
1372 function get_local_feed_title(id) {
1373 try {
1374
1375 var feed_title = "Unknown feed: " + id;
1376
1377 if (id > 0) {
1378 var rs = db.execute("SELECT title FROM feeds WHERE id = ?", [id]);
1379
1380 if (rs.isValidRow()) {
1381 feed_title = rs.field(0);
1382 }
1383
1384 rs.close();
1385 } else if (id == -1) {
1386 feed_title = __("Starred articles");
1387 } else if (id == -4) {
1388 feed_title = __("All articles");
1389 } else if (id < -10) {
1390
1391 var label_id = -11 - id;
1392
1393 var rs = db.execute("SELECT caption FROM labels WHERE id = ?", [label_id]);
1394
1395 if (rs.isValidRow()) {
1396 feed_title = rs.field(0);
1397 }
1398
1399 rs.close();
1400 }
1401
1402 return feed_title;
1403
1404 } catch (e) {
1405 exception_error("get_local_feed_title", e);
1406 }
1407 }
1408
1409 function format_article_labels(labels, id) {
1410 try {
1411
1412 var labels_str = "";
1413
1414 if (!labels) return "";
1415
1416 for (var i = 0; i < labels.length; i++) {
1417 var l = labels[i];
1418
1419 labels_str += "<span class='hlLabelRef' "+
1420 "style='color : "+l[2]+"; background-color : "+l[3]+"'>"+l[1]+"</span>";
1421 }
1422
1423 return labels_str;
1424
1425 } catch (e) {
1426 exception_error("format_article_labels", e);
1427 }
1428 }
1429
1430 function init_local_sync_data() {
1431 try {
1432
1433 if (!db) return;
1434
1435 var rs = db.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1436 var has_last_online = 0;
1437
1438 if (rs.isValidRow()) {
1439 has_last_online = rs.field(0);
1440 }
1441
1442 rs.close();
1443
1444 if (!has_last_online) {
1445 db.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1446 }
1447
1448 } catch (e) {
1449 exception_error("init_local_sync_data", e);
1450
1451 }
1452 }
1453
1454 function has_local_sync_data() {
1455 try {
1456
1457 var rs = db.execute("SELECT id FROM articles "+
1458 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1459 "LIMIT 1");
1460
1461 var tmp = 0;
1462
1463 if (rs.isValidRow()) {
1464 tmp = rs.field(0);
1465 }
1466
1467 rs.close();
1468
1469 return tmp != 0;
1470
1471 } catch (e) {
1472 exception_error("has_local_sync_data", e);
1473 }
1474 }
1475
1476 function prepare_local_sync_data() {
1477 try {
1478 var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1479
1480 var last_online = "";
1481
1482 if (rs.isValidRow()) {
1483 last_online = rs.field(0);
1484 }
1485
1486 rs.close();
1487
1488 var rs = db.execute("SELECT id,unread,marked FROM articles "+
1489 "WHERE modified > ? LIMIT 200", [last_online]);
1490
1491 var tmp = last_online + ";";
1492
1493 var entries = 0;
1494
1495 while (rs.isValidRow()) {
1496 var e = new Array();
1497
1498 tmp = tmp + rs.field(0) + "," + rs.field(1) + "," + rs.field(2) + ";";
1499 entries++;
1500
1501 rs.next();
1502 }
1503
1504 rs.close();
1505
1506 if (entries > 0) {
1507 return tmp;
1508 } else {
1509 return '';
1510 }
1511
1512 } catch (e) {
1513 exception_error("prepare_local_sync_data", e);
1514 }
1515 }
1516
1517 function update_local_sync_data() {
1518 try {
1519 if (db && !offline_mode) {
1520
1521 var rs = db.execute("SELECT id FROM articles "+
1522 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1523 "key = 'last_online') LIMIT 1")
1524
1525 var f_id = 0;
1526
1527 if (rs.isValidRow()) {
1528 f_id = rs.field(0);
1529 }
1530
1531 rs.close();
1532
1533 /* no pending articles to sync */
1534
1535 if (f_id == 0) {
1536 db.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1537 "WHERE key = 'last_online'");
1538 }
1539
1540 }
1541 } catch (e) {
1542 exception_error("update_local_sync_data", e);
1543 }
1544 }
1545
1546 function catchup_local_feed(id, is_cat) {
1547 try {
1548 if (!db) return;
1549
1550 if (!is_cat) {
1551 if (id >= 0) {
1552 db.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id]);
1553 } else if (id == -1) {
1554 db.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1555 } else if (id == -4) {
1556 db.execute("UPDATE articles SET unread = 0");
1557 } else if (id < -10) {
1558 var label_id = -11-id;
1559
1560 db.execute("UPDATE articles SET unread = 0 WHERE "+
1561 "(SELECT COUNT(*) FROM article_labels WHERE "+
1562 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id]);
1563 }
1564 }
1565
1566 update_local_feedlist_counters();
1567
1568 } catch (e) {
1569 exception_error("catchup_local_feed", e);
1570 }
1571 }
1572
1573 function toggleOfflineModeInfo() {
1574 try {
1575 var e = $('offlineModeDrop');
1576 var p = $('offlineModePic');
1577
1578 if (Element.visible(e)) {
1579 Element.hide(e);
1580 } else {
1581 Element.show(e);
1582 }
1583
1584 } catch (e) {
1585 exception_error("toggleOfflineModeInfo", e);
1586 }
1587 }
1588
1589 function offlineDownloadStart() {
1590 try {
1591 if (db && !sync_in_progress && getInitParam("offline_enabled") == "1") {
1592 window.setTimeout("update_offline_data(0)", 100);
1593 }
1594 } catch (e) {
1595 exception_error("offlineDownloadStart", e);
1596 }
1597 }
1598
1599 function offlineDownloadStop() {
1600 try {
1601 if (db && sync_in_progress && getInitParam("offline_enabled") == "1") {
1602
1603 sync_in_progress = false;
1604
1605 if (sync_timer) {
1606 window.clearTimeout(sync_timer);
1607 sync_timer = false;
1608 }
1609
1610 var pic = $("offlineModePic");
1611
1612 if (pic) {
1613 pic.src = "images/offline.png";
1614 var msg = __("Last sync: Cancelled.");
1615 articles_synced = 0;
1616 $("offlineModeSyncMsg").innerHTML = msg;
1617 }
1618
1619 offlineSyncShowHideElems(false);
1620
1621 }
1622 } catch (e) {
1623 exception_error("offlineDownloadStart", e);
1624 }
1625 }
1626
1627 function offlineClearData() {
1628 try {
1629 if (db) {
1630
1631 if (confirm(__("This will remove all offline data stored by Tiny Tiny RSS on this computer. Continue?"))) {
1632
1633 notify_progress("Removing offline data...");
1634
1635 localServer.removeManagedStore("tt-rss");
1636
1637 db.execute("DELETE FROM articles");
1638 db.execute("DELETE FROM article_labels");
1639 db.execute("DELETE FROM labels");
1640 db.execute("DELETE FROM feeds");
1641 db.execute("DELETE FROM cache");
1642
1643 notify_info("Local data removed.");
1644 }
1645 }
1646 } catch (e) {
1647 exception_error("offlineClearData", e);
1648 }
1649 }
1650
1651 function offlineUpdateStore() {
1652 try {
1653 if (offline_mode || !store) return;
1654
1655 store.checkForUpdate();
1656
1657 var timerId = window.setInterval(function() {
1658 if (store.currentVersion) {
1659 window.clearInterval(timerId);
1660 debug("[Local store] sync complete: " + store.currentVersion);
1661 } else if (store.updateStatus == 3) {
1662 debug("[Local store] sync error: " + store.lastErrorMessage);
1663 } }, 500);
1664
1665 } catch (e) {
1666 exception_error("offlineUpdateStore", e);
1667 }
1668 }
1669
1670 function offlineSyncShowHideElems(syncing) {
1671 try {
1672
1673 var elems = $$("div.hideWhenSyncing");
1674
1675 for (var j = 0; j < elems.length; j++) {
1676 if (syncing) {
1677 Element.hide(elems[j]);
1678 } else {
1679 Element.show(elems[j]);
1680 }
1681 }
1682
1683 var elems = $$("div.showWhenSyncing");
1684
1685 for (var j = 0; j < elems.length; j++) {
1686 if (syncing) {
1687 Element.show(elems[j]);
1688 } else {
1689 Element.hide(elems[j]);
1690 }
1691 }
1692
1693 } catch (e) {
1694 exception_error("offlineSyncShowHideElems", e);
1695 }
1696 }
1697
1698 function offlineConfirmModeChange() {
1699 try {
1700
1701 if (db && getInitParam("offline_enabled") == "1" && offlineArticlesStored() > 0) {
1702 var ok = confirm(__("Tiny Tiny RSS has trouble accessing its server. Would you like to go offline?"));
1703
1704 if (ok) {
1705 init_offline();
1706 }
1707 }
1708
1709 } catch (e) {
1710 exception_error("offlineConfirmModeChange", e);
1711 }
1712 }