[gthumb] bookmarks: fixed ability to modify the bookmarks
- From: Paolo Bacchilega <paobac src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gthumb] bookmarks: fixed ability to modify the bookmarks
- Date: Sat, 13 Apr 2013 22:43:04 +0000 (UTC)
commit df3a5412c39a24d3522d702fc3c03e0c9651a78d
Author: Paolo Bacchilega <paobac src gnome org>
Date: Sun Apr 14 00:35:16 2013 +0200
bookmarks: fixed ability to modify the bookmarks
Do not update the bookmark list while changing the name or the
location of the bookmark, wait for the user to press Enter.
extensions/bookmarks/dlg-bookmarks.c | 337 ++++++++++++++++++++++-------------
gthumb/gth-uri-list.c | 145 +++++++++++----
gthumb/gth-uri-list.h | 12 +-
3 files changed, 334 insertions(+), 160 deletions(-)
---
diff --git a/extensions/bookmarks/dlg-bookmarks.c b/extensions/bookmarks/dlg-bookmarks.c
index cd914f1..997d42f 100644
--- a/extensions/bookmarks/dlg-bookmarks.c
+++ b/extensions/bookmarks/dlg-bookmarks.c
@@ -24,30 +24,147 @@
#include <gthumb.h>
-#define UPDATE_DELAY 200
-
-
typedef struct {
GthBrowser *browser;
GtkBuilder *builder;
GtkWidget *dialog;
GtkWidget *uri_list;
- gboolean do_not_update;
+ char *last_selected_uri;
gulong bookmarks_changed_id;
- gulong update_from_entry_id;
+ gboolean entry_changed;
} DialogData;
+static void
+entry_activate_cb (GtkEntry *entry,
+ DialogData *data);
+
+
+static void
+set_bookmark_data (DialogData *data,
+ const char *name,
+ const char *location)
+{
+ g_signal_handlers_block_by_func (_gtk_builder_get_widget (data->builder, "entry_name"),
entry_activate_cb, data);
+ g_signal_handlers_block_by_func (_gtk_builder_get_widget (data->builder, "entry_location"),
entry_activate_cb, data);
+ gtk_entry_set_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_name")), name);
+ gtk_entry_set_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_location")), location);
+ g_signal_handlers_unblock_by_func (_gtk_builder_get_widget (data->builder, "entry_location"),
entry_activate_cb, data);
+ g_signal_handlers_unblock_by_func (_gtk_builder_get_widget (data->builder, "entry_name"),
entry_activate_cb, data);
+
+ data->entry_changed = FALSE;
+}
+
+
+static void
+update_dialog_from_bookmark_file (DialogData *data,
+ const char *uri)
+{
+ GBookmarkFile *bookmarks;
+ GFile *file;
+ char *location;
+ char *name;
+
+ bookmarks = gth_main_get_default_bookmarks ();
+
+ file = g_file_new_for_uri (uri);
+ location = g_file_get_parse_name (file);
+
+ name = g_bookmark_file_get_title (bookmarks, uri, NULL);
+ if (name == NULL)
+ name = g_file_get_basename (file);
+
+ set_bookmark_data (data, name, location);
+
+ g_free (name);
+ g_free (location);
+ g_object_unref (file);
+}
+
+
+static void
+update_current_entry (DialogData *data,
+ gboolean *update_selected_uri)
+{
+ const char *name;
+ const char *location;
+ GFile *file;
+ char *uri;
+ GBookmarkFile *bookmarks;
+
+ if (update_selected_uri != NULL)
+ *update_selected_uri = TRUE;
+
+ if (data->last_selected_uri == NULL)
+ return;
+
+ if (! data->entry_changed)
+ return;
+
+ data->entry_changed = FALSE;
+
+ name = gtk_entry_get_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_name")));
+ location = gtk_entry_get_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_location")));
+ file = g_file_parse_name (location);
+ uri = g_file_get_uri (file);
+
+ bookmarks = gth_main_get_default_bookmarks ();
+ gth_uri_list_update_uri (GTH_URI_LIST (data->uri_list), data->last_selected_uri, uri, name);
+ gth_uri_list_update_bookmarks (GTH_URI_LIST (data->uri_list), bookmarks);
+ gth_main_bookmarks_changed ();
+
+ if (g_strcmp0 (data->last_selected_uri, uri) != 0) {
+ g_free (data->last_selected_uri);
+ data->last_selected_uri = g_strdup (uri);
+ if (update_selected_uri != NULL)
+ *update_selected_uri = FALSE;
+ }
+
+ g_free (uri);
+ g_object_unref (file);
+}
+
+
+static void
+uri_list_selection_changed_cb (GtkTreeSelection *treeselection,
+ gpointer user_data)
+{
+ DialogData *data = user_data;
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ char *uri;
+ gboolean update_selected_uri;
+
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (data->uri_list));
+ if (! gtk_tree_selection_get_selected (treeselection,
+ &model,
+ &iter))
+ {
+ return;
+ }
+
+ uri = gth_uri_list_get_uri (GTH_URI_LIST (data->uri_list), &iter);
+ if (uri == NULL)
+ return;
+
+ update_current_entry (data, &update_selected_uri);
+ if (update_selected_uri) {
+ g_free (data->last_selected_uri);
+ data->last_selected_uri = uri;
+ }
+ update_dialog_from_bookmark_file (data, uri);
+}
+
+
/* called when the main dialog is closed. */
static void
-destroy_cb (GtkWidget *widget,
+destroy_cb (GtkWidget *widget,
DialogData *data)
{
- if (data->update_from_entry_id != 0)
- g_source_remove (data->update_from_entry_id);
+ update_current_entry (data, NULL);
gth_browser_set_dialog (data->browser, "bookmarks", NULL);
g_signal_handler_disconnect (gth_main_get_default_monitor (), data->bookmarks_changed_id);
-
+
g_object_unref (data->builder);
g_free (data);
}
@@ -60,18 +177,21 @@ remove_cb (GtkWidget *widget,
char *uri;
GBookmarkFile *bookmarks;
GError *error = NULL;
-
+
uri = gth_uri_list_get_selected (GTH_URI_LIST (data->uri_list));
if (uri == NULL)
return;
-
+
bookmarks = gth_main_get_default_bookmarks ();
- if (! g_bookmark_file_remove_item (bookmarks, uri, &error)) {
+ if (g_bookmark_file_remove_item (bookmarks, uri, &error)) {
+ gth_uri_list_remove_uri (GTH_URI_LIST (data->uri_list), uri);
+ gth_main_bookmarks_changed ();
+ }
+ else {
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->dialog), _("Could not remove the
bookmark"), error);
g_clear_error (&error);
}
- gth_main_bookmarks_changed ();
-
+
g_free (uri);
}
@@ -81,14 +201,14 @@ go_to_cb (GtkWidget *widget,
DialogData *data)
{
char *uri;
-
+
uri = gth_uri_list_get_selected (GTH_URI_LIST (data->uri_list));
if (uri != NULL) {
GFile *location;
-
+
location = g_file_new_for_uri (uri);
gth_browser_go_to (data->browser, location, NULL);
-
+
g_object_unref (location);
g_free (uri);
}
@@ -99,10 +219,49 @@ static void
bookmarks_changed_cb (GthMonitor *monitor,
DialogData *data)
{
- GBookmarkFile *bookmarks;
-
+ GBookmarkFile *bookmarks;
+ char *uri;
+ GtkTreeSelection *selection;
+ gboolean selected;
+
+ if (data->entry_changed)
+ return;
+
+ uri = gth_uri_list_get_selected (GTH_URI_LIST (data->uri_list));
+
+ g_free (data->last_selected_uri);
+ data->last_selected_uri = NULL; /* do no update the entry */
+
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->uri_list));
+ g_signal_handlers_block_by_func (selection, uri_list_selection_changed_cb, data);
bookmarks = gth_main_get_default_bookmarks ();
gth_uri_list_set_bookmarks (GTH_URI_LIST (data->uri_list), bookmarks);
+ g_signal_handlers_unblock_by_func (selection, uri_list_selection_changed_cb, data);
+
+ selected = FALSE;
+ if (uri != NULL)
+ selected = gth_uri_list_select_uri (GTH_URI_LIST (data->uri_list), uri);
+
+ if (! selected) {
+ /* select the last one */
+
+ char **uris;
+ char *last_uri;
+ int i;
+
+ uris = g_bookmark_file_get_uris (bookmarks, NULL);
+ last_uri = NULL;
+ for (i = 0; uris[i] != NULL; i++)
+ last_uri = uris[i];
+ if (last_uri != NULL)
+ gth_uri_list_select_uri (GTH_URI_LIST (data->uri_list), last_uri);
+ else
+ set_bookmark_data (data, "", "");
+
+ g_strfreev (uris);
+ }
+
+ g_free (uri);
}
@@ -129,51 +288,28 @@ uri_list_row_activated_cb (GtkTreeView *tree_view,
GtkTreeIter iter;
char *uri;
GFile *location;
-
+
tree_model = gtk_tree_view_get_model (tree_view);
if (! gtk_tree_model_get_iter (tree_model, &iter, path))
return;
-
+
uri = gth_uri_list_get_uri (GTH_URI_LIST (tree_view), &iter);
if (uri == NULL)
return;
-
+
location = g_file_new_for_uri (uri);
gth_browser_go_to (data->browser, location, NULL);
-
+
g_object_unref (location);
g_free (uri);
}
-static gboolean
-save_bookmarks_cb (gpointer user_data)
+static void
+entry_activate_cb (GtkEntry *entry,
+ DialogData *data)
{
- DialogData *data = user_data;
- const char *name;
- const char *location;
- GFile *file;
- char *uri;
- GBookmarkFile *bookmarks;
-
- if (data->update_from_entry_id != 0) {
- g_source_remove (data->update_from_entry_id);
- data->update_from_entry_id = 0;
- }
-
- name = gtk_entry_get_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_name")));
- location = gtk_entry_get_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_location")));
- file = g_file_parse_name (location);
- uri = g_file_get_uri (file);
-
- bookmarks = gth_main_get_default_bookmarks ();
- g_bookmark_file_set_title (bookmarks, uri, name);
- gth_main_bookmarks_changed ();
-
- g_free (uri);
- g_object_unref (file);
-
- return FALSE;
+ update_current_entry (data, NULL);
}
@@ -181,70 +317,7 @@ static void
entry_changed_cb (GtkEditable *editable,
DialogData *data)
{
- if (data->update_from_entry_id != 0) {
- g_source_remove (data->update_from_entry_id);
- data->update_from_entry_id = 0;
- }
-
- data->update_from_entry_id = gdk_threads_add_timeout (UPDATE_DELAY, save_bookmarks_cb, data);
-}
-
-
-static void
-update_dialog_from_bookmark_file (DialogData *data,
- const char *uri)
-{
- GBookmarkFile *bookmarks;
- GFile *file;
- char *location;
- char *name;
-
- bookmarks = gth_main_get_default_bookmarks ();
-
- file = g_file_new_for_uri (uri);
- location = g_file_get_parse_name (file);
-
- name = g_bookmark_file_get_title (bookmarks, uri, NULL);
- if (name == NULL)
- name = g_strdup (location);
-
- g_signal_handlers_block_by_func (_gtk_builder_get_widget (data->builder, "entry_name"),
entry_changed_cb, data);
- g_signal_handlers_block_by_func (_gtk_builder_get_widget (data->builder, "entry_location"),
entry_changed_cb, data);
- gtk_entry_set_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_name")), name);
- gtk_entry_set_text (GTK_ENTRY (_gtk_builder_get_widget (data->builder, "entry_location")), location);
- g_signal_handlers_unblock_by_func (_gtk_builder_get_widget (data->builder, "entry_location"),
entry_changed_cb, data);
- g_signal_handlers_unblock_by_func (_gtk_builder_get_widget (data->builder, "entry_name"),
entry_changed_cb, data);
-
- g_free (name);
- g_free (location);
- g_object_unref (file);
-}
-
-
-static void
-uri_list_selection_changed_cb (GtkTreeSelection *treeselection,
- gpointer user_data)
-{
- DialogData *data = user_data;
- GtkTreeModel *model;
- GtkTreeIter iter;
- char *uri;
-
- model = gtk_tree_view_get_model (GTK_TREE_VIEW (data->uri_list));
- if (! gtk_tree_selection_get_selected (treeselection,
- &model,
- &iter))
- {
- return;
- }
-
- uri = gth_uri_list_get_uri (GTH_URI_LIST (data->uri_list), &iter);
- if (uri == NULL)
- return;
-
- update_dialog_from_bookmark_file (data, uri);
-
- g_free (uri);
+ data->entry_changed = TRUE;
}
@@ -259,7 +332,7 @@ dlg_bookmarks (GthBrowser *browser)
GtkWidget *bm_go_to_button;
GBookmarkFile *bookmarks;
GtkTreeSelection *selection;
-
+
if (gth_browser_get_dialog (browser, "bookmarks") != NULL) {
gtk_window_present (GTK_WINDOW (gth_browser_get_dialog (browser, "bookmarks")));
return;
@@ -267,9 +340,9 @@ dlg_bookmarks (GthBrowser *browser)
data = g_new0 (DialogData, 1);
data->browser = browser;
- data->do_not_update = FALSE;
data->builder = _gtk_builder_new_from_file ("bookmarks.ui", "bookmarks");
- data->update_from_entry_id = 0;
+ data->last_selected_uri = NULL;
+ data->entry_changed = FALSE;
/* Get the widgets. */
@@ -282,7 +355,7 @@ dlg_bookmarks (GthBrowser *browser)
bm_remove_button = _gtk_builder_get_widget (data->builder, "bm_remove_button");
bm_close_button = _gtk_builder_get_widget (data->builder, "bm_close_button");
bm_go_to_button = _gtk_builder_get_widget (data->builder, "bm_go_to_button");
-
+
data->uri_list = gth_uri_list_new ();
gtk_widget_show (data->uri_list);
gtk_container_add (GTK_CONTAINER (bm_list_container), data->uri_list);
@@ -293,38 +366,46 @@ dlg_bookmarks (GthBrowser *browser)
bookmarks = gth_main_get_default_bookmarks ();
gth_uri_list_set_bookmarks (GTH_URI_LIST (data->uri_list), bookmarks);
- data->bookmarks_changed_id = g_signal_connect (gth_main_get_default_monitor (),
+ data->bookmarks_changed_id = g_signal_connect (gth_main_get_default_monitor (),
"bookmarks-changed",
- G_CALLBACK (bookmarks_changed_cb),
+ G_CALLBACK (bookmarks_changed_cb),
data);
/* Set the signals handlers. */
-
- g_signal_connect (G_OBJECT (data->dialog),
+
+ g_signal_connect (G_OBJECT (data->dialog),
"destroy",
G_CALLBACK (destroy_cb),
data);
- g_signal_connect_swapped (G_OBJECT (bm_close_button),
+ g_signal_connect_swapped (G_OBJECT (bm_close_button),
"clicked",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (data->dialog));
- g_signal_connect (G_OBJECT (bm_remove_button),
+ g_signal_connect (G_OBJECT (bm_remove_button),
"clicked",
G_CALLBACK (remove_cb),
data);
- g_signal_connect (G_OBJECT (bm_go_to_button),
+ g_signal_connect (G_OBJECT (bm_go_to_button),
"clicked",
G_CALLBACK (go_to_cb),
data);
- g_signal_connect (G_OBJECT (data->uri_list),
+ g_signal_connect (G_OBJECT (data->uri_list),
"order-changed",
G_CALLBACK (uri_list_order_changed_cb),
data);
- g_signal_connect (G_OBJECT (data->uri_list),
+ g_signal_connect (G_OBJECT (data->uri_list),
"row-activated",
G_CALLBACK (uri_list_row_activated_cb),
data);
g_signal_connect (_gtk_builder_get_widget (data->builder, "entry_location"),
+ "activate",
+ G_CALLBACK (entry_activate_cb),
+ data);
+ g_signal_connect (_gtk_builder_get_widget (data->builder, "entry_name"),
+ "activate",
+ G_CALLBACK (entry_activate_cb),
+ data);
+ g_signal_connect (_gtk_builder_get_widget (data->builder, "entry_location"),
"changed",
G_CALLBACK (entry_changed_cb),
data);
@@ -341,7 +422,7 @@ dlg_bookmarks (GthBrowser *browser)
/* run dialog. */
- gtk_window_set_transient_for (GTK_WINDOW (data->dialog),
+ gtk_window_set_transient_for (GTK_WINDOW (data->dialog),
GTK_WINDOW (browser));
gtk_window_set_modal (GTK_WINDOW (data->dialog), FALSE);
gtk_widget_show (data->dialog);
diff --git a/gthumb/gth-uri-list.c b/gthumb/gth-uri-list.c
index bb2cd4f..cdede55 100644
--- a/gthumb/gth-uri-list.c
+++ b/gthumb/gth-uri-list.c
@@ -205,6 +205,47 @@ gth_uri_list_new (void)
}
+static void
+_gth_uri_list_set_iter (GthUriList *uri_list,
+ GtkTreeIter *iter,
+ const char *uri,
+ const char *name)
+{
+ GFile *file;
+ GthFileSource *file_source;
+ GFileInfo *info;
+ const char *display_name;
+ GIcon *icon;
+ GdkPixbuf *pixbuf;
+
+ file = g_file_new_for_uri (uri);
+ file_source = gth_main_get_file_source (file);
+ info = gth_file_source_get_file_info (file_source, file, GFILE_DISPLAY_ATTRIBUTES);
+
+ if (info != NULL) {
+ display_name = (name != NULL) ? name : g_file_info_get_display_name (info);
+ icon = g_file_info_get_icon (info);
+ }
+ else {
+ display_name = (name != NULL) ? name : _g_file_get_display_name (file);
+ icon = _g_file_get_icon (file);
+ }
+ pixbuf = gth_icon_cache_get_pixbuf (uri_list->priv->icon_cache, icon);
+
+
+ gtk_list_store_set (uri_list->priv->list_store, iter,
+ URI_LIST_COLUMN_ICON, pixbuf,
+ URI_LIST_COLUMN_NAME, display_name,
+ URI_LIST_COLUMN_URI, uri,
+ -1);
+
+ _g_object_unref (pixbuf);
+ g_object_unref (file_source);
+ g_object_unref (file);
+
+}
+
+
void
gth_uri_list_set_uris (GthUriList *uri_list,
char **uris)
@@ -220,38 +261,10 @@ gth_uri_list_set_uris (GthUriList *uri_list,
for (i = 0; uris[i] != NULL; i++) {
char *uri = uris[i];
- GFile *file;
- GthFileSource *file_source;
- GFileInfo *info;
- const char *display_name;
- GIcon *icon;
- GdkPixbuf *pixbuf;
GtkTreeIter iter;
- file = g_file_new_for_uri (uri);
- file_source = gth_main_get_file_source (file);
- info = gth_file_source_get_file_info (file_source, file, GFILE_DISPLAY_ATTRIBUTES);
-
- if (info != NULL) {
- display_name = g_file_info_get_display_name (info);
- icon = g_file_info_get_icon (info);
- }
- else {
- display_name = _g_file_get_display_name (file);
- icon = _g_file_get_icon (file);
- }
- pixbuf = gth_icon_cache_get_pixbuf (uri_list->priv->icon_cache, icon);
-
gtk_list_store_append (uri_list->priv->list_store, &iter);
- gtk_list_store_set (uri_list->priv->list_store, &iter,
- URI_LIST_COLUMN_ICON, pixbuf,
- URI_LIST_COLUMN_NAME, display_name,
- URI_LIST_COLUMN_URI, uri,
- -1);
-
- g_object_unref (pixbuf);
- g_object_unref (file_source);
- g_object_unref (file);
+ _gth_uri_list_set_iter (uri_list, &iter, uri, NULL);
}
g_signal_handlers_unblock_by_func (uri_list->priv->list_store, row_deleted_cb, uri_list);
@@ -309,7 +322,7 @@ gth_uri_list_set_bookmarks (GthUriList *uri_list,
URI_LIST_COLUMN_URI, uri,
-1);
- g_object_unref (pixbuf);
+ _g_object_unref (pixbuf);
g_object_unref (file_source);
g_object_unref (file);
}
@@ -408,3 +421,75 @@ gth_uri_list_update_bookmarks (GthUriList *uri_list,
}
while (gtk_tree_model_iter_next (model, &iter));
}
+
+
+static gboolean
+_gth_uri_list_get_iter (GthUriList *uri_list,
+ const char *uri,
+ GtkTreeIter *iter)
+{
+ GtkTreeModel *model = GTK_TREE_MODEL (uri_list->priv->list_store);
+
+ if (! gtk_tree_model_get_iter_first (model, iter))
+ return FALSE;
+
+ do {
+ char *iter_uri;
+
+ gtk_tree_model_get (model, iter, URI_LIST_COLUMN_URI, &iter_uri, -1);
+ if (g_strcmp0 (iter_uri, uri) == 0) {
+ g_free (iter_uri);
+ return TRUE;
+ }
+
+ g_free (iter_uri);
+ }
+ while (gtk_tree_model_iter_next (model, iter));
+
+ return FALSE;
+}
+
+
+gboolean
+gth_uri_list_remove_uri (GthUriList *uri_list,
+ const char *uri)
+{
+ GtkTreeIter iter;
+
+ if (! _gth_uri_list_get_iter (uri_list, uri, &iter))
+ return FALSE;
+
+ return gtk_list_store_remove (uri_list->priv->list_store, &iter);
+}
+
+
+gboolean
+gth_uri_list_select_uri (GthUriList *uri_list,
+ const char *uri)
+{
+ GtkTreeIter iter;
+
+ if (! _gth_uri_list_get_iter (uri_list, uri, &iter))
+ return FALSE;
+
+ gtk_tree_selection_select_iter (gtk_tree_view_get_selection (GTK_TREE_VIEW (uri_list)), &iter);
+
+ return TRUE;
+}
+
+
+gboolean
+gth_uri_list_update_uri (GthUriList *uri_list,
+ const char *uri,
+ const char *new_uri,
+ const char *new_name)
+{
+ GtkTreeIter iter;
+
+ if (! _gth_uri_list_get_iter (uri_list, uri, &iter))
+ return FALSE;
+
+ _gth_uri_list_set_iter (uri_list, &iter, new_uri, new_name);
+
+ return TRUE;
+}
diff --git a/gthumb/gth-uri-list.h b/gthumb/gth-uri-list.h
index 1356823..e1c1ab4 100644
--- a/gthumb/gth-uri-list.h
+++ b/gthumb/gth-uri-list.h
@@ -18,7 +18,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
+
#ifndef GTH_URI_LIST_H
#define GTH_URI_LIST_H
@@ -46,7 +46,7 @@ struct _GthUriList {
struct _GthUriListClass {
GtkTreeViewClass parent_class;
-
+
/*< signals >*/
void (*order_changed) (GthUriList *uri_list);
@@ -64,6 +64,14 @@ char * gth_uri_list_get_selected (GthUriList *uri_list);
GList * gth_uri_list_get_uris (GthUriList *uri_list);
void gth_uri_list_update_bookmarks (GthUriList *uri_list,
GBookmarkFile *bookmarks);
+gboolean gth_uri_list_remove_uri (GthUriList *uri_list,
+ const char *uri);
+gboolean gth_uri_list_select_uri (GthUriList *uri_list,
+ const char *uri);
+gboolean gth_uri_list_update_uri (GthUriList *uri_list,
+ const char *uri,
+ const char *new_uri,
+ const char *new_name);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]