124299063cba60a9fba9bdc63f8fc6b6b4d45425
[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/ServiceInfo.h"
20
21 using namespace std;
22 using zypp::xml::escape;
23
24 ///////////////////////////////////////////////////////////////////////////////
25 namespace zypp
26 {//////////////////////////////////////////////////////////////////////////////
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //  CLASS NAME : ServiceInfo::Impl
31   //
32   struct ServiceInfo::Impl
33   {
34     typedef ServiceInfo::ReposToEnable  ReposToEnable;
35     typedef ServiceInfo::ReposToDisable ReposToDisable;
36
37   public:
38     RepoVariablesReplacedUrl url;
39     repo::ServiceType type;
40     ReposToEnable  reposToEnable;
41     ReposToDisable reposToDisable;
42     RepoStates     repoStates;
43
44
45   public:
46     Impl()
47       : type(repo::ServiceType::NONE_e)
48     {}
49
50     Impl(const Url & url_)
51       : url(url_)
52       , type(repo::ServiceType::NONE_e)
53     {}
54
55     ~Impl()
56     {}
57
58     void setProbedType( const repo::ServiceType & t ) const
59     {
60       if ( type == repo::ServiceType::NONE
61            && t != repo::ServiceType::NONE )
62       {
63         // lazy init!
64         const_cast<Impl*>(this)->type = t;
65       }
66     }
67
68   private:
69     friend Impl * rwcowClone<Impl>( const Impl * rhs );
70
71     /** clone for RWCOW_pointer */
72     Impl * clone() const
73     { return new Impl( *this ); }
74   };
75   ///////////////////////////////////////////////////////////////////
76
77
78   ///////////////////////////////////////////////////////////////////
79   //
80   //  CLASS NAME : ServiceInfo::Impl
81   //
82   ///////////////////////////////////////////////////////////////////
83
84   const ServiceInfo ServiceInfo::noService;
85
86   ServiceInfo::ServiceInfo() : _pimpl( new Impl() ) {}
87
88   ServiceInfo::ServiceInfo(const string & alias)
89     : repo::RepoInfoBase(alias), _pimpl( new Impl() )
90   {}
91
92   ServiceInfo::ServiceInfo(const string & alias, const Url & url)
93     : repo::RepoInfoBase(alias), _pimpl( new Impl(url) )
94   {}
95
96   ServiceInfo::~ServiceInfo()
97   {}
98
99   Url ServiceInfo::url() const                  // Variables replaced
100   { return _pimpl->url.transformed(); }
101
102   Url ServiceInfo::rawUrl() const               // Raw
103   { return _pimpl->url.raw(); }
104
105   void ServiceInfo::setUrl( const Url& url )    // Raw
106   { _pimpl->url.raw() = url; }
107
108   repo::ServiceType ServiceInfo::type() const
109   { return _pimpl->type; }
110   void ServiceInfo::setType( const repo::ServiceType & type )
111   { _pimpl->type = type; }
112
113   void ServiceInfo::setProbedType( const repo::ServiceType &t ) const
114   { _pimpl->setProbedType( t ); }
115
116   bool ServiceInfo::reposToEnableEmpty() const
117   { return _pimpl->reposToEnable.empty(); }
118
119   ServiceInfo::ReposToEnable::size_type ServiceInfo::reposToEnableSize() const
120   { return _pimpl->reposToEnable.size(); }
121
122   ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableBegin() const
123   { return _pimpl->reposToEnable.begin(); }
124
125   ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableEnd() const
126   { return _pimpl->reposToEnable.end(); }
127
128   bool ServiceInfo::repoToEnableFind( const std::string & alias_r ) const
129   { return( _pimpl->reposToEnable.find( alias_r ) != _pimpl->reposToEnable.end() ); }
130
131   void ServiceInfo::addRepoToEnable( const std::string & alias_r )
132   {
133     _pimpl->reposToEnable.insert( alias_r );
134     _pimpl->reposToDisable.erase( alias_r );
135   }
136
137   void ServiceInfo::delRepoToEnable( const std::string & alias_r )
138   { _pimpl->reposToEnable.erase( alias_r ); }
139
140   void ServiceInfo::clearReposToEnable()
141   { _pimpl->reposToEnable.clear(); }
142
143
144   bool ServiceInfo::reposToDisableEmpty() const
145   { return _pimpl->reposToDisable.empty(); }
146
147   ServiceInfo::ReposToDisable::size_type ServiceInfo::reposToDisableSize() const
148   { return _pimpl->reposToDisable.size(); }
149
150   ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableBegin() const
151   { return _pimpl->reposToDisable.begin(); }
152
153   ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableEnd() const
154   { return _pimpl->reposToDisable.end(); }
155
156   bool ServiceInfo::repoToDisableFind( const std::string & alias_r ) const
157   { return( _pimpl->reposToDisable.find( alias_r ) != _pimpl->reposToDisable.end() ); }
158
159   void ServiceInfo::addRepoToDisable( const std::string & alias_r )
160   {
161     _pimpl->reposToDisable.insert( alias_r );
162     _pimpl->reposToEnable.erase( alias_r );
163   }
164
165   void ServiceInfo::delRepoToDisable( const std::string & alias_r )
166   { _pimpl->reposToDisable.erase( alias_r ); }
167
168   void ServiceInfo::clearReposToDisable()
169   { _pimpl->reposToDisable.clear(); }
170
171   const ServiceInfo::RepoStates & ServiceInfo::repoStates() const
172   { return _pimpl->repoStates; }
173
174   void ServiceInfo::setRepoStates( RepoStates newStates_r )
175   { swap( _pimpl->repoStates, newStates_r ); }
176
177   std::ostream & operator<<( std::ostream & str, const ServiceInfo::RepoState & obj )
178   {
179     return str
180         << "enabled=" << obj.enabled << " "
181         << "autorefresh=" << obj.autorefresh << " "
182         << "priority=" << obj.priority;
183   }
184
185   std::ostream & ServiceInfo::dumpAsIniOn( std::ostream & str ) const
186   {
187     RepoInfoBase::dumpAsIniOn(str)
188       << "url = " << rawUrl() << endl
189       << "type = " << type() << endl;
190
191     if ( ! repoStates().empty() )
192     {
193       unsigned cnt = 0U;
194       for ( const auto & el : repoStates() )
195       {
196         std::string tag( "repo_" );
197         tag += str::numstring( ++cnt );
198         const RepoState & state( el.second );
199
200         str << tag << "=" << el.first << endl
201             << tag << "_enabled=" << state.enabled << endl
202             << tag << "_autorefresh=" << state.autorefresh << endl;
203         if ( state.priority != RepoInfo::defaultPriority() )
204           str
205             << tag << "_priority=" << state.priority << endl;
206       }
207     }
208
209     if ( ! reposToEnableEmpty() )
210       str << "repostoenable = " << str::joinEscaped( reposToEnableBegin(), reposToEnableEnd() ) << endl;
211     if ( ! reposToDisableEmpty() )
212       str << "repostodisable = " << str::joinEscaped( reposToDisableBegin(), reposToDisableEnd() ) << endl;
213     return str;
214   }
215
216   ostream & ServiceInfo::dumpAsXmlOn( ostream & str, const string & content ) const
217   {
218     str
219       << "<service"
220       << " alias=\"" << escape(alias()) << "\""
221       << " name=\"" << escape(name()) << "\""
222       << " enabled=\"" << enabled() << "\""
223       << " autorefresh=\"" << autorefresh() << "\""
224       << " url=\"" << escape(url().asString()) << "\""
225       << " type=\"" << type().asString() << "\"";
226
227     if (content.empty())
228       str << "/>" << endl;
229     else
230       str << ">" << endl << content << "</service>" << endl;
231
232     return str;
233   }
234
235
236   std::ostream & operator<<( std::ostream& str, const ServiceInfo &obj )
237   {
238     return obj.dumpAsIniOn(str);
239   }
240
241
242 ///////////////////////////////////////////////////////////////////////////////
243 } //namespace zypp
244 ///////////////////////////////////////////////////////////////////////////////