libyui  3.2.9
YUILoader.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: YUILoader.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <stdlib.h> // getenv()
26 #include <unistd.h> // isatty()a
27 #include <sys/stat.h>
28 #include <string.h>
29 
30 #define YUILogComponent "ui"
31 #include "YUILog.h"
32 #include "YCommandLine.h"
33 #include "YUILoader.h"
34 #include "YUIPlugin.h"
35 #include "YUIException.h"
36 #include "YPath.h"
37 
38 #include "Libyui_config.h"
39 
40 
41 void YUILoader::loadUI( bool withThreads )
42 {
43  const char * envDisplay = getenv( "DISPLAY" );
44 
45  YCommandLine cmdline;
46 
47  bool wantNcurses = cmdline.find("--ncurses") != -1;
48  bool wantQt = cmdline.find("--qt") != -1;
49  bool wantGtk = cmdline.find("--gtk") != -1;
50 
51  bool haveQt = pluginExists( YUIPlugin_Qt );
52  bool haveGtk = pluginExists( YUIPlugin_Gtk );
53 
54  if ( envDisplay && !wantNcurses )
55  {
56  std::string wantedGUI;
57 
58  if ( haveQt && !wantGtk)
59  wantedGUI = YUIPlugin_Qt;
60  else if ( haveGtk && !wantQt )
61  wantedGUI = YUIPlugin_Gtk;
62 
63  if ( strcmp( wantedGUI.c_str(), "" ) )
64  {
65  try
66  {
67  loadPlugin( wantedGUI, withThreads );
68  return;
69  }
70  catch ( YUIException & ex)
71  {
72  YUI_CAUGHT( ex );
73  }
74  }
75  }
76 
77  if ( isatty( STDOUT_FILENO ) )
78  {
79  //
80  // NCurses UI
81  //
82 
83  try
84  {
85  loadPlugin( YUIPlugin_NCurses, withThreads );
86  return;
87  }
88  catch ( YUIException & ex)
89  {
90  YUI_CAUGHT( ex );
91  YUI_RETHROW( ex ); // what else to do here?
92  }
93  }
94  else
95  {
96  YUI_THROW( YUICantLoadAnyUIException() );
97  }
98 }
99 
101 {
102  if ( YUI::_ui )
103  {
104  yuiMilestone() << "Shutting down UI" << std::endl;
105  delete YUI::_ui;
106 
107  YUI::_ui = 0;
108  }
109 }
110 
111 void YUILoader::loadPlugin( const std::string & name, bool withThreads )
112 {
113  YUIPlugin uiPlugin( name.c_str() );
114 
115  if ( uiPlugin.success() )
116  {
117  createUIFunction_t createUI = (createUIFunction_t) uiPlugin.locateSymbol( "_Z8createUIb" ); // createUI(bool)
118 
119  if ( createUI )
120  {
121  YUI * ui = createUI( withThreads ); // no threads
122 
123  // At this point the concrete UI will have loaded its own
124  // internal plugins and registered their destructors.
125  // Our destructor must get called before those get dlclose'd.
126  //
127  // Formerly ~YUI was called quite late, which called ~YQUI
128  // and that ran code in the already unloaded Qt internal plugins.
129  atexit(deleteUI);
130 
131  if ( ui )
132  return;
133  }
134  }
135 
136  YUI_THROW( YUIPluginException( name ) );
137 }
138 
139 void YUILoader::loadExternalWidgetsPlugin ( const std::string& name, const std::string& plugin_name, const std::string& symbol )
140 {
141  YUIPlugin uiPlugin ( plugin_name.c_str() );
142 
143  if ( uiPlugin.success() )
144  {
145  createEWFunction_t createEW = ( createEWFunction_t ) uiPlugin.locateSymbol ( symbol.c_str() );
146 
147  if ( createEW )
148  {
149  YExternalWidgets * we = createEW ( name.c_str() );
150 
151  if ( we )
152  return;
153  }
154  }
155 
156  YUI_THROW ( YUIPluginException ( plugin_name ) );
157 }
158 
159 void YUILoader::loadExternalWidgets ( const std::string& name, const std::string& symbol )
160 {
161  const char * envDisplay = getenv( "DISPLAY" );
162 
163  YCommandLine cmdline;
164 
165  bool wantNcurses = cmdline.find("--ncurses") != -1;
166  bool wantQt = cmdline.find("--qt") != -1;
167  bool wantGtk = cmdline.find("--gtk") != -1;
168 
169  bool haveQt = pluginExists( YUIPlugin_Qt );
170  bool haveGtk = pluginExists( YUIPlugin_Gtk );
171 
172  if ( envDisplay && !wantNcurses )
173  {
174  std::string wantedGUI = name;
175  wantedGUI.append("-");
176 
177  if ( haveQt && !wantGtk)
178  wantedGUI.append(YUIPlugin_Qt);
179  else if ( haveGtk && !wantQt )
180  wantedGUI.append(YUIPlugin_Gtk);
181 
182  try
183  {
184  loadExternalWidgetsPlugin(name, wantedGUI, symbol );
185  return;
186  }
187  catch ( YUIException & ex)
188  {
189  YUI_CAUGHT( ex );
190  }
191  }
192 
193  //
194  // NCurses UI (test on tty has already been done by loadUI)
195  //
196 
197  try
198  {
199  std::string wantedNcurses = name;
200  wantedNcurses.append("-");
201  wantedNcurses.append(YUIPlugin_NCurses);
202  loadExternalWidgetsPlugin(name, wantedNcurses, symbol );
203  return;
204  }
205  catch ( YUIException & ex)
206  {
207  YUI_CAUGHT( ex );
208  YUI_RETHROW( ex ); // what else to do here?
209  }
210 }
211 
212 bool YUILoader::pluginExists( const std::string & pluginBaseName )
213 {
214  struct stat fileinfo;
215  std::string pluginName = PLUGIN_PREFIX;
216 
217  pluginName.append( pluginBaseName );
218  pluginName.append( PLUGIN_SUFFIX );
219 
220  YPath plugin ( PLUGINDIR, pluginName );
221 
222  return stat( plugin.path().c_str(), &fileinfo) == 0;
223 
224 }
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
Abstract base class of a libYUI user interface.
Definition: YUI.h:48
static void loadPlugin(const std::string &name, bool withThreads=false)
Load a UI plug-in.
Definition: YUILoader.cc:111
static void loadExternalWidgets(const std::string &name, const std::string &symbol="_Z21createExternalWidgetsPKc")
Load the given External Widgets plugin followed by its graphical extension implementation in the foll...
Definition: YUILoader.cc:159
Exception class for plugin load failure.
Definition: YUIException.h:875
static void deleteUI()
This will make sure the UI singleton is deleted.
Definition: YUILoader.cc:100
Finds files (e.g.
Definition: YPath.h:43
Abstract base class of a libYUI Widget Extension interface.
Utility class to access /proc/<pid>/cmdline to retrieve argc and argv.
Definition: YCommandLine.h:37
int find(const std::string &argName) const
Find a command line argument &#39;argName&#39; ("-display" etc.).
Exception class for UI plugin load failure.
Definition: YUIException.h:890
Wrapper class for dlopen() and related.
Definition: YUIPlugin.h:35
Base class for UI Exceptions.
Definition: YUIException.h:297
static void loadUI(bool withThreads=false)
Load any of the available UI plug-ins in this order:
Definition: YUILoader.cc:41