Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created March 31, 2022 12:16
Show Gist options
  • Save feliwir/7fbb8b8d6e6a782abee41f85e85d7ae3 to your computer and use it in GitHub Desktop.
Save feliwir/7fbb8b8d6e6a782abee41f85e85d7ae3 to your computer and use it in GitHub Desktop.
static void
overwrite_dialog_response_cb (GtkDialog *dialog, int response, GTask *task)
{
gboolean overwrite;
overwrite = response == GTK_RESPONSE_OK;
GError *error = NULL;
GFile *file = g_task_get_task_data (task);
if (overwrite) {
g_file_delete (file, NULL, &error);
if (error != NULL) {
GtkWidget *err_dialog = _gtk_error_dialog_new (
GTK_WINDOW (&dialog->window),
GTK_DIALOG_MODAL,
NULL,
_ ("Could not delete the old archive."),
"%s",
error->message);
g_signal_connect (GTK_MESSAGE_DIALOG (err_dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (err_dialog);
g_error_free (error);
g_task_return_pointer (task, file, g_object_unref);
} else {
g_task_return_error (task, error);
}
}
gtk_widget_destroy (GTK_WIDGET (dialog));
}
void
fr_new_archive_dialog_get_file_async (FrNewArchiveDialog *self,
const char **mime_type,
GAsyncReadyCallback callback)
{
const char *basename;
int n_format;
char *basename_ext;
GFile *parent;
GFile *file;
GError *error = NULL;
const char *file_mime_type;
GFileInfo *parent_info;
GtkWidget *dialog;
GTask *task;
/* Check whether the user entered a valid archive name. */
basename = gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("filename_entry")));
if ((basename == NULL) || (*basename == '\0')) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
_("You have to specify an archive name."));
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
return;
}
/* file */
n_format = get_selected_format (self);
parent = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (GET_WIDGET ("parent_filechooserbutton")));
if (parent == NULL) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
_("You have to specify an archive name."));
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
return;
}
basename_ext = g_strconcat (basename, mime_type_desc[n_format].default_ext, NULL);
file = g_file_get_child_for_display_name (parent, basename_ext, &error);
if (file == NULL) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
error->message);
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
g_error_free (error);
g_free (basename_ext);
g_object_unref (parent);
return;
}
g_free (basename_ext);
/* mime type */
file_mime_type = mime_type_desc[n_format].mime_type;
if (mime_type != NULL)
*mime_type = file_mime_type;
/* check permissions */
parent_info = g_file_query_info (parent,
(G_FILE_ATTRIBUTE_ACCESS_CAN_READ ","
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE ","
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE","
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME),
0,
NULL,
&error);
g_object_unref (parent);
if (error != NULL) {
g_warning ("Failed to get permission for extraction dir: %s", error->message);
g_clear_error (&error);
g_object_unref (parent_info);
g_object_unref (file);
return;
}
if (g_file_info_has_attribute (parent_info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) &&
! g_file_info_get_attribute_boolean (parent_info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE)) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
_("You don’t have permission to create an archive in this folder"));
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
g_object_unref (parent_info);
g_object_unref (file);
return;
}
/* check whehter the file is equal to the original file */
if ((self->original_file != NULL) && (g_file_equal (file, self->original_file))) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
_("New name is the same as old one, please type other name."));
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
g_object_unref (parent_info);
g_object_unref (file);
return;
}
/* check whether the file is included in the files to add */
{
GList *scan;
for (scan = self->files_to_add; scan; scan = scan->next) {
if (_g_file_cmp_uris (G_FILE (scan->data), file) == 0) {
dialog = _gtk_error_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
NULL,
_("Could not create the archive"),
"%s",
_("You can’t add an archive to itself."));
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (dialog);
g_object_unref (parent_info);
g_object_unref (file);
return;
}
}
}
/* overwrite confirmation */
if (g_file_query_exists (file, NULL)) {
char *filename;
char *message;
char *secondary_message;
filename = _g_file_get_display_basename (file);
message = g_strdup_printf (_("A file named “%s” already exists. Do you want to replace it?"), filename);
secondary_message = g_strdup_printf (_("The file already exists in “%s”. Replacing it will overwrite its contents."), g_file_info_get_display_name (parent_info));
dialog = _gtk_message_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL,
message,
secondary_message,
_GTK_LABEL_CANCEL, GTK_RESPONSE_CANCEL,
_("_Replace"), GTK_RESPONSE_OK,
NULL);
task = g_task_new(self, NULL, callback, file);
g_task_set_task_data(task, file, (GDestroyNotify)g_clear_object);
g_signal_connect (GTK_MESSAGE_DIALOG (dialog), "response", G_CALLBACK (overwrite_dialog_response_cb), task);
gtk_widget_show (dialog);
g_free (secondary_message);
g_free (message);
g_free (filename);
}
g_object_unref (parent_info);
}
void fr_new_archive_dialog_get_file_async (FrNewArchiveDialog *dialog,
const char **mime_type,
GAsyncReadyCallback callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment