]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgConflictDialog.cc
48ec35d44c519c954670bffd4abaafd41625ef36
[duncan/yast2-qt4.git] / src / pkg / YQPkgConflictDialog.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgConflictDialog.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define y2log_component "qt-pkg"
22 #include <ycp/y2log.h>
23
24 #include <zypp/ZYppFactory.h>
25 #include <zypp/Resolver.h>
26
27 #include <QLabel>
28 #include <QKeyEvent>
29 #include <QLayout>
30 #include <QMenu>
31 #include <QPushButton>
32 #include <QDateTime>
33 #include <QPainter>
34 #include <QMessageBox>
35 #include <QDesktopWidget>
36 #include <QPixmap>
37 #include <QBoxLayout>
38
39 #include "YQPkgConflictDialog.h"
40 #include "YQPkgConflictList.h"
41 #include "YQDialog.h"
42
43 #include "QY2LayoutUtils.h"
44 #include "YQUI.h"
45 #include "YQi18n.h"
46 #include "utf8.h"
47
48
49 #define SPACING                 6       // between subwidgets
50 #define MARGIN                  4       // around the widget
51
52
53 // The busy dialog ("Checking Dependencies") will only be shown if solving
54 // (on average) takes longer than this many seconds. The first one will be
55 // shown in any case.
56
57 #define SUPPRESS_BUSY_DIALOG_SECONDS    1.5
58
59
60 YQPkgConflictDialog::YQPkgConflictDialog( QWidget * parent )
61     : QDialog( parent )
62 {
63     _solveCount         = 0;
64     _totalSolveTime     = 0.0;
65
66
67     // Set the dialog title.
68     //
69     // "Dependency conflict" is already used as the conflict list header just
70     // some pixels below that, so don't use this twice. This dialog title may
71     // or may not be visible, depending on whether or not there is a window
72     // manager running (and configured to show any dialog titles).
73
74     setWindowTitle( _( "Warning" ) );
75
76     // Enable dialog resizing even without window manager
77     setSizeGripEnabled( true );
78
79
80     // Layout for the dialog (can't simply insert a QVbox)
81
82     QVBoxLayout * layout = new QVBoxLayout( this );
83     layout->setMargin(MARGIN);
84     layout->setSpacing(SPACING);
85
86     Q_CHECK_PTR( layout );
87
88     // Conflict list
89
90     _conflictList = new YQPkgConflictList( this );
91     Q_CHECK_PTR( _conflictList );
92     layout->addWidget( _conflictList );
93     layout->addSpacing(8);
94
95     connect( _conflictList, SIGNAL( updatePackages() ),
96              this,          SIGNAL( updatePackages() ) );
97
98
99     // Button box
100     QHBoxLayout * buttonBox = new QHBoxLayout();
101     Q_CHECK_PTR( buttonBox );
102     buttonBox->setSpacing( SPACING );
103     buttonBox->setMargin ( MARGIN  );
104     layout->addLayout( buttonBox );
105
106     // "OK" button
107
108     QPushButton * button = new QPushButton( _( "&OK -- Try Again" ), this);
109     buttonBox->addWidget(button);
110     Q_CHECK_PTR( button );
111     button->setDefault( true );
112
113     connect( button, SIGNAL( clicked() ),
114              this,   SLOT  ( solveAndShowConflicts() ) );
115
116     // FIXME addHStretch( buttonBox );
117
118
119     // "Expert" menu button
120
121     button = new QPushButton( _( "&Expert" ), this );
122     buttonBox->addWidget(button);
123
124     Q_CHECK_PTR( button );
125
126     //FIXME addHStretch( buttonBox );
127
128
129     // "Expert" menu
130
131     _expertMenu = new QMenu( button );
132     Q_CHECK_PTR( _expertMenu );
133     button->setMenu( _expertMenu );
134
135     _expertMenu->addAction( _( "&Save This List to a File..." ),
136                              _conflictList, SLOT( askSaveToFile() ) );
137
138
139     // "Cancel" button
140
141     button = new QPushButton( _( "&Cancel" ), this);
142     buttonBox->addWidget(button);
143     Q_CHECK_PTR( button );
144
145     connect( button, SIGNAL( clicked() ),
146              this,   SLOT  ( reject()  ) );
147
148
149     // Busy popup
150
151     _busyPopup = new QLabel( "   " + _( "Checking Dependencies..." ) + "   ", parent, 0
152 #ifdef FIXME
153                              , WStyle_Customize | WStyle_DialogBorder | WStyle_Dialog | WStyle_Title
154 #endif
155                              );
156     Q_CHECK_PTR( _busyPopup );
157
158     _busyPopup->setWindowTitle( "" );
159     _busyPopup->resize( _busyPopup->sizeHint() );
160     YQDialog::center( _busyPopup, parent );
161
162
163     // Here comes a real nasty hack.
164     //
165     // The busy popup is needed to indicate that the application is (you
166     // guessed right) busy. But as long as it is busy, it doesn't process X
167     // events, either, and I didn't manage to convince Qt to please paint this
168     // popup before the solver's calculations (which take quite a while) start
169     // - all combinations of show(), repaint(), XSync(), XFlush(),
170     // processEvents() etc. failed.
171     //
172     // So, let's do it the hard way: Give this popup a background pixmap into
173     // which we render the text to display. The X server draws background
174     // pixmaps immediately, so we don't have to wait until the X server, the
175     // window manager and this application are finished negotiating all their
176     // various events.
177
178     // Create a pixmap. Make it large enough so it isn't replicated (i.e. the
179     // text is displayed several times) if some window manager chooses not to
180     // honor the size hints (KDM for example uses double the height we
181     // request).
182
183     QSize size = _busyPopup->sizeHint();
184     QPixmap pixmap( 3 * size.width(), 3 * size.height() );
185
186     // Clear the pixmap with the widget's normal background color.
187     //FIXME pixmap.fill( _busyPopup->paletteBackgroundColor() );
188
189     // Render the text - aligned top and left because otherwise it will of
190     // course be centered inside the pixmap which is usually much larger than
191     // the popup, thus the text would be cut off.
192     QPainter painter( &pixmap );
193     painter.drawText( pixmap.rect(), Qt::AlignLeft | Qt::AlignTop, _busyPopup->text() );
194     painter.end();
195
196     //FIXME _busyPopup->setPaletteBackgroundPixmap( pixmap );
197
198     // If the application manages to render the true contents of the label we
199     // just misused so badly, the real label will interfere with the background
200     // pixmap with (maybe) a few pixels offset (bug #25647). Fast or
201     // multiprocessor machines tend to have this problem.
202     // So let's get rid of the label text and solely rely on the background
203     // pixmap.
204     _busyPopup->setText( "" );
205
206     // Make sure the newly emptied text doesn't cause the busy dialog to be
207     // resized to nil (or a window manager dependent minimum size).
208     _busyPopup->setFixedSize( _busyPopup->size() );
209 }
210
211
212 YQPkgConflictDialog::~YQPkgConflictDialog()
213 {
214     // NOP
215 }
216
217
218 QSize
219 YQPkgConflictDialog::sizeHint() const
220 {
221     return limitToScreenSize( this, 550, 450 );
222 }
223
224
225 int
226 YQPkgConflictDialog::solveAndShowConflicts()
227 {
228     prepareSolving();
229
230     y2debug( "Solving..." );
231     QTime solveTime;
232     solveTime.start();
233
234     // Solve.
235
236     bool success = zypp::getZYpp()->resolver()->resolvePool();
237
238     _totalSolveTime += solveTime.elapsed() / 1000.0;
239
240     y2debug( "Solving done in %f s - average: %f s",
241              solveTime.elapsed() / 1000.0, averageSolveTime() );
242
243     return processSolverResult( success );
244 }
245
246
247 int
248 YQPkgConflictDialog::verifySystem()
249 {
250     prepareSolving();
251
252     y2debug( "Verifying system..." );
253     QTime solveTime;
254     solveTime.start();
255
256     bool success = zypp::getZYpp()->resolver()->verifySystem( true ); // considerNewHardware
257
258     y2debug( "System verified in %f s", solveTime.elapsed() / 1000.0 );
259
260     return processSolverResult( success );
261 }
262
263
264 void
265 YQPkgConflictDialog::prepareSolving()
266 {
267     Q_CHECK_PTR( _conflictList );
268     YQUI::ui()->busyCursor();
269
270     if ( isVisible() )
271     {
272         // This is not only the starting point for all the dependency solving
273         // magic, it is also used internally when clicking the "OK - Try again"
274         // button. Thus, before doing anything else, check if the conflict list
275         // still contains anything, and if so, apply any conflict resolutions
276         // the user selected - but only if this dialog is already visible.
277
278         _conflictList->applyResolutions();
279     }
280
281
282     // Initialize for next round of solving.
283     _conflictList->clear();
284
285     if ( _solveCount++ == 0 || averageSolveTime() > SUPPRESS_BUSY_DIALOG_SECONDS )
286     {
287         YQDialog::center( _busyPopup, parentWidget() );
288         _busyPopup->show();
289
290         // No _busyPopup->repaint() - that doesn't help anyway: Qt doesn't do
291         // any actual painting until the window is mapped. We just rely on the
292         // background pixmap we provided in the constructor.
293
294         // Make sure show() gets processed - usually, a window manager catches
295         // the show() (XMap) events, positions and maybe resizes the window and
296         // only then sends off an event that makes the window appear. This
297         // event needs to be processed.
298         qApp->processEvents();
299     }
300 }
301
302
303 int
304 YQPkgConflictDialog::processSolverResult( bool success )
305 {
306     if ( _busyPopup->isVisible() )
307         _busyPopup->hide();
308
309     // Package states may have changed: The solver may have set packages to
310     // autoInstall or autoUpdate. Make those changes known.
311     emit updatePackages();
312
313     YQUI::ui()->normalCursor();
314     int result = QDialog::Accepted;
315
316     if ( success )      // Solving went without any complaints?
317     {
318         result = QDialog::Accepted;
319
320         if ( isVisible() )
321             accept();   // Pop down the dialog.
322     }
323     else                // There were solving problems.
324     {
325         y2debug( "Dependency conflict!" );
326         YQUI::ui()->busyCursor();
327
328         _conflictList->fill( zypp::getZYpp()->resolver()->problems() );
329         YQUI::ui()->normalCursor();
330
331         if ( ! isVisible() )
332         {
333             // Pop up the dialog and run a local event loop.
334             result = exec();
335         }
336     }
337
338     return result;      // QDialog::Accepted or QDialog::Rejected
339 }
340
341
342 void
343 YQPkgConflictDialog::resetIgnoredDependencyProblems()
344 {
345     zypp::getZYpp()->resolver()->undo();
346 }
347
348
349 double
350 YQPkgConflictDialog::averageSolveTime() const
351 {
352     if ( _solveCount < 1 )
353         return 0.0;
354
355     return _totalSolveTime / _solveCount;
356 }
357
358
359 void
360 YQPkgConflictDialog::askCreateSolverTestCase()
361 {
362     QString testCaseDir = "/var/log/YaST2/solverTestcase";
363     // Heading for popup dialog
364     QString heading = QString( "<h2>%1</h2>" ).arg( _( "Create Dependency Resolver Test Case" ) );
365
366     QString msg =
367         _( "<p>Use this to generate extensive logs to help tracking down bugs in the dependency resolver. "
368            "The logs will be stored in directory <br><tt>%1</tt></p>" ).arg( testCaseDir );
369
370     int button_no = QMessageBox::information( 0,                        // parent
371                                               _( "Solver Test Case" ),  // caption
372                                               heading + msg,
373                                               _( "C&ontinue" ),         // button #0
374                                               _( "&Cancel" ) );         // button #1
375
376     if ( button_no == 1 )       // Cancel
377         return;
378
379     y2milestone( "Generating solver test case START" );
380     bool success = zypp::getZYpp()->resolver()->createSolverTestcase( qPrintable(testCaseDir) );
381     y2milestone( "Generating solver test case END" );
382
383     if ( success )
384     {
385         msg =
386             _( "<p>Dependency resolver test case written to <br><tt>%1</tt></p>"
387                "<p>Prepare <tt>y2logs.tgz tar</tt> archive to attach to Bugzilla?</p>" ).arg( testCaseDir ),
388         button_no = QMessageBox::question( 0,                           // parent
389                                            _( "Success" ),              // caption
390                                            msg,
391                                            QMessageBox::Yes    | QMessageBox::Default,
392                                            QMessageBox::No,
393                                            QMessageBox::Cancel | QMessageBox::Escape );
394
395         if ( button_no & QMessageBox::Yes ) // really binary (not logical) '&' - QMessageBox::Default is still in there
396             YQUI::ui()->askSaveLogs();
397     }
398     else // no success
399     {
400         QMessageBox::warning( 0,                                        // parent
401                               _( "Error" ),                             // caption
402                               _( "<p><b>Error</b> creating dependency resolver test case</p>"
403                                  "<p>Please check disk space and permissions for <tt>%1</tt></p>" ).arg( testCaseDir ),
404                               QMessageBox::Ok | QMessageBox::Default,
405                               QMessageBox::NoButton,
406                               QMessageBox::NoButton );
407     }
408 }
409
410 void
411 YQPkgConflictDialog::keyPressEvent( QKeyEvent * event )
412 {
413     if ( event &&  event->key() == Qt::Key_Print )
414     {
415         YQUI::ui()->makeScreenShot( "" );
416         return;
417     }
418     QWidget::keyPressEvent( event );
419 }
420
421
422
423 #include "YQPkgConflictDialog.moc"