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