merge REFACTORING-10_3 back to trunk
[platform/upstream/libzypp.git] / zypp / RepoManager.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoManager.h
10  *
11 */
12 #ifndef ZYPP_REPOMANAGER_H
13 #define ZYPP_REPOMANAGER_H
14
15 #include <iosfwd>
16 #include <list>
17
18 #include "zypp/base/PtrTypes.h"
19 //#include "zypp/base/ReferenceCounted.h"
20 //#include "zypp/base/NonCopyable.h"
21 #include "zypp/Pathname.h"
22 #include "zypp/ZConfig.h"
23 #include "zypp/Repository.h"
24 #include "zypp/RepoInfo.h"
25 #include "zypp/repo/RepoException.h"
26 #include "zypp/repo/RepoType.h"
27 #include "zypp/RepoStatus.h"
28 #include "zypp/ProgressData.h"
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34    /**
35     * Parses \a repo_file and returns a list of \ref RepoInfo objects
36     * corresponding to repositories found within the file.
37     * 
38     * \param repo_file Valid URL of the repo file.
39     * \return found list<RepoInfo>
40     *
41     * \throws MediaException If the access to the url fails
42     * \throws ParseException If the file parsing fails
43     * \throws Exception On other errors.
44     */
45    std::list<RepoInfo> readRepoFile(const Url & repo_file);
46
47   /**
48    * Repo manager settings.
49    * Settings default to ZYpp global settings.
50    */
51   struct RepoManagerOptions
52   {
53     RepoManagerOptions();
54     
55     Pathname repoCachePath;
56     Pathname repoRawCachePath;
57     Pathname knownReposPath;
58   };
59
60   /**
61    * \short creates and provides information about known sources.
62    *
63    */
64   class RepoManager
65   {
66     friend std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
67
68   public:
69     /** Implementation  */
70     class Impl;
71
72   public:
73    RepoManager( const RepoManagerOptions &options = RepoManagerOptions() );
74    /** Dtor */
75     ~RepoManager();
76     
77     enum RawMetadataRefreshPolicy
78     {
79       RefreshIfNeeded,
80       RefreshForced
81     };
82     
83     enum CacheBuildPolicy
84     {
85       BuildIfNeeded,
86       BuildForced
87     };
88     
89     enum RepoRemovePolicy
90     {
91       
92     };
93     
94    /**
95     * \short List known repositories.
96     *
97     * The known repositories are read from
98     * \ref RepoManagerOptions::knownReposPath passed on the Ctor.
99     * Which defaults to ZYpp global settings.
100     * \return found list<RepoInfo>
101     */
102    std::list<RepoInfo> knownRepositories() const;
103
104    /**
105     * \short Refresh local raw cache
106     *
107     * Will try to download the metadata
108     *
109     * In case of falure the metadata remains
110     * as it was before.
111     *
112     * \throws repo::RepoNoUrlException if no urls are available.
113     * \throws repo::RepoNoAliasException if can't figure an alias
114     * \throws repo::RepoUnknownTypeException if the metadata is unknown
115     * \throws Exception on unknown error.
116     */
117    void refreshMetadata( const RepoInfo &info,
118                          RawMetadataRefreshPolicy policy = RefreshIfNeeded,
119                          const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
120    
121    /**
122     * \short Clean local metadata
123     *
124     * Empty local metadata.
125     *
126     * \throws repo::RepoNoAliasException if can't figure an alias
127     * \throws Exception on unknown error.
128     */
129    void cleanMetadata( const RepoInfo &info,
130                        const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
131    
132    /**
133     * \short Refresh local cache
134     *
135     * Will try to build the cache from
136     * local metadata.
137     *
138     * If the cache exists it will be overwriten.
139     *
140     * \note the local metadata must be valid.
141     *
142     * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
143     * \throws Exception on unknown error.
144     */
145    void buildCache( const RepoInfo &info,
146                     CacheBuildPolicy policy = BuildIfNeeded,
147                     const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
148    
149    /**
150     * \short clean local cache
151     *
152     * Clean the cached version of the metadata
153     *
154     * \note the local metadata must be valid.
155     *
156     * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
157     * \throws Exception on unknown error.
158     */
159    void cleanCache( const RepoInfo &info,
160                     const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
161    
162    /**
163     * \short Whether a repository exists in cache
164     *
165     * \param RepoInfo to be checked.
166     */
167     bool isCached( const RepoInfo &info ) const;
168    
169    /**
170     * \short Create a repository object from the cache data
171     *
172     * Creating from cache requires that the repository is
173     * refreshed (metadata downloaded) and cached
174     *
175     * \throws repo::RepoNoAliasException if can't figure an alias to look in cache
176     * \throw RepoNotCachedException When the source is not cached.
177     */
178    Repository createFromCache( const RepoInfo &info,
179                                const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
180
181    /**
182     * \short Create a repository object from raw metadata
183     *
184     * Creating from cache requires that the repository is
185     * refreshed (metadata downloaded)
186     *
187     * \throw Exception If there are errors parsing the
188     * raw metadata
189     */
190    Repository createFromMetadata( const RepoInfo &info,
191                                   const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
192
193    /**
194     * \short Probe repo metadata type.
195     *
196     * \todo FIXME Should this be private?
197     */
198    repo::RepoType probe( const Url &url );
199    
200    
201    /**
202     * \short Adds a repository to the list of known repositories.
203     *
204     * 
205     *
206     * \throws repo::RepoAlreadyExistsException If the repo clash some 
207     * unique attribute like alias
208     *
209     * \throws RepoAlreadyExistsException
210     */
211    void addRepository( const RepoInfo &info,
212                        const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
213    
214    /**
215     * \short Adds repositores from a repo file to the list of known repositories.
216     * \param url Url of the repo file
217     * 
218     * \throws repo::RepoAlreadyExistsException If the repo clash some 
219     * unique attribute like alias
220     *
221     * \throws RepoAlreadyExistsException
222     * \throws MediaException If the access to the url fails
223     * \throws ParseException If the file parsing fails
224     * \throws Exception On other errors.
225     */
226     void addRepositories( const Url &url,
227                          const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
228     /**
229      * \short Remove the best matching repository from known repos list
230      *
231      * \throws RepoNotFoundException If no repo match
232      */
233     void removeRepository( const RepoInfo & info,
234                            const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
235     
236     /**
237      * \short Modify repository attributes
238      *
239      * \throws RepoNotFoundException If no repo match
240      * \throws ParseException If the file parsing fails
241      * \throws Exception On other errors.
242      */
243     void modifyRepository( const std::string &alias,
244                            const RepoInfo & newinfo,
245                            const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
246
247     /**
248      * \short Find a matching repository info
249      *
250      * \note if multple repositories incorrectly share the
251      * same alias, the first one found will be returned.
252      *
253      * \throws RepoNotFoundException If no repo match
254      * \throws ParseException If the file parsing fails
255      * \throws Exception On other errors.
256      */
257     RepoInfo getRepositoryInfo( const std::string &alias,
258                                 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() );
259     
260   protected:
261     RepoStatus rawMetadataStatus( const RepoInfo &info );
262     RepoStatus cacheStatus( const RepoInfo &info );
263   public:
264
265   private:
266     /** Pointer to implementation */
267     RWCOW_pointer<Impl> _pimpl;
268   };
269   ///////////////////////////////////////////////////////////////////
270
271   /** \relates RepoManager Stream output */
272   std::ostream & operator<<( std::ostream & str, const RepoManager & obj );
273
274   /////////////////////////////////////////////////////////////////
275 } // namespace zypp
276 ///////////////////////////////////////////////////////////////////
277 #endif // ZYPP2_REPOMANAGER_H