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