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