libyui  3.10.0
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::string;
40 using std::map;
41 
42 typedef map<string, int> YFunctionKeyMap;
43 
44 
46 {
48  : productName( "SUSE Linux" )
49  , reverseLayout( false )
50  , showProductLogo( false )
51  {}
52 
53  string productName;
54  bool reverseLayout;
55  string applicationTitle;
56  string applicationIcon;
57  YFunctionKeyMap defaultFunctionKey;
58  YIconLoader* iconLoader;
59  map<string,string> releaseNotes;
60  bool showProductLogo;
61 };
62 
63 
65  : priv( new YApplicationPrivate() )
66 {
67  YUI_CHECK_NEW( priv );
68  priv->iconLoader = new YIconLoader();
69  YCommandLine cmdLine; // Retrieve command line args from /proc/<pid>/cmdline
70  if ( cmdLine.argc() > 0 )
71  priv->applicationTitle = cmdLine.arg(0);
72 }
73 
74 
76 {
77  // NOP
78 }
79 
80 
81 YWidget *
82 YApplication::findWidget( YWidgetID * id, bool doThrow ) const
83 {
84  YDialog * dialog = YDialog::currentDialog( doThrow );
85 
86  if ( ! dialog ) // has already thrown if doThrow == true
87  return 0;
88 
89  return dialog->findWidget( id, doThrow );
90 }
91 
92 
93 string
95 {
96  return priv->iconLoader->iconBasePath();
97 }
98 
99 
100 void
101 YApplication::setIconBasePath( const string & newIconBasePath )
102 {
103  priv->iconLoader->setIconBasePath ( newIconBasePath );
104 }
105 
106 YIconLoader *
107 YApplication::iconLoader()
108 {
109  return priv->iconLoader;
110 }
111 
112 void
113 YApplication::setProductName( const string & productName )
114 {
115  priv->productName = productName;
116 }
117 
118 
119 string
121 {
122  return priv->productName;
123 }
124 
125 void
126 YApplication::setReleaseNotes( const map<string,string> & relNotes )
127 {
128  priv->releaseNotes = relNotes;
129 }
130 
131 map<string,string>
133 {
134  return priv->releaseNotes;
135 }
136 
137 void
139 {
140  priv->showProductLogo = show;
141 }
142 
143 bool
145 {
146  return priv->showProductLogo;
147 }
148 
149 void
151 {
152  priv->reverseLayout = reverse;
153 }
154 
155 
157 {
158  return priv->reverseLayout;
159 }
160 
161 
162 int
163 YApplication::defaultFunctionKey( const string & label ) const
164 {
165  YFunctionKeyMap::const_iterator result =
166  priv->defaultFunctionKey.find( YShortcut::cleanShortcutString( label ) );
167 
168  if ( result == priv->defaultFunctionKey.end() )
169  return 0;
170  else
171  return result->second;
172 }
173 
174 
175 void
176 YApplication::setDefaultFunctionKey( const string & label, int fkey )
177 {
178  if ( fkey > 0 )
179  priv->defaultFunctionKey[ YShortcut::cleanShortcutString( label ) ] = fkey;
180  else
181  YUI_THROW( YUIException( "Bad function key number" ) );
182 }
183 
184 
185 void
187 {
188  priv->defaultFunctionKey.clear();
189 }
190 
191 
192 void
193 YApplication::setLanguage( const string & language, const string & encoding )
194 {
195  string lang = language;
196 
197  if ( ! encoding.empty() )
198  {
199  lang += ".";
200  lang += encoding;
201  }
202 
203  setenv( "LANG", lang.c_str(), 1 ); // 1 : replace
204  setlocale( LC_NUMERIC, "C" ); // but always format numbers with "."
205 
206  yuiMilestone() << "Setting language to " << lang << endl;
207 }
208 
209 
210 string
211 YApplication::language( bool stripEncoding ) const
212 {
213  const char *lang_env = getenv( "LANG" );
214 
215  if ( ! lang_env )
216  return "";
217 
218  string lang( lang_env );
219 
220  if ( stripEncoding )
221  {
222  string::size_type pos = lang.find_first_of( ".@" );
223 
224  if ( pos != string::npos ) // if encoding etc. specified
225  {
226  lang = lang.substr( 0, pos ); // remove it
227  }
228  }
229 
230  return lang;
231 }
232 
233 
234 string
235 YApplication::glyph( const string & sym )
236 {
237  if ( sym == YUIGlyph_ArrowLeft ) return ( reverseLayout() ? "->" : "<-" );
238  else if ( sym == YUIGlyph_ArrowRight ) return ( reverseLayout() ? "<-" : "->" );
239  else if ( sym == YUIGlyph_ArrowUp ) return ( "^" );
240  else if ( sym == YUIGlyph_ArrowDown ) return ( "v" );
241  else if ( sym == YUIGlyph_CheckMark ) return ( "x" );
242  else if ( sym == YUIGlyph_BulletArrowRight ) return ( "=>" );
243  else if ( sym == YUIGlyph_BulletCircle ) return ( "o" );
244  else if ( sym == YUIGlyph_BulletSquare ) return ( "[]" );
245  else // unknown glyph symbol
246  {
247  yuiError() << "Unknown glyph `" << sym << endl;
248  return "";
249  }
250 }
251 
252 bool
254 {
255  YUI_THROW( YUIUnsupportedWidgetException( "ContextMenu" ) );
256  return false;
257 }
258 
259 
260 
261 int
262 YApplication::deviceUnits( YUIDimension dim, float layoutUnits )
263 {
264  return (int) ( layoutUnits + 0.5 );
265 }
266 
267 
268 float
269 YApplication::layoutUnits( YUIDimension dim, int deviceUnits )
270 {
271  return (float) deviceUnits;
272 }
273 
274 
275 int
276 YApplication::runInTerminal ( const string & module )
277 {
278  yuiError() << "Not in text mode: Cannot run external program in terminal." << endl;
279 
280  return -1;
281 }
282 
283 void YApplication::setApplicationTitle(const string &title)
284 {
285  priv->applicationTitle = title;
286 }
287 
288 const string &YApplication::applicationTitle() const
289 {
290  return priv->applicationTitle;
291 }
292 
293 void YApplication::setApplicationIcon(const string &icon)
294 {
295  priv->applicationIcon = icon;
296 }
297 const string &YApplication::applicationIcon() const
298 {
299  return priv->applicationIcon;
300 }
301 
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:38
virtual float layoutUnits(YUIDimension dim, int deviceUnits)
Convert device dependent units into logical layout spacing units.
std::string productName() const
Get the current product name ("openSUSE", "SLES", ...).
virtual void setLanguage(const std::string &language, const std::string &encoding=std::string())
Set language and encoding for the locale environment ($LANG).
void clearDefaultFunctionKeys()
Clear all previous label-to-function-key mappings.
virtual void setApplicationIcon(const std::string &icon)
Set the application Icon.
bool reverseLayout() const
Returns 'true' if widget geometry should be reversed for languages that have right-to-left writing di...
std::map< std::string, std::string > releaseNotes() const
Get the current release notes map.
void setReleaseNotes(const std::map< std::string, std::string > &relNotes)
Set release notes; map product => text.
virtual bool openContextMenu(const YItemCollection &itemCollection)
Open a context menu for a widget.
virtual void setIconBasePath(const std::string &newIconBasePath)
Set the icon base path.
virtual int runInTerminal(const std::string &command)
Run a shell command (typically an interactive program using NCurses) in a terminal (window).
bool showProductLogo() const
Return true if product logo should be shown.
void setShowProductLogo(bool show)
Set whether the product logo (in top bar) should be shown.
int defaultFunctionKey(const std::string &label) const
Return the default function key number for a widget with the specified label or 0 if there is none.
virtual const std::string & applicationIcon() const
Get the application Icon.
YApplication()
Constructor.
Definition: YApplication.cc:64
YWidget * findWidget(YWidgetID *id, bool doThrow=true) const
Find a widget in the topmost dialog by its ID.
Definition: YApplication.cc:82
virtual ~YApplication()
Destructor.
Definition: YApplication.cc:75
virtual int deviceUnits(YUIDimension dim, float layoutUnits)
Convert logical layout spacing units into device dependent units.
virtual std::string glyph(const std::string &glyphSymbolName)
Return a string for a named glyph:
void setDefaultFunctionKey(const std::string &label, int fkey)
Add a mapping from the specified label to the specified F-key number.
virtual std::string iconBasePath() const
Get the base path for icons used by the UI.
Definition: YApplication.cc:94
virtual void setReverseLayout(bool reverse)
Set reverse layout for Arabic / Hebrew support.
virtual void setApplicationTitle(const std::string &title)
Set the application title.
std::string language(bool stripEncoding=false) const
Return the current language from the locale environment ($LANG).
virtual void setProductName(const std::string &productName)
Set the current product name ("openSUSE", "SLES", ...).
virtual const std::string & applicationTitle() const
Get the application title.
Utility class to access /proc/<pid>/cmdline to retrieve argc and argv.
Definition: YCommandLine.h:38
int argc() const
Return the number of arguments in the command line.
Definition: YCommandLine.cc:80
std::string arg(int index) const
Return command line argument no.
A window in the desktop environment.
Definition: YDialog.h:48
static YDialog * currentDialog(bool doThrow=true)
Return the current (topmost) dialog.
Definition: YDialog.cc:539
std::string cleanShortcutString()
Returns the shortcut string ( from the widget's shortcut property ) without any "&" markers.
Definition: YShortcut.cc:93
Base class for UI Exceptions.
Definition: YUIException.h:298
Exception class for "optional widget not supported".
Definition: YUIException.h:776
Abstract base class for widget IDs.
Definition: YWidgetID.h:37
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YWidget * findWidget(YWidgetID *id, bool doThrow=true) const
Recursively find a widget by its ID.
Definition: YWidget.cc:607