- Offer a simpler, fate based status manipulation in ui::Selectable.
[platform/upstream/libzypp.git] / zypp / ui / Selectable.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/Selectable.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/ui/Selectable.h"
16 #include "zypp/ui/SelectableImpl.h"
17 #include "zypp/ResPool.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22   ///////////////////////////////////////////////////////////////////
23   namespace ui
24   { /////////////////////////////////////////////////////////////////
25
26     IMPL_PTR_TYPE(Selectable);
27
28     Selectable::Ptr Selectable::get( const pool::ByIdent & ident_r )
29     { return ResPool::instance().proxy().lookup( ident_r ); }
30
31     ///////////////////////////////////////////////////////////////////
32     //
33     //  METHOD NAME : Selectable::Selectable
34     //  METHOD TYPE : Ctor
35     //
36     Selectable::Selectable( Impl_Ptr pimpl_r )
37     : _pimpl( pimpl_r )
38     {}
39
40     ///////////////////////////////////////////////////////////////////
41     //
42     //  METHOD NAME : Selectable::~Selectable
43     //  METHOD TYPE : Dtor
44     //
45     Selectable::~Selectable()
46     {}
47
48     ///////////////////////////////////////////////////////////////////
49     //
50     // Forward to implementation.
51     // Restrict PoolItems to ResObject::constPtr!
52     //
53     ///////////////////////////////////////////////////////////////////
54
55     IdString Selectable::ident() const
56     { return _pimpl->ident(); }
57
58     ResObject::Kind Selectable::kind() const
59     { return _pimpl->kind(); }
60
61     const std::string & Selectable::name() const
62     { return _pimpl->name(); }
63
64     Status Selectable::status() const
65     { return _pimpl->status(); }
66
67     bool Selectable::setStatus( const Status state_r )
68     { return _pimpl->setStatus( state_r ); }
69
70     PoolItem Selectable::installedObj() const
71     { return _pimpl->installedObj(); }
72
73     PoolItem Selectable::candidateObj() const
74     { return _pimpl->candidateObj(); }
75
76     PoolItem Selectable::setCandidate( ResObject::constPtr byUser_r )
77     { return _pimpl->setCandidate( byUser_r ); }
78
79     PoolItem Selectable::theObj() const
80     { return _pimpl->theObj(); }
81
82     ////////////////////////////////////////////////////////////////////////
83
84     bool Selectable::availableEmpty() const
85     { return _pimpl->availableEmpty(); }
86
87     Selectable::available_size_type Selectable::availableSize() const
88     { return _pimpl->availableSize(); }
89
90     Selectable::available_iterator Selectable::availableBegin() const
91     { return _pimpl->availableBegin(); }
92
93     Selectable::available_iterator Selectable::availableEnd() const
94     { return _pimpl->availableEnd(); }
95
96     ////////////////////////////////////////////////////////////////////////
97
98     bool Selectable::installedEmpty() const
99     { return _pimpl->installedEmpty(); }
100
101     Selectable::installed_size_type Selectable::installedSize() const
102     { return _pimpl->installedSize(); }
103
104     Selectable::installed_iterator Selectable::installedBegin() const
105     { return _pimpl->installedBegin(); }
106
107     Selectable::installed_iterator Selectable::installedEnd() const
108     { return _pimpl->installedEnd(); }
109
110
111     ////////////////////////////////////////////////////////////////////////
112
113     bool Selectable::isUnmaintained() const
114     { return _pimpl->isUnmaintained(); }
115
116     bool Selectable::isUndetermined() const
117     { return _pimpl->isUndetermined(); }
118
119     bool Selectable::isRelevant() const
120     { return _pimpl->isRelevant(); }
121
122     bool Selectable::isSatisfied() const
123     { return _pimpl->isSatisfied(); }
124
125     bool Selectable::isBroken() const
126     { return _pimpl->isBroken(); }
127
128     bool Selectable::isNeeded() const
129     {
130       return fate() == TO_INSTALL || isBroken() ;
131     }
132
133     ResStatus::TransactByValue Selectable::modifiedBy() const
134     { return _pimpl->modifiedBy(); }
135
136     bool Selectable::hasLicenceConfirmed() const
137     { return _pimpl->hasLicenceConfirmed(); }
138
139     void Selectable::setLicenceConfirmed( bool val_r )
140     { _pimpl->setLicenceConfirmed( val_r ); }
141
142
143     Selectable::Fate Selectable::fate() const
144     {
145       switch ( status() ) {
146       case S_Update:
147       case S_Install:
148       case S_AutoUpdate:
149       case S_AutoInstall:
150         return TO_INSTALL;
151         break;
152
153       case S_Del:
154       case S_AutoDel:
155         return TO_DELETE;
156         break;
157
158       case S_Protected:
159       case S_Taboo:
160       case S_KeepInstalled:
161       case S_NoInst:
162         break;
163       }
164       return UNMODIFIED;
165     };
166
167     bool Selectable::setFate( Fate fate_r )
168     {
169       switch ( fate_r )
170       {
171         case TO_INSTALL:
172           return setStatus( hasInstalledObj() ? S_Update : S_Install );
173           break;
174
175         case TO_DELETE:
176           return setStatus( S_Del );
177           break;
178
179         case UNMODIFIED:
180           return setStatus( hasInstalledObj() ? S_KeepInstalled : S_NoInst );
181           break;
182       }
183     }
184
185     bool Selectable::setInstalled()
186     {
187       return( hasInstalledObj() || setStatus( S_Install ) );
188     }
189
190     bool Selectable::setUpToDate()
191     {
192       if ( ! hasInstalledObj() )
193         return setStatus( S_Install );
194
195       PoolItem cand( candidateObj() );
196       if ( ! cand )
197         return true;
198
199       return( installedObj()->edition() >= cand->edition()
200               || setStatus( S_Update ) );
201     }
202
203     bool Selectable::setDeleted()
204     {
205       return( ! hasInstalledObj() || setStatus( S_Del ) );
206     }
207
208     /******************************************************************
209     **
210     **  FUNCTION NAME : operator<<
211     **  FUNCTION TYPE : std::ostream &
212     */
213     std::ostream & operator<<( std::ostream & str, const Selectable & obj )
214     { return str << *(obj._pimpl); }
215
216     std::ostream & dumpOn( std::ostream & str, const Selectable & obj )
217     { return dumpOn( str, *(obj._pimpl) ); }
218
219     /////////////////////////////////////////////////////////////////
220   } // namespace ui
221   ///////////////////////////////////////////////////////////////////
222   /////////////////////////////////////////////////////////////////
223 } // namespace zypp
224 ///////////////////////////////////////////////////////////////////