constraint-editor: Port to async dialog API

This commit is contained in:
Matthias Clasen 2022-10-28 20:24:53 -04:00
parent ad2552ee43
commit d620197ae1

View File

@ -202,43 +202,36 @@ constraint_editor_window_load (ConstraintEditorWindow *self,
} }
static void static void
open_response_cb (GtkNativeDialog *dialog, open_response_cb (GObject *source,
int response, GAsyncResult *result,
ConstraintEditorWindow *self) void *user_data)
{ {
gtk_native_dialog_hide (dialog); GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
ConstraintEditorWindow *self = user_data;
GFile *file;
if (response == GTK_RESPONSE_ACCEPT) file = gtk_file_dialog_open_finish (dialog, result, NULL);
if (file)
{ {
GFile *file;
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
constraint_editor_window_load (self, file); constraint_editor_window_load (self, file);
g_object_unref (file); g_object_unref (file);
} }
gtk_native_dialog_destroy (dialog);
} }
static void static void
open_cb (GtkWidget *button, open_cb (GtkWidget *button,
ConstraintEditorWindow *self) ConstraintEditorWindow *self)
{ {
GtkFileChooserNative *dialog; GtkFileDialog *dialog;
GFile *cwd;
dialog = gtk_file_chooser_native_new ("Open file", dialog = gtk_file_dialog_new ();
GTK_WINDOW (self), gtk_file_dialog_set_title (dialog, "Open file");
GTK_FILE_CHOOSER_ACTION_OPEN, cwd = g_file_new_for_path (".");
"_Load", gtk_file_dialog_set_current_folder (dialog, cwd);
"_Cancel");
gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd); g_object_unref (cwd);
gtk_file_dialog_open (dialog, GTK_WINDOW (self), NULL, NULL, open_response_cb, self);
g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self); g_object_unref (dialog);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
} }
static void static void
@ -294,22 +287,23 @@ serialize_model (GListModel *list)
static void static void
save_response_cb (GtkNativeDialog *dialog, save_response_cb (GObject *source,
int response, GAsyncResult *result,
ConstraintEditorWindow *self) void *user_data)
{ {
gtk_native_dialog_hide (dialog); GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
ConstraintEditorWindow *self = user_data;
GFile *file;
if (response == GTK_RESPONSE_ACCEPT) file = gtk_file_dialog_save_finish (dialog, result, NULL);
if (file)
{ {
GListModel *model; GListModel *model;
GFile *file;
char *text; char *text;
GError *error = NULL; GError *error = NULL;
model = constraint_view_get_model (CONSTRAINT_VIEW (self->view)); model = constraint_view_get_model (CONSTRAINT_VIEW (self->view));
text = serialize_model (model); text = serialize_model (model);
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
g_file_replace_contents (file, text, strlen (text), g_file_replace_contents (file, text, strlen (text),
NULL, FALSE, NULL, FALSE,
G_FILE_CREATE_NONE, G_FILE_CREATE_NONE,
@ -318,46 +312,39 @@ save_response_cb (GtkNativeDialog *dialog,
&error); &error);
if (error != NULL) if (error != NULL)
{ {
GtkWidget *message_dialog; GtkAlertDialog *alert;
message_dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self))), alert = gtk_alert_dialog_new ("Saving failed");
GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, gtk_alert_dialog_set_detail (alert, error->message);
GTK_MESSAGE_INFO, gtk_alert_dialog_show (alert,
GTK_BUTTONS_OK, GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self))));
"Saving failed"); g_object_unref (alert);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
"%s", error->message);
g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_widget_show (message_dialog);
g_error_free (error); g_error_free (error);
} }
g_free (text); g_free (text);
g_object_unref (file); g_object_unref (file);
} }
gtk_native_dialog_destroy (dialog);
} }
static void static void
save_cb (GtkWidget *button, save_cb (GtkWidget *button,
ConstraintEditorWindow *self) ConstraintEditorWindow *self)
{ {
GtkFileChooserNative *dialog; GtkFileDialog *dialog;
GFile *cwd;
dialog = gtk_file_chooser_native_new ("Save constraints", dialog = gtk_file_dialog_new ();
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))), gtk_file_dialog_set_title (dialog, "Save constraints");
GTK_FILE_CHOOSER_ACTION_SAVE, cwd = g_file_new_for_path (".");
"_Save", gtk_file_dialog_set_current_folder (dialog, cwd);
"_Cancel");
gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd); g_object_unref (cwd);
gtk_file_dialog_save (dialog,
g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self); GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog)); NULL, NULL,
NULL,
save_response_cb, self);
g_object_unref (dialog);
} }
static void static void