Implement sokoke_register_privacy_item facility

The new function allows registering callbacks which clear data,
with a name and label so they show up in the Clear Private Data
dialogue and the chosen items are saved.

The old enumeration still keeps history, trash and the flag
whether to clear when Midori quits.
This commit is contained in:
Christian Dywan 2010-10-17 22:48:32 +02:00
parent 384e2f5afd
commit d73b471ec4
7 changed files with 189 additions and 113 deletions

View file

@ -23,11 +23,20 @@
#define MAXLENGTH 1024 * 1024 #define MAXLENGTH 1024 * 1024
static gchar*
web_cache_get_cache_dir (void)
{
static gchar* cache_dir = NULL;
if (!cache_dir)
cache_dir = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "web", NULL);
return cache_dir;
}
static gchar* static gchar*
web_cache_get_cached_path (MidoriExtension* extension, web_cache_get_cached_path (MidoriExtension* extension,
const gchar* uri) const gchar* uri)
{ {
static const gchar* cache_path = NULL;
gchar* checksum; gchar* checksum;
gchar* folder; gchar* folder;
gchar* sub_path; gchar* sub_path;
@ -36,13 +45,10 @@ web_cache_get_cached_path (MidoriExtension* extension,
gchar* cached_filename; gchar* cached_filename;
gchar* cached_path; gchar* cached_path;
if (!cache_path)
cache_path = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "web", NULL);
checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
folder = g_strdup_printf ("%c%c", checksum[0], checksum[1]); folder = g_strdup_printf ("%c%c", checksum[0], checksum[1]);
sub_path = g_build_path (G_DIR_SEPARATOR_S, cache_path, folder, NULL); sub_path = g_build_path (G_DIR_SEPARATOR_S,
/* FIXME: Wrong place? */ web_cache_get_cache_dir (), folder, NULL);
katze_mkdir_with_parents (sub_path, 0700); katze_mkdir_with_parents (sub_path, 0700);
g_free (folder); g_free (folder);
@ -436,14 +442,11 @@ static void
web_cache_activate_cb (MidoriExtension* extension, web_cache_activate_cb (MidoriExtension* extension,
MidoriApp* app) MidoriApp* app)
{ {
gchar* cache_path = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "web", NULL);
KatzeArray* browsers; KatzeArray* browsers;
MidoriBrowser* browser; MidoriBrowser* browser;
SoupSession* session = webkit_get_default_session (); SoupSession* session = webkit_get_default_session ();
katze_mkdir_with_parents (cache_path, 0700); katze_mkdir_with_parents (web_cache_get_cache_dir (), 0700);
g_free (cache_path);
g_signal_connect (session, "request-queued", g_signal_connect (session, "request-queued",
G_CALLBACK (web_cache_session_request_queued_cb), extension); G_CALLBACK (web_cache_session_request_queued_cb), extension);
@ -456,6 +459,12 @@ web_cache_activate_cb (MidoriExtension* extension,
g_object_unref (browsers); g_object_unref (browsers);
} }
static void
web_cache_clear_cache_cb (void)
{
sokoke_remove_path (web_cache_get_cache_dir (), TRUE);
}
MidoriExtension* MidoriExtension*
extension_init (void) extension_init (void)
{ {
@ -469,5 +478,8 @@ extension_init (void)
g_signal_connect (extension, "activate", g_signal_connect (extension, "activate",
G_CALLBACK (web_cache_activate_cb), NULL); G_CALLBACK (web_cache_activate_cb), NULL);
sokoke_register_privacy_item ("web-cache", _("Web Cache"),
G_CALLBACK (web_cache_clear_cache_cb));
return extension; return extension;
} }

View file

@ -1516,6 +1516,44 @@ midori_setup_inactivity_reset (MidoriBrowser* browser,
} }
} }
static void
midori_clear_page_icons_cb (void)
{
gchar* cache = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "icons", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
}
static void
midori_clear_web_cookies_cb (void)
{
SoupSession* session = webkit_get_default_session ();
SoupSessionFeature* jar = soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR);
GSList* cookies = soup_cookie_jar_all_cookies (SOUP_COOKIE_JAR (jar));
for (; cookies != NULL; cookies = g_slist_next (cookies))
{
SoupCookie* cookie = cookies->data;
soup_cookie_set_max_age (cookie, 0);
soup_cookie_free (cookie);
}
g_slist_free (cookies);
/* Removing KatzeHttpCookies makes it save outstanding changes */
soup_session_remove_feature_by_type (session, KATZE_TYPE_HTTP_COOKIES);
soup_session_add_feature_by_type (session, KATZE_TYPE_HTTP_COOKIES);
}
#ifdef GDK_WINDOWING_X11
static void
midori_clear_flash_cookies_cb (void)
{
gchar* cache = g_build_filename (g_get_home_dir (), ".macromedia",
"Flash_Player", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
}
#endif
int int
main (int argc, main (int argc,
char** argv) char** argv)
@ -1736,6 +1774,15 @@ main (int argc,
} }
#endif #endif
sokoke_register_privacy_item ("page-icons", _("Website icons"),
G_CALLBACK (midori_clear_page_icons_cb));
sokoke_register_privacy_item ("web-cookies", _("Cookies"),
G_CALLBACK (midori_clear_web_cookies_cb));
#ifdef GDK_WINDOWING_X11
sokoke_register_privacy_item ("flash-cookies", _("'Flash' Cookies"),
G_CALLBACK (midori_clear_flash_cookies_cb));
#endif
/* Web Application support */ /* Web Application support */
if (webapp) if (webapp)
{ {
@ -2158,30 +2205,19 @@ main (int argc,
g_object_get (settings, "clear-private-data", &clear_prefs, NULL); g_object_get (settings, "clear-private-data", &clear_prefs, NULL);
if (clear_prefs & MIDORI_CLEAR_ON_QUIT) if (clear_prefs & MIDORI_CLEAR_ON_QUIT)
{ {
GList* data_items = sokoke_register_privacy_item (NULL, NULL, NULL);
gchar* clear_data = katze_object_get_string (settings, "clear-data");
midori_remove_config_file (clear_prefs, MIDORI_CLEAR_HISTORY, "history.db"); midori_remove_config_file (clear_prefs, MIDORI_CLEAR_HISTORY, "history.db");
midori_remove_config_file (clear_prefs, MIDORI_CLEAR_COOKIES, "cookies.txt");
if ((clear_prefs & MIDORI_CLEAR_FLASH_COOKIES) == MIDORI_CLEAR_FLASH_COOKIES)
{
gchar* cache = g_build_filename (g_get_home_dir (), ".macromedia",
"Flash_Player", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
}
if ((clear_prefs & MIDORI_CLEAR_WEBSITE_ICONS) == MIDORI_CLEAR_WEBSITE_ICONS)
{
gchar* cache = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "icons", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
}
midori_remove_config_file (clear_prefs, MIDORI_CLEAR_TRASH, "tabtrash.xbel"); midori_remove_config_file (clear_prefs, MIDORI_CLEAR_TRASH, "tabtrash.xbel");
if ((clear_prefs & MIDORI_CLEAR_WEB_CACHE) == MIDORI_CLEAR_WEB_CACHE)
for (; data_items != NULL; data_items = g_list_next (data_items))
{ {
gchar* cache = g_build_filename (g_get_user_cache_dir (), SokokePrivacyItem* privacy = data_items->data;
PACKAGE_NAME, "web", NULL); if (clear_data && strstr (clear_data, privacy->name))
sokoke_remove_path (cache, TRUE); privacy->clear ();
g_free (cache);
} }
g_free (clear_data);
} }
if (katze_object_get_int (settings, "load-on-startup") if (katze_object_get_int (settings, "load-on-startup")

View file

@ -4164,19 +4164,17 @@ midori_browser_clear_private_data_response_cb (GtkWidget* dialog,
GtkToggleButton* button; GtkToggleButton* button;
gint clear_prefs = MIDORI_CLEAR_NONE; gint clear_prefs = MIDORI_CLEAR_NONE;
gint saved_prefs = MIDORI_CLEAR_NONE; gint saved_prefs = MIDORI_CLEAR_NONE;
GList* data_items = sokoke_register_privacy_item (NULL, NULL, NULL);
GString* clear_data = g_string_new (NULL);
g_object_get (browser->settings, "clear-private-data", &saved_prefs, NULL); g_object_get (browser->settings, "clear-private-data", &saved_prefs, NULL);
button = g_object_get_data (G_OBJECT (dialog), "history"); button = g_object_get_data (G_OBJECT (dialog), "history");
if (gtk_toggle_button_get_active (button)) if (gtk_toggle_button_get_active (button))
{ {
const gchar* sqlcmd; const gchar* sqlcmd = "DELETE FROM history";
sqlite3* db; sqlite3* db = g_object_get_data (G_OBJECT (browser->history), "db");
char* errmsg = NULL; char* errmsg = NULL;
db = g_object_get_data (G_OBJECT (browser->history), "db");
sqlcmd = "DELETE FROM history";
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK) if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
{ {
g_printerr (_("Failed to clear history: %s\n"), errmsg); g_printerr (_("Failed to clear history: %s\n"), errmsg);
@ -4193,66 +4191,32 @@ midori_browser_clear_private_data_response_cb (GtkWidget* dialog,
} }
clear_prefs |= MIDORI_CLEAR_HISTORY; clear_prefs |= MIDORI_CLEAR_HISTORY;
} }
button = g_object_get_data (G_OBJECT (dialog), "cookies");
if (gtk_toggle_button_get_active (button))
{
SoupSession* session = webkit_get_default_session ();
SoupSessionFeature* jar = soup_session_get_feature (session,
SOUP_TYPE_COOKIE_JAR);
GSList* cookies = soup_cookie_jar_all_cookies (SOUP_COOKIE_JAR (jar));
SoupCookie* cookie;
gsize i = 0;
while ((cookie = g_slist_nth_data (cookies, i++)))
{
soup_cookie_jar_delete_cookie (SOUP_COOKIE_JAR (jar), cookie);
soup_cookie_free (cookie);
}
g_slist_free (cookies);
clear_prefs |= MIDORI_CLEAR_COOKIES;
}
button = g_object_get_data (G_OBJECT (dialog), "flash-cookies");
if (gtk_toggle_button_get_active (button))
{
gchar* cache = g_build_filename (g_get_home_dir (), ".macromedia",
"Flash_Player", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
clear_prefs |= MIDORI_CLEAR_FLASH_COOKIES;
}
button = g_object_get_data (G_OBJECT (dialog), "website-icons");
if (gtk_toggle_button_get_active (button))
{
gchar* cache = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "icons", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
clear_prefs |= MIDORI_CLEAR_WEBSITE_ICONS;
}
button = g_object_get_data (G_OBJECT (dialog), "trash"); button = g_object_get_data (G_OBJECT (dialog), "trash");
if (gtk_toggle_button_get_active (button)) if (gtk_toggle_button_get_active (button) && browser->trash)
{ {
if (browser->trash) katze_array_clear (browser->trash);
{ _midori_browser_update_actions (browser);
katze_array_clear (browser->trash);
_midori_browser_update_actions (browser);
}
clear_prefs |= MIDORI_CLEAR_TRASH; clear_prefs |= MIDORI_CLEAR_TRASH;
} }
button = g_object_get_data (G_OBJECT (dialog), "web-cache");
if (gtk_toggle_button_get_active (button))
{
gchar* cache = g_build_filename (g_get_user_cache_dir (),
PACKAGE_NAME, "web", NULL);
sokoke_remove_path (cache, TRUE);
g_free (cache);
clear_prefs |= MIDORI_CLEAR_WEB_CACHE;
}
if (clear_prefs != saved_prefs) if (clear_prefs != saved_prefs)
{ {
clear_prefs |= (saved_prefs & MIDORI_CLEAR_ON_QUIT); clear_prefs |= (saved_prefs & MIDORI_CLEAR_ON_QUIT);
g_object_set (browser->settings, "clear-private-data", clear_prefs, NULL); g_object_set (browser->settings, "clear-private-data", clear_prefs, NULL);
} }
for (; data_items != NULL; data_items = g_list_next (data_items))
{
SokokePrivacyItem* privacy = data_items->data;
button = g_object_get_data (G_OBJECT (dialog), privacy->name);
g_return_if_fail (button != NULL && GTK_IS_TOGGLE_BUTTON (button));
if (gtk_toggle_button_get_active (button))
{
privacy->clear ();
g_string_append (clear_data, privacy->name);
g_string_append_c (clear_data, ',');
}
}
g_object_set (browser->settings, "clear-data", clear_data->str, NULL);
g_string_free (clear_data, TRUE);
} }
if (response_id != GTK_RESPONSE_DELETE_EVENT) if (response_id != GTK_RESPONSE_DELETE_EVENT)
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
@ -4289,6 +4253,8 @@ _action_clear_private_data_activate (GtkAction* action,
GtkWidget* icon; GtkWidget* icon;
GtkWidget* label; GtkWidget* label;
GtkWidget* button; GtkWidget* button;
GList* data_items;
gchar* clear_data = katze_object_get_string (browser->settings, "clear-data");
gint clear_prefs = MIDORI_CLEAR_NONE; gint clear_prefs = MIDORI_CLEAR_NONE;
g_object_get (browser->settings, "clear-private-data", &clear_prefs, NULL); g_object_get (browser->settings, "clear-private-data", &clear_prefs, NULL);
@ -4327,31 +4293,23 @@ _action_clear_private_data_activate (GtkAction* action,
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_object_set_data (G_OBJECT (dialog), "history", button); g_object_set_data (G_OBJECT (dialog), "history", button);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_check_button_new_with_mnemonic (_("Cookies"));
if ((clear_prefs & MIDORI_CLEAR_COOKIES) == MIDORI_CLEAR_COOKIES)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_object_set_data (G_OBJECT (dialog), "cookies", button);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_check_button_new_with_mnemonic (_("'Flash' Cookies"));
if ((clear_prefs & MIDORI_CLEAR_FLASH_COOKIES) == MIDORI_CLEAR_FLASH_COOKIES)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_object_set_data (G_OBJECT (dialog), "flash-cookies", button);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_check_button_new_with_mnemonic (_("Website icons"));
if ((clear_prefs & MIDORI_CLEAR_WEBSITE_ICONS) == MIDORI_CLEAR_WEBSITE_ICONS)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_object_set_data (G_OBJECT (dialog), "website-icons", button);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_check_button_new_with_mnemonic (_("_Closed Tabs")); button = gtk_check_button_new_with_mnemonic (_("_Closed Tabs"));
if ((clear_prefs & MIDORI_CLEAR_TRASH) == MIDORI_CLEAR_TRASH) if ((clear_prefs & MIDORI_CLEAR_TRASH) == MIDORI_CLEAR_TRASH)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_object_set_data (G_OBJECT (dialog), "trash", button); g_object_set_data (G_OBJECT (dialog), "trash", button);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_check_button_new_with_mnemonic (_("Web Cache"));
if ((clear_prefs & MIDORI_CLEAR_WEB_CACHE) == MIDORI_CLEAR_WEB_CACHE) data_items = sokoke_register_privacy_item (NULL, NULL, NULL);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); for (; data_items != NULL; data_items = g_list_next (data_items))
g_object_set_data (G_OBJECT (dialog), "web-cache", button); {
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0); SokokePrivacyItem* privacy = data_items->data;
button = gtk_check_button_new_with_mnemonic (privacy->label);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
g_object_set_data (G_OBJECT (dialog), privacy->name, button);
if (clear_data && strstr (clear_data, privacy->name))
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
}
g_free (clear_data);
gtk_container_add (GTK_CONTAINER (alignment), vbox); gtk_container_add (GTK_CONTAINER (alignment), vbox);
gtk_box_pack_start (GTK_BOX (hbox), alignment, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (hbox), alignment, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0);

View file

@ -87,6 +87,7 @@ struct _MidoriWebSettings
gchar* ident_string; gchar* ident_string;
gint clear_private_data; gint clear_private_data;
gchar* clear_data;
}; };
struct _MidoriWebSettingsClass struct _MidoriWebSettingsClass
@ -169,7 +170,8 @@ enum
PROP_USER_AGENT, PROP_USER_AGENT,
PROP_PREFERRED_LANGUAGES, PROP_PREFERRED_LANGUAGES,
PROP_CLEAR_PRIVATE_DATA PROP_CLEAR_PRIVATE_DATA,
PROP_CLEAR_DATA
}; };
GType GType
@ -1085,7 +1087,7 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class)
/** /**
* MidoriWebSettings:clear-private-data: * MidoriWebSettings:clear-private-data:
* *
* The private data selected for deletion. * The core data selected for deletion.
* *
* Since: 0.1.7 * Since: 0.1.7
*/ */
@ -1098,6 +1100,22 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class)
0, G_MAXINT, 0, 0, G_MAXINT, 0,
flags)); flags));
/**
* MidoriWebSettings:clear-data:
*
* The data selected for deletion, including extensions.
*
* Since: 0.2.9
*/
g_object_class_install_property (gobject_class,
PROP_CLEAR_DATA,
g_param_spec_string (
"clear-data",
_("Clear data"),
_("The data selected for deletion"),
NULL,
flags));
} }
static void static void
@ -1505,6 +1523,9 @@ midori_web_settings_set_property (GObject* object,
case PROP_CLEAR_PRIVATE_DATA: case PROP_CLEAR_PRIVATE_DATA:
web_settings->clear_private_data = g_value_get_int (value); web_settings->clear_private_data = g_value_get_int (value);
break; break;
case PROP_CLEAR_DATA:
katze_assign (web_settings->clear_data, g_value_dup_string (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -1733,6 +1754,9 @@ midori_web_settings_get_property (GObject* object,
case PROP_CLEAR_PRIVATE_DATA: case PROP_CLEAR_PRIVATE_DATA:
g_value_set_int (value, web_settings->clear_private_data); g_value_set_int (value, web_settings->clear_private_data);
break; break;
case PROP_CLEAR_DATA:
g_value_set_string (value, web_settings->clear_data);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;

View file

@ -38,12 +38,12 @@ enum
{ {
MIDORI_CLEAR_NONE = 0, MIDORI_CLEAR_NONE = 0,
MIDORI_CLEAR_HISTORY = 1, MIDORI_CLEAR_HISTORY = 1,
MIDORI_CLEAR_COOKIES = 2, MIDORI_CLEAR_COOKIES = 2, /* deprecated */
MIDORI_CLEAR_FLASH_COOKIES = 4, MIDORI_CLEAR_FLASH_COOKIES = 4, /* deprecated */
MIDORI_CLEAR_WEBSITE_ICONS = 8, MIDORI_CLEAR_WEBSITE_ICONS = 8, /* deprecated */
MIDORI_CLEAR_TRASH = 16, MIDORI_CLEAR_TRASH = 16,
MIDORI_CLEAR_ON_QUIT = 32, MIDORI_CLEAR_ON_QUIT = 32,
MIDORI_CLEAR_WEB_CACHE = 64, MIDORI_CLEAR_WEB_CACHE = 64, /* deprecated */
}; };
typedef enum typedef enum

View file

@ -2022,3 +2022,37 @@ sokoke_accept_languages (const gchar* const * lang_names)
return langs_str; return langs_str;
} }
/**
* sokoke_register_privacy_item:
* @name: the name of the privacy item
* @label: a user visible, localized label
* @clear: a callback clearing data
*
* Registers an item to clear data, either via the
* Clear Private Data dialogue or when Midori quits.
*
* Return value: a #GList if all arguments are %NULL
**/
GList*
sokoke_register_privacy_item (const gchar* name,
const gchar* label,
GCallback clear)
{
static GList* items = NULL;
SokokePrivacyItem* item;
if (name == NULL && label == NULL && clear == NULL)
return items;
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (label != NULL, NULL);
g_return_val_if_fail (clear != NULL, NULL);
item = g_new (SokokePrivacyItem, 1);
item->name = g_strdup (name);
item->label = g_strdup (label);
item->clear = clear;
items = g_list_append (items, item);
return NULL;
}

View file

@ -261,4 +261,16 @@ gboolean
sokoke_recursive_fork_protection (const gchar* uri, sokoke_recursive_fork_protection (const gchar* uri,
gboolean set_uri); gboolean set_uri);
typedef struct
{
gchar* name;
gchar* label;
GCallback clear;
} SokokePrivacyItem;
GList*
sokoke_register_privacy_item (const gchar* name,
const gchar* label,
GCallback clear);
#endif /* !__SOKOKE_H__ */ #endif /* !__SOKOKE_H__ */