- ServiceType introduced
[platform/upstream/libzypp.git] / zypp / ServiceInfo.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ServiceInfo.cc
10  *
11  */
12 #include <ostream>
13 #include <iostream>
14
15 #include "zypp/base/String.h"
16 #include "zypp/parser/xml/XmlEscape.h"
17
18 #include "zypp/RepoInfo.h"
19 #include "zypp/parser/RepoindexFileReader.h"
20 #include "zypp/repo/RepoInfoBaseImpl.h"
21
22 #include "zypp/ServiceInfo.h"
23
24 using namespace std;
25 using zypp::xml::escape;
26
27 ///////////////////////////////////////////////////////////////////////////////
28 namespace zypp
29 {//////////////////////////////////////////////////////////////////////////////
30
31
32   struct RepoInfoCollector
33   {
34     vector<RepoInfo> repos;
35     bool collect(const RepoInfo & info)
36     {
37       repos.push_back(info);
38       return true;
39     }
40   };
41
42   ///////////////////////////////////////////////////////////////////
43   //
44   //  CLASS NAME : ServiceInfo::Impl
45   //
46   struct ServiceInfo::Impl : public repo::RepoInfoBase::Impl
47   {
48     typedef ServiceInfo::CatalogsToEnable CatalogsToEnable;
49     typedef ServiceInfo::CatalogsToDisable CatalogsToDisable;
50
51   public:
52     Url url;
53     repo::ServiceType type;
54     CatalogsToEnable  catalogsToEnable;
55     CatalogsToDisable catalogsToDisable;
56
57   public:
58     Impl() : repo::RepoInfoBase::Impl() {}
59
60     Impl(const Url & url_) : url(url_) {}
61     
62     void setProbedType( const repo::ServiceType & t ) const
63     {
64       if ( type == repo::ServiceType::NONE
65            && t != repo::ServiceType::NONE )
66       {
67         // lazy init!
68         const_cast<Impl*>(this)->type = t;
69       }
70     }
71
72   private:
73     friend Impl * rwcowClone<Impl>( const Impl * rhs );
74
75     /** clone for RWCOW_pointer */
76     Impl * clone() const
77     { return new Impl( *this ); }
78   };
79   ///////////////////////////////////////////////////////////////////
80
81
82   ///////////////////////////////////////////////////////////////////
83   //
84   //  CLASS NAME : ServiceInfo::Impl
85   //
86   ///////////////////////////////////////////////////////////////////
87
88   const ServiceInfo ServiceInfo::noService;
89
90   ServiceInfo::ServiceInfo() : _pimpl( new Impl() ) {}
91
92   ServiceInfo::ServiceInfo(const string & alias)
93     : repo::RepoInfoBase(alias), _pimpl( new Impl() )
94   {}
95
96   ServiceInfo::ServiceInfo(const string & alias, const Url & url)
97     : repo::RepoInfoBase(alias), _pimpl( new Impl(url) )
98   {}
99
100   Url ServiceInfo::url() const { return _pimpl->url; }
101   void ServiceInfo::setUrl( const Url& url ) { _pimpl->url = url; }
102
103   repo::ServiceType ServiceInfo::type() const
104   { return _pimpl->type; }
105   void ServiceInfo::setType( const repo::ServiceType & type )
106   { _pimpl->type = type; }
107
108   void ServiceInfo::setProbedType( const repo::ServiceType &t ) const
109   { _pimpl->setProbedType( t ); }
110
111   bool ServiceInfo::catalogsToEnableEmpty() const
112   { return _pimpl->catalogsToEnable.empty(); }
113
114   ServiceInfo::CatalogsToEnable::size_type ServiceInfo::catalogsToEnableSize() const
115   { return _pimpl->catalogsToEnable.size(); }
116
117   ServiceInfo::CatalogsToEnable::const_iterator ServiceInfo::catalogsToEnableBegin() const
118   { return _pimpl->catalogsToEnable.begin(); }
119
120   ServiceInfo::CatalogsToEnable::const_iterator ServiceInfo::catalogsToEnableEnd() const
121   { return _pimpl->catalogsToEnable.end(); }
122
123   bool ServiceInfo::catalogToEnableFind( const std::string & alias_r ) const
124   { return( _pimpl->catalogsToEnable.find( alias_r ) != _pimpl->catalogsToEnable.end() ); }
125
126   void ServiceInfo::addCatalogToEnable( const std::string & alias_r )
127   { _pimpl->catalogsToEnable.insert( alias_r ); }
128
129   void ServiceInfo::delCatalogToEnable( const std::string & alias_r )
130   { _pimpl->catalogsToEnable.erase( alias_r ); }
131
132
133   bool ServiceInfo::catalogsToDisableEmpty() const
134   { return _pimpl->catalogsToDisable.empty(); }
135
136   ServiceInfo::CatalogsToDisable::size_type ServiceInfo::catalogsToDisableSize() const
137   { return _pimpl->catalogsToDisable.size(); }
138
139   ServiceInfo::CatalogsToDisable::const_iterator ServiceInfo::catalogsToDisableBegin() const
140   { return _pimpl->catalogsToDisable.begin(); }
141
142   ServiceInfo::CatalogsToDisable::const_iterator ServiceInfo::catalogsToDisableEnd() const
143   { return _pimpl->catalogsToDisable.end(); }
144
145   bool ServiceInfo::catalogToDisableFind( const std::string & alias_r ) const
146   { return( _pimpl->catalogsToDisable.find( alias_r ) != _pimpl->catalogsToDisable.end() ); }
147
148   void ServiceInfo::addCatalogToDisable( const std::string & alias_r )
149   { _pimpl->catalogsToDisable.insert( alias_r ); }
150
151   void ServiceInfo::delCatalogToDisable( const std::string & alias_r )
152   { _pimpl->catalogsToDisable.erase( alias_r ); }
153
154
155   std::ostream & ServiceInfo::dumpAsIniOn( std::ostream & str ) const
156   {
157     RepoInfoBase::dumpAsIniOn(str)
158       << "url = " << url() << endl
159       << "type = " << type() << endl;
160
161     if ( ! catalogsToEnableEmpty() )
162       str << "catalogstoenable = " << str::joinEscaped( catalogsToEnableBegin(), catalogsToEnableEnd() ) << endl;
163     if ( ! catalogsToDisableEmpty() )
164       str << "catalogstodisable = " << str::joinEscaped( catalogsToDisableBegin(), catalogsToDisableEnd() ) << endl;
165     return str;
166   }
167
168   std::ostream & ServiceInfo::dumpAsXMLOn(std::ostream & str) const
169   { return dumpAsXMLOn(str, ""); }
170
171   ostream & ServiceInfo::dumpAsXMLOn( ostream & str, const string & content) const
172   {
173     str
174       << "<service"
175       << " alias=\"" << escape(alias()) << "\""
176       << " name=\"" << escape(name()) << "\""
177       << " enabled=\"" << enabled() << "\""
178       << " autorefresh=\"" << autorefresh() << "\""
179       << " url=\"" << escape(url().asString()) << "\""
180       << " type=\"" << type().asString() << "\"";
181
182     if (content.empty())
183       str << "/>" << endl;
184     else
185       str << ">" << endl << content << "</service>" << endl;
186
187     return str;
188   }
189
190
191   std::ostream & operator<<( std::ostream& str, const ServiceInfo &obj )
192   {
193     return obj.dumpAsIniOn(str);
194   }
195
196
197 ///////////////////////////////////////////////////////////////////////////////
198 } //namespace zypp
199 ///////////////////////////////////////////////////////////////////////////////