854108f30c22bea7d02e36e91d901a1ff21f5061
[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 #include "zypp/base/DefaultIntegral.h"
16 #include "zypp/media/MediaAccess.h"
17
18 #include "zypp/RepoInfo.h"
19 #include "zypp/repo/RepoInfoBaseImpl.h"
20
21 using namespace std;
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   //
29   //    CLASS NAME : RepoInfo::Impl
30   //
31   /** RepoInfo implementation. */
32   struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
33   {
34     Impl()
35       : repo::RepoInfoBase::Impl()
36       , gpgcheck(indeterminate)
37       , keeppackages(indeterminate)
38       , type(repo::RepoType::NONE_e)
39     {}
40
41     ~Impl()
42     {}
43
44   public:
45     static const unsigned defaultPriority = 99;
46
47     void setProbedType( const repo::RepoType & t ) const
48     {
49       if ( type == repo::RepoType::NONE
50            && t != repo::RepoType::NONE )
51       {
52         // lazy init!
53         const_cast<Impl*>(this)->type = t;
54       }
55     }
56
57   public:
58     TriBool gpgcheck;
59     TriBool keeppackages;
60     Url gpgkey_url;
61     repo::RepoType type;
62     Url mirrorlist_url;
63     std::set<Url> baseUrls;
64     Pathname path;
65     std::string service;
66     Pathname metadatapath;
67     Pathname packagespath;
68     DefaultIntegral<unsigned,defaultPriority> priority;
69   public:
70
71   private:
72     friend Impl * rwcowClone<Impl>( const Impl * rhs );
73     /** clone for RWCOW_pointer */
74     Impl * clone() const
75     { return new Impl( *this ); }
76   };
77   ///////////////////////////////////////////////////////////////////
78
79   /** \relates RepoInfo::Impl Stream output */
80   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
81   {
82     return str << "RepoInfo::Impl";
83   }
84
85   ///////////////////////////////////////////////////////////////////
86   //
87   //    CLASS NAME : RepoInfo
88   //
89   ///////////////////////////////////////////////////////////////////
90
91   ///////////////////////////////////////////////////////////////////
92   //
93   //    METHOD NAME : RepoInfo::RepoInfo
94   //    METHOD TYPE : Ctor
95   //
96   RepoInfo::RepoInfo()
97   : _pimpl( new Impl() )
98   {}
99
100   ///////////////////////////////////////////////////////////////////
101   //
102   //    METHOD NAME : RepoInfo::~RepoInfo
103   //    METHOD TYPE : Dtor
104   //
105   RepoInfo::~RepoInfo()
106   {
107     //MIL << std::endl;
108   }
109
110   unsigned RepoInfo::priority() const
111   { return _pimpl->priority; }
112   unsigned RepoInfo::defaultPriority()
113   { return Impl::defaultPriority; }
114   RepoInfo & RepoInfo::setPriority( unsigned newval_r )
115   {
116     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
117     return *this;
118   }
119
120   RepoInfo & RepoInfo::setGpgCheck( bool check )
121   {
122     _pimpl->gpgcheck = check;
123     return *this;
124   }
125
126   RepoInfo & RepoInfo::setMirrorListUrl( const Url &url )
127   {
128     _pimpl->mirrorlist_url = url;
129     return *this;
130   }
131
132   RepoInfo & RepoInfo::setGpgKeyUrl( const Url &url )
133   {
134     _pimpl->gpgkey_url = url;
135     return *this;
136   }
137
138   RepoInfo & RepoInfo::addBaseUrl( const Url &url )
139   {
140     _pimpl->baseUrls.insert(url);
141     return *this;
142   }
143
144   RepoInfo & RepoInfo::setBaseUrl( const Url &url )
145   {
146     _pimpl->baseUrls.clear();
147     addBaseUrl(url);
148     return *this;
149   }
150
151   RepoInfo & RepoInfo::setPath( const Pathname &path )
152   {
153     _pimpl->path = path;
154     return *this;
155   }
156
157   RepoInfo & RepoInfo::setType( const repo::RepoType &t )
158   {
159     _pimpl->type = t;
160     return *this;
161   }
162
163   void RepoInfo::setProbedType( const repo::RepoType &t ) const
164   { _pimpl->setProbedType( t ); }
165
166
167   RepoInfo & RepoInfo::setMetadataPath( const Pathname &path )
168   {
169     _pimpl->metadatapath = path;
170     return *this;
171   }
172
173   RepoInfo & RepoInfo::setPackagesPath( const Pathname &path )
174   {
175     _pimpl->packagespath = path;
176     return *this;
177   }
178
179   RepoInfo & RepoInfo::setKeepPackages( bool keep )
180   {
181     _pimpl->keeppackages = keep;
182     return *this;
183   }
184
185   RepoInfo & RepoInfo::setService( const std::string& name )
186   {
187     _pimpl->service = name;
188     return *this;
189   }
190
191   bool RepoInfo::gpgCheck() const
192   { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
193
194   Pathname RepoInfo::metadataPath() const
195   { return _pimpl->metadatapath; }
196
197   Pathname RepoInfo::packagesPath() const
198   { return _pimpl->packagespath; }
199
200   repo::RepoType RepoInfo::type() const
201   { return _pimpl->type; }
202
203   Url RepoInfo::mirrorListUrl() const
204   { return _pimpl->mirrorlist_url; }
205
206   Url RepoInfo::gpgKeyUrl() const
207   { return _pimpl->gpgkey_url; }
208
209   std::set<Url> RepoInfo::baseUrls() const
210   {
211     RepoInfo::url_set replaced_urls;
212     repo::RepoVariablesUrlReplacer replacer;
213     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
214           it != _pimpl->baseUrls.end();
215           ++it )
216     {
217       replaced_urls.insert(replacer(*it));
218     }
219     return replaced_urls;
220
221     return _pimpl->baseUrls;
222   }
223
224   Pathname RepoInfo::path() const
225   { return _pimpl->path; }
226
227   std::string RepoInfo::service() const
228   { return _pimpl->service; }
229
230   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
231   {
232     return make_transform_iterator( _pimpl->baseUrls.begin(),
233                                     repo::RepoVariablesUrlReplacer() );
234     //return _pimpl->baseUrls.begin();
235   }
236
237   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
238   {
239     //return _pimpl->baseUrls.end();
240     return make_transform_iterator( _pimpl->baseUrls.end(),
241                                     repo::RepoVariablesUrlReplacer() );
242   }
243
244   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
245   { return _pimpl->baseUrls.size(); }
246
247   bool RepoInfo::baseUrlsEmpty() const
248   { return _pimpl->baseUrls.empty(); }
249
250   // false by default (if not set by setKeepPackages)
251   bool RepoInfo::keepPackages() const
252   {
253     if (indeterminate(_pimpl->keeppackages))
254     {
255       if (_pimpl->baseUrls.empty())
256         return false;
257       else if ( media::MediaAccess::downloads( *baseUrlsBegin() ) )
258         return true;
259       else
260         return false;
261     }
262
263     return (bool) _pimpl->keeppackages;
264   }
265
266
267   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
268   {
269     RepoInfoBase::dumpOn(str);
270     for ( urls_const_iterator it = baseUrlsBegin();
271           it != baseUrlsEnd();
272           ++it )
273     {
274       str << "- url         : " << *it << std::endl;
275     }
276     str << "- path        : " << path() << std::endl;
277     str << "- type        : " << type() << std::endl;
278     str << "- priority    : " << priority() << std::endl;
279
280     str << "- gpgcheck    : " << gpgCheck() << std::endl;
281     str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
282     str << "- keeppackages: " << keepPackages() << std::endl;
283     str << "- service     : " << service() << std::endl;
284
285     return str;
286   }
287
288   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
289   {
290     RepoInfoBase::dumpAsIniOn(str);
291
292     if ( ! _pimpl->baseUrls.empty() )
293       str << "baseurl=";
294     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
295           it != _pimpl->baseUrls.end();
296           ++it )
297     {
298       str << *it << endl;
299     }
300
301     if ( ! _pimpl->path.empty() )
302       str << "path="<< path() << endl;
303
304     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
305       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
306
307     str << "type=" << type().asString() << endl;
308
309     if ( priority() != defaultPriority() )
310       str << "priority=" << priority() << endl;
311
312     if (!indeterminate(_pimpl->gpgcheck))
313       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
314     if ( ! (gpgKeyUrl().asString().empty()) )
315       str << "gpgkey=" <<gpgKeyUrl() << endl;
316
317     if (!indeterminate(_pimpl->keeppackages))
318       str << "keeppackages=" << keepPackages() << endl;
319
320     if( ! service().empty() )
321       str << "service=" << service() << endl;
322
323     return str;
324   }
325
326   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
327   {
328     return obj.dumpOn(str);
329   }
330
331   /////////////////////////////////////////////////////////////////
332 } // namespace zypp
333 ///////////////////////////////////////////////////////////////////