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