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