Merge with git+ssh://pasky.or.cz/srv/git/elinks.git
[elinks.git] / src / globhist / dialogs.c
blobb8dc26a5287a8b9460ec2fef6bc9434909a4d485
1 /* Global history dialogs */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/dialog.h"
12 #include "bookmarks/dialogs.h"
13 #include "dialogs/edit.h"
14 #include "globhist/dialogs.h"
15 #include "globhist/globhist.h"
16 #include "intl/gettext/libintl.h"
17 #include "main/object.h"
18 #include "protocol/uri.h"
19 #include "terminal/terminal.h"
20 #include "util/memory.h"
21 #include "util/string.h"
24 /* Implementation of the listbox operations */
26 static void lock_globhist_item(struct listbox_item *item)
28 object_lock((struct global_history_item *) item->udata);
31 static void unlock_globhist_item(struct listbox_item *item)
33 object_unlock((struct global_history_item *) item->udata);
36 static int is_globhist_item_used(struct listbox_item *item)
38 return is_object_used((struct global_history_item *) item->udata);
41 static unsigned char *
42 get_globhist_item_text(struct listbox_item *box_item, struct terminal *term)
44 struct global_history_item *item = box_item->udata;
45 struct string info;
47 if (get_opt_int("document.history.global.display_type")
48 && *item->title)
49 return stracpy(item->title);
51 if (!init_string(&info)) return NULL;
52 add_string_uri_to_string(&info, item->url, URI_PUBLIC);
53 return info.source;
56 static unsigned char *
57 get_globhist_item_info(struct listbox_item *box_item, struct terminal *term)
59 struct global_history_item *item = box_item->udata;
60 struct string info;
62 if (box_item->type == BI_FOLDER) return NULL;
63 if (!init_string(&info)) return NULL;
65 add_format_to_string(&info, "%s: %s", _("Title", term), item->title);
66 add_format_to_string(&info, "\n%s: %s", _("URL", term), item->url);
67 add_format_to_string(&info, "\n%s: %s", _("Last visit time", term),
68 ctime(&item->last_visit));
70 return info.source;
73 static struct listbox_item *
74 get_globhist_item_root(struct listbox_item *box_item)
76 return NULL;
79 static struct uri *
80 get_globhist_item_uri(struct listbox_item *item)
82 struct global_history_item *historyitem = item->udata;
84 return get_uri(historyitem->url, 0);
87 static int
88 can_delete_globhist_item(struct listbox_item *item)
90 return 1;
93 static void
94 delete_globhist_item(struct listbox_item *item, int last)
96 struct global_history_item *historyitem = item->udata;
98 assert(!is_object_used(historyitem));
100 delete_global_history_item(historyitem);
103 static struct listbox_ops_messages globhist_messages = {
104 /* cant_delete_item */
105 N_("Sorry, but history entry \"%s\" cannot be deleted."),
106 /* cant_delete_used_item */
107 N_("Sorry, but history entry \"%s\" is being used by something else."),
108 /* cant_delete_folder */
109 NULL,
110 /* cant_delete_used_folder */
111 NULL,
112 /* delete_marked_items_title */
113 N_("Delete marked history entries"),
114 /* delete_marked_items */
115 N_("Delete marked history entries?"),
116 /* delete_folder_title */
117 NULL,
118 /* delete_folder */
119 NULL,
120 /* delete_item_title */
121 N_("Delete history entry"),
122 /* delete_item; xgettext:c-format */
123 N_("Delete this history entry?"),
124 /* clear_all_items_title */
125 N_("Clear all history entries"),
126 /* clear_all_items_title */
127 N_("Do you really want to remove all history entries?"),
130 static const struct listbox_ops gh_listbox_ops = {
131 lock_globhist_item,
132 unlock_globhist_item,
133 is_globhist_item_used,
134 get_globhist_item_text,
135 get_globhist_item_info,
136 get_globhist_item_uri,
137 get_globhist_item_root,
138 NULL,
139 can_delete_globhist_item,
140 delete_globhist_item,
141 NULL,
142 &globhist_messages,
145 /* Searching: */
147 static void
148 history_search_do(void *data)
150 struct dialog *dlg = data;
151 struct listbox_item *item = globhist_browser.root.child.next;
152 struct listbox_data *box;
154 if (!globhist_simple_search(dlg->widgets[1].data, dlg->widgets[0].data)) return;
155 if (list_empty(globhist_browser.root.child)) return;
157 /* Shouldn't we rather do this only for the specific listbox_data box
158 * in dlg->widget->data so only the current dialog is updated? --jonas */
159 foreach (box, globhist_browser.boxes) {
160 box->top = item;
161 box->sel = box->top;
165 static void
166 launch_search_dialog(struct terminal *term, struct dialog_data *parent,
167 struct session *ses)
169 do_edit_dialog(term, 1, N_("Search history"), gh_last_searched_title,
170 gh_last_searched_url, ses, parent, history_search_do,
171 NULL, NULL, EDIT_DLG_SEARCH);
174 static widget_handler_status_T
175 push_search_button(struct dialog_data *dlg_data, struct widget_data *widget_data)
177 launch_search_dialog(dlg_data->win->term, dlg_data,
178 (struct session *) dlg_data->dlg->udata);
179 return EVENT_PROCESSED;
182 /* Toggling: */
184 static widget_handler_status_T
185 push_toggle_display_button(struct dialog_data *dlg_data, struct widget_data *widget_data)
187 int *display_type;
189 display_type = &get_opt_int("document.history.global.display_type");
190 *display_type = !*display_type;
192 update_hierbox_browser(&globhist_browser);
194 return EVENT_PROCESSED;
197 /* Bookmarking: */
199 #ifdef CONFIG_BOOKMARKS
200 static widget_handler_status_T
201 push_bookmark_button(struct dialog_data *dlg_data,
202 struct widget_data *some_useless_info_button)
204 struct listbox_data *box = get_dlg_listbox_data(dlg_data);
205 struct terminal *term = dlg_data->win->term;
206 struct global_history_item *historyitem;
208 if (!box->sel) return EVENT_PROCESSED;
210 historyitem = box->sel->udata;
211 if (!historyitem) return EVENT_PROCESSED;
213 launch_bm_add_dialog(term, NULL, NULL,
214 historyitem->title, historyitem->url);
216 return EVENT_PROCESSED;
218 #endif
220 /* The global history manager: */
222 static const struct hierbox_browser_button globhist_buttons[] = {
223 /* [gettext_accelerator_context(.globhist_buttons)] */
224 { N_("~Goto"), push_hierbox_goto_button, 1 },
225 { N_("~Info"), push_hierbox_info_button, 1 },
226 #ifdef CONFIG_BOOKMARKS
227 { N_("~Bookmark"), push_bookmark_button, 0 },
228 #endif
229 { N_("~Delete"), push_hierbox_delete_button, 0 },
230 { N_("~Search"), push_search_button, 1 },
231 { N_("~Toggle display"), push_toggle_display_button, 1 },
232 { N_("C~lear"), push_hierbox_clear_button, 0 },
233 #if 0
234 /* TODO: Would this be useful? --jonas */
235 { N_("Save"), push_save_button, 0 },
236 #endif
239 struct_hierbox_browser(
240 globhist_browser,
241 N_("Global history manager"),
242 globhist_buttons,
243 &gh_listbox_ops
246 void
247 history_manager(struct session *ses)
249 mem_free_set(&gh_last_searched_title, NULL);
250 mem_free_set(&gh_last_searched_url, NULL);
251 hierbox_browser(&globhist_browser, ses);