- drafted interface suitable for pkg-bindings in
[platform/upstream/libzypp.git] / zypp / ui / Selectable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/Selectable.h
10  *
11 */
12 #ifndef ZYPP_UI_SELECTABLE_H
13 #define ZYPP_UI_SELECTABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/ReferenceCounted.h"
18 #include "zypp/base/NonCopyable.h"
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/base/Iterator.h"
21
22 #include "zypp/ui/SelectableTraits.h"
23 #include "zypp/ui/Status.h"
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29   namespace ui
30   { /////////////////////////////////////////////////////////////////
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //  CLASS NAME : Selectable
35     //
36     /** Collects ResObject of same kind and name.
37      *
38      * \note There's one Selectable per installed item, in case more
39      * than one item is intalled.
40      *
41      * \todo Make it a _Ref.
42     */
43     class Selectable : public base::ReferenceCounted, private base::NonCopyable
44     {
45       friend std::ostream & operator<<( std::ostream & str, const Selectable & obj );
46
47     public:
48       typedef intrusive_ptr<Selectable>        Ptr;
49       typedef intrusive_ptr<const Selectable>  constPtr;
50
51       /** Iterates over ResObject::constPtr */
52       typedef SelectableTraits::available_iterator      available_iterator;
53       typedef SelectableTraits::availableItem_size_type size_type;
54
55     public:
56       /** The ResObjects kind. */
57       ResObject::Kind kind() const;
58
59       /** The ResObjects name.  */
60       const std::string & name() const;
61
62       /** Installed object. */
63       ResObject::constPtr installedObj() const;
64
65       /** Best among available objects. */
66       ResObject::constPtr candidateObj() const;
67
68       /** Best among all objects. */
69       ResObject::constPtr theObj() const;
70
71       /** Number of available objects. */
72       size_type availableObjs() const;
73
74       /** */
75       available_iterator availableBegin() const;
76
77       /** */
78       available_iterator availableEnd() const;
79
80     public:
81       /** \name Query for objects within this Selectable.
82       */
83       //@{
84       /** True if either installed or candidate object is present */
85       bool hasObject() const
86       { return installedObj() || candidateObj(); }
87
88       /** True if installed object is present. */
89       bool hasInstalledObj() const
90       { return installedObj(); }
91
92       /** True if candidate object is present. */
93       bool hasCandidateObj() const
94       { return candidateObj(); }
95
96       /** True if installed and candidate object is present */
97       bool hasBothObjects() const
98       { return installedObj() && candidateObj(); }
99
100       /** True if installed object is present but no candidate. */
101       bool hasInstalledObjOnly() const
102       { return installedObj() && ! candidateObj(); }
103
104       /** True if candidate object is present but no installed. */
105       bool hasCandidateObjOnly() const
106       { return ! installedObj() && candidateObj(); }
107       //@}
108
109     public:
110       /** \name Query objects fate in case of commit.
111       */
112       //@{
113       enum Fate {
114         TO_DELETE  = -1,
115         UNMODIFIED = 0,
116         TO_INSTALL = 1
117       };
118       /**  */
119       Fate fate() const { return UNMODIFIED; } //TBI
120
121       /** True if either to delete or to install */
122       bool unmodified() const
123       { return fate() == UNMODIFIED; }
124
125       /** True if either to delete or to install */
126       bool toModify() const
127       { return fate() != UNMODIFIED; }
128
129       /** True if to delete */
130       bool toDelete() const
131       { return fate() == TO_DELETE; }
132
133       /** True if to install */
134       bool toInstall() const
135       { return fate() == TO_INSTALL; }
136
137       /** Return who caused the modification. */
138       ResStatus::TransactByValue modifiedBy() const { return ResStatus::USER; } //TBI
139
140       /** True if modification was caused by by_r. */
141       bool isModifiedBy( ResStatus::TransactByValue by_r ) const;
142
143       /** True if the object won't be present on the targetSystem after commit. */
144       bool isOffSystem() const { return false; } //TBI
145
146       /** True if the object will be present on the targetSystem after commit. */
147       bool isOnSystem() const { return false; } //TBI
148       //@}
149
150     public:
151       /** \name Status manipulation.
152        *
153        * \note Every status manipulation will fail, if it
154        * contradicts an action with higher ResStatus::TransactByValue.
155       */
156       //@{
157       bool setFate( Fate fate_r, ResStatus::TransactByValue by_r )
158       { return false; } //TBI
159
160       /** */
161       bool setUnmodified( ResStatus::TransactByValue by_r )
162       { return setFate( UNMODIFIED, by_r ); }
163
164       /** Request to delete an installed object.
165        * Also fails if no installed object is present.
166       */
167       bool setToDelete( ResStatus::TransactByValue by_r )
168       { return setFate( TO_DELETE, by_r ); }
169
170       /** Request to install an available object.
171        * Also fails if no available object is present.
172       */
173       bool setToInstall( ResStatus::TransactByValue by_r )
174       { return setFate( TO_INSTALL, by_r ); }
175
176        /** Request to take care the object is not present after commit.
177         * Delete if installed, and do not install.
178        */
179       bool setOffSystem( ResStatus::TransactByValue by_r )
180       { return false; } //TBI
181
182       /** Request to take care the object is present after commit.
183        * Install if not installed, and do not delete.
184       */
185       bool setOnSystem( ResStatus::TransactByValue by_r )
186       { return false; } //TBI
187
188       /** Request to take care the object is present after commit,
189        * replacing the installed one, if a newer candidate is available.
190        * Install if not installed, and do not delete.
191       */
192       bool setOnSystemNew( ResStatus::TransactByValue by_r )
193       { return false; } //TBI
194
195       //@}
196
197     public:
198       /** \name Special inteface for Y2UI.
199        * \note This interface acts on \ref ResStatus::USER level.
200        * The \ref Status enum, and allowed state transitions are
201        * tightly related to the Y2UI. It might be not verry usefull
202        * outside the Y2UI.
203       */
204       //@{
205       /** Return the current Status */
206       Status status() const;
207       /** Try to set a new Status.
208        * Returns \c false if the transitions is not allowed.
209       */
210       bool set_status( const Status state_r );
211       //@}
212
213     public:
214       /** Implementation  */
215       class Impl;
216       typedef shared_ptr<Impl> Impl_Ptr;
217       /** Default ctor */
218       Selectable( Impl_Ptr pimpl_r );
219     private:
220       /** Dtor */
221       ~Selectable();
222     private:
223       /** Pointer to implementation */
224       RW_pointer<Impl> _pimpl;
225     };
226     ///////////////////////////////////////////////////////////////////
227
228     /** \relates Selectable Stream output */
229     std::ostream & operator<<( std::ostream & str, const Selectable & obj );
230
231     /////////////////////////////////////////////////////////////////
232   } // namespace ui
233   ///////////////////////////////////////////////////////////////////
234   /////////////////////////////////////////////////////////////////
235 } // namespace zypp
236 ///////////////////////////////////////////////////////////////////
237 #endif // ZYPP_UI_SELECTABLE_H