[tepl] Add tepl_init() and tepl_finalize() public functions



commit 14e5278d48caff52be0c3335441cc78ecfa88554
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 29 15:26:51 2017 +0200

    Add tepl_init() and tepl_finalize() public functions

 docs/reference/tepl-3.0-sections.txt |    7 ++
 docs/reference/tepl-docs.xml.in      |    5 +
 tepl/Makefile.am                     |    1 -
 tepl/gconstructor.h                  |   94 -----------------
 tepl/tepl-init.c                     |  192 +++++++++------------------------
 tepl/tepl-init.h                     |   36 +++++++
 tepl/tepl-metadata-manager.c         |    2 +
 tepl/tepl.h                          |    1 +
 8 files changed, 104 insertions(+), 234 deletions(-)
---
diff --git a/docs/reference/tepl-3.0-sections.txt b/docs/reference/tepl-3.0-sections.txt
index c32705e..7e8958c 100644
--- a/docs/reference/tepl-3.0-sections.txt
+++ b/docs/reference/tepl-3.0-sections.txt
@@ -169,6 +169,13 @@ amtk_utils_recent_chooser_menu_get_item_uri
 <INCLUDE>tepl/tepl.h</INCLUDE>
 
 <SECTION>
+<FILE>init</FILE>
+<TITLE>Tepl Initialization and Finalization</TITLE>
+tepl_init
+tepl_finalize
+</SECTION>
+
+<SECTION>
 <FILE>application</FILE>
 TeplApplication
 tepl_application_get_from_gtk_application
diff --git a/docs/reference/tepl-docs.xml.in b/docs/reference/tepl-docs.xml.in
index 217bc95..ae312b4 100644
--- a/docs/reference/tepl-docs.xml.in
+++ b/docs/reference/tepl-docs.xml.in
@@ -37,6 +37,11 @@
   <part id="tepl-api-reference">
     <title>Tepl API Reference</title>
 
+    <chapter id="general">
+      <title>General</title>
+      <xi:include href="xml/init.xml"/>
+    </chapter>
+
     <chapter id="framework">
       <title>Framework</title>
       <xi:include href="xml/application.xml"/>
diff --git a/tepl/Makefile.am b/tepl/Makefile.am
index e979fdd..07cbd5b 100644
--- a/tepl/Makefile.am
+++ b/tepl/Makefile.am
@@ -55,7 +55,6 @@ tepl_public_c_files =                         \
        tepl-view.c
 
 tepl_private_headers =                         \
-       gconstructor.h                          \
        tepl-buffer-input-stream.h              \
        tepl-encoding-converter.h               \
        tepl-encoding-private.h                 \
diff --git a/tepl/tepl-init.c b/tepl/tepl-init.c
index fe6242a..b1be71f 100644
--- a/tepl/tepl-init.c
+++ b/tepl/tepl-init.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -17,152 +17,66 @@
  * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-/* Init i18n */
+#include "tepl-init.h"
+#include <amtk/amtk.h>
+#include "tepl-metadata-manager.h"
 
-/* Part of the code taken from the GtkSourceView library (gtksourceview-i18n.c).
- * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
+/**
+ * tepl_init:
+ *
+ * Initializes the Tepl library (e.g. for the internationalization).
+ *
+ * This function can be called several times, but is meant to be called at the
+ * beginning of main(), before any other Tepl function call.
+ *
+ * This function also calls amtk_init().
+ *
+ * Since: 3.0
  */
-
-#include "config.h"
-#include <glib/gi18n-lib.h>
-#include "gconstructor.h"
-
-#ifdef G_OS_WIN32
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-static HMODULE tepl_dll;
-#endif
-
-#ifdef OS_OSX
-#include <Cocoa/Cocoa.h>
-
-static gchar *
-dirs_os_x_get_bundle_resource_dir (void)
-{
-       NSAutoreleasePool *pool;
-       gchar *str = NULL;
-       NSString *path;
-
-       pool = [[NSAutoreleasePool alloc] init];
-
-       if ([[NSBundle mainBundle] bundleIdentifier] == nil)
-       {
-               [pool release];
-               return NULL;
-       }
-
-       path = [[NSBundle mainBundle] resourcePath];
-
-       if (!path)
-       {
-               [pool release];
-               return NULL;
-       }
-
-       str = g_strdup ([path UTF8String]);
-       [pool release];
-       return str;
-}
-
-static gchar *
-dirs_os_x_get_locale_dir (void)
-{
-       gchar *res_dir;
-       gchar *ret;
-
-       res_dir = dirs_os_x_get_bundle_resource_dir ();
-
-       if (res_dir == NULL)
-       {
-               ret = g_build_filename (DATADIR, "locale", NULL);
-       }
-       else
-       {
-               ret = g_build_filename (res_dir, "share", "locale", NULL);
-               g_free (res_dir);
-       }
-
-       return ret;
-}
-#endif /* OS_OSX */
-
-static gchar *
-get_locale_dir (void)
-{
-       gchar *locale_dir;
-
-#if defined (G_OS_WIN32)
-       gchar *win32_dir;
-
-       win32_dir = g_win32_get_package_installation_directory_of_module (tepl_dll);
-
-       locale_dir = g_build_filename (win32_dir, "share", "locale", NULL);
-
-       g_free (win32_dir);
-#elif defined (OS_OSX)
-       locale_dir = dirs_os_x_get_locale_dir ();
-#else
-       locale_dir = g_build_filename (DATADIR, "locale", NULL);
-#endif
-
-       return locale_dir;
-}
-
-static void
+void
 tepl_init (void)
 {
-       gchar *locale_dir;
-
-       locale_dir = get_locale_dir ();
-       bindtextdomain (GETTEXT_PACKAGE, locale_dir);
-       g_free (locale_dir);
-
-       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+       amtk_init ();
 }
 
