typos
[platform/upstream/libzypp.git] / zypp / ui / SelectableImpl.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/SelectableImpl.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/ui/SelectableImpl.h"
16
17 using std::endl;
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22   ///////////////////////////////////////////////////////////////////
23   namespace ui
24   { /////////////////////////////////////////////////////////////////
25
26     ///////////////////////////////////////////////////////////////////
27     //
28     //  CLASS NAME : StatusHelper
29     //
30     /**
31     */
32     struct StatusHelper
33     {
34       StatusHelper( const Selectable::Impl & impl )
35       : _impl( impl )
36       , inst( impl.installedObj() )
37       , cand( impl.candidateObj() )
38       {}
39
40       //
41       // Queries
42       //
43       bool hasInstalled() const
44       { return inst; }
45
46       bool hasCandidate() const
47       { return cand; }
48
49       bool hasInstalledOnly() const
50       { return inst && !cand; }
51
52       bool hasCandidateOnly() const
53       { return cand && !inst; }
54
55       bool hasBoth() const
56       { return inst && cand; }
57
58       //
59       // ResStatus manip
60       //
61       void resetTransactingCandidates() const
62       {
63           for ( Selectable::Impl::available_const_iterator it = _impl.availableBegin();
64               it != _impl.availableEnd(); ++it )
65           {
66             (*it).status().setTransact( false, ResStatus::USER );
67           }
68       }
69       void unlockCandidates() const
70       {
71           for ( Selectable::Impl::available_const_iterator it = _impl.availableBegin();
72               it != _impl.availableEnd(); ++it )
73           {
74             (*it).status().setTransact( false, ResStatus::USER );
75             (*it).status().setLock( false, ResStatus::USER );
76           }
77       }
78       void lockCandidates() const
79       {
80           for ( Selectable::Impl::available_const_iterator it = _impl.availableBegin();
81               it != _impl.availableEnd(); ++it )
82           {
83             (*it).status().setTransact( false, ResStatus::USER );
84             (*it).status().setLock( true, ResStatus::USER );
85           }
86       }
87
88       bool setInstall() const
89       {
90         if ( cand )
91           {
92               if ( inst ) {
93                   inst.status().resetTransact( ResStatus::USER );
94                   inst.status().setLock    ( false, ResStatus::USER );
95                   if ( ! cand->installOnly() )
96                   {
97                     // This is what the solver most probabely will do.
98                     // If we are wrong the solver will correct it. But
99                     // this way we will get a better disk usage result,
100                     // even if no autosolving is on.
101                     inst.status().setTransact( true,  ResStatus::SOLVER );
102                   }
103               }
104               unlockCandidates();
105               return cand.status().setToBeInstalled( ResStatus::USER );
106           }
107         return false;
108       }
109
110       bool setDelete() const
111       {
112         if ( inst )
113           {
114             resetTransactingCandidates();
115             inst.status().setLock( false, ResStatus::USER );
116             return inst.status().setToBeUninstalled( ResStatus::USER );
117           }
118         return false;
119       }
120
121       bool unset() const
122       {
123           if ( inst ) {
124               inst.status().resetTransact( ResStatus::USER );
125               inst.status().setLock( false, ResStatus::USER );
126           }
127           unlockCandidates();
128           return true;
129       }
130
131       bool setProtected() const
132       {
133           if ( inst ) {
134               resetTransactingCandidates();
135               inst.status().resetTransact( ResStatus::USER );
136               return inst.status().setLock( true, ResStatus::USER );
137           } else
138               return false;
139       }
140
141       bool setTaboo() const
142       {
143           if ( cand ) {
144               lockCandidates();
145               return true;
146           } else
147               return false;
148       }
149
150
151     public:
152       const Selectable::Impl & _impl;
153       PoolItem inst;
154       PoolItem cand;
155     };
156     ///////////////////////////////////////////////////////////////////
157
158     ///////////////////////////////////////////////////////////////////
159     //
160     //  CLASS NAME : Selectable::Impl
161     //
162     ///////////////////////////////////////////////////////////////////
163
164     Status Selectable::Impl::status() const
165     {
166       PoolItem cand( candidateObj() );
167       if ( cand && cand.status().transacts() )
168         {
169           if ( cand.status().isByUser() )
170             return( installedObj() ? S_Update : S_Install );
171           else
172             return( installedObj() ? S_AutoUpdate : S_AutoInstall );
173         }
174
175       if ( installedObj() && installedObj().status().transacts() )
176         {
177           return( installedObj().status().isByUser() ? S_Del : S_AutoDel );
178         }
179
180       if ( installedObj() && installedObj().status().isLocked() )
181           return S_Protected;
182
183       if ( !installedObj() && allCandidatesLocked() )
184           return S_Taboo;
185
186       // non packages are handled differently
187       if ( ! isKind<Package>(cand.resolvable()) )
188       {
189           return( cand.status().isSatisfied() ? S_KeepInstalled : S_NoInst );
190       }
191
192       return( installedObj() ? S_KeepInstalled : S_NoInst );
193     }
194
195     bool Selectable::Impl::setStatus( const Status state_r )
196     {
197       StatusHelper self( *this );
198
199       switch ( state_r )
200         {
201         case S_Protected:
202             return self.setProtected();
203         case S_Taboo:
204             return self.setTaboo();
205         case S_AutoDel:
206         case S_AutoInstall:
207         case S_AutoUpdate:
208           // Auto level is SOLVER level. UI may query, but not
209           // set at this level.
210           break;
211
212         case S_Del:
213           return self.setDelete();
214           break;
215
216         case S_Install:
217           return self.hasCandidateOnly() && self.setInstall();
218           break;
219
220         case S_Update:
221           return self.hasBoth() && self.setInstall();
222           break;
223
224         case S_KeepInstalled:
225           return self.hasInstalled() && self.unset();
226           break;
227
228         case S_NoInst:
229           return !self.hasInstalled() && self.unset();
230           break;
231         }
232
233       return false;
234     }
235
236     PoolItem Selectable::Impl::setCandidate( ResObject::constPtr byUser_r )
237     {
238       _candidate = PoolItem();
239
240       if ( byUser_r ) // must be in available list
241         {
242           for ( available_const_iterator it = availableBegin();
243                 it != availableEnd(); ++it )
244             {
245               if ( it->resolvable() == byUser_r )
246                 {
247                   _candidate = *it;
248                   break;
249                 }
250             }
251         }
252
253       if ( _candidate )
254         {
255           PoolItem trans( transactingCandidate() );
256           if ( trans && trans != _candidate )
257             {
258               // adjust transact to the new cancidate
259               trans.status().setTransact( false, ResStatus::USER );
260               _candidate.status().setTransact( true, ResStatus::USER );
261             }
262         }
263
264       return _candidate;
265     }
266
267     ResStatus::TransactByValue Selectable::Impl::modifiedBy() const
268     {
269       PoolItem cand( candidateObj() );
270       if ( cand && cand.status().transacts() )
271         return cand.status().getTransactByValue();
272
273       if ( installedObj() && installedObj().status().transacts() )
274         return installedObj().status().getTransactByValue();
275
276       if ( cand )
277         return cand.status().getTransactByValue();
278
279       if ( installedObj() )
280         return installedObj().status().getTransactByValue();
281
282       return ResStatus::SOLVER;
283     }
284
285     /////////////////////////////////////////////////////////////////
286   } // namespace ui
287   ///////////////////////////////////////////////////////////////////
288   /////////////////////////////////////////////////////////////////
289 } // namespace zypp
290 ///////////////////////////////////////////////////////////////////