This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
view.cpp
1088 lines (959 loc) · 32.7 KB
/
view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <itemlist.h>
#include <itemview.h>
#include <help.h>
#include <filebrowser.h>
#include <urlview.h>
#include <selecttag.h>
#include <feedlist.h>
#include <dialogs.h>
#include <formatstring.h>
#include <formaction.h>
#include <feedlist_formaction.h>
#include <itemlist_formaction.h>
#include <itemview_formaction.h>
#include <help_formaction.h>
#include <urlview_formaction.h>
#include <select_formaction.h>
#include <dialogs_formaction.h>
#include <logger.h>
#include <reloadthread.h>
#include <exception.h>
#include <exceptions.h>
#include <keymap.h>
#include <utils.h>
#include <regexmanager.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cerrno>
#include <assert.h>
#include <libgen.h>
#include <sys/types.h>
#include <dirent.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <config.h>
#include <sys/param.h>
#include <string.h>
#include <ncurses.h>
extern "C" {
#include <stfl.h>
}
#include <view.h>
#include <rss.h>
#include <htmlrenderer.h>
#include <cstring>
#include <cstdio>
namespace newsbeuter {
view::view(controller * c) : ctrl(c), cfg(0), keys(0), current_formaction(0), is_inside_qna(false), is_inside_cmdline(false), tab_count(0) {
}
view::~view() {
stfl::reset();
}
void view::set_config_container(configcontainer * cfgcontainer) {
cfg = cfgcontainer;
}
void view::set_keymap(keymap * k) {
keys = k;
}
void view::update_bindings() {
for (auto& form : formaction_stack) {
if (form) {
set_bindings(form);
}
}
}
void view::set_bindings(std::shared_ptr<formaction> fa) {
std::string upkey("** ");
upkey.append(keys->getkey(OP_SK_UP, fa->id()));
std::string downkey("** ");
downkey.append(keys->getkey(OP_SK_DOWN, fa->id()));
fa->get_form()->set("bind_up", upkey);
fa->get_form()->set("bind_down", downkey);
std::string pgupkey;
std::string pgdownkey;
if (fa->id() == "article" || fa->id() == "help") {
pgupkey.append("** b ");
pgdownkey.append("** SPACE ");
} else {
pgupkey.append("** ");
pgdownkey.append("** ");
}
pgupkey.append(keys->getkey(OP_SK_PGUP, fa->id()));
pgdownkey.append(keys->getkey(OP_SK_PGDOWN, fa->id()));
fa->get_form()->set("bind_page_up", pgupkey);
fa->get_form()->set("bind_page_down", pgdownkey);
std::string homekey("** ");
homekey.append(keys->getkey(OP_SK_HOME, fa->id()));
std::string endkey("** ");
endkey.append(keys->getkey(OP_SK_END, fa->id()));
fa->get_form()->set("bind_home", homekey);
fa->get_form()->set("bind_end", endkey);
}
std::shared_ptr<formaction> view::get_current_formaction() {
if (formaction_stack.size() > 0 && current_formaction < formaction_stack.size()) {
return formaction_stack[current_formaction];
}
return std::shared_ptr<formaction>();
}
void view::set_status_unlocked(const std::string& msg) {
if (formaction_stack.size() > 0 && get_current_formaction() != NULL) {
std::shared_ptr<stfl::form> form = get_current_formaction()->get_form();
if (form != NULL) {
form->set("msg",msg);
form->run(-1);
} else {
LOG(LOG_ERROR, "view::set_status_unlocked: form for formaction of type %s is NULL!", get_current_formaction()->id().c_str());
}
}
}
void view::set_status(const std::string& msg) {
std::lock_guard<std::mutex> lock(mtx);
set_status_unlocked(msg);
}
void view::show_error(const std::string& msg) {
set_status(msg);
}
void view::run() {
bool have_macroprefix = false;
std::vector<macrocmd> macrocmds;
// create feedlist
std::shared_ptr<feedlist_formaction> feedlist(new feedlist_formaction(this, feedlist_str));
set_bindings(feedlist);
feedlist->set_regexmanager(rxman);
feedlist->set_tags(tags);
apply_colors(feedlist);
formaction_stack.push_back(feedlist);
current_formaction = formaction_stack_size() - 1;
get_current_formaction()->init();
stfl::reset();
/*
* This is the main "event" loop of newsbeuter.
*/
while (formaction_stack.size() > 0) {
// first, we take the current formaction.
std::shared_ptr<formaction> fa = get_current_formaction();
// we signal "oh, you will receive an operation soon"
fa->prepare();
if (!macrocmds.empty()) {
// if there is any macro command left to process, we do so
fa->get_form()->run(-1);
fa->process_op(macrocmds[0].op, true, ¯ocmds[0].args);
macrocmds.erase(macrocmds.begin()); // remove first macro command, since it has already been processed
} else {
// we then receive the event and ignore timeouts.
const char * event = fa->get_form()->run(60000);
if (ctrl_c_hit) {
ctrl_c_hit = 0;
cancel_input(fa);
if (!get_cfg()->get_configvalue_as_bool("confirm-exit") || confirm(_("Do you really want to quit (y:Yes n:No)? "), _("yn")) == *_("y")) {
stfl::reset();
utils::remove_fs_lock(lock_file);
::exit(EXIT_FAILURE);
}
}
if (!event || strcmp(event,"TIMEOUT")==0) {
if (fa->id() == "article")
std::dynamic_pointer_cast<itemview_formaction, formaction>(fa)->update_percent();
continue;
}
if (is_inside_qna) {
LOG(LOG_DEBUG, "view::run: we're inside QNA input");
if (is_inside_cmdline && strcmp(event, "TAB")==0) {
handle_cmdline_completion(fa);
continue;
}
if (strcmp(event, "^U")==0) {
clear_line(fa);
continue;
} else if (strcmp(event, "^K")==0) {
clear_eol(fa);
continue;
} else if (strcmp(event, "^G")==0) {
cancel_input(fa);
continue;
} else if (strcmp(event, "^W")==0) {
delete_word(fa);
continue;
}
}
LOG(LOG_DEBUG, "view::run: event = %s", event);
// retrieve operation code through the keymap
operation op;
if (have_macroprefix) {
have_macroprefix = false;
LOG(LOG_DEBUG, "view::run: running macro `%s'", event);
macrocmds = keys->get_macro(event);
set_status("");
} else {
op = keys->get_operation(event, fa->id());
LOG(LOG_DEBUG, "view::run: event = %s op = %u", event, op);
if (OP_MACROPREFIX == op) {
have_macroprefix = true;
set_status("macro-");
}
// now we handle the operation to the formaction.
fa->process_op(op);
}
}
}
stfl::reset();
}
std::string view::run_modal(std::shared_ptr<formaction> f, const std::string& value) {
f->init();
unsigned int stacksize = formaction_stack.size();
formaction_stack.push_back(f);
current_formaction = formaction_stack_size() - 1;
while (formaction_stack.size() > stacksize) {
std::shared_ptr<formaction> fa = get_current_formaction();
fa->prepare();
const char * event = fa->get_form()->run(1000);
LOG(LOG_DEBUG, "view::run: event = %s", event);
if (!event || strcmp(event,"TIMEOUT")==0) continue;
operation op = keys->get_operation(event, fa->id());
if (OP_REDRAW == op) {
stfl::reset();
continue;
}
fa->process_op(op);
}
if (value == "")
return "";
else
return f->get_value(value);
}
std::string view::get_filename_suggestion(const std::string& s) {
/*
* With this function, we generate normalized filenames for saving
* articles to files.
*/
std::string retval;
for (unsigned int i=0; i<s.length(); ++i) {
if (isalnum(s[i]))
retval.append(1,s[i]);
else if (s[i] == '/' || s[i] == ' ' || s[i] == '\r' || s[i] == '\n')
retval.append(1,'_');
}
if (retval.length() == 0)
retval = "article.txt";
else
retval.append(".txt");
LOG(LOG_DEBUG,"view::get_filename_suggestion: %s -> %s", s.c_str(), retval.c_str());
return retval;
}
void view::push_empty_formaction() {
formaction_stack.push_back(std::shared_ptr<formaction>());
current_formaction = formaction_stack_size() - 1;
}
void view::open_in_pager(const std::string& filename) {
formaction_stack.push_back(std::shared_ptr<formaction>());
current_formaction = formaction_stack_size() - 1;
std::string cmdline;
std::string pager = cfg->get_configvalue("pager");
if (pager.find("%f") != std::string::npos) {
fmtstr_formatter fmt;
fmt.register_fmt('f', filename);
cmdline = fmt.do_format(pager, 0);
} else {
const char * env_pager = NULL;
if (pager != "")
cmdline.append(pager);
else if ((env_pager = getenv("PAGER")) != NULL)
cmdline.append(env_pager);
else
cmdline.append("more");
cmdline.append(" ");
cmdline.append(filename);
}
stfl::reset();
LOG(LOG_DEBUG, "view::open_in_browser: running `%s'", cmdline.c_str());
::system(cmdline.c_str());
pop_current_formaction();
}
void view::open_in_browser(const std::string& url) {
formaction_stack.push_back(std::shared_ptr<formaction>());
current_formaction = formaction_stack_size() - 1;
std::string cmdline;
std::string browser = cfg->get_configvalue("browser");
if (browser.find("%u") != std::string::npos) {
fmtstr_formatter fmt;
std::string newurl;
newurl = utils::replace_all(url, "'", "%27");
newurl.insert(0, "'");
newurl.append("'");
fmt.register_fmt('u', newurl);
cmdline = fmt.do_format(browser, 0);
} else {
if (browser != "")
cmdline.append(browser);
else
cmdline.append("lynx");
cmdline.append(" '");
cmdline.append(utils::replace_all(url,"'", "%27"));
cmdline.append("'");
}
stfl::reset();
LOG(LOG_DEBUG, "view::open_in_browser: running `%s'", cmdline.c_str());
::system(cmdline.c_str());
pop_current_formaction();
}
void view::update_visible_feeds(std::vector<std::shared_ptr<rss_feed>> feeds) {
try {
if (formaction_stack_size() > 0) {
std::lock_guard<std::mutex> lock(mtx);
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
feedlist->update_visible_feeds(feeds);
}
} catch (const matcherexception& e) {
set_status(utils::strprintf(_("Error: applying the filter failed: %s"), e.what()));
LOG(LOG_DEBUG, "view::update_visible_feeds: inside catch: %s", e.what());
}
}
void view::set_feedlist(std::vector<std::shared_ptr<rss_feed>> feeds) {
try {
std::lock_guard<std::mutex> lock(mtx);
for (auto feed : feeds) {
if (feed->rssurl().substr(0,6) != "query:") {
feed->set_feedptrs(feed);
}
}
if (formaction_stack_size() > 0) {
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
feedlist->set_feedlist(feeds);
}
} catch (const matcherexception& e) {
set_status(utils::strprintf(_("Error: applying the filter failed: %s"), e.what()));
}
}
void view::set_tags(const std::vector<std::string>& t) {
tags = t;
}
void view::push_searchresult(std::shared_ptr<rss_feed> feed, const std::string& phrase) {
assert(feed != NULL);
LOG(LOG_DEBUG, "view::push_searchresult: pushing search result");
if (feed->items().size() > 0) {
std::shared_ptr<itemlist_formaction> searchresult(new itemlist_formaction(this, itemlist_str));
set_bindings(searchresult);
searchresult->set_regexmanager(rxman);
searchresult->set_feed(feed);
searchresult->set_show_searchresult(true);
searchresult->set_searchphrase(phrase);
apply_colors(searchresult);
searchresult->set_parent_formaction(get_current_formaction());
searchresult->init();
formaction_stack.push_back(searchresult);
current_formaction = formaction_stack_size() - 1;
} else {
show_error(_("Error: feed contains no items!"));
}
}
void view::push_itemlist(std::shared_ptr<rss_feed> feed) {
assert(feed != NULL);
feed->purge_deleted_items();
prepare_query_feed(feed);
if (feed->items().size() > 0) {
std::shared_ptr<itemlist_formaction> itemlist(new itemlist_formaction(this, itemlist_str));
set_bindings(itemlist);
itemlist->set_regexmanager(rxman);
itemlist->set_feed(feed);
itemlist->set_show_searchresult(false);
apply_colors(itemlist);
itemlist->set_parent_formaction(get_current_formaction());
itemlist->init();
formaction_stack.push_back(itemlist);
current_formaction = formaction_stack_size() - 1;
} else {
show_error(_("Error: feed contains no items!"));
}
}
void view::push_itemlist(unsigned int pos) {
std::shared_ptr<rss_feed> feed = ctrl->get_feed(pos);
LOG(LOG_DEBUG, "view::push_itemlist: retrieved feed at position %d", pos);
push_itemlist(feed);
if (feed->items().size() > 0) {
std::shared_ptr<itemlist_formaction> itemlist = std::dynamic_pointer_cast<itemlist_formaction, formaction>(get_current_formaction());
itemlist->set_pos(pos);
}
}
void view::push_itemview(std::shared_ptr<rss_feed> f, const std::string& guid, const std::string& searchphrase) {
std::string pager;
if ((pager = cfg->get_configvalue("pager")) == "internal") {
std::shared_ptr<itemlist_formaction> itemlist = std::dynamic_pointer_cast<itemlist_formaction, formaction>(get_current_formaction());
assert(itemlist != NULL);
std::shared_ptr<itemview_formaction> itemview(new itemview_formaction(this, itemlist, itemview_str));
set_bindings(itemview);
itemview->set_regexmanager(rxman);
itemview->set_feed(f);
itemview->set_guid(guid);
itemview->set_parent_formaction(get_current_formaction());
if (searchphrase.length() > 0)
itemview->set_highlightphrase(searchphrase);
apply_colors(itemview);
itemview->init();
formaction_stack.push_back(itemview);
current_formaction = formaction_stack_size() - 1;
} else {
std::string filename = get_ctrl()->write_temporary_item(f->get_item_by_guid(guid));
open_in_pager(filename);
::unlink(filename.c_str());
}
}
void view::view_dialogs() {
if (get_current_formaction() != NULL && get_current_formaction()->id() != "dialogs") {
std::shared_ptr<dialogs_formaction> dialogs(new dialogs_formaction(this, dialogs_str));
dialogs->set_parent_formaction(get_current_formaction());
apply_colors(dialogs);
dialogs->init();
formaction_stack.push_back(dialogs);
current_formaction = formaction_stack_size() - 1;
}
}
void view::push_help() {
std::shared_ptr<help_formaction> helpview(new help_formaction(this, help_str));
set_bindings(helpview);
apply_colors(helpview);
helpview->set_context(get_current_formaction()->id());
helpview->set_parent_formaction(get_current_formaction());
helpview->init();
formaction_stack.push_back(helpview);
current_formaction = formaction_stack_size() - 1;
}
void view::push_urlview(const std::vector<linkpair>& links) {
std::shared_ptr<urlview_formaction> urlview(new urlview_formaction(this, urlview_str));
set_bindings(urlview);
apply_colors(urlview);
urlview->set_parent_formaction(get_current_formaction());
urlview->init();
urlview->set_links(links);
formaction_stack.push_back(urlview);
current_formaction = formaction_stack_size() - 1;
}
std::string view::run_filebrowser(const std::string& default_filename, const std::string& dir) {
std::shared_ptr<filebrowser_formaction> filebrowser(new filebrowser_formaction(this, filebrowser_str));
set_bindings(filebrowser);
apply_colors(filebrowser);
filebrowser->set_dir(dir);
filebrowser->set_default_filename(default_filename);
filebrowser->set_parent_formaction(get_current_formaction());
return run_modal(filebrowser, "filenametext");
}
std::string view::select_tag() {
if (tags.size() == 0) {
show_error(_("No tags defined."));
return "";
}
std::shared_ptr<select_formaction> selecttag(new select_formaction(this, selecttag_str));
selecttag->set_type(select_formaction::SELECTTAG);
set_bindings(selecttag);
apply_colors(selecttag);
selecttag->set_parent_formaction(get_current_formaction());
selecttag->set_tags(tags);
run_modal(selecttag, "");
return selecttag->get_selected_value();
}
std::string view::select_filter(const std::vector<filter_name_expr_pair>& filters) {
std::shared_ptr<select_formaction> selecttag(new select_formaction(this, selecttag_str));
selecttag->set_type(select_formaction::SELECTFILTER);
set_bindings(selecttag);
apply_colors(selecttag);
selecttag->set_parent_formaction(get_current_formaction());
selecttag->set_filters(filters);
run_modal(selecttag, "");
return selecttag->get_selected_value();
}
char view::confirm(const std::string& prompt, const std::string& charset) {
LOG(LOG_DEBUG, "view::confirm: charset = %s", charset.c_str());
std::shared_ptr<formaction> f = get_current_formaction();
formaction_stack.push_back(std::shared_ptr<formaction>());
current_formaction = formaction_stack_size() - 1;
f->get_form()->set("msg", prompt);
char result = 0;
do {
const char * event = f->get_form()->run(0);
LOG(LOG_DEBUG,"view::confirm: event = %s", event);
if (!event) continue;
if (strcmp(event, "ESC")==0 || strcmp(event, "ENTER")==0) {
result = 0;
LOG(LOG_DEBUG, "view::confirm: user pressed ESC or ENTER, we cancel confirmation dialog");
break;
}
result = keys->get_key(event);
LOG(LOG_DEBUG, "view::confirm: key = %c (%u)", result, result);
} while (!result || strchr(charset.c_str(), result)==NULL);
f->get_form()->set("msg", "");
f->get_form()->run(-1);
pop_current_formaction();
return result;
}
void view::notify_itemlist_change(std::shared_ptr<rss_feed> feed) {
for (auto& form : formaction_stack) {
if (form != NULL && form->id() == "articlelist") {
std::shared_ptr<itemlist_formaction> itemlist = std::dynamic_pointer_cast<itemlist_formaction, formaction>(form);
if (itemlist != NULL) {
std::shared_ptr<rss_feed> f = itemlist->get_feed();
if (f != NULL && f->rssurl() == feed->rssurl()) {
itemlist->set_feed(feed);
itemlist->set_redraw(true);
}
}
}
}
}
bool view::get_random_unread(itemlist_formaction * itemlist, itemview_formaction * itemview) {
unsigned int feedpos;
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
if (!cfg->get_configvalue_as_bool("goto-next-feed")) {
return false;
}
if (feedlist->jump_to_random_unread_feed(feedpos)) {
LOG(LOG_DEBUG, "view::get_previous_unread: found feed with unread articles");
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
if (itemlist->jump_to_random_unread_item()) {
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
}
}
return false;
}
bool view::get_previous_unread(itemlist_formaction * itemlist, itemview_formaction * itemview) {
unsigned int feedpos;
LOG(LOG_DEBUG, "view::get_previous_unread: trying to find previous unread");
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
if (itemlist->jump_to_previous_unread_item(false)) {
LOG(LOG_DEBUG, "view::get_previous_unread: found unread article in same feed");
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
} else if (cfg->get_configvalue_as_bool("goto-next-feed")==false) {
LOG(LOG_DEBUG, "view::get_previous_unread: goto-next-feed = false");
show_error(_("No unread items."));
} else if (feedlist->jump_to_previous_unread_feed(feedpos)) {
LOG(LOG_DEBUG, "view::get_previous_unread: found feed with unread articles");
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
if (itemlist->jump_to_previous_unread_item(true)) {
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
}
}
return false;
}
bool view::get_next_unread_feed(itemlist_formaction * itemlist) {
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
unsigned int feedpos;
assert(feedlist != NULL);
if (feedlist->jump_to_next_unread_feed(feedpos)) {
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
return true;
}
return false;
}
bool view::get_prev_unread_feed(itemlist_formaction * itemlist) {
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
unsigned int feedpos;
assert(feedlist != NULL);
if (feedlist->jump_to_previous_unread_feed(feedpos)) {
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
return true;
}
return false;
}
bool view::get_next_unread(itemlist_formaction * itemlist, itemview_formaction * itemview) {
unsigned int feedpos;
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
LOG(LOG_DEBUG, "view::get_next_unread: trying to find next unread");
if (itemlist->jump_to_next_unread_item(false)) {
LOG(LOG_DEBUG, "view::get_next_unread: found unread article in same feed");
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
} else if (cfg->get_configvalue_as_bool("goto-next-feed")==false) {
LOG(LOG_DEBUG, "view::get_next_unread: goto-next-feed = false");
show_error(_("No unread items."));
} else if (feedlist->jump_to_next_unread_feed(feedpos)) {
LOG(LOG_DEBUG, "view::get_next_unread: found feed with unread articles");
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
if (itemlist->jump_to_next_unread_item(true)) {
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
}
}
return false;
}
bool view::get_previous(itemlist_formaction * itemlist, itemview_formaction * itemview) {
unsigned int feedpos;
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
if (itemlist->jump_to_previous_item(false)) {
LOG(LOG_DEBUG, "view::get_previous: article in same feed");
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
} else if (cfg->get_configvalue_as_bool("goto-next-feed")==false) {
LOG(LOG_DEBUG, "view::get_previous: goto-next-feed = false");
show_error(_("Already on first item."));
} else if (feedlist->jump_to_previous_feed(feedpos)) {
LOG(LOG_DEBUG, "view::get_previous: previous feed");
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
if (itemlist->jump_to_previous_item(true)) {
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
}
}
return false;
}
bool view::get_next(itemlist_formaction * itemlist, itemview_formaction * itemview) {
unsigned int feedpos;
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
if (itemlist->jump_to_next_item(false)) {
LOG(LOG_DEBUG, "view::get_next: article in same feed");
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
} else if (cfg->get_configvalue_as_bool("goto-next-feed")==false) {
LOG(LOG_DEBUG, "view::get_next: goto-next-feed = false");
show_error(_("Already on last item."));
} else if (feedlist->jump_to_next_feed(feedpos)) {
LOG(LOG_DEBUG, "view::get_next: next feed");
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
if (itemlist->jump_to_next_item(true)) {
if (itemview) {
itemview->init();
itemview->set_feed(itemlist->get_feed());
itemview->set_guid(itemlist->get_guid());
}
return true;
}
}
return false;
}
bool view::get_next_feed(itemlist_formaction * itemlist) {
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
unsigned int feedpos;
assert(feedlist != NULL);
if (feedlist->jump_to_next_feed(feedpos)) {
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
return true;
}
return false;
}
bool view::get_prev_feed(itemlist_formaction * itemlist) {
std::shared_ptr<feedlist_formaction> feedlist = std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0]);
unsigned int feedpos;
assert(feedlist != NULL);
if (feedlist->jump_to_previous_feed(feedpos)) {
prepare_query_feed(feedlist->get_feed());
itemlist->set_feed(feedlist->get_feed());
itemlist->set_pos(feedpos);
itemlist->init();
return true;
}
return false;
}
void view::prepare_query_feed(std::shared_ptr<rss_feed> feed) {
if (feed->rssurl().substr(0,6) == "query:") {
LOG(LOG_DEBUG, "view::prepare_query_feed: %s", feed->rssurl().c_str());
set_status(_("Updating query feed..."));
feed->update_items(ctrl->get_all_feeds());
feed->sort(cfg->get_configvalue("article-sort-order"));
notify_itemlist_change(feed);
set_status("");
}
}
void view::force_redraw() {
std::shared_ptr<formaction> fa = get_current_formaction();
if (fa != NULL) {
fa->set_redraw(true);
fa->prepare();
fa->get_form()->run(-1);
}
}
void view::pop_current_formaction() {
std::shared_ptr<formaction> f = get_current_formaction();
auto it=formaction_stack.begin();
for (unsigned int i=0; i<current_formaction; i++)
++it;
formaction_stack.erase(it);
if (f == NULL) {
current_formaction = formaction_stack_size() - 1; // XXX TODO this is not correct... we'd need to return to the previous one, but NULL formactions have no parent
} else if (formaction_stack.size() > 0) {
// first, we set back the parent formactions of those who reference the formaction we just removed
for (auto& form : formaction_stack) {
if (form->get_parent_formaction() == f) {
form->set_parent_formaction(formaction_stack[0]);
}
}
// we set the new formaction based on the removed formaction's parent.
unsigned int i=0;
for (auto& form : formaction_stack) {
if (form == f->get_parent_formaction()) {
current_formaction = i;
break;
}
i++;
}
std::shared_ptr<formaction> f = get_current_formaction();
if (f) {
f->set_redraw(true);
f->get_form()->set("msg","");
f->recalculate_form();
}
}
}
void view::set_current_formaction(unsigned int pos) {
remove_formaction(current_formaction);
current_formaction = pos;
}
void view::remove_formaction(unsigned int pos) {
std::shared_ptr<formaction> f = formaction_stack[pos];
auto it = formaction_stack.begin();
for (unsigned int i=0; i<pos; i++)
++it;
formaction_stack.erase(it);
current_formaction--;
if (f != NULL && formaction_stack.size() > 0) {
// we set back the parent formactions of those who reference the formaction we just removed
for (auto& form : formaction_stack) {
if (form->get_parent_formaction() == f) {
form->set_parent_formaction(formaction_stack[0]);
}
}
}
}
void view::set_colors(std::map<std::string,std::string>& fgc, std::map<std::string,std::string>& bgc, std::map<std::string,std::vector<std::string>>& attribs) {
fg_colors = fgc;
bg_colors = bgc;
attributes = attribs;
}
void view::apply_colors_to_all_formactions() {
for (auto& form : formaction_stack) {
apply_colors(form);
}
if (formaction_stack.size() > 0 && formaction_stack[current_formaction]) {
formaction_stack[current_formaction]->set_redraw(true);
}
}
void view::apply_colors(std::shared_ptr<formaction> fa) {
auto fgcit = fg_colors.begin();
auto bgcit = bg_colors.begin();
auto attit = attributes.begin();
LOG(LOG_DEBUG, "view::apply_colors: fa = %s", fa->id().c_str());
std::string article_colorstr;
for (; fgcit != fg_colors.end(); ++fgcit, ++bgcit, ++attit) {
std::string colorattr;
if (fgcit->second != "default") {
colorattr.append("fg=");
colorattr.append(fgcit->second);
}
if (bgcit->second != "default") {
if (colorattr.length() > 0)
colorattr.append(",");
colorattr.append("bg=");
colorattr.append(bgcit->second);
}
for (auto attr : attit->second) {
if (colorattr.length() > 0)
colorattr.append(",");
colorattr.append("attr=");
colorattr.append(attr);
}
if (fgcit->first == "article") {
article_colorstr = colorattr;
if (fa->id() == "article") {
std::string bold = article_colorstr;
std::string ul = article_colorstr;
if (bold.length() > 0)
bold.append(",");
if (ul.length() > 0)
ul.append(",");
bold.append("attr=bold");
ul.append("attr=underline");
fa->get_form()->set("color_bold", bold.c_str());
fa->get_form()->set("color_underline", ul.c_str());
}
}
LOG(LOG_DEBUG,"view::apply_colors: %s %s %s\n", fa->id().c_str(), fgcit->first.c_str(), colorattr.c_str());
fa->get_form()->set(fgcit->first, colorattr);
if (fgcit->first == "article") {
if (fa->id() == "article" || fa->id() == "help") {
std::string styleend_str;
if (bgcit->second != "default") {
styleend_str.append("bg=");
styleend_str.append(bgcit->second);
}
if (styleend_str.length() > 0)
styleend_str.append(",");
styleend_str.append("attr=bold");
fa->get_form()->set("styleend", styleend_str.c_str());
}
}
}
}
void view::feedlist_mark_pos_if_visible(unsigned int pos) {
if (formaction_stack_size() > 0) {
std::dynamic_pointer_cast<feedlist_formaction, formaction>(formaction_stack[0])->mark_pos_if_visible(pos);
}
}
void view::set_regexmanager(regexmanager * r) {
rxman = r;
}
std::vector<std::pair<unsigned int, std::string>> view::get_formaction_names() {
std::vector<std::pair<unsigned int, std::string>> formaction_names;
unsigned int i=0;
for (auto& form : formaction_stack) {
if (form && form->id() != "dialogs") {
formaction_names.push_back(std::pair<unsigned int, std::string>(i, form->title()));
}
i++;
}
return formaction_names;
}
void view::goto_next_dialog() {
current_formaction++;
if (current_formaction >= formaction_stack.size())
current_formaction = 0;
}
void view::goto_prev_dialog() {
if (current_formaction > 0) {
current_formaction--;
} else {
current_formaction = formaction_stack.size() - 1;
}
}
void view::inside_qna(bool f) {
is_inside_qna = f;
}
void view::inside_cmdline(bool f) {
is_inside_cmdline = f;