Imported Upstream version 17.21.0
[platform/upstream/libzypp.git] / zypp / ui / SelectableImpl.h
index ef71ef7..01de374 100644 (file)
@@ -17,7 +17,8 @@
 
 #include "zypp/base/PtrTypes.h"
 
-#include "zypp/ZConfig.h"
+#include "zypp/ResPool.h"
+#include "zypp/Resolver.h"
 #include "zypp/ui/Selectable.h"
 #include "zypp/ui/SelectableTraits.h"
 
@@ -52,12 +53,14 @@ namespace zypp
       typedef SelectableTraits::installed_const_iterator installed_const_iterator;
       typedef SelectableTraits::installed_size_type      installed_size_type;
 
+      typedef SelectableTraits::PickList               PickList;
+
     public:
-      template <class _Iterator>
-      Impl( const ResObject::Kind & kind_r,
+      template <class TIterator>
+      Impl( const ResKind & kind_r,
             const std::string & name_r,
-            _Iterator begin_r,
-            _Iterator end_r )
+            TIterator begin_r,
+            TIterator end_r )
       : _ident( sat::Solvable::SplitIdent( kind_r, name_r ).ident() )
       , _kind( kind_r )
       , _name( name_r )
@@ -69,7 +72,6 @@ namespace zypp
           else
             _availableItems.insert( *it );
         }
-        _defaultCandidate = defaultCandidate();
       }
 
     public:
@@ -78,7 +80,7 @@ namespace zypp
       { return _ident; }
 
       /**  */
-      ResObject::Kind kind() const
+      ResKind kind() const
       { return _kind; }
 
       /**  */
@@ -89,7 +91,7 @@ namespace zypp
       Status status() const;
 
       /**  */
-      bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r );
+      bool setStatus( Status state_r, ResStatus::TransactByValue causer_r );
 
       /** Installed object (transacting ot highest version). */
       PoolItem installedObj() const
@@ -107,9 +109,9 @@ namespace zypp
       PoolItem candidateObj() const
       {
         PoolItem ret( transactingCandidate() );
-        if ( ret )
-          return ret;
-        return _candidate ? _candidate : _defaultCandidate;
+        if ( ret )
+         ret = _candidate ? _candidate : defaultCandidate();
+       return ret;
       }
 
       /** Set a userCandidate (out of available objects).
@@ -125,10 +127,12 @@ namespace zypp
        */
       PoolItem candidateObjFrom( Repository repo_r ) const
       {
-        for_( it, availableBegin(), availableEnd() )
+        for ( const PoolItem & pi : available() )
         {
-          if ( (*it)->repository() == repo_r )
-            return *it;
+         if ( pi.isRetracted() )
+           continue;
+          if ( pi.repository() == repo_r )
+            return pi;
         }
         return PoolItem();
       }
