Debian Codegraph @

src/af/xap/cocoa/xap_CocoaModule.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 2001 Dom Lachowicz <[email protected]>
5 * Copyright (C) 2001 Hubert Figuiere
6 * Copyright (C) 2004 Francis James Franklin
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dirent.h>
27
28 #include <glib.h>
29 #include <gmodule.h>
30
31 #include "ut_assert.h"
32 #include "ut_debugmsg.h"
33 #include "ut_path.h"
34 #include "ut_string.h"
35
36 #include "xap_App.h"
37 #include "xap_CocoaAppController.h"
38 #include "xap_CocoaModule.h"
39 #include "xap_CocoaPlugin.h"
40 #include "xap_ModuleManager.h"
41
42 XAP_CocoaModule::XAP_CocoaModule () :
43 m_szname("??"),
44 m_module_path("??"),
45 m_module(0),
46 m_bLoaded(false)
47 {
48 //
49 }
50
51 XAP_CocoaModule::~XAP_CocoaModule (void)
52 {
53 //
54 }
55
56 bool XAP_CocoaModule::getModuleName (char **dest) const
57 {
58 if (dest)
59 {
60 *dest = (char *) g_strdup(m_szname.c_str());
61 return (*dest ? true : false);
62 }
63 return false;
64 }
65
66 bool XAP_CocoaModule::load (const char *name)
67 {
68 if (m_bLoaded || !name)
69 return false;
70
71 if (!(m_module = g_module_open(name, G_MODULE_BIND_LAZY)))
72 {
73 UT_WARNINGMSG(("Module \"%s\" cannot be loaded\n"
74 " ERROR: %s\n", name, g_module_error() ? : "(null)"));
75 return false;
76 }
77
78 m_szname = UT_basename(name);
79 m_module_path = name;
80 m_bLoaded = true;
81
82 return m_bLoaded;
83 }
84
85 bool XAP_CocoaModule::unload (void)
86 {
87 if (!m_bLoaded || !m_module)
88 return false;
89
90 if (!g_module_close((GModule *) m_module))
91 return false;
92
93 m_bLoaded = false;
94 m_module = 0;
95 return true;
96 }
97
98 bool XAP_CocoaModule::resolveSymbol (const char *symbol_name, void **symbol)
99 {
100 if (!symbol_name || !symbol)
101 return false;
102
103 return (g_module_symbol((GModule *) m_module, symbol_name, symbol) ? true : false);
104 }
105
106 bool XAP_CocoaModule::getErrorMsg (char **dest) const
107 {
108 if (!dest)
109 return false;
110
111 *dest = (char *) g_strdup(g_module_error());
112 return (*dest ? true : false);
113 }
114
115 /**
116 * return > 0 for directory entries ending in ".so"
117 */
118 static int s_Abi_only (struct dirent *d)
119 {
120 const char *name = d->d_name;
121
122 if (name && XAP_CocoaModule::hasPluginExtension(name))
123 return 1;
124
125 return 0;
126 }
127
128 /**
129 * return true if dirname exists and is a directory; symlinks probably not counted
130 */
131 static bool s_dir_exists (const char *dirname)
132 {
133 struct stat sbuf;
134
135 if (stat(dirname, &sbuf) != 0)
136 return false;
137
138 if ((sbuf.st_mode & S_IFMT) != S_IFDIR)
139 return false;
140
141 return true;
142 }
143
144 /*!
145 Creates a directory if the specified one does not yet exist.
146 /param A character string representing the to-be-created directory.
147 /return True, if the directory already existed, or was successfully created.
148 False, if the input path was already a file, not a directory, or if the directory was unable to be created.
149 */
150 static bool s_createDirectoryIfNecessary(const char *szDir, bool publicdir = false)
151 {
152 struct stat statbuf;
153
154 if (stat(szDir,&statbuf) == 0) // if it exists
155 {
156 if (S_ISDIR(statbuf.st_mode)) // and is a directory
157 return true;
158
159 UT_DEBUGMSG(("Pathname [%s] is not a directory.\n",szDir));
160 return false;
161 }
162
163 bool success = true;
164 mode_t old_mask = umask(0);
165 if (mkdir(szDir, publicdir ? 0775 : 0700) != 0)
166 {
167 UT_DEBUGMSG(("Could not create Directory [%s].\n",szDir));
168 success = false;
169 }
170 umask (old_mask);
171 return success;
172 }
173
174 static void createSystemPluginsDirectory(void)
175 {
176 if (!s_createDirectoryIfNecessary("/Library", true))
177 return;
178
179 if (!s_createDirectoryIfNecessary("/Library/Application Support", true))
180 return;
181
182 if (!s_createDirectoryIfNecessary("/Library/Application Support/AbiSuite", true))
183 return;
184
185 s_createDirectoryIfNecessary ("/Library/Application Support/AbiSuite/PlugIns", true);
186 }
187
188 /**
189 * MacOSX applications look for plugins in Contents/PlugIns, and there's probably
190 * no need to jump through scandir hoops identifying these. Third party plugins or
191 * plugins not distributed with AbiWord.app can be found in the system directory
192 * "/Library/Application Support" or in the user's home equivalent - I'm choosing
193 * to make the plug-in directory "/Library/Application Support/AbiWord/PlugIns".
194 */
195 void XAP_CocoaModule::loadAllPlugins ()
196 {
197 int support_dir_count = 0;
198
199 std::string support_dir[3];
200
201 /* Load from:
202 * a. "/Library/Application Support/AbiSuite/PlugIns"
203 * b. "/Library/Application Support/AbiSuite/PlugIns"
204 * c. "$HOME/Library/Application Support/AbiSuite/PlugIns"
205 */
206
207 NSString *app_path = [[NSBundle mainBundle] bundlePath];
208 if (app_path)
209 {
210 NSString *plugin_path = [app_path stringByAppendingString:@"/Contents/PlugIns"];
211 if (plugin_path)
212 {
213 support_dir[support_dir_count] = [plugin_path UTF8String];
214
215 if (s_dir_exists (support_dir[support_dir_count].c_str()))
216 support_dir_count++;
217 }
218 }
219
220 /* create the system plugins directory - if we can...
221 */
222 createSystemPluginsDirectory();
223
224 support_dir[support_dir_count] = "/Library/Application Support/AbiSuite/PlugIns";
225
226 if (s_dir_exists(support_dir[support_dir_count].c_str()))
227 support_dir_count++;
228
229 const char *homedir = XAP_App::getApp()->getUserPrivateDirectory();
230 if (homedir && s_dir_exists(homedir))
231 {
232 std::string plugin_dir(homedir);
233 plugin_dir += "/PlugIns";
234
235 if (s_createDirectoryIfNecessary(plugin_dir.c_str()))
236 support_dir[support_dir_count++] = plugin_dir;
237 }
238
239 for (int i = 0; i < support_dir_count; i++)
240 {
241 struct dirent **namelist = NULL;
242 int n = scandir(support_dir[i].c_str(), &namelist, s_Abi_only, alphasort);
243
244 if (n < 0)
245 continue;
246
247 if (n == 0)
248 {
249 free(namelist);
250 continue;
251 }
252
253 std::string plugin_path;
254 while (n--)
255 {
256 plugin_path = support_dir[i] + "/" + namelist[n]->d_name;
257 XAP_CocoaModule::loadPlugin(plugin_path);
258 free (namelist[n]);
259 }
260 free (namelist);
261 }
262 }
263
264 bool XAP_CocoaModule::loadPlugin (const std::string &path)
265 {
266 bool bLoaded = XAP_ModuleManager::instance().loadModule(path.c_str());
267
268 return bLoaded;
269 }
270
271 bool XAP_CocoaModule::hasPluginExtension (const std::string &path)
272 {
273 if (path.length() <= 3)
274 return false;
275
276 if (strcmp(path.c_str() + path.length() - 3, ".so") != 0)
277 return false;
278
279 return true;
280 }