Use service alias as namespace for its repositories aliases.
[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/repo/RepoInfoBaseImpl.h"
20
21 #include "zypp/ServiceInfo.h"
22
23 using namespace std;
24 using zypp::xml::escape;
25
26 ///////////////////////////////////////////////////////////////////////////////
27 namespace zypp
28 {//////////////////////////////////////////////////////////////////////////////
29
30   ///////////////////////////////////////////////////////////////////
31   //
32   //  CLASS NAME : ServiceInfo::Impl
33   //
34   struct ServiceInfo::Impl : public repo::RepoInfoBase::Impl
35   {
36     typedef ServiceInfo::ReposToEnable  ReposToEnable;
37     typedef ServiceInfo::ReposToDisable ReposToDisable;
38
39   public:
40     Url url;
41     repo::ServiceType type;
42     ReposToEnable  reposToEnable;
43     ReposToDisable reposToDisable;
44
45   public:
46     Impl()
47       : repo::RepoInfoBase::Impl()
48       , type(repo::ServiceType::NONE_e)
49     {}
50
51     Impl(const Url & url_)
52       : repo::RepoInfoBase::Impl()
53       , url(url_)
54       , type(repo::ServiceType::NONE_e)
55     {}
56
57     ~Impl()
58     {}
59
60     void setProbedType( const repo::ServiceType & t ) const
61     {
62       if ( type == repo::ServiceType::NONE
63            && t != repo::ServiceType::NONE )
64       {
65         // lazy init!
66         const_cast<Impl*>(this)->type = t;
67       }
68     }
69
70   private:
71     friend Impl * rwcowClone<Impl>( const Impl * rhs );
72
73     /** clone for RWCOW_pointer */
74     Impl * clone() const
75     { return new Impl( *this ); }
76   };
77   ///////////////////////////////////////////////////////////////////
78
79
80   ///////////////////////////////////////////////////////////////////
81   //
82   //  CLASS NAME : ServiceInfo::Impl
83   //
84   ///////////////////////////////////////////////////////////////////
85
86   const ServiceInfo ServiceInfo::noService;
87
88   ServiceInfo::ServiceInfo() : _pimpl( new Impl() ) {}
89
90   ServiceInfo::ServiceInfo(const string & alias)
91     : repo::RepoInfoBase(alias), _pimpl( new Impl() )
92   {}
93
94   ServiceInfo::ServiceInfo(const string & alias, const Url & url)
95     : repo::RepoInfoBase(alias), _pimpl( new Impl(url) )
96   {}
97
98   ServiceInfo::~ServiceInfo()
99   {}
100
101   Url ServiceInfo::url() const { return _pimpl->url; }
102   void ServiceInfo::setUrl( const Url& url ) { _pimpl->url = url; }
103
104   repo::ServiceType ServiceInfo::type() const
105   { return _pimpl->type; }
106   void ServiceInfo::setType( const repo::ServiceType & type )
107   { _pimpl->type = type; }
108
109   void ServiceInfo::setProbedType( const repo::ServiceType &t ) const
110   { _pimpl->setProbedType( t ); }
111
112   bool ServiceInfo::reposToEnableEmpty() const
113   { return _pimpl->reposToEnable.empty(); }
114
115   ServiceInfo::ReposToEnable::size_type ServiceInfo::reposToEnableSize() const
116   { return _pimpl->reposToEnable.size(); }
117
118   ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableBegin() const
119   { return _pimpl->reposToEnable.begin(); }
120
121   ServiceInfo::ReposToEnable::const_iterator ServiceInfo::reposToEnableEnd() const
122   { return _pimpl->reposToEnable.end(); }
123
124   bool ServiceInfo::repoToEnableFind( const std::string & alias_r ) const
125   { return( _pimpl->reposToEnable.find( alias_r ) != _pimpl->reposToEnable.end() ); }
126
127   void ServiceInfo::addRepoToEnable( const std::string & alias_r )
128   { _pimpl->reposToEnable.insert( alias_r ); }
129
130   void ServiceInfo::delRepoToEnable( const std::string & alias_r )
131   { _pimpl->reposToEnable.erase( alias_r ); }
132
133   void ServiceInfo::clearReposToEnable()
134   { _pimpl->reposToEnable.clear(); }
135
136
137   bool ServiceInfo::reposToDisableEmpty() const
138   { return _pimpl->reposToDisable.empty(); }
139
140   ServiceInfo::ReposToDisable::size_type ServiceInfo::reposToDisableSize() const
141   { return _pimpl->reposToDisable.size(); }
142
143   ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableBegin() const
144   { return _pimpl->reposToDisable.begin(); }
145
146   ServiceInfo::ReposToDisable::const_iterator ServiceInfo::reposToDisableEnd() const
147   { return _pimpl->reposToDisable.end(); }
148
149   bool ServiceInfo::repoToDisableFind( const std::string & alias_r ) const
150   { return( _pimpl->reposToDisable.find( alias_r ) != _pimpl->reposToDisable.end() ); }
151
152   void ServiceInfo::addRepoToDisable( const std::string & alias_r )
153   { _pimpl->reposToDisable.insert( alias_r ); }
154
155   void ServiceInfo::delRepoToDisable( const std::string & alias_r )
156   { _pimpl->reposToDisable.erase( alias_r ); }
157
158   void ServiceInfo::clearReposToDisable()
159   { _pimpl->reposToDisable.clear(); }
160
161
162   std::ostream & ServiceInfo::dumpAsIniOn( std::ostream & str ) const
163   {
164     RepoInfoBase::dumpAsIniOn(str)
165       << "url = " << url() << endl
166       << "type = " << type() << endl;
167
168     if ( ! reposToEnableEmpty() )
169       str << "repostoenable = " << str::joinEscaped( reposToEnableBegin(), reposToEnableEnd() ) << endl;
170     if ( ! reposToDisableEmpty() )
171       str << "repostodisable = " << str::joinEscaped( reposToDisableBegin(), reposToDisableEnd() ) << endl;
172     return str;
173   }
174
175   std::ostream & ServiceInfo::dumpAsXMLOn(std::ostream & str) const
176   { return dumpAsXMLOn(str, ""); }
177
178   ostream & ServiceInfo::dumpAsXMLOn( ostream & str, const string & content) const
179   {
180     str
181       << "<service"
182       << " alias=\"" << escape(alias()) << "\""
183       << " name=\"" << escape(name()) << "\""
184       << " enabled=\"" << enabled() << "\""
185       << " autorefresh=\"" << autorefresh() << "\""
186       << " url=\"" << escape(url().asString()) << "\""
187       << " type=\"" << type().asString() << "\"";
188
189     if (content.empty())
190       str << "/>" << endl;
191     else
192       str << ">" << endl << content << "</service>" << endl;
193
194     return str;
195   }
196
197
198   std::ostream & operator<<( std::ostream& str, const ServiceInfo &obj )
199   {
200     return obj.dumpAsIniOn(str);
201   }
202
203
204 ///////////////////////////////////////////////////////////////////////////////
205 } //namespace zypp
206 ///////////////////////////////////////////////////////////////////////////////