libyui  3.0.13
 All Classes Functions Variables Enumerations Friends
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 #include "YCommandLine.h"
31 #include "YUILoader.h"
32 #include "YUIPlugin.h"
33 #include "YUIException.h"
34 #include "YPath.h"
35 
36 #include "Libyui_config.h"
37 
38 
39 void YUILoader::loadUI( bool withThreads )
40 {
41  const char * envDisplay = getenv( "DISPLAY" );
42 
43  YCommandLine cmdline;
44 
45  bool wantNcurses = cmdline.find("--ncurses") != -1;
46  bool wantQt = cmdline.find("--qt") != -1;
47  bool wantGtk = cmdline.find("--gtk") != -1;
48 
49  bool haveQt = pluginExists( YUIPlugin_Qt );
50  bool haveGtk = pluginExists( YUIPlugin_Gtk );
51 
52  if ( envDisplay && !wantNcurses )
53  {
54  std::string wantedGUI;
55 
56  if ( haveQt && !wantGtk)
57  wantedGUI = YUIPlugin_Qt;
58  else if ( haveGtk && !wantQt )
59  wantedGUI = YUIPlugin_Gtk;
60 
61  if ( strcmp( wantedGUI.c_str(), "" ) )
62  {
63  try
64  {
65  loadPlugin( wantedGUI, withThreads );
66  return;
67  }
68  catch ( YUIException & ex)
69  {
70  YUI_CAUGHT( ex );
71  }
72  }
73  }
74 
75  if ( isatty( STDOUT_FILENO ) )
76  {
77  //
78  // NCurses UI
79  //
80 
81  try
82  {
83  loadPlugin( YUIPlugin_NCurses, withThreads );
84  return;
85  }
86  catch ( YUIException & ex)
87  {
88  YUI_CAUGHT( ex );
89  YUI_RETHROW( ex ); // what else to do here?
90  }
91  }
92  else
93  {
94  YUI_THROW( YUICantLoadAnyUIException() );
95  }
96 }
97 
98 
99 void YUILoader::loadPlugin( const std::string & name, bool withThreads )
100 {
101  YUIPlugin uiPlugin( name.c_str() );
102 
103  if ( uiPlugin.success() )
104  {
105  createUIFunction_t createUI = (createUIFunction_t) uiPlugin.locateSymbol( "_Z8createUIb" ); // createUI(bool)
106 
107  if ( createUI )
108  {
109  YUI * ui = createUI( withThreads ); // no threads
110 
111  if ( ui )
112  return;
113  }
114  }
115 
116  YUI_THROW( YUIPluginException( name ) );
117 }
118 
119 void YUILoader::loadExternalWidgetsPlugin ( const std::string& name, const std::string& plugin_name, const std::string& symbol )
120 {
121  YUIPlugin uiPlugin ( plugin_name.c_str() );
122 
123  if ( uiPlugin.success() )
124  {
125  createEWFunction_t createEW = ( createEWFunction_t ) uiPlugin.locateSymbol ( symbol.c_str() );
126 
127  if ( createEW )
128  {
129  YExternalWidgets * we = createEW ( name.c_str() );
130 
131  if ( we )
132  return;
133  }
134  }
135 
136  YUI_THROW ( YUIPluginException ( plugin_name ) );
137 }
138 
139 void YUILoader::loadExternalWidgets ( const std::string& name, const std::string& symbol )
140 {
141  const char * envDisplay = getenv( "DISPLAY" );
142 
143  YCommandLine cmdline;
144 
145  bool wantNcurses = cmdline.find("--ncurses") != -1;
146  bool wantQt = cmdline.find("--qt") != -1;
147  bool wantGtk = cmdline.find("--gtk") != -1;
148 
149  bool haveQt = pluginExists( YUIPlugin_Qt );
150  bool haveGtk = pluginExists( YUIPlugin_Gtk );
151 
152  if ( envDisplay && !wantNcurses )
153  {
154  std::string wantedGUI = name;
155  wantedGUI.append("-");
156 
157  if ( haveQt && !wantGtk)
158  wantedGUI.append(YUIPlugin_Qt);
159  else if ( haveGtk && !wantQt )
160  wantedGUI.append(YUIPlugin_Gtk);
161 
162  try
163  {
164  loadExternalWidgetsPlugin(name, wantedGUI, symbol );
165  return;
166  }
167  catch ( YUIException & ex)
168  {
169  YUI_CAUGHT( ex );
170  }
171  }
172 
173  //
174  // NCurses UI (test on tty has already been done by loadUI)
175  //
176 
177  try
178  {
179  std::string wantedNcurses = name;
180  wantedNcurses.append("-");
181  wantedNcurses.append(YUIPlugin_NCurses);
182  loadExternalWidgetsPlugin(name, wantedNcurses, symbol );
183  return;
184  }
185  catch ( YUIException & ex)
186  {
187  YUI_CAUGHT( ex );
188  YUI_RETHROW( ex ); // what else to do here?
189  }
190 }
191 
192 bool YUILoader::pluginExists( const std::string & pluginBaseName )
193 {
194  struct stat fileinfo;
195  std::string pluginName = PLUGIN_PREFIX;
196 
197  pluginName.append( pluginBaseName );
198  pluginName.append( PLUGIN_SUFFIX );
199 
200  YPath plugin ( PLUGINDIR, pluginName );
201 
202  return stat( plugin.path().c_str(), &fileinfo) == 0;
203 
204 }
Definition: YUI.h:48
static void loadPlugin(const std::string &name, bool withThreads=false)
Definition: YUILoader.cc:99
static void loadExternalWidgets(const std::string &name, const std::string &symbol="_Z21createExternalWidgetsPKc")
Definition: YUILoader.cc:139
Definition: YPath.h:43
int find(const std::string &argName) const
static void loadUI(bool withThreads=false)
Definition: YUILoader.cc:39