libyui-ncurses-pkg  2.43.4.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 )
356  return false;
357 
358  bool valid = true;
359  ZyppStatus retStat = S_NoInst;
360  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
361  bool installed = !slbPtr->installedEmpty();
362 
363  // get the new status
364  switch ( key )
365  {
366  case '-':
367  if ( installed ) // installed ->set status to delete
368  {
369  retStat = S_Del;
370  }
371  else
372  {
373  retStat = S_NoInst;
374  }
375  break;
376  case '+':
377  if ( oldStatus == S_NoInst
378  || oldStatus == S_AutoInstall )
379  {
380  retStat = S_Install;
381  }
382  else if ( oldStatus == S_Del
383  || oldStatus == S_AutoDel)
384  {
385  retStat = S_KeepInstalled;
386  }
387  else
388  {
389  valid = false;
390  }
391 
392  break;
393  case '>':
394  if ( oldStatus == S_KeepInstalled
395  || oldStatus == S_Del
396  || oldStatus == S_AutoDel )
397  {
398  if ( slbPtr->hasCandidateObj() )
399  {
400  retStat = S_Update;
401  }
402  }
403  else
404  {
405  valid = false;
406  }
407  break;
408  default:
409  yuiDebug() << "Key not valid" << endl;
410  valid = false;
411  }
412 
413  if ( valid )
414  newStat = retStat;
415 
416  return valid;
417 }
418 
419 #if EXTRA_PATCH_STRATEGY
420 
421 ///////////////////////////////////////////////////////////////////
422 //
423 // PatchStatStrategy::toggleStatus()
424 //
425 // Returns the new status
426 //
427 bool PatchStatStrategy::toggleStatus( ZyppSel slbPtr,
428  ZyppObj objPtr,
429  ZyppStatus & newStat )
430 {
431  if ( !slbPtr )
432  return false;
433 
434  bool ok = true;
435 
436  ZyppStatus oldStatus = getPackageStatus( slbPtr, objPtr );
437  ZyppStatus newStatus = oldStatus;
438 
439  switch ( oldStatus )
440  {
441  case S_Install:
442  case S_AutoInstall:
443  newStatus =S_NoInst ;
444  break;
445  case S_Update:
446  case S_AutoUpdate:
447  case S_AutoDel:
448  newStatus = S_KeepInstalled;
449  break;
450  case S_KeepInstalled:
451  if ( slbPtr->hasCandidateObj() )
452  {
453  newStatus = S_Update;
454  }
455  break;
456  case S_NoInst:
457  newStatus = S_Install ;
458  break;
459  case S_Taboo:
460  newStatus = S_NoInst;
461  break;
462  case S_Protected:
463  newStatus = S_KeepInstalled;
464  break;
465  default:
466  newStatus = oldStatus;
467  }
468 
469  newStat = newStatus;
470 
471  return ok;
472 }
473 #endif
474 
475 /////////////////////////////////////////////////////////////////
476 //
477 // PatchStatStrategy::setObjectStatus()
478 //
479 // Inform the package manager about the status change
480 // of the patch
481 //
482 bool PatchStatStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
483 {
484  bool ok = false;
485 
486  if ( !slbPtr )
487  {
488  yuiError() << "Invalid patch object" << endl;
489  return false;
490  }
491 
492  ok = slbPtr->setStatus( newstatus );
493  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
494  << newstatus << " returns: " << (ok?"true":"false") << endl;
495 
496  // do a solver run
498 
499  return ok;
500 }
501 
502 //------------------------------------------------------------
503 // Class for strategies for selections
504 //------------------------------------------------------------
505 
506 //
507 // Constructor
508 //
509 SelectionStatStrategy::SelectionStatStrategy()
511 {
512 }
513 
514 /////////////////////////////////////////////////////////////////
515 //
516 // SelectionStatStrategy::setObjectStatus()
517 //
518 // Inform the package manager about the status change
519 // of the selection
520 //
521 bool SelectionStatStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
522 {
523  bool ok = false;
524 
525  if ( !slbPtr || !objPtr )
526  {
527  yuiError() << "Invalid selection" << endl;
528  return false;
529  }
530 
531  ok = slbPtr->setStatus( newstatus );
532  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
533  << newstatus << " returns: " << (ok?"true":"false") << endl;
534 
535  // do a solver run -> solver runs in NCPkgTable::changeStatus()
536  // solveResolvableCollections();
537 
538  return ok;
539 }
540 
541 //------------------------------------------------------------
542 // Class for strategies for depndencies
543 //------------------------------------------------------------
544 
545 //
546 // Constructor
547 //
548 DependencyStatStrategy::DependencyStatStrategy()
550 {
551 }
552 
553 //------------------------------------------------------------
554 // Class for strategies to get status for available packages
555 //------------------------------------------------------------
556 
557 //
558 // Constructor
559 //
560 AvailableStatStrategy::AvailableStatStrategy()
562 {
563 }
564 
565 ///////////////////////////////////////////////////////////////////
566 //
567 // AvailableStatStrategy::setObjectStatus
568 //
569 // Informs the package manager about the new status (sets the candidate)
570 //
571 bool AvailableStatStrategy::setObjectStatus( ZyppStatus newstatus,
572  ZyppSel slbPtr, ZyppObj objPtr )
573 {
574  bool ok = false;
575 
576  if ( !slbPtr || !objPtr )
577  {
578  return false;
579  }
580 
581  ZyppObj newCandidate = objPtr;
582 
583  if ( newCandidate != slbPtr->candidateObj() )
584  {
585  yuiMilestone() << "CANDIDATE changed" << endl;
586 
587  // Change status of selectable
588  ZyppStatus status = slbPtr->status();
589 
590  if ( slbPtr->installedObj() &&
591  slbPtr->installedObj()->edition() == newCandidate->edition() &&
592  slbPtr->installedObj()->vendor() == newCandidate->vendor()
593  )
594  {
595  yuiMilestone() << "Identical package installed" << endl;
596  // Switch back to the original instance -
597  // the version that was previously installed
598  status = S_KeepInstalled;
599  }
600  else
601  {
602  switch ( status )
603  {
604  case S_KeepInstalled:
605  case S_Protected:
606  case S_AutoDel:
607  case S_AutoUpdate:
608  case S_Del:
609  case S_Update:
610 
611  status = S_Update;
612  break;
613 
614  case S_NoInst:
615  case S_Taboo:
616  case S_Install:
617  case S_AutoInstall:
618  status = S_Install;
619  break;
620  }
621  }
622 
623  // Set candidate
624  ok = slbPtr->setCandidate( newCandidate );
625  yuiMilestone() << "Set user candidate returns: " << (ok?"true":"false") << endl;
626  if ( ok )
627  {
628  // Set status
629  ok = slbPtr->setStatus( status );
630  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
631  << status << " returns: " << (ok?"true":"false") << endl;
632  }
633  }
634 
635  return ok;
636 }
637 
638 
639 //------------------------------------------------------------
640 // Class for strategies to get status for multi version packages
641 //------------------------------------------------------------
642 
643 //
644 // Constructor
645 //
646 MultiVersionStatStrategy::MultiVersionStatStrategy()
648 {
649 }
650 
651 ///////////////////////////////////////////////////////////////////
652 //
653 // MultiVersionStatStrategy::getPackageStatus()
654 //
655 // Gets status from package manager
656 //
658  ZyppObj objPtr )
659 {
660  if ( !slbPtr || !objPtr )
661  {
662  yuiError() << "Selectable pointer not valid" << endl;
663  return S_NoInst;
664  }
665 
666  zypp::PoolItem itemPtr ( objPtr->satSolvable() );
667  return slbPtr->pickStatus( itemPtr );
668 }
669 
670 ///////////////////////////////////////////////////////////////////
671 //
672 // MultiVersionStatStrategy::setObjectStatus
673 //
674 // Informs the package manager about the new status
675 //
676 bool MultiVersionStatStrategy::setObjectStatus( ZyppStatus newstatus,
677  ZyppSel slbPtr, ZyppObj objPtr )
678 {
679  bool ok = false;
680 
681  if ( !slbPtr || !objPtr )
682  {
683  return false;
684  }
685 
686  zypp::PoolItem itemPtr ( objPtr->satSolvable() );
687  ok = slbPtr->setPickStatus( itemPtr, newstatus );
688  yuiMilestone() << "Set new status of: "<< slbPtr->name() << ", " << objPtr->edition()
689  << " to: " << newstatus << " returns: " << (ok?"true":"false") << endl;
690 
691  return ok;
692 }
693 
694 //------------------------------------------------------------
695 // Class for strategies to get status for update packages
696 //------------------------------------------------------------
697 
698 //
699 // Constructor
700 //
701 UpdateStatStrategy::UpdateStatStrategy()
703 {
704 }
705 
706 
707 //------------------------------------------------------------
708 // Class for strategies to get status for patch packages
709 //------------------------------------------------------------
710 
711 //
712 // Constructor
713 //
714 PatchPkgStatStrategy::PatchPkgStatStrategy()
716 {
717 }
718 
719 bool PatchPkgStatStrategy::setObjectStatus( ZyppStatus newstatus,
720  ZyppSel slbPtr, ZyppObj objPtr )
721 {
722  // it is not possible to set the status of the packages belonging to a certain patch
723  return false;
724 }