libyui-ncurses-pkg  2.46.1
 All Classes Functions
NCPkgStatusStrategy.cc
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | All Rights Reserved.
5 |
6 | This program is free software; you can redistribute it and/or
7 | modify it under the terms of version 2 of the GNU General Public License as
8 | published by the Free Software Foundation.
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
16 | along with this program; if not, contact Novell, Inc.
17 |
18 | To contact Novell about this file by physical or electronic mail,
19 | you may find current contact information at www.novell.com
20 |
21 |***************************************************************************/
22 
23 
24 /*---------------------------------------------------------------------\
25 | |
26 | __ __ ____ _____ ____ |
27 | \ \ / /_ _/ ___|_ _|___ \ |
28 | \ V / _` \___ \ | | __) | |
29 | | | (_| |___) || | / __/ |
30 | |_|\__,_|____/ |_| |_____| |
31 | |
32 | core system |
33 | (C) SuSE GmbH |
34 \----------------------------------------------------------------------/
35 
36  File: NCPkgStatusStrategy.cc
37 
38  Author: Gabriele Strattner <gs@suse.de>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 
44 #include "NCPkgStatusStrategy.h"
45 
46 #include "NCTable.h"
47 
48 #include "NCZypp.h"
49 
50 #include <zypp/ui/Selectable.h>
51 #include <zypp/ResObject.h>
52 
53 using std::endl;
54 
55 //------------------------------------------------------------
56 // Base class for strategies to handle status
57 //------------------------------------------------------------
58 
59 //
60 // Constructor
61 //
62 NCPkgStatusStrategy::NCPkgStatusStrategy()
63 {
64 }
65 
66 //
67 // Destructor - must be defined here (because it is pure virtual)
68 //
69 NCPkgStatusStrategy::~NCPkgStatusStrategy()
70 {
71 }
72 
73 ///////////////////////////////////////////////////////////////////
74 //
75 // NCPkgStatusStrategy::getPackageStatus()
76 //
77 // Gets status from package manager
78 //
79 ZyppStatus NCPkgStatusStrategy::getPackageStatus( ZyppSel slbPtr,
80  ZyppObj objPtr )
81 {
82  if ( slbPtr )
83  {
84  return slbPtr->status();
85  }
86  else
87  {
88  yuiError() << "Selectable pointer not valid" << endl;
89  return S_NoInst;
90  }
91 }
92 
93 /////////////////////////////////////////////////////////////////
94 //
95 // NCPkgStatusStrategy::setObjectStatus()
96 //
97 // Informs the package manager about the status change
98 //
99 bool NCPkgStatusStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
100 {
101  bool ok = false;
102 
103  if ( !slbPtr )
104  {
105  yuiError() << "Invalid package object" << endl;
106  return false;
107  }
108 
109  ok = slbPtr->setStatus( newstatus );
110 
111  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
112  << newstatus << " returns: " << (ok?"true":"false") << endl;
113 
114  return ok;
115 }
116 
117 ///////////////////////////////////////////////////////////////////
118 //
119 // NCPkgStatusStrategy::keyToStatus()
120 //
121 // Returns the corresponding status
122 //
123 bool NCPkgStatusStrategy::keyToStatus( const int & key,
124  ZyppSel slbPtr,
125  ZyppObj objPtr,
126  ZyppStatus & newStat )
127 {
128  if ( !slbPtr )
129  return false;
130 
131  bool valid = true;
132  ZyppStatus retStat = S_NoInst;
133  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
134  bool installed = !slbPtr->installedEmpty();
135 
136  // get the new status
137  switch ( key )
138  {
139  case '-':
140  if ( installed ) // installed package -> always set status to delete
141  {
142  // if required, NCPkgTable::changeStatus() shows the delete notify
143  retStat = S_Del;
144  }
145  else
146  {
147  retStat = S_NoInst;
148  }
149  break;
150  case '+':
151  if ( oldStatus == S_NoInst
152  || oldStatus == S_AutoInstall )
153  {
154  // if required, NCPkgTable::changeStatus() shows the notify message
155  retStat = S_Install;
156  }
157  else if ( oldStatus == S_Del
158  || oldStatus == S_AutoDel)
159  {
160  retStat = S_KeepInstalled;
161  }
162  else if ( oldStatus == S_AutoUpdate )
163  {
164  retStat = S_Update;
165  }
166  else
167  {
168  valid = false;
169  }
170  break;
171  case '>':
172  if ( oldStatus == S_KeepInstalled
173  || oldStatus == S_Del
174  || oldStatus == S_AutoDel )
175  {
176  if ( slbPtr->hasCandidateObj() )
177  {
178  retStat = S_Update;
179  }
180  }
181  else
182  {
183  valid = false;
184  }
185  break;
186  //this is the case for 'going back' i.e. S_Install -> S_NoInst, S_Update -> S_KeepInstalled
187  //not for S_Del, since '+' key does this
188  case '<':
189  if ( oldStatus == S_Install
190  || oldStatus == S_AutoInstall )
191  {
192  retStat = S_NoInst;
193  }
194  else if ( oldStatus == S_Update
195  || oldStatus == S_AutoUpdate )
196  {
197  retStat = S_KeepInstalled;
198  }
199  break;
200  case '!': // set S_Taboo
201  if ( !installed )
202  {
203  retStat = S_Taboo;
204  }
205  break;
206  case '*': // set S_Protected
207  if ( installed )
208  {
209  retStat = S_Protected;
210  }
211  break;
212  default:
213  yuiDebug() << "Key not valid" << endl;
214  valid = false;
215  }
216 
217  if ( valid )
218  newStat = retStat;
219 
220  return valid;
221 }
222 
223 
224 ///////////////////////////////////////////////////////////////////
225 //
226 // NCPkgStatusStrategy::toggleStatus()
227 //
228 // Returns the new status
229 //
231  ZyppObj objPtr,
232  ZyppStatus & newStat )
233 {
234  if ( !slbPtr )
235  return false;
236 
237  bool ok = true;
238 
239  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
240  ZyppStatus newStatus = oldStatus;
241  ZyppPattern patPtr = tryCastToZyppPattern (objPtr);
242 
243  switch ( oldStatus )
244  {
245  case S_Del:
246  newStatus = S_KeepInstalled;
247  break;
248  case S_Install:
249  newStatus =S_NoInst ;
250  break;
251  case S_Update:
252  newStatus = S_Del;
253  break;
254  case S_KeepInstalled:
255  if ( patPtr )
256  newStatus = S_Install;
257 
258  else if ( slbPtr->hasCandidateObj() )
259  {
260  newStatus = S_Update;
261  }
262  else
263  {
264  newStatus = S_Del;
265  }
266  break;
267  case S_NoInst:
268  if ( slbPtr->hasCandidateObj() || patPtr )
269  {
270  newStatus = S_Install;
271  }
272  else
273  {
274  yuiWarning() << "No candidate object for " << slbPtr->theObj()->name().c_str() << endl;
275  newStatus = S_NoInst;
276  }
277  break;
278  case S_AutoInstall:
279  //Correct transition is S_Taboo -> #254816
280  newStatus = S_Taboo;
281  break;
282  case S_AutoDel:
283  newStatus = S_KeepInstalled;
284  break;
285  case S_AutoUpdate:
286  newStatus = S_KeepInstalled;
287  break;
288  case S_Taboo:
289  newStatus = S_NoInst;
290  break;
291  case S_Protected:
292  newStatus = S_KeepInstalled;
293  break;
294  }
295 
296  yuiMilestone() << "Status toogled: old " << oldStatus << ", new " << newStatus << endl;
297  newStat = newStatus;
298 
299  return ok;
300 }
301 
302 ///////////////////////////////////////////////////////////////////
303 //
304 // NCPkgStatusStrategy::solveResolvableCollections()
305 //
306 // Do a "small" solver run
307 //
309 {
310  zypp::Resolver_Ptr resolver = zypp::getZYpp()->resolver();
311  resolver->resolvePool();
312 }
313 
314 
315 
316 //------------------------------------------------------------
317 // Class for strategies to get status for packages
318 //------------------------------------------------------------
319 
320 //
321 // Constructor
322 //
323 PackageStatStrategy::PackageStatStrategy()
325 {
326 }
327 
328 
329 
330 
331 //------------------------------------------------------------
332 // Class for strategies to get status for patches
333 //------------------------------------------------------------
334 
335 //
336 // Constructor
337 //
338 PatchStatStrategy::PatchStatStrategy()
340 {
341 }
342 
343 
344 ///////////////////////////////////////////////////////////////////
345 //
346 // PatchStatStrategy::keyToStatus()
347 //
348 // Returns the corresponding status
349 //
350 bool PatchStatStrategy::keyToStatus( const int & key,
351  ZyppSel slbPtr,
352  ZyppObj objPtr,
353  ZyppStatus & newStat )
354 {
355  if ( !slbPtr || !slbPtr->hasCandidateObj() )
356  return false;
357 
358  bool valid = true;
359  ZyppStatus retStat = S_NoInst;
360  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
361  bool toBeInst = slbPtr->candidateObj().status().isToBeInstalled();
362  bool isRelevant = slbPtr->candidateObj().isRelevant();
363  bool isBroken = slbPtr->candidateObj().isBroken();
364 
365  yuiMilestone() << slbPtr->name() << ": " << (toBeInst?"to be installed":"not to be installed,")
366  << " old status: " << oldStatus << endl;
367 
368  // get the new status
369  switch ( key )
370  {
371  case '-':
372  // To be installed includes S_Install and S_AutoInstall
373  if ( toBeInst )
374  {
375  retStat = S_NoInst;
376  }
377  else if ( oldStatus == S_Taboo )
378  {
379  if ( isBroken )
380  {
381  retStat = S_Install;
382  }
383  else
384  {
385  retStat = S_NoInst;
386  }
387  }
388  else // patches cannot be deleted
389  {
390  valid = false;
391  }
392  break;
393  case '+':
394  // only relevant patches can be installed
395  if ( isRelevant &&
396  ( oldStatus == S_NoInst ||
397  oldStatus == S_AutoInstall ||
398  oldStatus == S_KeepInstalled ) )
399  {
400  retStat = S_Install;
401  }
402  else
403  {
404  valid = false;
405  }
406  break;
407  case '!':
408  {
409  // For patches there isn't an installed object available (patches are not installed,
410  // they can be satisfied because required version/s of the patch package/s is/are
411  // installed). Therefore they only can be set to S_Taboo (not to S_Protected).
412  retStat = S_Taboo;
413  }
414  break;
415  default:
416  yuiDebug() << "Key not valid" << endl;
417  valid = false;
418  }
419 
420  if ( valid )
421  {
422  newStat = retStat;
423  }
424  yuiMilestone() << "New status: " << newStat << endl;
425 
426  return valid;
427 }
428 
429 ///////////////////////////////////////////////////////////////////
430 //
431 // PatchStatStrategy::toggleStatus()
432 //
433 // Returns the new status
434 //
435 bool PatchStatStrategy::toggleStatus( ZyppSel slbPtr,
436  ZyppObj objPtr,
437  ZyppStatus & newStat )
438 {
439  if ( !slbPtr || !slbPtr->hasCandidateObj() )
440  return false;
441 
442  bool ok = true;
443 
444  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
445  bool isBroken = slbPtr->candidateObj().isBroken();
446  ZyppStatus newStatus = oldStatus;
447 
448  switch ( oldStatus )
449  {
450  case S_Install:
451  case S_AutoInstall:
452  newStatus = S_NoInst ;
453  break;
454  case S_KeepInstalled:
455  newStatus = S_Install;
456  break;
457  case S_NoInst:
458  newStatus = S_Install ;
459  break;
460  case S_Taboo:
461  if ( isBroken )
462  {
463  newStatus = S_Install;
464  }
465  else
466  {
467  newStatus = S_NoInst;
468  }
469  break;
470  default:
471  newStatus = oldStatus;
472  }
473  yuiMilestone() << "Status toogled: old " << oldStatus << ", new " << newStatus << endl;
474 
475  newStat = newStatus;
476 
477  return ok;
478 }
479 
480 
481 /////////////////////////////////////////////////////////////////
482 //
483 // PatchStatStrategy::setObjectStatus()
484 //
485 // Inform the package manager about the status change
486 // of the patch
487 //
488 bool PatchStatStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
489 {
490  bool ok = false;
491 
492  if ( !slbPtr )
493  {
494  yuiError() << "Invalid patch object" << endl;
495  return false;
496  }
497 
498  ok = slbPtr->setStatus( newstatus );
499  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
500  << newstatus << " returns: " << (ok?"true":"false") << endl;
501 
502  // do a solver run
504 
505  return ok;
506 }
507 
508 //------------------------------------------------------------
509 // Class for strategies for selections
510 //------------------------------------------------------------
511 
512 //
513 // Constructor
514 //
515 SelectionStatStrategy::SelectionStatStrategy()
517 {
518 }
519 
520 /////////////////////////////////////////////////////////////////
521 //
522 // SelectionStatStrategy::setObjectStatus()
523 //
524 // Inform the package manager about the status change
525 // of the selection
526 //
527 bool SelectionStatStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
528 {
529  bool ok = false;
530 
531  if ( !slbPtr || !objPtr )
532  {
533  yuiError() << "Invalid selection" << endl;
534  return false;
535  }
536 
537  ok = slbPtr->setStatus( newstatus );
538  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
539  << newstatus << " returns: " << (ok?"true":"false") << endl;
540 
541  // do a solver run -> solver runs in NCPkgTable::changeStatus()
542  // solveResolvableCollections();
543 
544  return ok;
545 }
546 
547 //------------------------------------------------------------
548 // Class for strategies for depndencies
549 //------------------------------------------------------------
550 
551 //
552 // Constructor
553 //
554 DependencyStatStrategy::DependencyStatStrategy()
556 {
557 }
558 
559 //------------------------------------------------------------
560 // Class for strategies to get status for available packages
561 //------------------------------------------------------------
562 
563 //
564 // Constructor
565 //
566 AvailableStatStrategy::AvailableStatStrategy()
568 {
569 }
570 
571 ///////////////////////////////////////////////////////////////////
572 //
573 // AvailableStatStrategy::setObjectStatus
574 //
575 // Informs the package manager about the new status (sets the candidate)
576 //
577 bool AvailableStatStrategy::setObjectStatus( ZyppStatus newstatus,
578  ZyppSel slbPtr, ZyppObj objPtr )
579 {
580  bool ok = false;
581 
582  if ( !slbPtr || !objPtr )
583  {
584  return false;
585  }
586 
587  ZyppObj newCandidate = objPtr;
588 
589  if ( newCandidate != slbPtr->candidateObj() )
590  {
591  yuiMilestone() << "CANDIDATE changed" << endl;
592 
593  // Change status of selectable
594  ZyppStatus status = slbPtr->status();
595 
596  if ( slbPtr->installedObj() &&
597  slbPtr->installedObj()->edition() == newCandidate->edition() &&
598  slbPtr->installedObj()->vendor() == newCandidate->vendor()
599  )
600  {
601  yuiMilestone() << "Identical package installed" << endl;
602  // Switch back to the original instance -
603  // the version that was previously installed
604  status = S_KeepInstalled;
605  }
606  else
607  {
608  switch ( status )
609  {
610  case S_KeepInstalled:
611  case S_Protected:
612  case S_AutoDel:
613  case S_AutoUpdate:
614  case S_Del:
615  case S_Update:
616 
617  status = S_Update;
618  break;
619 
620  case S_NoInst:
621  case S_Taboo:
622  case S_Install:
623  case S_AutoInstall:
624  status = S_Install;
625  break;
626  }
627  }
628 
629  // Set candidate
630  ok = bool( slbPtr->setCandidate( newCandidate ) );
631  yuiMilestone() << "Set user candidate returns: " << (ok?"true":"false") << endl;
632  if ( ok )
633  {
634  // Set status
635  ok = slbPtr->setStatus( status );
636  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
637  << status << " returns: " << (ok?"true":"false") << endl;
638  }
639  }
640 
641  return ok;
642 }
643 
644 
645 //------------------------------------------------------------
646 // Class for strategies to get status for multi version packages
647 //------------------------------------------------------------
648 
649 //
650 // Constructor
651 //
652 MultiVersionStatStrategy::MultiVersionStatStrategy()
654 {
655 }
656 
657 ///////////////////////////////////////////////////////////////////
658 //
659 // MultiVersionStatStrategy::getPackageStatus()
660 //
661 // Gets status from package manager
662 //
664  ZyppObj objPtr )
665 {
666  if ( !slbPtr || !objPtr )
667  {
668  yuiError() << "Selectable pointer not valid" << endl;
669  return S_NoInst;
670  }
671 
672  zypp::PoolItem itemPtr ( objPtr->satSolvable() );
673  return slbPtr->pickStatus( itemPtr );
674 }
675 
676 ///////////////////////////////////////////////////////////////////
677 //
678 // MultiVersionStatStrategy::setObjectStatus
679 //
680 // Informs the package manager about the new status
681 //
682 bool MultiVersionStatStrategy::setObjectStatus( ZyppStatus newstatus,
683  ZyppSel slbPtr, ZyppObj objPtr )
684 {
685  bool ok = false;
686 
687  if ( !slbPtr || !objPtr )
688  {
689  return false;
690  }
691 
692  zypp::PoolItem itemPtr ( objPtr->satSolvable() );
693  ok = slbPtr->setPickStatus( itemPtr, newstatus );
694  yuiMilestone() << "Set new status of: "<< slbPtr->name() << ", " << objPtr->edition()
695  << " to: " << newstatus << " returns: " << (ok?"true":"false") << endl;
696 
697  return ok;
698 }
699 
700 //------------------------------------------------------------
701 // Class for strategies to get status for update packages
702 //------------------------------------------------------------
703 
704 //
705 // Constructor
706 //
707 UpdateStatStrategy::UpdateStatStrategy()
709 {
710 }
711 
712 
713 //------------------------------------------------------------
714 // Class for strategies to get status for patch packages
715 //------------------------------------------------------------
716 
717 //
718 // Constructor
719 //
720 PatchPkgStatStrategy::PatchPkgStatStrategy()
722 {
723 }
724 
725 bool PatchPkgStatStrategy::setObjectStatus( ZyppStatus newstatus,
726  ZyppSel slbPtr, ZyppObj objPtr )
727 {
728  // it is not possible to set the status of the packages belonging to a certain patch
729  return false;
730 }
virtual bool keyToStatus(const int &key, ZyppSel slbPtr, ZyppObj objPtr, ZyppStatus &newStat)
Returns the new status to the given key (respecting the old status of th eobject).
virtual bool toggleStatus(ZyppSel slbPtr, ZyppObj objPtr, ZyppStatus &newStat)
Toggles the package status (e.g.
virtual bool toggleStatus(ZyppSel slbPtr, ZyppObj objPtr, ZyppStatus &newStat)
Toggles the patch status (e.g.
virtual bool keyToStatus(const int &key, ZyppSel slbPtr, ZyppObj objPtr, ZyppStatus &newStat)
Returns the new status to the given key (respecting the old status of the patch). ...
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
Informs the package manager about the new status.
virtual ZyppStatus getPackageStatus(ZyppSel slbPtr, ZyppObj objPtr)
Gets the status information from the package manager.
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
It is not possible to std::set the package status for packages belonging to a patch, i.e.
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
Informs the package manager about the new status and additionally sets the candidate object to the us...
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
Sets the status of the patch AND the status of the patch packages.
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
Sets the status of the selection.
virtual ZyppStatus getPackageStatus(ZyppSel slbPtr, ZyppObj objPtr)
Gets the status information from the package manager.
virtual bool setObjectStatus(ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr)
Informs the package manager about the new status and additionally sets the candidate object to the us...
void solveResolvableCollections()
Do a "small" solver run for all "resolvable collections", i.e., for selections, patterns, languages, patches.