libyui  3.0.13
 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  std::map<std::string,std::string> releaseNotes;
58 };
59 
60 
62  : priv( new YApplicationPrivate() )
63 {
64  YUI_CHECK_NEW( priv );
65  priv->iconLoader = new YIconLoader();
66  YCommandLine cmdLine; // Retrieve command line args from /proc/<pid>/cmdline
67  if ( cmdLine.argc() > 0 )
68  priv->applicationTitle = cmdLine.arg(0);
69 }
70 
71 
73 {
74  // NOP
75 }
76 
77 
78 YWidget *
79 YApplication::findWidget( YWidgetID * id, bool doThrow ) const
80 {
81  YDialog * dialog = YDialog::currentDialog( doThrow );
82 
83  if ( ! dialog ) // has already thrown if doThrow == true
84  return 0;
85 
86  return dialog->findWidget( id, doThrow );
87 }
88 
89 
90 std::string
92 {
93  return priv->iconLoader->iconBasePath();
94 }
95 
96 
97 void
98 YApplication::setIconBasePath( const std::string & newIconBasePath )
99 {
100  priv->iconLoader->setIconBasePath ( newIconBasePath );
101 }
102 
103 YIconLoader *
104 YApplication::iconLoader()
105 {
106  return priv->iconLoader;
107 }
108 
109 void
110 YApplication::setProductName( const std::string & productName )
111 {
112  priv->productName = productName;
113 }
114 
115 
116 std::string
118 {
119  return priv->productName;
120 }
121 
122 void
123 YApplication::setReleaseNotes( const std::map<std::string,std::string> & relNotes )
124 {
125  priv->releaseNotes = relNotes;
126 }
127 
128 std::map<std::string,std::string>
130 {
131  return priv->releaseNotes;
132 }
133 
134 void
136 {
137  priv->reverseLayout = reverse;
138 }
139 
140 
142 {
143  return priv->reverseLayout;
144 }
145 
146 
147 int
148 YApplication::defaultFunctionKey( const std::string & label ) const
149 {
150  YFunctionKeyMap::const_iterator result =
151  priv->defaultFunctionKey.find( YShortcut::cleanShortcutString( label ) );
152 
153  if ( result == priv->defaultFunctionKey.end() )
154  return 0;
155  else
156  return result->second;
157 }
158 
159 
160 void
161 YApplication::setDefaultFunctionKey( const std::string & label, int fkey )
162 {
163  if ( fkey > 0 )
164  priv->defaultFunctionKey[ YShortcut::cleanShortcutString( label ) ] = fkey;
165  else
166  YUI_THROW( YUIException( "Bad function key number" ) );
167 }
168 
169 
170 void
172 {
173  priv->defaultFunctionKey.clear();
174 }
175 
176 
177 void
178 YApplication::setLanguage( const std::string & language, const std::string & encoding )
179 {
180  std::string lang = language;
181 
182  if ( ! encoding.empty() )
183  {
184  lang += ".";
185  lang += encoding;
186  }
187 
188  setenv( "LANG", lang.c_str(), 1 ); // 1 : replace
189  setlocale( LC_NUMERIC, "C" ); // but always format numbers with "."
190 
191  yuiMilestone() << "Setting language to " << lang << endl;
192 }
193 
194 
195 std::string
196 YApplication::language( bool stripEncoding ) const
197 {
198  const char *lang_env = getenv( "LANG" );
199 
200  if ( ! lang_env )
201  return "";
202 
203  std::string lang( lang_env );
204 
205  if ( stripEncoding )
206  {
207  std::string::size_type pos = lang.find_first_of( ".@" );
208 
209  if ( pos != std::string::npos ) // if encoding etc. specified
210  {
211  lang = lang.substr( 0, pos ); // remove it
212  }
213  }
214 
215  return lang;
216 }
217 
218 
219 std::string
220 YApplication::glyph( const std::string & sym )
221 {
222  if ( sym == YUIGlyph_ArrowLeft ) return ( reverseLayout() ? "->" : "<-" );
223  else if ( sym == YUIGlyph_ArrowRight ) return ( reverseLayout() ? "<-" : "->" );
224  else if ( sym == YUIGlyph_ArrowUp ) return ( "^" );
225  else if ( sym == YUIGlyph_ArrowDown ) return ( "v" );
226  else if ( sym == YUIGlyph_CheckMark ) return ( "x" );
227  else if ( sym == YUIGlyph_BulletArrowRight ) return ( "=>" );
228  else if ( sym == YUIGlyph_BulletCircle ) return ( "o" );
229  else if ( sym == YUIGlyph_BulletSquare ) return ( "[]" );
230  else // unknown glyph symbol
231  {
232  yuiError() << "Unknown glyph `" << sym << endl;
233  return "";
234  }
235 }
236 
237 bool
238 YApplication::openContextMenu( const YItemCollection & itemCollection )
239 {
240  YUI_THROW( YUIUnsupportedWidgetException( "ContextMenu" ) );
241  return false;
242 }
243 
244 
245 
246 int
247 YApplication::deviceUnits( YUIDimension dim, float layoutUnits )
248 {
249  return (int) ( layoutUnits + 0.5 );
250 }
251 
252 
253 float
254 YApplication::layoutUnits( YUIDimension dim, int deviceUnits )
255 {
256  return (float) deviceUnits;
257 }
258 
259 
260 int
261 YApplication::runInTerminal ( const std::string & module )
262 {
263  yuiError() << "Not in text mode: Cannot run external program in terminal." << endl;
264 
265  return -1;
266 }
267 
268 void YApplication::setApplicationTitle(const std::string &title)
269 {
270  priv->applicationTitle = title;
271 }
272 
273 const std::string &YApplication::applicationTitle() const
274 {
275  return priv->applicationTitle;
276 }
277 
278 void YApplication::setApplicationIcon(const std::string &icon)
279 {
280  priv->applicationIcon = icon;
281 }
282 const std::string &YApplication::applicationIcon() const
283 {
284  return priv->applicationIcon;
285 }
286 
virtual void setApplicationTitle(const std::string &title)
virtual bool openContextMenu(const YItemCollection &itemCollection)
std::string productName() const
virtual float layoutUnits(YUIDimension dim, int deviceUnits)
void clearDefaultFunctionKeys()
std::map< std::string, std::string > releaseNotes() const
virtual void setIconBasePath(const std::string &newIconBasePath)
Definition: YApplication.cc:98
virtual int deviceUnits(YUIDimension dim, float layoutUnits)
int defaultFunctionKey(const std::string &label) const
virtual std::string iconBasePath() const
Definition: YApplication.cc:91
virtual ~YApplication()
Definition: YApplication.cc:72
int argc() const
Definition: YCommandLine.cc:78
virtual int runInTerminal(const std::string &command)
YWidget * findWidget(YWidgetID *id, bool doThrow=true) const
Definition: YApplication.cc:79
std::string cleanShortcutString()
Definition: YShortcut.cc:91
static YDialog * currentDialog(bool doThrow=true)
Definition: YDialog.cc:531
std::string arg(int index) const
YWidget * findWidget(YWidgetID *id, bool doThrow=true) const
Definition: YWidget.cc:602
virtual const std::string & applicationTitle() const
void setDefaultFunctionKey(const std::string &label, int fkey)
std::string language(bool stripEncoding=false) const
virtual std::string glyph(const std::string &glyphSymbolName)
virtual void setApplicationIcon(const std::string &icon)
virtual void setProductName(const std::string &productName)
virtual void setReverseLayout(bool reverse)
virtual void setLanguage(const std::string &language, const std::string &encoding=std::string())
bool reverseLayout() const
void setReleaseNotes(const std::map< std::string, std::string > &relNotes)
virtual const std::string & applicationIcon() const