55bfcb48bbf61f234812b022c943310176183c71
[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 #include "zypp/parser/xml/XmlEscape.h"
18
19 #include "zypp/RepoInfo.h"
20 #include "zypp/repo/RepoInfoBaseImpl.h"
21
22 using namespace std;
23 using zypp::xml::escape;
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   ///////////////////////////////////////////////////////////////////
30   //
31   //    CLASS NAME : RepoInfo::Impl
32   //
33   /** RepoInfo implementation. */
34   struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
35   {
36     Impl()
37       : repo::RepoInfoBase::Impl()
38       , gpgcheck(indeterminate)
39       , keeppackages(indeterminate)
40       , type(repo::RepoType::NONE_e)
41     {}
42
43     ~Impl()
44     {}
45
46   public:
47     static const unsigned defaultPriority = 99;
48
49     void setProbedType( const repo::RepoType & t ) const
50     {
51       if ( type == repo::RepoType::NONE
52            && t != repo::RepoType::NONE )
53       {
54         // lazy init!
55         const_cast<Impl*>(this)->type = t;
56       }
57     }
58
59   public:
60     TriBool gpgcheck;
61     TriBool keeppackages;
62     Url gpgkey_url;
63     repo::RepoType type;
64     Url mirrorlist_url;
65     std::set<Url> baseUrls;
66     Pathname path;
67     std::string service;
68     std::string targetDistro;
69     Pathname metadatapath;
70     Pathname packagespath;
71     DefaultIntegral<unsigned,defaultPriority> priority;
72   public:
73
74   private:
75     friend Impl * rwcowClone<Impl>( const Impl * rhs );
76     /** clone for RWCOW_pointer */
77     Impl * clone() const
78     { return new Impl( *this ); }
79   };
80   ///////////////////////////////////////////////////////////////////
81
82   /** \relates RepoInfo::Impl Stream output */
83   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
84   {
85     return str << "RepoInfo::Impl";
86   }
87
88   ///////////////////////////////////////////////////////////////////
89   //
90   //    CLASS NAME : RepoInfo
91   //
92   ///////////////////////////////////////////////////////////////////
93
94   ///////////////////////////////////////////////////////////////////
95   //
96   //    METHOD NAME : RepoInfo::RepoInfo
97   //    METHOD TYPE : Ctor
98   //
99   RepoInfo::RepoInfo()
100   : _pimpl( new Impl() )
101   {}
102
103   ///////////////////////////////////////////////////////////////////
104   //
105   //    METHOD NAME : RepoInfo::~RepoInfo
106   //    METHOD TYPE : Dtor
107   //
108   RepoInfo::~RepoInfo()
109   {
110     //MIL << std::endl;
111   }
112
113   unsigned RepoInfo::priority() const
114   { return _pimpl->priority; }
115   unsigned RepoInfo::defaultPriority()
116   { return Impl::defaultPriority; }
117   void RepoInfo::setPriority( unsigned newval_r )
118   {
119     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
120   }
121
122   void RepoInfo::setGpgCheck( bool check )
123   {
124     _pimpl->gpgcheck = check;
125   }
126
127   void RepoInfo::setMirrorListUrl( const Url &url )
128   {
129     _pimpl->mirrorlist_url = url;
130   }
131
132   void RepoInfo::setGpgKeyUrl( const Url &url )
133   {
134     _pimpl->gpgkey_url = url;
135   }
136
137   void RepoInfo::addBaseUrl( const Url &url )
138   {
139     _pimpl->baseUrls.insert(url);
140   }
141
142   void RepoInfo::setBaseUrl( const Url &url )
143   {
144     _pimpl->baseUrls.clear();
145     addBaseUrl(url);
146   }
147
148   void RepoInfo::setPath( const Pathname &path )
149   {
150     _pimpl->path = path;
151   }
152
153   void RepoInfo::setType( const repo::RepoType &t )
154   {
155     _pimpl->type = t;
156   }
157
158   void RepoInfo::setProbedType( const repo::RepoType &t ) const
159   { _pimpl->setProbedType( t ); }
160
161
162   void RepoInfo::setMetadataPath( const Pathname &path )
163   {
164     _pimpl->metadatapath = path;
165   }
166
167   void RepoInfo::setPackagesPath( const Pathname &path )
168   {
169     _pimpl->packagespath = path;
170   }
171
172   void RepoInfo::setKeepPackages( bool keep )
173   {
174     _pimpl->keeppackages = keep;
175   }
176
177   void RepoInfo::setService( const std::string& name )
178   {
179     _pimpl->service = name;
180   }
181   
182   void RepoInfo::setTargetDistribution(
183       const std::string & targetDistribution)
184   {
185     _pimpl->targetDistro = targetDistribution;
186   }
187
188   bool RepoInfo::gpgCheck() const
189   { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
190
191   Pathname RepoInfo::metadataPath() const
192   { return _pimpl->metadatapath; }
193
194   Pathname RepoInfo::packagesPath() const
195   { return _pimpl->packagespath; }
196
197   repo::RepoType RepoInfo::type() const
198   { return _pimpl->type; }
199
200   Url RepoInfo::mirrorListUrl() const
201   { return _pimpl->mirrorlist_url; }
202
203   Url RepoInfo::gpgKeyUrl() const
204   { return _pimpl->gpgkey_url; }
205
206   std::set<Url> RepoInfo::baseUrls() const
207   {
208     RepoInfo::url_set replaced_urls;
209     repo::RepoVariablesUrlReplacer replacer;
210     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
211           it != _pimpl->baseUrls.end();
212           ++it )
213     {
214       replaced_urls.insert(replacer(*it));
215     }
216     return replaced_urls;
217
218     return _pimpl->baseUrls;
219   }
220
221   Pathname RepoInfo::path() const
222   { return _pimpl->path; }
223
224   std::string RepoInfo::service() const
225   { return _pimpl->service; }
226   
227   std::string RepoInfo::targetDistribution() const
228   { return _pimpl->targetDistro; }
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     if (!targetDistribution().empty())
286       str << "- targetdistro: " << targetDistribution() << std::endl;
287
288     return str;
289   }
290
291   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
292   { return dumpAsIniOn(str); }
293
294   std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
295   {
296     RepoInfoBase::dumpAsIniOn(str);
297
298     if ( ! _pimpl->baseUrls.empty() )
299       str << "baseurl=";
300     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
301           it != _pimpl->baseUrls.end();
302           ++it )
303     {
304       str << *it << endl;
305     }
306
307     if ( ! _pimpl->path.empty() )
308       str << "path="<< path() << endl;
309
310     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
311       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
312
313     str << "type=" << type().asString() << endl;
314
315     if ( priority() != defaultPriority() )
316       str << "priority=" << priority() << endl;
317
318     if (!indeterminate(_pimpl->gpgcheck))
319       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
320     if ( ! (gpgKeyUrl().asString().empty()) )
321       str << "gpgkey=" <<gpgKeyUrl() << endl;
322
323     if (!indeterminate(_pimpl->keeppackages))
324       str << "keeppackages=" << keepPackages() << endl;
325
326     if( ! service().empty() )
327       str << "service=" << service() << endl;
328
329     return str;
330   }
331
332   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
333   {
334     string tmpstr;
335     str
336       << "<repo"
337       << " alias=\"" << escape(alias()) << "\""
338       << " name=\"" << escape(name()) << "\"";
339     if (type() != repo::RepoType::NONE)
340       str << " type=\"" << type().asString() << "\"";
341     str
342       << " enabled=\"" << enabled() << "\""
343       << " autorefresh=\"" << autorefresh() << "\""
344       << " gpgcheck=\"" << gpgCheck() << "\"";
345     if (!(tmpstr = gpgKeyUrl().asString()).empty())
346       str << " gpgkey=\"" << escape(tmpstr) << "\"";
347     if (!(tmpstr = mirrorListUrl().asString()).empty())
348       str << " mirrorlist=\"" << escape(tmpstr) << "\"";
349     str << ">" << endl;
350
351     for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
352          urlit != baseUrlsEnd(); ++urlit)
353       str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
354
355     str << "</repo>" << endl;
356     return str;
357   }
358
359   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
360   {
361     return obj.dumpOn(str);
362   }
363
364   /////////////////////////////////////////////////////////////////
365 } // namespace zypp
366 ///////////////////////////////////////////////////////////////////