static void add_file_to_dialog (gpointer data, gpointer user_data) { CopyThemeDialogPrivate *priv; GFile *file; gchar *basename = NULL, *raw_basename; priv = COPY_THEME_DIALOG (user_data)->priv; file = G_FILE (data); raw_basename = g_file_get_basename (file); if (g_str_has_suffix (raw_basename, ".desktop")) { /* FIXME: validate key file? */ basename = g_strndup (raw_basename, /* 8 = strlen (".desktop") */ strlen (raw_basename) - 8); } g_free (raw_basename); if (basename) { g_object_ref (file); priv->all_files = g_slist_append (priv->all_files, file); priv->all_basenames = g_slist_append (priv->all_basenames, basename); priv->total_files++; } else { GtkWidget *dialog; gchar *uri; dialog = gtk_message_dialog_new (GTK_WINDOW (user_data), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Invalid screensaver theme")); uri = g_file_get_uri (file); gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), _("%s does not appear to be a valid screensaver theme."), uri); g_free (uri); gtk_window_set_title (GTK_WINDOW (dialog), ""); gtk_window_set_icon_name (GTK_WINDOW (dialog), "preferences-desktop-screensaver"); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); } }
static void copy_theme_dialog_finalize (GObject *obj) { CopyThemeDialog *dlg = COPY_THEME_DIALOG (obj); g_object_unref (dlg->priv->theme_dir); g_slist_foreach (dlg->priv->all_files, (GFunc) (g_object_unref), NULL); g_slist_free (dlg->priv->all_files); g_slist_foreach (dlg->priv->all_basenames, (GFunc) (g_free), NULL); g_slist_free (dlg->priv->all_basenames); g_object_unref (dlg->priv->cancellable); if (parent_class->finalize) parent_class->finalize (G_OBJECT (dlg)); }
static gboolean timeout_display_dialog (gpointer data) { if (IS_COPY_THEME_DIALOG (data)) { CopyThemeDialog *dialog = COPY_THEME_DIALOG (data); if (!copy_finished (dialog)) { gtk_widget_show (GTK_WIDGET (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (copy_theme_dialog_response), dialog); } } return FALSE; }
static void theme_installer_run (GtkWidget *prefs_dialog, GList *files) { GtkWidget *copy_dialog; copy_dialog = copy_theme_dialog_new (files); g_list_foreach (files, (GFunc) (g_object_unref), NULL); g_list_free (files); gtk_window_set_transient_for (GTK_WINDOW (copy_dialog), GTK_WINDOW (prefs_dialog)); gtk_window_set_icon_name (GTK_WINDOW (copy_dialog), "preferences-desktop-screensaver"); g_signal_connect (copy_dialog, "complete", G_CALLBACK (theme_copy_complete_cb), NULL); copy_theme_dialog_begin (COPY_THEME_DIALOG (copy_dialog)); }
GtkWidget* copy_theme_dialog_new (GList *files) { GtkWidget *dialog; CopyThemeDialogPrivate *priv; dialog = GTK_WIDGET (g_object_new (COPY_THEME_DIALOG_TYPE, NULL)); priv = COPY_THEME_DIALOG (dialog)->priv; priv->index = 0; priv->total_files = 0; priv->all_files = NULL; priv->all_basenames = NULL; g_list_foreach (files, add_file_to_dialog, dialog); priv->file = priv->all_files; priv->basename = priv->all_basenames; return dialog; }
static void copy_theme_dialog_response (GtkDialog *dialog, gint response_id) { g_cancellable_cancel (COPY_THEME_DIALOG (dialog)->priv->cancellable); }
static void single_copy_complete (GObject *source_object, GAsyncResult *res, gpointer user_data) { GError *error = NULL; gboolean should_continue = FALSE; CopyThemeDialog *dialog = COPY_THEME_DIALOG (user_data); if (g_file_copy_finish (G_FILE (source_object), res, &error)) { should_continue = TRUE; } else { /* If the file already exists, generate a new random name * and try again. **/ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) { GFile *file, *destination; gchar *basename, *full_basename; g_error_free (error); file = G_FILE (dialog->priv->file->data); basename = (gchar *) (dialog->priv->basename->data); g_return_if_fail (file != NULL); g_return_if_fail (basename != NULL); full_basename = g_strdup_printf ("%s-%u.desktop", basename, g_random_int ()); destination = g_file_get_child (dialog->priv->theme_dir, full_basename); g_free (full_basename); g_file_copy_async (file, destination, G_FILE_COPY_NONE, G_PRIORITY_DEFAULT, dialog->priv->cancellable, NULL, NULL, single_copy_complete, dialog); } else { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { /* User has cancelled the theme copy */ g_signal_emit (G_OBJECT (dialog), signals[CANCELLED], 0, NULL); } else { /* Some other error occurred, ignore and * try to copy remaining files **/ should_continue = TRUE; } g_error_free (error); } } /* Update informational widgets and, if needed, signal * copy completion. **/ if (should_continue) { dialog->priv->index++; dialog->priv->file = dialog->priv->file->next; dialog->priv->basename = dialog->priv->basename->next; copy_theme_dialog_update_num_files (dialog); copy_theme_dialog_copy_next (dialog); } }