Imported Upstream version 17.25.3
[platform/upstream/libzypp.git] / zypp / ui / UserWantedPackages.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /**
10  * \file        zypp/ui/UserWantedPackages.cc
11  *
12  *  \author     Stefan Hundhammer <sh@suse.de>
13  *
14  */
15 #include <iostream>
16 #include <zypp/base/Logger.h>
17
18 #include <zypp/ui/UserWantedPackages.h>
19
20 #include <zypp/base/PtrTypes.h>
21 #include <zypp/ui/Selectable.h>
22
23 #include <zypp/ResObjects.h>
24 #include <zypp/ZYppFactory.h>
25 #include <zypp/ResPoolProxy.h>
26
27
28 using std::string;
29 using std::set;
30 using std::endl;
31
32
33 namespace zypp
34 {
35     namespace ui
36     {
37         typedef ResPoolProxy::const_iterator    PoolProxyIterator;
38
39         static inline ResPoolProxy              poolProxy()     { return getZYpp()->poolProxy();        }
40
41         template<class T> PoolProxyIterator poolProxyBegin()    { return poolProxy().byKindBegin<T>();  }
42         template<class T> PoolProxyIterator poolProxyEnd()      { return poolProxy().byKindEnd<T>();    }
43
44         static inline PoolProxyIterator pkgBegin()              { return poolProxyBegin<Package>();     }
45         static inline PoolProxyIterator pkgEnd()                { return poolProxyEnd<Package>();       }
46
47         static inline PoolProxyIterator patchesBegin()          { return poolProxyBegin<Patch>();       }
48         static inline PoolProxyIterator patchesEnd()            { return poolProxyEnd<Patch>();         }
49
50         template<typename T> bool contains( const std::set<T> & container, T search )
51         {
52             return container.find( search ) != container.end();
53         }
54
55
56
57         static void addDirectlySelectedPackages ( set<string> & pkgNames );
58         template<class PkgSet_T> void addPkgSetPackages( set<string> & pkgNames );
59
60         static void addPatchPackages            ( set<string> & pkgNames );
61
62
63
64         set<string> userWantedPackageNames()
65         {
66             set<string> pkgNames;
67
68             DBG << "Collecting packages the user explicitly asked for" << endl;
69
70             addDirectlySelectedPackages ( pkgNames );
71             addPatchPackages            ( pkgNames );
72
73             return pkgNames;
74         }
75
76
77
78         static void addDirectlySelectedPackages( set<string> & pkgNames )
79         {
80             for ( PoolProxyIterator it = pkgBegin();
81                   it != pkgEnd();
82                   ++it )
83             {
84                 // Add all packages the user wanted to transact directly,
85                 // no matter what the transaction is (install, update, delete)
86
87                 if ( (*it)->toModify() && (*it)->modifiedBy() == ResStatus::USER )
88                 {
89                     DBG << "Explicit user transaction on pkg \"" << (*it)->name() << "\"" << endl;
90
91                     pkgNames.insert( (*it)->name() );
92                 }
93             }
94         }
95
96
97         static void addPatchPackages( set<string> & pkgNames )
98         {
99             for ( PoolProxyIterator patch_it = patchesBegin();
100                   patch_it != patchesEnd();
101                   ++patch_it )
102             {
103                 Patch::constPtr patch = dynamic_pointer_cast<const Patch>( (*patch_it)->theObj() ? (*patch_it)->theObj().resolvable() : 0 );
104
105                 if ( patch && (*patch_it)->toModify() )
106                 {
107                     DBG << "Patch will be transacted: \"" << patch->name()
108                         << "\" - \"" << patch->summary() << "\"" << endl;
109
110                     Patch::Contents contents( patch->contents() );
111                     for_( it, contents.begin(), contents.end() )
112                     {
113                       pkgNames.insert( it->name() );
114                     }
115                 }
116             }
117         }
118
119     } // namespace ui
120 } // namespace zypp