big dialogs: dlg_format_text: no need to pass the term.
[elinks.git] / src / globhist / dialogs.c
blob3023b4bcfba52fb33608779202fbd221ef2ead59
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
27 lock_globhist_item(struct listbox_item *item)
29 object_lock((struct global_history_item *) item->udata);
32 static void
33 unlock_globhist_item(struct listbox_item *item)
35 object_unlock((struct global_history_item *) item->udata);
38 static int
39 is_globhist_item_used(struct listbox_item *item)
41 return is_object_used((struct global_history_item *) item->udata);
44 static unsigned char *
45 get_globhist_item_text(struct listbox_item *box_item, struct terminal *term)
47 struct global_history_item *item = box_item->udata;
48 struct string info;
50 if (get_opt_int("document.history.global.display_type", NULL)
51 && *item->title)
52 return stracpy(item->title);
54 if (!init_string(&info)) return NULL;
55 add_string_uri_to_string(&info, item->url, URI_PUBLIC);
56 return info.source;
59 static unsigned char *
60 get_globhist_item_info(struct listbox_item *box_item, struct terminal *term)
62 struct global_history_item *item = box_item->udata;
63 struct string info;
65 if (box_item->type == BI_FOLDER) return NULL;
66 if (!init_string(&info)) return NULL;
68 add_format_to_string(&info, "%s: %s", _("Title", term), item->title);
69 add_format_to_string(&info, "\n%s: %s", _("URL", term), item->url);
70 add_format_to_string(&info, "\n%s: %s", _("Last visit time", term),
71 ctime(&item->last_visit));
73 return info.source;
76 static struct listbox_item *
77 get_globhist_item_root(struct listbox_item *box_item)
79 return NULL;
82 static struct uri *
83 get_globhist_item_uri(struct listbox_item *item)
85 struct global_history_item *historyitem = item->udata;
87 return get_uri(historyitem->url, 0);
90 static int
91 can_delete_globhist_item(struct listbox_item *item)
93 return 1;
96 static void
97 delete_globhist_item(struct listbox_item *item, int last)
99 struct global_history_item *historyitem = item->udata;
101 assert(!is_object_used(historyitem));
103 delete_global_history_item(historyitem);
106 static struct listbox_ops_messages globhist_messages = {
107 /* cant_delete_item */
108 N_("Sorry, but history entry \"%s\" cannot be deleted."),
109 /* cant_delete_used_item */
110 N_("Sorry, but history entry \"%s\" is being used by something else."),
111 /* cant_delete_folder */
112 NULL,
113 /* cant_delete_used_folder */
114 NULL,
115 /* delete_marked_items_title */
116 N_("Delete marked history entries"),
117 /* delete_marked_items */
118 N_("Delete marked history entries?"),
119 /* delete_folder_title */
120 NULL,
121 /* delete_folder */
122 NULL,
123 /* delete_item_title */
124 N_("Delete history entry"),
125 /* delete_item; xgettext:c-format */
126 N_("Delete this history entry?"),
127 /* clear_all_items_title */
128 N_("Clear all history entries"),
129 /* clear_all_items_title */
130 N_("Do you really want to remove all history entries?"),
133 static const struct listbox_ops gh_listbox_ops = {
134 lock_globhist_item,
135 unlock_globhist_item,
136 is_globhist_item_used,
137 get_globhist_item_text,
138 get_globhist_item_info,
139 get_globhist_item_uri,
140 get_globhist_item_root,
141 NULL,
142 can_delete_globhist_item,
143 delete_globhist_item,
144 NULL,
145 &globhist_messages,
148 /* Searching: */
150 static void
151 history_search_do(void *data)
153 struct dialog *dlg = data;
154 struct listbox_item *item = globhist_browser.root.child.next;
155 struct listbox_data *box;
157 if (!globhist_simple_search(dlg->widgets[1].data, dlg->widgets[0].data)) return;
158 if (list_empty(globhist_browser.root.child)) return;
160 /* Shouldn't we rather do this only for the specific listbox_data box
161 * in dlg->widget->data so only the current dialog is updated? --jonas */
162 foreach (box, globhist_browser.boxes) {
163 box->top = item;
164 box->sel = box->top;
168 static void
169 launch_search_dialog(struct terminal *term, struct dialog_data *parent,
170 struct session *ses)
172 do_edit_dialog(term, 1, N_("Search history"), gh_last_searched_title,
173 gh_last_searched_url, ses, parent, history_search_do,
174 NULL, NULL, EDIT_DLG_SEARCH);
177 static widget_handler_status_T
178 push_search_button(struct dialog_data *dlg_data, struct widget_data *widget_data)
180 launch_search_dialog(dlg_data->win->term, dlg_data,
181 (struct session *) dlg_data->dlg->udata);
182 return EVENT_PROCESSED;
185 /* Toggling: */
187 static widget_handler_status_T
188 push_toggle_display_button(struct dialog_data *dlg_data, struct widget_data *widget_data)
190 int *display_type;
192 display_type = &get_opt_int("document.history.global.display_type",
193 NULL);
194 *display_type = !*display_type;
196 update_hierbox_browser(&globhist_browser);
198 return EVENT_PROCESSED;
201 /* Bookmarking: */
203 #ifdef CONFIG_BOOKMARKS
204 static widget_handler_status_T
205 push_bookmark_button(struct dialog_data *dlg_data,
206 struct widget_data *some_useless_info_button)
208 struct listbox_data *box = get_dlg_listbox_data(dlg_data);
209 struct terminal *term = dlg_data->win->term;
210 struct global_history_item *historyitem;
212 if (!box->sel) return EVENT_PROCESSED;
214 historyitem = box->sel->udata;
215 if (!historyitem) return EVENT_PROCESSED;
217 launch_bm_add_dialog(term, NULL, NULL,
218 historyitem->title, historyitem->url);
220 return EVENT_PROCESSED;
222 #endif
224 /* The global history manager: */
226 static const struct hierbox_browser_button globhist_buttons[] = {
227 /* [gettext_accelerator_context(.globhist_buttons)] */
228 { N_("~Goto"), push_hierbox_goto_button, 1 },
229 { N_("~Info"), push_hierbox_info_button, 1 },
230 #ifdef CONFIG_BOOKMARKS
231 { N_("~Bookmark"), push_bookmark_button, 0 },
232 #endif
233 { N_("~Delete"), push_hierbox_delete_button, 0 },
234 { N_("~Search"), push_search_button, 1 },
235 { N_("~Toggle display"), push_toggle_display_button, 1 },
236 { N_("C~lear"), push_hierbox_clear_button, 0 },
237 #if 0
238 /* TODO: Would this be useful? --jonas */
239 { N_("Save"), push_save_button, 0 },
240 #endif
243 struct_hierbox_browser(
244 globhist_browser,
245 N_("Global history manager"),
246 globhist_buttons,
247 &gh_listbox_ops
250 void
251 history_manager(struct session *ses)
253 mem_free_set(&gh_last_searched_title, NULL);
254 mem_free_set(&gh_last_searched_url, NULL);
255 hierbox_browser(&globhist_browser, ses);