Commit 2a6c090b authored by Cosimo Cecchi's avatar Cosimo Cecchi
Browse files

Use g_autofree/g_autoptr to simplify code

parent afb3e379
......@@ -36,8 +36,7 @@
int
main (int argc, char *argv[])
{
gint result;
ScreenshotApplication *app;
g_autoptr(GApplication) app;
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
......@@ -45,8 +44,5 @@ main (int argc, char *argv[])
textdomain (GETTEXT_PACKAGE);
app = screenshot_application_new ();
result = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return result;
return g_application_run (app, argc, argv);
}
......@@ -60,22 +60,18 @@ struct _ScreenshotApplicationPriv {
static void
save_folder_to_settings (ScreenshotApplication *self)
{
char *folder;
folder = screenshot_dialog_get_folder (self->priv->dialog);
g_autofree gchar *folder = screenshot_dialog_get_folder (self->priv->dialog);
g_settings_set_string (screenshot_config->settings,
LAST_SAVE_DIRECTORY_KEY, folder);
g_free (folder);
}
static void
set_recent_entry (ScreenshotApplication *self)
{
char *app_exec = NULL;
g_autofree gchar *app_exec = NULL;
g_autoptr(GAppInfo) app = NULL;
GtkRecentManager *recent;
GtkRecentData recent_data;
GAppInfo *app;
const char *exec_name = NULL;
static char * groups[2] = { "Graphics", NULL };
......@@ -100,9 +96,6 @@ set_recent_entry (ScreenshotApplication *self)
recent_data.is_private = FALSE;
gtk_recent_manager_add_full (recent, self->priv->save_uri, &recent_data);
g_object_unref (app);
g_free (app_exec);
}
static void
......@@ -142,25 +135,18 @@ save_pixbuf_handle_error (ScreenshotApplication *self,
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS) &&
!self->priv->should_overwrite)
{
gchar *folder = screenshot_dialog_get_folder (dialog);
gchar *folder_uri = g_path_get_basename (folder);
gchar *folder_name = g_uri_unescape_string (folder_uri, NULL);
gchar *file_name = screenshot_dialog_get_filename (dialog);
gchar *detail = g_strdup_printf (_("A file named “%s” already exists in “%s”"),
file_name, folder_name);
gint response;
response = screenshot_show_dialog (GTK_WINDOW (dialog->dialog),
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
_("Overwrite existing file?"),
detail);
g_free (folder);
g_free (folder_uri);
g_free (folder_name);
g_free (file_name);
g_free (detail);
g_autofree gchar *folder = screenshot_dialog_get_folder (dialog);
g_autofree gchar *folder_uri = g_path_get_basename (folder);
g_autofree gchar *folder_name = g_uri_unescape_string (folder_uri, NULL);
g_autofree gchar *file_name = screenshot_dialog_get_filename (dialog);
g_autofree gchar *detail = g_strdup_printf (_("A file named “%s” already exists in “%s”"),
file_name, folder_name);
gint response = screenshot_show_dialog (GTK_WINDOW (dialog->dialog),
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
_("Overwrite existing file?"),
detail);
if (response == GTK_RESPONSE_YES)
{
......@@ -196,7 +182,7 @@ save_pixbuf_ready_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
g_autoptr(GError) error = NULL;
ScreenshotApplication *self = user_data;
gdk_pixbuf_save_to_stream_finish (res, &error);
......@@ -204,7 +190,6 @@ save_pixbuf_ready_cb (GObject *source,
if (error != NULL)
{
save_pixbuf_handle_error (self, error);
g_error_free (error);
return;
}
......@@ -217,29 +202,20 @@ find_out_writable_format_by_extension (gpointer data,
{
GdkPixbufFormat *format = (GdkPixbufFormat*) data;
gchar **name = (gchar **) user_data;
gchar **extensions = gdk_pixbuf_format_get_extensions (format);
g_auto(GStrv) extensions = gdk_pixbuf_format_get_extensions (format);
gchar **ptr = extensions;
gboolean found = FALSE;
while (*ptr != NULL)
{
if (g_strcmp0 (*ptr, *name) == 0 &&
gdk_pixbuf_format_is_writable (format) == TRUE)
{
g_free (*name);
*name = gdk_pixbuf_format_get_name (format);
found = TRUE;
break;
}
ptr++;
}
g_strfreev (extensions);
/* Needing to duplicate string here because
* gdk_pixbuf_format_get_name will return a duplicated string.
*/
if (!found)
*name = g_strdup (*name);
}
static gboolean
......@@ -304,11 +280,11 @@ save_file_create_ready_cb (GObject *source,
gpointer user_data)
{
ScreenshotApplication *self = user_data;
GFileOutputStream *os;
GError *error = NULL;
gchar *basename = g_file_get_basename (G_FILE (source));
g_autoptr(GFileOutputStream) os = NULL;
g_autoptr(GError) error = NULL;
g_autofree gchar *basename = g_file_get_basename (G_FILE (source));
g_autofree gchar *format = NULL;
gchar *extension = g_strrstr (basename, ".");
gchar *format = NULL;
GSList *formats = NULL;
if (extension == NULL)
......@@ -316,14 +292,13 @@ save_file_create_ready_cb (GObject *source,
else
extension++;
format = extension;
format = g_strdup (extension);
formats = gdk_pixbuf_get_formats();
g_slist_foreach (formats,
find_out_writable_format_by_extension,
(gpointer) &format);
g_slist_free (formats);
g_free (basename);
if (self->priv->should_overwrite)
os = g_file_replace_finish (G_FILE (source), res, &error);
......@@ -333,7 +308,6 @@ save_file_create_ready_cb (GObject *source,
if (error != NULL)
{
save_pixbuf_handle_error (self, error);
g_error_free (error);
return;
}
......@@ -348,15 +322,12 @@ save_file_create_ready_cb (GObject *source,
{
save_with_no_profile_or_description (self, os, format);
}
g_object_unref (os);
g_free (format);
}
static void
screenshot_save_to_file (ScreenshotApplication *self)
{
GFile *target_file;
g_autoptr(GFile) target_file = NULL;
if (self->priv->dialog != NULL)
screenshot_dialog_set_busy (self->priv->dialog, TRUE);
......@@ -380,8 +351,6 @@ screenshot_save_to_file (ScreenshotApplication *self)
NULL,
save_file_create_ready_cb, self);
}
g_object_unref (target_file);
}
static void
......@@ -431,19 +400,13 @@ build_filename_ready_cb (GObject *source,
gpointer user_data)
{
ScreenshotApplication *self = user_data;
GError *error = NULL;
char *save_path;
g_autoptr(GError) error = NULL;
g_autofree gchar *save_path = screenshot_build_filename_finish (res, &error);
save_path = screenshot_build_filename_finish (res, &error);
if (save_path != NULL)
{
GFile *file;
file = g_file_new_for_path (save_path);
g_free (save_path);
g_autoptr(GFile) file = g_file_new_for_path (save_path);
self->priv->save_uri = g_file_get_uri (file);
g_object_unref (file);
}
else
self->priv->save_uri = NULL;
......@@ -455,7 +418,6 @@ build_filename_ready_cb (GObject *source,
{
g_critical ("Impossible to find a valid location to save the screenshot: %s",
error->message);
g_error_free (error);
if (screenshot_config->interactive)
screenshot_show_dialog (NULL,
......@@ -828,8 +790,8 @@ static GActionEntry action_entries[] = {
static void
screenshot_application_startup (GApplication *app)
{
GMenuModel *menu;
GtkBuilder *builder;
g_autoptr(GMenuModel) menu = NULL;
g_autoptr(GtkBuilder) builder = NULL;
ScreenshotApplication *self = SCREENSHOT_APPLICATION (app);
G_APPLICATION_CLASS (screenshot_application_parent_class)->startup (app);
......@@ -846,9 +808,6 @@ screenshot_application_startup (GApplication *app)
gtk_builder_add_from_resource (builder, "/org/gnome/screenshot/screenshot-app-menu.ui", NULL);
menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
gtk_application_set_app_menu (GTK_APPLICATION (app), menu);
g_object_unref (builder);
g_object_unref (menu);
}
static void
......@@ -904,7 +863,7 @@ screenshot_application_init (ScreenshotApplication *self)
g_application_add_main_option_entries (G_APPLICATION (self), entries);
}
ScreenshotApplication *
GApplication *
screenshot_application_new (void)
{
return g_object_new (SCREENSHOT_TYPE_APPLICATION,
......
......@@ -49,6 +49,6 @@ typedef struct {
} ScreenshotApplicationClass;
GType screenshot_application_get_type (void);
ScreenshotApplication * screenshot_application_new (void);
GApplication * screenshot_application_new (void);
#endif /* __SCREENSHOT_APPLICATION_H__ */
......@@ -226,8 +226,8 @@ emit_select_callback_in_idle (gpointer user_data)
static void
screenshot_select_area_x11_async (CallbackData *cb_data)
{
g_autoptr(GdkCursor) cursor = NULL;
GdkDisplay *display;
GdkCursor *cursor;
select_area_filter_data data;
GdkDeviceManager *manager;
GdkDevice *pointer, *keyboard;
......@@ -260,10 +260,7 @@ screenshot_select_area_x11_async (CallbackData *cb_data)
cursor, GDK_CURRENT_TIME);
if (res != GDK_GRAB_SUCCESS)
{
g_object_unref (cursor);
goto out;
}
goto out;
res = gdk_device_grab (keyboard, gtk_widget_get_window (data.window),
GDK_OWNERSHIP_NONE, FALSE,
......@@ -273,7 +270,6 @@ screenshot_select_area_x11_async (CallbackData *cb_data)
if (res != GDK_GRAB_SUCCESS)
{
gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
g_object_unref (cursor);
goto out;
}
......@@ -283,8 +279,6 @@ screenshot_select_area_x11_async (CallbackData *cb_data)
gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
gtk_widget_destroy (data.window);
g_object_unref (cursor);
gdk_flush ();
out:
......@@ -304,15 +298,14 @@ select_area_done (GObject *source_object,
gpointer user_data)
{
CallbackData *cb_data = user_data;
GError *error = NULL;
GVariant *ret;
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) ret = NULL;
ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, &error);
if (error != NULL)
{
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_error_free (error);
cb_data->aborted = TRUE;
g_idle_add (emit_select_callback_in_idle, cb_data);
return;
......@@ -320,7 +313,6 @@ select_area_done (GObject *source_object,
g_message ("Unable to select area using GNOME Shell's builtin screenshot "
"interface, resorting to fallback X11.");
g_error_free (error);
screenshot_select_area_x11_async (cb_data);
return;
......@@ -331,7 +323,6 @@ select_area_done (GObject *source_object,
&cb_data->rectangle.y,
&cb_data->rectangle.width,
&cb_data->rectangle.height);
g_variant_unref (ret);
g_idle_add (emit_select_callback_in_idle, cb_data);
}
......
......@@ -202,14 +202,12 @@ screenshot_dialog_new (GdkPixbuf *screenshot,
SaveScreenshotCallback f,
gpointer user_data)
{
g_autoptr(GFile) tmp_file = NULL, parent_file = NULL;
g_autoptr(GtkBuilder) ui = NULL;
g_autofree gchar *current_folder = NULL, *current_name = NULL;
ScreenshotDialog *dialog;
GtkBuilder *ui;
char *current_folder;
char *current_name;
char *ext;
gint pos;
GFile *tmp_file;
GFile *parent_file;
guint res;
tmp_file = g_file_new_for_uri (initial_uri);
......@@ -217,8 +215,6 @@ screenshot_dialog_new (GdkPixbuf *screenshot,
current_name = g_file_get_basename (tmp_file);
current_folder = g_file_get_uri (parent_file);
g_object_unref (tmp_file);
g_object_unref (parent_file);
dialog = g_new0 (ScreenshotDialog, 1);
dialog->screenshot = screenshot;
......@@ -266,30 +262,19 @@ screenshot_dialog_new (GdkPixbuf *screenshot,
0,
pos);
g_free (current_name);
g_free (current_folder);
g_object_unref (ui);
return dialog;
}
char *
screenshot_dialog_get_uri (ScreenshotDialog *dialog)
{
gchar *folder, *file;
gchar *uri;
gchar *tmp;
g_autofree gchar *folder = NULL, *file = NULL, *tmp = NULL;
folder = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog->save_widget));
tmp = screenshot_dialog_get_filename (dialog);
file = g_uri_escape_string (tmp, NULL, FALSE);
g_free (tmp);
uri = g_build_filename (folder, file, NULL);
g_free (folder);
g_free (file);
return uri;
return g_build_filename (folder, file, NULL);
}
char *
......@@ -301,21 +286,19 @@ screenshot_dialog_get_folder (ScreenshotDialog *dialog)
char *
screenshot_dialog_get_filename (ScreenshotDialog *dialog)
{
g_autoptr(GError) error = NULL;
const gchar *file_name;
gchar *tmp;
GError *error;
file_name = gtk_entry_get_text (GTK_ENTRY (dialog->filename_entry));
error = NULL;
tmp = g_filename_from_utf8 (file_name, -1, NULL, NULL, &error);
if (error)
if (error != NULL)
{
g_warning ("Unable to convert `%s' to valid UTF-8: %s\n"
"Falling back to default file.",
file_name,
error->message);
g_error_free (error);
tmp = g_strdup (_("Screenshot.png"));
}
......@@ -332,13 +315,12 @@ screenshot_dialog_set_busy (ScreenshotDialog *dialog,
if (busy)
{
g_autoptr(GdkCursor) cursor = NULL;
GdkDisplay *display;
GdkCursor *cursor;
/* Change cursor to busy */
display = gtk_widget_get_display (GTK_WIDGET (dialog));
cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
gdk_window_set_cursor (window, cursor);
g_object_unref (cursor);
}
else
{
......
......@@ -48,7 +48,8 @@ typedef struct
static char *
expand_initial_tilde (const char *path)
{
char *slash_after_user_name, *user_name;
g_autofree gchar *user_name = NULL;
char *slash_after_user_name;
struct passwd *passwd_file_entry;
if (path[1] == '/' || path[1] == '\0') {
......@@ -63,7 +64,6 @@ expand_initial_tilde (const char *path)
slash_after_user_name - &path[1]);
}
passwd_file_entry = getpwnam (user_name);
g_free (user_name);
if (passwd_file_entry == NULL || passwd_file_entry->pw_dir == NULL) {
return g_strdup (path);
......@@ -89,35 +89,26 @@ get_default_screenshot_dir (void)
static gchar *
sanitize_save_directory (const gchar *save_dir)
{
gchar *retval = g_strdup (save_dir);
if (save_dir == NULL)
return NULL;
if (save_dir[0] == '~')
{
char *tmp = expand_initial_tilde (save_dir);
g_free (retval);
retval = tmp;
}
else if (strstr (save_dir, "://") != NULL)
{
GFile *file;
return expand_initial_tilde (save_dir);
g_free (retval);
file = g_file_new_for_uri (save_dir);
retval = g_file_get_path (file);
g_object_unref (file);
if (strstr (save_dir, "://") != NULL)
{
g_autoptr(GFile) file = g_file_new_for_uri (save_dir);
return g_file_get_path (file);
}
return retval;
return g_strdup (save_dir);
}
static char *
build_path (AsyncExistenceJob *job)
{
g_autofree gchar *file_name = NULL, *origin = NULL;
const gchar *base_path, *file_type;
char *retval, *file_name, *origin;
base_path = job->base_paths[job->type];
file_type = screenshot_config->file_type;
......@@ -128,11 +119,8 @@ build_path (AsyncExistenceJob *job)
if (job->screenshot_origin == NULL)
{
GDateTime *d;
d = g_date_time_new_now_local ();
g_autoptr(GDateTime) d = g_date_time_new_now_local ();
origin = g_date_time_format (d, "%Y-%m-%d %H-%M-%S");
g_date_time_unref (d);
}
else
origin = g_strdup (job->screenshot_origin);
......@@ -157,11 +145,7 @@ build_path (AsyncExistenceJob *job)
file_name = g_strdup_printf (_("Screenshot from %s - %d.%s"), origin, job->iteration, file_type);
}
retval = g_build_filename (base_path, file_name, NULL);
g_free (file_name);
g_free (origin);
return retval;
return g_build_filename (base_path, file_name, NULL);
}
static void
......@@ -200,104 +184,56 @@ try_check_file (GTask *task,
GCancellable *cancellable)
{
AsyncExistenceJob *job = data;
GFile *file;
GFileInfo *info;
GError *error;
char *path, *retval;
retry:
error = NULL;
path = build_path (job);
if (path == NULL)
while (TRUE)
{
(job->type)++;
goto retry;
}
g_autoptr(GError) error = NULL;
g_autoptr(GFile) file = NULL;
g_autoptr(GFileInfo) info = NULL;
g_autofree gchar *path = build_path (job);
file = g_file_new_for_path (path);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE, cancellable, &error);
if (info != NULL)
{
/* file already exists, iterate again */
g_object_unref (info);
g_object_unref (file);
g_free (path);
if (path == NULL)
{
(job->type)++;
continue;
}
(job->iteration)++;
file = g_file_new_for_path (path);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE, cancellable, &error);
if (info != NULL)
{
/* file already exists, iterate again */
(job->iteration)++;
continue;
}
goto retry;
}
else
{
/* see the error to check whether the location is not accessible
* or the file does not exist.
*/
if (error->code == G_IO_ERROR_NOT_FOUND)
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
GFile *parent;
g_autoptr(GFile) parent = g_file_get_parent (file);
/* if the parent directory doesn't exist as well, forget the saved
/* if the parent directory doesn't exist as well, we'll forget the saved
* directory and treat this as a generic error.
*/
parent = g_file_get_parent (file);
if (!g_file_query_exists (parent, NULL))
if (g_file_query_exists (parent, NULL))
{
if (!prepare_next_cycle (job))
{
retval = NULL;
g_object_unref (parent);
goto out;
}
g_object_unref (file);
g_object_unref (parent);
goto retry;
}
else
{
retval = path;
g_object_unref (parent);
goto out;
g_task_return_pointer (task, g_steal_pointer (&path), NULL);
return;
}
}
else
{
/* another kind of error, assume this location is not