From 05250dc8a68ca704386e997ad1da3227d1b87a05 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Fri, 10 Jan 2020 11:04:38 -0300 Subject: [PATCH] Refactoring plugin engine. --- configure.ac | 2 +- src/include/pw3270/application.h | 3 +-- src/objects/application/application.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index d79c96a..af4d686 100644 --- a/configure.ac +++ b/configure.ac @@ -86,7 +86,7 @@ dnl INSTALL_PACKAGES="windows-lib ${INSTALL_PACKAGES}" *) app_cv_osname="linux" - CFLAGS="$CFLAGS -pthread" + CFLAGS="$CFLAGS -pthread -DLIBDIR=\$(libdir)" LDFLAGS="$LDFLAGS -pthread" dnl INSTALL_PACKAGES="linux-lib ${INSTALL_PACKAGES}" diff --git a/src/include/pw3270/application.h b/src/include/pw3270/application.h index e348c6d..3ff1fc1 100644 --- a/src/include/pw3270/application.h +++ b/src/include/pw3270/application.h @@ -69,8 +69,7 @@ void pw3270_application_set_ui_style(GApplication *app, PW3270_UI_STYLE type); PW3270_UI_STYLE pw3270_application_get_ui_style(GApplication *app); -// gboolean pw3270_settings_set_int(const gchar *key, gint value); - + void pw3270_application_plugin_foreach(GApplication *app, GFunc func, gpointer user_data); // Tools GtkBuilder * pw3270_application_get_builder(const gchar *name); diff --git a/src/objects/application/application.c b/src/objects/application/application.c index ac09a29..4ba66d9 100644 --- a/src/objects/application/application.c +++ b/src/objects/application/application.c @@ -54,6 +54,8 @@ GSettings * settings; + GSList * plugins; ///< @brief Handlers of the loaded plugins. + PW3270_UI_STYLE ui_style; }; @@ -176,12 +178,78 @@ g_settings_bind(app->settings, "ui-style", app, "ui-style", G_SETTINGS_BIND_DEFAULT); } + // Get plugins. + { +#ifdef _WIN32 + UINT errorMode; + lib3270_autoptr(char) path = lib3270_build_data_filename("plugins",NULL); +#else + const gchar * path = G_STRINGIFY(LIBDIR) G_DIR_SEPARATOR_S G_STRINGIFY(PRODUCT_NAME) "-plugins"; +#endif // _WIN32 + + if(g_file_test(path,G_FILE_TEST_IS_DIR)) { + + g_message("Loading plugins from %s",path); + + GError * err = NULL; + GDir * dir = g_dir_open(path,0,&err); + + if(dir) { + + const gchar *name; + while((name = g_dir_read_name(dir)) != NULL) { + + g_autofree gchar *filename = g_build_filename(path,name,NULL); + + if(g_str_has_suffix(filename,G_MODULE_SUFFIX)) { + + g_message("Loading %s",filename); + + GModule *handle = g_module_open(filename,G_MODULE_BIND_LOCAL); + + if(handle) { + + app->plugins = g_slist_append(app->plugins,handle); + + } else { + + g_warning("Can't load %s: %s",filename,g_module_error()); + + } + + } + + + + } + + g_dir_close(dir); + } + + if(err) { + + g_warning("Can't load plugins from %s: %s",path,err->message); + g_error_free(err); + + } + + + } + + } + } + static void finalize(GObject *object) { pw3270Application * application = PW3270_APPLICATION(object); + if(application->plugins) { + g_slist_free_full(application->plugins,(GDestroyNotify) g_module_close); + application->plugins = NULL; + } + if(application->settings) { g_object_unref(application->settings); application->settings = NULL; @@ -320,3 +388,10 @@ return PW3270_APPLICATION(app)->settings; } + + void pw3270_application_plugin_foreach(GApplication *app, GFunc func, gpointer user_data) { + + g_return_if_fail(PW3270_IS_APPLICATION(app)); + g_slist_foreach(PW3270_APPLICATION(app)->plugins, func, user_data); + + } -- libgit2 0.21.2