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