1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YUIQt_builtins.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
21 #define USE_QT_CURSORS 1
22 #define FORCE_UNICODE_FONT 0
28 #include <q3filedialog.h>
29 #include <qmessagebox.h>
31 #include <qinputdialog.h>
34 #include <ycp/YCPTerm.h>
35 #define y2log_component "qt-ui"
36 #include <ycp/y2log.h>
40 #include "YMacroRecorder.h"
41 #include "YUISymbols.h"
43 #include "YQSignalBlocker.h"
51 #define DEFAULT_MACRO_FILE_NAME "macro.ycp"
56 YQUI::glyph( const YCPSymbol & glyphSymbol )
58 string sym = glyphSymbol->symbol();
61 // Hint: Use 'xfd' to view characters available in the Unicode font.
63 if ( sym == YUIGlyph_ArrowLeft ) unicodeChar = QChar( 0x2190 );
64 else if ( sym == YUIGlyph_ArrowRight ) unicodeChar = QChar( 0x2192 );
65 else if ( sym == YUIGlyph_ArrowUp ) unicodeChar = QChar( 0x2191 );
66 else if ( sym == YUIGlyph_ArrowDown ) unicodeChar = QChar( 0x2193 );
67 else if ( sym == YUIGlyph_CheckMark ) unicodeChar = QChar( 0x2714 );
68 else if ( sym == YUIGlyph_BulletArrowRight ) unicodeChar = QChar( 0x279c );
69 else if ( sym == YUIGlyph_BulletCircle ) unicodeChar = QChar( 0x274d );
70 else if ( sym == YUIGlyph_BulletSquare ) unicodeChar = QChar( 0x274f );
71 else return YCPString( "" );
73 QString qstr( unicodeChar );
75 return YCPString( toUTF8( qstr ) );
79 YCPValue YQUI::runPkgSelection( YWidget * packageSelector )
81 y2milestone( "Running package selection..." );
82 _wm_close_blocked = true;
83 _auto_activate_dialogs = false;
85 YCPValue input = YCPVoid();
89 input = evaluateUserInput();
91 catch (const std::exception & e)
93 y2error( "Caught std::exception: %s", e.what() );
94 y2error( "This is a libzypp problem. Do not file a bug against the UI!" );
98 y2error( "Caught unspecified exception." );
99 y2error( "This is a libzypp problem. Do not file a bug against the UI!" );
102 _auto_activate_dialogs = true;
103 _wm_close_blocked = false;
104 y2milestone( "Package selection done - returning %s", input->toString().c_str() );
110 void YQUI::makeScreenShot( std::string stl_filename )
114 // Grab the pixels off the screen
117 QWidget * dialog = (QWidget *) YDialog::currentDialog()->widgetRep();
118 QPixmap screenShot = QPixmap::grabWindow( dialog->winId() );
119 XSync( dialog->x11Display(), false );
120 QString fileName ( stl_filename.c_str() );
121 bool interactive = false;
123 if ( fileName.isEmpty() )
127 // Open a file selection box. Figure out a reasonable default
128 // directory / file name.
130 if ( screenShotNameTemplate.isEmpty() )
133 // Initialize screen shot directory
136 QString home = QDir::homeDirPath();
137 char * ssdir = getenv("Y2SCREENSHOTS");
138 QString dir = ssdir ? ssdir : "yast2-screen-shots";
142 // Special case: $HOME is not set. This is normal in the inst-sys.
143 // In this case, rather than simply dumping all screen shots into
144 // /tmp which is world-writable, let's try to create a subdirectory
145 // below /tmp with restrictive permissions.
146 // If that fails, trust nobody - in particular, do not suggest /tmp
147 // as the default in the file selection box.
151 if ( mkdir( dir, 0700 ) == -1 )
156 // For all others let's create a directory ~/yast2-screen-shots and
157 // simply ignore if this is already present. This gives the user a
158 // chance to create symlinks to a better location if he wishes so.
160 dir = home + "/" + dir;
161 (void) mkdir( dir, 0750 );
164 screenShotNameTemplate = dir + "/%s-%03d.png";
169 // Figure out a file name
172 const char * baseName = moduleName();
173 if ( ! baseName ) baseName = "scr";
174 int no = screenShotNo[ baseName ];
175 fileName.sprintf( screenShotNameTemplate, baseName, no );
176 y2debug( "screenshot: %s", (const char *) fileName );
179 YQSignalBlocker sigBlocker( &_user_input_timer );
181 fileName = askForSaveFileName( fileName, QString( "*.png" ) , _( "Save screen shot to..." ) );
184 if ( fileName.isEmpty() )
186 y2debug( "Save screen shot canceled by user" );
190 screenShotNo.insert( baseName, ++no );
191 } // if fileName.isEmpty()
195 // Actually save the screen shot
198 y2debug( "Saving screen shot to %s", (const char *) fileName );
199 bool success = screenShot.save( fileName, "PNG" );
203 y2error( "Couldn't save screen shot %s", (const char *) fileName );
207 QMessageBox::warning( 0, // parent
209 QString( "Couldn't save screen shot\nto %1" ).arg( fileName ),
210 QMessageBox::Ok | QMessageBox::Default, // button0
211 Qt::NoButton, // button1
212 Qt::NoButton ); // button2
216 if ( recordingMacro() )
218 macroRecorder->beginBlock();
219 YDialog::currentDialog()->saveUserInput( macroRecorder );
220 macroRecorder->recordMakeScreenShot( true, (const char *) fileName );
221 macroRecorder->recordUserInput( YCPVoid() );
222 macroRecorder->endBlock();
227 void YQUI::askSaveLogs()
229 QString fileName = askForSaveFileName( "/tmp/y2logs.tgz", // startWith
230 "*.tgz *.tar.gz", // filter
231 "Save y2logs to..." ); // headline
233 if ( ! fileName.isEmpty() )
235 QString saveLogsCommand = "/sbin/save_y2logs";
237 if ( access( saveLogsCommand.ascii(), X_OK ) == 0 )
239 saveLogsCommand += " '" + fileName + "'";
240 y2milestone( "Saving y2logs: %s", saveLogsCommand.ascii() );
241 int result = system( saveLogsCommand.ascii() );
245 y2error( "Error saving y2logs: \"%s\" exited with %d",
246 (const char *) saveLogsCommand, result );
247 QMessageBox::warning( 0, // parent
249 QString( "Couldn't save y2logs to %1 - "
250 "exit code %2" ).arg( fileName ).arg( result ),
251 QMessageBox::Ok | QMessageBox::Default, // button0
252 QMessageBox::NoButton, // button1
253 QMessageBox::NoButton ); // button2
257 y2milestone( "y2logs saved to %s", (const char *) fileName );
262 y2error( "Error saving y2logs: Command %s not found",
263 saveLogsCommand.ascii() );
265 QMessageBox::warning( 0, // parent
267 QString( "Couldn't save y2logs to %1:\n"
268 "Command %2 not found" ).arg( fileName ).arg( saveLogsCommand ),
269 QMessageBox::Ok | QMessageBox::Default, // button0
270 QMessageBox::NoButton, // button1
271 QMessageBox::NoButton ); // button2
277 void YQUI::askConfigureLogging()
279 bool okButtonPressed = false;
281 items << "Debug logging off"
282 << "Debug logging on";
284 QString result = QInputDialog::getItem( "YaST2 Logging", // caption
285 "Configure YaST2 Logging:", // label
287 get_log_debug() ? 1 : 0,
290 _main_win ); // parent
292 if ( okButtonPressed )
294 set_log_debug( result.endsWith( "on" ) );
295 y2milestone( "Changing logging: %s - %s", (const char *) result,
296 get_log_debug() ? "y2debug on" : "y2debug off" );
301 void YQUI::toggleRecordMacro()
303 if ( recordingMacro() )
308 QMessageBox::information( 0, // parent
309 "YaST2 Macro Recorder", // caption
310 "Macro recording done.", // text
311 QMessageBox::Ok | QMessageBox::Default, // button0
312 QMessageBox::NoButton, // button1
313 QMessageBox::NoButton ); // button2
320 Q3FileDialog::getSaveFileName( DEFAULT_MACRO_FILE_NAME, // startWith
324 "Select Macro File to Record to" ); // caption
326 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled
328 recordMacro( (const char *) filename );
334 void YQUI::askPlayMacro()
339 Q3FileDialog::getOpenFileName( DEFAULT_MACRO_FILE_NAME, // startWith
343 "Select Macro File to Play" ); // caption
346 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled
348 playMacro( (const char *) filename );
350 // Do special magic to get out of any UserInput() loop right now
351 // without doing any harm - otherwise this would hang until the next
352 // mouse click on a PushButton etc.
354 sendEvent( new YEvent() );
365 YCPValue YQUI::askForExistingDirectory( const YCPString & startDir,
366 const YCPString & headline )
371 Q3FileDialog::getExistingDirectory( fromUTF8( startDir->value() ),
373 "dir_selector", // name
374 fromUTF8( headline->value() ) ); // caption
377 if ( dir_name.isEmpty() ) // this includes dir_name.isNull()
378 return YCPVoid(); // nothing selected -> return 'nil'
380 return YCPString( toUTF8( dir_name ) );
384 YCPValue YQUI::askForExistingFile( const YCPString & startWith,
385 const YCPString & filter,
386 const YCPString & headline )
391 Q3FileDialog::getOpenFileName( fromUTF8( startWith->value() ),
392 fromUTF8( filter->value() ),
394 "file_selector", // name
395 fromUTF8( headline->value() ) ); // caption
398 if ( file_name.isEmpty() ) // this includes file_name.isNull()
399 return YCPVoid(); // nothing selected -> return 'nil'
401 return YCPString( toUTF8( file_name ) );
405 YCPValue YQUI::askForSaveFileName( const YCPString & startWith,
406 const YCPString & filter,
407 const YCPString & headline )
411 QString file_name = askForSaveFileName( fromUTF8( startWith->value() ),
412 fromUTF8( filter->value() ),
413 fromUTF8( headline->value() ) );
416 if ( file_name.isEmpty() ) // this includes file_name.isNull()
417 return YCPVoid(); // nothing selected -> return 'nil'
419 return YCPString( toUTF8( file_name ) );
424 QString YQUI::askForSaveFileName( const QString & startWith,
425 const QString & filter,
426 const QString & headline )
429 bool try_again = false;
433 // Leave the mouse cursor alone - this function might be called from
434 // some other widget, not only from UI::AskForSaveFileName().
436 file_name = Q3FileDialog::getSaveFileName( startWith,
439 "file_selector", // name
440 headline ); // caption
442 if ( file_name.isEmpty() ) // this includes file_name.isNull()
443 return QString::null;
446 if ( access( (const char *) file_name, F_OK ) == 0 ) // file exists?
450 if ( access( (const char *) file_name, W_OK ) == 0 )
452 // Confirm if the user wishes to overwrite an existing file
453 msg = ( _( "%1 exists! Really overwrite?" ) ).arg( file_name );
457 // Confirm if the user wishes to overwrite a write-protected file %1
458 msg = ( _( "%1 exists and is write-protected!\nReally overwrite?" ) ).arg( file_name );
461 int button_no = QMessageBox::information( _main_win,
462 // Window title for confirmation dialog
467 try_again = ( button_no != 0 );
470 } while ( try_again );