1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
19 #include <rpc/types.h> // MAXHOSTNAMELEN
24 #include <QMessageBox>
25 #include <QSocketNotifier>
26 #include <QStackedWidget>
27 #include <QDesktopWidget>
29 #include <QVBoxLayout>
31 #include <ycp/YCPTerm.h>
32 #include <ycp/YCPCode.h>
34 #define y2log_component "qt-ui"
35 #include <ycp/y2log.h>
38 #include "QY2Styler.h"
39 #include "YQApplication.h"
40 #include "YQWidgetFactory.h"
41 #include "YQOptionalWidgetFactory.h"
43 #include "YUISymbols.h"
47 #include "QY2Settings.h"
50 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
54 static void qMessageHandler( QtMsgType type, const char * msg );
56 YQUI::YQUI( int argc, char **argv, bool with_threads, const char * macro_file )
60 , _do_exit_loop( false )
63 y2milestone( "YQUI constructor start" );
68 _usingVisionImpairedPalette = false;
69 _leftHandedMouse = false;
70 _askedForLeftHandedMouse = false;
72 screenShotNameTemplate = "";
75 qInstallMsgHandler( qMessageHandler );
77 // Copy command line arguments for QApplication
79 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
82 for ( int i=0; i < argc; i++ )
83 _ui_argv[i+1] = strdup( argv[i] );
85 _ui_argv[0] = strdup( "YaST2" );
88 topmostConstructorHasFinished();
98 Qt thinks the first QObject defines the main thread, but
99 the main thread in this case is the ycp thread, not UI
101 extern void qt_set_current_thread_to_main_thread();
102 qt_set_current_thread_to_main_thread();
107 new QApplication( _ui_argc, _ui_argv);
108 _busy_cursor_timer = new QTimer( qApp );
109 _busy_cursor_timer->setSingleShot( true );
111 _user_input_timer = new QTimer( qApp );
112 _user_input_timer->setSingleShot( true );
114 _normalPalette = qApp->palette();
116 // Qt keeps track to a global QApplication in qApp.
119 processCommandLineArgs( _ui_argc, _ui_argv );
122 _styler = new QY2Styler( qApp );
123 QString style = getenv("Y2STYLE");
124 if ( !style.isNull() )
125 _styler->setStyleSheet( style );
127 // Event loop object. Required since a YaST2 UI needs to react to commands
128 // from the YCP command stream as well as to X11 / Qt events.
129 _eventLoop = new QEventLoop( qApp );
130 _do_exit_loop = false;
132 // Create main window for `opt(`defaultsize) dialogs.
134 // We have to use something else than QWidgetStack since QWidgetStack
135 // doesn't accept a WFlags arg which we badly need here.
137 _main_win = new QWidget( 0, Qt::Window ); // parent, wflags
138 _main_win->setFocusPolicy( Qt::StrongFocus );
139 _main_win->setObjectName( "main_window" );
141 _main_win->resize( _default_size );
144 _main_win->move( 0, 0 );
149 QString title( "YaST2" );
150 char hostname[ MAXHOSTNAMELEN+1 ];
152 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
154 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
156 if ( strlen( hostname ) > 0 )
158 if ( ( strcmp( hostname, "(none)" ) != 0 &&
159 strcmp( hostname, "linux" ) != 0 ) )
167 _main_win->setWindowTitle( title );
170 // Hide the main window for now. The first call to UI::OpenDialog() on an
171 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
172 // the main window - there is nothing to display yet.
177 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
178 // if no Qt style defined):
180 // Qt does not seem to be designed for use in plugin libs. It loads some
181 // add-on libs dynamically with dlopen() and unloads them at program exit
182 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
183 // lib (libqt-mt) themselves, when they are unloading the last call to
184 // dlclose() for them causes the last reference to libqt-mt to vanish as
185 // well. Since libqt-mt is already in the process of destruction there is
186 // no more reference from the caller of libqt-mt, and the GLIBC decides
187 // that libqt-mt is not needed any more (zero references) and unmaps
188 // libqt-mt. When the static destructor of libqt-mt that triggered the
189 // cleanup in QGPluginManager returns, the code it is to return to is
190 // already unmapped, causing a segfault.
192 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
193 // and make sure there is no corresponding dlclose().
195 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
196 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
197 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
198 qt_lib ? "successful" : "failed" );
202 qApp->setFont( yqApp()->currentFont() );
205 connect( _user_input_timer, SIGNAL( timeout() ),
206 this, SLOT ( userInputTimeout() ) );
208 connect( _busy_cursor_timer, SIGNAL( timeout() ),
209 this, SLOT ( busyCursor() ) );
213 // playMacro( macro_file );
215 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
216 qApp->processEvents();
223 return static_cast<YQApplication *>( app() );
227 void YQUI::processCommandLineArgs( int argc, char **argv )
231 for( int i=0; i < argc; i++ )
233 QString opt = argv[i];
235 y2milestone ("Qt argument: %s", argv[i]);
237 // Normalize command line option - accept "--xy" as well as "-xy"
239 if ( opt.startsWith( "--" ) )
242 if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
243 else if ( opt == QString( "-noborder" ) ) _noborder = true;
244 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
245 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
246 // --macro is handled by YUI_component
247 else if ( opt == QString( "-help" ) )
250 "Command line options for the YaST2 Qt UI:\n"
252 "--nothreads run without additional UI threads\n"
253 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
254 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
255 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
256 "--help this help text\n"
258 "--macro <macro-file> play a macro right on startup\n"
260 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
261 "to maintain backwards compatibility.\n"
270 // Qt handles command line option "-reverse" for Arabic / Hebrew
277 y2debug("Closing down Qt UI.");
281 // Intentionally NOT calling dlclose() to libqt-mt
282 // (see constructor for explanation)
288 YQUI::createWidgetFactory()
290 YQWidgetFactory * factory = new YQWidgetFactory();
291 YUI_CHECK_NEW( factory );
298 YOptionalWidgetFactory *
299 YQUI::createOptionalWidgetFactory()
301 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
302 YUI_CHECK_NEW( factory );
309 YQUI::createApplication()
311 YQApplication * app = new YQApplication();
312 YUI_CHECK_NEW( app );
318 void YQUI::calcDefaultSize()
320 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
321 QSize availableSize = qApp->desktop()->availableGeometry().size();
325 _default_size = availableSize;
327 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
328 _default_size.width(), _default_size.height() );
332 // Get _default_size via -geometry command line option (if set)
334 // NOTE not needed in qt4
335 // QWidget * dummy = new QWidget();
337 // qApp->setMainWidget( dummy );
338 // _default_size = dummy->size();
340 // Set min defaultsize or figure one out if -geometry was not used
342 if ( _default_size.width() < 800 ||
343 _default_size.height() < 600 )
345 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
347 // Scale down to 70% of screen size
349 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
350 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
354 _default_size = availableSize;
359 y2milestone( "Forced size (via -geometry): %dx%d",
360 _default_size.width(), _default_size.height() );
364 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
369 void YQUI::internalError( const char * msg )
372 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
373 QMessageBox::Abort | QMessageBox::Default,
377 if ( button == QMessageBox::Abort )
382 // exit() leaves a process running (WFM?), so this really seems to be
383 // the only way to make sure we are really going down.
387 void YQUI::idleLoop( int fd_ycp )
391 // runs in main thread
392 _eventLoop->wakeUp();
397 FD_SET(fd_ycp, &rfds);
401 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
408 void YQUI::sendEvent( YEvent * event )
412 _event_handler.sendEvent( event );
415 _eventLoop->exit( 1 );
420 YEvent * YQUI::userInput( unsigned long timeout_millisec )
424 _event_handler.blockEvents( false );
425 _eventLoop->wakeUp();
428 //y2milestone( "userInput %ld", timeout_millisec );
431 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
433 if ( _user_input_timer->isActive() )
434 _user_input_timer->stop();
438 if ( timeout_millisec > 0 )
439 _user_input_timer->start( timeout_millisec ); // single shot
441 if ( qApp->focusWidget() )
442 qApp->focusWidget()->setFocus();
445 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
447 _do_exit_loop = false;
449 event = _event_handler.consumePendingEvent();
451 // Display a busy cursor, but only if there is no other activity within
452 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
454 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
457 if ( _user_input_timer->isActive() )
458 _user_input_timer->stop();
464 YEvent * YQUI::pollInput()
468 if ( _user_input_timer->isActive() )
469 _user_input_timer->stop();
471 if ( ! pendingEvent() )
473 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
477 //qApp->processEvents();
478 event = _event_handler.consumePendingEvent();
482 if ( pendingEvent() )
483 event = _event_handler.consumePendingEvent();
489 void YQUI::userInputTimeout()
491 if ( ! pendingEvent() )
492 sendEvent( new YTimeoutEvent() );
496 #warning FIXME Move this to Y(Q)Dialog (and rename it to ::finalize()?)
497 void YQUI::showDialog( YDialog * dialog )
499 QWidget * qw = (QWidget *) dialog->widgetRep();
508 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
510 //qApp->processEvents();
514 void YQUI::closeDialog( YDialog * dialog )
519 QString YQUI::productName() const
521 return fromUTF8( YUI::productName() );
526 YQUI::setTextdomain( const char * domain )
528 bindtextdomain( domain, LOCALEDIR );
529 bind_textdomain_codeset( domain, "utf8" );
530 textdomain( domain );
532 // Make change known.
534 extern int _nl_msg_cat_cntr;
540 void YQUI::blockEvents( bool block )
544 if ( ++blocked_level == 1 )
546 _event_handler.blockEvents( true );
552 if ( --blocked_level == 0 )
554 _event_handler.blockEvents( false );
555 _eventLoop->wakeUp();
561 bool YQUI::eventsBlocked() const
563 return _event_handler.eventsBlocked();
567 qMessageHandler( QtMsgType type, const char * msg )
572 y2milestone ("qt-debug: %s\n", msg);
575 y2warning ("qt-warning: %s\n", msg);
581 y2warning ("qt-critical: %s\n", msg);
584 y2internal ("qt-fatal: %s\n", msg);
585 exit (1); // qt does the same