@@ -141,59 +145,94 @@ namespace zypp
        */
       PoolItem updateCandidateObj() const
       {
-        if ( multiversionInstall() || installedEmpty() || ! _defaultCandidate )
-          return _defaultCandidate;
-        // Here: installed and _defaultCandidate are non NULL and it's not a
-        //       multiversion install.
+       PoolItem defaultCand( defaultCandidate() );
 
-        // update candidate must come from the highest priority repo
-        if ( _defaultCandidate->repoInfo().priority() != (*availableBegin())->repoInfo().priority() )
-          return PoolItem();
+       // multiversionInstall: This returns the candidate for the last
+       // instance installed. Actually we'd need a list here.
+
+       if ( ! defaultCand || defaultCand.isRetracted() )
+         return PoolItem();
+
+        if ( installedEmpty() )
+          return defaultCand;
+        // Here: installed and defaultCand are non NULL and it's not a
+        //       multiversion install.
 
         PoolItem installed( installedObj() );
         // check vendor change
-        if ( ! ( ZConfig::instance().solver_allowVendorChange()
-                 || VendorAttr::instance().equivalent( _defaultCandidate->vendor(), installed->vendor() ) ) )
+        if ( ! ( ResPool::instance().resolver().allowVendorChange()
+                 || VendorAttr::instance().equivalent( defaultCand->vendor(), installed->vendor() ) ) )
           return PoolItem();
 
         // check arch change (arch noarch changes are allowed)
-        if ( _defaultCandidate->arch() != installed->arch()
-           && ! ( _defaultCandidate->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
+        if ( defaultCand->arch() != installed->arch()
+           && ! ( defaultCand->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
           return PoolItem();
 
         // check greater edition
-        if ( _defaultCandidate->edition() <= installed->edition() )
+        if ( defaultCand->edition() <= installed->edition() )
           return PoolItem();
 
-        return _defaultCandidate;
+        return defaultCand;
       }
 
-      /** \copydoc Selectable::highestAvailableVersionObj() */
+      /** \copydoc Selectable::highestAvailableVersionObj()const */
       PoolItem highestAvailableVersionObj() const
       {
         PoolItem ret;
-        for_( it, availableBegin(), availableEnd() )
+       bool retractedOk = false;
+        for ( const PoolItem & pi : available() )
         {
-          if ( !ret || (*it).satSolvable().edition() > ret.satSolvable().edition() )
-            ret = *it;
+         if ( !retractedOk && pi.isRetracted() )
+         {
+           if ( ret )
+             break;    // prefer a not retracted candidate
+           retractedOk = true;
+         }
+          if ( !ret || pi.edition() > ret.edition() )
+            ret = pi;
         }
         return ret;
       }
 
-      /** \c True if \a rhs has the same content as an installed one.
-       * \see \ref sat::Solvable::identical
-       */
+      /** \copydoc Selectable::identIsAutoInstalled()const */
+      bool identIsAutoInstalled() const
+      { return sat::Solvable::identIsAutoInstalled( ident() ); }
+
+      /** \copydoc Selectable::identicalAvailable( const PoolItem & )const */
+      bool identicalAvailable( const PoolItem & rhs ) const
+      { return bool(identicalAvailableObj( rhs )); }
+
+      /** \copydoc Selectable::identicalInstalled( const PoolItem & )const */
       bool identicalInstalled( const PoolItem & rhs ) const
+      { return bool(identicalInstalledObj( rhs )); }
+
+      /** \copydoc Selectable::identicalAvailableObj( const PoolItem & rhs ) const */
+      PoolItem identicalAvailableObj( const PoolItem & rhs ) const
+      {
+        if ( !availableEmpty() && rhs )
+        {
+          for_( it, _availableItems.begin(), _availableItems.end() )
+          {
+            if ( identical( *it, rhs ) )
+              return *it;
+          }
+        }
+        return PoolItem();
+      }
+
+      /** \copydoc Selectable::identicalInstalledObj( const PoolItem & rhs ) const */
+      PoolItem identicalInstalledObj( const PoolItem & rhs ) const
       {
         if ( !installedEmpty() && rhs )
         {
           for_( it, _installedItems.begin(), _installedItems.end() )
           {
             if ( identical( *it, rhs ) )
-              return true;
+              return *it;
           }
         }
-        return false;
+        return PoolItem();
       }
 
       /** Best among all objects. */
@@ -213,12 +252,15 @@ namespace zypp
       available_size_type availableSize() const
       { return _availableItems.size(); }
 
-      available_const_iterator availableBegin() const
+      available_iterator availableBegin() const
       { return _availableItems.begin(); }
 
-      available_const_iterator availableEnd() const
+      available_iterator availableEnd() const
       { return _availableItems.end(); }
 
+      inline Iterable<available_iterator>  available() const
+      { return makeIterable( availableBegin(), availableEnd() ); }
+
       ////////////////////////////////////////////////////////////////////////
 
       bool installedEmpty() const
@@ -233,18 +275,88 @@ namespace zypp
       installed_iterator installedEnd() const
       { return _installedItems.end(); }
 
+      inline Iterable<installed_iterator>  installed() const
+      { return makeIterable( installedBegin(), installedEnd() ); }
+
+      ////////////////////////////////////////////////////////////////////////
+
+      const PickList & picklist() const
+      {
+        if ( ! _picklistPtr )
+        {
+          _picklistPtr.reset( new PickList );
+          // installed without identical avaialble first:
+          for ( const PoolItem & pi : installed() )
+          {
+            if ( ! identicalAvailable( pi ) )
+              _picklistPtr->push_back( pi );
+          }
+          _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
+        }
+        return *_picklistPtr;
+      }
+
+      bool picklistEmpty() const
+      { return picklist().empty(); }
+
+      picklist_size_type picklistSize() const
+      { return picklist().size(); }
+
+      picklist_iterator picklistBegin() const
+      { return picklist().begin(); }
+
+      picklist_iterator picklistEnd() const
+      { return picklist().end(); }
+
       ////////////////////////////////////////////////////////////////////////
 
+      bool hasRetracted() const
+      { return !_availableItems.empty() && _availableItems.rbegin()->isRetracted(); }
+
+      bool hasRetractedInstalled() const
+      {
+       bool ret = false;
+       if ( hasRetracted() )
+       {
+         for ( const PoolItem & ipi : installed() )
+         {
+           PoolItem pi { identicalAvailableObj( ipi ) };
+           if ( pi && pi.isRetracted() )
+           {
+             ret = true;
+             break;
+           }
+         }
+       }
+       return ret;
+// later if pool index is available:
+//     for ( const PoolItem & pi : installed() )
+//       if ( pi.isRetracted() )
+//         return true;
+//     return false;
+      }
+
       bool isUnmaintained() const
       { return availableEmpty(); }
 
       bool multiversionInstall() const
-      { return theObj().satSolvable().multiversionInstall(); }
+      {
+       for ( const PoolItem & pi : picklist() )
+       {
+         if ( pi.multiversionInstall() )
+           return true;
+       }
+       return false;
+      }
 
       bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
 
       bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
 
+      Status pickStatus( const PoolItem & pi_r ) const;
+
+      bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
+
       ////////////////////////////////////////////////////////////////////////
 
       bool isUndetermined() const
@@ -279,52 +391,66 @@ namespace zypp
       void setLicenceConfirmed( bool val_r )
       { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
 
+      /** \copydoc Selectable::hasLocks()const */
+      bool hasLocks() const
+      {
+       for ( const PoolItem & pi : available() )
+       {
+         if ( pi.status().isLocked() )
+           return true;
+       }
+        for ( const PoolItem & pi : installed() )
+       {
+         if ( pi.status().isLocked() )
+           return true;
+       }
+       return false;
+      }
+
     private:
       PoolItem transactingInstalled() const
       {
-        for_( it, installedBegin(), installedEnd() )
+        for ( const PoolItem & pi : installed() )
           {
-            if ( (*it).status().transacts() )
-              return (*it);
+            if ( pi.status().transacts() )
+              return pi;
           }
         return PoolItem();
       }
 
       PoolItem transactingCandidate() const
       {
-        for_( it, availableBegin(), availableEnd() )
+        for ( const PoolItem & pi : available() )
           {
-            if ( (*it).status().transacts() )
-              return (*it);
+            if ( pi.status().transacts() )
+              return pi;
           }
         return PoolItem();
       }
 
       PoolItem defaultCandidate() const
       {
-        if ( ! ( multiversionInstall() || installedEmpty() ) )
+        if ( ! installedEmpty() )
         {
           // prefer the installed objects arch and vendor
-          bool solver_allowVendorChange( ZConfig::instance().solver_allowVendorChange() );
-          for ( installed_const_iterator iit = installedBegin();
-                iit != installedEnd(); ++iit )
+          bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
+          for ( const PoolItem & ipi : installed() )
           {
             PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
-            for ( available_const_iterator it = availableBegin();
-                  it != availableEnd(); ++it )
+            for (  const PoolItem & api : available() )
             {
               // 'same arch' includes allowed changes to/from noarch.
-              if ( (*iit)->arch() == (*it)->arch() || (*iit)->arch() == Arch_noarch || (*it)->arch() == Arch_noarch )
+              if ( ipi.arch() == api.arch() || ipi.arch() == Arch_noarch || api.arch() == Arch_noarch )
               {
                 if ( ! solver_allowVendorChange )
                 {
-                  if ( VendorAttr::instance().equivalent( (*iit), (*it) ) )
-                    return *it;
+                  if ( VendorAttr::instance().equivalent( ipi, api ) )
+                    return api;
                   else if ( ! sameArch ) // remember best same arch in case no same vendor found
-                     sameArch = *it;
+                     sameArch = api;
                 }
                 else // same arch is sufficient
-                  return *it;
+                  return api;
               }
             }
             if ( sameArch )
@@ -339,26 +465,35 @@ namespace zypp
 
       bool allCandidatesLocked() const
       {
-        for ( available_const_iterator it = availableBegin();
-              it != availableEnd(); ++it )
+        for ( const PoolItem & pi : available() )
           {
-            if ( ! (*it).status().isLocked() )
+            if ( ! pi.status().isLocked() )
               return false;
           }
         return( ! _availableItems.empty() );
       }
 
+      bool allInstalledLocked() const
+      {
+        for ( const PoolItem & pi : installed() )
+          {
+            if ( ! pi.status().isLocked() )
+              return false;
+          }
+        return( ! _installedItems.empty() );
+      }
+
+
     private:
       const IdString         _ident;
-      const ResObject::Kind  _kind;
+      const ResKind          _kind;
       const std::string      _name;
       InstalledItemSet       _installedItems;
       AvailableItemSet       _availableItems;
-      //! Best among availabe with restpect to installed.
-      PoolItem               _defaultCandidate;
-
       //! The object selected by setCandidateObj() method.
       PoolItem               _candidate;
+      //! lazy initialized picklist
+      mutable scoped_ptr<PickList> _picklistPtr;
     };
     ///////////////////////////////////////////////////////////////////
 
@@ -383,39 +518,64 @@ namespace zypp
       {
         PoolItem icand( obj.installedObj() );
         str << "   (I " << obj.installedSize() << ") {" << endl;
-        for_( it, obj.installedBegin(), obj.installedEnd() )
+        for ( const PoolItem & pi : obj.installed() )
         {
           char t = ' ';
-          if ( *it == icand )
+          if ( pi == icand )
           {
             t = 'i';
           }
-          str << " " << t << " " << *it << endl;
+          str << " " << t << " " << pi << endl;
         }
         str << "}  ";
       }
 
       if ( obj.availableEmpty() )
       {
-        str << "(A 0) {}";
+        str << "(A 0) {}" << endl << "   ";
       }
       else
       {
         PoolItem cand( obj.candidateObj() );
         PoolItem up( obj.updateCandidateObj() );
         str << "(A " << obj.availableSize() << ") {" << endl;
-        for_( it, obj.availableBegin(), obj.availableEnd() )
+        for ( const PoolItem & pi : obj.available() )
+        {
+          char t = ' ';
+          if ( pi == cand )
+          {
+            t = pi == up ? 'C' : 'c';
+          }
+          else if ( pi == up )
+          {
+            t = 'u';
+          }
+          str << " " << t << " " << pi << endl;
+        }
+        str << "}  ";
+      }
+
+      if ( obj.picklistEmpty() )
+      {
+        str << "(P 0) {}";
+      }
+      else
+      {
+        PoolItem cand( obj.candidateObj() );
+        PoolItem up( obj.updateCandidateObj() );
+        str << "(P " << obj.picklistSize() << ") {" << endl;
+        for ( const PoolItem & pi : obj.picklist() )
         {
           char t = ' ';
-          if ( *it == cand )
+          if ( pi == cand )
           {
-            t = *it == up ? 'C' : 'c';
+            t = pi == up ? 'C' : 'c';
           }
-          else if ( *it == up )
+          else if ( pi == up )
           {
             t = 'u';
           }
-          str << " " << t << " " << *it << endl;
+          str << " " << t << " " << pi << "\t" << obj.pickStatus( pi ) << endl;
         }
         str << "}  ";
       }