-#if defined (G_OS_WIN32)
-
-BOOL WINAPI DllMain (HINSTANCE hinstDLL,
-                    DWORD     fdwReason,
-                    LPVOID    lpvReserved);
+/**
+ * tepl_finalize:
+ *
+ * Free the resources allocated by Tepl. For example it unrefs the singleton
+ * objects. It also properly shutdowns the metadata manager by calling
+ * tepl_metadata_manager_shutdown().
+ *
+ * This function also calls amtk_finalize().
+ *
+ * It is not mandatory to call this function, it's just to be friendlier to
+ * memory debugging tools (but if you don't call this function and you use the
+ * metadata manager, you should call tepl_metadata_manager_shutdown()). This
+ * function is meant to be called at the end of main(). It can be called several
+ * times.
+ *
+ * Since: 3.0
+ */
 
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-        DWORD     fdwReason,
-        LPVOID    lpvReserved)
+/* Another way is to use a DSO destructor, see gconstructor.h in GLib.
+ *
+ * The advantage of calling tepl_finalize() at the end of main() is that
+ * gobject-list [1] correctly reports that all Tepl* objects have been finalized
+ * when quitting the application. On the other hand a DSO destructor runs after
+ * the gobject-list's last output, so it's much less convenient, see:
+ * https://git.gnome.org/browse/gtksourceview/commit/?id=e761de9c2bee90c232875bbc41e6e73e1f63e145
+ *
+ * [1] A tool for debugging the lifetime of GObjects:
+ * https://github.com/danni/gobject-list
+ */
+void
+tepl_finalize (void)
 {
-       switch (fdwReason)
-       {
-               case DLL_PROCESS_ATTACH:
-                       tepl_dll = hinstDLL;
-                       tepl_init ();
-                       break;
+       static gboolean done = FALSE;
 
-               case DLL_THREAD_DETACH:
-               default:
-                       /* do nothing */
-                       break;
+       if (!done)
+       {
+               amtk_finalize ();
+               tepl_metadata_manager_shutdown ();
+               done = TRUE;
        }
-
-       return TRUE;
 }
-
-#elif defined (G_HAS_CONSTRUCTORS)
-
-#  ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
-#    pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(tepl_constructor)
-#  endif
-G_DEFINE_CONSTRUCTOR (tepl_constructor)
-
-static void
-tepl_constructor (void)
-{
-       tepl_init ();
-}
-
-#else
-#  error Your platform/compiler is missing constructor support
-#endif
-
-/* ex:set ts=8 noet: */
diff --git a/tepl/tepl-init.h b/tepl/tepl-init.h
new file mode 100644
index 0000000..e5e578a
--- /dev/null
+++ b/tepl/tepl-init.h
@@ -0,0 +1,36 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEPL_INIT_H
+#define TEPL_INIT_H
+
+#if !defined (TEPL_H_INSIDE) && !defined (TEPL_COMPILATION)
+#error "Only <tepl/tepl.h> can be included directly."
+#endif
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void   tepl_init               (void);
+void   tepl_finalize           (void);
+
+G_END_DECLS
+
+#endif /* TEPL_INIT_H */
diff --git a/tepl/tepl-metadata-manager.c b/tepl/tepl-metadata-manager.c
index 22bf57d..4ea2eb6 100644
--- a/tepl/tepl-metadata-manager.c
+++ b/tepl/tepl-metadata-manager.c
@@ -153,6 +153,8 @@ tepl_metadata_manager_init (const gchar *metadata_path)
  * This function saves synchronously metadata if they need to be saved, and
  * frees the internal data of the metadata manager.
  *
+ * See also tepl_finalize(), which calls this function.
+ *
  * Since: 1.0
  */
 void
diff --git a/tepl/tepl.h b/tepl/tepl.h
index c59f330..f05f081 100644
--- a/tepl/tepl.h
+++ b/tepl/tepl.h
@@ -38,6 +38,7 @@
 #include <tepl/tepl-fold-region.h>
 #include <tepl/tepl-gutter-renderer-folds.h>
 #include <tepl/tepl-info-bar.h>
+#include <tepl/tepl-init.h>
 #include <tepl/tepl-iter.h>
 #include <tepl/tepl-menu-shell.h>
 #include <tepl/tepl-metadata-manager.h>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]