[gnac/devel] Code refactoring
- From: BenoÃt Dupasquier <bdupasqu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnac/devel] Code refactoring
- Date: Wed, 14 Dec 2011 10:08:08 +0000 (UTC)
commit b72f8483ff989c342feb7528143be65837ccecde
Author: BenoÃt Dupasquier <bdupasqu src gnome org>
Date: Wed Dec 14 01:06:38 2011 +0000
Code refactoring
libgnac/libgnac-converter.c | 28 ++++++++----
src/gnac-main.c | 98 +++++++++++++++++++++----------------------
src/gnac-prefs.c | 4 +-
src/gnac-properties.c | 68 +++++++++++++----------------
src/gnac-ui.c | 11 +++--
src/gnac-utils.c | 24 ++++++++++
src/gnac-utils.h | 3 +
7 files changed, 133 insertions(+), 103 deletions(-)
---
diff --git a/libgnac/libgnac-converter.c b/libgnac/libgnac-converter.c
index 34af785..e68dca6 100644
--- a/libgnac/libgnac-converter.c
+++ b/libgnac/libgnac-converter.c
@@ -774,6 +774,22 @@ libgnac_converter_eos_cb(GstBus *bus,
}
+static void
+libgnac_converter_delete_file(GFile *file)
+{
+ if (!g_file_query_exists(file, NULL)) return;
+
+ GError *error = NULL;
+ g_file_delete(file, NULL, &error);
+ if (error) {
+ gchar *uri = g_file_get_uri(file);
+ libgnac_warning("Unable to remove file %s: %s", uri, error->message);
+ g_free(uri);
+ g_clear_error(&error);
+ }
+}
+
+
static gboolean
libgnac_converter_error_sink(LibgnacMediaItem *item,
GstMessage *message,
@@ -826,6 +842,7 @@ libgnac_converter_error_src(LibgnacMediaItem *item,
GError *error)
{
libgnac_debug("Source error");
+ libgnac_converter_delete_file(item->destination);
g_signal_emit(item->parent, signals[ERROR], 0, uri,
_("Unable to read source file"), error);
return FALSE;
@@ -839,18 +856,9 @@ libgnac_converter_error_other(LibgnacMediaItem *item,
GError *error)
{
libgnac_debug("Other error");
-
- GError *err = NULL;
-
- g_file_delete(item->destination, NULL, &err);
- if (err) {
- libgnac_warning("Unable to remove partial file %s: %s", uri, err->message);
- g_clear_error(&err);
- }
-
+ libgnac_converter_delete_file(item->destination);
g_signal_emit(item->parent, signals[ERROR], 0, uri,
_("An error occured during conversion"), error);
-
return FALSE;
}
diff --git a/src/gnac-main.c b/src/gnac-main.c
index 4573f7f..7287ecd 100644
--- a/src/gnac-main.c
+++ b/src/gnac-main.c
@@ -130,7 +130,6 @@ gnac_change_state(GnacState new_state)
default:
/* does nothing */
break;
-
}
}
@@ -257,7 +256,6 @@ gnac_add_files(GSList *files)
file_action_thread = g_thread_create(
(GThreadFunc) gnac_add_files_thread_entry, files, TRUE, &error);
if (error) {
- libgnac_debug("Failed to create thread");
libgnac_warning(_("Failed to add files: %s"), error->message);
g_clear_error(&error);
gnac_return_prev_state();
@@ -271,28 +269,22 @@ gnac_add_file(GFile *file)
gchar *uri = g_file_get_uri(file);
libgnac_debug("Trying to add file %s", uri);
- GError *error = NULL;
- GFileInfo *info = g_file_query_info(file,
- G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
- G_FILE_QUERY_INFO_NONE, NULL, &error);
- if (error) {
- libgnac_debug("Unable to query file info for %s: %s", uri, error->message);
- g_clear_error(&error);
- g_free(uri);
- return;
- }
-
- const gchar *mime_type = g_file_info_get_content_type(info);
+ gchar *mime_type = gnac_utils_get_mime_type(file);
if (gnac_playlist_is_mime_playlist(mime_type)) {
gnac_playlist_parse(file, mime_type);
} else if (gnac_utils_file_format_is_supported(mime_type)) {
+ GError *error = NULL;
libgnac_info("Add file %s", uri);
libgnac_converter_add(converter, file, &error);
+ if (error) {
+ libgnac_debug("Failed to add file %s: %s", uri, error->message);
+ g_clear_error(&error);
+ }
}
g_free(uri);
- g_object_unref(info);
+ g_free(mime_type);
}
@@ -464,51 +456,57 @@ gnac_on_converter_file_started_cb(LibgnacConverter *converter,
static void
-gnac_on_converter_file_completed_cb(LibgnacConverter *converter,
- const gchar *uri)
+gnac_trash_file(GFile *file)
{
- /* if delete source is selected, there is nothing else to do */
- if (!gnac_settings_get_boolean(GNAC_KEY_CLEAR_SOURCE)) return;
-
GError *error = NULL;
- GFile *file = g_file_new_for_uri(uri);
g_file_trash(file, NULL, &error);
+ if (!error ||
+ !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
+ {
+ g_clear_error(&error);
+ return;
+ }
+
+ g_clear_error(&error);
+ gchar *filename = gnac_utils_get_display_name_from_file(file, &error);
if (error) {
- if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED)) {
- g_clear_error(&error);
- gchar *filename = gnac_utils_get_display_name(uri, &error);
+ g_clear_error(&error);
+ g_object_unref(file);
+ return;
+ }
+
+ gint response = gnac_ui_show_error_trash(filename);
+
+ g_free(filename);
+
+ switch (response) {
+ case 1:
+ g_file_delete(file, NULL, &error);
if (error) {
+ g_printerr("%s: %s\n", _("Error"), error->message);
g_clear_error(&error);
- g_object_unref(file);
- return;
}
+ break;
+ default:
+ /* do nothing */
+ break;
+ }
- gdk_threads_enter();
- gint response = gnac_ui_show_error_trash(filename);
- gdk_threads_leave();
-
- g_free(filename);
-
- switch (response) {
- case 1:
- g_file_delete(file, NULL, &error);
- if (error) {
- g_printerr("%s: %s\n", _("Error"), error->message);
- g_clear_error(&error);
- }
- break;
- case GTK_RESPONSE_CANCEL:
- case GTK_RESPONSE_CLOSE:
- default:
- /* do nothing */
- break;
- }
- }
+ g_clear_error(&error);
+}
- g_object_unref(file);
- g_clear_error(&error);
- }
+
+static void
+gnac_on_converter_file_completed_cb(LibgnacConverter *converter,
+ const gchar *uri)
+{
+ /* if delete source is selected, there is nothing else to do */
+ if (!gnac_settings_get_boolean(GNAC_KEY_CLEAR_SOURCE)) return;
+
+ GFile *file = g_file_new_for_uri(uri);
+ gnac_trash_file(file);
+ g_object_unref(file);
}
diff --git a/src/gnac-prefs.c b/src/gnac-prefs.c
index aca829d..a42224b 100644
--- a/src/gnac-prefs.c
+++ b/src/gnac-prefs.c
@@ -263,9 +263,9 @@ gnac_prefs_retrieve_settings(void)
static void
gnac_prefs_set_parent(void)
{
- GtkWidget *prefs_window = gnac_prefs_get_widget("gnac_preference_window");
+ GtkWidget *window = gnac_prefs_get_widget("gnac_preference_window");
GtkWidget *parent = gnac_ui_get_widget("main_window");
- gtk_window_set_transient_for(GTK_WINDOW(prefs_window), GTK_WINDOW(parent));
+ gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
}
diff --git a/src/gnac-properties.c b/src/gnac-properties.c
index c0dc425..5bb79fd 100644
--- a/src/gnac-properties.c
+++ b/src/gnac-properties.c
@@ -45,8 +45,6 @@ static GtkBuilder *gnac_properties_builder = NULL;
static GHashTable *gnac_properties_table;
static GtkTreeModel *properties_filter;
-static GtkWidget *gnac_properties_window;
-
static gboolean properties_displayed = FALSE;
static GtkTreeRowReference *current_ref;
@@ -221,7 +219,6 @@ gnac_properties_reset_properties(void)
{
gnac_properties_reset_basic_tab();
gnac_properties_reset_properties_tab();
-
gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(properties_filter));
}
@@ -471,21 +468,22 @@ gnac_properties_dup_channels(const GValue *value)
}
-static GtkWidget *
-gnac_properties_window_new(void)
+static void
+gnac_properties_set_parent(void)
{
- GtkWidget *properties_window;
+ GtkWidget *window = gnac_properties_get_widget("properties_window");
+ GtkWidget *parent = gnac_ui_get_widget("main_window");
+ gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
+}
+
+static void
+gnac_properties_window_new(void)
+{
gnac_properties_builder = gnac_ui_utils_create_gtk_builder(
PKGDATADIR "/gnac-properties-window.xml");
-
+ gnac_properties_set_parent();
gnac_properties_build_table();
-
- properties_window = gnac_properties_get_widget("properties_window");
- gtk_window_set_transient_for(GTK_WINDOW(properties_window),
- GTK_WINDOW(gnac_ui_get_widget("main_window")));
-
- return properties_window;
}
@@ -495,7 +493,6 @@ gnac_properties_update_display(GtkTreeRowReference *reference)
if (!properties_displayed) return;
gchar *uri;
-
if (!gnac_file_list_get(reference, &uri)) return;
GFile *file = g_file_new_for_uri(uri);
@@ -515,7 +512,7 @@ gnac_properties_update_display(GtkTreeRowReference *reference)
void
gnac_properties_update_arrows(void)
{
- if (current_ref) {
+ if (current_ref && gnac_properties_builder) {
gnac_properties_update_forward_arrow();
gnac_properties_update_backward_arrow();
}
@@ -562,12 +559,10 @@ gnac_properties_reset_spin_if_empty(const gchar *name)
void
gnac_properties_window_show(void)
{
- if (!gnac_properties_window) {
- gnac_properties_window = gnac_properties_window_new();
+ if (!gnac_properties_builder) {
+ gnac_properties_window_new();
}
- GtkWidget *notebook = gnac_properties_get_widget("notebook1");
-
properties_displayed = TRUE;
if (gnac_file_list_get_current_row(¤t_ref)) {
@@ -575,7 +570,8 @@ gnac_properties_window_show(void)
gnac_properties_update_display(current_ref);
}
- gtk_widget_show_all(gnac_properties_window);
+ GtkWidget *window = gnac_properties_get_widget("properties_window");
+ gtk_widget_show_all(window);
gnac_properties_reset_spin_if_empty("track-number");
gnac_properties_reset_spin_if_empty("track-count");
@@ -583,18 +579,20 @@ gnac_properties_window_show(void)
gnac_properties_reset_spin_if_empty("album-disc-count");
gnac_properties_reset_spin_if_empty("date");
+ GtkWidget *notebook = gnac_properties_get_widget("notebook1");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
- gtk_window_present(GTK_WINDOW(gnac_properties_window));
+ gtk_window_present(GTK_WINDOW(window));
}
static void
gnac_properties_window_hide(void)
{
- if (gnac_properties_window) {
- gtk_widget_hide(gnac_properties_window);
- properties_displayed = FALSE;
- }
+ if (!gnac_properties_builder) return;
+
+ GtkWidget *window = gnac_properties_get_widget("properties_window");
+ gtk_widget_hide(window);
+ properties_displayed = FALSE;
}
@@ -617,9 +615,7 @@ void
gnac_properties_on_back(GtkWidget *widget,
gpointer data)
{
- GtkTreeRowReference *old_ref;
-
- old_ref = gtk_tree_row_reference_copy(current_ref);
+ GtkTreeRowReference *old_ref = gtk_tree_row_reference_copy(current_ref);
if (gnac_file_list_get_prev_row(¤t_ref)) {
gnac_file_list_select_row_and_follow(current_ref);
@@ -638,9 +634,7 @@ void
gnac_properties_on_forward(GtkWidget *widget,
gpointer data)
{
- GtkTreeRowReference *old_ref;
-
- old_ref = gtk_tree_row_reference_copy(current_ref);
+ GtkTreeRowReference *old_ref = gtk_tree_row_reference_copy(current_ref);
if (gnac_file_list_get_next_row(¤t_ref)) {
gnac_file_list_select_row_and_follow(current_ref);
@@ -677,10 +671,10 @@ gnac_properties_on_delete_event(GtkWidget *widget,
void
gnac_properties_destroy(void)
{
- if (gnac_properties_builder) {
- GtkWidget *window = gnac_properties_get_widget("properties_window");
- if (window) gtk_widget_destroy(window);
- g_object_unref(gnac_properties_builder);
- gnac_properties_builder = NULL;
- }
+ if (!gnac_properties_builder) return;
+
+ GtkWidget *window = gnac_properties_get_widget("properties_window");
+ if (window) gtk_widget_destroy(window);
+ g_object_unref(gnac_properties_builder);
+ gnac_properties_builder = NULL;
}
diff --git a/src/gnac-ui.c b/src/gnac-ui.c
index 567bbbd..1290e7e 100644
--- a/src/gnac-ui.c
+++ b/src/gnac-ui.c
@@ -1296,7 +1296,7 @@ gnac_ui_on_trayicon_popup(GtkStatusIcon *trayicon,
void
gnac_ui_trayicon_tooltip_update(const gchar *tooltip)
{
- if (!gnac_settings_get_boolean(GNAC_KEY_TRAY_ICON) || !trayicon) return;
+ if (!gnac_settings_ui_get_boolean(GNAC_KEY_TRAY_ICON) || !trayicon) return;
g_free(tooltip_path);
tooltip_path = g_strdup(tooltip);
@@ -1345,12 +1345,15 @@ gnac_ui_show_error_trash(const gchar *filename)
GtkWindow *main_window = GTK_WINDOW(gnac_ui_get_widget("main_window"));
GtkWidget *dialog = gtk_message_dialog_new(main_window,
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL,
- _("Failed to move the file to the Trash. Delete it permanently?"));
+ _("Cannot move file to trash, do you\nwant to delete immediately?"));
gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_DELETE, 1);
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
- _("Cannot move file %s to the Trash"), filename);
+ _("The file \"%s\" cannot be moved to the trash."), filename);
+
+ gdk_threads_enter();
+ gint response = gtk_dialog_run(GTK_DIALOG(dialog));
+ gdk_threads_leave();
- gint response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return response;
diff --git a/src/gnac-utils.c b/src/gnac-utils.c
index a55ef61..dc7b411 100644
--- a/src/gnac-utils.c
+++ b/src/gnac-utils.c
@@ -30,6 +30,7 @@
#include <glib/gi18n.h>
#include "gnac-utils.h"
+#include "libgnac-debug.h"
static guint64 average = 0;
@@ -72,6 +73,29 @@ gnac_utils_get_display_name(const gchar *uri,
gchar *
+gnac_utils_get_mime_type(GFile *file)
+{
+ GError *error = NULL;
+ GFileInfo *info = g_file_query_info(file,
+ G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+ G_FILE_QUERY_INFO_NONE, NULL, &error);
+ if (error) {
+ gchar *uri = g_file_get_uri(file);
+ libgnac_debug("Unable to query file info for %s: %s", uri, error->message);
+ g_clear_error(&error);
+ g_free(uri);
+ return NULL;
+ }
+
+ gchar *mime_type = g_strdup(g_file_info_get_content_type(info));
+
+ g_object_unref(info);
+
+ return mime_type;
+}
+
+
+gchar *
gnac_utils_format_duration_for_display(guint64 duration)
{
gint minutes = (gint) ((duration / 60) % 60);
diff --git a/src/gnac-utils.h b/src/gnac-utils.h
index 52fb87a..89733f6 100644
--- a/src/gnac-utils.h
+++ b/src/gnac-utils.h
@@ -40,6 +40,9 @@ gnac_utils_get_display_name(const gchar *uri,
GError **error);
gchar *
+gnac_utils_get_mime_type(GFile *uri);
+
+gchar *
gnac_utils_format_duration_for_display(guint64 duration);
guint
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]