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 <QFileDialog>
30 #include <QMessageBox>
32 #include <QInputDialog>
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 YCPValue input = YCPVoid();
86 input = evaluateUserInput();
88 catch (const std::exception & e)
90 y2error( "Caught std::exception: %s", e.what() );
91 y2error( "This is a libzypp problem. Do not file a bug against the UI!" );
95 y2error( "Caught unspecified exception." );
96 y2error( "This is a libzypp problem. Do not file a bug against the UI!" );
99 y2milestone( "Package selection done - returning %s", input->toString().c_str() );
105 void YQUI::makeScreenShot( std::string stl_filename )
109 // Grab the pixels off the screen
112 QWidget * dialog = (QWidget *) YDialog::currentDialog()->widgetRep();
113 QPixmap screenShot = QPixmap::grabWindow( dialog->winId() );
114 XSync( QX11Info::display(), false );
115 QString fileName ( stl_filename.c_str() );
116 bool interactive = false;
118 if ( fileName.isEmpty() )
122 // Open a file selection box. Figure out a reasonable default
123 // directory / file name.
125 if ( screenShotNameTemplate.isEmpty() )
128 // Initialize screen shot directory
131 QString home = QDir::homePath();
132 char * ssdir = getenv("Y2SCREENSHOTS");
133 QString dir = ssdir ? ssdir : "yast2-screen-shots";
137 // Special case: $HOME is not set. This is normal in the inst-sys.
138 // In this case, rather than simply dumping all screen shots into
139 // /tmp which is world-writable, let's try to create a subdirectory
140 // below /tmp with restrictive permissions.
141 // If that fails, trust nobody - in particular, do not suggest /tmp
142 // as the default in the file selection box.
146 if ( mkdir( qPrintable(dir), 0700 ) == -1 )
151 // For all others let's create a directory ~/yast2-screen-shots and
152 // simply ignore if this is already present. This gives the user a
153 // chance to create symlinks to a better location if he wishes so.
155 dir = home + "/" + dir;
156 (void) mkdir( qPrintable(dir), 0750 );
159 screenShotNameTemplate = dir + "/%s-%03d.png";
164 // Figure out a file name
167 const char * baseName = moduleName();
168 if ( ! baseName ) baseName = "scr";
169 int no = screenShotNo[ baseName ];
170 fileName.sprintf( qPrintable(screenShotNameTemplate), baseName, no );
171 y2debug( "screenshot: %s", qPrintable(fileName) );
174 YQSignalBlocker sigBlocker( _user_input_timer );
176 fileName = askForSaveFileName( fileName, QString( "*.png" ) , _( "Save screen shot to..." ) );
179 if ( fileName.isEmpty() )
181 y2debug( "Save screen shot canceled by user" );
185 screenShotNo.insert( baseName, ++no );
186 } // if fileName.isEmpty()
190 // Actually save the screen shot
193 y2debug( "Saving screen shot to %s", qPrintable(fileName) );
194 bool success = screenShot.save( fileName, "PNG" );
198 y2error( "Couldn't save screen shot %s", qPrintable(fileName) );
202 QMessageBox::warning( 0, // parent
204 QString( "Couldn't save screen shot\nto %1" ).arg( fileName ),
205 QMessageBox::Ok | QMessageBox::Default, // button0
206 Qt::NoButton, // button1
207 Qt::NoButton ); // button2
211 if ( recordingMacro() )
213 macroRecorder->beginBlock();
214 YDialog::currentDialog()->saveUserInput( macroRecorder );
215 macroRecorder->recordMakeScreenShot( true, qPrintable(fileName) );
216 macroRecorder->recordUserInput( YCPVoid() );
217 macroRecorder->endBlock();
222 void YQUI::askSaveLogs()
224 QString fileName = askForSaveFileName( "/tmp/y2logs.tgz", // startWith
225 "*.tgz *.tar.gz", // filter
226 "Save y2logs to..." ); // headline
228 if ( ! fileName.isEmpty() )
230 QString saveLogsCommand = "/sbin/save_y2logs";
232 if ( access( saveLogsCommand.toAscii(), X_OK ) == 0 )
234 saveLogsCommand += " '" + fileName + "'";
235 y2milestone( "Saving y2logs: %s", qPrintable(saveLogsCommand) );
236 int result = system( qPrintable(saveLogsCommand) );
240 y2error( "Error saving y2logs: \"%s\" exited with %d",
241 qPrintable(saveLogsCommand), result );
242 QMessageBox::warning( 0, // parent
244 QString( "Couldn't save y2logs to %1 - "
245 "exit code %2" ).arg( fileName ).arg( result ),
246 QMessageBox::Ok | QMessageBox::Default, // button0
247 QMessageBox::NoButton, // button1
248 QMessageBox::NoButton ); // button2
252 y2milestone( "y2logs saved to %s", qPrintable(fileName) );
257 y2error( "Error saving y2logs: Command %s not found",
258 qPrintable(saveLogsCommand) );
260 QMessageBox::warning( 0, // parent
262 QString( "Couldn't save y2logs to %1:\n"
263 "Command %2 not found" ).arg( fileName ).arg( saveLogsCommand ),
264 QMessageBox::Ok | QMessageBox::Default, // button0
265 QMessageBox::NoButton, // button1
266 QMessageBox::NoButton ); // button2
272 void YQUI::askConfigureLogging()
274 bool okButtonPressed = false;
276 items << "Debug logging off"
277 << "Debug logging on";
279 QString result = QInputDialog::getItem( _main_win,
281 tr("Configure YaST2 Logging:"),
282 items, 0, get_log_debug() ? 1 : 0, &okButtonPressed);
283 if ( okButtonPressed )
285 set_log_debug( result.endsWith( "on" ) );
286 y2milestone( "Changing logging: %s - %s", qPrintable(result),
287 get_log_debug() ? "y2debug on" : "y2debug off" );
292 void YQUI::toggleRecordMacro()
294 if ( recordingMacro() )
299 QMessageBox::information( 0, // parent
300 "YaST2 Macro Recorder", // caption
301 "Macro recording done.", // text
302 QMessageBox::Ok | QMessageBox::Default, // button0
303 QMessageBox::NoButton, // button1
304 QMessageBox::NoButton ); // button2
311 QFileDialog::getSaveFileName( 0,
312 "Select Macro File to Record to",
313 DEFAULT_MACRO_FILE_NAME, // startWith
317 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled
319 recordMacro( qPrintable(filename) );
325 void YQUI::askPlayMacro()
330 QFileDialog::getOpenFileName( 0,
331 "Select Macro File to Play",
332 DEFAULT_MACRO_FILE_NAME, // startWith
336 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled
338 playMacro( qPrintable(filename) );
340 // Do special magic to get out of any UserInput() loop right now
341 // without doing any harm - otherwise this would hang until the next
342 // mouse click on a PushButton etc.
344 sendEvent( new YEvent() );
355 YCPValue YQUI::askForExistingDirectory( const YCPString & startDir,
356 const YCPString & headline )
361 QFileDialog::getExistingDirectory( _main_win,
362 fromUTF8( headline->value() ),
363 fromUTF8( startDir->value() ) );
366 if ( dir_name.isEmpty() ) // this includes dir_name.isNull()
367 return YCPVoid(); // nothing selected -> return 'nil'
369 return YCPString( toUTF8( dir_name ) );
373 YCPValue YQUI::askForExistingFile( const YCPString & startWith,
374 const YCPString & filter,
375 const YCPString & headline )
380 QFileDialog::getOpenFileName( _main_win, // parent
381 fromUTF8( headline->value() ),
382 fromUTF8( startWith->value() ),
383 fromUTF8( filter->value() ) );
386 if ( file_name.isEmpty() ) // this includes file_name.isNull()
387 return YCPVoid(); // nothing selected -> return 'nil'
389 return YCPString( toUTF8( file_name ) );
393 YCPValue YQUI::askForSaveFileName( const YCPString & startWith,
394 const YCPString & filter,
395 const YCPString & headline )
399 QString file_name = askForSaveFileName( fromUTF8( startWith->value() ),
400 fromUTF8( filter->value() ),
401 fromUTF8( headline->value() ) );
404 if ( file_name.isEmpty() ) // this includes file_name.isNull()
405 return YCPVoid(); // nothing selected -> return 'nil'
407 return YCPString( toUTF8( file_name ) );
412 QString YQUI::askForSaveFileName( const QString & startWith,
413 const QString & filter,
414 const QString & headline )
417 bool try_again = false;
421 // Leave the mouse cursor alone - this function might be called from
422 // some other widget, not only from UI::AskForSaveFileName().
424 file_name = QFileDialog::getSaveFileName( _main_win,
429 if ( file_name.isEmpty() ) // this includes file_name.isNull()
430 return QString::null;
433 if ( access( qPrintable(file_name), F_OK ) == 0 ) // file exists?
437 if ( access( qPrintable(file_name), W_OK ) == 0 )
439 // Confirm if the user wishes to overwrite an existing file
440 msg = ( _( "%1 exists! Really overwrite?" ) ).arg( file_name );
444 // Confirm if the user wishes to overwrite a write-protected file %1
445 msg = ( _( "%1 exists and is write-protected!\nReally overwrite?" ) ).arg( file_name );
448 int button_no = QMessageBox::information( _main_win,
449 // Window title for confirmation dialog
454 try_again = ( button_no != 0 );
457 } while ( try_again );