Service handling fixes. Added ServiceException.
[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   const RepoInfo RepoInfo::noRepo;
95
96   ///////////////////////////////////////////////////////////////////
97   //
98   //    METHOD NAME : RepoInfo::RepoInfo
99   //    METHOD TYPE : Ctor
100   //
101   RepoInfo::RepoInfo()
102   : _pimpl( new Impl() )
103   {}
104
105   ///////////////////////////////////////////////////////////////////
106   //
107   //    METHOD NAME : RepoInfo::~RepoInfo
108   //    METHOD TYPE : Dtor
109   //
110   RepoInfo::~RepoInfo()
111   {
112     //MIL << std::endl;
113   }
114
115   unsigned RepoInfo::priority() const
116   { return _pimpl->priority; }
117   unsigned RepoInfo::defaultPriority()
118   { return Impl::defaultPriority; }
119   void RepoInfo::setPriority( unsigned newval_r )
120   {
121     _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
122   }
123
124   void RepoInfo::setGpgCheck( bool check )
125   {
126     _pimpl->gpgcheck = check;
127   }
128
129   void RepoInfo::setMirrorListUrl( const Url &url )
130   {
131     _pimpl->mirrorlist_url = url;
132   }
133
134   void RepoInfo::setGpgKeyUrl( const Url &url )
135   {
136     _pimpl->gpgkey_url = url;
137   }
138
139   void RepoInfo::addBaseUrl( const Url &url )
140   {
141     _pimpl->baseUrls.insert(url);
142   }
143
144   void RepoInfo::setBaseUrl( const Url &url )
145   {
146     _pimpl->baseUrls.clear();
147     addBaseUrl(url);
148   }
149
150   void RepoInfo::setPath( const Pathname &path )
151   {
152     _pimpl->path = path;
153   }
154
155   void RepoInfo::setType( const repo::RepoType &t )
156   {
157     _pimpl->type = t;
158   }
159
160   void RepoInfo::setProbedType( const repo::RepoType &t ) const
161   { _pimpl->setProbedType( t ); }
162
163
164   void RepoInfo::setMetadataPath( const Pathname &path )
165   {
166     _pimpl->metadatapath = path;
167   }
168
169   void RepoInfo::setPackagesPath( const Pathname &path )
170   {
171     _pimpl->packagespath = path;
172   }
173
174   void RepoInfo::setKeepPackages( bool keep )
175   {
176     _pimpl->keeppackages = keep;
177   }
178
179   void RepoInfo::setService( const std::string& name )
180   {
181     _pimpl->service = name;
182   }
183
184   void RepoInfo::setTargetDistribution(
185       const std::string & targetDistribution)
186   {
187     _pimpl->targetDistro = targetDistribution;
188   }
189
190   bool RepoInfo::gpgCheck() const
191   { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
192
193   Pathname RepoInfo::metadataPath() const
194   { return _pimpl->metadatapath; }
195
196   Pathname RepoInfo::packagesPath() const
197   { return _pimpl->packagespath; }
198
199   repo::RepoType RepoInfo::type() const
200   { return _pimpl->type; }
201
202   Url RepoInfo::mirrorListUrl() const
203   { return _pimpl->mirrorlist_url; }
204
205   Url RepoInfo::gpgKeyUrl() const
206   { return _pimpl->gpgkey_url; }
207
208   std::set<Url> RepoInfo::baseUrls() const
209   {
210     RepoInfo::url_set replaced_urls;
211     repo::RepoVariablesUrlReplacer replacer;
212     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
213           it != _pimpl->baseUrls.end();
214           ++it )
215     {
216       replaced_urls.insert(replacer(*it));
217     }
218     return replaced_urls;
219
220     return _pimpl->baseUrls;
221   }
222
223   Pathname RepoInfo::path() const
224   { return _pimpl->path; }
225
226   std::string RepoInfo::service() const
227   { return _pimpl->service; }
228
229   std::string RepoInfo::targetDistribution() const
230   { return _pimpl->targetDistro; }
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   // false by default (if not set by setKeepPackages)
253   bool RepoInfo::keepPackages() const
254   {
255     if (indeterminate(_pimpl->keeppackages))
256     {
257       if (_pimpl->baseUrls.empty())
258         return false;
259       else if ( media::MediaAccess::downloads( *baseUrlsBegin() ) )
260         return true;
261       else
262         return false;
263     }
264
265     return (bool) _pimpl->keeppackages;
266   }
267
268
269   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
270   {
271     RepoInfoBase::dumpOn(str);
272     for ( urls_const_iterator it = baseUrlsBegin();
273           it != baseUrlsEnd();
274           ++it )
275     {
276       str << "- url         : " << *it << std::endl;
277     }
278     str << "- path        : " << path() << std::endl;
279     str << "- type        : " << type() << std::endl;
280     str << "- priority    : " << priority() << std::endl;
281
282     str << "- gpgcheck    : " << gpgCheck() << std::endl;
283     str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
284     str << "- keeppackages: " << keepPackages() << std::endl;
285     str << "- service     : " << service() << std::endl;
286
287     if (!targetDistribution().empty())
288       str << "- targetdistro: " << targetDistribution() << std::endl;
289
290     return str;
291   }
292
293   std::ostream & RepoInfo::dumpRepoOn( std::ostream & str ) const
294   { return dumpAsIniOn(str); }
295
296   std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
297   {
298     RepoInfoBase::dumpAsIniOn(str);
299
300     if ( ! _pimpl->baseUrls.empty() )
301       str << "baseurl=";
302     for ( url_set::const_iterator it = _pimpl->baseUrls.begin();
303           it != _pimpl->baseUrls.end();
304           ++it )
305     {
306       str << *it << endl;
307     }
308
309     if ( ! _pimpl->path.empty() )
310       str << "path="<< path() << endl;
311
312     if ( ! (_pimpl->mirrorlist_url.asString().empty()) )
313       str << "mirrorlist=" << _pimpl->mirrorlist_url << endl;
314
315     str << "type=" << type().asString() << endl;
316
317     if ( priority() != defaultPriority() )
318       str << "priority=" << priority() << endl;
319
320     if (!indeterminate(_pimpl->gpgcheck))
321       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
322     if ( ! (gpgKeyUrl().asString().empty()) )
323       str << "gpgkey=" <<gpgKeyUrl() << endl;
324
325     if (!indeterminate(_pimpl->keeppackages))
326       str << "keeppackages=" << keepPackages() << endl;
327
328     if( ! service().empty() )
329       str << "service=" << service() << endl;
330
331     return str;
332   }
333
334   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
335   { return dumpAsXMLOn(str, ""); }
336
337   std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
338   {
339     string tmpstr;
340     str
341       << "<repo"
342       << " alias=\"" << escape(alias()) << "\""
343       << " name=\"" << escape(name()) << "\"";
344     if (type() != repo::RepoType::NONE)
345       str << " type=\"" << type().asString() << "\"";
346     str
347       << " enabled=\"" << enabled() << "\""
348       << " autorefresh=\"" << autorefresh() << "\""
349       << " gpgcheck=\"" << gpgCheck() << "\"";
350     if (!(tmpstr = gpgKeyUrl().asString()).empty())
351       str << " gpgkey=\"" << escape(tmpstr) << "\"";
352     if (!(tmpstr = mirrorListUrl().asString()).empty())
353       str << " mirrorlist=\"" << escape(tmpstr) << "\"";
354     str << ">" << endl;
355
356     for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
357          urlit != baseUrlsEnd(); ++urlit)
358       str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
359
360     str << "</repo>" << endl;
361     return str;
362   }
363
364
365   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
366   {
367     return obj.dumpOn(str);
368   }
369
370
371   /////////////////////////////////////////////////////////////////
372 } // namespace zypp
373 ///////////////////////////////////////////////////////////////////