libyui-ncurses-pkg  2.43.4
 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 
244  switch ( oldStatus )
245  {
246  case S_Del:
247  newStatus = S_KeepInstalled;
248  break;
249  case S_Install:
250  newStatus =S_NoInst ;
251  break;
252  case S_Update:
253  newStatus = S_Del;
254  break;
255  case S_KeepInstalled:
256  if ( patPtr )
257  newStatus = S_Install;
258 
259  else if ( slbPtr->hasCandidateObj() )
260  {
261  newStatus = S_Update;
262  }
263  else
264  {
265  newStatus = S_Del;
266  }
267  break;
268  case S_NoInst:
269  if ( slbPtr->hasCandidateObj() || patPtr )
270  {
271  newStatus = S_Install;
272  }
273  else
274  {
275  yuiWarning() << "No candidate object for " << slbPtr->theObj()->name().c_str() << endl;
276  newStatus = S_NoInst;
277  }
278  break;
279  case S_AutoInstall:
280  //Correct transition is S_Taboo -> #254816
281  newStatus = S_Taboo;
282  break;
283  case S_AutoDel:
284  newStatus = S_KeepInstalled;
285  break;
286  case S_AutoUpdate:
287  newStatus = S_KeepInstalled;
288  break;
289  case S_Taboo:
290  newStatus = S_NoInst;
291  break;
292  case S_Protected:
293  newStatus = S_KeepInstalled;
294  break;
295  }
296 
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 ///////////////////////////////////////////////////////////////////
568 //
569 // AvailableStatStrategy::setObjectStatus
570 //
571 // Informs the package manager about the new status (sets the candidate)
572 //
573 bool AvailableStatStrategy::setObjectStatus( ZyppStatus newstatus, ZyppSel slbPtr, ZyppObj objPtr )
574 {
575  bool ok = false;
576 
577  if ( !slbPtr || !objPtr )
578  {
579  return false;
580  }
581 
582  ZyppObj newCandidate = objPtr;
583 
584  if ( newCandidate != slbPtr->candidateObj() )
585  {
586  yuiMilestone() << "CANDIDATE changed" << endl;
587 
588  // Change status of selectable
589  ZyppStatus status = slbPtr->status();
590 
591  if ( slbPtr->installedObj() &&
592  slbPtr->installedObj()->edition() == newCandidate->edition() )
593  {
594  // Switch back to the original instance -
595  // the version that was previously installed
596  status = S_KeepInstalled;
597  }
598  else
599  {
600  switch ( status )
601  {
602  case S_KeepInstalled:
603  case S_Protected:
604  case S_AutoDel:
605  case S_AutoUpdate:
606  case S_Del:
607  case S_Update:
608 
609  status = S_Update;
610  break;
611 
612  case S_NoInst:
613  case S_Taboo:
614  case S_Install:
615  case S_AutoInstall:
616  status = S_Install;
617  break;
618  }
619  }
620 
621  // Set candidate
622  ok = slbPtr->setCandidate( newCandidate );
623  yuiMilestone() << "Set user candidate returns: " << (ok?"true":"false") << endl;
624 
625  // Set status
626  ok = slbPtr->setStatus( status );
627  yuiMilestone() << "Set status of: " << slbPtr->name() << " to: "
628  << status << " returns: " << (ok?"true":"false") << endl;
629 
630  }
631 
632  return ok;
633 }
634 
635 //------------------------------------------------------------
636 // Class for strategies to get status for available packages
637 //------------------------------------------------------------
638 
639 //
640 // Constructor
641 //
642 UpdateStatStrategy::UpdateStatStrategy()
644 {
645 }
646 
647 
648 //------------------------------------------------------------
649 // Class for strategies to get status for patch packages
650 //------------------------------------------------------------
651 
652 //
653 // Constructor
654 //
655 PatchPkgStatStrategy::PatchPkgStatStrategy()
657 {
658 }
659 
660 bool PatchPkgStatStrategy::setObjectStatus( ZyppStatus newstatus,
661  ZyppSel slbPtr, ZyppObj objPtr )
662 {
663  // it is not possible to set the status of the packages belonging to a certain patch
664  return false;
665 }