fixup Fix to build with libxml 2.12.x (fixes #505)
[platform/upstream/libzypp.git] / zypp / ServiceInfo.cc
index 17f7df5..04bfcb2 100644 (file)
 #include <ostream>
 #include <iostream>
 
-#include "zypp/parser/xml/XmlEscape.h"
+#include <zypp/base/String.h>
+#include <zypp/base/DefaultIntegral.h>
+#include <zypp/parser/xml/XmlEscape.h>
 
-#include "zypp/RepoInfo.h"
-#include "zypp/parser/RepoindexFileReader.h"
-#include "zypp/repo/RepoInfoBaseImpl.h"
+#include <zypp/RepoInfo.h>
+#include <zypp/ServiceInfo.h>
 
-#include "zypp/ServiceInfo.h"
-
-using namespace std;
+using std::endl;
 using zypp::xml::escape;
 
 ///////////////////////////////////////////////////////////////////////////////
 namespace zypp
 {//////////////////////////////////////////////////////////////////////////////
 
-
-  struct RepoInfoCollector
-  {
-    vector<RepoInfo> repos;
-    bool collect(const RepoInfo & info)
-    {
-      repos.push_back(info);
-      return true;
-    }
-  };
-
   ///////////////////////////////////////////////////////////////////
   //
   //  CLASS NAME : ServiceInfo::Impl
   //
-  struct ServiceInfo::Impl : public repo::RepoInfoBase::Impl
+  struct ServiceInfo::Impl
   {
-    typedef ServiceInfo::CatalogsToEnable CatalogsToEnable;
+    typedef ServiceInfo::ReposToEnable  ReposToEnable;
+    typedef ServiceInfo::ReposToDisable ReposToDisable;
 
   public:
-    Url url;
-    CatalogsToEnable catalogsToEnable;
+    RepoVariablesReplacedUrl _url;
+    repo::ServiceType _type;
+    ReposToEnable _reposToEnable;
+    ReposToDisable _reposToDisable;
+    RepoStates _repoStates;
+    DefaultIntegral<Date::Duration,0> _ttl;
+    Date _lrf;
 
   public:
-    Impl() : repo::RepoInfoBase::Impl() {}
+    Impl()
+    {}
+
+    Impl( const Url & url_r )
+    : _url( url_r )
+    {}
+
+    ~Impl()
+    {}
 
-    Impl(const Url & url_) : url(url_) {}
+    void setProbedType( const repo::ServiceType & type_r ) const
+    {
+      if ( _type == repo::ServiceType::NONE
+           && type_r != repo::ServiceType::NONE )
+      {
+        // lazy init!
+        const_cast<Impl*>(this)->_type = type_r;
+      }
+    }
 
   private:
     friend Impl * rwcowClone<Impl>( const Impl * rhs );
@@ -75,58 +85,149 @@ namespace zypp
 
   ServiceInfo::ServiceInfo() : _pimpl( new Impl() ) {}
 
-  ServiceInfo::ServiceInfo(const string & alias)
+  ServiceInfo::ServiceInfo(const std::string & alias)
     : repo::RepoInfoBase(alias), _pimpl( new Impl() )
   {}
 
-  ServiceInfo::ServiceInfo(const string & alias, const Url & url)
+  ServiceInfo::ServiceInfo(const std::string & alias, const Url & url)
     : repo::RepoInfoBase(alias), _pimpl( new Impl(url) )
   {}
 
-  Url ServiceInfo::url() const { return _pimpl->url; }
-  void ServiceInfo::setUrl( const Url& url ) { _pimpl->url = url; }
+  ServiceInfo::~ServiceInfo()
+  {}
 
+  Url ServiceInfo::url() const                 // Variables replaced
+  { return _pimpl->_url.transformed(); }
 
-  bool catalogsToEnableEmpty() const
-  { return _pimpl->catalogsToEnable.empty(); }
+  Url ServiceInfo::rawUrl() const              // Raw
+  { return _pimpl->_url.raw(); }
 
-  CatalogsToEnable::size_type catalogsToEnableSize() const
-  { return _pimpl->catalogsToEnable.size(); }
+  void ServiceInfo::setUrl( const Url& url )   // Raw
+  { _pimpl->_url.raw() = url; }
 
-  CatalogsToEnable::const_iterator catalogsToEnableBegin() const
-  { return _pimpl->catalogsToEnable.begin(); }
+  repo::ServiceType ServiceInfo::type() const                          { return _pimpl->_type; }
+  void ServiceInfo::setType( const repo::ServiceType & type )          { _pimpl->_type = type; }
+  void ServiceInfo::setProbedType( const repo::ServiceType &t ) const  { _pimpl->setProbedType( t ); }
 
-  CatalogsToEnable::const_iterator catalogsToEnableEnd() const
-  { return _pimpl->catalogsToEnable.end(); }
+  Date::Duration ServiceInfo::ttl() const                      { return _pimpl->_ttl; }
+  void ServiceInfo::setTtl( Date::Duration ttl_r )             { _pimpl->_ttl = ttl_r; }
+  void ServiceInfo::setProbedTtl( Date::Duration ttl_r ) const { const_cast<ServiceInfo*>(this)->setTtl( ttl_r ); }
 
-  bool catalogToEnableFind( const std::string & alias_r ) const
-  { return( _pimpl->catalogsToEnable.find( alias_r ) != _pimpl->catalogsToEnable.end() ); }
+  Date ServiceInfo::lrf() const                                        { return _pimpl->_lrf; }
+  void ServiceInfo::setLrf( Date lrf_r )                       { _pimpl->_lrf = lrf_r; }
 
-  void addCatalogToEnable( const std::string & alias_r )
-  { return _pimpl->catalogsToEnable.insert( alias_r ); }
+  bool ServiceInfo::reposToEnableEmpty() const                                         { return _pimpl->_reposToEnable.empty(); }
+  ServiceInfo::ReposToEnable::size_type ServiceInfo::reposToEnableSize() const         { return _pimpl->_reposToEnable.size(); }
+  ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableBegin() const   { return _pimpl->_reposToEnable.begin(); }
+  ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableEnd() const     { return _pimpl->_reposToEnable.end(); }
 
-  void delCatalogToEnable( const std::string & alias_r )
-  { return _pimpl->catalogsToEnable.erase( alias_r ); }
+  bool ServiceInfo::repoToEnableFind( const std::string & alias_r ) const
+  { return( _pimpl->_reposToEnable.find( alias_r ) != _pimpl->_reposToEnable.end() ); }
 
+  void ServiceInfo::addRepoToEnable( const std::string & alias_r )
+  {
+    _pimpl->_reposToEnable.insert( alias_r );
+    _pimpl->_reposToDisable.erase( alias_r );
+  }
 
-  std::ostream & ServiceInfo::dumpAsIniOn( std::ostream & str ) const
+  void ServiceInfo::delRepoToEnable( const std::string & alias_r )
+  { _pimpl->_reposToEnable.erase( alias_r ); }
+
+  void ServiceInfo::clearReposToEnable()
+  { _pimpl->_reposToEnable.clear(); }
+
+
+  bool ServiceInfo::reposToDisableEmpty() const                                                { return _pimpl->_reposToDisable.empty(); }
+  ServiceInfo::ReposToDisable::size_type ServiceInfo::reposToDisableSize() const       { return _pimpl->_reposToDisable.size(); }
+  ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableBegin() const { return _pimpl->_reposToDisable.begin(); }
+  ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableEnd() const   { return _pimpl->_reposToDisable.end(); }
+
+  bool ServiceInfo::repoToDisableFind( const std::string & alias_r ) const
+  { return( _pimpl->_reposToDisable.find( alias_r ) != _pimpl->_reposToDisable.end() ); }
+
+  void ServiceInfo::addRepoToDisable( const std::string & alias_r )
   {
-    return RepoInfoBase::dumpAsIniOn(str)
-        << "url = " << url() << endl;
+    _pimpl->_reposToDisable.insert( alias_r );
+    _pimpl->_reposToEnable.erase( alias_r );
   }
 
-  std::ostream & ServiceInfo::dumpAsXMLOn( std::ostream & str) const
+  void ServiceInfo::delRepoToDisable( const std::string & alias_r )
+  { _pimpl->_reposToDisable.erase( alias_r ); }
+
+  void ServiceInfo::clearReposToDisable()
+  { _pimpl->_reposToDisable.clear(); }
+
+
+  const ServiceInfo::RepoStates & ServiceInfo::repoStates() const      { return _pimpl->_repoStates; }
+  void ServiceInfo::setRepoStates( RepoStates newStates_r )            { swap( _pimpl->_repoStates, newStates_r ); }
+
+
+  std::ostream & operator<<( std::ostream & str, const ServiceInfo::RepoState & obj )
   {
     return str
+       << "enabled=" << obj.enabled << " "
+       << "autorefresh=" << obj.autorefresh << " "
+       << "priority=" << obj.priority;
+  }
+
+  std::ostream & ServiceInfo::dumpAsIniOn( std::ostream & str ) const
+  {
+    RepoInfoBase::dumpAsIniOn(str)
+      << "url = " << rawUrl() << endl
+      << "type = " << type() << endl;
+
+    if ( ttl() )
+      str << "ttl_sec = " << ttl() << endl;
+
+    if ( lrf() )
+      str << "lrf_dat = " << lrf().asSeconds() << endl;
+
+    if ( ! repoStates().empty() )
+    {
+      unsigned cnt = 0U;
+      for ( const auto & el : repoStates() )
+      {
+       std::string tag( "repo_" );
+       tag += str::numstring( ++cnt );
+       const RepoState & state( el.second );
+
+       str << tag << "=" << el.first << endl
+           << tag << "_enabled=" << state.enabled << endl
+           << tag << "_autorefresh=" << state.autorefresh << endl;
+       if ( state.priority != RepoInfo::defaultPriority() )
+         str
+           << tag << "_priority=" << state.priority << endl;
+      }
+    }
+
+    if ( ! reposToEnableEmpty() )
+      str << "repostoenable = " << str::joinEscaped( reposToEnableBegin(), reposToEnableEnd() ) << endl;
+    if ( ! reposToDisableEmpty() )
+      str << "repostodisable = " << str::joinEscaped( reposToDisableBegin(), reposToDisableEnd() ) << endl;
+    return str;
+  }
+
+  std::ostream & ServiceInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
+  {
+    str
       << "<service"
       << " alias=\"" << escape(alias()) << "\""
       << " name=\"" << escape(name()) << "\""
       << " enabled=\"" << enabled() << "\""
       << " autorefresh=\"" << autorefresh() << "\""
       << " url=\"" << escape(url().asString()) << "\""
-      << "/>" << endl;
+      << " type=\"" << type().asString() << "\""
+      << " ttl_sec=\"" << ttl() << "\"";
+
+    if (content.empty())
+      str << "/>" << endl;
+    else
+      str << ">" << endl << content << "</service>" << endl;
+
+    return str;
   }
 
+
   std::ostream & operator<<( std::ostream& str, const ServiceInfo &obj )
   {
     return obj.dumpAsIniOn(str);