- merge Stano's patch to keep downloaded rpms
[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         keeppackages(false),
37         type(repo::RepoType::NONE_e)
38     {}
39
40     ~Impl()
41     {
42       //MIL << std::endl;
43     }
44   public:
45     bool enabled;
46     bool autorefresh;
47     bool gpgcheck;
48     bool keeppackages;
49     Url gpgkey_url;
50     repo::RepoType type;
51     Url mirrorlist_url;
52     std::set<Url> baseUrls;
53     Pathname path;
54     std::string alias;
55     std::string escaped_alias;
56     std::string name;
57     Pathname filepath;
58     Pathname metadatapath;
59     Pathname packagespath;
60   public:
61
62   private:
63     friend Impl * rwcowClone<Impl>( const Impl * rhs );
64     /** clone for RWCOW_pointer */
65     Impl * clone() const
66     { return new Impl( *this ); }
67   };
68   ///////////////////////////////////////////////////////////////////
69
70   /** \relates RepoInfo::Impl Stream output */
71   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
72   {
73     return str << "RepoInfo::Impl";
74   }
75
76   ///////////////////////////////////////////////////////////////////
77   //
78   //    CLASS NAME : RepoInfo
79   //
80   ///////////////////////////////////////////////////////////////////
81
82   ///////////////////////////////////////////////////////////////////
83   //
84   //    METHOD NAME : RepoInfo::RepoInfo
85   //    METHOD TYPE : Ctor
86   //
87   RepoInfo::RepoInfo()
88   : _pimpl( new Impl() )
89   {}
90
91   ///////////////////////////////////////////////////////////////////
92   //
93   //    METHOD NAME : RepoInfo::~RepoInfo
94   //    METHOD TYPE : Dtor
95   //
96   RepoInfo::~RepoInfo()
97   {
98     //MIL << std::endl;
99   }
100
101   RepoInfo & RepoInfo::setEnabled( bool enabled )
102   {
103     _pimpl->enabled = enabled;
104     return *this;
105   }
106
107   RepoInfo & RepoInfo::setAutorefresh( bool autorefresh )
108   {
109     _pimpl->autorefresh = autorefresh;
110     return *this;
111   }
112
113   RepoInfo & RepoInfo::setGpgCheck( bool check )
114   {
115     _pimpl->gpgcheck = check;
116     return *this;
117   }
118
119   RepoInfo & RepoInfo::setMirrorListUrl( const Url &url )
120   {
121     _pimpl->mirrorlist_url = url;
122     return *this;
123   }
124
125   RepoInfo & RepoInfo::setGpgKeyUrl( const Url &url )
126   {
127     _pimpl->gpgkey_url = url;
128     return *this;
129   }
130
131   RepoInfo & RepoInfo::addBaseUrl( const Url &url )
132   {
133     _pimpl->baseUrls.insert(url);
134     return *this;
135   }
136
137   RepoInfo & RepoInfo::setBaseUrl( const Url &url )
138   {
139     _pimpl->baseUrls.clear();
140     addBaseUrl(url);
141     return *this;
142   }
143
144   RepoInfo & RepoInfo::setPath( const Pathname &path )
145   {
146     _pimpl->path = path;
147     return *this;
148   }
149
150   RepoInfo & RepoInfo::setAlias( const std::string &alias )
151   {
152     _pimpl->alias = alias;
153     // replace slashes with underscores
154     std::string fnd="/";
155     std::string rep="_";
156     std::string escaped_alias = alias;
157     size_t pos = escaped_alias.find(fnd);
158     while(pos!=string::npos)
159     {
160       escaped_alias.replace(pos,fnd.length(),rep);
161       pos = escaped_alias.find(fnd,pos+rep.length());
162     }
163     _pimpl->escaped_alias = escaped_alias;
164     return *this;
165   }
166
167   RepoInfo & RepoInfo::setType( const repo::RepoType &t )
168   {
169     _pimpl->type = t;
170     return *this;
171   }
172
173   RepoInfo & RepoInfo::setName( const std::string &name )
174   {
175     _pimpl->name = name;
176     return *this;
177   }
178
179   RepoInfo & RepoInfo::setFilepath( const Pathname &filepath )
180   {
181     _pimpl->filepath = filepath;
182     return *this;
183   }
184
185   RepoInfo & RepoInfo::setMetadataPath( const Pathname &path )
186   {
187     _pimpl->metadatapath = path;
188     return *this;
189   }
190
191   RepoInfo & RepoInfo::setPackagesPath( const Pathname &path )
192   {
193     _pimpl->packagespath = path;
194     return *this;
195   }
196
197   RepoInfo & RepoInfo::setKeepPackages( bool keep )
198   {
199     _pimpl->keeppackages = keep;
200     return *this;
201   }
202
203   bool RepoInfo::enabled() const
204   { return _pimpl->enabled; }
205
206   bool RepoInfo::autorefresh() const
207   { return _pimpl->autorefresh; }
208
209   bool RepoInfo::gpgCheck() const
210   { return _pimpl->gpgcheck; }
211
212   std::string RepoInfo::alias() const
213   { return _pimpl->alias; }
214
215   std::string RepoInfo::escaped_alias() const
216   { return _pimpl->escaped_alias; }
217
218   std::string RepoInfo::name() const
219   {
220     if ( _pimpl->name.empty() )
221     {
222       return alias();
223     }
224
225     repo::RepoVariablesStringReplacer replacer;
226     return replacer(_pimpl->name);
227   }
228
229   Pathname RepoInfo::filepath() const
230   { return _pimpl->filepath; }
231
232   Pathname RepoInfo::metadataPath() const
233   { return _pimpl->metadatapath; }
234
235   Pathname RepoInfo::packagesPath() const
236   { return _pimpl->packagespath; }
237
238   repo::RepoType RepoInfo::type() const
239   { return _pimpl->type; }
240
241   Url RepoInfo::mirrorListUrl() const
242   { return _pimpl->mirrorlist_url; }
243
244   Url RepoInfo::gpgKeyUrl() const
245   { return _pimpl->gpgkey_url; }
246
247   std::set<Url> RepoInfo::baseUrls() const
248   {
249     RepoInfo::url_set replaced_urls;
250     repo::RepoVariablesUrlReplacer replacer;
251     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
252           it != _pimpl->baseUrls.end();
253           ++it )
254     {
255       replaced_urls.insert(replacer(*it));
256     }
257     return replaced_urls;
258
259     return _pimpl->baseUrls;
260   }
261
262   Pathname RepoInfo::path() const
263   { return _pimpl->path; }
264
265   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
266   {
267     return make_transform_iterator( _pimpl->baseUrls.begin(),
268                                     repo::RepoVariablesUrlReplacer() );
269     //return _pimpl->baseUrls.begin();
270   }
271
272   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
273   {
274     //return _pimpl->baseUrls.end();
275     return make_transform_iterator( _pimpl->baseUrls.end(),
276                                     repo::RepoVariablesUrlReplacer() );
277   }
278
279   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
280   { return _pimpl->baseUrls.size(); }
281
282   bool RepoInfo::baseUrlsEmpty() const
283   { return _pimpl->baseUrls.empty(); }
284
285   bool RepoInfo::keepPackages() const
286   { return _pimpl->keeppackages; }
287
288   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
289   {
290     str << "--------------------------------------" << std::endl;
291     str << "- alias       : " << alias() << std::endl;
292     for ( urls_const_iterator it = baseUrlsBegin();
293           it != baseUrlsEnd();
294           ++it )
295     {
296       str << "- url         : " << *it << std::endl;
297     }
298     str << "- path        : " << path() << std::endl;
299     str << "- type        : " << type() << std::endl;
300     str << "- enabled     : " << enabled() << std::endl;
301
302     str << "- autorefresh : " << autorefresh() << std::endl;
303     str << "- gpgcheck : " << gpgCheck() << std::endl;
304     str << "- gpgkey : " << gpgKeyUrl() << std::endl;
305     str << "- keeppackages : " << keepPackages() << std::endl;
306
307     return str;
308   }
309
310   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
311   {
312     // we save the original data without variable replacement
313     str << "[" << alias() << "]" << endl;
314     str << "name=" << _pimpl->name << endl;
315
316     if ( ! _pimpl->baseUrls.empty() )
317       str << "baseurl=";
318     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
319           it != _pimpl->baseUrls.end();
320           ++it )
321     {
322       str << *it << endl;
323     }
324
325     if ( ! _pimpl->path.empty() )
326       str << "path="<< path() << endl;
327
328     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
329       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
330
331     str << "type=" << type().asString() << endl;
332     str << "enabled=" << (enabled() ? "1" : "0") << endl;
333     str << "autorefresh=" << (autorefresh() ? "1" : "0") << endl;
334     str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
335     if ( ! (gpgKeyUrl().asString().empty()) )
336       str << "gpgkey=" <<gpgKeyUrl() << endl;
337       
338     str << "keeppackages=" << keepPackages() << endl;
339
340     return str;
341   }
342
343   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
344   {
345     return obj.dumpOn(str);
346   }
347
348   /////////////////////////////////////////////////////////////////
349 } // namespace zypp
350 ///////////////////////////////////////////////////////////////////