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