Skip to content

Commit

Permalink
Use RAII for GtkFileChooserNative
Browse files Browse the repository at this point in the history
  • Loading branch information
jhilmer committed Apr 7, 2023
1 parent 645e548 commit f27908a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/core/plugin/luapi_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,34 @@ static int applib_glib_rename(lua_State* L) {
* local filename = app.saveAs("foo") -- suggests "foo" as filename
*/
static int applib_saveAs(lua_State* L) {
GtkFileChooserNative* native;
gint res;
int args_returned = 0; // change to 1 if user chooses file

const char* filename = luaL_checkstring(L, -1);

// Create a 'Save As' native dialog
native = gtk_file_chooser_native_new(_("Save file"), nullptr, GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr);
xoj::util::GObjectSPtr<GtkFileChooserNative> native(

This comment has been minimized.

Copy link
@bhennion

bhennion Apr 7, 2023

Contributor

I think that a GtkFileChooserNative should be destroyed via gtk_native_dialog_destroy and not g_object_unref.

You can probably simply use a std::unique_ptr<GtkNativeFileChooser, void(GtkNativeFileChooser*)> with the right custom destructor.

This comment has been minimized.

Copy link
@jhilmer

jhilmer Apr 7, 2023

Author Contributor

In the documentation https://docs.gtk.org/gtk3/class.FileChooserNative.html they use g_object_unref.

This comment has been minimized.

Copy link
@bhennion

bhennion Apr 7, 2023

Contributor

Right.. I'm confused about what gtk_native_dialog_destroy is for then. Anyway, leave it like that

gtk_file_chooser_native_new(_("Save file"), nullptr, GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr),
xoj::util::adopt);

// If user tries to overwrite a file, ask if it's OK
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(native), TRUE);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(native.get()), TRUE);
// Offer a suggestion for the filename if filename absent
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(native),
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(native.get()),
filename ? filename : (std::string{_("Untitled")}).c_str());

// Wait until user responds to dialog
res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native));
res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native.get()));

// Return the filename chosen to lua
if (res == GTK_RESPONSE_ACCEPT) {
char* filename = static_cast<char*>(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native)));
char* filename = static_cast<char*>(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native.get())));

lua_pushlstring(L, filename, strlen(filename));
g_free(static_cast<gchar*>(filename));
args_returned = 1;
}

// Destroy the dialog and free memory
g_object_unref(native);

return args_returned;
}

Expand All @@ -147,8 +145,9 @@ static int applib_saveAs(lua_State* L) {
* path = app.getFilePath({'*.bmp', '*.png'})
*/
static int applib_getFilePath(lua_State* L) {
GtkFileChooserNative* native =
gtk_file_chooser_native_new(_("Open file"), nullptr, GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr);
xoj::util::GObjectSPtr<GtkFileChooserNative> native(
gtk_file_chooser_native_new(_("Open file"), nullptr, GTK_FILE_CHOOSER_ACTION_OPEN, nullptr, nullptr),
xoj::util::adopt);
gint res;
int args_returned = 0; // change to 1 if user chooses file
char* filename;
Expand All @@ -171,20 +170,19 @@ static int applib_getFilePath(lua_State* L) {
GtkFileFilter* filterSupported = gtk_file_filter_new();
gtk_file_filter_set_name(filterSupported, _("Supported files"));
for (std::string format: formats) gtk_file_filter_add_pattern(filterSupported, format.c_str());
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(native), filterSupported);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(native.get()), filterSupported);
}

// Wait until user responds to dialog
res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native));
res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native.get()));
// Return the filename chosen to lua
if (res == GTK_RESPONSE_ACCEPT) {
filename = static_cast<char*>(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native)));
filename = static_cast<char*>(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native.get())));
lua_pushlstring(L, filename, strlen(filename));
g_free(static_cast<gchar*>(filename));
args_returned = 1;
}
// Destroy the dialog and free memory
g_object_unref(native);
return args_returned;
}

Expand Down

0 comments on commit f27908a

Please sign in to comment.