~ubuntu-branches/ubuntu/trusty/newsbeuter/trusty-proposed

« back to all changes in this revision

Viewing changes to src/feedlist_formaction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2008-05-01 14:51:20 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080501145120-i65avsclnrpl2cfq
Tags: 0.9-1
* New upstream release.
  - Fix problem displaying titles when using zh_CN.UTF-8
    locale (Closes: #471434).
* Add default configuration to set sensible-browser as browser and
  add dirs file to create /etc/newsbeuter (Closes: #470833).
* remove libnxml-depends and libmrss-depends calls in rules and
  related dependency lines in control since they are not needed
  anymore (Closes: #467201).

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include <utils.h>
9
9
#include <formatstring.h>
10
10
 
 
11
#include <listformatter.h>
 
12
 
11
13
#include <sstream>
12
14
#include <string>
13
15
 
 
16
#include <langinfo.h>
 
17
 
14
18
#define FILTER_UNREAD_FEEDS "unread_count != \"0\""
15
19
 
16
20
namespace newsbeuter {
17
21
 
18
22
feedlist_formaction::feedlist_formaction(view * vv, std::string formstr) 
19
23
        : formaction(vv,formstr), zero_feedpos(false), feeds_shown(0),
20
 
                auto_open(false), quit(false), apply_filter(false), search_dummy_feed(v->get_ctrl()->get_cache()) {
 
24
                auto_open(false), quit(false), apply_filter(false), search_dummy_feed(v->get_ctrl()->get_cache()),
 
25
                filterpos(0), set_filterpos(false) {
21
26
        assert(true==m.parse(FILTER_UNREAD_FEEDS));
22
27
}
23
28
 
54
59
 
55
60
        std::string listwidth = f->get("items:w");
56
61
        std::istringstream is(listwidth);
57
 
        unsigned int width;
 
62
        unsigned int width = 0;
58
63
        is >> width;
59
64
 
60
65
        if (old_width != width) {
67
72
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::prepare: doing redraw");
68
73
                do_redraw = false;
69
74
                v->get_ctrl()->update_feedlist();
70
 
                if (zero_feedpos) {
 
75
                if (set_filterpos) {
 
76
                        set_filterpos = false;
 
77
                        unsigned int i = 0;
 
78
                        for (std::vector<feedptr_pos_pair>::iterator it=visible_feeds.begin();it!=visible_feeds.end();++it, ++i) {
 
79
                                if (it->second == filterpos) {
 
80
                                        f->set("feedpos", utils::to_s(i));
 
81
                                        return;
 
82
                                }
 
83
                        }
 
84
                        f->set("feedpos", "0");
 
85
                } else if (zero_feedpos) {
71
86
                        f->set("feedpos","0");
72
87
                        zero_feedpos = false;
73
88
                }
112
127
                        break;
113
128
                case OP_RELOADALL:
114
129
                        GetLogger().log(LOG_INFO, "feedlist_formaction: reloading all feeds");
115
 
                        v->get_ctrl()->start_reload_all_thread();
 
130
                        {
 
131
                                bool reload_only_visible_feeds = v->get_cfg()->get_configvalue_as_bool("reload-only-visible-feeds");
 
132
                                std::vector<int> idxs;
 
133
                                for (std::vector<feedptr_pos_pair>::iterator it=visible_feeds.begin();it!=visible_feeds.end();++it) {
 
134
                                        idxs.push_back(it->second);
 
135
                                }
 
136
                                v->get_ctrl()->start_reload_all_thread(reload_only_visible_feeds ? &idxs : NULL);
 
137
                        }
116
138
                        break;
117
139
                case OP_MARKFEEDREAD: {
118
140
                                GetLogger().log(LOG_INFO, "feedlist_formaction: marking feed read at position `%s'",feedpos.c_str());
123
145
                                                do_redraw = true;
124
146
                                                v->set_status("");
125
147
                                        } catch (const dbexception& e) {
126
 
                                                char buf[1024];
127
 
                                                snprintf(buf, sizeof(buf), _("Error: couldn't mark feed read: %s"), e.what());
128
 
                                                v->show_error(buf);
 
148
                                                v->show_error(utils::strprintf(_("Error: couldn't mark feed read: %s"), e.what()));
129
149
                                        }
130
150
                                } else {
131
151
                                        v->show_error(_("No feed selected!")); // should not happen
138
158
                        if (v->get_cfg()->get_configvalue_as_bool("show-read-feeds")) {
139
159
                                v->get_cfg()->set_configvalue("show-read-feeds","no");
140
160
                                apply_filter = true;
141
 
                                f->set("feedpos", "0");
142
161
                        } else {
143
162
                                v->get_cfg()->set_configvalue("show-read-feeds","yes");
144
163
                                apply_filter = false;
145
164
                        }
 
165
                        save_filterpos();
146
166
                        do_redraw = true;
147
167
                        break;
148
168
                case OP_NEXTUNREAD: {
205
225
                                                        v->show_error(_("Error: couldn't parse filter command!"));
206
226
                                                        m.parse(FILTER_UNREAD_FEEDS);
207
227
                                                } else {
 
228
                                                        save_filterpos();
208
229
                                                        apply_filter = true;
209
230
                                                        do_redraw = true;
210
231
                                                }
232
253
                        apply_filter = !(v->get_cfg()->get_configvalue_as_bool("show-read-feeds"));
233
254
                        m.parse(FILTER_UNREAD_FEEDS);
234
255
                        do_redraw = true;
 
256
                        save_filterpos();
235
257
                        break;
236
258
                case OP_SETFILTER:
237
259
                        if (automatic && args->size() > 0) {
261
283
        }
262
284
}
263
285
 
 
286
void feedlist_formaction::update_visible_feeds(std::vector<rss_feed>& feeds) {
 
287
        assert(v->get_cfg() != NULL); // must not happen
 
288
 
 
289
        if (visible_feeds.size() > 0)
 
290
                visible_feeds.erase(visible_feeds.begin(), visible_feeds.end());
 
291
 
 
292
        unsigned int i = 0;
 
293
 
 
294
        for (std::vector<rss_feed>::iterator it = feeds.begin(); it != feeds.end(); ++it, ++i) {
 
295
                if ((tag == "" || it->matches_tag(tag)) && (!apply_filter || m.matches(&(*it)))) {
 
296
                        visible_feeds.push_back(feedptr_pos_pair(&(*it),i));
 
297
                }
 
298
        }
 
299
 
 
300
        feeds_shown = visible_feeds.size();
 
301
}
 
302
 
264
303
void feedlist_formaction::set_feedlist(std::vector<rss_feed>& feeds) {
265
 
        std::string code = "{list";
266
 
        
267
304
        assert(v->get_cfg() != NULL); // must not happen
268
305
 
269
306
        std::string listwidth = f->get("feeds:w");
271
308
        unsigned int width;
272
309
        is >> width;
273
310
 
274
 
        feeds_shown = 0;
275
311
        unsigned int i = 0;
276
312
        unsigned short feedlist_number = 1;
277
313
        unsigned int unread_feeds = 0;
278
314
 
279
 
        if (visible_feeds.size() > 0)
280
 
                visible_feeds.erase(visible_feeds.begin(), visible_feeds.end());
281
 
 
282
315
        std::string feedlist_format = v->get_cfg()->get_configvalue("feedlist-format");
283
316
 
284
 
        for (std::vector<rss_feed>::iterator it = feeds.begin(); it != feeds.end(); ++it, ++i, ++feedlist_number) {
285
 
                rss_feed feed = *it;
286
 
                std::string title = it->title();
 
317
        listformatter listfmt;
 
318
 
 
319
        update_visible_feeds(feeds);
 
320
 
 
321
        for (std::vector<feedptr_pos_pair>::iterator it = visible_feeds.begin(); it != visible_feeds.end(); ++it, ++i, ++feedlist_number) {
 
322
                std::string title = it->first->title();
287
323
                if (title.length()==0) {
288
 
                        title = it->rssurl(); // rssurl must always be present.
 
324
                        title = it->first->rssurl(); // rssurl must always be present.
289
325
                        if (title.length()==0) {
290
326
                                title = "<no title>"; // shouldn't happen
291
327
                        }
292
328
                }
293
329
 
294
 
                // TODO: refactor
295
 
                char sbuf[20];
296
 
                char idxbuf[5];
297
330
                unsigned int unread_count = 0;
298
 
                if (it->items().size() > 0) {
299
 
                        unread_count = it->unread_item_count();
 
331
                if (it->first->items().size() > 0) {
 
332
                        unread_count = it->first->unread_item_count();
300
333
                }
301
334
                if (unread_count > 0)
302
335
                        ++unread_feeds;
303
336
 
304
 
                /*
305
 
                 * we only display an entry in the feedlist if:
306
 
                 *   - no tag is active, or the entry matches the currently selected tag
307
 
                 *   - no filter shall be applied, or the entry matches the currently set filter
308
 
                 */
309
 
                if ((tag == "" || it->matches_tag(tag)) && (!apply_filter || m.matches(&(*it)))) {
310
 
                        visible_feeds.push_back(feedptr_pos_pair(&(*it),i));
311
 
 
312
 
                        fmtstr_formatter fmt;
313
 
 
314
 
                        snprintf(idxbuf, sizeof(idxbuf),"%u", feedlist_number);
315
 
                        snprintf(sbuf,sizeof(sbuf),"(%u/%u)",unread_count,static_cast<unsigned int>(it->items().size()));
316
 
 
317
 
                        fmt.register_fmt('i', idxbuf);
318
 
                        fmt.register_fmt('u', sbuf);
319
 
                        fmt.register_fmt('n', unread_count > 0 ? "N" : " ");
320
 
                        fmt.register_fmt('t', title);
321
 
                        fmt.register_fmt('l', it->link());
322
 
                        fmt.register_fmt('L', it->rssurl());
323
 
                        fmt.register_fmt('d', it->description());
324
 
 
325
 
                        std::string line = "{listitem[";
326
 
                        std::ostringstream num;
327
 
                        num << i;
328
 
                        line.append(num.str());
329
 
                        line.append("] text:");
330
 
                        line.append(stfl::quote(fmt.do_format(feedlist_format, width)));
331
 
                        line.append("}");
332
 
 
333
 
                        code.append(line);
334
 
 
335
 
                        ++feeds_shown;
336
 
                }
 
337
                fmtstr_formatter fmt;
 
338
 
 
339
                fmt.register_fmt('i', utils::strprintf("%u", it->second + 1));
 
340
                fmt.register_fmt('u', utils::strprintf("(%u/%u)",unread_count,static_cast<unsigned int>(it->first->items().size())));
 
341
                fmt.register_fmt('n', unread_count > 0 ? "N" : " ");
 
342
                fmt.register_fmt('t', title);
 
343
                fmt.register_fmt('l', it->first->link());
 
344
                fmt.register_fmt('L', it->first->rssurl());
 
345
                fmt.register_fmt('d', it->first->description());
 
346
 
 
347
                std::string format = fmt.do_format(feedlist_format, width);
 
348
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::set_feedlist: format result = %s", format.c_str());
 
349
 
 
350
                listfmt.add_line(format, it->second);
337
351
        }
338
352
 
339
 
        code.append("}");
340
 
 
341
 
        f->modify("feeds","replace_inner",code);
 
353
        f->modify("feeds","replace_inner",listfmt.format_list());
342
354
 
343
355
        std::string title_format = v->get_cfg()->get_configvalue("feedlist-title-format");
344
356
 
382
394
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_previous_unread_feed: visible_feeds[%u] unread items: %u", i, visible_feeds[i].first->unread_item_count());
383
395
                if (visible_feeds[i].first->unread_item_count() > 0) {
384
396
                        GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_previous_unread_feed: hit");
385
 
                        std::ostringstream os;
386
 
                        os << i;
387
 
                        f->set("feedpos", os.str());
 
397
                        f->set("feedpos", utils::to_s(i));
388
398
                        feedpos = visible_feeds[i].second;
389
399
                        return true;
390
400
                }
393
403
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_previous_unread_feed: visible_feeds[%u] unread items: %u", i, visible_feeds[i].first->unread_item_count());
394
404
                if (visible_feeds[i].first->unread_item_count() > 0) {
395
405
                        GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_previous_unread_feed: hit");
396
 
                        std::ostringstream os;
397
 
                        os << i;
398
 
                        f->set("feedpos", os.str());
 
406
                        f->set("feedpos", utils::to_s(i));
 
407
                        feedpos = visible_feeds[i].second;
399
408
                        return true;
400
409
                }
401
410
        }
402
411
        return false;
403
412
}
404
413
 
 
414
void feedlist_formaction::goto_feed(const std::string& str) {
 
415
        unsigned int curpos;
 
416
        std::istringstream is(f->get("feedpos"));
 
417
        is >> curpos;
 
418
        GetLogger().log(LOG_DEBUG, "feedlist_formaction::goto_feed: curpos = %u str = `%s'", curpos, str.c_str());
 
419
        for (unsigned int i=curpos+1;i<visible_feeds.size();++i) {
 
420
                if (strcasestr(visible_feeds[i].first->title().c_str(), str.c_str()) != NULL) {
 
421
                        f->set("feedpos", utils::to_s(i));
 
422
                        return;
 
423
                }
 
424
        }
 
425
        for (unsigned int i=0;i<=curpos;++i) {
 
426
                if (strcasestr(visible_feeds[i].first->title().c_str(), str.c_str()) != NULL) {
 
427
                        f->set("feedpos", utils::to_s(i));
 
428
                        return;
 
429
                }
 
430
        }
 
431
}
 
432
 
405
433
bool feedlist_formaction::jump_to_next_unread_feed(unsigned int& feedpos) {
406
434
        unsigned int curpos;
407
435
        std::istringstream is(f->get("feedpos"));
412
440
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_next_unread_feed: visible_feeds[%u] unread items: %u", i, visible_feeds[i].first->unread_item_count());
413
441
                if (visible_feeds[i].first->unread_item_count() > 0) {
414
442
                        GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_next_unread_feed: hit");
415
 
                        std::ostringstream os;
416
 
                        os << i;
417
 
                        f->set("feedpos", os.str());
 
443
                        f->set("feedpos", utils::to_s(i));
418
444
                        feedpos = visible_feeds[i].second;
419
445
                        return true;
420
446
                }
423
449
                GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_next_unread_feed: visible_feeds[%u] unread items: %u", i, visible_feeds[i].first->unread_item_count());
424
450
                if (visible_feeds[i].first->unread_item_count() > 0) {
425
451
                        GetLogger().log(LOG_DEBUG, "feedlist_formaction::jump_to_next_unread_feed: hit");
426
 
                        std::ostringstream os;
427
 
                        os << i;
428
 
                        f->set("feedpos", os.str());
 
452
                        f->set("feedpos", utils::to_s(i));
 
453
                        feedpos = visible_feeds[i].second;
429
454
                        return true;
430
455
                }
431
456
        }
462
487
                        if (i == -1) {
463
488
                                v->show_error(_("Position not visible!"));
464
489
                        } else {
465
 
                                std::ostringstream idxstr;
466
 
                                idxstr << i;
467
 
                                f->set("feedpos", idxstr.str());
 
490
                                f->set("feedpos", utils::to_s(i));
468
491
                        }
469
492
                } else {
470
493
                        v->show_error(_("Invalid position!"));
479
502
                                        do_redraw = true;
480
503
                                        zero_feedpos = true;
481
504
                                }
 
505
                        } else if (tokens[0] == "goto") {
 
506
                                if (tokens.size() >= 2 && tokens[1] != "") {
 
507
                                        goto_feed(tokens[1]);
 
508
                                }
482
509
                        } else {
483
510
                                formaction::handle_cmdline(cmd);
484
511
                        }
498
525
                                                v->show_error(_("Error: couldn't parse filter command!"));
499
526
                                                m.parse(FILTER_UNREAD_FEEDS);
500
527
                                        } else {
501
 
                                                f->set("feedpos", "0");
 
528
                                                save_filterpos();
502
529
                                                apply_filter = true;
503
530
                                                do_redraw = true;
504
531
                                        }
512
539
                                        searchhistory.add_line(searchphrase);
513
540
                                        std::vector<rss_item> items;
514
541
                                        try {
515
 
                                                items = v->get_ctrl()->search_for_items(searchphrase, "");
 
542
                                                std::string utf8searchphrase = utils::convert_text(searchphrase, "utf-8", nl_langinfo(CODESET));
 
543
                                                items = v->get_ctrl()->search_for_items(utf8searchphrase, "");
516
544
                                        } catch (const dbexception& e) {
517
 
                                                char buf[1024];
518
 
                                                snprintf(buf, sizeof(buf), _("Error while searching for `%s': %s"), searchphrase.c_str(), e.what());
519
 
                                                v->show_error(buf);
 
545
                                                v->show_error(utils::strprintf(_("Error while searching for `%s': %s"), searchphrase.c_str(), e.what()));
520
546
                                                return;
521
547
                                        }
522
548
                                        if (items.size() > 0) {
533
559
        }
534
560
}
535
561
 
 
562
void feedlist_formaction::mark_pos_if_visible(unsigned int pos) {
 
563
        scope_measure m1("feedlist_formaction::mark_pos_if_visible");
 
564
        unsigned int vpos = 0;
 
565
        v->get_ctrl()->update_visible_feeds();
 
566
        for (std::vector<feedptr_pos_pair>::iterator it=visible_feeds.begin();it!=visible_feeds.end();++it, ++vpos) {
 
567
                if (it->second == pos) {
 
568
                        GetLogger().log(LOG_DEBUG, "feedlist_formaction::mark_pos_if_visible: match, setting position to %u", vpos);
 
569
                        f->set("feedpos", utils::to_s(vpos));
 
570
                        break;
 
571
                }
 
572
        }
 
573
}
 
574
 
 
575
void feedlist_formaction::save_filterpos() {
 
576
        std::istringstream is(f->get("feedpos"));
 
577
        unsigned int i;
 
578
        is >> i;
 
579
        if (i<visible_feeds.size()) {
 
580
                filterpos = visible_feeds[i].second;
 
581
                set_filterpos = true;
 
582
        }
 
583
}
 
584
 
536
585
}