libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
YApplication.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: YApplication.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <locale.h> // setlocale()
26 #include <map>
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YApplication.h"
32 #include "YDialog.h"
33 #include "YUIException.h"
34 #include "YShortcut.h"
35 #include "YUI.h"
36 #include "YItem.h"
37 #include "YCommandLine.h"
38 
39 using std::endl;
40 
41 typedef std::map<std::string, int> YFunctionKeyMap;
42 
43 
45 {
47  : productName( "openSUSE" )
48  , reverseLayout( false )
49  {}
50 
51  std::string productName;
52  bool reverseLayout;
53  std::string applicationTitle;
54  std::string applicationIcon;
55  YFunctionKeyMap defaultFunctionKey;
56  YIconLoader* iconLoader;
57 };
58 
59 
61  : priv( new YApplicationPrivate() )
62 {
63  YUI_CHECK_NEW( priv );
64  priv->iconLoader = new YIconLoader();
65  YCommandLine cmdLine; // Retrieve command line args from /proc/<pid>/cmdline
66  if ( cmdLine.argc() > 0 )
67  priv->applicationTitle = cmdLine.arg(0);
68 }
69 
70 
72 {
73  // NOP
74 }
75 
76 
77 YWidget *
78 YApplication::findWidget( YWidgetID * id, bool doThrow ) const
79 {
80  YDialog * dialog = YDialog::currentDialog( doThrow );
81 
82  if ( ! dialog ) // has already thrown if doThrow == true
83  return 0;
84 
85  return dialog->findWidget( id, doThrow );
86 }
87 
88 
89 std::string
91 {
92  return priv->iconLoader->iconBasePath();
93 }
94 
95 
96 void
97 YApplication::setIconBasePath( const std::string & newIconBasePath )
98 {
99  priv->iconLoader->setIconBasePath ( newIconBasePath );
100 }
101 
102 YIconLoader *
103 YApplication::iconLoader()
104 {
105  return priv->iconLoader;
106 }
107 
108 void
109 YApplication::setProductName( const std::string & productName )
110 {
111  priv->productName = productName;
112 }
113 
114 
115 std::string
117 {
118  return priv->productName;
119 }
120 
121 
122 void
124 {
125  priv->reverseLayout = reverse;
126 }
127 
128 
130 {
131  return priv->reverseLayout;
132 }
133 
134 
135 int
136 YApplication::defaultFunctionKey( const std::string & label ) const
137 {
138  YFunctionKeyMap::const_iterator result =
139  priv->defaultFunctionKey.find( YShortcut::cleanShortcutString( label ) );
140 
141  if ( result == priv->defaultFunctionKey.end() )
142  return 0;
143  else
144  return result->second;
145 }
146 
147 
148 void
149 YApplication::setDefaultFunctionKey( const std::string & label, int fkey )
150 {
151  if ( fkey > 0 )
152  priv->defaultFunctionKey[ YShortcut::cleanShortcutString( label ) ] = fkey;
153  else
154  YUI_THROW( YUIException( "Bad function key number" ) );
155 }
156 
157 
158 void
160 {
161  priv->defaultFunctionKey.clear();
162 }
163 
164 
165 void
166 YApplication::setLanguage( const std::string & language, const std::string & encoding )
167 {
168  std::string lang = language;
169 
170  if ( ! encoding.empty() )
171  {
172  lang += ".";
173  lang += encoding;
174  }
175 
176  setenv( "LANG", lang.c_str(), 1 ); // 1 : replace
177  setlocale( LC_NUMERIC, "C" ); // but always format numbers with "."
178 
179  yuiMilestone() << "Setting language to " << lang << endl;
180 }
181 
182 
183 std::string
184 YApplication::language( bool stripEncoding ) const
185 {
186  const char *lang_env = getenv( "LANG" );
187 
188  if ( ! lang_env )
189  return "";
190 
191  std::string lang( lang_env );
192 
193  if ( stripEncoding )
194  {
195  std::string::size_type pos = lang.find_first_of( ".@" );
196 
197  if ( pos != std::string::npos ) // if encoding etc. specified
198  {
199  lang = lang.substr( 0, pos ); // remove it
200  }
201  }
202 
203  return lang;
204 }
205 
206 
207 std::string
208 YApplication::glyph( const std::string & sym )
209 {
210  if ( sym == YUIGlyph_ArrowLeft ) return ( reverseLayout() ? "->" : "<-" );
211  else if ( sym == YUIGlyph_ArrowRight ) return ( reverseLayout() ? "<-" : "->" );
212  else if ( sym == YUIGlyph_ArrowUp ) return ( "^" );
213  else if ( sym == YUIGlyph_ArrowDown ) return ( "v" );
214  else if ( sym == YUIGlyph_CheckMark ) return ( "x" );
215  else if ( sym == YUIGlyph_BulletArrowRight ) return ( "=>" );
216  else if ( sym == YUIGlyph_BulletCircle ) return ( "o" );
217  else if ( sym == YUIGlyph_BulletSquare ) return ( "[]" );
218  else // unknown glyph symbol
219  {
220  yuiError() << "Unknown glyph `" << sym << endl;
221  return "";
222  }
223 }
224 
225 bool
226 YApplication::openContextMenu( const YItemCollection & itemCollection )
227 {
228  YUI_THROW( YUIUnsupportedWidgetException( "ContextMenu" ) );
229  return false;
230 }
231 
232 
233 
234 int
235 YApplication::deviceUnits( YUIDimension dim, float layoutUnits )
236 {
237  return (int) ( layoutUnits + 0.5 );
238 }
239 
240 
241 float
242 YApplication::layoutUnits( YUIDimension dim, int deviceUnits )
243 {
244  return (float) deviceUnits;
245 }
246 
247 
248 int
249 YApplication::runInTerminal ( const std::string & module )
250 {
251  yuiError() << "Not in text mode: Cannot run external program in terminal." << endl;
252 
253  return -1;
254 }
255 
256 void YApplication::setApplicationTitle(const std::string &title)
257 {
258  priv->applicationTitle = title;
259 }
260 
261 const std::string &YApplication::applicationTitle() const
262 {
263  return priv->applicationTitle;
264 }
265 
266 void YApplication::setApplicationIcon(const std::string &icon)
267 {
268  priv->applicationIcon = icon;
269 }
270 const std::string &YApplication::applicationIcon() const
271 {
272  return priv->applicationIcon;
273 }
274