Deprecate MediaAccess::downloads (accidentally deleted)
[platform/upstream/libzypp.git] / zypp / Patch.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Patch.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/base/LogTools.h"
15 #include "zypp/Patch.h"
16 #include "zypp/sat/WhatProvides.h"
17
18 using std::endl;
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   IMPL_PTR_TYPE( Patch );
25
26   ///////////////////////////////////////////////////////////////////
27   //
28   //    METHOD NAME : Patch::Patch
29   //    METHOD TYPE : Ctor
30   //
31   Patch::Patch( const sat::Solvable & solvable_r )
32   : ResObject( solvable_r )
33   {}
34
35   ///////////////////////////////////////////////////////////////////
36   //
37   //    METHOD NAME : Patch::~Patch
38   //    METHOD TYPE : Dtor
39   //
40   Patch::~Patch()
41   {}
42
43   ///////////////////////////////////////////////////////////////////
44   //
45   //    Patch interface forwarded to implementation
46   //
47   ///////////////////////////////////////////////////////////////////
48
49   Patch::Category Patch::categoryEnum() const
50   {
51     static const IdString cat_yast( "yast" );
52     static const IdString cat_security( "security" );
53     static const IdString cat_recommended( "recommended" );
54     static const IdString cat_optional( "optional" );
55     static const IdString cat_document( "document" );
56
57     // patch category is not poolized in the solv file (i.e. an IdString) ;(
58     IdString cat( sat::LookupAttr( sat::SolvAttr::patchcategory, satSolvable() ).begin().c_str() );
59
60     if ( cat == cat_yast )
61       return CAT_YAST;
62     if ( cat == cat_security )
63       return CAT_SECURITY;
64     if ( cat == cat_recommended )
65       return CAT_RECOMMENDED;
66     if ( cat == cat_optional )
67       return CAT_OPTIONAL;
68     if ( cat == cat_document )
69       return CAT_DOCUMENT;
70
71     return CAT_OTHER;
72   }
73
74   std::string Patch::message( const Locale & lang_r ) const
75   { return lookupStrAttribute( sat::SolvAttr::message, lang_r ); }
76
77   std::string Patch::category() const
78   { return lookupStrAttribute( sat::SolvAttr::patchcategory ); }
79
80   bool Patch::rebootSuggested() const
81   { return lookupBoolAttribute( sat::SolvAttr::rebootSuggested ); }
82
83   bool Patch::restartSuggested() const
84   { return lookupBoolAttribute( sat::SolvAttr::restartSuggested ); }
85
86   bool Patch::reloginSuggested() const
87   { return lookupBoolAttribute( sat::SolvAttr::reloginSuggested ); }
88
89
90   bool Patch::interactive() const
91   {
92     if ( rebootSuggested()
93          || ! message().empty()
94          || ! licenseToConfirm().empty() )
95     {
96       return true;
97     }
98
99     Patch::Contents c( contents() );
100     for_( it, c.begin(), c.end() )
101     {
102       if ( ! licenseToConfirm().empty() )
103       {
104         return true;
105       }
106     }
107
108     return false;
109   }
110
111   Patch::Contents Patch::contents() const
112   {
113     Contents result;
114     // DBG << *this << endl;
115     sat::LookupAttr updateCollection( sat::SolvAttr::updateCollection, satSolvable() );
116     for_( entry, updateCollection.begin(), updateCollection.end() )
117     {
118       IdString name    ( entry.subFind( sat::SolvAttr::updateCollectionName ).idStr() );
119       Edition  edition ( entry.subFind( sat::SolvAttr::updateCollectionEvr ).idStr() );
120       Arch     arch    ( entry.subFind( sat::SolvAttr::updateCollectionArch ).idStr() );
121       if ( name.empty() )
122       {
123         WAR << "Ignore malformed updateCollection entry: " << name << "-" << edition << "." << arch << endl;
124         continue;
125       }
126
127       // The entry is relevant if there is an installed
128       // package with the same name and arch.
129       bool relevant = false;
130       sat::WhatProvides providers( (Capability( name.id() )) );
131       for_( it, providers.begin(), providers.end() )
132       {
133         if ( it->isSystem() && it->ident() == name && it->arch() == arch )
134         {
135           relevant = true;
136           break;
137         }
138       }
139       if ( ! relevant )
140       {
141         // DBG << "Not relevant: " << name << "-" << edition << "." << arch << endl;
142         continue;
143       }
144
145 #warning definition of patch contents is poor - needs review
146       /* find exact providers first (this matches the _real_ 'collection content' of the patch */
147       providers = sat::WhatProvides( Capability( arch, name.c_str(), Rel::EQ, edition, ResKind::package ) );
148       if ( providers.empty() )
149       {
150         /* no exact providers: find 'best' providers: those with a larger evr */
151         providers = sat::WhatProvides( Capability( arch, name.c_str(), Rel::GT, edition, ResKind::package ) );
152         if ( providers.empty() )
153         {
154           // Hmm, this patch is not installable, no one is providing the package in the collection
155           // FIXME: raise execption ? fake a solvable ?
156           WAR << "Missing provider: " << name << "-" << edition << "." << arch << endl;
157           continue;
158         }
159       }
160
161       // FIXME ?! loop over providers and try to find installed ones ?
162       // DBG << "Found " << name << "-" << edition << "." << arch << ": " << *(providers.begin()) << endl;
163       result.get().insert( *(providers.begin()) );
164     }
165
166     return result;
167   }
168
169   ///////////////////////////////////////////////////////////////////
170   //
171   //    CLASS NAME : Patch::ReferenceIterator
172   //
173   ///////////////////////////////////////////////////////////////////
174
175   Patch::ReferenceIterator::ReferenceIterator( const sat::Solvable & val_r )
176   { base_reference() = sat::LookupAttr( sat::SolvAttr::updateReference, val_r ).begin(); }
177
178   std::string Patch::ReferenceIterator::id() const
179   { return base_reference().subFind( sat::SolvAttr::updateReferenceId ).asString(); }
180   std::string Patch::ReferenceIterator::href() const
181   { return base_reference().subFind( sat::SolvAttr::updateReferenceHref ).asString(); }
182   std::string Patch::ReferenceIterator::title() const
183   { return base_reference().subFind( sat::SolvAttr::updateReferenceTitle ).asString(); }
184   std::string Patch::ReferenceIterator::type() const
185   { return base_reference().subFind( sat::SolvAttr::updateReferenceType ).asString(); }
186
187   /////////////////////////////////////////////////////////////////
188 } // namespace zypp
189 ///////////////////////////////////////////////////////////////////