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