Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Pass feed's title to bookmark script. Closes #341
Browse files Browse the repository at this point in the history
  • Loading branch information
Minoru committed Jul 30, 2016
1 parent d851b10 commit c8c8b36
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 29 deletions.
4 changes: 3 additions & 1 deletion doc/example-bookmark-plugin.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/sh
# this is a simple example script that demonstrates how bookmarking plugins for newsbeuter are implemented
# (c) 2007 Andreas Krennmair
# (c) 2016 Alexander Batischev

url="$1"
title="$2"
description="$3"
feed_title="$4"

echo -e "${url}\t${title}\t${description}" >> ~/bookmarks.txt
echo -e "${url}\t${title}\t${description}\t${feed_title}" >> ~/bookmarks.txt
7 changes: 4 additions & 3 deletions doc/newsbeuter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ Bookmarking
Since version 0.7, newsbeuter contains a plugin-based bookmarking system. When a user bookmarks a link (possible
in the article list, in the article view, and in the URL view), he is asked for the URL to bookmark (already
preset with the URL of the current selection), the bookmark title (in most cases preset with the
title of the current selection) and the bookmark description. After the question for the description, an
external program, configured via the configuration command "bookmark-cmd", is executed with 3 commandline
title of the current selection), the bookmark description and (since 2.10) the
title of the feed the user is currently in. After the question for the description,
an external program, configured via the configuration command "bookmark-cmd", is executed with 4 (since 2.10) commandline
parameters. The plugin itself implements the actual bookmark saving (e.g. writing the bookmark to an
external file, or storing it to a del.icio.us account). When everything went OK, the plugin simply exits.
external file, or storing it to a del.icio.us account). If everything went OK, the plugin simply exits.
In case something goes wrong while saving the bookmark, it writes out an error message as a single line.
This error message is then presented to the user from within newsbeuter.

Expand Down
2 changes: 1 addition & 1 deletion include/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class controller {
return filters;
}

std::string bookmark(const std::string& url, const std::string& title, const std::string& description);
std::string bookmark(const std::string& url, const std::string& title, const std::string& description, const std::string& feed_title);

