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