Add asUserSting: human readable (translated) string representation
[platform/upstream/libzypp.git] / zypp / Repository.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Repository.h
10  *
11 */
12 #ifndef ZYPP_SAT_REPOSITORY_H
13 #define ZYPP_SAT_REPOSITORY_H
14
15 #include <iosfwd>
16 #include "zypp/Pathname.h"
17 #include "zypp/sat/detail/PoolMember.h"
18 #include "zypp/sat/LookupAttr.h"     // LookupAttrTools.h included at EOF
19 #include "zypp/sat/Solvable.h"
20 #include "zypp/RepoInfo.h"
21 #include "zypp/Date.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27     namespace detail
28     {
29       struct ByRepository;
30     }
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //  CLASS NAME : Repository
35     //
36     /** */
37     class Repository : protected sat::detail::PoolMember
38     {
39     public:
40         typedef filter_iterator<detail::ByRepository, sat::detail::SolvableIterator> SolvableIterator;
41         typedef sat::detail::size_type size_type;
42         typedef sat::detail::RepoIdType IdType;
43
44         typedef sat::ArrayAttr<std::string,std::string> Keywords;
45
46     public:
47         /** Default ctor creates \ref noRepository.*/
48         Repository()
49         : _id( sat::detail::noRepoId ) {}
50
51         /** \ref PoolImpl ctor. */
52         explicit Repository( IdType id_r )
53         : _id( id_r ) {}
54
55     public:
56         /** Represents no \ref Repository. */
57         static const Repository noRepository;
58
59         /** Evaluate \ref Repository in a boolean context (\c != \c noRepository). */
60         explicit operator bool() const
61         { return get() != nullptr; }
62
63         /** Reserved system repository alias \c @System. */
64         static const std::string & systemRepoAlias();
65
66         /** Return whether this is the system repository. */
67         bool isSystemRepo() const;
68
69     public:
70          /**
71           * Short unique string to identify a repo.
72           * ie: openSUSE-10.3
73           *
74           * If you are looking for a label to display
75           * see \ref name().
76           * ie: "openSUSE 10.3 Main repository"
77           *
78           */
79         std::string alias() const;
80
81         /** Label to display for this repo. */
82         std::string name() const;
83
84         /** User string */
85         std::string asUserString() const
86         { return name(); }
87
88         /**
89          * Timestamp when this repository was generated
90          *
91          * Usually this value is calculated as the newer
92          * timestamp from the timestamp of all the resources
93          * that conform the repository's metadata.
94          *
95          * For example in a rpm-md repository, it would be
96          * the resource specified in the xml file whith
97          * the newest timestamp attribute (which is the
98          * timestamp of the file in the server ).
99          *
100          * The timestamp is 0 if the repository does not
101          * specify when it was generated.
102          *
103          */
104         Date generatedTimestamp() const;
105
106         /**
107          * Suggested expiration timestamp.
108          *
109          * Repositories can define an amount of time
110          * they expire, with the generated timestamp as
111          * the base point of time.
112          *
113          * Note that is the responsability of the repository
114          * to freshen the generated timestamp to tell the
115          * client that the repo is alive and updating the
116          * metadata.
117          *
118          * The timestamp is 0 if the repository does not specify
119          * an expiration date.
120          *
121          */
122         Date suggestedExpirationTimestamp() const;
123
124         /**
125          * repository keywords (tags)
126          */
127         Keywords keywords() const;
128
129         /**
130          * The suggested expiration date of this repository
131          * already passed
132          *
133          * rpm-md repositories can provide this tag using the
134          * expire extension tag:
135          * \see http://en.opensuse.org/Standards/Rpm_Metadata#SUSE_repository_info_.28suseinfo.xml.29.2C_extensions_to_repomd.xml
136          */
137         bool maybeOutdated() const;
138
139         /**
140          * if the repository claims to update something then
141          * it is an update repository
142          *
143          * This is implemented by looking at the repository updates
144          * tag.
145          * \see http://en.opensuse.org/Standards/Rpm_Metadata#SUSE_repository_info_.28suseinfo.xml.29.2C_extensions_to_repomd.xml
146          */
147         bool isUpdateRepo() const;
148
149         /**
150          * whether the repository claims to update something \ref prod
151          * with key \ref cpeid
152          *
153          * \see zypp::Product::cpeId()
154          *
155          * See http://cpe.mitre.org/ for more information on the
156          * Common Platform Enumearation.
157          */
158         bool providesUpdatesFor( const std::string &cpeid ) const;
159
160         /** Whether \ref Repository contains solvables. */
161         bool solvablesEmpty() const;
162
163         /** Number of solvables in \ref Repository. */
164         size_type solvablesSize() const;
165
166         /** Iterator to the first \ref Solvable. */
167         SolvableIterator solvablesBegin() const;
168
169         /** Iterator behind the last \ref Solvable. */
170         SolvableIterator solvablesEnd() const;
171
172     public:
173
174       /** Query class for Repository */
175       class ProductInfoIterator;
176
177       /**
178        * Get an iterator to the beginning of the repository
179        * compatible distros.
180        * \note This is only a hint. There is no guarantee that
181        * the repository is built for that product.
182        * \see Repository::ProductInfoIterator
183        */
184       ProductInfoIterator compatibleWithProductBegin() const;
185
186       /**
187        * Get an iterator to the end of the repository
188        * compatible distros.
189        * \see Repository::ProductInfoIterator
190        */
191       ProductInfoIterator compatibleWithProductEnd() const;
192
193       /**
194        * Get an iterator to the beginning of the repository
195        * compatible distros.
196        * \see Repository::ProductInfoIterator
197        */
198       ProductInfoIterator updatesProductBegin() const;
199
200       /**
201        * Get an iterator to the end of the repository
202        * compatible distros.
203        * \see Repository::ProductInfoIterator
204        */
205       ProductInfoIterator updatesProductEnd() const;
206
207     public:
208         /** Return any associated \ref RepoInfo. */
209         RepoInfo info() const;
210
211         /** Set \ref RepoInfo for this repository.
212          * \throws Exception if this is \ref noRepository
213          * \throws Exception if the \ref RepoInfo::alias
214          *         does not match the \ref Repository::name.
215          */
216         void setInfo( const RepoInfo & info_r );
217
218         /** Remove any \ref RepoInfo set for this repository. */
219         void clearInfo();
220
221     public:
222         /** Remove this \ref Repository from it's \ref Pool. */
223         void eraseFromPool();
224
225         /** Functor calling \ref eraseFromPool. */
226         struct EraseFromPool;
227
228    public:
229         /** Return next Repository in \ref Pool (or \ref noRepository). */
230         Repository nextInPool() const;
231
232    public:
233         /** \name Repository content manipulating methods.
234          * \todo maybe a separate Repository/Solvable content manip interface
235          * provided by the pool.
236          */
237         //@{
238         /** Load \ref Solvables from a solv-file.
239          * In case of an exception the repository remains in the \ref Pool.
240          * \throws Exception if this is \ref noRepository
241          * \throws Exception if loading the solv-file fails.
242          * \see \ref Pool::addRepoSolv and \ref Repository::EraseFromPool
243          */
244         void addSolv( const Pathname & file_r );
245
246          /** Load \ref Solvables from a helix-file.
247          * Supports loading of gzip compressed files (.gz). In case of an exception
248          * the repository remains in the \ref Pool.
249          * \throws Exception if this is \ref noRepository
250          * \throws Exception if loading the helix-file fails.
251          * \see \ref Pool::addRepoHelix and \ref Repository::EraseFromPool
252          */
253         void addHelix( const Pathname & file_r );
254
255        /** Add \c count_r new empty \ref Solvable to this \ref Repository. */
256         sat::Solvable::IdType addSolvables( unsigned count_r );
257         /** \overload Add only one new \ref Solvable. */
258         sat::Solvable::IdType addSolvable()
259             { return addSolvables( 1 ); }
260         //@}
261
262     public:
263         /** Expert backdoor. */
264         ::_Repo * get() const;
265         /** Expert backdoor. */
266         IdType id() const { return _id; }
267         /** libsolv internal priorities.
268          * Unlike the \ref RepoInfo priority which tries to be YUM conform
269          * (H[1-99]L), this one is the solvers internal priority representation.
270          * It is type \c int and as one might expect it, the higher the value
271          * the higher the priority. Subpriority is currently used to express
272          * media preferences (\see \ref MediaPriority).
273          */
274         //@{
275         int satInternalPriority() const;
276         int satInternalSubPriority() const;
277         //@}
278
279     private:
280         IdType _id;
281     };
282     ///////////////////////////////////////////////////////////////////
283
284     /** \relates Repository Stream output */
285     std::ostream & operator<<( std::ostream & str, const Repository & obj );
286
287     /** \relates Repository */
288     inline bool operator==( const Repository & lhs, const Repository & rhs )
289     { return lhs.get() == rhs.get(); }
290
291     /** \relates Repository */
292     inline bool operator!=( const Repository & lhs, const Repository & rhs )
293     { return lhs.get() != rhs.get(); }
294
295     /** \relates Repository */
296     inline bool operator<( const Repository & lhs, const Repository & rhs )
297     { return lhs.get() < rhs.get(); }
298
299     ///////////////////////////////////////////////////////////////////
300     /**
301      * Query class for Repository related products
302      *
303      * The iterator does not provide a dereference
304      * operator so you can do * on it, but you can
305      * access the attributes of each related product
306      * directly from the iterator.
307      *
308      * \code
309      * for_( it, repo.compatibleWithProductBegin(), repo.compatibleWithProductEnd() )
310      * {
311      *   cout << it.cpeid() << endl;
312      * }
313      * \endcode
314      *
315      */
316     class Repository::ProductInfoIterator : public boost::iterator_adaptor<
317         Repository::ProductInfoIterator    // Derived
318         , sat::LookupAttr::iterator        // Base
319         , int                              // Value
320         , boost::forward_traversal_tag     // CategoryOrTraversal
321         , int                              // Reference
322     >
323     {
324       public:
325         ProductInfoIterator()
326         {}
327
328         /**
329          * Product label
330          */
331         std::string label() const;
332
333         /**
334          * The Common Platform Enumeration name
335          * for this product.
336          *
337          * See http://cpe.mitre.org
338          */
339         std::string cpeId() const;
340
341       private:
342         friend class Repository;
343         /** Hide ctor as just a limited set of attributes is valid. */
344         explicit ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r );
345
346       private:
347         friend class boost::iterator_core_access;
348         int dereference() const { return 0; }
349     };
350     ///////////////////////////////////////////////////////////////////
351
352     ///////////////////////////////////////////////////////////////////
353     //
354     //  CLASS NAME : Repository::EraseFromPool
355     //
356     /** Functor removing \ref Repository from it's \ref Pool.
357      *
358      * E.g. used as dispose function in. \ref AutoDispose
359      * to provide a convenient and exception safe temporary
360      * \ref Repository.
361      * \code
362      *  sat::Pool satpool;
363      *  MIL << "1 " << satpool << endl;
364      *  {
365      *    AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
366      *    *tmprepo = satpool.reposInsert( "A" );
367      *    tmprepo->addSolv( "sl10.1-beta7-packages.solv" );
368      *    DBG << "2 " << satpool << endl;
369      *    // Calling 'tmprepo.resetDispose();' here
370      *    // would keep the Repo.
371      *  }
372      *  MIL << "3 " << satpool << endl;
373      * \endcode
374      * \code
375      * 1 sat::pool(){0repos|2slov}
376      * 2 sat::pool(){1repos|2612slov}
377      * 3 sat::pool(){0repos|2slov}
378      * \endcode
379      * Leaving the block without calling <tt>tmprepo.resetDispose();</tt>
380      * before, will automatically remove the \ref Repo from it's \ref Pool.
381      */
382     struct Repository::EraseFromPool
383     {
384         void operator()( Repository repository_r ) const
385             { repository_r.eraseFromPool(); }
386     };
387     ///////////////////////////////////////////////////////////////////
388
389     ///////////////////////////////////////////////////////////////////
390     namespace detail
391     { /////////////////////////////////////////////////////////////////
392       ///////////////////////////////////////////////////////////////////
393       //
394       //        CLASS NAME : RepositoryIterator
395       //
396       /** */
397       class RepositoryIterator : public boost::iterator_adaptor<
398             RepositoryIterator                            // Derived
399                            , ::_Repo **                   // Base
400                            , Repository                   // Value
401                            , boost::forward_traversal_tag // CategoryOrTraversal
402                            , Repository                   // Reference
403                              >
404       {
405         public:
406           RepositoryIterator()
407           : RepositoryIterator::iterator_adaptor_( 0 )
408           {}
409
410           explicit RepositoryIterator( ::_Repo ** p )
411           : RepositoryIterator::iterator_adaptor_( p )
412           {}
413
414         private:
415           friend class boost::iterator_core_access;
416
417           Repository dereference() const
418           { return Repository( *base() ); }
419
420           void increment();
421       };
422       ///////////////////////////////////////////////////////////////////
423       ///////////////////////////////////////////////////////////////////
424       //
425       //        CLASS NAME : ByRepository
426       //
427       /** Functor filtering \ref Solvable by \ref Repository.*/
428       struct ByRepository
429       {
430         public:
431           ByRepository( const Repository & repository_r ) : _repository( repository_r ) {}
432           ByRepository( sat::detail::RepoIdType id_r ) : _repository( id_r ) {}
433           ByRepository() {}
434
435           bool operator()( const sat::Solvable & slv_r ) const
436           { return slv_r.repository() == _repository; }
437
438         private:
439           Repository _repository;
440       };
441       ///////////////////////////////////////////////////////////////////
442       /////////////////////////////////////////////////////////////////
443     } // namespace detail
444     ///////////////////////////////////////////////////////////////////
445   /////////////////////////////////////////////////////////////////
446 } // namespace zypp
447 ///////////////////////////////////////////////////////////////////
448
449 // Late include as sat::ArrayAttr requires Repository.h
450 #include "zypp/sat/LookupAttrTools.h"
451
452 #endif // ZYPP_SAT_REPOSITORY_H