inline cache * get_cache() {
return rsscache;
Expand Down
2 changes: 1 addition & 1 deletion include/formaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class formaction {
virtual void set_keymap_hints();


void start_bookmark_qna(const std::string& default_title, const std::string& default_url, const std::string& default_desc);
void start_bookmark_qna(const std::string& default_title, const std::string& default_url, const std::string& default_desc, const std::string& default_feed_title);

view * v;
std::shared_ptr<stfl::form> f;
Expand Down
3 changes: 2 additions & 1 deletion include/urlview_formaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace newsbeuter {

class urlview_formaction : public formaction {
public:
urlview_formaction(view *, std::string formstr);
urlview_formaction(view *, std::shared_ptr<rss_feed>& feed, std::string formstr);
virtual ~urlview_formaction();
virtual void prepare();
virtual void init();
Expand All @@ -25,6 +25,7 @@ class urlview_formaction : public formaction {
virtual void process_operation(operation op, bool automatic = false, std::vector<std::string> * args = NULL);
std::vector<linkpair> links;
bool quit;
std::shared_ptr<rss_feed> feed;
};

}
Expand Down
2 changes: 1 addition & 1 deletion include/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class view {
void push_itemlist(std::shared_ptr<rss_feed> feed);
void push_itemview(std::shared_ptr<rss_feed> f, const std::string& guid, const std::string& searchphrase = "");
void push_help();
void push_urlview(const std::vector<linkpair>& links);
void push_urlview(const std::vector<linkpair>& links, std::shared_ptr<rss_feed>& feed);
void push_searchresult(std::shared_ptr<rss_feed> feed, const std::string& phrase = "");
void view_dialogs();

Expand Down
27 changes: 23 additions & 4 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,13 +1347,32 @@ void controller::edit_urls_file() {
reload_urls_file();
}

std::string controller::bookmark(const std::string& url, const std::string& title, const std::string& description) {
/* When passing an argument to a shell script, empty string should be
* represented as '' (two quote marks), otherwise shell won't be able to tell
* that the parameter is empty */
std::string quote_empty(const std::string& input) {
if (input.empty()) {
return "''";
} else {
return input;
}
}

std::string controller::bookmark(
const std::string& url,
const std::string& title,
const std::string& description,
const std::string& feed_title)
{
std::string bookmark_cmd = cfg.get_configvalue("bookmark-cmd");
bool is_interactive = cfg.get_configvalue_as_bool("bookmark-interactive");
if (bookmark_cmd.length() > 0) {
std::string cmdline = utils::strprintf("%s '%s' %s %s",
bookmark_cmd.c_str(), utils::replace_all(url,"'", "%27").c_str(),
stfl::quote(title).c_str(), stfl::quote(description).c_str());
std::string cmdline = utils::strprintf("%s '%s' %s %s %s",
bookmark_cmd.c_str(),
utils::replace_all(url,"'", "%27").c_str(),
quote_empty(stfl::quote(title)).c_str(),
quote_empty(stfl::quote(description)).c_str(),
quote_empty(stfl::quote(feed_title)).c_str());

LOG(LOG_DEBUG, "controller::bookmark: cmd = %s", cmdline.c_str());

Expand Down
32 changes: 24 additions & 8 deletions src/formaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ void formaction::finished_qna(operation op) {
* - signal success (or failure) to the user
*/
case OP_INT_BM_END: {
assert(qna_responses.size() == 3 && qna_prompts.size() == 0); // everything must be answered
assert(qna_responses.size() == 4 && qna_prompts.size() == 0); // everything must be answered
v->set_status(_("Saving bookmark..."));
std::string retval = v->get_ctrl()->bookmark(qna_responses[0], qna_responses[1], qna_responses[2]);
std::string retval = v->get_ctrl()->bookmark(qna_responses[0], qna_responses[1], qna_responses[2], qna_responses[3]);
if (retval.length() == 0) {
v->set_status(_("Saved bookmark."));
} else {
Expand All @@ -305,8 +305,20 @@ void formaction::finished_qna(operation op) {
}


void formaction::start_bookmark_qna(const std::string& default_title, const std::string& default_url, const std::string& default_desc) {
LOG(LOG_DEBUG, "formaction::start_bookmark_qna: starting bookmark Q&A... default_title = %s default_url = %s default_desc = %s", default_title.c_str(), default_url.c_str(), default_desc.c_str());
void formaction::start_bookmark_qna(
const std::string& default_title,
const std::string& default_url,
const std::string& default_desc,
const std::string& default_feed_title)
{
LOG(LOG_DEBUG,
"formaction::start_bookmark_qna: starting bookmark Q&A... "
"default_title = %s default_url = %s default_desc = %s "
"default_feed_title = %s",
default_title.c_str(),
default_url.c_str(),
default_desc.c_str(),
default_feed_title.c_str());
std::vector<qna_pair> prompts;

std::string new_title = "";
Expand All @@ -319,17 +331,21 @@ void formaction::start_bookmark_qna(const std::string& default_title, const std:
prompts.push_back(qna_pair(_("Title: "), default_title));
}
prompts.push_back(qna_pair(_("Description: "), default_desc));
prompts.push_back(qna_pair(_("Feed title: "), default_feed_title));

if (is_bm_autopilot) { //If bookmarking is set to autopilot don't prompt for url, title, desc
if (default_title.empty())
if (default_title.empty()) {
new_title = make_title(default_url); // try to make the title from url
else
} else {
new_title = default_title; // assignment just to make the call to bookmark() below easier
if (default_url.empty() || new_title.empty()) { //if url or title is missing, abort autopilot and ask user
}

//if url or title is missing, abort autopilot and ask user
if (default_url.empty() || new_title.empty() || default_feed_title.empty()) {
start_qna(prompts, OP_INT_BM_END);
} else {
v->set_status(_("Saving bookmark on autopilot..."));
std::string retval = v->get_ctrl()->bookmark(default_url, new_title, default_desc);
std::string retval = v->get_ctrl()->bookmark(default_url, new_title, default_desc, default_feed_title);
if (retval.length() == 0) {
v->set_status(_("Saved bookmark."));
} else {
Expand Down
9 changes: 7 additions & 2 deletions src/itemlist_formaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void itemlist_formaction::process_operation(operation op, bool automatic, std::v
std::string baseurl = visible_items[itempos].first->get_base() != "" ? visible_items[itempos].first->get_base() : visible_items[itempos].first->feedurl();
rnd.render(visible_items[itempos].first->description(), lines, links, baseurl);
if (!links.empty()) {
v->push_urlview(links);
v->push_urlview(links, feed);
} else {
v->show_error(_("URL list empty."));
}
Expand All @@ -176,9 +176,14 @@ void itemlist_formaction::process_operation(operation op, bool automatic, std::v
qna_responses.push_back(visible_items[itempos].first->link());
qna_responses.push_back(visible_items[itempos].first->title());
qna_responses.push_back(args->size() > 0 ? (*args)[0] : "");
qna_responses.push_back(feed->title());
this->finished_qna(OP_INT_BM_END);
} else {
this->start_bookmark_qna(visible_items[itempos].first->title(), visible_items[itempos].first->link(), "");
this->start_bookmark_qna(
visible_items[itempos].first->title(),
visible_items[itempos].first->link(),
"",
feed->title());
}
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/itemview_formaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ void itemview_formaction::process_operation(operation op, bool automatic, std::v
qna_responses.push_back(item->link());
qna_responses.push_back(item->title());
qna_responses.push_back(args->size() > 0 ? (*args)[0] : "");
qna_responses.push_back(feed->title());
} else {
this->start_bookmark_qna(item->title(), item->link(), "");
this->start_bookmark_qna(
item->title(), item->link(), "", feed->title());
}
break;
case OP_SEARCH: {
Expand Down Expand Up @@ -266,7 +268,7 @@ void itemview_formaction::process_operation(operation op, bool automatic, std::v
LOG(LOG_DEBUG, "view::run_itemview: showing URLs");
if (urlviewer == "") {
if (links.size() > 0) {
v->push_urlview(links);
v->push_urlview(links, feed);
} else {
v->show_error(_("URL list empty."));
}
Expand Down
6 changes: 3 additions & 3 deletions src/urlview_formaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace newsbeuter {
* in a browser or to bookmark them.
*/

urlview_formaction::urlview_formaction(view * vv, std::string formstr)
: formaction(vv, formstr), quit(false) { }
urlview_formaction::urlview_formaction(view * vv, std::shared_ptr<rss_feed>& feed, std::string formstr)
: formaction(vv, formstr), quit(false), feed(feed) { }

urlview_formaction::~urlview_formaction() {
}
Expand All @@ -41,7 +41,7 @@ void urlview_formaction::process_operation(operation op, bool /* automatic */, s
if (posstr.length() > 0) {
unsigned int idx = utils::to_u(posstr, 0);

this->start_bookmark_qna("", links[idx].first, "");
this->start_bookmark_qna("", links[idx].first, "", feed->title());

} else {
v->show_error(_("No link selected!"));
Expand Down
4 changes: 2 additions & 2 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ void view::push_help() {
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));
void view::push_urlview(const std::vector<linkpair>& links, std::shared_ptr<rss_feed>& feed) {
std::shared_ptr<urlview_formaction> urlview(new urlview_formaction(this, feed, urlview_str));
set_bindings(urlview);
apply_colors(urlview);
urlview->set_parent_formaction(get_current_formaction());
Expand Down

0 comments on commit c8c8b36

Please sign in to comment.