zypp/RepoInfo.cc
[platform/upstream/libzypp.git] / zypp / RepoInfo.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoInfo.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/base/Logger.h"
15
16 #include "zypp/RepoInfo.h"
17
18 using namespace std;
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   ///////////////////////////////////////////////////////////////////
25   //
26   //    CLASS NAME : RepoInfo::Impl
27   //
28   /** RepoInfo implementation. */
29   struct RepoInfo::Impl
30   {
31
32     Impl()
33       : enabled (false),
34         autorefresh(false),
35         gpgcheck(true),
36         type(repo::RepoType::NONE_e)
37     {}
38
39     ~Impl()
40     {
41       //MIL << std::endl;
42     }
43   public:
44     bool enabled;
45     bool autorefresh;
46     bool gpgcheck;
47     Url gpgkey_url;
48     repo::RepoType type;
49     Url mirrorlist_url;
50     std::set<Url> baseUrls;
51     Pathname path;
52     std::string alias;
53     std::string name;
54     Pathname filepath;
55     Pathname metadatapath;
56   public:
57
58   private:
59     friend Impl * rwcowClone<Impl>( const Impl * rhs );
60     /** clone for RWCOW_pointer */
61     Impl * clone() const
62     { return new Impl( *this ); }
63   };
64   ///////////////////////////////////////////////////////////////////
65
66   /** \relates RepoInfo::Impl Stream output */
67   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
68   {
69     return str << "RepoInfo::Impl";
70   }
71
72   ///////////////////////////////////////////////////////////////////
73   //
74   //    CLASS NAME : RepoInfo
75   //
76   ///////////////////////////////////////////////////////////////////
77
78   ///////////////////////////////////////////////////////////////////
79   //
80   //    METHOD NAME : RepoInfo::RepoInfo
81   //    METHOD TYPE : Ctor
82   //
83   RepoInfo::RepoInfo()
84   : _pimpl( new Impl() )
85   {}
86
87   ///////////////////////////////////////////////////////////////////
88   //
89   //    METHOD NAME : RepoInfo::~RepoInfo
90   //    METHOD TYPE : Dtor
91   //
92   RepoInfo::~RepoInfo()
93   {
94     //MIL << std::endl;
95   }
96
97   RepoInfo & RepoInfo::setEnabled( bool enabled )
98   {
99     _pimpl->enabled = enabled;
100     return *this;
101   }
102
103   RepoInfo & RepoInfo::setAutorefresh( bool autorefresh )
104   {
105     _pimpl->autorefresh = autorefresh;
106     return *this;
107   }
108
109   RepoInfo & RepoInfo::setGpgCheck( bool check )
110   {
111     _pimpl->gpgcheck = check;
112     return *this;
113   }
114
115   RepoInfo & RepoInfo::setMirrorListUrl( const Url &url )
116   {
117     _pimpl->mirrorlist_url = url;
118     return *this;
119   }
120
121   RepoInfo & RepoInfo::setGpgKeyUrl( const Url &url )
122   {
123     _pimpl->gpgkey_url = url;
124     return *this;
125   }
126
127   RepoInfo & RepoInfo::addBaseUrl( const Url &url )
128   {
129     _pimpl->baseUrls.insert(url);
130     return *this;
131   }
132
133   RepoInfo & RepoInfo::setBaseUrl( const Url &url )
134   {
135     _pimpl->baseUrls.clear();
136     addBaseUrl(url);
137     return *this;
138   }
139
140   RepoInfo & RepoInfo::setPath( const Pathname &path )
141   {
142     _pimpl->path = path;
143     return *this;
144   }
145
146   RepoInfo & RepoInfo::setAlias( const std::string &alias )
147   {
148     _pimpl->alias = alias;
149     return *this;
150   }
151
152   RepoInfo & RepoInfo::setType( const repo::RepoType &t )
153   {
154     _pimpl->type = t;
155     return *this;
156   }
157
158   RepoInfo & RepoInfo::setName( const std::string &name )
159   {
160     _pimpl->name = name;
161     return *this;
162   }
163
164   RepoInfo & RepoInfo::setFilepath( const Pathname &filepath )
165   {
166     _pimpl->filepath = filepath;
167     return *this;
168   }
169
170   RepoInfo & RepoInfo::setMetadataPath( const Pathname &path )
171   {
172     _pimpl->metadatapath = path;
173     return *this;
174   }
175
176   bool RepoInfo::enabled() const
177   { return _pimpl->enabled; }
178
179   bool RepoInfo::autorefresh() const
180   { return _pimpl->autorefresh; }
181
182   bool RepoInfo::gpgCheck() const
183   { return _pimpl->gpgcheck; }
184
185   std::string RepoInfo::alias() const
186   { return _pimpl->alias; }
187
188   std::string RepoInfo::name() const
189   {
190     if ( _pimpl->name.empty() )
191     {
192       return alias();
193     }
194     
195     repo::RepoVariablesStringReplacer replacer;
196     return replacer(_pimpl->name);
197   }
198
199   Pathname RepoInfo::filepath() const
200   { return _pimpl->filepath; }
201
202   Pathname RepoInfo::metadataPath() const
203   { return _pimpl->metadatapath; }
204
205   repo::RepoType RepoInfo::type() const
206   { return _pimpl->type; }
207
208   Url RepoInfo::mirrorListUrl() const
209   { return _pimpl->mirrorlist_url; }
210
211   Url RepoInfo::gpgKeyUrl() const
212   { return _pimpl->gpgkey_url; }
213
214   std::set<Url> RepoInfo::baseUrls() const
215   {
216     RepoInfo::url_set replaced_urls;
217     repo::RepoVariablesUrlReplacer replacer;
218     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
219           it != _pimpl->baseUrls.end();
220           ++it )
221     {
222       replaced_urls.insert(replacer(*it));
223     }
224     return replaced_urls;
225
226     return _pimpl->baseUrls;
227   }
228
229   Pathname RepoInfo::path() const
230   { return _pimpl->path; }
231
232   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
233   {
234     return make_transform_iterator( _pimpl->baseUrls.begin(),
235                                     repo::RepoVariablesUrlReplacer() );
236     //return _pimpl->baseUrls.begin();
237   }
238
239   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
240   {
241     //return _pimpl->baseUrls.end();
242     return make_transform_iterator( _pimpl->baseUrls.end(),
243                                     repo::RepoVariablesUrlReplacer() );
244   }
245
246   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
247   { return _pimpl->baseUrls.size(); }
248
249   bool RepoInfo::baseUrlsEmpty() const
250   { return _pimpl->baseUrls.empty(); }
251
252   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
253   {
254     str << "--------------------------------------" << std::endl;
255     str << "- alias       : " << alias() << std::endl;
256     for ( urls_const_iterator it = baseUrlsBegin();
257           it != baseUrlsEnd();
258           ++it )
259     {
260       str << "- url         : " << *it << std::endl;
261     }
262     str << "- path        : " << path() << std::endl;
263     str << "- type        : " << type() << std::endl;
264     str << "- enabled     : " << enabled() << std::endl;
265
266     str << "- autorefresh : " << autorefresh() << std::endl;
267     str << "- gpgcheck : " << gpgCheck() << std::endl;
268     str << "- gpgkey : " << gpgKeyUrl() << std::endl;
269
270     return str;
271   }
272
273   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
274   {
275     // we save the original data without variable replacement
276     str << "[" << alias() << "]" << endl;
277     str << "name=" << _pimpl->name << endl;
278
279     if ( ! _pimpl->baseUrls.empty() )
280       str << "baseurl=";
281     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
282           it != _pimpl->baseUrls.end();
283           ++it )
284     {
285       str << *it << endl;
286     }
287
288     if ( ! _pimpl->path.empty() )
289       str << "path="<< path() << endl;
290
291     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
292       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
293
294     str << "type=" << type().asString() << endl;
295     str << "enabled=" << (enabled() ? "1" : "0") << endl;
296     str << "autorefresh=" << (autorefresh() ? "1" : "0") << endl;
297     str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
298     if ( ! (gpgKeyUrl().asString().empty()) )
299       str << "gpgkey=" <<gpgKeyUrl() << endl;
300
301     return str;
302   }
303
304   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
305   {
306     return obj.dumpOn(str);
307   }
308
309   /////////////////////////////////////////////////////////////////
310 } // namespace zypp
311 ///////////////////////////////////////////////////////////////////