aboutsummaryrefslogtreecommitdiff
path: root/src/urlviewformaction.cpp
blob: 0490df1d55ec2e991f9e85c5373486eb70ceee99 (plain) (blame)
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
#include "urlviewformaction.h"

#include <string>

#include "config.h"
#include "fmtstrformatter.h"
#include "rssfeed.h"
#include "stflrichtext.h"
#include "strprintf.h"
#include "utils.h"
#include "view.h"

namespace newsboat {

/*
 * The UrlViewFormAction is probably the simplest dialog of all. It
 * displays a list of URLs, and makes it possible to open the URLs
 * in a browser or to bookmark them.
 */

UrlViewFormAction::UrlViewFormAction(View& vv,
	std::shared_ptr<RssFeed>& feed,
	std::string formstr,
	ConfigContainer* cfg)
	: FormAction(vv, formstr, cfg)
	, quit(false)
	, feed(feed)
	, urls_list("urls", FormAction::f, cfg->get_configvalue_as_int("scrolloff"))
{
}

bool UrlViewFormAction::process_operation(Operation op,
	const std::vector<std::string>& /* args */,
	BindingType /*bindingType*/)
{
	bool hardquit = false;
	switch (op) {
	case OP_PREV:
		urls_list.move_up(cfg->get_configvalue_as_bool("wrap-scroll"));
		break;
	case OP_NEXT:
		urls_list.move_down(cfg->get_configvalue_as_bool("wrap-scroll"));
		break;
	case OP_OPENINBROWSER:
	case OP_OPENBROWSER_AND_MARK:
	case OP_OPEN: {
		const bool interactive = true;
		open_current_position_in_browser(interactive);
	}
	break;
	case OP_OPENINBROWSER_NONINTERACTIVE: {
		const bool interactive = false;
		open_current_position_in_browser(interactive);
	}
	break;
	case OP_BOOKMARK: {
		if (!links.empty()) {
			const unsigned int pos = urls_list.get_position();
			this->start_bookmark_qna("", links[pos].url, feed->title());
		} else {
			v.get_statusline().show_error(_("No links available!"));
		}
	}
	break;
	case OP_OPEN_URL_1:
	case OP_OPEN_URL_2:
	case OP_OPEN_URL_3:
	case OP_OPEN_URL_4:
	case OP_OPEN_URL_5:
	case OP_OPEN_URL_6:
	case OP_OPEN_URL_7:
	case OP_OPEN_URL_8:
	case OP_OPEN_URL_9:
	case OP_OPEN_URL_10: {
		unsigned int idx = op - OP_OPEN_URL_1;

		if (idx < links.size()) {
			const std::string feedurl = (feed != nullptr ?  feed->rssurl() : "");
			const bool interactive = true;
			v.open_in_browser(links[idx].url, feedurl, utils::link_type_str(links[idx].type),
				feed->title(), interactive);
		}
	}
	break;
	case OP_HELP:
		v.push_help();
		break;
	case OP_QUIT:
		quit = true;
		break;
	case OP_HARDQUIT:
		hardquit = true;
		break;
	default:
		if (handle_list_operations(urls_list, op)) {
			break;
		}
		break;
	}
	if (hardquit) {
		while (v.formaction_stack_size() > 0) {
			v.pop_current_formaction();
		}
	} else if (quit) {
		v.pop_current_formaction();
	}
	return true;
}

void UrlViewFormAction::open_current_position_in_browser(bool interactive)
{
	if (!links.empty()) {
		const unsigned int pos = urls_list.get_position();
		const std::string feedurl = (feed != nullptr ?  feed->rssurl() : "");
		v.open_in_browser(links[pos].url, feedurl, utils::link_type_str(links[pos].type),
			feed->title(), interactive);
	} else {
		v.get_statusline().show_error(_("No links available!"));
	}
}

void UrlViewFormAction::prepare()
{
	if (do_redraw) {
		update_heading();

		auto render_line = [this](std::uint32_t line, std::uint32_t width) -> StflRichText {
			(void)width;
			const auto& link = links[line];
			return StflRichText::from_plaintext(strprintf::fmt("%2u  %s", line + 1, link.url));
		};

		urls_list.invalidate_list_content(links.size(), render_line);
	}
}

void UrlViewFormAction::init()
{
	recalculate_widget_dimensions();

	do_redraw = true;
	quit = false;
	set_keymap_hints();
}

void UrlViewFormAction::update_heading()
{
	const unsigned int width = urls_list.get_width();

	FmtStrFormatter fmt;
	fmt.register_fmt('N', PROGRAM_NAME);
	fmt.register_fmt('V', utils::program_version());

	set_title(fmt.do_format(cfg->get_configvalue("urlview-title-format"), width));
}

std::vector<KeyMapHintEntry> UrlViewFormAction::get_keymap_hint() const
{
	static const std::vector<KeyMapHintEntry> hints = {{OP_QUIT, _("Quit")},
		{OP_OPEN, _("Open in Browser")},
		{OP_BOOKMARK, _("Save Bookmark")},
		{OP_HELP, _("Help")}
	};
	return hints;
}

void UrlViewFormAction::handle_cmdline(const std::string& cmd)
{
	unsigned int idx = 0;
	if (1 == sscanf(cmd.c_str(), "%u", &idx)) {
		if (idx < 1 || idx > links.size()) {
			v.get_statusline().show_error(_("Invalid position!"));
		} else {
			urls_list.set_position(idx - 1);
		}
	} else {
		FormAction::handle_cmdline(cmd);
	}
}

std::string UrlViewFormAction::title()
{
	return _("URLs");
}

} // namespace newsboat