libyui-qt
2.43.5
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YUIQt_builtins.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 Textdomain "qt" 00024 00025 /-*/ 00026 00027 #define USE_QT_CURSORS 1 00028 #define FORCE_UNICODE_FONT 0 00029 00030 #include <sys/stat.h> 00031 #include <unistd.h> 00032 00033 #include <QCursor> 00034 #include <QFileDialog> 00035 #include <QX11Info> 00036 #include <QMessageBox> 00037 #include <QPixmap> 00038 #include <QInputDialog> 00039 #include <qdir.h> 00040 00041 #define YUILogComponent "qt-ui" 00042 #include <yui/YUILog.h> 00043 00044 #include "YQUI.h" 00045 #include <yui/YEvent.h> 00046 #include <yui/YMacro.h> 00047 #include <yui/YUISymbols.h> 00048 #include "YQDialog.h" 00049 #include "YQSignalBlocker.h" 00050 #include "YQApplication.h" 00051 00052 #include "utf8.h" 00053 #include "YQi18n.h" 00054 00055 #include <X11/Xlib.h> 00056 00057 00058 #define DEFAULT_MACRO_FILE_NAME "macro.ycp" 00059 00060 00061 00062 YEvent * YQUI::runPkgSelection( YWidget * packageSelector ) 00063 { 00064 YUI_CHECK_PTR( packageSelector ); 00065 YEvent * event = 0; 00066 00067 try 00068 { 00069 event = packageSelector->findDialog()->waitForEvent(); 00070 } 00071 catch ( YUIException & uiEx ) 00072 { 00073 YUI_CAUGHT( uiEx ); 00074 } 00075 catch ( std::exception & e) 00076 { 00077 yuiError() << "Caught std::exception: " << e.what() << "\n" 00078 << "This is a libzypp problem. Do not file a bug against the UI!" 00079 << std::endl; 00080 } 00081 catch (...) 00082 { 00083 yuiError() << "Caught unspecified exception.\n" 00084 << "This is a libzypp problem. Do not file a bug against the UI!" 00085 << std::endl; 00086 } 00087 00088 return event; 00089 } 00090 00091 00092 void YQUI::makeScreenShot( std::string stl_filename ) 00093 { 00094 // 00095 // Grab the pixels off the screen 00096 // 00097 00098 QWidget * dialog = (QWidget *) YDialog::currentDialog()->widgetRep(); 00099 00100 QPixmap screenShot = QPixmap::grabWindow( dialog->topLevelWidget()->winId() ); 00101 XSync( QX11Info::display(), false ); 00102 QString fileName ( stl_filename.c_str() ); 00103 bool interactive = false; 00104 00105 if ( fileName.isEmpty() ) 00106 { 00107 interactive = true; 00108 00109 // Open a file selection box. Figure out a reasonable default 00110 // directory / file name. 00111 00112 if ( screenShotNameTemplate.isEmpty() ) 00113 { 00114 // 00115 // Initialize screen shot directory 00116 // 00117 00118 QString home = QDir::homePath(); 00119 char * ssdir = getenv( "Y2SCREENSHOTS" ); 00120 QString dir = ssdir ? fromUTF8( ssdir ) : "yast2-screen-shots"; 00121 00122 if ( home == "/" ) 00123 { 00124 // Special case: $HOME is not set. This is normal in the inst-sys. 00125 // In this case, rather than simply dumping all screen shots into 00126 // /tmp which is world-writable, let's try to create a subdirectory 00127 // below /tmp with restrictive permissions. 00128 // If that fails, trust nobody - in particular, do not suggest /tmp 00129 // as the default in the file selection box. 00130 00131 dir = "/tmp/" + dir; 00132 00133 if ( mkdir( toUTF8( dir ).c_str(), 0700 ) == -1 ) 00134 dir = ""; 00135 } 00136 else 00137 { 00138 // For all others let's create a directory ~/yast2-screen-shots and 00139 // simply ignore if this is already present. This gives the user a 00140 // chance to create symlinks to a better location if he wishes so. 00141 00142 dir = home + "/" + dir; 00143 (void) mkdir( toUTF8( dir ).c_str(), 0750 ); 00144 } 00145 00146 screenShotNameTemplate = dir + "/%s-%03d.png"; 00147 } 00148 00149 00150 // 00151 // Figure out a file name 00152 // 00153 00154 const char * baseName = "yast2"; 00155 00156 int no = screenShotNo[ baseName ]; 00157 fileName.sprintf( qPrintable( screenShotNameTemplate ), baseName, no ); 00158 yuiDebug() << "Screenshot: " << fileName << std::endl; 00159 00160 { 00161 fileName = YQApplication::askForSaveFileName( fileName, 00162 QString( "*.png" ) , 00163 _( "Save screen shot to..." ) ); 00164 } 00165 00166 if ( fileName.isEmpty() ) 00167 { 00168 yuiDebug() << "Save screen shot canceled by user" << std::endl; 00169 return; 00170 } 00171 00172 screenShotNo.insert( baseName, ++no ); 00173 } // if fileName.isEmpty() 00174 00175 00176 // 00177 // Actually save the screen shot 00178 // 00179 00180 yuiDebug() << "Saving screen shot to " << fileName << std::endl; 00181 bool success = screenShot.save( fileName, "PNG" ); 00182 00183 if ( ! success ) 00184 { 00185 yuiError() << "Couldn't save screen shot " << fileName << std::endl; 00186 00187 if ( interactive ) 00188 { 00189 QWidget* parent = 0; 00190 YDialog * currentDialog = YDialog::currentDialog( false ); 00191 00192 if (currentDialog) 00193 parent = (QWidget *) currentDialog->widgetRep(); 00194 00195 QMessageBox::warning( parent, // parent 00196 "Error", // caption 00197 QString( "Couldn't save screen shot\nto %1" ).arg( fileName ), 00198 QMessageBox::Ok | QMessageBox::Default, // button0 00199 Qt::NoButton, // button1 00200 Qt::NoButton ); // button2 00201 } 00202 } 00203 } 00204 00205 00206 void YQUI::askSaveLogs() 00207 { 00208 QString fileName = YQApplication::askForSaveFileName( QString( "/tmp/y2logs.tgz" ), // startWith 00209 QString( "*.tgz *.tar.gz" ), // filter 00210 QString( "Save y2logs to..." ) ); // headline 00211 00212 QWidget* parent = 0; 00213 YDialog * currentDialog = YDialog::currentDialog( false ); 00214 00215 if (currentDialog) 00216 parent = (QWidget *) currentDialog->widgetRep(); 00217 00218 if ( ! fileName.isEmpty() ) 00219 { 00220 QString saveLogsCommand = "/sbin/save_y2logs"; 00221 00222 if ( access( saveLogsCommand.toAscii(), X_OK ) == 0 ) 00223 { 00224 saveLogsCommand += " '" + fileName + "'"; 00225 yuiMilestone() << "Saving y2logs: " << saveLogsCommand << std::endl; 00226 int result = system( qPrintable( saveLogsCommand ) ); 00227 00228 if ( result != 0 ) 00229 { 00230 yuiError() << "Error saving y2logs: \"" << saveLogsCommand 00231 << "\" exited with " << result 00232 << std::endl; 00233 00234 QMessageBox::warning( parent, // parent 00235 "Error", // caption 00236 QString( "Couldn't save y2logs to %1 - " 00237 "exit code %2" ).arg( fileName ).arg( result ), 00238 QMessageBox::Ok | QMessageBox::Default, // button0 00239 QMessageBox::NoButton, // button1 00240 QMessageBox::NoButton ); // button2 00241 } 00242 else 00243 { 00244 yuiMilestone() << "y2logs saved to " << fileName << std::endl; 00245 } 00246 } 00247 else 00248 { 00249 yuiError() << "Error saving y2logs: Command \"" 00250 << saveLogsCommand << "\" not found" 00251 << std::endl; 00252 00253 QMessageBox::warning( parent, // parent 00254 "Error", // caption 00255 QString( "Couldn't save y2logs to %1:\n" 00256 "Command %2 not found" ).arg( fileName ).arg( saveLogsCommand ), 00257 QMessageBox::Ok | QMessageBox::Default, // button0 00258 QMessageBox::NoButton, // button1 00259 QMessageBox::NoButton ); // button2 00260 } 00261 } 00262 } 00263 00264 00265 void YQUI::askConfigureLogging() 00266 { 00267 bool okButtonPressed = false; 00268 QStringList items; 00269 items << "Debug logging off" 00270 << "Debug logging on"; 00271 00272 00273 QWidget* parent = 0; 00274 YDialog * currentDialog = YDialog::currentDialog( false ); 00275 00276 if (currentDialog) 00277 parent = (QWidget *) currentDialog->widgetRep(); 00278 00279 QString result = QInputDialog::getItem( parent, 00280 _("YaST Logging"), 00281 _("Configure YaST Logging:"), 00282 items, 0, 00283 YUILog::debugLoggingEnabled() ? 1 : 0, 00284 &okButtonPressed ); 00285 if ( okButtonPressed ) 00286 { 00287 YUILog::enableDebugLogging( result.endsWith( "on" ) ); 00288 yuiMilestone() << "Changing logging: " << result << std::endl; 00289 } 00290 } 00291 00292 00293 void YQUI::toggleRecordMacro() 00294 { 00295 QWidget* parent = 0; 00296 YDialog * currentDialog = YDialog::currentDialog( false ); 00297 00298 if (currentDialog) 00299 parent = (QWidget *) currentDialog->widgetRep(); 00300 00301 00302 if ( YMacro::recording() ) 00303 { 00304 YMacro::endRecording(); 00305 normalCursor(); 00306 00307 QMessageBox::information( parent, // parent 00308 "YaST2 Macro Recorder", // caption 00309 "Macro recording done.", // text 00310 QMessageBox::Ok | QMessageBox::Default, // button0 00311 QMessageBox::NoButton, // button1 00312 QMessageBox::NoButton ); // button2 00313 } 00314 else 00315 { 00316 normalCursor(); 00317 00318 QString filename = 00319 QFileDialog::getSaveFileName( parent, 00320 "Select Macro File to Record to", 00321 DEFAULT_MACRO_FILE_NAME, // startWith 00322 "*.ycp", // filter 00323 0, // selectedFilter 00324 QFileDialog::DontUseNativeDialog 00325 ); 00326 00327 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled 00328 { 00329 YMacro::record( toUTF8( filename ) ); 00330 } 00331 } 00332 } 00333 00334 00335 void YQUI::askPlayMacro() 00336 { 00337 normalCursor(); 00338 00339 QWidget* parent = 0; 00340 YDialog * currentDialog = YDialog::currentDialog( false ); 00341 00342 if (currentDialog) 00343 parent = (QWidget *) currentDialog->widgetRep(); 00344 00345 00346 QString filename = 00347 QFileDialog::getOpenFileName( parent, 00348 "Select Macro File to Play", 00349 DEFAULT_MACRO_FILE_NAME, // startWith 00350 "*.ycp", 0, QFileDialog::DontUseNativeDialog ); 00351 busyCursor(); 00352 00353 if ( ! filename.isEmpty() ) // file selection dialog has been cancelled 00354 { 00355 YMacro::play( toUTF8( filename ) ); 00356 00357 // Do special magic to get out of any UserInput() loop right now 00358 // without doing any harm - otherwise this would hang until the next 00359 // mouse click on a PushButton etc. 00360 00361 sendEvent( new YEvent() ); 00362 } 00363 } 00364 00365 00366 // EOF