libyui-ncurses  2.44.1
 All Classes Functions Variables
NCAskForFile.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: NCAskForFile.cc
20 
21  Author: Gabriele Strattner <gs@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 
28 #include "NCAskForFile.h"
29 
30 #include <yui/YDialog.h>
31 
32 #include "NCWidgetFactory.h"
33 #include "NCLayoutBox.h"
34 #include "NCSpacing.h"
35 #include "NCFrame.h"
36 #include "NCi18n.h"
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <dirent.h>
42 #include <sys/errno.h>
43 
44 /*
45  Textdomain "ncurses"
46 */
47 
48 
49 NCAskForFile::NCAskForFile( const wpos at,
50  const std::string & iniDir,
51  const std::string & filter,
52  const std::string & headline )
53  : NCPopup( at, true )
54  , okButton( 0 )
55  , cancelButton( 0 )
56  , dirName( 0 )
57  , dirList( 0 )
58  , detailed( 0 )
59  , fileList( 0 )
60  , fileName( 0 )
61 {
62 
63 }
64 
65 
66 NCAskForFile::~NCAskForFile( )
67 {
68 
69 }
70 
71 
72 std::string NCAskForFile::checkIniDir( std::string iniDir )
73 {
74  std::string dname = "";
75 
76  struct stat64 statInfo;
77  stat64( iniDir.c_str(), &statInfo );
78 
79  if ( S_ISDIR( statInfo.st_mode ) )
80  {
81  dname = iniDir;
82  }
83  else
84  {
85  std::string::size_type pos;
86 
87  pos = iniDir.find_last_of( "/" );
88 
89  if ( pos != std::string::npos
90  && pos != 0 )
91  {
92  std::string dir = iniDir.substr( 0, pos );
93  stat64( dir.c_str(), &statInfo );
94 
95  if ( S_ISDIR( statInfo.st_mode ) )
96  {
97  dname = dir;
98  iniFileName = iniDir.substr( pos + 1 );
99  }
100  }
101  }
102 
103  return dname;
104 }
105 
106 
107 void NCAskForFile::createLayout( const std::string & iniDir,
108  const std::string & filter,
109  const std::string & headline,
110  bool edit )
111 {
112  std::string startDir;
113  std::string old_textdomain = textdomain( NULL );
114  setTextdomain( "ncurses" );
115 
116  startDir = checkIniDir( iniDir );
117 
118  // the vertical split is the (only) child of the dialog
119  YLayoutBox * split = YUI::widgetFactory()->createVBox( this );
120 
121  new NCLabel( split, headline, true, false ); // isHeading = true
122 
123  YFrame * frame = YUI::widgetFactory()->createFrame( split, "" );
124 
125  // label for text field showing the selected dir
126  dirName = new NCComboBox( frame, _( "Selected Directory:" ), false ); // editable = false;
127  dirName->setNotify( true );
128  dirName->setStretchable( YD_HORIZ, true );
129 
130  // add the checkBox detailed
131  YLayoutBox * hSplit = YUI::widgetFactory()->createHBox( split );
132 
133  // label for checkbox
134  detailed = new NCCheckBox( hSplit, _( "&Detailed View" ), false );
135  detailed->setNotify( true );
136 
137  // HBox for the lists
138  YLayoutBox * hSplit1 = YUI::widgetFactory()->createHBox( split );
139 
140  // create table header for table type T_Overview
141  YTableHeader * dirHeader = new YTableHeader();
142  dirHeader->addColumn( " " );
143  dirHeader->addColumn( _( "Directory name" ) );
144 
145  // add the list of directories
146  dirList = new NCDirectoryTable( hSplit1,
147  dirHeader,
148  NCFileSelection::T_Overview,
149  startDir );
150  dirList->setSendKeyEvents( true );
151 
152  // create table header for table type T_Overview
153  YTableHeader * fileHeader = new YTableHeader();
154  fileHeader->addColumn( " " );
155  fileHeader->addColumn( _( "File name" ) );
156 
157  // add the list of files
158  fileList = new NCFileTable( hSplit1,
159  fileHeader,
160  NCFileSelection::T_Overview,
161  filter,
162  startDir );
163 
164  fileList->setSendKeyEvents( true );
165 
166  YLayoutBox * hSplit2 = YUI::widgetFactory()->createHBox( split );
167 
168  // opt.isEditable.setValue( edit );
169  // NCInputField doesn't support mode 'not editable' any longer
170  // -> an InputField IS editable
171 
172  // add the text entry for the file name
173  fileName = new NCInputField( hSplit2,
174  // label for text field showing the filename
175  _( "&File name:" ),
176  false, // passWordMode = false
177  100,
178  50 );
179  fileName->setValue( iniFileName );
180 
181  // label for text field showing the filter (e.g. *.bak)
182  NCComboBox * extension = new NCComboBox( hSplit2, _( "Filter:" ), false ); // editable = false
183  extension->setStretchable( YD_HORIZ, true );
184  extension->addItem( filter,
185  true ); // selected
186 
187  YUI::widgetFactory()->createSpacing( split, YD_VERT, false, 1.0 );
188 
189  // HBox for the buttons
190  YLayoutBox * hSplit3 = YUI::widgetFactory()->createHBox( split );
191 
192  YUI::widgetFactory()->createSpacing( hSplit3, YD_HORIZ, true, 0.2 ); // stretchable = true
193 
194  // add the OK button
195  okButton = new NCPushButton( hSplit3, _( "&OK" ) );
196  okButton->setFunctionKey( 10 );
197  okButton->setStretchable( YD_HORIZ, true );
198 
199  YUI::widgetFactory()->createSpacing( hSplit3, YD_HORIZ, true, 0.4 );
200 
201  // add the Cancel button
202  cancelButton = new NCPushButton( hSplit3, _( "&Cancel" ) );
203  cancelButton->setFunctionKey( 9 );
204  cancelButton->setStretchable( YD_HORIZ, true );
205 
206  YUI::widgetFactory()->createSpacing( hSplit3, YD_HORIZ, true, 0.2 );
207  // restore former text domain
208  setTextdomain( old_textdomain.c_str() );
209 }
210 
211 
213 {
214  postevent = NCursesEvent();
215 
216  if ( !dirList || !fileList || !dirName )
217  return postevent;
218 
219  dirList->fillList();
220  fileList->fillList();
221  dirList->setKeyboardFocus();
222  dirName->addItem( dirList->getCurrentDir(),
223  true ); // selected
224 
225  if ( iniFileName == "" )
226  // show the currently selected file
227  fileName->setValue( fileList->getCurrentFile() );
228 
229  // event loop
230  do
231  {
232  popupDialog();
233  }
234  while ( postAgain() );
235 
236  popdownDialog();
237 
238  return postevent;
239 }
240 
241 
243 {
244  return NCurses::cols() - 10;
245 }
246 
247 
248 int NCAskForFile::preferredHeight()
249 {
250  return NCurses::lines() - 4;
251 }
252 
253 
254 NCursesEvent NCAskForFile::wHandleInput( wint_t ch )
255 {
256  if ( ch == 27 ) // ESC
257  return NCursesEvent::cancel;
258 
259  return NCDialog::wHandleInput( ch );
260 }
261 
262 
264 {
265  // set new start dir and show the file list
266  fileList->setStartDir( dirList->getCurrentDir() );
267  fileList->fillList( );
268 
269  if ( iniFileName == "" )
270  // show the currently selected file
271  fileName->setValue( fileList->getCurrentFile() );
272 }
273 
274 
275 bool NCAskForFile::postAgain( )
276 {
277  if ( !postevent.widget )
278  return false;
279 
280  postevent.detail = NCursesEvent::NODETAIL;
281 
282  if ( postevent.keySymbol == "CursorLeft" )
283  {
284  dirList->setKeyboardFocus();
285  return true;
286  }
287  else if ( postevent.keySymbol == "CursorRight" )
288  {
289  fileList->setKeyboardFocus();
290  fileName->setValue( fileList->getCurrentFile() );
291  return true;
292  }
293 
294  if ( postevent.widget == okButton )
295  {
296  postevent.result = dirList->getCurrentDir() + "/" + getFileName();
297  // return false means: close the popup
298  return false;
299  }
300  else if (( postevent.widget == dirList ) &&
301  ( postevent.result != "" ) )
302  {
303  // show the currently selected directory
304  dirName->addItem( postevent.result,
305  true );
306  updateFileList();
307 
308  if ( postevent.reason == YEvent::Activated )
309  {
310  // fill directory and file list
311  dirList->fillList();
312  updateFileList();
313  }
314  }
315  else if ( postevent.widget == dirName )
316  {
317  dirList->setStartDir( dirName->text() );
318  dirList->fillList();
319 
320  updateFileList();
321  }
322  else if ( postevent.widget == detailed )
323  {
324  bool details = getCheckBoxValue( detailed );
325 
326  if ( details )
327  {
328  fileList->setTableType( NCFileTable::T_Detailed );
329  dirList->setTableType( NCFileTable::T_Detailed );
330  }
331  else
332  {
333  fileList->setTableType( NCFileTable::T_Overview );
334  dirList->setTableType( NCFileTable::T_Overview );
335  }
336 
337  fileList->fillList();
338 
339  dirList->fillList();
340  }
341  else if ( postevent.widget == fileList )
342  {
343  if ( postevent.result != "" )
344  {
345  fileName->setValue( postevent.result );
346  }
347  }
348  else
349  {
350  postevent.result = "";
351  return false;
352  }
353 
354  if ( postevent.widget == cancelButton ||
355  postevent == NCursesEvent::cancel )
356  {
357  postevent.result = "";
358  return false;
359  }
360 
361  return true;
362 }
363 
364 
365 bool NCAskForFile::getCheckBoxValue( NCCheckBox * checkBox )
366 {
367  if ( checkBox )
368  {
369  // return whether the option is selected or not
370  return ( checkBox->isChecked() );
371  }
372 
373  return false;
374 }
375 
376 
377 NCAskForExistingFile::NCAskForExistingFile( const wpos at,
378  const std::string & iniDir,
379  const std::string & filter,
380  const std::string & headline )
381  : NCAskForFile( at, iniDir, filter, headline )
382 {
383  createLayout( iniDir,
384  filter,
385  headline,
386  false ); // file name is not editable
387 }
388 
389 
390 std::string NCAskForExistingFile::getFileName()
391 {
392  if ( fileName->value() == "" )
393  return fileList->getCurrentFile();
394  else
395  return fileName->value();
396 }
397 
398 
399 NCAskForSaveFileName::NCAskForSaveFileName( const wpos at,
400  const std::string & iniDir,
401  const std::string & filter,
402  const std::string & headline )
403  : NCAskForFile( at, iniDir, filter, headline )
404 {
405  createLayout( iniDir,
406  filter,
407  headline,
408  true ); // file name is editable
409 }
410 
411 
412 std::string NCAskForSaveFileName::getFileName()
413 {
414  return fileName->value();
415 }
Definition: position.h:109
virtual bool fillList()
void setStartDir(const std::string &start)
void setTableType(NCFileSelectionType type)
NCursesEvent & showDirPopup()
void updateFileList()
virtual int preferredWidth()
void createLayout(const std::string &iniDir, const std::string &filter, const std::string &headline, bool editable)
std::string getCurrentDir()
virtual bool fillList()