]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-init.c
Remove some code duplication
[evince.git] / libdocument / ev-init.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  * Copyright © 2009 Christian Persch
4  *
5  * Evince is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
10  * Evince is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <config.h>
21
22 #include <glib.h>
23 #include <glib/gi18n-lib.h>
24 #ifdef G_OS_WIN32
25 #include <windows.h>
26 #endif
27
28 #include "ev-init.h"
29 #include "ev-backends-manager.h"
30 #include "ev-debug.h"
31 #include "ev-file-helpers.h"
32
33 static int ev_init_count;
34
35 #ifdef G_OS_WIN32
36
37 static HMODULE evdocument_dll = NULL;
38 static gchar *locale_dir = NULL;
39
40 #ifdef DLL_EXPORT
41 BOOL WINAPI
42 DllMain (HINSTANCE hinstDLL,
43          DWORD     fdwReason,
44          LPVOID    lpvReserved)
45 {
46         if (fdwReason == DLL_PROCESS_ATTACH)
47                 evdocument_dll = hinstDLL;
48
49         return TRUE;
50 }
51 #endif
52
53 static const gchar *
54 _ev_win32_get_locale_dir (HMODULE module)
55 {
56         gchar *install_dir = NULL, *utf8_locale_dir;
57         gchar *retval = NULL;
58
59         if (evdocument_dll != NULL)
60                 install_dir =
61                 g_win32_get_package_installation_directory_of_module (module);
62
63         if (install_dir) {
64                 utf8_locale_dir = g_build_filename (install_dir,
65                         "share", "locale", NULL);
66
67                 locale_dir = g_win32_locale_filename_from_utf8 (utf8_locale_dir);
68
69                 g_free (install_dir);
70                 g_free (utf8_locale_dir);
71         }
72
73         if (!locale_dir)
74                 locale_dir = g_strdup ("");
75 }
76
77 #endif
78
79 const gchar *
80 ev_get_locale_dir (void)
81 {
82 #ifdef G_OS_WIN32
83         return _ev_win32_get_locale_dir (evdocument_dll);
84 #else
85         return GNOMELOCALEDIR;
86 #endif
87 }
88
89 /**
90  * ev_init:
91  *
92  * Initializes the evince document library, and binds the evince
93  * gettext domain.
94  *
95  * You must call this before calling any other function in the evince
96  * document library.
97  *
98  * Returns: %TRUE if any backends were found; %FALSE otherwise
99  */
100 gboolean
101 ev_init (void)
102 {
103         static gboolean have_backends;
104
105         if (ev_init_count++ > 0)
106                 return have_backends;
107
108         /* set up translation catalog */
109         bindtextdomain (GETTEXT_PACKAGE, ev_get_locale_dir ());
110         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
111
112         _ev_debug_init ();
113         _ev_file_helpers_init ();
114         have_backends = _ev_backends_manager_init ();
115
116         return have_backends;
117 }
118
119 /**
120  * ev_shutdown:
121  *
122  * Shuts the evince document library down.
123  */
124 void
125 ev_shutdown (void)
126 {
127         g_assert (_ev_is_initialized ());
128
129         if (--ev_init_count > 0)
130                 return;
131
132 #ifdef G_OS_WIN32
133         if (locale_dir != NULL)
134                 g_free(locale_dir);
135 #endif
136
137         _ev_backends_manager_shutdown ();
138         _ev_file_helpers_shutdown ();
139         _ev_debug_shutdown ();
140 }
141
142 /*
143  * _ev_is_initialized:
144  *
145  * Returns: %TRUE if the evince document library has been initialized
146  */
147 gboolean
148 _ev_is_initialized (void)
149 {
150         return ev_init_count > 0;
151 }