1 /* Cookie-related dialogs */
13 #include "bfu/dialog.h"
14 #include "cookies/cookies.h"
15 #include "cookies/dialogs.h"
16 #include "dialogs/edit.h"
17 #include "intl/gettext/libintl.h"
18 #include "main/object.h"
19 #include "session/session.h"
20 #include "terminal/draw.h"
21 #include "terminal/terminal.h"
22 #include "util/conv.h"
23 #include "util/lists.h"
24 #include "util/memory.h"
25 #include "util/string.h"
28 INIT_LIST_HEAD(cookie_queries
);
31 add_cookie_info_to_string(struct string
*string
, struct cookie
*cookie
,
32 struct terminal
*term
)
34 add_format_to_string(string
, "\n%s: %s", _("Name", term
), cookie
->name
);
35 add_format_to_string(string
, "\n%s: %s", _("Value", term
), cookie
->value
);
36 add_format_to_string(string
, "\n%s: %s", _("Domain", term
), cookie
->domain
);
37 add_format_to_string(string
, "\n%s: %s", _("Path", term
), cookie
->path
);
39 if (!cookie
->expires
) {
40 add_format_to_string(string
, "\n%s: ", _("Expires", term
));
41 add_to_string(string
, _("at quit time", term
));
44 add_format_to_string(string
, "\n%s: ", _("Expires", term
));
45 add_date_to_string(string
, get_opt_str("ui.date_format"), &cookie
->expires
);
49 add_format_to_string(string
, "\n%s: %s", _("Secure", term
),
50 _(cookie
->secure
? N_("yes") : N_("no"), term
));
53 /* TODO: Store cookie in data arg. --jonas*/
55 accept_cookie_dialog(struct session
*ses
, void *data
)
57 struct cookie
*cookie
= cookie_queries
.next
;
62 if (list_empty(cookie_queries
)
63 || !init_string(&string
))
66 del_from_list(cookie
);
68 add_format_to_string(&string
,
69 _("Do you want to accept a cookie from %s?", ses
->tab
->term
),
70 cookie
->server
->host
);
72 add_to_string(&string
, "\n\n");
74 add_cookie_info_to_string(&string
, cookie
, ses
->tab
->term
);
76 msg_box(ses
->tab
->term
, NULL
, MSGBOX_FREE_TEXT
,
77 N_("Accept cookie?"), ALIGN_LEFT
,
80 N_("~Accept"), accept_cookie
, B_ENTER
,
81 N_("~Reject"), done_cookie
, B_ESC
);
86 lock_cookie(struct listbox_item
*item
)
88 if (item
->type
== BI_LEAF
)
89 object_lock((struct cookie
*) item
->udata
);
91 object_lock((struct cookie_server
*) item
->udata
);
95 unlock_cookie(struct listbox_item
*item
)
97 if (item
->type
== BI_LEAF
)
98 object_unlock((struct cookie
*) item
->udata
);
100 object_unlock((struct cookie_server
*) item
->udata
);
104 is_cookie_used(struct listbox_item
*item
)
106 if (item
->type
== BI_FOLDER
) {
107 struct listbox_item
*root
= item
;
109 foreach (item
, root
->child
)
110 if (is_object_used((struct cookie
*) item
->udata
))
116 return is_object_used((struct cookie
*) item
->udata
);
119 static unsigned char *
120 get_cookie_text(struct listbox_item
*item
, struct terminal
*term
)
122 /* Are we dealing with a folder? */
123 if (item
->type
== BI_FOLDER
) {
124 struct cookie_server
*server
= item
->udata
;
126 return stracpy(server
->host
);
129 struct cookie
*cookie
= item
->udata
;
131 return stracpy(cookie
->name
);
135 static unsigned char *
136 get_cookie_info(struct listbox_item
*item
, struct terminal
*term
)
138 struct cookie
*cookie
= item
->udata
;
139 struct cookie_server
*server
;
140 struct string string
;
142 if (item
->type
== BI_FOLDER
) return NULL
;
144 if (!init_string(&string
)) return NULL
;
146 server
= cookie
->server
;
148 add_format_to_string(&string
, "%s: %s", _("Server", term
), server
->host
);
150 add_cookie_info_to_string(&string
, cookie
, term
);
152 return string
.source
;
155 static struct listbox_item
*
156 get_cookie_root(struct listbox_item
*item
)
158 /* Are we dealing with a folder? */
159 if (item
->type
== BI_FOLDER
) {
162 struct cookie
*cookie
= item
->udata
;
164 return cookie
->server
->box_item
;
169 can_delete_cookie(struct listbox_item
*item
)
175 delete_cookie_item(struct listbox_item
*item
, int last
)
177 struct cookie
*cookie
= item
->udata
;
179 if (item
->type
== BI_FOLDER
) {
180 struct listbox_item
*next
, *root
= item
;
182 /* Releasing refcounts on the cookie_server will automagically
184 foreachsafe (item
, next
, root
->child
)
185 delete_cookie_item(item
, 0);
187 assert(!is_object_used(cookie
));
189 delete_cookie(cookie
);
194 static struct listbox_ops_messages cookies_messages
= {
195 /* cant_delete_item */
196 N_("Sorry, but cookie \"%s\" cannot be deleted."),
197 /* cant_delete_used_item */
198 N_("Sorry, but cookie \"%s\" is being used by something else."),
199 /* cant_delete_folder */
200 N_("Sorry, but cookie domain \"%s\" cannot be deleted."),
201 /* cant_delete_used_folder */
202 N_("Sorry, but cookie domain \"%s\" is being used by something else."),
203 /* delete_marked_items_title */
204 N_("Delete marked cookies"),
205 /* delete_marked_items */
206 N_("Delete marked cookies?"),
207 /* delete_folder_title */
208 N_("Delete domain's cookies"),
210 N_("Delete all cookies from domain \"%s\"?"),
211 /* delete_item_title */
214 N_("Delete this cookie?"),
215 /* clear_all_items_title */
216 N_("Clear all cookies"),
217 /* clear_all_items_title */
218 N_("Do you really want to remove all cookies?"),
221 static struct listbox_ops cookies_listbox_ops
= {
236 static widget_handler_status_T
237 set_cookie_name(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
239 struct cookie
*cookie
= dlg_data
->dlg
->udata
;
240 unsigned char *value
= widget_data
->cdata
;
242 if (!value
|| !cookie
) return EVENT_NOT_PROCESSED
;
243 mem_free_set(&cookie
->name
, stracpy(value
));
245 return EVENT_PROCESSED
;
248 static widget_handler_status_T
249 set_cookie_value(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
251 struct cookie
*cookie
= dlg_data
->dlg
->udata
;
252 unsigned char *value
= widget_data
->cdata
;
254 if (!value
|| !cookie
) return EVENT_NOT_PROCESSED
;
255 mem_free_set(&cookie
->value
, stracpy(value
));
257 return EVENT_PROCESSED
;
260 static widget_handler_status_T
261 set_cookie_domain(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
263 struct cookie
*cookie
= dlg_data
->dlg
->udata
;
264 unsigned char *value
= widget_data
->cdata
;
266 if (!value
|| !cookie
) return EVENT_NOT_PROCESSED
;
267 mem_free_set(&cookie
->domain
, stracpy(value
));
269 return EVENT_PROCESSED
;
272 static widget_handler_status_T
273 set_cookie_expires(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
275 struct cookie
*cookie
= dlg_data
->dlg
->udata
;
276 unsigned char *value
= widget_data
->cdata
;
280 if (!value
|| !cookie
) return EVENT_NOT_PROCESSED
;
283 number
= strtol(value
, (char **) &end
, 10);
284 if (errno
|| *end
|| number
< 0) return EVENT_NOT_PROCESSED
;
286 cookie
->expires
= (time_t) number
;
288 return EVENT_PROCESSED
;
291 static widget_handler_status_T
292 set_cookie_secure(struct dialog_data
*dlg_data
, struct widget_data
*widget_data
)
294 struct cookie
*cookie
= dlg_data
->dlg
->udata
;
295 unsigned char *value
= widget_data
->cdata
;
299 if (!value
|| !cookie
) return EVENT_NOT_PROCESSED
;
302 number
= strtol(value
, (char **) &end
, 10);
303 if (errno
|| *end
) return EVENT_NOT_PROCESSED
;
305 cookie
->secure
= (number
!= 0);
307 return EVENT_PROCESSED
;
311 build_edit_dialog(struct terminal
*term
, struct cookie
*cookie
)
313 #define EDIT_WIDGETS_COUNT 8
315 unsigned char *name
, *value
, *domain
, *expires
, *secure
;
316 unsigned char *dlg_server
;
319 dlg
= calloc_dialog(EDIT_WIDGETS_COUNT
, MAX_STR_LEN
* 5);
322 dlg
->title
= _("Edit", term
);
323 dlg
->layouter
= generic_dialog_layouter
;
327 name
= get_dialog_offset(dlg
, EDIT_WIDGETS_COUNT
);
328 value
= name
+ MAX_STR_LEN
;
329 domain
= value
+ MAX_STR_LEN
;
330 expires
= domain
+ MAX_STR_LEN
;
331 secure
= expires
+ MAX_STR_LEN
;
333 safe_strncpy(name
, cookie
->name
, MAX_STR_LEN
);
334 safe_strncpy(value
, cookie
->value
, MAX_STR_LEN
);
335 safe_strncpy(domain
, cookie
->domain
, MAX_STR_LEN
);
336 ulongcat(expires
, &length
, cookie
->expires
, MAX_STR_LEN
, 0);
338 ulongcat(secure
, &length
, cookie
->secure
, MAX_STR_LEN
, 0);
340 dlg_server
= cookie
->server
->host
;
341 dlg_server
= straconcat(_("Server", term
), ": ", dlg_server
, "\n", NULL
);
348 add_dlg_text(dlg
, dlg_server
, ALIGN_LEFT
, 0);
349 add_dlg_field_float(dlg
, _("Name", term
), 0, 0, set_cookie_name
, MAX_STR_LEN
, name
, NULL
);
350 add_dlg_field_float(dlg
, _("Value", term
), 0, 0, set_cookie_value
, MAX_STR_LEN
, value
, NULL
);
351 add_dlg_field_float(dlg
, _("Domain", term
), 0, 0, set_cookie_domain
, MAX_STR_LEN
, domain
, NULL
);
352 add_dlg_field_float(dlg
, _("Expires", term
), 0, 0, set_cookie_expires
, MAX_STR_LEN
, expires
, NULL
);
353 add_dlg_field_float(dlg
, _("Secure", term
), 0, 0, set_cookie_secure
, MAX_STR_LEN
, secure
, NULL
);
355 add_dlg_button(dlg
, _("~OK", term
), B_ENTER
, ok_dialog
, NULL
);
356 add_dlg_button(dlg
, _("~Cancel", term
), B_ESC
, cancel_dialog
, NULL
);
358 add_dlg_end(dlg
, EDIT_WIDGETS_COUNT
);
360 do_dialog(term
, dlg
, getml(dlg
, dlg_server
, NULL
));
361 #undef EDIT_WIDGETS_COUNT
364 static widget_handler_status_T
365 push_edit_button(struct dialog_data
*dlg_data
, struct widget_data
*button
)
367 struct listbox_data
*box
= get_dlg_listbox_data(dlg_data
);
368 struct terminal
*term
= dlg_data
->win
->term
;
369 struct cookie
*cookie
;
371 if (!box
->sel
) return EVENT_PROCESSED
;
372 if (box
->sel
->type
== BI_FOLDER
) return EVENT_PROCESSED
;
373 cookie
= box
->sel
->udata
;
374 if (!cookie
) return EVENT_PROCESSED
;
375 build_edit_dialog(term
, cookie
);
376 return EVENT_PROCESSED
;
379 static widget_handler_status_T
380 push_add_button(struct dialog_data
*dlg_data
, struct widget_data
*button
)
382 struct listbox_data
*box
= get_dlg_listbox_data(dlg_data
);
383 struct terminal
*term
= dlg_data
->win
->term
;
384 struct cookie
*new_cookie
;
385 struct cookie_server
*server
;
387 if (!box
->sel
|| !box
->sel
->udata
) return EVENT_PROCESSED
;
389 new_cookie
= mem_calloc(1, sizeof(*new_cookie
));
390 if (!new_cookie
) return EVENT_PROCESSED
;
392 if (box
->sel
->type
== BI_FOLDER
) {
393 assert(box
->sel
->depth
== 0);
394 server
= box
->sel
->udata
;
396 struct cookie
*cookie
= box
->sel
->udata
;
398 server
= cookie
->server
;
402 new_cookie
->server
= server
;
404 new_cookie
->name
= stracpy("");
405 new_cookie
->value
= stracpy("");
406 new_cookie
->domain
= stracpy("");
407 accept_cookie(new_cookie
);
408 build_edit_dialog(term
, new_cookie
);
409 return EVENT_PROCESSED
;
412 static widget_handler_status_T
413 push_save_button(struct dialog_data
*dlg_data
, struct widget_data
*button
)
416 return EVENT_PROCESSED
;
419 static struct hierbox_browser_button cookie_buttons
[] = {
420 { N_("~Info"), push_hierbox_info_button
, 1 },
421 { N_("~Add"), push_add_button
, 1 },
422 { N_("~Edit"), push_edit_button
, 1 },
423 { N_("~Delete"), push_hierbox_delete_button
, 1 },
424 { N_("C~lear"), push_hierbox_clear_button
, 1 },
425 { N_("Sa~ve"), push_save_button
, 0 },
428 struct_hierbox_browser(
430 N_("Cookie manager"),
436 cookie_manager(struct session
*ses
)
438 hierbox_browser(&cookie_browser
, ses
);