summaryrefslogtreecommitdiff
path: root/src/view.cpp
blob: 2763c5208cf06db72913dceef7761de769cdd6a0 (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
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
#include "feedlist.h"
#include "itemlist.h"
#include "itemview.h"

extern "C" {
#include <stfl.h>
}

#include <view.h>
#include <rss.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace noos;

view::view(controller * c) : ctrl(c) { 
	feedlist_form = stfl_create(feedlist_str);
	itemlist_form = stfl_create(itemlist_str);
	itemview_form = stfl_create(itemview_str);
}

view::~view() {
	stfl_reset();
	stfl_free(feedlist_form);
	stfl_free(itemlist_form);
	stfl_free(itemview_form);
}

void view::feedlist_status(char * msg) {
	stfl_set(feedlist_form,"msg",msg);
	stfl_run(feedlist_form,-1);
}

void view::itemlist_status(char * msg) {
	stfl_set(itemlist_form,"msg",msg);
	stfl_run(itemlist_form,-1);
}

void view::feedlist_error(char * msg) {
	feedlist_status(msg);
	::sleep(2);
	feedlist_status("");
}

void view::itemlist_error(char * msg) {
	itemlist_status(msg);
	::sleep(2);
	itemlist_status("");
}

void view::run_feedlist() {
	bool quit = false;
	do {
		const char * event = stfl_run(feedlist_form,0);
		if (!event) continue;

		if (strcmp(event,"ENTER")==0) {
			const char * feedposname = stfl_get(feedlist_form, "feedposname");
			if (feedposname) {
				std::istringstream posname(feedposname);
				unsigned int pos = 0;
				posname >> pos;
				ctrl->open_feed(pos);
			} else {
				feedlist_error("Error: no feed selected!"); // should not happen
			}
		} else if (strncmp(event,"CHAR(",5)==0) {

			unsigned int x;
			char c;
			sscanf(event,"CHAR(%d)",&x); // XXX: refactor

			c = static_cast<char>(x);

			switch (c) {
				case 'r':
					// ctrl->reload();
					break;
				case 'R':
					// ctrl->reload_all();
					break;
				case 'q':
					quit = true;
					break;
				default:
					break;
			}

		}
	} while (!quit);
}

void view::run_itemlist(rss_feed& feed) {
	bool quit = false;

	std::vector<rss_item>& items = feed.items();

	std::string code = "{list";

	unsigned int i=0;
	for (std::vector<rss_item>::iterator it = items.begin(); it != items.end(); ++it, ++i) {
		std::string line = "{listitem[";
		std::ostringstream x;
		x << i;
		line.append(x.str());
		line.append("] text:");
		std::string title = it->title();
		line.append(stfl_quote(title.c_str()));
		line.append("}");
		code.append(line);
	}

	code.append("}");

	stfl_modify(itemlist_form,"items","replace_inner",code.c_str());

	do {
		const char * event = stfl_run(itemlist_form,0);
		if (!event) continue;

		if (strcmp(event,"ENTER")==0) {
			const char * itemposname = stfl_get(itemlist_form, "itemposname");
			if (itemposname) {
				std::istringstream posname(itemposname);
				unsigned int pos = 0;
				posname >> pos;
				ctrl->open_item(items[pos]);
			} else {
				itemlist_error("Error: no item selected!"); // should not happen
			}
		} else if (strncmp(event,"CHAR(",5)==0) {

			unsigned int x;
			char c;
			sscanf(event,"CHAR(%d)",&x); // XXX: refactor

			c = static_cast<char>(x);

			switch (c) {
				case 's':
					// TODO: save currently selected article
					break;
				case 'q':
					quit = true;
					break;
			}
		}

	} while (!quit);
}

void view::run_itemview(rss_item& item) {
	bool quit = false;

	std::string code = "{list";

	code.append("{listitem text:");
	std::ostringstream title;
	title << "Title: ";
	title << item.title();
	code.append(stfl_quote(title.str().c_str()));
	code.append("}");

	code.append("{listitem text:");
	std::ostringstream author;
	author << "Author: ";
	author << item.author();
	code.append(stfl_quote(author.str().c_str()));
	code.append("}");

	code.append("{listitem text:");
	std::ostringstream link;
	link << "Link: ";
	link << item.link();
	code.append(stfl_quote(link.str().c_str()));
	code.append("}");

	code.append("{listitem text:\"\"}");

	code.append("{listitem text:");
	code.append(stfl_quote(item.description().c_str()));
	code.append("}");

	code.append("}");

	stfl_modify(itemview_form,"article","replace_inner",code.c_str());

	do {
		const char * event = stfl_run(itemview_form,0);
		if (!event) continue;

		if (strcmp(event,"ENTER")==0) {
			// nothing
		} else if (strncmp(event,"CHAR(",5)==0) {

			unsigned int x;
			char c;
			sscanf(event,"CHAR(%d)",&x); // XXX: refactor

			c = static_cast<char>(x);

			switch (c) {
				case 's':
					// TODO: save currently selected article
					break;
				case 'q':
					quit = true;
					break;
			}
		}


	} while (!quit);
}

void view::set_feedlist(const std::vector<std::string>& feeds) {
	std::string code = "{list";

	unsigned int i = 0;
	for (std::vector<std::string>::const_iterator it = feeds.begin(); it != feeds.end(); ++it, ++i) {
		std::string line = "{listitem[";
		std::ostringstream num;
		num << i;
		line.append(num.str());
		line.append("] text:");
		line.append(stfl_quote(it->c_str()));
		line.append("}");

		code.append(line);
	}

	code.append("}");

	// std::cerr << code << std::endl;

	stfl_modify(feedlist_form,"feeds","replace_inner",code.c_str());
}