Commit 60db9ce4 authored by Stephan Arts's avatar Stephan Arts

Do not let the image-list assume a reference on File objects (Bug #8297)

 - Re-implement reference-counting.
 - Tested with valgrind, does not appear to have caused any mem-leaks
parent 230fe68c
......@@ -207,6 +207,7 @@ rstto_file_new ( GFile *file )
o_file->priv->file,
file) )
{
g_object_ref (G_OBJECT (iter->data));
return (RsttoFile *)iter->data;
}
iter = g_list_next (iter);
......
......@@ -267,12 +267,18 @@ rstto_image_list_new (void)
}
gboolean
rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError **error)
rstto_image_list_add_file (
RsttoImageList *image_list,
RsttoFile *file,
GError **error )
{
GtkFileFilterInfo filter_info;
GList *image_iter = g_list_find (image_list->priv->images, file);
GSList *iter = image_list->priv->iterators;
g_return_val_if_fail ( NULL != file , FALSE);
g_return_val_if_fail ( RSTTO_IS_FILE (file) , FALSE);
if (!image_iter)
{
if (file)
......@@ -283,6 +289,8 @@ rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError *
if ( TRUE == gtk_file_filter_filter (image_list->priv->filter, &filter_info))
{
g_object_ref (G_OBJECT (file));
image_list->priv->images = g_list_insert_sorted (image_list->priv->images, file, rstto_image_list_get_compare_func (image_list));
image_list->priv->n_images++;
......@@ -301,12 +309,12 @@ rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError *
}
else
{
g_object_unref (file);
return FALSE;
}
}
return FALSE;
}
g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_NEW_IMAGE], 0, image_iter->data, NULL);
return TRUE;
......@@ -508,17 +516,18 @@ cb_file_monitor_changed (
{
case G_FILE_MONITOR_EVENT_DELETED:
rstto_image_list_remove_file ( image_list, r_file );
r_file = NULL;
break;
case G_FILE_MONITOR_EVENT_CREATED:
rstto_image_list_add_file (image_list, r_file, NULL);
r_file = NULL;
break;
case G_FILE_MONITOR_EVENT_MOVED:
rstto_image_list_remove_file ( image_list, r_file );
/* Remove our reference, reusing pointer */
g_object_unref (r_file);
r_file = rstto_file_new (other_file);
rstto_image_list_add_file (image_list, r_file, NULL);
r_file = NULL;
break;
default:
break;
......
......@@ -204,6 +204,8 @@ cb_rstto_open_files (RsttoOpenFiles *rof)
{
rstto_main_window_add_file_to_recent_files (file);
}
g_object_unref (r_file);
r_file = NULL;
}
}
......@@ -249,6 +251,9 @@ cb_rstto_open_files (RsttoOpenFiles *rof)
/* Point the iterator to the correct image */
rstto_image_list_iter_find_file (iter, r_file);
g_object_unref (r_file);
r_file = NULL;
}
}
return FALSE;
......
......@@ -2757,7 +2757,7 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
GSList *files = NULL, *_files_iter;
GValue current_uri_val = {0, };
GtkFileFilter *filter;
RsttoFile *rfile;
RsttoFile *r_file = NULL;
g_value_init (&current_uri_val, G_TYPE_STRING);
g_object_get_property (G_OBJECT(window->priv->settings_manager), "current-uri", &current_uri_val);
......@@ -2806,23 +2806,29 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
file = _files_iter->data;
if (g_file_query_exists (file, NULL) )
{
if (rstto_image_list_add_file (window->priv->image_list, rstto_file_new(file), NULL) == FALSE)
r_file = rstto_file_new (file);
if (NULL != r_file)
{
err_dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
_("Could not open file"));
gtk_dialog_run(GTK_DIALOG(err_dialog));
gtk_widget_destroy(err_dialog);
}
else
{
/* Add a reference to the file, it is owned by the
* sourcefunc and will be unref-ed by it.
*/
g_object_ref (file);
g_idle_add_full(G_PRIORITY_LOW, (GSourceFunc) rstto_main_window_add_file_to_recent_files, file, NULL);
if (rstto_image_list_add_file (window->priv->image_list, r_file, NULL) == FALSE)
{
err_dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
_("Could not open file"));
gtk_dialog_run(GTK_DIALOG(err_dialog));
gtk_widget_destroy(err_dialog);
}
else
{
/* Add a reference to the file, it is owned by the
* sourcefunc and will be unref-ed by it.
*/
g_object_ref (file);
g_idle_add_full(G_PRIORITY_LOW, (GSourceFunc) rstto_main_window_add_file_to_recent_files, file, NULL);
}
g_object_unref (G_OBJECT (r_file));
r_file = NULL;
}
}
......@@ -2838,8 +2844,7 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
{
if (g_file_query_exists (files->data, NULL) )
{
rfile = rstto_file_new (files->data);
g_object_ref (rfile);
r_file = rstto_file_new (files->data);
p_file = g_file_get_parent (files->data);
rstto_image_list_set_directory (
......@@ -2848,7 +2853,9 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
NULL );
rstto_image_list_iter_find_file (
window->priv->iter,
rfile );
r_file );
g_object_unref (r_file);
}
}
}
......@@ -2883,22 +2890,26 @@ cb_rstto_main_window_open_recent(GtkRecentChooser *chooser, RsttoMainWindow *win
GError *error = NULL;
GFile *file = g_file_new_for_uri (uri);
GFile *p_file;
RsttoFile *rfile;
RsttoFile *r_file = NULL;
if ((error == NULL) &&
(g_file_query_exists (file, NULL)))
{
rfile = rstto_file_new (file);
g_object_ref (rfile);
p_file = g_file_get_parent (file);
rstto_image_list_set_directory (
window->priv->image_list,
p_file,
NULL);
rstto_image_list_iter_find_file (
window->priv->iter,
rfile );
r_file = rstto_file_new (file);
if ( NULL != r_file )
{
p_file = g_file_get_parent (file);
rstto_image_list_set_directory (
window->priv->image_list,
p_file,
NULL);
rstto_image_list_iter_find_file (
window->priv->iter,
r_file );
g_object_unref (G_OBJECT (r_file));
r_file = NULL;
}
}
else
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment