From e46b4adef5c6c6805b3ca6dbfbe99a4299252514 Mon Sep 17 00:00:00 2001 From: haxar Date: Tue, 21 Feb 2012 20:14:01 -0800 Subject: gsd to msd complete rename patch by NiceandGently; file rename commit --- plugins/housekeeping/gsd-disk-space.c | 733 ------------------------ plugins/housekeeping/gsd-disk-space.h | 40 -- plugins/housekeeping/gsd-housekeeping-manager.c | 389 ------------- plugins/housekeeping/gsd-housekeeping-manager.h | 59 -- plugins/housekeeping/gsd-housekeeping-plugin.c | 104 ---- plugins/housekeeping/gsd-housekeeping-plugin.h | 61 -- plugins/housekeeping/gsd-ldsm-dialog.c | 476 --------------- plugins/housekeeping/gsd-ldsm-dialog.h | 72 --- plugins/housekeeping/gsd-ldsm-trash-empty.c | 398 ------------- plugins/housekeeping/gsd-ldsm-trash-empty.h | 27 - plugins/housekeeping/msd-disk-space.c | 733 ++++++++++++++++++++++++ plugins/housekeeping/msd-disk-space.h | 40 ++ plugins/housekeeping/msd-housekeeping-manager.c | 389 +++++++++++++ plugins/housekeeping/msd-housekeeping-manager.h | 59 ++ plugins/housekeeping/msd-housekeeping-plugin.c | 104 ++++ plugins/housekeeping/msd-housekeeping-plugin.h | 61 ++ plugins/housekeeping/msd-ldsm-dialog.c | 476 +++++++++++++++ plugins/housekeeping/msd-ldsm-dialog.h | 72 +++ plugins/housekeeping/msd-ldsm-trash-empty.c | 398 +++++++++++++ plugins/housekeeping/msd-ldsm-trash-empty.h | 27 + 20 files changed, 2359 insertions(+), 2359 deletions(-) delete mode 100644 plugins/housekeeping/gsd-disk-space.c delete mode 100644 plugins/housekeeping/gsd-disk-space.h delete mode 100644 plugins/housekeeping/gsd-housekeeping-manager.c delete mode 100644 plugins/housekeeping/gsd-housekeeping-manager.h delete mode 100644 plugins/housekeeping/gsd-housekeeping-plugin.c delete mode 100644 plugins/housekeeping/gsd-housekeeping-plugin.h delete mode 100644 plugins/housekeeping/gsd-ldsm-dialog.c delete mode 100644 plugins/housekeeping/gsd-ldsm-dialog.h delete mode 100644 plugins/housekeeping/gsd-ldsm-trash-empty.c delete mode 100644 plugins/housekeeping/gsd-ldsm-trash-empty.h create mode 100644 plugins/housekeeping/msd-disk-space.c create mode 100644 plugins/housekeeping/msd-disk-space.h create mode 100644 plugins/housekeeping/msd-housekeeping-manager.c create mode 100644 plugins/housekeeping/msd-housekeeping-manager.h create mode 100644 plugins/housekeeping/msd-housekeeping-plugin.c create mode 100644 plugins/housekeeping/msd-housekeeping-plugin.h create mode 100644 plugins/housekeeping/msd-ldsm-dialog.c create mode 100644 plugins/housekeeping/msd-ldsm-dialog.h create mode 100644 plugins/housekeeping/msd-ldsm-trash-empty.c create mode 100644 plugins/housekeeping/msd-ldsm-trash-empty.h (limited to 'plugins/housekeeping') diff --git a/plugins/housekeeping/gsd-disk-space.c b/plugins/housekeeping/gsd-disk-space.c deleted file mode 100644 index 6842ae5..0000000 --- a/plugins/housekeeping/gsd-disk-space.c +++ /dev/null @@ -1,733 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * vim: set et sw=8 ts=8: - * - * Copyright (c) 2008, Novell, Inc. - * - * Authors: Vincent Untz - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -/* gcc -DHAVE_LIBMATENOTIFY -DTEST -Wall `pkg-config --cflags --libs gobject-2.0 gio-unix-2.0 glib-2.0 gtk+-2.0 libmatenotify` -o msd-disk-space-test msd-disk-space.c */ - -#include "config.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "msd-disk-space.h" -#include "msd-ldsm-dialog.h" -#include "msd-ldsm-trash-empty.h" - - -#define GIGABYTE 1024 * 1024 * 1024 - -#define CHECK_EVERY_X_SECONDS 60 - -#define DISK_SPACE_ANALYZER "baobab" - -#define MATECONF_HOUSEKEEPING_DIR "/apps/mate_settings_daemon/plugins/housekeeping" -#define MATECONF_FREE_PC_NOTIFY_KEY "free_percent_notify" -#define MATECONF_FREE_PC_NOTIFY_AGAIN_KEY "free_percent_notify_again" -#define MATECONF_FREE_SIZE_NO_NOTIFY "free_size_gb_no_notify" -#define MATECONF_MIN_NOTIFY_PERIOD "min_notify_period" -#define MATECONF_IGNORE_PATHS "ignore_paths" - -typedef struct -{ - GUnixMountEntry *mount; - struct statvfs buf; - time_t notify_time; -} LdsmMountInfo; - -static GHashTable *ldsm_notified_hash = NULL; -static unsigned int ldsm_timeout_id = 0; -static GUnixMountMonitor *ldsm_monitor = NULL; -static double free_percent_notify = 0.05; -static double free_percent_notify_again = 0.01; -static unsigned int free_size_gb_no_notify = 2; -static unsigned int min_notify_period = 10; -static GSList *ignore_paths = NULL; -static unsigned int mateconf_notify_id; -static MateConfClient *client = NULL; -static MsdLdsmDialog *dialog = NULL; -static guint64 *time_read; - -static gchar* -ldsm_get_fs_id_for_path (const gchar *path) -{ - GFile *file; - GFileInfo *fileinfo; - gchar *attr_id_fs; - - file = g_file_new_for_path (path); - fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_ID_FILESYSTEM, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL); - if (fileinfo) { - attr_id_fs = g_strdup (g_file_info_get_attribute_string (fileinfo, G_FILE_ATTRIBUTE_ID_FILESYSTEM)); - g_object_unref (fileinfo); - } else { - attr_id_fs = NULL; - } - - g_object_unref (file); - - return attr_id_fs; -} - -static gboolean -ldsm_mount_has_trash (LdsmMountInfo *mount) -{ - const gchar *user_data_dir; - gchar *user_data_attr_id_fs; - gchar *path_attr_id_fs; - gboolean mount_uses_user_trash = FALSE; - gchar *trash_files_dir; - gboolean has_trash = FALSE; - GDir *dir; - const gchar *path; - - user_data_dir = g_get_user_data_dir (); - user_data_attr_id_fs = ldsm_get_fs_id_for_path (user_data_dir); - - path = g_unix_mount_get_mount_path (mount->mount); - path_attr_id_fs = ldsm_get_fs_id_for_path (path); - - if (g_strcmp0 (user_data_attr_id_fs, path_attr_id_fs) == 0) { - /* The volume that is low on space is on the same volume as our home - * directory. This means the trash is at $XDG_DATA_HOME/Trash, - * not at the root of the volume which is full. - */ - mount_uses_user_trash = TRUE; - } - - g_free (user_data_attr_id_fs); - g_free (path_attr_id_fs); - - /* I can't think of a better way to find out if a volume has any trash. Any suggestions? */ - if (mount_uses_user_trash) { - trash_files_dir = g_build_filename (g_get_user_data_dir (), "Trash", "files", NULL); - } else { - gchar *uid; - - uid = g_strdup_printf ("%d", getuid ()); - trash_files_dir = g_build_filename (path, ".Trash", uid, "files", NULL); - if (!g_file_test (trash_files_dir, G_FILE_TEST_IS_DIR)) { - gchar *trash_dir; - - g_free (trash_files_dir); - trash_dir = g_strdup_printf (".Trash-%s", uid); - trash_files_dir = g_build_filename (path, trash_dir, "files", NULL); - g_free (trash_dir); - if (!g_file_test (trash_files_dir, G_FILE_TEST_IS_DIR)) { - g_free (trash_files_dir); - g_free (uid); - return has_trash; - } - } - g_free (uid); - } - - dir = g_dir_open (trash_files_dir, 0, NULL); - if (dir) { - if (g_dir_read_name (dir)) - has_trash = TRUE; - g_dir_close (dir); - } - - g_free (trash_files_dir); - - return has_trash; -} - -static void -ldsm_analyze_path (const gchar *path) -{ - const gchar *argv[] = { DISK_SPACE_ANALYZER, path, NULL }; - - g_spawn_async (NULL, (gchar **) argv, NULL, G_SPAWN_SEARCH_PATH, - NULL, NULL, NULL, NULL); -} - -static gboolean -ldsm_notify_for_mount (LdsmMountInfo *mount, - gboolean multiple_volumes, - gboolean other_usable_volumes) -{ - gchar *name, *program; - gint64 free_space; - gint response; - gboolean has_trash; - gboolean has_disk_analyzer; - gboolean retval = TRUE; - const gchar *path; - - /* Don't show a dialog if one is already displayed */ - if (dialog) - return retval; - - name = g_unix_mount_guess_name (mount->mount); - free_space = (gint64) mount->buf.f_frsize * (gint64) mount->buf.f_bavail; - has_trash = ldsm_mount_has_trash (mount); - path = g_unix_mount_get_mount_path (mount->mount); - - program = g_find_program_in_path (DISK_SPACE_ANALYZER); - has_disk_analyzer = (program != NULL); - g_free (program); - - dialog = msd_ldsm_dialog_new (other_usable_volumes, - multiple_volumes, - has_disk_analyzer, - has_trash, - free_space, - name, - path); - - g_free (name); - - g_object_ref (G_OBJECT (dialog)); - response = gtk_dialog_run (GTK_DIALOG (dialog)); - - gtk_object_destroy (GTK_OBJECT (dialog)); - dialog = NULL; - - switch (response) { - case GTK_RESPONSE_CANCEL: - retval = FALSE; - break; - case MSD_LDSM_DIALOG_RESPONSE_ANALYZE: - retval = FALSE; - ldsm_analyze_path (g_unix_mount_get_mount_path (mount->mount)); - break; - case MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH: - retval = TRUE; - msd_ldsm_trash_empty (); - break; - case GTK_RESPONSE_NONE: - case GTK_RESPONSE_DELETE_EVENT: - retval = TRUE; - break; - default: - g_assert_not_reached (); - } - - return retval; -} - -static gboolean -ldsm_mount_has_space (LdsmMountInfo *mount) -{ - gdouble free_space; - - free_space = (double) mount->buf.f_bavail / (double) mount->buf.f_blocks; - /* enough free space, nothing to do */ - if (free_space > free_percent_notify) - return TRUE; - - if (((gint64) mount->buf.f_frsize * (gint64) mount->buf.f_bavail) > ((gint64) free_size_gb_no_notify * GIGABYTE)) - return TRUE; - - /* If we got here, then this volume is low on space */ - return FALSE; -} - -static gboolean -ldsm_mount_is_virtual (LdsmMountInfo *mount) -{ - if (mount->buf.f_blocks == 0) { - /* Filesystems with zero blocks are virtual */ - return TRUE; - } - - return FALSE; -} - -static gint -ldsm_ignore_path_compare (gconstpointer a, - gconstpointer b) -{ - return g_strcmp0 ((const gchar *)a, (const gchar *)b); -} - -static gboolean -ldsm_mount_is_user_ignore (const gchar *path) -{ - if (g_slist_find_custom (ignore_paths, path, (GCompareFunc) ldsm_ignore_path_compare) != NULL) - return TRUE; - else - return FALSE; -} - - -static gboolean -is_in (const gchar *value, const gchar *set[]) -{ - int i; - for (i = 0; set[i] != NULL; i++) - { - if (strcmp (set[i], value) == 0) - return TRUE; - } - return FALSE; -} - -static gboolean -ldsm_mount_should_ignore (GUnixMountEntry *mount) -{ - const gchar *fs, *device, *path; - - path = g_unix_mount_get_mount_path (mount); - if (ldsm_mount_is_user_ignore (path)) - return TRUE; - - /* This is borrowed from GLib and used as a way to determine - * which mounts we should ignore by default. GLib doesn't - * expose this in a way that allows it to be used for this - * purpose - */ - - const gchar *ignore_fs[] = { - "auto", - "autofs", - "devfs", - "devpts", - "ecryptfs", - "kernfs", - "linprocfs", - "proc", - "procfs", - "ptyfs", - "selinuxfs", - "linsysfs", - "sysfs", - "tmpfs", - "usbfs", - "nfsd", - "rpc_pipefs", - "zfs", - NULL - }; - const gchar *ignore_devices[] = { - "none", - "sunrpc", - "devpts", - "nfsd", - "/dev/loop", - "/dev/vn", - NULL - }; - - fs = g_unix_mount_get_fs_type (mount); - device = g_unix_mount_get_device_path (mount); - - if (is_in (fs, ignore_fs)) - return TRUE; - - if (is_in (device, ignore_devices)) - return TRUE; - - return FALSE; -} - -static void -ldsm_free_mount_info (gpointer data) -{ - LdsmMountInfo *mount = data; - - g_return_if_fail (mount != NULL); - - g_unix_mount_free (mount->mount); - g_free (mount); -} - -static void -ldsm_maybe_warn_mounts (GList *mounts, - gboolean multiple_volumes, - gboolean other_usable_volumes) -{ - GList *l; - gboolean done = FALSE; - - for (l = mounts; l != NULL; l = l->next) { - LdsmMountInfo *mount_info = l->data; - LdsmMountInfo *previous_mount_info; - gdouble free_space; - gdouble previous_free_space; - time_t curr_time; - const gchar *path; - gboolean show_notify; - - if (done) { - /* Don't show any more dialogs if the user took action with the last one. The user action - * might free up space on multiple volumes, making the next dialog redundant. - */ - ldsm_free_mount_info (mount_info); - continue; - } - - path = g_unix_mount_get_mount_path (mount_info->mount); - - previous_mount_info = g_hash_table_lookup (ldsm_notified_hash, path); - if (previous_mount_info != NULL) - previous_free_space = (gdouble) previous_mount_info->buf.f_bavail / (gdouble) previous_mount_info->buf.f_blocks; - - free_space = (gdouble) mount_info->buf.f_bavail / (gdouble) mount_info->buf.f_blocks; - - if (previous_mount_info == NULL) { - /* We haven't notified for this mount yet */ - show_notify = TRUE; - mount_info->notify_time = time (NULL); - g_hash_table_replace (ldsm_notified_hash, g_strdup (path), mount_info); - } else if ((previous_free_space - free_space) > free_percent_notify_again) { - /* We've notified for this mount before and free space has decreased sufficiently since last time to notify again */ - curr_time = time (NULL); - if (difftime (curr_time, previous_mount_info->notify_time) > (gdouble)(min_notify_period * 60)) { - show_notify = TRUE; - mount_info->notify_time = curr_time; - } else { - /* It's too soon to show the dialog again. However, we still replace the LdsmMountInfo - * struct in the hash table, but give it the notfiy time from the previous dialog. - * This will stop the notification from reappearing unnecessarily as soon as the timeout expires. - */ - show_notify = FALSE; - mount_info->notify_time = previous_mount_info->notify_time; - } - g_hash_table_replace (ldsm_notified_hash, g_strdup (path), mount_info); - } else { - /* We've notified for this mount before, but the free space hasn't decreased sufficiently to notify again */ - ldsm_free_mount_info (mount_info); - show_notify = FALSE; - } - - if (show_notify) { - if (ldsm_notify_for_mount (mount_info, multiple_volumes, other_usable_volumes)) - done = TRUE; - } - } -} - -static gboolean -ldsm_check_all_mounts (gpointer data) -{ - GList *mounts; - GList *l; - GList *check_mounts = NULL; - GList *full_mounts = NULL; - guint number_of_mounts; - guint number_of_full_mounts; - gboolean multiple_volumes = FALSE; - gboolean other_usable_volumes = FALSE; - - /* We iterate through the static mounts in /etc/fstab first, seeing if - * they're mounted by checking if the GUnixMountPoint has a corresponding GUnixMountEntry. - * Iterating through the static mounts means we automatically ignore dynamically mounted media. - */ - mounts = g_unix_mount_points_get (time_read); - - for (l = mounts; l != NULL; l = l->next) { - GUnixMountPoint *mount_point = l->data; - GUnixMountEntry *mount; - LdsmMountInfo *mount_info; - const gchar *path; - - path = g_unix_mount_point_get_mount_path (mount_point); - mount = g_unix_mount_at (path, time_read); - g_unix_mount_point_free (mount_point); - if (mount == NULL) { - /* The GUnixMountPoint is not mounted */ - continue; - } - - mount_info = g_new0 (LdsmMountInfo, 1); - mount_info->mount = mount; - - path = g_unix_mount_get_mount_path (mount); - - if (g_unix_mount_is_readonly (mount)) { - ldsm_free_mount_info (mount_info); - continue; - } - - if (ldsm_mount_should_ignore (mount)) { - ldsm_free_mount_info (mount_info); - continue; - } - - if (statvfs (path, &mount_info->buf) != 0) { - ldsm_free_mount_info (mount_info); - continue; - } - - if (ldsm_mount_is_virtual (mount_info)) { - ldsm_free_mount_info (mount_info); - continue; - } - - check_mounts = g_list_prepend (check_mounts, mount_info); - } - - number_of_mounts = g_list_length (check_mounts); - if (number_of_mounts > 1) - multiple_volumes = TRUE; - - for (l = check_mounts; l != NULL; l = l->next) { - LdsmMountInfo *mount_info = l->data; - - if (!ldsm_mount_has_space (mount_info)) { - full_mounts = g_list_prepend (full_mounts, mount_info); - } else { - g_hash_table_remove (ldsm_notified_hash, g_unix_mount_get_mount_path (mount_info->mount)); - ldsm_free_mount_info (mount_info); - } - } - - number_of_full_mounts = g_list_length (full_mounts); - if (number_of_mounts > number_of_full_mounts) - other_usable_volumes = TRUE; - - ldsm_maybe_warn_mounts (full_mounts, multiple_volumes, - other_usable_volumes); - - g_list_free (check_mounts); - g_list_free (full_mounts); - - return TRUE; -} - -static gboolean -ldsm_is_hash_item_not_in_mounts (gpointer key, - gpointer value, - gpointer user_data) -{ - GList *l; - - for (l = (GList *) user_data; l != NULL; l = l->next) { - GUnixMountEntry *mount = l->data; - const char *path; - - path = g_unix_mount_get_mount_path (mount); - - if (strcmp (path, key) == 0) - return FALSE; - } - - return TRUE; -} - -static void -ldsm_mounts_changed (GObject *monitor, - gpointer data) -{ - GList *mounts; - - /* remove the saved data for mounts that got removed */ - mounts = g_unix_mounts_get (time_read); - g_hash_table_foreach_remove (ldsm_notified_hash, - ldsm_is_hash_item_not_in_mounts, mounts); - g_list_foreach (mounts, (GFunc) g_unix_mount_free, NULL); - - /* check the status now, for the new mounts */ - ldsm_check_all_mounts (NULL); - - /* and reset the timeout */ - if (ldsm_timeout_id) - g_source_remove (ldsm_timeout_id); - ldsm_timeout_id = g_timeout_add_seconds (CHECK_EVERY_X_SECONDS, - ldsm_check_all_mounts, NULL); -} - -static gboolean -ldsm_is_hash_item_in_ignore_paths (gpointer key, - gpointer value, - gpointer user_data) -{ - return ldsm_mount_is_user_ignore (key); -} - -static void -msd_ldsm_get_config () -{ - GError *error = NULL; - - free_percent_notify = mateconf_client_get_float (client, - MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_PC_NOTIFY_KEY, - &error); - if (error != NULL) { - g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); - g_clear_error (&error); - } - if (free_percent_notify >= 1 || free_percent_notify < 0) { - g_warning ("Invalid configuration of free_percent_notify: %f\n" \ - "Using sensible default", free_percent_notify); - free_percent_notify = 0.05; - } - - free_percent_notify_again = mateconf_client_get_float (client, - MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_PC_NOTIFY_AGAIN_KEY, - &error); - if (error != NULL) { - g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); - g_clear_error (&error); - } - if (free_percent_notify_again >= 1 || free_percent_notify_again < 0) { - g_warning ("Invalid configuration of free_percent_notify_again: %f\n" \ - "Using sensible default\n", free_percent_notify_again); - free_percent_notify_again = 0.01; - } - - free_size_gb_no_notify = mateconf_client_get_int (client, - MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_SIZE_NO_NOTIFY, - &error); - if (error != NULL) { - g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); - g_clear_error (&error); - } - min_notify_period = mateconf_client_get_int (client, - MATECONF_HOUSEKEEPING_DIR "/" MATECONF_MIN_NOTIFY_PERIOD, - &error); - if (error != NULL) { - g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); - g_clear_error (&error); - } - - if (ignore_paths != NULL) { - g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); - g_slist_free (ignore_paths); - } - ignore_paths = mateconf_client_get_list (client, - MATECONF_HOUSEKEEPING_DIR "/" MATECONF_IGNORE_PATHS, - MATECONF_VALUE_STRING, &error); - if (error != NULL) { - g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); - g_clear_error (&error); - } else { - /* Make sure we dont leave stale entries in ldsm_notified_hash */ - g_hash_table_foreach_remove (ldsm_notified_hash, - ldsm_is_hash_item_in_ignore_paths, NULL); - } -} - -static void -msd_ldsm_update_config (MateConfClient *client, - guint cnxn_id, - MateConfEntry *entry, - gpointer user_data) -{ - msd_ldsm_get_config (); -} - -void -msd_ldsm_setup (gboolean check_now) -{ - GError *error = NULL; - - if (ldsm_notified_hash || ldsm_timeout_id || ldsm_monitor) { - g_warning ("Low disk space monitor already initialized."); - return; - } - - ldsm_notified_hash = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, - ldsm_free_mount_info); - - client = mateconf_client_get_default (); - if (client != NULL) { - msd_ldsm_get_config (); - mateconf_notify_id = mateconf_client_notify_add (client, - MATECONF_HOUSEKEEPING_DIR, - (MateConfClientNotifyFunc) msd_ldsm_update_config, - NULL, NULL, &error); - if (error != NULL) { - g_warning ("Cannot register callback for MateConf notification"); - g_clear_error (&error); - } - } else { - g_warning ("Failed to get default client"); - } - - ldsm_monitor = g_unix_mount_monitor_new (); - g_unix_mount_monitor_set_rate_limit (ldsm_monitor, 1000); - g_signal_connect (ldsm_monitor, "mounts-changed", - G_CALLBACK (ldsm_mounts_changed), NULL); - - if (check_now) - ldsm_check_all_mounts (NULL); - - ldsm_timeout_id = g_timeout_add_seconds (CHECK_EVERY_X_SECONDS, - ldsm_check_all_mounts, NULL); - -} - -void -msd_ldsm_clean (void) -{ - if (ldsm_timeout_id) - g_source_remove (ldsm_timeout_id); - ldsm_timeout_id = 0; - - if (ldsm_notified_hash) - g_hash_table_destroy (ldsm_notified_hash); - ldsm_notified_hash = NULL; - - if (ldsm_monitor) - g_object_unref (ldsm_monitor); - ldsm_monitor = NULL; - - if (client) { - mateconf_client_notify_remove (client, mateconf_notify_id); - g_object_unref (client); - } - - if (dialog) { - gtk_widget_destroy (GTK_WIDGET (dialog)); - dialog = NULL; - } - - if (ignore_paths) { - g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); - g_slist_free (ignore_paths); - } -} - -#ifdef TEST -int -main (int argc, - char **argv) -{ - GMainLoop *loop; - - gtk_init (&argc, &argv); - - loop = g_main_loop_new (NULL, FALSE); - - msd_ldsm_setup (TRUE); - - g_main_loop_run (loop); - - msd_ldsm_clean (); - g_main_loop_unref (loop); - - return 0; -} -#endif /* TEST */ diff --git a/plugins/housekeeping/gsd-disk-space.h b/plugins/housekeeping/gsd-disk-space.h deleted file mode 100644 index 43f7059..0000000 --- a/plugins/housekeeping/gsd-disk-space.h +++ /dev/null @@ -1,40 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * vim: set et sw=8 ts=8: - * - * Copyright (c) 2008, Novell, Inc. - * - * Authors: Vincent Untz - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#ifndef __MSD_DISK_SPACE_H -#define __MSD_DISK_SPACE_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -void msd_ldsm_setup (gboolean check_now); -void msd_ldsm_clean (void); - -#ifdef __cplusplus -} -#endif - -#endif /* __MSD_DISK_SPACE_H */ diff --git a/plugins/housekeeping/gsd-housekeeping-manager.c b/plugins/housekeeping/gsd-housekeeping-manager.c deleted file mode 100644 index 6cb3353..0000000 --- a/plugins/housekeeping/gsd-housekeeping-manager.c +++ /dev/null @@ -1,389 +0,0 @@ -/* - * Copyright (C) 2008 Michael J. Chudobiak - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "mate-settings-profile.h" -#include "msd-housekeeping-manager.h" -#include "msd-disk-space.h" - - -/* General */ -#define INTERVAL_ONCE_A_DAY 24*60*60 -#define INTERVAL_TWO_MINUTES 2*60 - - -/* Thumbnail cleaner */ -#define MATECONF_THUMB_AGE "/desktop/mate/thumbnail_cache/maximum_age" -#define DEFAULT_MAX_AGE_IN_DAYS 180 -#define MATECONF_THUMB_SIZE "/desktop/mate/thumbnail_cache/maximum_size" -#define DEFAULT_MAX_SIZE_IN_MB 512 -#define MATECONF_THUMB_BINDING_DIR "/desktop/mate/thumbnail_cache" - - -struct MsdHousekeepingManagerPrivate { - guint long_term_cb; - guint short_term_cb; - guint mateconf_notify; -}; - - -#define MSD_HOUSEKEEPING_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerPrivate)) - -static void msd_housekeeping_manager_class_init (MsdHousekeepingManagerClass *klass); -static void msd_housekeeping_manager_init (MsdHousekeepingManager *housekeeping_manager); - -G_DEFINE_TYPE (MsdHousekeepingManager, msd_housekeeping_manager, G_TYPE_OBJECT) - -static gpointer manager_object = NULL; - - -typedef struct { - glong now; - glong max_age; - goffset total_size; - goffset max_size; -} PurgeData; - - -typedef struct { - time_t mtime; - char *path; - glong size; -} ThumbData; - - -static void -thumb_data_free (gpointer data) -{ - ThumbData *info = data; - - if (info) { - g_free (info->path); - g_free (info); - } -} - - -static GList * -read_dir_for_purge (const char *path, GList *files) -{ - GFile *read_path; - GFileEnumerator *enum_dir; - - read_path = g_file_new_for_path (path); - enum_dir = g_file_enumerate_children (read_path, - G_FILE_ATTRIBUTE_STANDARD_NAME "," - G_FILE_ATTRIBUTE_TIME_MODIFIED "," - G_FILE_ATTRIBUTE_STANDARD_SIZE, - G_FILE_QUERY_INFO_NONE, - NULL, - NULL); - - if (enum_dir != NULL) { - GFileInfo *info; - while ((info = g_file_enumerator_next_file (enum_dir, NULL, NULL)) != NULL) { - const char *name; - name = g_file_info_get_name (info); - - if (strlen (name) == 36 && strcmp (name + 32, ".png") == 0) { - ThumbData *td; - GFile *entry; - char *entry_path; - GTimeVal mod_time; - - entry = g_file_get_child (read_path, name); - entry_path = g_file_get_path (entry); - g_object_unref (entry); - - g_file_info_get_modification_time (info, &mod_time); - - td = g_new0 (ThumbData, 1); - td->path = entry_path; - td->mtime = mod_time.tv_sec; - td->size = g_file_info_get_size (info); - - files = g_list_prepend (files, td); - } - g_object_unref (info); - } - g_object_unref (enum_dir); - } - g_object_unref (read_path); - - return files; -} - - -static void -purge_old_thumbnails (ThumbData *info, PurgeData *purge_data) -{ - if ((purge_data->now - info->mtime) > purge_data->max_age) { - g_unlink (info->path); - info->size = 0; - } else { - purge_data->total_size += info->size; - } -} - - -static int -sort_file_mtime (ThumbData *file1, ThumbData *file2) -{ - return file1->mtime - file2->mtime; -} - - -static int -get_mateconf_int_with_default (char *key, int default_value) -{ - /* If the key is unset, we use a non-zero default value. - A zero value corresponds to an extra-paranoid level - of cleaning - it deletes all files. We don't want that - as a default condition. */ - - MateConfValue *value; - MateConfClient *client; - int res; - - client = mateconf_client_get_default (); - value = mateconf_client_get (client, key, NULL); - g_object_unref (client); - - if (value == NULL || value->type != MATECONF_VALUE_INT) { - res = default_value; - } else { - res = mateconf_value_get_int (value); - mateconf_value_free (value); - } - - return res; -} - - -static void -purge_thumbnail_cache (void) -{ - - char *path; - GList *files; - PurgeData purge_data; - GTimeVal current_time; - - g_debug ("housekeeping: checking thumbnail cache size and freshness"); - - path = g_build_filename (g_get_home_dir (), - ".thumbnails", - "normal", - NULL); - files = read_dir_for_purge (path, NULL); - g_free (path); - - path = g_build_filename (g_get_home_dir (), - ".thumbnails", - "large", - NULL); - files = read_dir_for_purge (path, files); - g_free (path); - - path = g_build_filename (g_get_home_dir (), - ".thumbnails", - "fail", - "mate-thumbnail-factory", - NULL); - files = read_dir_for_purge (path, files); - g_free (path); - - g_get_current_time (¤t_time); - - purge_data.now = current_time.tv_sec; - purge_data.max_age = get_mateconf_int_with_default (MATECONF_THUMB_AGE, DEFAULT_MAX_AGE_IN_DAYS) * 24 * 60 * 60; - purge_data.max_size = get_mateconf_int_with_default (MATECONF_THUMB_SIZE, DEFAULT_MAX_SIZE_IN_MB) * 1024 * 1024; - purge_data.total_size = 0; - - if (purge_data.max_age >= 0) - g_list_foreach (files, (GFunc) purge_old_thumbnails, &purge_data); - - if ((purge_data.total_size > purge_data.max_size) && (purge_data.max_size >= 0)) { - GList *scan; - files = g_list_sort (files, (GCompareFunc) sort_file_mtime); - for (scan = files; scan && (purge_data.total_size > purge_data.max_size); scan = scan->next) { - ThumbData *info = scan->data; - g_unlink (info->path); - purge_data.total_size -= info->size; - } - } - - g_list_foreach (files, (GFunc) thumb_data_free, NULL); - g_list_free (files); -} - - -static gboolean -do_cleanup (MsdHousekeepingManager *manager) -{ - purge_thumbnail_cache (); - return TRUE; -} - - -static gboolean -do_cleanup_once (MsdHousekeepingManager *manager) -{ - do_cleanup (manager); - manager->priv->short_term_cb = 0; - return FALSE; -} - - -static void -do_cleanup_soon (MsdHousekeepingManager *manager) -{ - if (manager->priv->short_term_cb == 0) { - g_debug ("housekeeping: will tidy up in 2 minutes"); - manager->priv->short_term_cb = g_timeout_add_seconds (INTERVAL_TWO_MINUTES, - (GSourceFunc) do_cleanup_once, - manager); - } -} - - -static void -bindings_callback (MateConfClient *client, - guint cnxn_id, - MateConfEntry *entry, - MsdHousekeepingManager *manager) -{ - do_cleanup_soon (manager); -} - - -static guint -register_config_callback (MsdHousekeepingManager *manager, - const char *path, - MateConfClientNotifyFunc func) -{ - MateConfClient *client = mateconf_client_get_default (); - guint notify; - - mateconf_client_add_dir (client, path, MATECONF_CLIENT_PRELOAD_NONE, NULL); - notify = mateconf_client_notify_add (client, path, func, manager, NULL, NULL); - - g_object_unref (client); - - return notify; -} - - -gboolean -msd_housekeeping_manager_start (MsdHousekeepingManager *manager, - GError **error) -{ - g_debug ("Starting housekeeping manager"); - mate_settings_profile_start (NULL); - - msd_ldsm_setup (FALSE); - - manager->priv->mateconf_notify = register_config_callback (manager, - MATECONF_THUMB_BINDING_DIR, - (MateConfClientNotifyFunc) bindings_callback); - - /* Clean once, a few minutes after start-up */ - do_cleanup_soon (manager); - - /* Clean periodically, on a daily basis. */ - manager->priv->long_term_cb = g_timeout_add_seconds (INTERVAL_ONCE_A_DAY, - (GSourceFunc) do_cleanup, - manager); - mate_settings_profile_end (NULL); - - return TRUE; -} - - -void -msd_housekeeping_manager_stop (MsdHousekeepingManager *manager) -{ - MsdHousekeepingManagerPrivate *p = manager->priv; - - g_debug ("Stopping housekeeping manager"); - - if (p->mateconf_notify != 0) { - MateConfClient *client = mateconf_client_get_default (); - - mateconf_client_remove_dir (client, MATECONF_THUMB_BINDING_DIR, NULL); - mateconf_client_notify_remove (client, p->mateconf_notify); - - g_object_unref (client); - p->mateconf_notify = 0; - } - - if (p->short_term_cb) { - g_source_remove (p->short_term_cb); - p->short_term_cb = 0; - } - - if (p->long_term_cb) { - g_source_remove (p->long_term_cb); - p->long_term_cb = 0; - - /* Do a clean-up on shutdown if and only if the size or age - limits have been set to paranoid levels (zero) */ - if ((get_mateconf_int_with_default (MATECONF_THUMB_AGE, DEFAULT_MAX_AGE_IN_DAYS) == 0) || - (get_mateconf_int_with_default (MATECONF_THUMB_SIZE, DEFAULT_MAX_SIZE_IN_MB) == 0)) { - do_cleanup (manager); - } - } - - msd_ldsm_clean (); -} - - -static void -msd_housekeeping_manager_class_init (MsdHousekeepingManagerClass *klass) -{ - g_type_class_add_private (klass, sizeof (MsdHousekeepingManagerPrivate)); -} - - -static void -msd_housekeeping_manager_init (MsdHousekeepingManager *manager) -{ - manager->priv = MSD_HOUSEKEEPING_MANAGER_GET_PRIVATE (manager); -} - - -MsdHousekeepingManager * -msd_housekeeping_manager_new (void) -{ - if (manager_object != NULL) { - g_object_ref (manager_object); - } else { - manager_object = g_object_new (MSD_TYPE_HOUSEKEEPING_MANAGER, NULL); - g_object_add_weak_pointer (manager_object, - (gpointer *) &manager_object); - } - - return MSD_HOUSEKEEPING_MANAGER (manager_object); -} diff --git a/plugins/housekeeping/gsd-housekeeping-manager.h b/plugins/housekeeping/gsd-housekeeping-manager.h deleted file mode 100644 index 11f50a7..0000000 --- a/plugins/housekeeping/gsd-housekeeping-manager.h +++ /dev/null @@ -1,59 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * Copyright (C) 2008 Michael J. Chudobiak - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#ifndef __MSD_HOUSEKEEPING_MANAGER_H -#define __MSD_HOUSEKEEPING_MANAGER_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define MSD_TYPE_HOUSEKEEPING_MANAGER (msd_housekeeping_manager_get_type ()) -#define MSD_HOUSEKEEPING_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManager)) -#define MSD_HOUSEKEEPING_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerClass)) -#define MSD_IS_HOUSEKEEPING_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_HOUSEKEEPING_MANAGER)) -#define MSD_IS_HOUSEKEEPING_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_HOUSEKEEPING_MANAGER)) -#define MSD_HOUSEKEEPING_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerClass)) - -typedef struct MsdHousekeepingManagerPrivate MsdHousekeepingManagerPrivate; - -typedef struct { - GObject parent; - MsdHousekeepingManagerPrivate *priv; -} MsdHousekeepingManager; - -typedef struct { - GObjectClass parent_class; -} MsdHousekeepingManagerClass; - -GType msd_housekeeping_manager_get_type (void); - -MsdHousekeepingManager * msd_housekeeping_manager_new (void); -gboolean msd_housekeeping_manager_start (MsdHousekeepingManager *manager, - GError **error); -void msd_housekeeping_manager_stop (MsdHousekeepingManager *manager); - -#ifdef __cplusplus -} -#endif - -#endif /* __MSD_HOUSEKEEPING_MANAGER_H */ diff --git a/plugins/housekeeping/gsd-housekeeping-plugin.c b/plugins/housekeeping/gsd-housekeeping-plugin.c deleted file mode 100644 index 5b0cfef..0000000 --- a/plugins/housekeeping/gsd-housekeeping-plugin.c +++ /dev/null @@ -1,104 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * Copyright (C) 2008 Michael J. Chudobiak - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#include "config.h" - -#include -#include - -#include "mate-settings-plugin.h" -#include "msd-housekeeping-plugin.h" -#include "msd-housekeeping-manager.h" - -struct MsdHousekeepingPluginPrivate { - MsdHousekeepingManager *manager; -}; - -#define MSD_HOUSEKEEPING_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginPrivate)) - -MATE_SETTINGS_PLUGIN_REGISTER (MsdHousekeepingPlugin, msd_housekeeping_plugin) - -static void -msd_housekeeping_plugin_init (MsdHousekeepingPlugin *plugin) -{ - plugin->priv = MSD_HOUSEKEEPING_PLUGIN_GET_PRIVATE (plugin); - - g_debug ("MsdHousekeepingPlugin initializing"); - - plugin->priv->manager = msd_housekeeping_manager_new (); -} - -static void -msd_housekeeping_plugin_finalize (GObject *object) -{ - MsdHousekeepingPlugin *plugin; - - g_return_if_fail (object != NULL); - g_return_if_fail (MSD_IS_HOUSEKEEPING_PLUGIN (object)); - - g_debug ("MsdHousekeepingPlugin finalizing"); - - plugin = MSD_HOUSEKEEPING_PLUGIN (object); - - g_return_if_fail (plugin->priv != NULL); - - if (plugin->priv->manager != NULL) { - g_object_unref (plugin->priv->manager); - } - - G_OBJECT_CLASS (msd_housekeeping_plugin_parent_class)->finalize (object); -} - -static void -impl_activate (MateSettingsPlugin *plugin) -{ - gboolean res; - GError *error; - - g_debug ("Activating housekeeping plugin"); - - error = NULL; - res = msd_housekeeping_manager_start (MSD_HOUSEKEEPING_PLUGIN (plugin)->priv->manager, &error); - if (! res) { - g_warning ("Unable to start housekeeping manager: %s", error->message); - g_error_free (error); - } -} - -static void -impl_deactivate (MateSettingsPlugin *plugin) -{ - g_debug ("Deactivating housekeeping plugin"); - msd_housekeeping_manager_stop (MSD_HOUSEKEEPING_PLUGIN (plugin)->priv->manager); -} - -static void -msd_housekeeping_plugin_class_init (MsdHousekeepingPluginClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - MateSettingsPluginClass *plugin_class = MATE_SETTINGS_PLUGIN_CLASS (klass); - - object_class->finalize = msd_housekeeping_plugin_finalize; - - plugin_class->activate = impl_activate; - plugin_class->deactivate = impl_deactivate; - - g_type_class_add_private (klass, sizeof (MsdHousekeepingPluginPrivate)); -} diff --git a/plugins/housekeeping/gsd-housekeeping-plugin.h b/plugins/housekeeping/gsd-housekeeping-plugin.h deleted file mode 100644 index 1c2d816..0000000 --- a/plugins/housekeeping/gsd-housekeeping-plugin.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * Copyright (C) 2008 Michael J. Chudobiak - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#ifndef __MSD_HOUSEKEEPING_PLUGIN_H__ -#define __MSD_HOUSEKEEPING_PLUGIN_H__ - -#include -#include -#include - -#include "mate-settings-plugin.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define MSD_TYPE_HOUSEKEEPING_PLUGIN (msd_housekeeping_plugin_get_type ()) -#define MSD_HOUSEKEEPING_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPlugin)) -#define MSD_HOUSEKEEPING_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginClass)) -#define MSD_IS_HOUSEKEEPING_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN)) -#define MSD_IS_HOUSEKEEPING_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_HOUSEKEEPING_PLUGIN)) -#define MSD_HOUSEKEEPING_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginClass)) - -typedef struct MsdHousekeepingPluginPrivate MsdHousekeepingPluginPrivate; - -typedef struct { - MateSettingsPlugin parent; - MsdHousekeepingPluginPrivate *priv; -} MsdHousekeepingPlugin; - -typedef struct { - MateSettingsPluginClass parent_class; -} MsdHousekeepingPluginClass; - -GType msd_housekeeping_plugin_get_type (void) G_GNUC_CONST; - -/* All the plugins must implement this function */ -G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); - -#ifdef __cplusplus -} -#endif - -#endif /* __MSD_HOUSEKEEPING_PLUGIN_H__ */ diff --git a/plugins/housekeeping/gsd-ldsm-dialog.c b/plugins/housekeeping/gsd-ldsm-dialog.c deleted file mode 100644 index 80ca857..0000000 --- a/plugins/housekeeping/gsd-ldsm-dialog.c +++ /dev/null @@ -1,476 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * msd-ldsm-dialog.c - * Copyright (C) Chris Coulson 2009 - * - * msd-ldsm-dialog.c is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * msd-ldsm-dialog.c is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include -#include - -#include "msd-ldsm-dialog.h" - -#define MATECONF_CLIENT_IGNORE_PATHS "/apps/mate_settings_daemon/plugins/housekeeping/ignore_paths" - -enum -{ - PROP_0, - PROP_OTHER_USABLE_PARTITIONS, - PROP_OTHER_PARTITIONS, - PROP_HAS_TRASH, - PROP_SPACE_REMAINING, - PROP_PARTITION_NAME, - PROP_MOUNT_PATH -}; - -struct MsdLdsmDialogPrivate -{ - GtkWidget *primary_label; - GtkWidget *secondary_label; - GtkWidget *ignore_check_button; - gboolean other_usable_partitions; - gboolean other_partitions; - gboolean has_trash; - gint64 space_remaining; - gchar *partition_name; - gchar *mount_path; -}; - -#define MSD_LDSM_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogPrivate)) - -static void msd_ldsm_dialog_class_init (MsdLdsmDialogClass *klass); -static void msd_ldsm_dialog_init (MsdLdsmDialog *dialog); - -G_DEFINE_TYPE (MsdLdsmDialog, msd_ldsm_dialog, GTK_TYPE_DIALOG); - -static const gchar* -msd_ldsm_dialog_get_checkbutton_text (MsdLdsmDialog *dialog) -{ - g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); - - if (dialog->priv->other_partitions) - return _("Don't show any warnings again for this file system"); - else - return _("Don't show any warnings again"); -} - -static gchar* -msd_ldsm_dialog_get_primary_text (MsdLdsmDialog *dialog) -{ - gchar *primary_text, *free_space; - - g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); - - free_space = g_format_size_for_display (dialog->priv->space_remaining); - - if (dialog->priv->other_partitions) { - primary_text = g_strdup_printf (_("The volume \"%s\" has only %s disk space remaining."), - dialog->priv->partition_name, free_space); - } else { - primary_text = g_strdup_printf (_("This computer has only %s disk space remaining."), - free_space); - } - - g_free (free_space); - - return primary_text; -} - -static const gchar* -msd_ldsm_dialog_get_secondary_text (MsdLdsmDialog *dialog) -{ - g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); - - if (dialog->priv->other_usable_partitions) { - if (dialog->priv->has_trash) { - return _("You can free up disk space by emptying the Trash, removing " \ - "unused programs or files, or moving files to another disk or partition."); - } else { - return _("You can free up disk space by removing unused programs or files, " \ - "or by moving files to another disk or partition."); - } - } else { - if (dialog->priv->has_trash) { - return _("You can free up disk space by emptying the Trash, removing unused " \ - "programs or files, or moving files to an external disk."); - } else { - return _("You can free up disk space by removing unused programs or files, " \ - "or by moving files to an external disk."); - } - } -} - -static gint -ignore_path_compare (gconstpointer a, - gconstpointer b) -{ - return g_strcmp0 ((const gchar *)a, (const gchar *)b); -} - -static gboolean -update_ignore_paths (GSList **ignore_paths, - const gchar *mount_path, - gboolean ignore) -{ - GSList *found; - gchar *path_to_remove; - - found = g_slist_find_custom (*ignore_paths, mount_path, (GCompareFunc) ignore_path_compare); - - if (ignore && (found == NULL)) { - *ignore_paths = g_slist_prepend (*ignore_paths, g_strdup (mount_path)); - return TRUE; - } - - if (!ignore && (found != NULL)) { - path_to_remove = found->data; - *ignore_paths = g_slist_remove (*ignore_paths, path_to_remove); - g_free (path_to_remove); - return TRUE; - } - - return FALSE; -} - -static void -ignore_check_button_toggled_cb (GtkToggleButton *button, - gpointer user_data) -{ - MsdLdsmDialog *dialog = (MsdLdsmDialog *)user_data; - MateConfClient *client; - GSList *ignore_paths; - GError *error = NULL; - gboolean ignore, ret, updated; - - client = mateconf_client_get_default (); - if (client != NULL) { - ignore_paths = mateconf_client_get_list (client, - MATECONF_CLIENT_IGNORE_PATHS, - MATECONF_VALUE_STRING, &error); - if (error != NULL) { - g_warning ("Cannot change ignore preference - failed to read existing configuration: %s", - error->message ? error->message : "Unkown error"); - g_clear_error (&error); - return; - } else { - ignore = gtk_toggle_button_get_active (button); - updated = update_ignore_paths (&ignore_paths, dialog->priv->mount_path, ignore); - } - - if (!updated) - return; - - ret = mateconf_client_set_list (client, - MATECONF_CLIENT_IGNORE_PATHS, - MATECONF_VALUE_STRING, - ignore_paths, &error); - if (!ret || error != NULL) { - g_warning ("Cannot change ignore preference - failed to commit changes: %s", - error->message ? error->message : "Unkown error"); - g_clear_error (&error); - } - - g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); - g_slist_free (ignore_paths); - g_object_unref (client); - } else { - g_warning ("Cannot change ignore preference - failed to get MateConfClient"); - } -} - -static void -msd_ldsm_dialog_init (MsdLdsmDialog *dialog) -{ - GtkWidget *main_vbox, *text_vbox, *hbox; - GtkWidget *image; - - dialog->priv = MSD_LDSM_DIALOG_GET_PRIVATE (dialog); - - main_vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); - - /* Set up all the window stuff here */ - gtk_window_set_title (GTK_WINDOW (dialog), _("Low Disk Space")); - gtk_window_set_icon_name (GTK_WINDOW (dialog), - GTK_STOCK_DIALOG_WARNING); - gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE); - gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER); - gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE); - gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE); - gtk_container_set_border_width (GTK_CONTAINER (dialog), 5); - - /* We don't want a separator - they're really ugly */ - gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE); - - /* Create the image */ - image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG); - gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0); - - /* Create the labels */ - dialog->priv->primary_label = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (dialog->priv->primary_label), TRUE); - gtk_label_set_single_line_mode (GTK_LABEL (dialog->priv->primary_label), FALSE); - gtk_misc_set_alignment (GTK_MISC (dialog->priv->primary_label), 0.0, 0.0); - - dialog->priv->secondary_label = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (dialog->priv->secondary_label), TRUE); - gtk_label_set_single_line_mode (GTK_LABEL (dialog->priv->secondary_label), FALSE); - gtk_misc_set_alignment (GTK_MISC (dialog->priv->secondary_label), 0.0, 0.0); - - /* Create the check button to ignore future warnings */ - dialog->priv->ignore_check_button = gtk_check_button_new (); - /* The button should be inactive if the dialog was just called. - * I suppose it could be possible for the user to manually edit the MateConf key between - * the mount being checked and the dialog appearing, but I don't think it matters - * too much */ - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->ignore_check_button), FALSE); - g_signal_connect (dialog->priv->ignore_check_button, "toggled", - G_CALLBACK (ignore_check_button_toggled_cb), dialog); - - /* Now set up the dialog's GtkBox's' */ - gtk_box_set_spacing (GTK_BOX (main_vbox), 14); - - hbox = gtk_hbox_new (FALSE, 12); - gtk_container_set_border_width (GTK_CONTAINER (hbox), 5); - - text_vbox = gtk_vbox_new (FALSE, 12); - - gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->primary_label, FALSE, FALSE, 0); - gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->secondary_label, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->ignore_check_button, FALSE, FALSE, 0); - gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0); - gtk_box_pack_start (GTK_BOX (hbox), text_vbox, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0); - - /* Set up the action area */ - gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), 6); - gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), 5); - - gtk_widget_show_all (hbox); -} - -static void -msd_ldsm_dialog_finalize (GObject *object) -{ - MsdLdsmDialog *self; - - g_return_if_fail (object != NULL); - g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); - - self = MSD_LDSM_DIALOG (object); - - if (self->priv->partition_name) - g_free (self->priv->partition_name); - - if (self->priv->mount_path) - g_free (self->priv->mount_path); - - G_OBJECT_CLASS (msd_ldsm_dialog_parent_class)->finalize (object); -} - -static void -msd_ldsm_dialog_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) -{ - MsdLdsmDialog *self; - - g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); - - self = MSD_LDSM_DIALOG (object); - - switch (prop_id) - { - case PROP_OTHER_USABLE_PARTITIONS: - self->priv->other_usable_partitions = g_value_get_boolean (value); - break; - case PROP_OTHER_PARTITIONS: - self->priv->other_partitions = g_value_get_boolean (value); - break; - case PROP_HAS_TRASH: - self->priv->has_trash = g_value_get_boolean (value); - break; - case PROP_SPACE_REMAINING: - self->priv->space_remaining = g_value_get_int64 (value); - break; - case PROP_PARTITION_NAME: - self->priv->partition_name = g_value_dup_string (value); - break; - case PROP_MOUNT_PATH: - self->priv->mount_path = g_value_dup_string (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -msd_ldsm_dialog_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) -{ - MsdLdsmDialog *self; - - g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); - - self = MSD_LDSM_DIALOG (object); - - switch (prop_id) - { - case PROP_OTHER_USABLE_PARTITIONS: - g_value_set_boolean (value, self->priv->other_usable_partitions); - break; - case PROP_OTHER_PARTITIONS: - g_value_set_boolean (value, self->priv->other_partitions); - break; - case PROP_HAS_TRASH: - g_value_set_boolean (value, self->priv->has_trash); - break; - case PROP_SPACE_REMAINING: - g_value_set_int64 (value, self->priv->space_remaining); - break; - case PROP_PARTITION_NAME: - g_value_set_string (value, self->priv->partition_name); - break; - case PROP_MOUNT_PATH: - g_value_set_string (value, self->priv->mount_path); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -msd_ldsm_dialog_class_init (MsdLdsmDialogClass *klass) -{ - GObjectClass* object_class = G_OBJECT_CLASS (klass); - - object_class->finalize = msd_ldsm_dialog_finalize; - object_class->set_property = msd_ldsm_dialog_set_property; - object_class->get_property = msd_ldsm_dialog_get_property; - - g_object_class_install_property (object_class, - PROP_OTHER_USABLE_PARTITIONS, - g_param_spec_boolean ("other-usable-partitions", - "other-usable-partitions", - "Set to TRUE if there are other usable partitions on the system", - FALSE, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_object_class_install_property (object_class, - PROP_OTHER_PARTITIONS, - g_param_spec_boolean ("other-partitions", - "other-partitions", - "Set to TRUE if there are other partitions on the system", - FALSE, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_object_class_install_property (object_class, - PROP_HAS_TRASH, - g_param_spec_boolean ("has-trash", - "has-trash", - "Set to TRUE if the partition has files in it's trash folder that can be deleted", - FALSE, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_object_class_install_property (object_class, - PROP_SPACE_REMAINING, - g_param_spec_int64 ("space-remaining", - "space-remaining", - "Specify how much space is remaining in bytes", - G_MININT64, G_MAXINT64, 0, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_object_class_install_property (object_class, - PROP_PARTITION_NAME, - g_param_spec_string ("partition-name", - "partition-name", - "Specify the name of the partition", - "Unknown", - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_object_class_install_property (object_class, - PROP_MOUNT_PATH, - g_param_spec_string ("mount-path", - "mount-path", - "Specify the mount path for the partition", - "Unknown", - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - - g_type_class_add_private (klass, sizeof (MsdLdsmDialogPrivate)); -} - -MsdLdsmDialog* -msd_ldsm_dialog_new (gboolean other_usable_partitions, - gboolean other_partitions, - gboolean display_baobab, - gboolean display_empty_trash, - gint64 space_remaining, - const gchar *partition_name, - const gchar *mount_path) -{ - MsdLdsmDialog *dialog; - GtkWidget *button_empty_trash, *button_ignore, *button_analyze; - GtkWidget *empty_trash_image, *analyze_image, *ignore_image; - gchar *primary_text, *primary_text_markup; - const gchar *secondary_text, *checkbutton_text; - - dialog = MSD_LDSM_DIALOG (g_object_new (MSD_TYPE_LDSM_DIALOG, - "other-usable-partitions", other_usable_partitions, - "other-partitions", other_partitions, - "has-trash", display_empty_trash, - "space-remaining", space_remaining, - "partition-name", partition_name, - "mount-path", mount_path, - NULL)); - - /* Add some buttons */ - if (dialog->priv->has_trash) { - button_empty_trash = gtk_dialog_add_button (GTK_DIALOG (dialog), - _("Empty Trash"), - MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH); - empty_trash_image = gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_BUTTON); - gtk_button_set_image (GTK_BUTTON (button_empty_trash), empty_trash_image); - } - - if (display_baobab) { - button_analyze = gtk_dialog_add_button (GTK_DIALOG (dialog), - _("Examine…"), - MSD_LDSM_DIALOG_RESPONSE_ANALYZE); - analyze_image = gtk_image_new_from_icon_name ("baobab", GTK_ICON_SIZE_BUTTON); - gtk_button_set_image (GTK_BUTTON (button_analyze), analyze_image); - } - - button_ignore = gtk_dialog_add_button (GTK_DIALOG (dialog), - _("Ignore"), - GTK_RESPONSE_CANCEL); - ignore_image = gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON); - gtk_button_set_image (GTK_BUTTON (button_ignore), ignore_image); - - gtk_widget_grab_default (button_ignore); - - /* Set the label text */ - primary_text = msd_ldsm_dialog_get_primary_text (dialog); - primary_text_markup = g_markup_printf_escaped ("%s", primary_text); - gtk_label_set_markup (GTK_LABEL (dialog->priv->primary_label), primary_text_markup); - - secondary_text = msd_ldsm_dialog_get_secondary_text (dialog); - gtk_label_set_text (GTK_LABEL (dialog->priv->secondary_label), secondary_text); - - checkbutton_text = msd_ldsm_dialog_get_checkbutton_text (dialog); - gtk_button_set_label (GTK_BUTTON (dialog->priv->ignore_check_button), checkbutton_text); - - g_free (primary_text); - g_free (primary_text_markup); - - return dialog; -} diff --git a/plugins/housekeeping/gsd-ldsm-dialog.h b/plugins/housekeeping/gsd-ldsm-dialog.h deleted file mode 100644 index 8b95fb9..0000000 --- a/plugins/housekeeping/gsd-ldsm-dialog.h +++ /dev/null @@ -1,72 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * msd-ldsm-dialog.c - * Copyright (C) Chris Coulson 2009 - * - * msd-ldsm-dialog.c is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * msd-ldsm-dialog.c is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef _MSD_LDSM_DIALOG_H_ -#define _MSD_LDSM_DIALOG_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define MSD_TYPE_LDSM_DIALOG (msd_ldsm_dialog_get_type ()) -#define MSD_LDSM_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialog)) -#define MSD_LDSM_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogClass)) -#define MSD_IS_LDSM_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MSD_TYPE_LDSM_DIALOG)) -#define MSD_IS_LDSM_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MSD_TYPE_LDSM_DIALOG)) -#define MSD_LDSM_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogClass)) - -enum -{ - MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH = -20, - MSD_LDSM_DIALOG_RESPONSE_ANALYZE = -21 -}; - -typedef struct MsdLdsmDialogPrivate MsdLdsmDialogPrivate; -typedef struct _MsdLdsmDialogClass MsdLdsmDialogClass; -typedef struct _MsdLdsmDialog MsdLdsmDialog; - -struct _MsdLdsmDialogClass -{ - GtkDialogClass parent_class; -}; - -struct _MsdLdsmDialog -{ - GtkDialog parent_instance; - MsdLdsmDialogPrivate *priv; -}; - -GType msd_ldsm_dialog_get_type (void) G_GNUC_CONST; - -MsdLdsmDialog * msd_ldsm_dialog_new (gboolean other_usable_partitions, - gboolean other_partitions, - gboolean display_baobab, - gboolean display_empty_trash, - gint64 space_remaining, - const gchar *partition_name, - const gchar *mount_path); - -#ifdef __cplusplus -} -#endif - -#endif /* _MSD_LDSM_DIALOG_H_ */ diff --git a/plugins/housekeeping/gsd-ldsm-trash-empty.c b/plugins/housekeeping/gsd-ldsm-trash-empty.c deleted file mode 100644 index 05f82cd..0000000 --- a/plugins/housekeeping/gsd-ldsm-trash-empty.c +++ /dev/null @@ -1,398 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * msd-ldsm-trash-empty.c - * Copyright (C) Chris Coulson 2009 - * (C) Ryan Lortie 2008 - * - * msd-ldsm-trash-empty.c is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * msd-ldsm-trash-empty.c is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include -#include -#include - -#include "msd-ldsm-trash-empty.h" - -#define CAJA_CONFIRM_TRASH_KEY "/apps/caja/preferences/confirm_trash" - -/* Some of this code has been borrowed from the trash-applet, courtesy of Ryan Lortie */ - -static GtkWidget *trash_empty_confirm_dialog = NULL; -static GtkWidget *trash_empty_dialog = NULL; -static GtkWidget *location_label; -static GtkWidget *file_label; -static GtkWidget *progressbar; - -static gsize trash_empty_total_files; -static gboolean trash_empty_update_pending = FALSE; -static GFile *trash_empty_current_file = NULL; -static gsize trash_empty_deleted_files; -static GTimer *timer = NULL; -static gboolean trash_empty_actually_deleting; - -static gboolean -trash_empty_done (gpointer data) -{ - gtk_widget_destroy (trash_empty_dialog); - trash_empty_dialog = NULL; - if (timer) { - g_timer_destroy (timer); - timer = NULL; - } - - return FALSE; -} - -static gboolean -trash_empty_update_dialog (gpointer user_data) -{ - gsize deleted, total; - GFile *file; - gboolean actually_deleting; - - g_assert (trash_empty_update_pending); - - deleted = trash_empty_deleted_files; - total = trash_empty_total_files; - file = trash_empty_current_file; - actually_deleting = trash_empty_actually_deleting; - - /* maybe the done() got processed first. */ - if (!trash_empty_dialog) - goto out; - - if (!actually_deleting) { - /* If we havent finished counting yet, then pulse the progressbar every 100ms. - * This stops the user from thinking the dialog has frozen if there are - * a lot of files to delete. We don't pulse it every time we are called from the - * worker thread, otherwise it moves to fast and looks hideous - */ - if (timer) { - if (g_timer_elapsed (timer, NULL) > 0.1) { - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progressbar)); - g_timer_start (timer); - } - } else { - timer = g_timer_new (); - g_timer_start (timer); - gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progressbar)); - } - } else { - gchar *text; - gchar *tmp; - gchar *markup; - GFile *parent; - - text = g_strdup_printf (_("Removing item %lu of %lu"), - deleted, total); - gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar), text); - - g_free (text); - - if (deleted > total) - gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progressbar), 1.0); - else - gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progressbar), - (gdouble) deleted / (gdouble) total); - - parent = g_file_get_parent (file); - text = g_file_get_uri (parent); - g_object_unref (parent); - - gtk_label_set_text (GTK_LABEL (location_label), text); - g_free (text); - - tmp = g_file_get_basename (file); - text = g_markup_printf_escaped (_("Removing: %s"), tmp); - markup = g_strdup_printf ("%s", text); - gtk_label_set_markup (GTK_LABEL (file_label), text); - g_free (markup); - g_free (text); - g_free (tmp); - - /* unhide the labels */ - gtk_widget_show_all (GTK_WIDGET (trash_empty_dialog)); - } - -out: - trash_empty_current_file = NULL; - g_object_unref (file); - - trash_empty_update_pending = FALSE; - - return FALSE; -} - -/* Worker thread begin */ - -static void -trash_empty_maybe_schedule_update (GIOSchedulerJob *job, - GFile *file, - gsize deleted, - gboolean actually_deleting) -{ - if (!trash_empty_update_pending) { - g_assert (trash_empty_current_file == NULL); - - trash_empty_current_file = g_object_ref (file); - trash_empty_deleted_files = deleted; - trash_empty_actually_deleting = actually_deleting; - - trash_empty_update_pending = TRUE; - g_io_scheduler_job_send_to_mainloop_async (job, - trash_empty_update_dialog, - NULL, NULL); - } -} - -static void -trash_empty_delete_contents (GIOSchedulerJob *job, - GCancellable *cancellable, - GFile *file, - gboolean actually_delete, - gsize *deleted) -{ - GFileEnumerator *enumerator; - GFileInfo *info; - GFile *child; - - if (g_cancellable_is_cancelled (cancellable)) - return; - - enumerator = g_file_enumerate_children (file, - G_FILE_ATTRIBUTE_STANDARD_NAME "," - G_FILE_ATTRIBUTE_STANDARD_TYPE, - G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, - cancellable, NULL); - - if (enumerator) { - while ((info = g_file_enumerator_next_file (enumerator, - cancellable, NULL)) != NULL) { - child = g_file_get_child (file, g_file_info_get_name (info)); - - if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) - trash_empty_delete_contents (job, cancellable, child, - actually_delete, deleted); - - trash_empty_maybe_schedule_update (job, child, *deleted, actually_delete); - if (actually_delete) - g_file_delete (child, cancellable, NULL); - - (*deleted)++; - - g_object_unref (child); - g_object_unref (info); - - if (g_cancellable_is_cancelled (cancellable)) - break; - } - - g_object_unref (enumerator); - } -} - -static gboolean -trash_empty_job (GIOSchedulerJob *job, - GCancellable *cancellable, - gpointer user_data) -{ - gsize deleted; - GFile *trash; - - trash = g_file_new_for_uri ("trash:///"); - - /* first do a dry run to count the number of files */ - deleted = 0; - trash_empty_delete_contents (job, cancellable, trash, FALSE, &deleted); - trash_empty_total_files = deleted; - - /* now do the real thing */ - deleted = 0; - trash_empty_delete_contents (job, cancellable, trash, TRUE, &deleted); - - /* done */ - g_object_unref (trash); - g_io_scheduler_job_send_to_mainloop_async (job, - trash_empty_done, - NULL, NULL); - - return FALSE; -} - -/* Worker thread end */ - -static void -trash_empty_start () -{ - GtkWidget *vbox1, *vbox2, *hbox; - GtkWidget *label1, *label3; - gchar *markup; - GCancellable *cancellable; - - trash_empty_dialog = gtk_dialog_new (); - gtk_window_set_default_size (GTK_WINDOW (trash_empty_dialog), 400, -1); - gtk_window_set_icon_name (GTK_WINDOW (trash_empty_dialog), "user-trash"); - gtk_window_set_title (GTK_WINDOW (trash_empty_dialog), - _("Emptying the trash")); - - vbox1 = gtk_vbox_new (FALSE, 12); - vbox2 = gtk_vbox_new (FALSE, 0); - hbox = gtk_hbox_new (FALSE, 0); - - label1 = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (label1), TRUE); - gtk_misc_set_alignment (GTK_MISC (label1), 0.0, 0.5); - - label3 = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (label3), TRUE); - gtk_misc_set_alignment (GTK_MISC (label3), 0.0, 0.5); - gtk_widget_hide (label3); - - location_label = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (location_label), TRUE); - gtk_misc_set_alignment (GTK_MISC (location_label), 0.0, 0.5); - - file_label = gtk_label_new (NULL); - gtk_label_set_line_wrap (GTK_LABEL (file_label), TRUE); - gtk_misc_set_alignment (GTK_MISC (file_label), 0.0, 0.5); - - progressbar = gtk_progress_bar_new (); - gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (progressbar), 0.1); - gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar), _("Preparing to empty trash…")); - - gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (trash_empty_dialog))), vbox1, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (vbox1), label1, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (hbox), label3, FALSE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (hbox), location_label, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (vbox1), hbox, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (vbox2), progressbar, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (vbox2), file_label, TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (vbox1), vbox2, TRUE, TRUE, 0); - - gtk_widget_show (label1); - gtk_widget_show (vbox1); - gtk_widget_show_all (vbox2); - gtk_widget_show (hbox); - gtk_widget_show (location_label); - - gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (trash_empty_dialog))), 6); - gtk_container_set_border_width (GTK_CONTAINER (vbox1), 6); - - gtk_dialog_add_button (GTK_DIALOG (trash_empty_dialog), - GTK_STOCK_CANCEL, - GTK_RESPONSE_CANCEL); - - markup = g_markup_printf_escaped ("%s", _("Emptying the trash")); - gtk_label_set_markup (GTK_LABEL (label1), markup); - /* Translators: "Emptying trash from " */ - gtk_label_set_text (GTK_LABEL (label3), _("From: ")); - - cancellable = g_cancellable_new (); - g_signal_connect_object (trash_empty_dialog, "response", - G_CALLBACK (g_cancellable_cancel), - cancellable, G_CONNECT_SWAPPED); - g_io_scheduler_push_job (trash_empty_job, NULL, NULL, 0, cancellable); - - gtk_widget_show (trash_empty_dialog); - - g_free (markup); - g_object_unref (cancellable); -} - -static void -trash_empty_confirmation_response (GtkDialog *dialog, - gint response_id, - gpointer user_data) -{ - if (response_id == GTK_RESPONSE_YES) - trash_empty_start (); - - gtk_object_destroy (GTK_OBJECT (dialog)); - trash_empty_confirm_dialog = NULL; -} - -static gboolean -trash_empty_require_confirmation () -{ - MateConfClient *client; - gboolean require_confirmation = TRUE; - GError *error = NULL; - - client = mateconf_client_get_default (); - if (client) { - require_confirmation = mateconf_client_get_bool (client, CAJA_CONFIRM_TRASH_KEY, &error); - if (error) { - g_warning ("Failed to read confirm_trash key from MateConf: %s", error->message ? error->message : "Unknown error"); - /* It's safest to assume that confirmation is required here */ - require_confirmation = TRUE; - g_error_free (error); - } - g_object_unref (client); - } - - return require_confirmation; -} - -static void -trash_empty_show_confirmation_dialog () -{ - GtkWidget *button; - - if (!trash_empty_require_confirmation ()) { - trash_empty_start (); - return; - } - - trash_empty_confirm_dialog = gtk_message_dialog_new (NULL, 0, - GTK_MESSAGE_WARNING, - GTK_BUTTONS_NONE, - _("Empty all of the items from the trash?")); - - gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (trash_empty_confirm_dialog), - _("If you choose to empty the trash, all items in " - "it will be permanently lost. Please note that " - "you can also delete them separately.")); - - gtk_dialog_add_button (GTK_DIALOG (trash_empty_confirm_dialog), GTK_STOCK_CANCEL, - GTK_RESPONSE_CANCEL); - - button = gtk_button_new_with_mnemonic (_("_Empty Trash")); - gtk_widget_show (button); - gtk_widget_set_can_default (button, TRUE); - - gtk_dialog_add_action_widget (GTK_DIALOG (trash_empty_confirm_dialog), - button, GTK_RESPONSE_YES); - - gtk_dialog_set_default_response (GTK_DIALOG (trash_empty_confirm_dialog), - GTK_RESPONSE_YES); - - gtk_window_set_icon_name (GTK_WINDOW (trash_empty_confirm_dialog), - "user-trash"); - - gtk_widget_show (trash_empty_confirm_dialog); - - g_signal_connect (trash_empty_confirm_dialog, "response", - G_CALLBACK (trash_empty_confirmation_response), NULL); -} - -void -msd_ldsm_trash_empty () -{ - if (trash_empty_confirm_dialog) - gtk_window_present (GTK_WINDOW (trash_empty_confirm_dialog)); - else if (trash_empty_dialog) - gtk_window_present (GTK_WINDOW (trash_empty_dialog)); - else - trash_empty_show_confirmation_dialog (); -} diff --git a/plugins/housekeeping/gsd-ldsm-trash-empty.h b/plugins/housekeeping/gsd-ldsm-trash-empty.h deleted file mode 100644 index 85b09c0..0000000 --- a/plugins/housekeeping/gsd-ldsm-trash-empty.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * msd-ldsm-trash-empty.h - * Copyright (C) Chris Coulson 2009 - * - * msd-ldsm-trash-empty.h is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * msd-ldsm-trash-empty.h is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef _msd_ldsm_trash_empty_h_ -#define _msd_ldsm_trash_empty_h_ - -#include - -void msd_ldsm_trash_empty (); - -#endif /* _msd_ldsm_trash_empty_h_ */ diff --git a/plugins/housekeeping/msd-disk-space.c b/plugins/housekeeping/msd-disk-space.c new file mode 100644 index 0000000..6842ae5 --- /dev/null +++ b/plugins/housekeeping/msd-disk-space.c @@ -0,0 +1,733 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * vim: set et sw=8 ts=8: + * + * Copyright (c) 2008, Novell, Inc. + * + * Authors: Vincent Untz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +/* gcc -DHAVE_LIBMATENOTIFY -DTEST -Wall `pkg-config --cflags --libs gobject-2.0 gio-unix-2.0 glib-2.0 gtk+-2.0 libmatenotify` -o msd-disk-space-test msd-disk-space.c */ + +#include "config.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "msd-disk-space.h" +#include "msd-ldsm-dialog.h" +#include "msd-ldsm-trash-empty.h" + + +#define GIGABYTE 1024 * 1024 * 1024 + +#define CHECK_EVERY_X_SECONDS 60 + +#define DISK_SPACE_ANALYZER "baobab" + +#define MATECONF_HOUSEKEEPING_DIR "/apps/mate_settings_daemon/plugins/housekeeping" +#define MATECONF_FREE_PC_NOTIFY_KEY "free_percent_notify" +#define MATECONF_FREE_PC_NOTIFY_AGAIN_KEY "free_percent_notify_again" +#define MATECONF_FREE_SIZE_NO_NOTIFY "free_size_gb_no_notify" +#define MATECONF_MIN_NOTIFY_PERIOD "min_notify_period" +#define MATECONF_IGNORE_PATHS "ignore_paths" + +typedef struct +{ + GUnixMountEntry *mount; + struct statvfs buf; + time_t notify_time; +} LdsmMountInfo; + +static GHashTable *ldsm_notified_hash = NULL; +static unsigned int ldsm_timeout_id = 0; +static GUnixMountMonitor *ldsm_monitor = NULL; +static double free_percent_notify = 0.05; +static double free_percent_notify_again = 0.01; +static unsigned int free_size_gb_no_notify = 2; +static unsigned int min_notify_period = 10; +static GSList *ignore_paths = NULL; +static unsigned int mateconf_notify_id; +static MateConfClient *client = NULL; +static MsdLdsmDialog *dialog = NULL; +static guint64 *time_read; + +static gchar* +ldsm_get_fs_id_for_path (const gchar *path) +{ + GFile *file; + GFileInfo *fileinfo; + gchar *attr_id_fs; + + file = g_file_new_for_path (path); + fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_ID_FILESYSTEM, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL); + if (fileinfo) { + attr_id_fs = g_strdup (g_file_info_get_attribute_string (fileinfo, G_FILE_ATTRIBUTE_ID_FILESYSTEM)); + g_object_unref (fileinfo); + } else { + attr_id_fs = NULL; + } + + g_object_unref (file); + + return attr_id_fs; +} + +static gboolean +ldsm_mount_has_trash (LdsmMountInfo *mount) +{ + const gchar *user_data_dir; + gchar *user_data_attr_id_fs; + gchar *path_attr_id_fs; + gboolean mount_uses_user_trash = FALSE; + gchar *trash_files_dir; + gboolean has_trash = FALSE; + GDir *dir; + const gchar *path; + + user_data_dir = g_get_user_data_dir (); + user_data_attr_id_fs = ldsm_get_fs_id_for_path (user_data_dir); + + path = g_unix_mount_get_mount_path (mount->mount); + path_attr_id_fs = ldsm_get_fs_id_for_path (path); + + if (g_strcmp0 (user_data_attr_id_fs, path_attr_id_fs) == 0) { + /* The volume that is low on space is on the same volume as our home + * directory. This means the trash is at $XDG_DATA_HOME/Trash, + * not at the root of the volume which is full. + */ + mount_uses_user_trash = TRUE; + } + + g_free (user_data_attr_id_fs); + g_free (path_attr_id_fs); + + /* I can't think of a better way to find out if a volume has any trash. Any suggestions? */ + if (mount_uses_user_trash) { + trash_files_dir = g_build_filename (g_get_user_data_dir (), "Trash", "files", NULL); + } else { + gchar *uid; + + uid = g_strdup_printf ("%d", getuid ()); + trash_files_dir = g_build_filename (path, ".Trash", uid, "files", NULL); + if (!g_file_test (trash_files_dir, G_FILE_TEST_IS_DIR)) { + gchar *trash_dir; + + g_free (trash_files_dir); + trash_dir = g_strdup_printf (".Trash-%s", uid); + trash_files_dir = g_build_filename (path, trash_dir, "files", NULL); + g_free (trash_dir); + if (!g_file_test (trash_files_dir, G_FILE_TEST_IS_DIR)) { + g_free (trash_files_dir); + g_free (uid); + return has_trash; + } + } + g_free (uid); + } + + dir = g_dir_open (trash_files_dir, 0, NULL); + if (dir) { + if (g_dir_read_name (dir)) + has_trash = TRUE; + g_dir_close (dir); + } + + g_free (trash_files_dir); + + return has_trash; +} + +static void +ldsm_analyze_path (const gchar *path) +{ + const gchar *argv[] = { DISK_SPACE_ANALYZER, path, NULL }; + + g_spawn_async (NULL, (gchar **) argv, NULL, G_SPAWN_SEARCH_PATH, + NULL, NULL, NULL, NULL); +} + +static gboolean +ldsm_notify_for_mount (LdsmMountInfo *mount, + gboolean multiple_volumes, + gboolean other_usable_volumes) +{ + gchar *name, *program; + gint64 free_space; + gint response; + gboolean has_trash; + gboolean has_disk_analyzer; + gboolean retval = TRUE; + const gchar *path; + + /* Don't show a dialog if one is already displayed */ + if (dialog) + return retval; + + name = g_unix_mount_guess_name (mount->mount); + free_space = (gint64) mount->buf.f_frsize * (gint64) mount->buf.f_bavail; + has_trash = ldsm_mount_has_trash (mount); + path = g_unix_mount_get_mount_path (mount->mount); + + program = g_find_program_in_path (DISK_SPACE_ANALYZER); + has_disk_analyzer = (program != NULL); + g_free (program); + + dialog = msd_ldsm_dialog_new (other_usable_volumes, + multiple_volumes, + has_disk_analyzer, + has_trash, + free_space, + name, + path); + + g_free (name); + + g_object_ref (G_OBJECT (dialog)); + response = gtk_dialog_run (GTK_DIALOG (dialog)); + + gtk_object_destroy (GTK_OBJECT (dialog)); + dialog = NULL; + + switch (response) { + case GTK_RESPONSE_CANCEL: + retval = FALSE; + break; + case MSD_LDSM_DIALOG_RESPONSE_ANALYZE: + retval = FALSE; + ldsm_analyze_path (g_unix_mount_get_mount_path (mount->mount)); + break; + case MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH: + retval = TRUE; + msd_ldsm_trash_empty (); + break; + case GTK_RESPONSE_NONE: + case GTK_RESPONSE_DELETE_EVENT: + retval = TRUE; + break; + default: + g_assert_not_reached (); + } + + return retval; +} + +static gboolean +ldsm_mount_has_space (LdsmMountInfo *mount) +{ + gdouble free_space; + + free_space = (double) mount->buf.f_bavail / (double) mount->buf.f_blocks; + /* enough free space, nothing to do */ + if (free_space > free_percent_notify) + return TRUE; + + if (((gint64) mount->buf.f_frsize * (gint64) mount->buf.f_bavail) > ((gint64) free_size_gb_no_notify * GIGABYTE)) + return TRUE; + + /* If we got here, then this volume is low on space */ + return FALSE; +} + +static gboolean +ldsm_mount_is_virtual (LdsmMountInfo *mount) +{ + if (mount->buf.f_blocks == 0) { + /* Filesystems with zero blocks are virtual */ + return TRUE; + } + + return FALSE; +} + +static gint +ldsm_ignore_path_compare (gconstpointer a, + gconstpointer b) +{ + return g_strcmp0 ((const gchar *)a, (const gchar *)b); +} + +static gboolean +ldsm_mount_is_user_ignore (const gchar *path) +{ + if (g_slist_find_custom (ignore_paths, path, (GCompareFunc) ldsm_ignore_path_compare) != NULL) + return TRUE; + else + return FALSE; +} + + +static gboolean +is_in (const gchar *value, const gchar *set[]) +{ + int i; + for (i = 0; set[i] != NULL; i++) + { + if (strcmp (set[i], value) == 0) + return TRUE; + } + return FALSE; +} + +static gboolean +ldsm_mount_should_ignore (GUnixMountEntry *mount) +{ + const gchar *fs, *device, *path; + + path = g_unix_mount_get_mount_path (mount); + if (ldsm_mount_is_user_ignore (path)) + return TRUE; + + /* This is borrowed from GLib and used as a way to determine + * which mounts we should ignore by default. GLib doesn't + * expose this in a way that allows it to be used for this + * purpose + */ + + const gchar *ignore_fs[] = { + "auto", + "autofs", + "devfs", + "devpts", + "ecryptfs", + "kernfs", + "linprocfs", + "proc", + "procfs", + "ptyfs", + "selinuxfs", + "linsysfs", + "sysfs", + "tmpfs", + "usbfs", + "nfsd", + "rpc_pipefs", + "zfs", + NULL + }; + const gchar *ignore_devices[] = { + "none", + "sunrpc", + "devpts", + "nfsd", + "/dev/loop", + "/dev/vn", + NULL + }; + + fs = g_unix_mount_get_fs_type (mount); + device = g_unix_mount_get_device_path (mount); + + if (is_in (fs, ignore_fs)) + return TRUE; + + if (is_in (device, ignore_devices)) + return TRUE; + + return FALSE; +} + +static void +ldsm_free_mount_info (gpointer data) +{ + LdsmMountInfo *mount = data; + + g_return_if_fail (mount != NULL); + + g_unix_mount_free (mount->mount); + g_free (mount); +} + +static void +ldsm_maybe_warn_mounts (GList *mounts, + gboolean multiple_volumes, + gboolean other_usable_volumes) +{ + GList *l; + gboolean done = FALSE; + + for (l = mounts; l != NULL; l = l->next) { + LdsmMountInfo *mount_info = l->data; + LdsmMountInfo *previous_mount_info; + gdouble free_space; + gdouble previous_free_space; + time_t curr_time; + const gchar *path; + gboolean show_notify; + + if (done) { + /* Don't show any more dialogs if the user took action with the last one. The user action + * might free up space on multiple volumes, making the next dialog redundant. + */ + ldsm_free_mount_info (mount_info); + continue; + } + + path = g_unix_mount_get_mount_path (mount_info->mount); + + previous_mount_info = g_hash_table_lookup (ldsm_notified_hash, path); + if (previous_mount_info != NULL) + previous_free_space = (gdouble) previous_mount_info->buf.f_bavail / (gdouble) previous_mount_info->buf.f_blocks; + + free_space = (gdouble) mount_info->buf.f_bavail / (gdouble) mount_info->buf.f_blocks; + + if (previous_mount_info == NULL) { + /* We haven't notified for this mount yet */ + show_notify = TRUE; + mount_info->notify_time = time (NULL); + g_hash_table_replace (ldsm_notified_hash, g_strdup (path), mount_info); + } else if ((previous_free_space - free_space) > free_percent_notify_again) { + /* We've notified for this mount before and free space has decreased sufficiently since last time to notify again */ + curr_time = time (NULL); + if (difftime (curr_time, previous_mount_info->notify_time) > (gdouble)(min_notify_period * 60)) { + show_notify = TRUE; + mount_info->notify_time = curr_time; + } else { + /* It's too soon to show the dialog again. However, we still replace the LdsmMountInfo + * struct in the hash table, but give it the notfiy time from the previous dialog. + * This will stop the notification from reappearing unnecessarily as soon as the timeout expires. + */ + show_notify = FALSE; + mount_info->notify_time = previous_mount_info->notify_time; + } + g_hash_table_replace (ldsm_notified_hash, g_strdup (path), mount_info); + } else { + /* We've notified for this mount before, but the free space hasn't decreased sufficiently to notify again */ + ldsm_free_mount_info (mount_info); + show_notify = FALSE; + } + + if (show_notify) { + if (ldsm_notify_for_mount (mount_info, multiple_volumes, other_usable_volumes)) + done = TRUE; + } + } +} + +static gboolean +ldsm_check_all_mounts (gpointer data) +{ + GList *mounts; + GList *l; + GList *check_mounts = NULL; + GList *full_mounts = NULL; + guint number_of_mounts; + guint number_of_full_mounts; + gboolean multiple_volumes = FALSE; + gboolean other_usable_volumes = FALSE; + + /* We iterate through the static mounts in /etc/fstab first, seeing if + * they're mounted by checking if the GUnixMountPoint has a corresponding GUnixMountEntry. + * Iterating through the static mounts means we automatically ignore dynamically mounted media. + */ + mounts = g_unix_mount_points_get (time_read); + + for (l = mounts; l != NULL; l = l->next) { + GUnixMountPoint *mount_point = l->data; + GUnixMountEntry *mount; + LdsmMountInfo *mount_info; + const gchar *path; + + path = g_unix_mount_point_get_mount_path (mount_point); + mount = g_unix_mount_at (path, time_read); + g_unix_mount_point_free (mount_point); + if (mount == NULL) { + /* The GUnixMountPoint is not mounted */ + continue; + } + + mount_info = g_new0 (LdsmMountInfo, 1); + mount_info->mount = mount; + + path = g_unix_mount_get_mount_path (mount); + + if (g_unix_mount_is_readonly (mount)) { + ldsm_free_mount_info (mount_info); + continue; + } + + if (ldsm_mount_should_ignore (mount)) { + ldsm_free_mount_info (mount_info); + continue; + } + + if (statvfs (path, &mount_info->buf) != 0) { + ldsm_free_mount_info (mount_info); + continue; + } + + if (ldsm_mount_is_virtual (mount_info)) { + ldsm_free_mount_info (mount_info); + continue; + } + + check_mounts = g_list_prepend (check_mounts, mount_info); + } + + number_of_mounts = g_list_length (check_mounts); + if (number_of_mounts > 1) + multiple_volumes = TRUE; + + for (l = check_mounts; l != NULL; l = l->next) { + LdsmMountInfo *mount_info = l->data; + + if (!ldsm_mount_has_space (mount_info)) { + full_mounts = g_list_prepend (full_mounts, mount_info); + } else { + g_hash_table_remove (ldsm_notified_hash, g_unix_mount_get_mount_path (mount_info->mount)); + ldsm_free_mount_info (mount_info); + } + } + + number_of_full_mounts = g_list_length (full_mounts); + if (number_of_mounts > number_of_full_mounts) + other_usable_volumes = TRUE; + + ldsm_maybe_warn_mounts (full_mounts, multiple_volumes, + other_usable_volumes); + + g_list_free (check_mounts); + g_list_free (full_mounts); + + return TRUE; +} + +static gboolean +ldsm_is_hash_item_not_in_mounts (gpointer key, + gpointer value, + gpointer user_data) +{ + GList *l; + + for (l = (GList *) user_data; l != NULL; l = l->next) { + GUnixMountEntry *mount = l->data; + const char *path; + + path = g_unix_mount_get_mount_path (mount); + + if (strcmp (path, key) == 0) + return FALSE; + } + + return TRUE; +} + +static void +ldsm_mounts_changed (GObject *monitor, + gpointer data) +{ + GList *mounts; + + /* remove the saved data for mounts that got removed */ + mounts = g_unix_mounts_get (time_read); + g_hash_table_foreach_remove (ldsm_notified_hash, + ldsm_is_hash_item_not_in_mounts, mounts); + g_list_foreach (mounts, (GFunc) g_unix_mount_free, NULL); + + /* check the status now, for the new mounts */ + ldsm_check_all_mounts (NULL); + + /* and reset the timeout */ + if (ldsm_timeout_id) + g_source_remove (ldsm_timeout_id); + ldsm_timeout_id = g_timeout_add_seconds (CHECK_EVERY_X_SECONDS, + ldsm_check_all_mounts, NULL); +} + +static gboolean +ldsm_is_hash_item_in_ignore_paths (gpointer key, + gpointer value, + gpointer user_data) +{ + return ldsm_mount_is_user_ignore (key); +} + +static void +msd_ldsm_get_config () +{ + GError *error = NULL; + + free_percent_notify = mateconf_client_get_float (client, + MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_PC_NOTIFY_KEY, + &error); + if (error != NULL) { + g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); + g_clear_error (&error); + } + if (free_percent_notify >= 1 || free_percent_notify < 0) { + g_warning ("Invalid configuration of free_percent_notify: %f\n" \ + "Using sensible default", free_percent_notify); + free_percent_notify = 0.05; + } + + free_percent_notify_again = mateconf_client_get_float (client, + MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_PC_NOTIFY_AGAIN_KEY, + &error); + if (error != NULL) { + g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); + g_clear_error (&error); + } + if (free_percent_notify_again >= 1 || free_percent_notify_again < 0) { + g_warning ("Invalid configuration of free_percent_notify_again: %f\n" \ + "Using sensible default\n", free_percent_notify_again); + free_percent_notify_again = 0.01; + } + + free_size_gb_no_notify = mateconf_client_get_int (client, + MATECONF_HOUSEKEEPING_DIR "/" MATECONF_FREE_SIZE_NO_NOTIFY, + &error); + if (error != NULL) { + g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); + g_clear_error (&error); + } + min_notify_period = mateconf_client_get_int (client, + MATECONF_HOUSEKEEPING_DIR "/" MATECONF_MIN_NOTIFY_PERIOD, + &error); + if (error != NULL) { + g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); + g_clear_error (&error); + } + + if (ignore_paths != NULL) { + g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); + g_slist_free (ignore_paths); + } + ignore_paths = mateconf_client_get_list (client, + MATECONF_HOUSEKEEPING_DIR "/" MATECONF_IGNORE_PATHS, + MATECONF_VALUE_STRING, &error); + if (error != NULL) { + g_warning ("Error reading configuration from MateConf: %s", error->message ? error->message : "Unknown error"); + g_clear_error (&error); + } else { + /* Make sure we dont leave stale entries in ldsm_notified_hash */ + g_hash_table_foreach_remove (ldsm_notified_hash, + ldsm_is_hash_item_in_ignore_paths, NULL); + } +} + +static void +msd_ldsm_update_config (MateConfClient *client, + guint cnxn_id, + MateConfEntry *entry, + gpointer user_data) +{ + msd_ldsm_get_config (); +} + +void +msd_ldsm_setup (gboolean check_now) +{ + GError *error = NULL; + + if (ldsm_notified_hash || ldsm_timeout_id || ldsm_monitor) { + g_warning ("Low disk space monitor already initialized."); + return; + } + + ldsm_notified_hash = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, + ldsm_free_mount_info); + + client = mateconf_client_get_default (); + if (client != NULL) { + msd_ldsm_get_config (); + mateconf_notify_id = mateconf_client_notify_add (client, + MATECONF_HOUSEKEEPING_DIR, + (MateConfClientNotifyFunc) msd_ldsm_update_config, + NULL, NULL, &error); + if (error != NULL) { + g_warning ("Cannot register callback for MateConf notification"); + g_clear_error (&error); + } + } else { + g_warning ("Failed to get default client"); + } + + ldsm_monitor = g_unix_mount_monitor_new (); + g_unix_mount_monitor_set_rate_limit (ldsm_monitor, 1000); + g_signal_connect (ldsm_monitor, "mounts-changed", + G_CALLBACK (ldsm_mounts_changed), NULL); + + if (check_now) + ldsm_check_all_mounts (NULL); + + ldsm_timeout_id = g_timeout_add_seconds (CHECK_EVERY_X_SECONDS, + ldsm_check_all_mounts, NULL); + +} + +void +msd_ldsm_clean (void) +{ + if (ldsm_timeout_id) + g_source_remove (ldsm_timeout_id); + ldsm_timeout_id = 0; + + if (ldsm_notified_hash) + g_hash_table_destroy (ldsm_notified_hash); + ldsm_notified_hash = NULL; + + if (ldsm_monitor) + g_object_unref (ldsm_monitor); + ldsm_monitor = NULL; + + if (client) { + mateconf_client_notify_remove (client, mateconf_notify_id); + g_object_unref (client); + } + + if (dialog) { + gtk_widget_destroy (GTK_WIDGET (dialog)); + dialog = NULL; + } + + if (ignore_paths) { + g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); + g_slist_free (ignore_paths); + } +} + +#ifdef TEST +int +main (int argc, + char **argv) +{ + GMainLoop *loop; + + gtk_init (&argc, &argv); + + loop = g_main_loop_new (NULL, FALSE); + + msd_ldsm_setup (TRUE); + + g_main_loop_run (loop); + + msd_ldsm_clean (); + g_main_loop_unref (loop); + + return 0; +} +#endif /* TEST */ diff --git a/plugins/housekeeping/msd-disk-space.h b/plugins/housekeeping/msd-disk-space.h new file mode 100644 index 0000000..43f7059 --- /dev/null +++ b/plugins/housekeeping/msd-disk-space.h @@ -0,0 +1,40 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * vim: set et sw=8 ts=8: + * + * Copyright (c) 2008, Novell, Inc. + * + * Authors: Vincent Untz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef __MSD_DISK_SPACE_H +#define __MSD_DISK_SPACE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void msd_ldsm_setup (gboolean check_now); +void msd_ldsm_clean (void); + +#ifdef __cplusplus +} +#endif + +#endif /* __MSD_DISK_SPACE_H */ diff --git a/plugins/housekeeping/msd-housekeeping-manager.c b/plugins/housekeeping/msd-housekeeping-manager.c new file mode 100644 index 0000000..6cb3353 --- /dev/null +++ b/plugins/housekeeping/msd-housekeeping-manager.c @@ -0,0 +1,389 @@ +/* + * Copyright (C) 2008 Michael J. Chudobiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "mate-settings-profile.h" +#include "msd-housekeeping-manager.h" +#include "msd-disk-space.h" + + +/* General */ +#define INTERVAL_ONCE_A_DAY 24*60*60 +#define INTERVAL_TWO_MINUTES 2*60 + + +/* Thumbnail cleaner */ +#define MATECONF_THUMB_AGE "/desktop/mate/thumbnail_cache/maximum_age" +#define DEFAULT_MAX_AGE_IN_DAYS 180 +#define MATECONF_THUMB_SIZE "/desktop/mate/thumbnail_cache/maximum_size" +#define DEFAULT_MAX_SIZE_IN_MB 512 +#define MATECONF_THUMB_BINDING_DIR "/desktop/mate/thumbnail_cache" + + +struct MsdHousekeepingManagerPrivate { + guint long_term_cb; + guint short_term_cb; + guint mateconf_notify; +}; + + +#define MSD_HOUSEKEEPING_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerPrivate)) + +static void msd_housekeeping_manager_class_init (MsdHousekeepingManagerClass *klass); +static void msd_housekeeping_manager_init (MsdHousekeepingManager *housekeeping_manager); + +G_DEFINE_TYPE (MsdHousekeepingManager, msd_housekeeping_manager, G_TYPE_OBJECT) + +static gpointer manager_object = NULL; + + +typedef struct { + glong now; + glong max_age; + goffset total_size; + goffset max_size; +} PurgeData; + + +typedef struct { + time_t mtime; + char *path; + glong size; +} ThumbData; + + +static void +thumb_data_free (gpointer data) +{ + ThumbData *info = data; + + if (info) { + g_free (info->path); + g_free (info); + } +} + + +static GList * +read_dir_for_purge (const char *path, GList *files) +{ + GFile *read_path; + GFileEnumerator *enum_dir; + + read_path = g_file_new_for_path (path); + enum_dir = g_file_enumerate_children (read_path, + G_FILE_ATTRIBUTE_STANDARD_NAME "," + G_FILE_ATTRIBUTE_TIME_MODIFIED "," + G_FILE_ATTRIBUTE_STANDARD_SIZE, + G_FILE_QUERY_INFO_NONE, + NULL, + NULL); + + if (enum_dir != NULL) { + GFileInfo *info; + while ((info = g_file_enumerator_next_file (enum_dir, NULL, NULL)) != NULL) { + const char *name; + name = g_file_info_get_name (info); + + if (strlen (name) == 36 && strcmp (name + 32, ".png") == 0) { + ThumbData *td; + GFile *entry; + char *entry_path; + GTimeVal mod_time; + + entry = g_file_get_child (read_path, name); + entry_path = g_file_get_path (entry); + g_object_unref (entry); + + g_file_info_get_modification_time (info, &mod_time); + + td = g_new0 (ThumbData, 1); + td->path = entry_path; + td->mtime = mod_time.tv_sec; + td->size = g_file_info_get_size (info); + + files = g_list_prepend (files, td); + } + g_object_unref (info); + } + g_object_unref (enum_dir); + } + g_object_unref (read_path); + + return files; +} + + +static void +purge_old_thumbnails (ThumbData *info, PurgeData *purge_data) +{ + if ((purge_data->now - info->mtime) > purge_data->max_age) { + g_unlink (info->path); + info->size = 0; + } else { + purge_data->total_size += info->size; + } +} + + +static int +sort_file_mtime (ThumbData *file1, ThumbData *file2) +{ + return file1->mtime - file2->mtime; +} + + +static int +get_mateconf_int_with_default (char *key, int default_value) +{ + /* If the key is unset, we use a non-zero default value. + A zero value corresponds to an extra-paranoid level + of cleaning - it deletes all files. We don't want that + as a default condition. */ + + MateConfValue *value; + MateConfClient *client; + int res; + + client = mateconf_client_get_default (); + value = mateconf_client_get (client, key, NULL); + g_object_unref (client); + + if (value == NULL || value->type != MATECONF_VALUE_INT) { + res = default_value; + } else { + res = mateconf_value_get_int (value); + mateconf_value_free (value); + } + + return res; +} + + +static void +purge_thumbnail_cache (void) +{ + + char *path; + GList *files; + PurgeData purge_data; + GTimeVal current_time; + + g_debug ("housekeeping: checking thumbnail cache size and freshness"); + + path = g_build_filename (g_get_home_dir (), + ".thumbnails", + "normal", + NULL); + files = read_dir_for_purge (path, NULL); + g_free (path); + + path = g_build_filename (g_get_home_dir (), + ".thumbnails", + "large", + NULL); + files = read_dir_for_purge (path, files); + g_free (path); + + path = g_build_filename (g_get_home_dir (), + ".thumbnails", + "fail", + "mate-thumbnail-factory", + NULL); + files = read_dir_for_purge (path, files); + g_free (path); + + g_get_current_time (¤t_time); + + purge_data.now = current_time.tv_sec; + purge_data.max_age = get_mateconf_int_with_default (MATECONF_THUMB_AGE, DEFAULT_MAX_AGE_IN_DAYS) * 24 * 60 * 60; + purge_data.max_size = get_mateconf_int_with_default (MATECONF_THUMB_SIZE, DEFAULT_MAX_SIZE_IN_MB) * 1024 * 1024; + purge_data.total_size = 0; + + if (purge_data.max_age >= 0) + g_list_foreach (files, (GFunc) purge_old_thumbnails, &purge_data); + + if ((purge_data.total_size > purge_data.max_size) && (purge_data.max_size >= 0)) { + GList *scan; + files = g_list_sort (files, (GCompareFunc) sort_file_mtime); + for (scan = files; scan && (purge_data.total_size > purge_data.max_size); scan = scan->next) { + ThumbData *info = scan->data; + g_unlink (info->path); + purge_data.total_size -= info->size; + } + } + + g_list_foreach (files, (GFunc) thumb_data_free, NULL); + g_list_free (files); +} + + +static gboolean +do_cleanup (MsdHousekeepingManager *manager) +{ + purge_thumbnail_cache (); + return TRUE; +} + + +static gboolean +do_cleanup_once (MsdHousekeepingManager *manager) +{ + do_cleanup (manager); + manager->priv->short_term_cb = 0; + return FALSE; +} + + +static void +do_cleanup_soon (MsdHousekeepingManager *manager) +{ + if (manager->priv->short_term_cb == 0) { + g_debug ("housekeeping: will tidy up in 2 minutes"); + manager->priv->short_term_cb = g_timeout_add_seconds (INTERVAL_TWO_MINUTES, + (GSourceFunc) do_cleanup_once, + manager); + } +} + + +static void +bindings_callback (MateConfClient *client, + guint cnxn_id, + MateConfEntry *entry, + MsdHousekeepingManager *manager) +{ + do_cleanup_soon (manager); +} + + +static guint +register_config_callback (MsdHousekeepingManager *manager, + const char *path, + MateConfClientNotifyFunc func) +{ + MateConfClient *client = mateconf_client_get_default (); + guint notify; + + mateconf_client_add_dir (client, path, MATECONF_CLIENT_PRELOAD_NONE, NULL); + notify = mateconf_client_notify_add (client, path, func, manager, NULL, NULL); + + g_object_unref (client); + + return notify; +} + + +gboolean +msd_housekeeping_manager_start (MsdHousekeepingManager *manager, + GError **error) +{ + g_debug ("Starting housekeeping manager"); + mate_settings_profile_start (NULL); + + msd_ldsm_setup (FALSE); + + manager->priv->mateconf_notify = register_config_callback (manager, + MATECONF_THUMB_BINDING_DIR, + (MateConfClientNotifyFunc) bindings_callback); + + /* Clean once, a few minutes after start-up */ + do_cleanup_soon (manager); + + /* Clean periodically, on a daily basis. */ + manager->priv->long_term_cb = g_timeout_add_seconds (INTERVAL_ONCE_A_DAY, + (GSourceFunc) do_cleanup, + manager); + mate_settings_profile_end (NULL); + + return TRUE; +} + + +void +msd_housekeeping_manager_stop (MsdHousekeepingManager *manager) +{ + MsdHousekeepingManagerPrivate *p = manager->priv; + + g_debug ("Stopping housekeeping manager"); + + if (p->mateconf_notify != 0) { + MateConfClient *client = mateconf_client_get_default (); + + mateconf_client_remove_dir (client, MATECONF_THUMB_BINDING_DIR, NULL); + mateconf_client_notify_remove (client, p->mateconf_notify); + + g_object_unref (client); + p->mateconf_notify = 0; + } + + if (p->short_term_cb) { + g_source_remove (p->short_term_cb); + p->short_term_cb = 0; + } + + if (p->long_term_cb) { + g_source_remove (p->long_term_cb); + p->long_term_cb = 0; + + /* Do a clean-up on shutdown if and only if the size or age + limits have been set to paranoid levels (zero) */ + if ((get_mateconf_int_with_default (MATECONF_THUMB_AGE, DEFAULT_MAX_AGE_IN_DAYS) == 0) || + (get_mateconf_int_with_default (MATECONF_THUMB_SIZE, DEFAULT_MAX_SIZE_IN_MB) == 0)) { + do_cleanup (manager); + } + } + + msd_ldsm_clean (); +} + + +static void +msd_housekeeping_manager_class_init (MsdHousekeepingManagerClass *klass) +{ + g_type_class_add_private (klass, sizeof (MsdHousekeepingManagerPrivate)); +} + + +static void +msd_housekeeping_manager_init (MsdHousekeepingManager *manager) +{ + manager->priv = MSD_HOUSEKEEPING_MANAGER_GET_PRIVATE (manager); +} + + +MsdHousekeepingManager * +msd_housekeeping_manager_new (void) +{ + if (manager_object != NULL) { + g_object_ref (manager_object); + } else { + manager_object = g_object_new (MSD_TYPE_HOUSEKEEPING_MANAGER, NULL); + g_object_add_weak_pointer (manager_object, + (gpointer *) &manager_object); + } + + return MSD_HOUSEKEEPING_MANAGER (manager_object); +} diff --git a/plugins/housekeeping/msd-housekeeping-manager.h b/plugins/housekeeping/msd-housekeeping-manager.h new file mode 100644 index 0000000..11f50a7 --- /dev/null +++ b/plugins/housekeeping/msd-housekeeping-manager.h @@ -0,0 +1,59 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Michael J. Chudobiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef __MSD_HOUSEKEEPING_MANAGER_H +#define __MSD_HOUSEKEEPING_MANAGER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define MSD_TYPE_HOUSEKEEPING_MANAGER (msd_housekeeping_manager_get_type ()) +#define MSD_HOUSEKEEPING_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManager)) +#define MSD_HOUSEKEEPING_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerClass)) +#define MSD_IS_HOUSEKEEPING_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_HOUSEKEEPING_MANAGER)) +#define MSD_IS_HOUSEKEEPING_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_HOUSEKEEPING_MANAGER)) +#define MSD_HOUSEKEEPING_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_HOUSEKEEPING_MANAGER, MsdHousekeepingManagerClass)) + +typedef struct MsdHousekeepingManagerPrivate MsdHousekeepingManagerPrivate; + +typedef struct { + GObject parent; + MsdHousekeepingManagerPrivate *priv; +} MsdHousekeepingManager; + +typedef struct { + GObjectClass parent_class; +} MsdHousekeepingManagerClass; + +GType msd_housekeeping_manager_get_type (void); + +MsdHousekeepingManager * msd_housekeeping_manager_new (void); +gboolean msd_housekeeping_manager_start (MsdHousekeepingManager *manager, + GError **error); +void msd_housekeeping_manager_stop (MsdHousekeepingManager *manager); + +#ifdef __cplusplus +} +#endif + +#endif /* __MSD_HOUSEKEEPING_MANAGER_H */ diff --git a/plugins/housekeeping/msd-housekeeping-plugin.c b/plugins/housekeeping/msd-housekeeping-plugin.c new file mode 100644 index 0000000..5b0cfef --- /dev/null +++ b/plugins/housekeeping/msd-housekeeping-plugin.c @@ -0,0 +1,104 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Michael J. Chudobiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include "config.h" + +#include +#include + +#include "mate-settings-plugin.h" +#include "msd-housekeeping-plugin.h" +#include "msd-housekeeping-manager.h" + +struct MsdHousekeepingPluginPrivate { + MsdHousekeepingManager *manager; +}; + +#define MSD_HOUSEKEEPING_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginPrivate)) + +MATE_SETTINGS_PLUGIN_REGISTER (MsdHousekeepingPlugin, msd_housekeeping_plugin) + +static void +msd_housekeeping_plugin_init (MsdHousekeepingPlugin *plugin) +{ + plugin->priv = MSD_HOUSEKEEPING_PLUGIN_GET_PRIVATE (plugin); + + g_debug ("MsdHousekeepingPlugin initializing"); + + plugin->priv->manager = msd_housekeeping_manager_new (); +} + +static void +msd_housekeeping_plugin_finalize (GObject *object) +{ + MsdHousekeepingPlugin *plugin; + + g_return_if_fail (object != NULL); + g_return_if_fail (MSD_IS_HOUSEKEEPING_PLUGIN (object)); + + g_debug ("MsdHousekeepingPlugin finalizing"); + + plugin = MSD_HOUSEKEEPING_PLUGIN (object); + + g_return_if_fail (plugin->priv != NULL); + + if (plugin->priv->manager != NULL) { + g_object_unref (plugin->priv->manager); + } + + G_OBJECT_CLASS (msd_housekeeping_plugin_parent_class)->finalize (object); +} + +static void +impl_activate (MateSettingsPlugin *plugin) +{ + gboolean res; + GError *error; + + g_debug ("Activating housekeeping plugin"); + + error = NULL; + res = msd_housekeeping_manager_start (MSD_HOUSEKEEPING_PLUGIN (plugin)->priv->manager, &error); + if (! res) { + g_warning ("Unable to start housekeeping manager: %s", error->message); + g_error_free (error); + } +} + +static void +impl_deactivate (MateSettingsPlugin *plugin) +{ + g_debug ("Deactivating housekeeping plugin"); + msd_housekeeping_manager_stop (MSD_HOUSEKEEPING_PLUGIN (plugin)->priv->manager); +} + +static void +msd_housekeeping_plugin_class_init (MsdHousekeepingPluginClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + MateSettingsPluginClass *plugin_class = MATE_SETTINGS_PLUGIN_CLASS (klass); + + object_class->finalize = msd_housekeeping_plugin_finalize; + + plugin_class->activate = impl_activate; + plugin_class->deactivate = impl_deactivate; + + g_type_class_add_private (klass, sizeof (MsdHousekeepingPluginPrivate)); +} diff --git a/plugins/housekeeping/msd-housekeeping-plugin.h b/plugins/housekeeping/msd-housekeeping-plugin.h new file mode 100644 index 0000000..1c2d816 --- /dev/null +++ b/plugins/housekeeping/msd-housekeeping-plugin.h @@ -0,0 +1,61 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Michael J. Chudobiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef __MSD_HOUSEKEEPING_PLUGIN_H__ +#define __MSD_HOUSEKEEPING_PLUGIN_H__ + +#include +#include +#include + +#include "mate-settings-plugin.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define MSD_TYPE_HOUSEKEEPING_PLUGIN (msd_housekeeping_plugin_get_type ()) +#define MSD_HOUSEKEEPING_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPlugin)) +#define MSD_HOUSEKEEPING_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginClass)) +#define MSD_IS_HOUSEKEEPING_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN)) +#define MSD_IS_HOUSEKEEPING_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_HOUSEKEEPING_PLUGIN)) +#define MSD_HOUSEKEEPING_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_HOUSEKEEPING_PLUGIN, MsdHousekeepingPluginClass)) + +typedef struct MsdHousekeepingPluginPrivate MsdHousekeepingPluginPrivate; + +typedef struct { + MateSettingsPlugin parent; + MsdHousekeepingPluginPrivate *priv; +} MsdHousekeepingPlugin; + +typedef struct { + MateSettingsPluginClass parent_class; +} MsdHousekeepingPluginClass; + +GType msd_housekeeping_plugin_get_type (void) G_GNUC_CONST; + +/* All the plugins must implement this function */ +G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); + +#ifdef __cplusplus +} +#endif + +#endif /* __MSD_HOUSEKEEPING_PLUGIN_H__ */ diff --git a/plugins/housekeeping/msd-ldsm-dialog.c b/plugins/housekeeping/msd-ldsm-dialog.c new file mode 100644 index 0000000..80ca857 --- /dev/null +++ b/plugins/housekeeping/msd-ldsm-dialog.c @@ -0,0 +1,476 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * msd-ldsm-dialog.c + * Copyright (C) Chris Coulson 2009 + * + * msd-ldsm-dialog.c is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * msd-ldsm-dialog.c is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include + +#include "msd-ldsm-dialog.h" + +#define MATECONF_CLIENT_IGNORE_PATHS "/apps/mate_settings_daemon/plugins/housekeeping/ignore_paths" + +enum +{ + PROP_0, + PROP_OTHER_USABLE_PARTITIONS, + PROP_OTHER_PARTITIONS, + PROP_HAS_TRASH, + PROP_SPACE_REMAINING, + PROP_PARTITION_NAME, + PROP_MOUNT_PATH +}; + +struct MsdLdsmDialogPrivate +{ + GtkWidget *primary_label; + GtkWidget *secondary_label; + GtkWidget *ignore_check_button; + gboolean other_usable_partitions; + gboolean other_partitions; + gboolean has_trash; + gint64 space_remaining; + gchar *partition_name; + gchar *mount_path; +}; + +#define MSD_LDSM_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogPrivate)) + +static void msd_ldsm_dialog_class_init (MsdLdsmDialogClass *klass); +static void msd_ldsm_dialog_init (MsdLdsmDialog *dialog); + +G_DEFINE_TYPE (MsdLdsmDialog, msd_ldsm_dialog, GTK_TYPE_DIALOG); + +static const gchar* +msd_ldsm_dialog_get_checkbutton_text (MsdLdsmDialog *dialog) +{ + g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); + + if (dialog->priv->other_partitions) + return _("Don't show any warnings again for this file system"); + else + return _("Don't show any warnings again"); +} + +static gchar* +msd_ldsm_dialog_get_primary_text (MsdLdsmDialog *dialog) +{ + gchar *primary_text, *free_space; + + g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); + + free_space = g_format_size_for_display (dialog->priv->space_remaining); + + if (dialog->priv->other_partitions) { + primary_text = g_strdup_printf (_("The volume \"%s\" has only %s disk space remaining."), + dialog->priv->partition_name, free_space); + } else { + primary_text = g_strdup_printf (_("This computer has only %s disk space remaining."), + free_space); + } + + g_free (free_space); + + return primary_text; +} + +static const gchar* +msd_ldsm_dialog_get_secondary_text (MsdLdsmDialog *dialog) +{ + g_return_val_if_fail (MSD_IS_LDSM_DIALOG (dialog), NULL); + + if (dialog->priv->other_usable_partitions) { + if (dialog->priv->has_trash) { + return _("You can free up disk space by emptying the Trash, removing " \ + "unused programs or files, or moving files to another disk or partition."); + } else { + return _("You can free up disk space by removing unused programs or files, " \ + "or by moving files to another disk or partition."); + } + } else { + if (dialog->priv->has_trash) { + return _("You can free up disk space by emptying the Trash, removing unused " \ + "programs or files, or moving files to an external disk."); + } else { + return _("You can free up disk space by removing unused programs or files, " \ + "or by moving files to an external disk."); + } + } +} + +static gint +ignore_path_compare (gconstpointer a, + gconstpointer b) +{ + return g_strcmp0 ((const gchar *)a, (const gchar *)b); +} + +static gboolean +update_ignore_paths (GSList **ignore_paths, + const gchar *mount_path, + gboolean ignore) +{ + GSList *found; + gchar *path_to_remove; + + found = g_slist_find_custom (*ignore_paths, mount_path, (GCompareFunc) ignore_path_compare); + + if (ignore && (found == NULL)) { + *ignore_paths = g_slist_prepend (*ignore_paths, g_strdup (mount_path)); + return TRUE; + } + + if (!ignore && (found != NULL)) { + path_to_remove = found->data; + *ignore_paths = g_slist_remove (*ignore_paths, path_to_remove); + g_free (path_to_remove); + return TRUE; + } + + return FALSE; +} + +static void +ignore_check_button_toggled_cb (GtkToggleButton *button, + gpointer user_data) +{ + MsdLdsmDialog *dialog = (MsdLdsmDialog *)user_data; + MateConfClient *client; + GSList *ignore_paths; + GError *error = NULL; + gboolean ignore, ret, updated; + + client = mateconf_client_get_default (); + if (client != NULL) { + ignore_paths = mateconf_client_get_list (client, + MATECONF_CLIENT_IGNORE_PATHS, + MATECONF_VALUE_STRING, &error); + if (error != NULL) { + g_warning ("Cannot change ignore preference - failed to read existing configuration: %s", + error->message ? error->message : "Unkown error"); + g_clear_error (&error); + return; + } else { + ignore = gtk_toggle_button_get_active (button); + updated = update_ignore_paths (&ignore_paths, dialog->priv->mount_path, ignore); + } + + if (!updated) + return; + + ret = mateconf_client_set_list (client, + MATECONF_CLIENT_IGNORE_PATHS, + MATECONF_VALUE_STRING, + ignore_paths, &error); + if (!ret || error != NULL) { + g_warning ("Cannot change ignore preference - failed to commit changes: %s", + error->message ? error->message : "Unkown error"); + g_clear_error (&error); + } + + g_slist_foreach (ignore_paths, (GFunc) g_free, NULL); + g_slist_free (ignore_paths); + g_object_unref (client); + } else { + g_warning ("Cannot change ignore preference - failed to get MateConfClient"); + } +} + +static void +msd_ldsm_dialog_init (MsdLdsmDialog *dialog) +{ + GtkWidget *main_vbox, *text_vbox, *hbox; + GtkWidget *image; + + dialog->priv = MSD_LDSM_DIALOG_GET_PRIVATE (dialog); + + main_vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); + + /* Set up all the window stuff here */ + gtk_window_set_title (GTK_WINDOW (dialog), _("Low Disk Space")); + gtk_window_set_icon_name (GTK_WINDOW (dialog), + GTK_STOCK_DIALOG_WARNING); + gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE); + gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER); + gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE); + gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE); + gtk_container_set_border_width (GTK_CONTAINER (dialog), 5); + + /* We don't want a separator - they're really ugly */ + gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE); + + /* Create the image */ + image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG); + gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0); + + /* Create the labels */ + dialog->priv->primary_label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (dialog->priv->primary_label), TRUE); + gtk_label_set_single_line_mode (GTK_LABEL (dialog->priv->primary_label), FALSE); + gtk_misc_set_alignment (GTK_MISC (dialog->priv->primary_label), 0.0, 0.0); + + dialog->priv->secondary_label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (dialog->priv->secondary_label), TRUE); + gtk_label_set_single_line_mode (GTK_LABEL (dialog->priv->secondary_label), FALSE); + gtk_misc_set_alignment (GTK_MISC (dialog->priv->secondary_label), 0.0, 0.0); + + /* Create the check button to ignore future warnings */ + dialog->priv->ignore_check_button = gtk_check_button_new (); + /* The button should be inactive if the dialog was just called. + * I suppose it could be possible for the user to manually edit the MateConf key between + * the mount being checked and the dialog appearing, but I don't think it matters + * too much */ + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->ignore_check_button), FALSE); + g_signal_connect (dialog->priv->ignore_check_button, "toggled", + G_CALLBACK (ignore_check_button_toggled_cb), dialog); + + /* Now set up the dialog's GtkBox's' */ + gtk_box_set_spacing (GTK_BOX (main_vbox), 14); + + hbox = gtk_hbox_new (FALSE, 12); + gtk_container_set_border_width (GTK_CONTAINER (hbox), 5); + + text_vbox = gtk_vbox_new (FALSE, 12); + + gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->primary_label, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->secondary_label, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (text_vbox), dialog->priv->ignore_check_button, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (hbox), text_vbox, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0); + + /* Set up the action area */ + gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), 6); + gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), 5); + + gtk_widget_show_all (hbox); +} + +static void +msd_ldsm_dialog_finalize (GObject *object) +{ + MsdLdsmDialog *self; + + g_return_if_fail (object != NULL); + g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); + + self = MSD_LDSM_DIALOG (object); + + if (self->priv->partition_name) + g_free (self->priv->partition_name); + + if (self->priv->mount_path) + g_free (self->priv->mount_path); + + G_OBJECT_CLASS (msd_ldsm_dialog_parent_class)->finalize (object); +} + +static void +msd_ldsm_dialog_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + MsdLdsmDialog *self; + + g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); + + self = MSD_LDSM_DIALOG (object); + + switch (prop_id) + { + case PROP_OTHER_USABLE_PARTITIONS: + self->priv->other_usable_partitions = g_value_get_boolean (value); + break; + case PROP_OTHER_PARTITIONS: + self->priv->other_partitions = g_value_get_boolean (value); + break; + case PROP_HAS_TRASH: + self->priv->has_trash = g_value_get_boolean (value); + break; + case PROP_SPACE_REMAINING: + self->priv->space_remaining = g_value_get_int64 (value); + break; + case PROP_PARTITION_NAME: + self->priv->partition_name = g_value_dup_string (value); + break; + case PROP_MOUNT_PATH: + self->priv->mount_path = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +msd_ldsm_dialog_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + MsdLdsmDialog *self; + + g_return_if_fail (MSD_IS_LDSM_DIALOG (object)); + + self = MSD_LDSM_DIALOG (object); + + switch (prop_id) + { + case PROP_OTHER_USABLE_PARTITIONS: + g_value_set_boolean (value, self->priv->other_usable_partitions); + break; + case PROP_OTHER_PARTITIONS: + g_value_set_boolean (value, self->priv->other_partitions); + break; + case PROP_HAS_TRASH: + g_value_set_boolean (value, self->priv->has_trash); + break; + case PROP_SPACE_REMAINING: + g_value_set_int64 (value, self->priv->space_remaining); + break; + case PROP_PARTITION_NAME: + g_value_set_string (value, self->priv->partition_name); + break; + case PROP_MOUNT_PATH: + g_value_set_string (value, self->priv->mount_path); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +msd_ldsm_dialog_class_init (MsdLdsmDialogClass *klass) +{ + GObjectClass* object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = msd_ldsm_dialog_finalize; + object_class->set_property = msd_ldsm_dialog_set_property; + object_class->get_property = msd_ldsm_dialog_get_property; + + g_object_class_install_property (object_class, + PROP_OTHER_USABLE_PARTITIONS, + g_param_spec_boolean ("other-usable-partitions", + "other-usable-partitions", + "Set to TRUE if there are other usable partitions on the system", + FALSE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_OTHER_PARTITIONS, + g_param_spec_boolean ("other-partitions", + "other-partitions", + "Set to TRUE if there are other partitions on the system", + FALSE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_HAS_TRASH, + g_param_spec_boolean ("has-trash", + "has-trash", + "Set to TRUE if the partition has files in it's trash folder that can be deleted", + FALSE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_SPACE_REMAINING, + g_param_spec_int64 ("space-remaining", + "space-remaining", + "Specify how much space is remaining in bytes", + G_MININT64, G_MAXINT64, 0, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_PARTITION_NAME, + g_param_spec_string ("partition-name", + "partition-name", + "Specify the name of the partition", + "Unknown", + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_MOUNT_PATH, + g_param_spec_string ("mount-path", + "mount-path", + "Specify the mount path for the partition", + "Unknown", + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_type_class_add_private (klass, sizeof (MsdLdsmDialogPrivate)); +} + +MsdLdsmDialog* +msd_ldsm_dialog_new (gboolean other_usable_partitions, + gboolean other_partitions, + gboolean display_baobab, + gboolean display_empty_trash, + gint64 space_remaining, + const gchar *partition_name, + const gchar *mount_path) +{ + MsdLdsmDialog *dialog; + GtkWidget *button_empty_trash, *button_ignore, *button_analyze; + GtkWidget *empty_trash_image, *analyze_image, *ignore_image; + gchar *primary_text, *primary_text_markup; + const gchar *secondary_text, *checkbutton_text; + + dialog = MSD_LDSM_DIALOG (g_object_new (MSD_TYPE_LDSM_DIALOG, + "other-usable-partitions", other_usable_partitions, + "other-partitions", other_partitions, + "has-trash", display_empty_trash, + "space-remaining", space_remaining, + "partition-name", partition_name, + "mount-path", mount_path, + NULL)); + + /* Add some buttons */ + if (dialog->priv->has_trash) { + button_empty_trash = gtk_dialog_add_button (GTK_DIALOG (dialog), + _("Empty Trash"), + MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH); + empty_trash_image = gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_BUTTON); + gtk_button_set_image (GTK_BUTTON (button_empty_trash), empty_trash_image); + } + + if (display_baobab) { + button_analyze = gtk_dialog_add_button (GTK_DIALOG (dialog), + _("Examine…"), + MSD_LDSM_DIALOG_RESPONSE_ANALYZE); + analyze_image = gtk_image_new_from_icon_name ("baobab", GTK_ICON_SIZE_BUTTON); + gtk_button_set_image (GTK_BUTTON (button_analyze), analyze_image); + } + + button_ignore = gtk_dialog_add_button (GTK_DIALOG (dialog), + _("Ignore"), + GTK_RESPONSE_CANCEL); + ignore_image = gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON); + gtk_button_set_image (GTK_BUTTON (button_ignore), ignore_image); + + gtk_widget_grab_default (button_ignore); + + /* Set the label text */ + primary_text = msd_ldsm_dialog_get_primary_text (dialog); + primary_text_markup = g_markup_printf_escaped ("%s", primary_text); + gtk_label_set_markup (GTK_LABEL (dialog->priv->primary_label), primary_text_markup); + + secondary_text = msd_ldsm_dialog_get_secondary_text (dialog); + gtk_label_set_text (GTK_LABEL (dialog->priv->secondary_label), secondary_text); + + checkbutton_text = msd_ldsm_dialog_get_checkbutton_text (dialog); + gtk_button_set_label (GTK_BUTTON (dialog->priv->ignore_check_button), checkbutton_text); + + g_free (primary_text); + g_free (primary_text_markup); + + return dialog; +} diff --git a/plugins/housekeeping/msd-ldsm-dialog.h b/plugins/housekeeping/msd-ldsm-dialog.h new file mode 100644 index 0000000..8b95fb9 --- /dev/null +++ b/plugins/housekeeping/msd-ldsm-dialog.h @@ -0,0 +1,72 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * msd-ldsm-dialog.c + * Copyright (C) Chris Coulson 2009 + * + * msd-ldsm-dialog.c is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * msd-ldsm-dialog.c is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef _MSD_LDSM_DIALOG_H_ +#define _MSD_LDSM_DIALOG_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define MSD_TYPE_LDSM_DIALOG (msd_ldsm_dialog_get_type ()) +#define MSD_LDSM_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialog)) +#define MSD_LDSM_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogClass)) +#define MSD_IS_LDSM_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MSD_TYPE_LDSM_DIALOG)) +#define MSD_IS_LDSM_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MSD_TYPE_LDSM_DIALOG)) +#define MSD_LDSM_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MSD_TYPE_LDSM_DIALOG, MsdLdsmDialogClass)) + +enum +{ + MSD_LDSM_DIALOG_RESPONSE_EMPTY_TRASH = -20, + MSD_LDSM_DIALOG_RESPONSE_ANALYZE = -21 +}; + +typedef struct MsdLdsmDialogPrivate MsdLdsmDialogPrivate; +typedef struct _MsdLdsmDialogClass MsdLdsmDialogClass; +typedef struct _MsdLdsmDialog MsdLdsmDialog; + +struct _MsdLdsmDialogClass +{ + GtkDialogClass parent_class; +}; + +struct _MsdLdsmDialog +{ + GtkDialog parent_instance; + MsdLdsmDialogPrivate *priv; +}; + +GType msd_ldsm_dialog_get_type (void) G_GNUC_CONST; + +MsdLdsmDialog * msd_ldsm_dialog_new (gboolean other_usable_partitions, + gboolean other_partitions, + gboolean display_baobab, + gboolean display_empty_trash, + gint64 space_remaining, + const gchar *partition_name, + const gchar *mount_path); + +#ifdef __cplusplus +} +#endif + +#endif /* _MSD_LDSM_DIALOG_H_ */ diff --git a/plugins/housekeeping/msd-ldsm-trash-empty.c b/plugins/housekeeping/msd-ldsm-trash-empty.c new file mode 100644 index 0000000..05f82cd --- /dev/null +++ b/plugins/housekeeping/msd-ldsm-trash-empty.c @@ -0,0 +1,398 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * msd-ldsm-trash-empty.c + * Copyright (C) Chris Coulson 2009 + * (C) Ryan Lortie 2008 + * + * msd-ldsm-trash-empty.c is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * msd-ldsm-trash-empty.c is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include +#include + +#include "msd-ldsm-trash-empty.h" + +#define CAJA_CONFIRM_TRASH_KEY "/apps/caja/preferences/confirm_trash" + +/* Some of this code has been borrowed from the trash-applet, courtesy of Ryan Lortie */ + +static GtkWidget *trash_empty_confirm_dialog = NULL; +static GtkWidget *trash_empty_dialog = NULL; +static GtkWidget *location_label; +static GtkWidget *file_label; +static GtkWidget *progressbar; + +static gsize trash_empty_total_files; +static gboolean trash_empty_update_pending = FALSE; +static GFile *trash_empty_current_file = NULL; +static gsize trash_empty_deleted_files; +static GTimer *timer = NULL; +static gboolean trash_empty_actually_deleting; + +static gboolean +trash_empty_done (gpointer data) +{ + gtk_widget_destroy (trash_empty_dialog); + trash_empty_dialog = NULL; + if (timer) { + g_timer_destroy (timer); + timer = NULL; + } + + return FALSE; +} + +static gboolean +trash_empty_update_dialog (gpointer user_data) +{ + gsize deleted, total; + GFile *file; + gboolean actually_deleting; + + g_assert (trash_empty_update_pending); + + deleted = trash_empty_deleted_files; + total = trash_empty_total_files; + file = trash_empty_current_file; + actually_deleting = trash_empty_actually_deleting; + + /* maybe the done() got processed first. */ + if (!trash_empty_dialog) + goto out; + + if (!actually_deleting) { + /* If we havent finished counting yet, then pulse the progressbar every 100ms. + * This stops the user from thinking the dialog has frozen if there are + * a lot of files to delete. We don't pulse it every time we are called from the + * worker thread, otherwise it moves to fast and looks hideous + */ + if (timer) { + if (g_timer_elapsed (timer, NULL) > 0.1) { + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progressbar)); + g_timer_start (timer); + } + } else { + timer = g_timer_new (); + g_timer_start (timer); + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progressbar)); + } + } else { + gchar *text; + gchar *tmp; + gchar *markup; + GFile *parent; + + text = g_strdup_printf (_("Removing item %lu of %lu"), + deleted, total); + gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar), text); + + g_free (text); + + if (deleted > total) + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progressbar), 1.0); + else + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progressbar), + (gdouble) deleted / (gdouble) total); + + parent = g_file_get_parent (file); + text = g_file_get_uri (parent); + g_object_unref (parent); + + gtk_label_set_text (GTK_LABEL (location_label), text); + g_free (text); + + tmp = g_file_get_basename (file); + text = g_markup_printf_escaped (_("Removing: %s"), tmp); + markup = g_strdup_printf ("%s", text); + gtk_label_set_markup (GTK_LABEL (file_label), text); + g_free (markup); + g_free (text); + g_free (tmp); + + /* unhide the labels */ + gtk_widget_show_all (GTK_WIDGET (trash_empty_dialog)); + } + +out: + trash_empty_current_file = NULL; + g_object_unref (file); + + trash_empty_update_pending = FALSE; + + return FALSE; +} + +/* Worker thread begin */ + +static void +trash_empty_maybe_schedule_update (GIOSchedulerJob *job, + GFile *file, + gsize deleted, + gboolean actually_deleting) +{ + if (!trash_empty_update_pending) { + g_assert (trash_empty_current_file == NULL); + + trash_empty_current_file = g_object_ref (file); + trash_empty_deleted_files = deleted; + trash_empty_actually_deleting = actually_deleting; + + trash_empty_update_pending = TRUE; + g_io_scheduler_job_send_to_mainloop_async (job, + trash_empty_update_dialog, + NULL, NULL); + } +} + +static void +trash_empty_delete_contents (GIOSchedulerJob *job, + GCancellable *cancellable, + GFile *file, + gboolean actually_delete, + gsize *deleted) +{ + GFileEnumerator *enumerator; + GFileInfo *info; + GFile *child; + + if (g_cancellable_is_cancelled (cancellable)) + return; + + enumerator = g_file_enumerate_children (file, + G_FILE_ATTRIBUTE_STANDARD_NAME "," + G_FILE_ATTRIBUTE_STANDARD_TYPE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + cancellable, NULL); + + if (enumerator) { + while ((info = g_file_enumerator_next_file (enumerator, + cancellable, NULL)) != NULL) { + child = g_file_get_child (file, g_file_info_get_name (info)); + + if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) + trash_empty_delete_contents (job, cancellable, child, + actually_delete, deleted); + + trash_empty_maybe_schedule_update (job, child, *deleted, actually_delete); + if (actually_delete) + g_file_delete (child, cancellable, NULL); + + (*deleted)++; + + g_object_unref (child); + g_object_unref (info); + + if (g_cancellable_is_cancelled (cancellable)) + break; + } + + g_object_unref (enumerator); + } +} + +static gboolean +trash_empty_job (GIOSchedulerJob *job, + GCancellable *cancellable, + gpointer user_data) +{ + gsize deleted; + GFile *trash; + + trash = g_file_new_for_uri ("trash:///"); + + /* first do a dry run to count the number of files */ + deleted = 0; + trash_empty_delete_contents (job, cancellable, trash, FALSE, &deleted); + trash_empty_total_files = deleted; + + /* now do the real thing */ + deleted = 0; + trash_empty_delete_contents (job, cancellable, trash, TRUE, &deleted); + + /* done */ + g_object_unref (trash); + g_io_scheduler_job_send_to_mainloop_async (job, + trash_empty_done, + NULL, NULL); + + return FALSE; +} + +/* Worker thread end */ + +static void +trash_empty_start () +{ + GtkWidget *vbox1, *vbox2, *hbox; + GtkWidget *label1, *label3; + gchar *markup; + GCancellable *cancellable; + + trash_empty_dialog = gtk_dialog_new (); + gtk_window_set_default_size (GTK_WINDOW (trash_empty_dialog), 400, -1); + gtk_window_set_icon_name (GTK_WINDOW (trash_empty_dialog), "user-trash"); + gtk_window_set_title (GTK_WINDOW (trash_empty_dialog), + _("Emptying the trash")); + + vbox1 = gtk_vbox_new (FALSE, 12); + vbox2 = gtk_vbox_new (FALSE, 0); + hbox = gtk_hbox_new (FALSE, 0); + + label1 = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (label1), TRUE); + gtk_misc_set_alignment (GTK_MISC (label1), 0.0, 0.5); + + label3 = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (label3), TRUE); + gtk_misc_set_alignment (GTK_MISC (label3), 0.0, 0.5); + gtk_widget_hide (label3); + + location_label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (location_label), TRUE); + gtk_misc_set_alignment (GTK_MISC (location_label), 0.0, 0.5); + + file_label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (file_label), TRUE); + gtk_misc_set_alignment (GTK_MISC (file_label), 0.0, 0.5); + + progressbar = gtk_progress_bar_new (); + gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (progressbar), 0.1); + gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar), _("Preparing to empty trash…")); + + gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (trash_empty_dialog))), vbox1, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox1), label1, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (hbox), label3, FALSE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (hbox), location_label, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox1), hbox, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox2), progressbar, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox2), file_label, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox1), vbox2, TRUE, TRUE, 0); + + gtk_widget_show (label1); + gtk_widget_show (vbox1); + gtk_widget_show_all (vbox2); + gtk_widget_show (hbox); + gtk_widget_show (location_label); + + gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (trash_empty_dialog))), 6); + gtk_container_set_border_width (GTK_CONTAINER (vbox1), 6); + + gtk_dialog_add_button (GTK_DIALOG (trash_empty_dialog), + GTK_STOCK_CANCEL, + GTK_RESPONSE_CANCEL); + + markup = g_markup_printf_escaped ("%s", _("Emptying the trash")); + gtk_label_set_markup (GTK_LABEL (label1), markup); + /* Translators: "Emptying trash from " */ + gtk_label_set_text (GTK_LABEL (label3), _("From: ")); + + cancellable = g_cancellable_new (); + g_signal_connect_object (trash_empty_dialog, "response", + G_CALLBACK (g_cancellable_cancel), + cancellable, G_CONNECT_SWAPPED); + g_io_scheduler_push_job (trash_empty_job, NULL, NULL, 0, cancellable); + + gtk_widget_show (trash_empty_dialog); + + g_free (markup); + g_object_unref (cancellable); +} + +static void +trash_empty_confirmation_response (GtkDialog *dialog, + gint response_id, + gpointer user_data) +{ + if (response_id == GTK_RESPONSE_YES) + trash_empty_start (); + + gtk_object_destroy (GTK_OBJECT (dialog)); + trash_empty_confirm_dialog = NULL; +} + +static gboolean +trash_empty_require_confirmation () +{ + MateConfClient *client; + gboolean require_confirmation = TRUE; + GError *error = NULL; + + client = mateconf_client_get_default (); + if (client) { + require_confirmation = mateconf_client_get_bool (client, CAJA_CONFIRM_TRASH_KEY, &error); + if (error) { + g_warning ("Failed to read confirm_trash key from MateConf: %s", error->message ? error->message : "Unknown error"); + /* It's safest to assume that confirmation is required here */ + require_confirmation = TRUE; + g_error_free (error); + } + g_object_unref (client); + } + + return require_confirmation; +} + +static void +trash_empty_show_confirmation_dialog () +{ + GtkWidget *button; + + if (!trash_empty_require_confirmation ()) { + trash_empty_start (); + return; + } + + trash_empty_confirm_dialog = gtk_message_dialog_new (NULL, 0, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_NONE, + _("Empty all of the items from the trash?")); + + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (trash_empty_confirm_dialog), + _("If you choose to empty the trash, all items in " + "it will be permanently lost. Please note that " + "you can also delete them separately.")); + + gtk_dialog_add_button (GTK_DIALOG (trash_empty_confirm_dialog), GTK_STOCK_CANCEL, + GTK_RESPONSE_CANCEL); + + button = gtk_button_new_with_mnemonic (_("_Empty Trash")); + gtk_widget_show (button); + gtk_widget_set_can_default (button, TRUE); + + gtk_dialog_add_action_widget (GTK_DIALOG (trash_empty_confirm_dialog), + button, GTK_RESPONSE_YES); + + gtk_dialog_set_default_response (GTK_DIALOG (trash_empty_confirm_dialog), + GTK_RESPONSE_YES); + + gtk_window_set_icon_name (GTK_WINDOW (trash_empty_confirm_dialog), + "user-trash"); + + gtk_widget_show (trash_empty_confirm_dialog); + + g_signal_connect (trash_empty_confirm_dialog, "response", + G_CALLBACK (trash_empty_confirmation_response), NULL); +} + +void +msd_ldsm_trash_empty () +{ + if (trash_empty_confirm_dialog) + gtk_window_present (GTK_WINDOW (trash_empty_confirm_dialog)); + else if (trash_empty_dialog) + gtk_window_present (GTK_WINDOW (trash_empty_dialog)); + else + trash_empty_show_confirmation_dialog (); +} diff --git a/plugins/housekeeping/msd-ldsm-trash-empty.h b/plugins/housekeeping/msd-ldsm-trash-empty.h new file mode 100644 index 0000000..85b09c0 --- /dev/null +++ b/plugins/housekeeping/msd-ldsm-trash-empty.h @@ -0,0 +1,27 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * msd-ldsm-trash-empty.h + * Copyright (C) Chris Coulson 2009 + * + * msd-ldsm-trash-empty.h is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * msd-ldsm-trash-empty.h is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef _msd_ldsm_trash_empty_h_ +#define _msd_ldsm_trash_empty_h_ + +#include + +void msd_ldsm_trash_empty (); + +#endif /* _msd_ldsm_trash_empty_h_ */ -- cgit v1.2.1