]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
fix sql syntax error when exporting opml w/o cats
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17
18 var xmlhttp = Ajax.getTransport();
19 var xmlhttp_ctr = Ajax.getTransport();
20
21 var init_params = new Object();
22
23 function toggleTags() {
24 display_tags = !display_tags;
25
26 var p = document.getElementById("dispSwitchPrompt");
27
28 if (display_tags) {
29 p.innerHTML = "display feeds";
30 } else {
31 p.innerHTML = "display tags";
32 }
33
34 notify("Loading, please wait...");
35
36 updateFeedList();
37 }
38
39 function dlg_frefresh_callback() {
40 if (xmlhttp.readyState == 4) {
41 // notify(xmlhttp.responseText);
42
43 if (getActiveFeedId() == _qfd_deleted_feed) {
44 var h = document.getElementById("headlines-frame");
45 if (h) {
46 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
47 }
48 }
49
50 setTimeout('updateFeedList(false, false)', 50);
51 closeInfoBox();
52 }
53 }
54
55 function refetch_callback() {
56 if (xmlhttp_ctr.readyState == 4) {
57 try {
58
59 var date = new Date();
60
61 last_refetch = date.getTime() / 1000;
62
63 if (!xmlhttp_ctr.responseXML) {
64 notify("refetch_callback: backend did not return valid XML", true, true);
65 return;
66 }
67
68 var reply = xmlhttp_ctr.responseXML.firstChild;
69
70 if (!reply) {
71 notify("refetch_callback: backend did not return expected XML object", true, true);
72 updateTitle("");
73 return;
74 }
75
76 var error_code = reply.getAttribute("error-code");
77
78 if (error_code && error_code != 0) {
79 return fatalError(error_code, reply.getAttribute("error-msg"));
80 }
81
82 var counters = reply.firstChild;
83
84 parse_counters(counters, true);
85
86 var runtime_info = counters.nextSibling;
87
88 parse_runtime_info(runtime_info);
89
90 debug("refetch_callback: done");
91
92 if (!daemon_enabled && !daemon_refresh_only) {
93 notify("All feeds updated.");
94 updateTitle("");
95 } else {
96 notify("");
97 }
98 } catch (e) {
99 exception_error("refetch_callback", e);
100 updateTitle("");
101 }
102 }
103 }
104
105 function backend_sanity_check_callback() {
106
107 if (xmlhttp.readyState == 4) {
108
109 try {
110
111 if (!xmlhttp.responseXML) {
112 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
113 return;
114 }
115
116 var reply = xmlhttp.responseXML.firstChild.firstChild;
117
118 if (!reply) {
119 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
120 return;
121 }
122
123 var error_code = reply.getAttribute("error-code");
124
125 if (error_code && error_code != 0) {
126 return fatalError(error_code, reply.getAttribute("error-msg"));
127 }
128
129 debug("sanity check ok");
130
131 var params = reply.nextSibling;
132
133 if (params) {
134 debug('reading init-params...');
135 var param = params.firstChild;
136
137 while (param) {
138 var k = param.getAttribute("key");
139 var v = param.getAttribute("value");
140 debug(k + " => " + v);
141 init_params[k] = v;
142 param = param.nextSibling;
143 }
144 }
145
146 init_second_stage();
147
148 } catch (e) {
149 exception_error("backend_sanity_check_callback", e);
150 }
151 }
152 }
153
154 function scheduleFeedUpdate(force) {
155
156 if (!daemon_enabled && !daemon_refresh_only) {
157 notify("Updating feeds, please wait.", true);
158 updateTitle("Updating");
159 }
160
161 var query_str = "backend.php?op=rpc&subop=";
162
163 if (force) {
164 query_str = query_str + "forceUpdateAllFeeds";
165 } else {
166 query_str = query_str + "updateAllFeeds";
167 }
168
169 var omode;
170
171 if (firsttime_update && !navigator.userAgent.match("Opera")) {
172 firsttime_update = false;
173 omode = "T";
174 } else {
175 if (display_tags) {
176 omode = "t";
177 } else {
178 omode = "flc";
179 }
180 }
181
182 query_str = query_str + "&omode=" + omode;
183 query_str = query_str + "&uctr=" + global_unread;
184
185 debug("in scheduleFeedUpdate");
186
187 var date = new Date();
188
189 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
190 debug("<b>xmlhttp seems to be stuck, aborting</b>");
191 xmlhttp_ctr.abort();
192 }
193
194 debug("REFETCH query: " + query_str);
195
196 if (xmlhttp_ready(xmlhttp_ctr)) {
197 xmlhttp_ctr.open("GET", query_str, true);
198 xmlhttp_ctr.onreadystatechange=refetch_callback;
199 xmlhttp_ctr.send(null);
200 } else {
201 debug("xmlhttp_ctr busy");
202 //printLockingError();
203 }
204 }
205
206 function updateFeedList(silent, fetch) {
207
208 // if (silent != true) {
209 // notify("Loading feed list...");
210 // }
211
212 var query_str = "backend.php?op=feeds";
213
214 if (display_tags) {
215 query_str = query_str + "&tags=1";
216 }
217
218 if (getActiveFeedId() && !activeFeedIsCat()) {
219 query_str = query_str + "&actid=" + getActiveFeedId();
220 }
221
222 if (navigator.userAgent.match("Opera")) {
223 var date = new Date();
224 var timestamp = Math.round(date.getTime() / 1000);
225 query_str = query_str + "&ts=" + timestamp
226 }
227
228 if (fetch) query_str = query_str + "&fetch=yes";
229
230 // var feeds_frame = document.getElementById("feeds-frame");
231 // feeds_frame.src = query_str;
232
233 debug("updateFeedList Q=" + query_str);
234
235 if (xmlhttp_ready(xmlhttp)) {
236 xmlhttp.open("GET", query_str, true);
237 xmlhttp.onreadystatechange=feedlist_callback;
238 xmlhttp.send(null);
239 } else {
240 debug("xmlhttp busy");
241 //printLockingError();
242 }
243
244 }
245
246 function catchupAllFeeds() {
247
248 var query_str = "backend.php?op=feeds&subop=catchupAll";
249
250 notify("Marking all feeds as read...");
251
252 var feeds_frame = document.getElementById("feeds-frame");
253
254 feeds_frame.src = query_str;
255
256 global_unread = 0;
257 updateTitle("");
258
259 }
260
261 function viewCurrentFeed(subop) {
262
263 // if (getActiveFeedId()) {
264 if (getActiveFeedId() != undefined) {
265 viewfeed(getActiveFeedId(), subop);
266 } else {
267 disableContainerChildren("headlinesToolbar", false, document);
268 // viewfeed(-1, subop); // FIXME
269 }
270 return false; // block unneeded form submits
271 }
272
273 function viewfeed(feed, subop) {
274 var f = window.frames["feeds-frame"];
275 f.viewfeed(feed, subop);
276 }
277
278 function timeout() {
279 scheduleFeedUpdate(false);
280
281 var refresh_time = getInitParam("feeds_frame_refresh");
282
283 if (!refresh_time) refresh_time = 600;
284
285 setTimeout("timeout()", refresh_time*1000);
286 }
287
288 function resetSearch() {
289 var searchbox = document.getElementById("searchbox")
290
291 if (searchbox.value != "" && getActiveFeedId()) {
292 searchbox.value = "";
293 viewfeed(getActiveFeedId(), "");
294 }
295 }
296
297 function searchCancel() {
298 closeInfoBox(true);
299 }
300
301 function search() {
302 closeInfoBox();
303 viewCurrentFeed(0, "");
304 }
305
306 function localPiggieFunction(enable) {
307 if (enable) {
308 var query_str = "backend.php?op=feeds&subop=piggie";
309
310 if (xmlhttp_ready(xmlhttp)) {
311
312 xmlhttp.open("GET", query_str, true);
313 xmlhttp.onreadystatechange=feedlist_callback;
314 xmlhttp.send(null);
315 }
316 }
317 }
318
319 // if argument is undefined, current subtitle is not updated
320 // use blank string to clear subtitle
321 function updateTitle(s) {
322 var tmp = "Tiny Tiny RSS";
323
324 if (s != undefined) {
325 current_subtitle = s;
326 }
327
328 if (global_unread > 0) {
329 tmp = tmp + " (" + global_unread + ")";
330 }
331
332 if (current_subtitle) {
333 tmp = tmp + " - " + current_subtitle;
334 }
335
336 if (active_title_text.length > 0) {
337 tmp = tmp + " > " + active_title_text;
338 }
339
340 document.title = tmp;
341 }
342
343 function genericSanityCheck() {
344
345 if (!xmlhttp) fatalError(1);
346
347 setCookie("ttrss_vf_test", "TEST");
348
349 if (getCookie("ttrss_vf_test") != "TEST") {
350 fatalError(2);
351 }
352
353 return true;
354 }
355
356 function init() {
357
358 try {
359
360 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
361
362 if (arguments.callee.done) return;
363 arguments.callee.done = true;
364
365 disableContainerChildren("headlinesToolbar", true);
366
367 Form.disable("main_toolbar_form");
368
369 if (!genericSanityCheck())
370 return;
371
372 if (getURLParam('debug')) {
373 document.getElementById('debug_output').style.display = 'block';
374 debug('debug mode activated');
375 }
376
377 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
378 xmlhttp.onreadystatechange=backend_sanity_check_callback;
379 xmlhttp.send(null);
380
381 } catch (e) {
382 exception_error("init", e);
383 }
384 }
385
386 function init_second_stage() {
387
388 try {
389
390 cookie_lifetime = getCookie("ttrss_cltime");
391
392 delCookie("ttrss_vf_test");
393
394 document.onkeydown = hotkey_handler;
395
396 var tb = parent.document.forms["main_toolbar_form"];
397
398 // dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
399 // dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
400
401 daemon_enabled = getInitParam("daemon_enabled") == 1;
402 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
403
404 setTimeout('updateFeedList(false, false)', 50);
405
406 debug("second stage ok");
407
408 } catch (e) {
409 exception_error("init_second_stage", e);
410 }
411 }
412
413 function quickMenuChange() {
414 var chooser = document.getElementById("quickMenuChooser");
415 var opid = chooser[chooser.selectedIndex].value;
416
417 chooser.selectedIndex = 0;
418 quickMenuGo(opid);
419 }
420
421 function quickMenuGo(opid) {
422 try {
423
424 if (opid == "qmcPrefs") {
425 gotoPreferences();
426 }
427
428 if (opid == "qmcSearch") {
429 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
430 return;
431 }
432
433 if (opid == "qmcAddFeed") {
434 displayDlg("quickAddFeed");
435 return;
436 }
437
438 if (opid == "qmcRemoveFeed") {
439 var actid = getActiveFeedId();
440
441 if (!actid || activeFeedIsCat()) {
442 alert("Please select some feed first.");
443 return;
444 }
445
446 var fn = getFeedName(actid);
447
448 if (confirm("Unsubscribe from " + fn + "?")) {
449 qfdDelete(actid);
450 }
451
452 return;
453 }
454
455 if (opid == "qmcUpdateFeeds") {
456 scheduleFeedUpdate(true);
457 return;
458 }
459
460 if (opid == "qmcCatchupAll") {
461 catchupAllFeeds();
462 return;
463 }
464
465 if (opid == "qmcShowOnlyUnread") {
466 toggleDispRead();
467 return;
468 }
469
470 if (opid == "qmcAddFilter") {
471 displayDlg("quickAddFilter", getActiveFeedId());
472 }
473 } catch (e) {
474 exception_error("quickMenuGo", e);
475 }
476 }
477
478 function qfdDelete(feed_id) {
479
480 notify("Removing feed...");
481
482 if (!xmlhttp_ready(xmlhttp)) {
483 printLockingError();
484 return
485 }
486
487 _qfd_deleted_feed = feed_id;
488
489 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
490 xmlhttp.onreadystatechange=dlg_frefresh_callback;
491 xmlhttp.send(null);
492
493 return false;
494 }
495
496
497 function updateFeedTitle(t) {
498 active_title_text = t;
499 updateTitle();
500 }
501
502 function toggleDispRead() {
503 try {
504
505 if (!xmlhttp_ready(xmlhttp)) {
506 printLockingError();
507 return
508 }
509
510 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
511
512 hide_read_feeds = !hide_read_feeds;
513
514 debug("toggle_disp_read => " + hide_read_feeds);
515
516 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
517
518 var query = "backend.php?op=rpc&subop=setpref" +
519 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
520
521 storeInitParam("hide_read_feeds", hide_read_feeds, true);
522
523 new Ajax.Request(query);
524
525 } catch (e) {
526 exception_error("toggleDispRead", e);
527 }
528 }
529
530 function parse_runtime_info(elem) {
531 var param = elem.firstChild;
532
533 debug("parse_runtime_info: " + param);
534
535 while (param) {
536 var k = param.getAttribute("key");
537 var v = param.getAttribute("value");
538
539 debug("RI: " + k + " => " + v);
540
541 var w = document.getElementById("noDaemonWarning");
542
543 if (w) {
544 if (k == "daemon_is_running" && v != 1) {
545 w.style.display = "block";
546 } else {
547 w.style.display = "none";
548 }
549 }
550 param = param.nextSibling;
551 }
552 }
553
554 function catchupCurrentFeed() {
555
556 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
557
558 var str = "Mark all articles in " + fn + " as read?";
559
560 /* if (active_feed_is_cat) {
561 str = "Mark all articles in this category as read?";
562 } */
563
564 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
565 return viewCurrentFeed('MarkAllRead')
566 }
567 }
568
569 function userSwitch() {
570 var chooser = document.getElementById("userSwitch");
571 var user = chooser[chooser.selectedIndex].value;
572 window.location = "tt-rss.php?swu=" + user;
573 }
574
575