]> git.wh0rd.org - tt-rss.git/blob - offline.js
add missing schema files
[tt-rss.git] / offline.js
1 var SCHEMA_VERSION = 10;
2
3 var offline_mode = false;
4 var store = false;
5 var localServer = false;
6 var db = false;
7
8 function view_offline(id, feed_id) {
9 try {
10
11 enableHotkeys();
12 showArticleInHeadlines(id);
13
14 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
15
16 var rs = db.execute("SELECT * FROM articles WHERE id = ?", [id]);
17
18 if (rs.isValidRow()) {
19
20 var tmp = "<div class=\"postReply\">";
21
22 tmp += "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\" "+
23 "onmouseout=\"enable_resize(false)\">";
24
25 tmp += "<div class=\"postDate\">"+rs.fieldByName("updated")+"</div>";
26
27 if (rs.fieldByName("link") != "") {
28 tmp += "<div clear='both'><a target=\"_blank\" "+
29 "href=\"" + rs.fieldByName("link") + "\">" +
30 rs.fieldByName("title") + "</a></div>";
31 } else {
32 tmp += "<div clear='both'>" + rs.fieldByName("title") + "</div>";
33 }
34
35 /* tmp += "<div style='float : right'> "+
36 "<img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
37 tmp += rs.fieldByName("tags");
38 tmp += "</div>"; */
39
40 /* tmp += "<div clear='both'>"+
41 "<a target=\"_blank\" "+
42 "href=\"" + rs.fieldByName("comments") + "\">" +
43 __("comments") + "</a></div>"; */
44
45 tmp += "</div>";
46
47 tmp += "<div class=\"postContent\">"
48 tmp += rs.fieldByName("content");
49 tmp += "</div>";
50
51 tmp += "</div>";
52
53 render_article(tmp);
54 update_local_feedlist_counters();
55 }
56
57 rs.close();
58
59 return false;
60
61 } catch (e) {
62 exception_error("view_offline", e);
63 }
64 }
65
66 function viewfeed_offline(feed_id, subop, is_cat, subop_param, skip_history, offset) {
67 try {
68 notify('');
69
70 if (!offset) offset = 0;
71
72 if (offset > 0) {
73 _feed_cur_page = parseInt(offset);
74 if (_infscroll_request_sent) {
75 return;
76 }
77 } else {
78 _feed_cur_page = 0;
79 _infscroll_disable = 0;
80 }
81
82 if (getActiveFeedId() != feed_id) {
83 _feed_cur_page = 0;
84 active_post_id = 0;
85 _infscroll_disable = 0;
86 }
87
88 loading_set_progress(100);
89
90 clean_feed_selections();
91
92 setActiveFeedId(feed_id, is_cat);
93
94 if (!is_cat) {
95 var feedr = document.getElementById("FEEDR-" + feed_id);
96 if (feedr && !feedr.className.match("Selected")) {
97 feedr.className = feedr.className + "Selected";
98 }
99 } else {
100 var feedr = document.getElementById("FCAT-" + feed_id);
101 if (feedr && !feedr.className.match("Selected")) {
102 feedr.className = feedr.className + "Selected";
103 }
104 }
105
106 if (subop == "MarkAllRead") {
107 catchup_local_feed(feed_id, is_cat);
108 }
109
110 disableContainerChildren("headlinesToolbar", false);
111 Form.enable("main_toolbar_form");
112
113 var f = document.getElementById("headlines-frame");
114 try {
115 if (reply.offset == 0) {
116 debug("resetting headlines scrollTop");
117 f.scrollTop = 0;
118 }
119 } catch (e) { };
120
121
122 var tmp = "";
123 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 (document.getElementById("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 (document.getElementById("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 (document.getElementById("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 = document.getElementById("headlines-frame");
417 container.innerHTML = tmp;
418 } else {
419 var ids = getSelectedArticleIds2();
420
421 var container = document.getElementById("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 = document.getElementById("main_toolbar_form");
575 Element.hide(tb_form.update);
576
577 var chooser = document.getElementById("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)+")", 10*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)+")", 10*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 db.execute("DELETE FROM articles WHERE "+
734 "updated < DATETIME('NOW', 'localtime', '-31 days')");
735
736 }
737 }
738
739 update_local_sync_data();
740
741 // notify('');
742
743 }
744 } catch (e) {
745 exception_error("offline_download_parse", e);
746 }
747 }
748
749 function update_offline_data(stage) {
750 try {
751
752 if (!stage) stage = 0;
753 if (offline_mode) return;
754 if (getInitParam("offline_enabled") != "1") return;
755
756 debug("update_offline_data: stage " + stage);
757
758 // notify_progress("Updating offline data... (" + stage +")", true);
759
760 var query = "backend.php?op=rpc&subop=download&stage=" + stage;
761
762 var rs = db.execute("SELECT MAX(id), MIN(id) FROM articles");
763
764 if (rs.isValidRow() && rs.field(0)) {
765 var offline_dl_max_id = rs.field(0);
766 var offline_dl_min_id = rs.field(1);
767
768 query = query + "&cidt=" + offline_dl_max_id;
769 query = query + "&cidb=" + offline_dl_min_id;
770 }
771
772 rs.close();
773
774 var to_sync = prepare_local_sync_data();
775
776 if (to_sync != "") {
777 to_sync = "?sync=" + param_escape(to_sync);
778 }
779
780 debug(query + "/" + to_sync);
781
782 new Ajax.Request(query, {
783 parameters: to_sync,
784 onComplete: function(transport) {
785 offline_download_parse(stage, transport);
786 } });
787
788 } catch (e) {
789 exception_error("initiate_offline_download", e);
790 }
791 }
792
793 function set_feedlist_counter(id, ctr, is_cat) {
794 try {
795
796 var feedctr = document.getElementById("FEEDCTR-" + id);
797 var feedu = document.getElementById("FEEDU-" + id);
798 var feedr = document.getElementById("FEEDR-" + id);
799
800 if (is_cat) {
801 var catctr = document.getElementById("FCATCTR-" + id);
802 if (catctr) {
803 catctr.innerHTML = "(" + ctr + ")";
804 if (ctr > 0) {
805 catctr.className = "catCtrHasUnread";
806 } else {
807 catctr.className = "catCtrNoUnread";
808 }
809 }
810 } else if (feedctr && feedu && feedr) {
811
812 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
813
814 feedu.innerHTML = ctr;
815
816 if (ctr > 0) {
817 feedctr.className = "feedCtrHasUnread";
818 if (!feedr.className.match("Unread")) {
819 var is_selected = feedr.className.match("Selected");
820
821 feedr.className = feedr.className.replace("Selected", "");
822 feedr.className = feedr.className.replace("Unread", "");
823
824 feedr.className = feedr.className + "Unread";
825
826 if (is_selected) {
827 feedr.className = feedr.className + "Selected";
828 }
829
830 }
831
832 if (row_needs_hl) {
833 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
834 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
835 }
836 } else {
837 feedctr.className = "feedCtrNoUnread";
838 feedr.className = feedr.className.replace("Unread", "");
839 }
840 }
841
842 } catch (e) {
843 exception_error("set_feedlist_counter", e);
844 }
845 }
846
847 function update_local_feedlist_counters() {
848 try {
849 if (!offline_mode || !db) return;
850
851 /* var rs = db.execute("SELECT feeds.id,COUNT(articles.id) "+
852 "FROM feeds LEFT JOIN articles ON (feed_id = feeds.id) "+
853 "WHERE unread = 1 OR unread IS NULL GROUP BY feeds.id "+
854 "ORDER BY feeds.title"); */
855
856 var rs = db.execute("SELECT id FROM feeds "+
857 "ORDER BY title");
858
859 while (rs.isValidRow()) {
860 var id = rs.field(0);
861 var ctr = get_local_feed_unread(id);
862 set_feedlist_counter(id, ctr, false);
863 rs.next();
864 }
865
866 rs.close();
867
868 var rs = db.execute("SELECT cat_id,SUM(unread) "+
869 "FROM articles, feeds WHERE feeds.id = feed_id GROUP BY cat_id");
870
871 while (rs.isValidRow()) {
872 var id = rs.field(0);
873 var ctr = rs.field(1);
874 set_feedlist_counter(id, ctr, true);
875 rs.next();
876 }
877
878 rs.close();
879
880 set_feedlist_counter(-2, get_local_category_unread(-2), true);
881
882 set_feedlist_counter(-4, get_local_feed_unread(-4));
883 set_feedlist_counter(-1, get_local_feed_unread(-1));
884
885 var rs = db.execute("SELECT id FROM labels");
886
887 while (rs.isValidRow()) {
888 var id = -11 - rs.field(0);
889 var ctr = get_local_feed_unread(id);
890 set_feedlist_counter(id, ctr, false);
891 rs.next();
892 }
893
894 rs.close();
895
896 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
897
898 global_unread = get_local_feed_unread(-4);
899 updateTitle();
900
901 } catch (e) {
902 exception_error("update_local_feedlist_counters", e);
903 }
904 }
905
906 function get_local_feed_unread(id) {
907 try {
908 var rs;
909
910 if (id == -4) {
911 rs = db.execute("SELECT SUM(unread) FROM articles");
912 } else if (id == -1) {
913 rs = db.execute("SELECT SUM(unread) FROM articles WHERE marked = 1");
914 } else if (id > 0) {
915 rs = db.execute("SELECT SUM(unread) FROM articles WHERE feed_id = ?", [id]);
916 } else if (id < -10) {
917 var label_id = -11 - id;
918 rs = db.execute("SELECT SUM(unread) FROM articles,article_labels "+
919 "WHERE article_labels.id = articles.id AND label_id = ?", [label_id]);
920 }
921
922 var a = false;
923
924 if (rs.isValidRow()) {
925 a = rs.field(0);
926 } else {
927 a = 0;
928 }
929
930 rs.close();
931
932 return a;
933
934 } catch (e) {
935 exception_error("get_local_feed_unread", e);
936 }
937 }
938
939 function enable_offline_reading() {
940 try {
941
942 if (getInitParam("offline_enabled") == "1") {
943 init_local_sync_data();
944 Element.show("restartOfflinePic");
945 window.setTimeout("update_offline_data(0)", 100);
946 }
947
948 } catch (e) {
949 exception_error("enable_offline_reading", e);
950 }
951 }
952
953 function init_gears() {
954 try {
955
956 if (window.google && google.gears) {
957 localServer = google.gears.factory.create("beta.localserver");
958 store = localServer.createManagedStore("tt-rss");
959 db = google.gears.factory.create('beta.database');
960 db.open('tt-rss');
961
962 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
963
964 var rs = db.execute("SELECT schema_version FROM version");
965
966 var version = "";
967
968 if (rs.isValidRow()) {
969 version = rs.field(0);
970 }
971
972 rs.close();
973
974 if (version != SCHEMA_VERSION) {
975 db.execute("DROP TABLE IF EXISTS init_params");
976 db.execute("DROP TABLE IF EXISTS cache");
977 db.execute("DROP TABLE IF EXISTS feeds");
978 db.execute("DROP TABLE IF EXISTS categories");
979 db.execute("DROP TABLE IF EXISTS labels");
980 db.execute("DROP TABLE IF EXISTS article_labels");
981 db.execute("DROP TABLE IF EXISTS articles");
982 db.execute("DROP INDEX IF EXISTS article_labels_label_id_idx");
983 db.execute("DROP INDEX IF EXISTS articles_unread_idx");
984 db.execute("DROP INDEX IF EXISTS articles_feed_id_idx");
985 db.execute("DROP INDEX IF EXISTS articles_id_idx");
986 db.execute("DROP INDEX IF EXISTS article_labels_id_idx");
987 db.execute("DROP TABLE IF EXISTS version");
988 db.execute("DROP TRIGGER IF EXISTS articles_update_unread");
989 db.execute("DROP TRIGGER IF EXISTS articles_update_marked");
990 db.execute("DROP TRIGGER IF EXISTS articles_remove_labelrefs");
991 db.execute("CREATE TABLE IF NOT EXISTS version (schema_version text)");
992 db.execute("DROP TABLE IF EXISTS syncdata");
993 db.execute("INSERT INTO version (schema_version) VALUES (?)",
994 [SCHEMA_VERSION]);
995 }
996
997 db.execute("CREATE TABLE IF NOT EXISTS init_params (key text, value text)");
998
999 db.execute("CREATE TABLE IF NOT EXISTS cache (id integer, article text, param text, added text)");
1000 db.execute("CREATE TABLE IF NOT EXISTS feeds (id integer, title text, has_icon integer, cat_id integer)");
1001 db.execute("CREATE TABLE IF NOT EXISTS categories (id integer, title text, collapsed integer)");
1002 db.execute("CREATE TABLE IF NOT EXISTS labels (id integer, caption text, fg_color text, bg_color text)");
1003 db.execute("CREATE TABLE IF NOT EXISTS article_labels (id integer, label_id integer)");
1004 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)");
1005
1006 db.execute("CREATE INDEX IF NOT EXISTS articles_unread_idx ON articles(unread)");
1007 db.execute("CREATE INDEX IF NOT EXISTS article_labels_label_id_idx ON article_labels(label_id)");
1008 db.execute("CREATE INDEX IF NOT EXISTS articles_feed_id_idx ON articles(feed_id)");
1009 db.execute("CREATE INDEX IF NOT EXISTS articles_id_idx ON articles(id)");
1010 db.execute("CREATE INDEX IF NOT EXISTS article_labels_id_idx ON article_labels(id)");
1011
1012 db.execute("CREATE TABLE IF NOT EXISTS syncdata (key integer, value text)");
1013
1014 db.execute("DELETE FROM cache WHERE id LIKE 'F:%' OR id LIKE 'C:%'");
1015
1016 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_unread "+
1017 "UPDATE OF unread ON articles "+
1018 "BEGIN "+
1019 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1020 "WHERE id = OLD.id AND "+
1021 "OLD.unread != NEW.unread;"+
1022 "END;");
1023
1024 db.execute("CREATE TRIGGER IF NOT EXISTS articles_update_marked "+
1025 "UPDATE OF marked ON articles "+
1026 "BEGIN "+
1027 "UPDATE articles SET modified = DATETIME('NOW', 'localtime') "+
1028 "WHERE id = OLD.id;"+
1029 "END;");
1030
1031 db.execute("CREATE TRIGGER IF NOT EXISTS articles_remove_labelrefs "+
1032 "DELETE ON articles "+
1033 "BEGIN "+
1034 "DELETE FROM article_labels WHERE id = OLD.id; "+
1035 "END; ");
1036
1037 }
1038
1039 cache_expire();
1040
1041 } catch (e) {
1042 exception_error("init_gears", e);
1043 }
1044 }
1045
1046 function gotoOffline() {
1047 window.location.href = "tt-rss.php?offline=1";
1048 }
1049
1050 function gotoOnline() {
1051 window.location.href = "tt-rss.php";
1052 }
1053
1054 function local_collapse_cat(id) {
1055 try {
1056 if (db) {
1057 db.execute("UPDATE categories SET collapsed = NOT collapsed WHERE id = ?",
1058 [id]);
1059 }
1060 } catch (e) {
1061 exception_error("local_collapse_cat", e);
1062 }
1063 }
1064
1065 function get_local_category_title(id) {
1066 try {
1067
1068 var rs = db.execute("SELECT title FROM categories WHERE id = ?", [id]);
1069 var tmp = "";
1070
1071 if (rs.isValidRow()) {
1072 tmp = rs.field(0);
1073 }
1074
1075 rs.close();
1076
1077 return tmp;
1078
1079 } catch (e) {
1080 exception_error("get_local_category_title", e);
1081 }
1082 }
1083
1084 function get_local_category_unread(id) {
1085 try {
1086 var rs = false;
1087
1088 if (id >= 0) {
1089 rs = db.execute("SELECT SUM(unread) FROM articles, feeds "+
1090 "WHERE feeds.id = feed_id AND cat_id = ?", [id]);
1091 } else if (id == -2) {
1092 rs = db.execute("SELECT SUM(unread) FROM article_labels, articles "+
1093 "where article_labels.id = articles.id");
1094 } else {
1095 return 0;
1096 }
1097
1098 var tmp = 0;
1099
1100 if (rs.isValidRow()) {
1101 tmp = rs.field(0);
1102 }
1103
1104 rs.close();
1105
1106 return tmp;
1107
1108 } catch (e) {
1109 exception_error("get_local_category_unread", e);
1110 }
1111 }
1112
1113 function printCategoryHeader(cat_id, hidden, can_browse) {
1114 try {
1115 if (hidden == undefined) hidden = false;
1116 if (can_browse == undefined) can_browse = false;
1117
1118 var tmp_category = get_local_category_title(cat_id);
1119 var tmp = "";
1120
1121 var cat_unread = get_local_category_unread(cat_id);
1122
1123 var holder_style = "";
1124 var ellipsis = "";
1125
1126 if (hidden) {
1127 holder_style = "display:none;";
1128 ellipsis = "…";
1129 }
1130
1131 var catctr_class = (cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
1132
1133 var browse_cat_link = "";
1134 var inner_title_class = "catTitleNL";
1135
1136 if (can_browse) {
1137 browse_cat_link = "onclick=\"javascript:viewCategory("+cat_id+")\"";
1138 inner_title_class = "catTitle";
1139 }
1140
1141 var cat_class = "feedCat";
1142
1143 tmp += "<li class=\""+cat_class+"\" id=\"FCAT-"+cat_id+"\">"+
1144 "<img onclick=\"toggleCollapseCat("+cat_id+")\" class=\"catCollapse\""+
1145 " title=\""+__('Click to collapse category')+"\""+
1146 " src=\"images/cat-collapse.png\"><span class=\""+inner_title_class+"\" "+
1147 " id=\"FCATN-"+cat_id+"\" "+browse_cat_link+
1148 "\">"+tmp_category+"</span>";
1149
1150 tmp += "<span id=\"FCAP-"+cat_id+"\">";
1151
1152 tmp += " <span id=\"FCATCTR-"+cat_id+"\" "+
1153 "class=\""+catctr_class+"\">("+cat_unread+")</span> "+ellipsis;
1154
1155 tmp += "</span>";
1156
1157 tmp += "<ul class=\"feedCatList\" id=\"FCATLIST-"+cat_id+"\" "+
1158 "style='"+holder_style+"'>";
1159
1160 return tmp;
1161 } catch (e) {
1162 exception_error("printCategoryHeader", e);
1163 }
1164 }
1165
1166 function is_local_cat_collapsed(id) {
1167 try {
1168
1169 var rs = db.execute("SELECT collapsed FROM categories WHERE id = ?", [id]);
1170 var cat_hidden = 0;
1171
1172 if (rs.isValidRow()) {
1173 cat_hidden = rs.field(0);
1174 }
1175
1176 rs.close();
1177
1178 return cat_hidden == "1";
1179
1180 } catch (e) {
1181 exception_error("is_local_cat_collapsed", e);
1182 }
1183 }
1184
1185 function get_local_article_labels(id) {
1186 try {
1187 var rs = db.execute("SELECT DISTINCT label_id,caption,fg_color,bg_color "+
1188 "FROM labels, article_labels "+
1189 "WHERE labels.id = label_id AND article_labels.id = ?", [id]);
1190
1191 var tmp = new Array();
1192
1193 while (rs.isValidRow()) {
1194 var e = new Array();
1195
1196 e[0] = rs.field(0);
1197 e[1] = rs.field(1);
1198 e[2] = rs.field(2);
1199 e[3] = rs.field(3);
1200
1201 tmp.push(e);
1202
1203 rs.next();
1204 }
1205
1206 return tmp;
1207
1208 } catch (e) {
1209 exception_error("get_local_article_labels", e);
1210 }
1211 }
1212
1213 function label_local_add_article(id, label_id) {
1214 try {
1215 //debug("label_local_add_article " + id + " => " + label_id);
1216
1217 var rs = db.execute("SELECT COUNT(id) FROM article_labels WHERE "+
1218 "id = ? AND label_id = ?", [id, label_id]);
1219 var check = rs.field(0);
1220
1221 if (rs.isValidRow()) {
1222 var check = rs.field(0);
1223 }
1224 rs.close();
1225
1226 if (check == 0) {
1227 db.execute("INSERT INTO article_labels (id, label_id) VALUES "+
1228 "(?,?)", [id, label_id]);
1229 }
1230
1231 } catch (e) {
1232 exception_error("label_local_add_article", e);
1233 }
1234 }
1235
1236 function get_local_feed_title(id) {
1237 try {
1238
1239 var feed_title = "Unknown feed: " + id;
1240
1241 if (id > 0) {
1242 var rs = db.execute("SELECT title FROM feeds WHERE id = ?", [id]);
1243
1244 if (rs.isValidRow()) {
1245 feed_title = rs.field(0);
1246 }
1247
1248 rs.close();
1249 } else if (id == -1) {
1250 feed_title = __("Starred articles");
1251 } else if (id == -4) {
1252 feed_title = __("All articles");
1253 } else if (id < -10) {
1254
1255 var label_id = -11 - id;
1256
1257 var rs = db.execute("SELECT caption FROM labels WHERE id = ?", [label_id]);
1258
1259 if (rs.isValidRow()) {
1260 feed_title = rs.field(0);
1261 }
1262
1263 rs.close();
1264 }
1265
1266 return feed_title;
1267
1268 } catch (e) {
1269 exception_error("get_local_feed_title", e);
1270 }
1271 }
1272
1273 function format_article_labels(labels, id) {
1274 try {
1275
1276 var labels_str = "";
1277
1278 if (!labels) return "";
1279
1280 for (var i = 0; i < labels.length; i++) {
1281 var l = labels[i];
1282
1283 labels_str += "<span class='hlLabelRef' "+
1284 "style='color : "+l[2]+"; background-color : "+l[3]+"'>"+l[1]+"</span>";
1285 }
1286
1287 return labels_str;
1288
1289 } catch (e) {
1290 exception_error("format_article_labels", e);
1291 }
1292 }
1293
1294 function init_local_sync_data() {
1295 try {
1296 var rs = db.execute("SELECT COUNT(*) FROM syncdata WHERE key = 'last_online'");
1297 var has_last_online = 0;
1298
1299 if (rs.isValidRow()) {
1300 has_last_online = rs.field(0);
1301 }
1302
1303 rs.close();
1304
1305 if (!has_last_online) {
1306 db.execute("INSERT INTO syncdata (key, value) VALUES ('last_online', '')");
1307 }
1308
1309 } catch (e) {
1310 exception_error("init_local_sync_data", e);
1311
1312 }
1313 }
1314
1315 function has_local_sync_data() {
1316 try {
1317
1318 var rs = db.execute("SELECT id FROM articles "+
1319 "WHERE modified > (SELECT value FROM syncdata WHERE key = 'last_online') "+
1320 "LIMIT 1");
1321
1322 var tmp = 0;
1323
1324 if (rs.isValidRow()) {
1325 tmp = rs.field(0);
1326 }
1327
1328 rs.close();
1329
1330 return tmp != 0;
1331
1332 } catch (e) {
1333 exception_error("has_local_sync_data", e);
1334 }
1335 }
1336
1337 function prepare_local_sync_data() {
1338 try {
1339 var rs = db.execute("SELECT value FROM syncdata WHERE key = 'last_online'");
1340
1341 var last_online = "";
1342
1343 if (rs.isValidRow()) {
1344 last_online = rs.field(0);
1345 }
1346
1347 rs.close();
1348
1349 var rs = db.execute("SELECT id,unread,marked FROM articles "+
1350 "WHERE modified > ? LIMIT 200", [last_online]);
1351
1352 var tmp = last_online + ";";
1353
1354 var entries = 0;
1355
1356 while (rs.isValidRow()) {
1357 var e = new Array();
1358
1359 tmp = tmp + rs.field(0) + "," + rs.field(1) + "," + rs.field(2) + ";";
1360 entries++;
1361
1362 rs.next();
1363 }
1364
1365 rs.close();
1366
1367 if (entries > 0) {
1368 return tmp;
1369 } else {
1370 return '';
1371 }
1372
1373 } catch (e) {
1374 exception_error("prepare_local_sync_data", e);
1375 }
1376 }
1377
1378 function update_local_sync_data() {
1379 try {
1380 if (db && !offline_mode) {
1381
1382 var rs = db.execute("SELECT id FROM articles "+
1383 "WHERE modified > (SELECT value FROM syncdata WHERE "+
1384 "key = 'last_online') LIMIT 1")
1385
1386 var f_id = 0;
1387
1388 if (rs.isValidRow()) {
1389 f_id = rs.field(0);
1390 }
1391
1392 rs.close();
1393
1394 /* no pending articles to sync */
1395
1396 if (f_id == 0) {
1397 db.execute("UPDATE syncdata SET value = DATETIME('NOW', 'localtime') "+
1398 "WHERE key = 'last_online'");
1399 }
1400
1401 }
1402 } catch (e) {
1403 exception_error("update_local_sync_data", e);
1404 }
1405 }
1406
1407 function catchup_local_feed(id, is_cat) {
1408 try {
1409 if (!db) return;
1410
1411 if (!is_cat) {
1412 if (id >= 0) {
1413 db.execute("UPDATE articles SET unread = 0 WHERE feed_id = ?", [id]);
1414 } else if (id == -1) {
1415 db.execute("UPDATE articles SET unread = 0 WHERE marked = 1");
1416 } else if (id == -4) {
1417 db.execute("UPDATE articles SET unread = 0");
1418 } else if (id < -10) {
1419 var label_id = -11-id;
1420
1421 db.execute("UPDATE articles SET unread = 0 WHERE "+
1422 "(SELECT COUNT(*) FROM article_labels WHERE "+
1423 "article_labels.id = articles.id AND label_id = ?) > 0", [label_id]);
1424 }
1425 }
1426
1427 update_local_feedlist_counters();
1428
1429 } catch (e) {
1430 exception_error("catchup_local_feed", e);
1431 }
1432 }