Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / ui / SelectableImpl.h
index 008333b..9bdf405 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,14 +91,15 @@ 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. */
+      /** Installed object (transacting ot highest version). */
       PoolItem installedObj() const
       {
-          if (!installedEmpty())
-              return *_installedItems.begin();
+        if ( installedEmpty() )
           return PoolItem();
+        PoolItem ret( transactingInstalled() );
+        return ret ? ret : *_installedItems.begin();
       }
 
       /** Best among available objects.
@@ -108,7 +111,7 @@ namespace zypp
         PoolItem ret( transactingCandidate() );
         if ( ret )
           return ret;
-        return _candidate ? _candidate : _defaultCandidate;
+        return _candidate ? _candidate : defaultCandidate();
       }
 
       /** Set a userCandidate (out of available objects).
@@ -117,19 +120,114 @@ namespace zypp
       */
       PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r );
 
-      /** Best among all objects. */
-      PoolItem theObj() const
+      /** The best candidate provided by a specific \ref Repository, if there is one.
+       * In contrary to \ref candidateObj, this may return no item even if
+       * there are available objects. This simply means the \ref Repository
+       * does not provide this object.
+       */
+      PoolItem candidateObjFrom( Repository repo_r ) const
       {
-        PoolItem ret( candidateObj() );
-        if (ret)
-            return ret;
+        for ( const PoolItem & pi : available() )
+        {
+          if ( pi.repository() == repo_r )
+            return pi;
+        }
+        return PoolItem();
+      }
+
+      /** The best candidate for update, if there is one.
+       * In contrary to \ref candidateObj, this may return no item even if
+       * there are available objects. This simply means the best object is
+       * already installed, and all available objects violate at least one
+       * update policy.
+       */
+      PoolItem updateCandidateObj() const
+      {
+       PoolItem defaultCand( defaultCandidate() );
+
+       // multiversionInstall: This returns the candidate for the last
+       // instance installed. Actually we'd need a list here.
+
+        if ( installedEmpty() || ! defaultCand )
+          return defaultCand;
+        // Here: installed and defaultCand are non NULL and it's not a
+        //       multiversion install.
+
+        PoolItem installed( installedObj() );
+        // check vendor change
+        if ( ! ( ResPool::instance().resolver().allowVendorChange()
+                 || VendorAttr::instance().equivalent( defaultCand->vendor(), installed->vendor() ) ) )
+          return PoolItem();
+
+        // check arch change (arch noarch changes are allowed)
+        if ( defaultCand->arch() != installed->arch()
+           && ! ( defaultCand->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
+          return PoolItem();
+
+        // check greater edition
+        if ( defaultCand->edition() <= installed->edition() )
+          return PoolItem();
 
-        if ( ! _installedItems.empty() )
-            return  (*_installedItems.begin());
+        return defaultCand;
+      }
 
+      /** \copydoc Selectable::highestAvailableVersionObj()const */
+      PoolItem highestAvailableVersionObj() const
+      {
+        PoolItem ret;
+        for ( const PoolItem & pi : available() )
+        {
+          if ( !ret || pi.edition() > ret.edition() )
+            ret = pi;
+        }
+        return ret;
+      }
+
+      /** \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 *it;
+          }
+        }
+        return PoolItem();
+      }
+
+      /** Best among all objects. */
+      PoolItem theObj() const
+      {
+        PoolItem ret( candidateObj() );
+        if ( ret )
+          return ret;
+        return installedObj();
+      }
+
       ////////////////////////////////////////////////////////////////////////
 
       bool availableEmpty() const
@@ -138,13 +236,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
@@ -159,11 +259,64 @@ 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 isUnmaintained() const
       { return availableEmpty(); }
 
+      bool multiversionInstall() const
+      {
+       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
       {
         PoolItem cand( candidateObj() );
@@ -197,41 +350,49 @@ namespace zypp
       { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
 
     private:
+      PoolItem transactingInstalled() const
+      {
+        for ( const PoolItem & pi : installed() )
+          {
+            if ( pi.status().transacts() )
+              return pi;
+          }
+        return PoolItem();
+      }
+
       PoolItem transactingCandidate() const
       {
-        for ( available_const_iterator it = availableBegin();
-              it != availableEnd(); ++it )
+        for ( const PoolItem & pi : available() )
           {
-            if ( (*it).status().transacts() )
-              return (*it);
+            if ( pi.status().transacts() )
+              return pi;
           }
         return PoolItem();
       }
 
       PoolItem defaultCandidate() const
       {
-        if ( !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 )
+            PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
+            for (  const PoolItem & api : available() )
             {
-              if ( (*iit)->arch() == (*it)->arch() )
+              // 'same arch' includes allowed changes to/from noarch.
+              if ( ipi.arch() == api.arch() || ipi.arch() == Arch_noarch || api.arch() == Arch_noarch )
               {
                 if ( ! solver_allowVendorChange )
                 {
-                  if ( VendorAttr::instance().equivalent( (*iit).satSolvable().vendor(), (*it).satSolvable().vendor() ) )
-                    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 )
@@ -246,25 +407,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;
     };
     ///////////////////////////////////////////////////////////////////
 
@@ -280,15 +451,76 @@ namespace zypp
     /** \relates Selectable::Impl Stream output */
     inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
     {
-      str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status() << endl;
-      if ( obj.candidateObj() )
-        str << "(C " << obj.candidateObj() << ")" << endl;
-      else
-        str << "(C NONE )" << endl;
-      dumpRange( str << "  (I " << obj.installedSize() << ") ", obj.installedBegin(), obj.installedEnd() );
+      str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
+          << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
+
       if ( obj.installedEmpty() )
-        str << endl << " ";
-      dumpRange( str << " (A " << obj.availableSize() << ") ", obj.availableBegin(), obj.availableEnd() ) << endl;
+        str << "   (I 0) {}" << endl << "   ";
+      else
+      {
+        PoolItem icand( obj.installedObj() );
+        str << "   (I " << obj.installedSize() << ") {" << endl;
+        for ( const PoolItem & pi : obj.installed() )
+        {
+          char t = ' ';
+          if ( pi == icand )
+          {
+            t = 'i';
+          }
+          str << " " << t << " " << pi << endl;
+        }
+        str << "}  ";
+      }
+
+      if ( obj.availableEmpty() )
+      {
+        str << "(A 0) {}" << endl << "   ";
+      }
+      else
+      {
+        PoolItem cand( obj.candidateObj() );
+        PoolItem up( obj.updateCandidateObj() );
+        str << "(A " << obj.availableSize() << ") {" << endl;
+        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 ( pi == cand )
+          {
+            t = pi == up ? 'C' : 'c';
+          }
+          else if ( pi == up )
+          {
+            t = 'u';
+          }
+          str << " " << t << " " << pi << "\t" << obj.pickStatus( pi ) << endl;
+        }
+        str << "}  ";
+      }
 
       return str;
     }