libyui  3.4.2
YUIPlugin.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YUIPlugin.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <dlfcn.h>
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUIPlugin.h"
32 #include "YPath.h"
33 
34 #include "Libyui_config.h"
35 
36 
37 YUIPlugin::YUIPlugin( const char * pluginLibBaseName )
38 {
39  _pluginLibBaseName = std::string( pluginLibBaseName );
40 
41  std::string pluginFilename = pluginLibFullPath();
42 
43  _pluginLibHandle = dlopen( pluginFilename.c_str(),
44  RTLD_NOW | RTLD_GLOBAL);
45 
46  if ( ! _pluginLibHandle )
47  {
48  _errorMsg = dlerror();
49 
50  yuiError() << "Could not load UI plugin \"" << pluginLibBaseName
51  << "\": " << _errorMsg
52  << std::endl;
53  }
54 }
55 
56 
58 {
59  // This intentionally does NOT call unload(): This would be
60  // counterproductive for almost all use cases of this class.
61 }
62 
63 
64 void
66 {
67  if ( _pluginLibHandle )
68  dlclose( _pluginLibHandle );
69 }
70 
71 
72 std::string
74 {
75 
76  std::string pluginName = PLUGIN_PREFIX;
77  pluginName.append( _pluginLibBaseName );
78  pluginName.append( PLUGIN_SUFFIX );
79 
80  YPath plugin( PLUGINDIR, pluginName );
81 
82  return plugin.path();
83 }
84 
85 
86 void * YUIPlugin::locateSymbol( const char * symbol )
87 {
88  if ( ! _pluginLibHandle )
89  return 0;
90 
91  void * addr = dlsym( _pluginLibHandle, symbol );
92 
93  if ( ! addr )
94  {
95  yuiError() << "Could not locate symbol \"" << symbol
96  << "\" in " << pluginLibFullPath()
97  << std::endl;
98  }
99 
100  return addr;
101 }
102 
103 
104 bool YUIPlugin::error() const
105 {
106  return _pluginLibHandle == 0;
107 }
108 
109 
110 bool YUIPlugin::success() const
111 {
112  return _pluginLibHandle != 0;
113 }
114 
115 
116 std::string YUIPlugin::errorMsg() const
117 {
118  return _errorMsg;
119 }
std::string path()
Returns the full path of the file if found; if not found just the filename given in constructor...
Definition: YPath.cc:171
std::string errorMsg() const
Returns a human readable (but in most cases untranslated) error message if there was an error...
Definition: YUIPlugin.cc:116
virtual ~YUIPlugin()
Destructor.
Definition: YUIPlugin.cc:57
YUIPlugin(const char *pluginLibBaseName)
Constructor: Load the specified plugin library from the standard UI plugin directory (/usr/lib/yui/)...
Definition: YUIPlugin.cc:37
void * locateSymbol(const char *symbol)
Try to locate the specified symbol (function or global variable) in the plugin library.
Definition: YUIPlugin.cc:86
Finds files (e.g.
Definition: YPath.h:43
bool success() const
Returns &#39;true&#39; if there was no error loading the plugin.
Definition: YUIPlugin.cc:110
bool error() const
Returns &#39;true&#39; if there was an error loading the plugin.
Definition: YUIPlugin.cc:104
std::string pluginLibFullPath() const
Returns the full path of the plugin library.
Definition: YUIPlugin.cc:73
void unload()
Unload this plugin.
Definition: YUIPlugin.cc:65