libyui  3.0.10
 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 bool YUILoader::pluginExists( const std::string & pluginBaseName )
120 {
121  struct stat fileinfo;
122  std::string pluginName = PLUGIN_PREFIX;
123 
124  pluginName.append( pluginBaseName );
125  pluginName.append( PLUGIN_SUFFIX );
126 
127  YPath plugin ( PLUGINDIR, pluginName );
128 
129  return stat( plugin.path().c_str(), &fileinfo) == 0;
130 
131 }