1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/SourceManager.h
12 #ifndef ZYPP_SOURCEMANAGER_H
13 #define ZYPP_SOURCEMANAGER_H
19 #include "zypp/base/ReferenceCounted.h"
20 #include "zypp/base/NonCopyable.h"
21 //#include "zypp/base/Iterator.h"
23 #include "zypp/base/Gettext.h" // move with FailedSourcesRestoreException & SourcesAlreadyRestoredException
25 #include "zypp/source/SourceInfo.h"
27 #include "zypp/Source.h"
29 #include "zypp/Pathname.h"
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
35 DEFINE_PTR_TYPE(SourceManager)
37 /** \todo move to separate header file.*/
38 class FailedSourcesRestoreException : public Exception
41 FailedSourcesRestoreException()
42 : Exception(N_("Unable to restore all sources."))
44 , _translatedSummary()
47 virtual ~FailedSourcesRestoreException() throw() {};
49 void append( std::string source, std::string alias, const Exception& problem );
52 std::set<std::string> aliases() const;
55 virtual std::ostream & dumpOn( std::ostream & str ) const;
56 virtual std::ostream & dumpOnTranslated( std::ostream & str ) const;
59 std::string _translatedSummary;
60 std::set<std::string> _aliases;
63 /** \todo move to separate header file.*/
64 class SourcesAlreadyRestoredException : public Exception
67 SourcesAlreadyRestoredException()
68 : Exception(N_("At least one source already registered, stored sources cannot be restored."))
70 virtual ~SourcesAlreadyRestoredException() throw() {};
73 ///////////////////////////////////////////////////////////////////
75 // CLASS NAME : SourceManager
77 /** Provide the known Sources.
78 * \todo make it a resl singleton
79 * \todo throwing findSource is not acceptable, return either
80 * a Source or noSource.
81 * \todo Make restore either void or nonthrowing, but two ways of
82 * error reporting is bad.
84 class SourceManager : public base::ReferenceCounted, private base::NonCopyable
86 friend std::ostream & operator<<( std::ostream & str, const SourceManager & obj );
89 /** Singleton access */
90 static SourceManager_Ptr sourceManager();
97 /** Runtime unique numeric Source Id. */
98 typedef Source_Ref::NumericId SourceId;
101 /** exposition only */
102 typedef std::map<SourceId, Source_Ref> SourceMap;
104 /** \name Iterate over all (SourceId,Source_Ref) pairs. */
106 typedef SourceMap::const_iterator const_iterator;
108 const_iterator begin() const;
110 const_iterator end() const;
114 /** \name Iterate over all known SourceIds. */
116 typedef MapKVIteratorTraits<SourceMap>::Key_const_iterator SourceId_const_iterator;
118 SourceId_const_iterator SourceId_begin() const;
120 SourceId_const_iterator SourceId_end() const;
124 /** \name Iterate over all known Sources. */
126 typedef MapKVIteratorTraits<SourceMap>::Value_const_iterator Source_const_iterator;
128 Source_const_iterator Source_begin() const;
130 Source_const_iterator Source_end() const;
136 * Reset the manager - discard the sources database,
137 * do not store the changes to the persistent store.
144 * List the known aliases and urls ( no need to restore first )
148 source::SourceInfoList knownSourceInfos(const Pathname &root_r);
151 * Store the current state to the given path
153 * \param root_r root path for storing the source definitions
154 * \param metadata_cache if true, this will also store/update
155 * metadata caches for the sources.
159 void store(Pathname root_r, bool metadata_cache );
162 * Restore the sources state to the given path. If the sources
163 * database is not empty, it throws an exception
165 * \param use_caches if true, source creation will try to use source cache
166 * and it's behavior on autorefresh. If false, it will not use
168 * \param alias_filter if non-empty, restore only a source matching
170 * \param url_filter if non-empty, restore only a source matching
173 * The alias_filter take precedence over the url_filter.
175 * \return true on success
179 bool restore(Pathname root_r, bool use_caches = true, const std::string &alias_filter = "", const std::string &url_filter = "" );
182 * Find a source with a specified ID
186 Source_Ref findSource(SourceId id);
189 * Find a source with a specified alias
193 Source_Ref findSource(const std::string & alias_r);
196 * Find a source with a specified URL.
197 * URLs are unique in zenworks but NOT in zypp.
198 * A bug in SL 10.1 causes alias mismatches so we have to resort to URLs.
203 Source_Ref findSourceByUrl(const Url & url_r);
206 * Return the list of the currently enabled sources
209 std::list<SourceId> enabledSources() const;
212 * Return ids of all sources
214 std::list<SourceId> allSources() const;
216 /** Add a new source.
217 * An attempt to add Source_Ref::noSource does nothing but
218 * returning Source_Ref::noSource.numericId(). Thus it
219 * results in adding no Source.
221 SourceId addSource(Source_Ref source_r);
223 /** Rename an existing source by Alias. */
224 void renameSource( SourceId id, const std::string & new_alias_r );
226 /** Remove an existing source by ID. */
227 void removeSource(SourceId id);
229 /** Remove an existing source by Alias. */
230 void removeSource(const std::string & alias_r);
232 /** Remove an existing source by Url. */
233 void removeSourceByUrl(const Url & url_r);
236 * Release all medias held by all sources
240 void releaseAllSources();
243 * Reattach all sources which are not mounted, but downloaded,
244 * to different directory
248 void reattachSources(const Pathname & attach_point);
251 * Disable all registered sources
253 void disableAllSources();
256 * Helper function to disable all sources in the persistent
257 * store on the given location. Does not manipulate with
258 * the current status of the source manager.
260 * \throws Exception ...
262 static void disableSourcesAt( const Pathname & root );
265 /** Singleton ctor */
268 ///////////////////////////////////////////////////////////////////
270 /** \relates SourceManager Stream output */
271 std::ostream & operator<<( std::ostream & str, const SourceManager & obj );
273 /////////////////////////////////////////////////////////////////
275 ///////////////////////////////////////////////////////////////////
276 #endif // ZYPP_SOURCEMANAGER_H