db9f92a607b2884253e20e73b915a38caabed80d
[platform/upstream/libzypp.git] / zypp / sat / Solvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Solvable.h
10  *
11 */
12 #ifndef ZYPP_SAT_SOLVABLE_H
13 #define ZYPP_SAT_SOLVABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/sat/detail/PoolMember.h"
18 #include "zypp/sat/SolvAttr.h"
19 #include "zypp/ResTraits.h"
20 #include "zypp/IdString.h"
21 #include "zypp/Edition.h"
22 #include "zypp/Arch.h"
23 #include "zypp/Dep.h"
24 #include "zypp/Capabilities.h"
25 #include "zypp/Capability.h"
26 #include "zypp/Locale.h"
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 {
31   class ByteCount;
32   class CheckSum;
33   class CpeId;
34   class Date;
35   class OnMediaLocation;
36   ///////////////////////////////////////////////////////////////////
37   namespace sat
38   {
39     ///////////////////////////////////////////////////////////////////
40     /// \class Solvable
41     /// \brief  A \ref Solvable object within the sat \ref Pool.
42     ///
43     /// \note Unfortunately libsolv combines the objects kind and
44     /// name in a single identifier \c "pattern:kde_multimedia",
45     /// \b except for packages and source packes. They are not prefixed
46     /// by any kind string. Instead the architecture is abused to store
47     /// \c "src" and \c "nosrc" values.
48     ///
49     /// \ref Solvable will hide this inconsistency by treating source
50     /// packages as an own kind of solvable and map their arch to
51     /// \ref Arch_noarch.
52     ///////////////////////////////////////////////////////////////////
53     class Solvable : protected detail::PoolMember
54     {
55     public:
56       typedef sat::detail::SolvableIdType IdType;
57
58     public:
59       /** Default ctor creates \ref noSolvable.*/
60       Solvable()
61       : _id( detail::noSolvableId )
62       {}
63
64       /** \ref PoolImpl ctor. */
65       explicit Solvable( IdType id_r )
66       : _id( id_r )
67       {}
68
69     public:
70       /** Represents no \ref Solvable. */
71       static const Solvable noSolvable;
72
73       /** Evaluate \ref Solvable in a boolean context (\c != \c noSolvable). */
74       explicit operator bool() const
75       { return get(); }
76
77     public:
78       /** The identifier.
79        * This is the solvables \ref name, \b except for packages and
80        * source packes, prefixed by it's \ref kind.
81        */
82       IdString ident()const;
83
84       /** The Solvables ResKind. */
85       ResKind kind()const;
86
87       /** Test whether a Solvable is of a certain \ref ResKind.
88        * The test is far cheaper than actually retrieving and
89        * comparing the \ref kind.
90        */
91       bool isKind( const ResKind & kind_r ) const;
92       /** \overload */
93       template<class _Res>
94       bool isKind() const
95       { return isKind( resKind<_Res>() ); }
96       /** \overload Extend the test to a range of \ref ResKind. */
97       template<class _Iterator>
98       bool isKind( _Iterator begin, _Iterator end ) const
99       { for_( it, begin, end ) if ( isKind( *it ) ) return true; return false; }
100
101       /** The name (without any ResKind prefix). */
102       std::string name() const;
103
104       /** The edition (version-release). */
105       Edition edition() const;
106
107       /** The architecture. */
108       Arch arch() const;
109
110       /** The vendor. */
111       IdString vendor() const;
112
113       /** The \ref Repository this \ref Solvable belongs to. */
114       Repository repository() const;
115       /** The repositories \ref RepoInfo. */
116       RepoInfo repoInfo() const;
117
118       /** Return whether this \ref Solvable belongs to the system repo.
119        * \note This includes the otherwise hidden systemSolvable.
120        */
121       bool isSystem() const;
122
123       /** Whether this is known to be installed on behalf of a user request.
124        * \note This is a hint guessed by evaluating an available install history.
125        * Returns \c false for non-system (uninstalled) solvables, or if no history
126        * is available.
127        */
128       bool onSystemByUser() const;
129
130       /** Whether different versions of this package can be installed at the same time.
131        * Per default \c false. \see also \ref ZConfig::multiversion.
132        */
133       bool multiversionInstall() const;
134
135       /** The items build time. */
136       Date buildtime() const;
137
138       /** The items install time (\c false if not installed). */
139       Date installtime() const;
140
141     public:
142       /** String representation <tt>"ident-edition.arch"</tt> or \c "noSolvable"
143        * \code
144        *   product:openSUSE-11.1.x86_64
145        *   autoyast2-2.16.19-0.1.src
146        *   noSolvable
147        * \endcode
148        */
149       std::string asString() const;
150
151       /** String representation <tt>"ident-edition.arch(repo)"</tt> or \c "noSolvable" */
152       std::string asUserString() const;
153
154       /** Test whether two Solvables have the same content.
155        * Basically the same name, edition, arch, vendor and buildtime.
156        */
157       bool identical( const Solvable & rhs ) const;
158
159       /** Test for same name-version-release.arch */
160       bool sameNVRA( const Solvable & rhs ) const
161       { return( get() == rhs.get() || ( ident() == rhs.ident() && edition() == rhs.edition() && arch() == rhs.arch() ) ); }
162
163     public:
164       /** \name Access to the \ref Solvable dependencies.
165        *
166        * \note Prerequires are a subset of requires.
167        */
168       //@{
169       Capabilities provides()    const;
170       Capabilities requires()    const;
171       Capabilities conflicts()   const;
172       Capabilities obsoletes()   const;
173       Capabilities recommends()  const;
174       Capabilities suggests()    const;
175       Capabilities enhances()    const;
176       Capabilities supplements() const;
177       Capabilities prerequires() const;
178
179       /** Return \ref Capabilities selected by \ref Dep constant. */
180       Capabilities dep( Dep which_r ) const
181       {
182         switch( which_r.inSwitch() )
183         {
184           case Dep::PROVIDES_e:    return provides();    break;
185           case Dep::REQUIRES_e:    return requires();    break;
186           case Dep::CONFLICTS_e:   return conflicts();   break;
187           case Dep::OBSOLETES_e:   return obsoletes();   break;
188           case Dep::RECOMMENDS_e:  return recommends();  break;
189           case Dep::SUGGESTS_e:    return suggests();    break;
190           case Dep::ENHANCES_e:    return enhances();    break;
191           case Dep::SUPPLEMENTS_e: return supplements(); break;
192           case Dep::PREREQUIRES_e: return prerequires(); break;
193         }
194         return Capabilities();
195       }
196       /** \overload operator[] */
197       Capabilities operator[]( Dep which_r ) const
198       { return dep( which_r ); }
199
200
201       /** Return the namespaced provides <tt>'namespace([value])[ op edition]'</tt> of this Solvable. */
202       CapabilitySet providesNamespace( const std::string & namespace_r ) const;
203
204       /** Return <tt>'value[ op edition]'</tt> for namespaced provides <tt>'namespace(value)[ op edition]'</tt>.
205        * Similar to \ref providesNamespace, but the namespace is stripped from the
206        * dependencies. This is convenient if the namespace denotes packages that
207        * should be looked up. E.g. the \c weakremover namespace used in a products
208        * release package denotes the packages that were dropped from the distribution.
209        * \see \ref Product::droplist
210        */
211       CapabilitySet valuesOfNamespace( const std::string & namespace_r ) const;
212       //@}
213
214     public:
215       /** \name Locale support. */
216       //@{
217       /** Whether this \c Solvable claims to support locales. */
218       bool supportsLocales() const;
219       /** Whether this \c Solvable supports a specific \ref Locale. */
220       bool supportsLocale( const Locale & locale_r ) const;
221       /** Whether this \c Solvable supports at least one of the specified locales. */
222       bool supportsLocale( const LocaleSet & locales_r ) const;
223       /** Whether this \c Solvable supports at least one requested locale.
224        * \see \ref Pool::setRequestedLocales
225        */
226       bool supportsRequestedLocales() const;
227       /** Return the supported locales. */
228       LocaleSet getSupportedLocales() const;
229       /** \overload Legacy return via arg \a locales_r */
230       void getSupportedLocales( LocaleSet & locales_r ) const
231       { locales_r = getSupportedLocales(); }
232       //@}
233
234     public:
235       /** The solvables CpeId if available. */
236       CpeId cpeId() const;
237
238       /** Media number the solvable is located on (\c 0 if no media access required). */
239       unsigned mediaNr() const;
240
241       /** Installed (unpacked) size.
242        * This is just a total number. Many objects provide even more detailed
243        * disk usage data. You can use \ref DiskUsageCounter to find out
244        * how objects data are distributed across partitions/directories.
245        * \code
246        *   // Load directory set into ducounter
247        *   DiskUsageCounter ducounter( { "/", "/usr", "/var" } );
248        *
249        *   // see how noch space the packages use
250        *   for ( const PoolItem & pi : pool )
251        *   {
252        *     cout << pi << ducounter.disk_usage( pi ) << endl;
253        *     // I__s_(7)GeoIP-1.4.8-3.1.2.x86_64(@System) {
254        *     // dir:[/] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.0 KiB)]
255        *     // dir:[/usr] [ bs: 0 B ts: 0 B us: 0 B (+-: 133.0 KiB)]
256        *     // dir:[/var] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.1 MiB)]
257        *     // }
258        *   }
259        * \endcode
260        * \see \ref DiskUsageCounter
261        */
262       ByteCount installSize() const;
263
264       /** Download size. */
265       ByteCount downloadSize() const;
266
267       /** The distribution string. */
268       std::string distribution() const;
269
270       /** Short (singleline) text describing the solvable (opt. translated). */
271       std::string summary( const Locale & lang_r = Locale() ) const;
272
273       /** Long (multiline) text describing the solvable (opt. translated). */
274       std::string description( const Locale & lang_r = Locale() ) const;
275
276       /** UI hint text when selecting the solvable for install (opt. translated). */
277       std::string insnotify( const Locale & lang_r = Locale() ) const;
278       /** UI hint text when selecting the solvable for uninstall (opt. translated).*/
279       std::string delnotify( const Locale & lang_r = Locale() ) const;
280
281       /** License or agreement to accept before installing the solvable (opt. translated). */
282       std::string licenseToConfirm( const Locale & lang_r = Locale() ) const;
283       /** \c True except for well known exceptions (i.e show license but no need to accept it). */
284       bool needToAcceptLicense() const;
285
286     public:
287       /** Helper that splits an identifier into kind and name or vice versa.
288        * \note In case \c name_r is preceded by a well known kind spec, the
289        * \c kind_r argument is ignored, and kind is derived from name.
290        * \see \ref ident
291        */
292       class SplitIdent
293       {
294       public:
295         SplitIdent() {}
296         SplitIdent( IdString ident_r );
297         SplitIdent( const char * ident_r );
298         SplitIdent( const std::string & ident_r );
299         SplitIdent( ResKind kind_r, IdString name_r );
300         SplitIdent( ResKind kind_r, const C_Str & name_r );
301
302         IdString ident() const { return _ident; }
303         ResKind  kind()  const { return _kind; }
304         IdString name()  const { return _name; }
305
306       private:
307         IdString  _ident;
308         ResKind   _kind;
309         IdString  _name;
310       };
311
312     public:
313       /** \name Attribute lookup.
314        * \see \ref LookupAttr and  \ref ArrayAttr providing a general, more
315        * query like interface for attribute retrieval.
316        */
317       //@{
318       /**
319        * returns the string attribute value for \ref attr
320        * or an empty string if it does not exists.
321        */
322       std::string lookupStrAttribute( const SolvAttr & attr ) const;
323       /** \overload Trying to look up a translated string attribute.
324        *
325        * Returns the translation for \c lang_r.
326        *
327        * Passing an empty \ref Locale will return the string for the
328        * current default locale (\see \ref ZConfig::TextLocale),
329        * \b considering all fallback locales.
330        *
331        * Returns an empty string if no translation is available.
332        */
333       std::string lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const;
334
335       /**
336        * returns the numeric attribute value for \ref attr
337        * or 0 if it does not exists.
338        */
339       unsigned long long lookupNumAttribute( const SolvAttr & attr ) const;
340
341       /**
342        * returns the boolean attribute value for \ref attr
343        * or \c false if it does not exists.
344        */
345       bool lookupBoolAttribute( const SolvAttr & attr ) const;
346
347       /**
348        * returns the id attribute value for \ref attr
349        * or \ref detail::noId if it does not exists.
350        */
351       detail::IdType lookupIdAttribute( const SolvAttr & attr ) const;
352
353       /**
354        * returns the CheckSum attribute value for \ref attr
355        * or an empty CheckSum if ir does not exist.
356        */
357       CheckSum lookupCheckSumAttribute( const SolvAttr & attr ) const;
358
359       /**
360        * returns OnMediaLocation data: This is everything we need to
361        * download e.g. an rpm (path, checksum, downloadsize, etc.).
362        */
363       OnMediaLocation lookupLocation() const;
364       //@}
365
366     public:
367       /** Return next Solvable in \ref Pool (or \ref noSolvable). */
368       Solvable nextInPool() const;
369       /** Return next Solvable in \ref Repo (or \ref noSolvable). */
370       Solvable nextInRepo() const;
371       /** Expert backdoor. */
372       ::_Solvable * get() const;
373       /** Expert backdoor. */
374       IdType id() const { return _id; }
375
376     private:
377       IdType _id;
378     };
379     ///////////////////////////////////////////////////////////////////
380
381     /** \relates Solvable Stream output */
382     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
383
384     /** \relates Solvable More verbose stream output including dependencies */
385     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
386
387     /** \relates Solvable XML output */
388     std::ostream & dumpAsXmlOn( std::ostream & str, const Solvable & obj );
389
390     /** \relates Solvable */
391     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
392     { return lhs.get() == rhs.get(); }
393
394     /** \relates Solvable */
395     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
396     { return lhs.get() != rhs.get(); }
397
398     /** \relates Solvable */
399     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
400     { return lhs.get() < rhs.get(); }
401
402     /** \relates Solvable Test whether a \ref Solvable is of a certain Kind. */
403     template<class _Res>
404     inline bool isKind( const Solvable & solvable_r )
405     { return solvable_r.isKind( ResTraits<_Res>::kind ); }
406
407     /** \relates Solvable Test for same content. */
408     inline bool identical( const Solvable & lhs, const Solvable & rhs )
409     { return lhs.identical( rhs ); }
410
411     /** \relates Solvable Test for same name version release and arch. */
412     inline bool sameNVRA( const Solvable & lhs, const Solvable & rhs )
413     { return lhs.sameNVRA( rhs ); }
414
415
416     /** \relates Solvable Compare according to \a kind and \a name. */
417     inline int compareByN( const Solvable & lhs, const Solvable & rhs )
418     {
419       int res = 0;
420       if ( lhs != rhs )
421       {
422         if ( (res = lhs.kind().compare( rhs.kind() )) == 0 )
423           res = lhs.name().compare( rhs.name() );
424       }
425       return res;
426     }
427
428     /** \relates Solvable Compare according to \a kind, \a name and \a edition. */
429     inline int compareByNVR( const Solvable & lhs, const Solvable & rhs )
430     {
431       int res = compareByN( lhs, rhs );
432       if ( res == 0 )
433         res = lhs.edition().compare( rhs.edition() );
434       return res;
435     }
436
437     /** \relates Solvable Compare according to \a kind, \a name, \a edition and \a arch. */
438     inline int compareByNVRA( const Solvable & lhs, const Solvable & rhs )
439     {
440       int res = compareByNVR( lhs, rhs );
441       if ( res == 0 )
442         res = lhs.arch().compare( rhs.arch() );
443       return res;
444     }
445
446     ///////////////////////////////////////////////////////////////////
447     namespace detail
448     {
449       ///////////////////////////////////////////////////////////////////
450       //
451       //        CLASS NAME : SolvableIterator
452       //
453       /** */
454       class SolvableIterator : public boost::iterator_adaptor<
455           SolvableIterator                   // Derived
456           , ::_Solvable*                     // Base
457           , const Solvable                   // Value
458           , boost::forward_traversal_tag     // CategoryOrTraversal
459           , const Solvable                   // Reference
460           >
461       {
462         public:
463           SolvableIterator()
464           : SolvableIterator::iterator_adaptor_( 0 )
465           {}
466
467           explicit SolvableIterator( const Solvable & val_r )
468           : SolvableIterator::iterator_adaptor_( 0 )
469           { assignVal( val_r ); }
470
471           explicit SolvableIterator( SolvableIdType id_r )
472           : SolvableIterator::iterator_adaptor_( 0 )
473           { assignVal( Solvable( id_r ) ); }
474
475         private:
476           friend class boost::iterator_core_access;
477
478           Solvable dereference() const
479           { return _val; }
480
481           void increment()
482           { assignVal( _val.nextInPool() ); }
483
484         private:
485           void assignVal( const Solvable & val_r )
486           { _val = val_r; base_reference() = _val.get(); }
487
488           Solvable _val;
489       };
490     } // namespace detail
491     ///////////////////////////////////////////////////////////////////
492   } // namespace sat
493   ///////////////////////////////////////////////////////////////////
494
495   class PoolItem;
496   ///////////////////////////////////////////////////////////////////
497   namespace sat
498   {
499     /** To Solvable transform functor.
500      * \relates Solvable
501      * \relates sat::SolvIterMixin
502      */
503     struct asSolvable
504     {
505       typedef Solvable result_type;
506
507       Solvable operator()( const Solvable & solv_r ) const
508       { return solv_r; }
509
510       Solvable operator()( const PoolItem & pi_r ) const;
511
512       Solvable operator()( const ResObject_constPtr & res_r ) const;
513     };
514   } // namespace sat
515   ///////////////////////////////////////////////////////////////////
516 } // namespace zypp
517 ///////////////////////////////////////////////////////////////////
518
519 ZYPP_DEFINE_ID_HASHABLE( ::zypp::sat::Solvable );
520
521 #endif // ZYPP_SAT_SOLVABLE_H