1 /* Cache-related dialogs */
4 #define _GNU_SOURCE /* XXX: we _WANT_ strcasestr() ! */
15 #include "bfu/dialog.h"
16 #include "cache/cache.h"
17 #include "cache/dialogs.h"
18 #include "dialogs/edit.h"
19 #include "intl/gettext/libintl.h"
20 #include "main/object.h"
21 #include "protocol/uri.h"
22 #include "session/session.h"
23 #include "terminal/draw.h"
24 #include "terminal/terminal.h"
25 #include "util/conv.h"
26 #include "util/memory.h"
27 #include "util/string.h"
31 lock_cache_entry(struct listbox_item
*item
)
33 object_lock((struct cache_entry
*) item
->udata
);
37 unlock_cache_entry(struct listbox_item
*item
)
39 object_unlock((struct cache_entry
*) item
->udata
);
43 is_cache_entry_used(struct listbox_item
*item
)
45 return is_object_used((struct cache_entry
*) item
->udata
);
48 static unsigned char *
49 get_cache_entry_text(struct listbox_item
*item
, struct terminal
*term
)
51 struct cache_entry
*cached
= item
->udata
;
53 return get_uri_string(cached
->uri
, URI_PUBLIC
);
56 static unsigned char *
57 get_cache_entry_info(struct listbox_item
*item
, struct terminal
*term
)
59 struct cache_entry
*cached
= item
->udata
;
62 if (item
->type
== BI_FOLDER
) return NULL
;
63 if (!init_string(&msg
)) return NULL
;
65 add_to_string(&msg
, _("URL", term
));
66 add_to_string(&msg
, ": ");
67 add_uri_to_string(&msg
, cached
->uri
, URI_PUBLIC
);
69 /* No need to use compare_uri() here we only want to check whether they
70 * point to the same URI. */
71 if (cached
->proxy_uri
!= cached
->uri
) {
72 add_format_to_string(&msg
, "\n%s: ", _("Proxy URL", term
));
73 add_uri_to_string(&msg
, cached
->proxy_uri
, URI_PUBLIC
);
76 if (cached
->redirect
) {
77 add_format_to_string(&msg
, "\n%s: ", _("Redirect", term
));
78 add_uri_to_string(&msg
, cached
->redirect
, URI_PUBLIC
);
80 if (cached
->redirect_get
) {
81 add_to_string(&msg
, " (GET)");
85 add_format_to_string(&msg
, "\n%s: %" OFF_PRINT_FORMAT
, _("Size", term
),
86 (off_print_T
) cached
->length
);
87 add_format_to_string(&msg
, "\n%s: %" OFF_PRINT_FORMAT
, _("Loaded size", term
),
88 (off_print_T
) cached
->data_size
);
89 if (cached
->content_type
) {
90 add_format_to_string(&msg
, "\n%s: %s", _("Content type", term
),
91 cached
->content_type
);
93 if (cached
->last_modified
) {
94 add_format_to_string(&msg
, "\n%s: %s", _("Last modified", term
),
95 cached
->last_modified
);
98 add_format_to_string(&msg
, "\n%s: %s", "ETag",
101 if (cached
->ssl_info
) {
102 add_format_to_string(&msg
, "\n%s: %s", _("SSL Cipher", term
),
105 if (cached
->encoding_info
) {
106 add_format_to_string(&msg
, "\n%s: %s", _("Encoding", term
),
107 cached
->encoding_info
);
110 if (cached
->incomplete
|| !cached
->valid
) {
111 add_char_to_string(&msg
, '\n');
112 add_to_string(&msg
, _("Flags", term
));
113 add_to_string(&msg
, ": ");
114 if (cached
->incomplete
) {
115 add_to_string(&msg
, _("incomplete", term
));
116 add_char_to_string(&msg
, ' ');
118 if (!cached
->valid
) add_to_string(&msg
, _("invalid", term
));
122 if (cached
->expire
) {
123 time_t expires
= timeval_to_seconds(&cached
->max_age
);
125 add_format_to_string(&msg
, "\n%s: ", _("Expires", term
));
126 add_date_to_string(&msg
, get_opt_str("ui.date_format", NULL
), &expires
);
130 add_format_to_string(&msg
, "\n%s: ", _("Cache mode", term
));
131 switch (cached
->cache_mode
) {
132 case CACHE_MODE_NEVER
:
133 add_to_string(&msg
, _("never use cache entry", term
));
135 case CACHE_MODE_ALWAYS
:
136 add_to_string(&msg
, _("always use cache entry", term
));
138 case CACHE_MODE_INCREMENT
:
139 case CACHE_MODE_NORMAL
:
140 case CACHE_MODE_CHECK_IF_MODIFIED
:
141 case CACHE_MODE_FORCE_RELOAD
:
142 /* Cache entries only use two values of enum cache_mode. */
143 INTERNAL("cached->cache_mode = %d", cached
->cache_mode
);
148 add_format_to_string(&msg
, "\n%s: %d", "Refcount", get_object_refcount(cached
));
149 add_format_to_string(&msg
, "\n%s: %u", _("ID", term
), cached
->cache_id
);
151 if (cached
->head
&& *cached
->head
) {
152 add_format_to_string(&msg
, "\n%s:\n\n%s", _("Header", term
),
161 get_cache_entry_uri(struct listbox_item
*item
)
163 struct cache_entry
*cached
= item
->udata
;
165 return get_uri_reference(cached
->uri
);
168 static struct listbox_item
*
169 get_cache_entry_root(struct listbox_item
*item
)
175 can_delete_cache_entry(struct listbox_item
*item
)
181 delete_cache_entry_item(struct listbox_item
*item
, int last
)
183 struct cache_entry
*cached
= item
->udata
;
185 assert(!is_object_used(cached
));
187 delete_cache_entry(cached
);
190 static enum listbox_match
191 match_cache_entry(struct listbox_item
*item
, struct terminal
*term
,
194 struct cache_entry
*cached
= item
->udata
;
196 if (strcasestr(struri(cached
->uri
), text
)
197 || (cached
->head
&& strcasestr(cached
->head
, text
)))
198 return LISTBOX_MATCH_OK
;
200 return LISTBOX_MATCH_NO
;
203 static struct listbox_ops_messages cache_messages
= {
204 /* cant_delete_item */
205 N_("Sorry, but cache entry \"%s\" cannot be deleted."),
206 /* cant_delete_used_item */
207 N_("Sorry, but cache entry \"%s\" is being used by something else."),
208 /* cant_delete_folder */
210 /* cant_delete_used_folder */
212 /* delete_marked_items_title */
213 N_("Delete marked cache entries"),
214 /* delete_marked_items */
215 N_("Delete marked cache entries?"),
216 /* delete_folder_title */
220 /* delete_item_title */
221 N_("Delete cache entry"),
222 /* delete_item; xgettext:c-format */
223 N_("Delete this cache entry?"),
224 /* clear_all_items_title */
226 /* clear_all_items_title */
230 static const struct listbox_ops cache_entry_listbox_ops
= {
234 get_cache_entry_text
,
235 get_cache_entry_info
,
237 get_cache_entry_root
,
239 can_delete_cache_entry
,
240 delete_cache_entry_item
,
245 static const struct hierbox_browser_button cache_buttons
[] = {
246 /* [gettext_accelerator_context(.cache_buttons)] */
247 { N_("~Info"), push_hierbox_info_button
, 1 },
248 { N_("~Goto"), push_hierbox_goto_button
, 1 },
249 { N_("~Delete"), push_hierbox_delete_button
, 1 },
250 { N_("~Search"), push_hierbox_search_button
, 1 },
253 struct_hierbox_browser(
257 &cache_entry_listbox_ops
261 cache_manager(struct session
*ses
)
263 hierbox_browser(&cache_browser
, ses
);