Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

PMSelectable.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       PMSelectable.h
00014 
00015    Author:     Michael Andres <ma@suse.de>
00016    Maintainer: Michael Andres <ma@suse.de>
00017 
00018 /-*/
00019 #ifndef PMSelectable_h
00020 #define PMSelectable_h
00021 
00022 #include <iosfwd>
00023 #include <list>
00024 
00025 #include <y2pm/PMSelectablePtr.h>
00026 
00027 #include <y2pm/PMTypes.h>
00028 #include <y2pm/SelState.h>
00029 #include <y2pm/PkgName.h>
00030 #include <y2pm/PkgArch.h>
00031 #include <y2pm/PMObjectPtr.h>
00032 
00034 //
00035 //      CLASS NAME : PMSelectable
00039 class PMSelectable : public CountedRep {
00040   REP_BODY(PMSelectable);
00041 
00042   public:
00043 
00044     typedef std::list<PMObjectPtr> PMObjectList;
00045 
00046     class SavedState {
00047       public:
00048         SavedState() {}
00049       private:
00050         friend class PMManager;
00051 
00052         PMSelectablePtr _item;
00053         SelState        _state;
00054         PMObjectPtr     _userCandidateObj;
00055 
00056         SavedState( const PMSelectablePtr & item_r );
00057         bool mayReplay() const;
00058         bool replay();
00059         bool diff() const;
00060     };
00061 
00062   private:
00063 
00064     friend class Y2PM;
00065     static bool setCandidateOrder( PM::CandidateOrder neworder_r );
00066 
00067     typedef bool (*ClistCompare)( const PMObjectPtr & lhs, const PMObjectPtr & rhs );
00068 
00069     static ClistCompare _clistCompare;
00070 
00071     static bool clistAVS( const PMObjectPtr & lhs, const PMObjectPtr & rhs );
00072 
00073     static bool clistASV( const PMObjectPtr & lhs, const PMObjectPtr & rhs );
00074 
00075   private:
00076 
00077     friend class PMManager;
00078     friend class SavedState;
00079 
00080     PMManager *      _manager;
00081     SelState         _state;
00082 
00083     void _mgr_attach( PMManager * mgr_r );
00084     void _mgr_detach();
00085 
00086     void _attach_obj( PMObjectPtr & obj_r );
00087     void _detach_obj( PMObjectPtr & obj_r );
00088 
00089     void setInstalledObj( PMObjectPtr obj_r );
00090     void delInstalledObj() { setInstalledObj( PMObjectPtr() ); }
00091 
00092     static bool clistIsBetter( const PMObjectPtr & lhs, const PMObjectPtr & rhs );
00093     void clistAdd( PMObjectPtr obj_r );
00094     void clistDel( PMObjectPtr obj_r );
00095     void clistSort();
00096     void clistClearAll();
00097 
00098     void clearAll();
00099 
00100     bool isEmpty() const;
00101 
00102     void check() const;
00103 
00104   private:
00105 
00106     PkgName _name;
00107 
00108     PMObjectPtr _installedObj;
00109     PMObjectPtr _candidateObj;
00110     PMObjectPtr _userCandidateObj;
00111 
00112     PMObjectList _candidateList;
00113 
00114     PMObjectList::iterator clistLookup( PMObjectPtr obj_r );
00115 
00116     void chooseCandidateObj();
00117     void clearCandidateObj();
00118 
00119   private:
00120 
00121     typedef void (PMManager::*SelectableNotify)( constPMSelectablePtr item_r, SelState old_r, SelState new_r );
00122 
00123     // If the Selectables state is manipulated through this,
00124     // a notify is triggered on state change.
00125     struct ObservedSelState {
00126       const PMSelectable * _item;
00127       SelState &           _cstate;
00128       PMManager *&         _mgr;
00129       SelectableNotify     _notifyFnc;
00130 
00131       ObservedSelState( SelState & cstate_r, const PMSelectable * item_r,
00132                         PMManager *& mgr_r, SelectableNotify notifyFnc_r )
00133         : _item( item_r )
00134         , _cstate( cstate_r )
00135         , _mgr( mgr_r )
00136         , _notifyFnc( notifyFnc_r )
00137           {}
00138 
00139       // signature of SelState:: state manipulating methods
00140       typedef bool (SelState::*Manip)( const bool doit );
00141 
00142       bool operator()( Manip manip, const bool doit ) {
00143         if ( _mgr && _notifyFnc ) {
00144           SelState old( _cstate );
00145           bool ret = (_cstate.*manip)( doit );
00146           if ( old != _cstate ) {
00147             (_mgr->*_notifyFnc)( _item, old, _cstate );
00148           }
00149           return ret;
00150         }
00151         // no Manager to notify
00152         return (_cstate.*manip)( doit );
00153       }
00154     };
00155 
00156     ObservedSelState _observedState; // !decl. AFTER _manager _name and _state
00157 
00158   public:
00159 
00160     //PMSelectable();
00161 
00162     PMSelectable( const PkgName & name_r, SelectableNotify notify_Fnc_r = 0 );
00163 
00164     virtual ~PMSelectable();
00165 
00166   public:
00167 
00171     const PkgName & name() const { return _name; }
00172 
00179     PMObjectPtr installedObj() const { return _installedObj; }
00180 
00187     PMObjectPtr candidateObj() const { return _candidateObj; }
00188 
00193     unsigned availableObjs() const { return _candidateList.size(); }
00194 
00198     PMObjectList::const_iterator av_begin() const { return _candidateList.begin(); }
00199 
00203     PMObjectList::const_iterator av_end() const { return _candidateList.end(); }
00204 
00208     PMObjectList::const_reverse_iterator av_rbegin() const { return _candidateList.rbegin(); }
00209 
00213     PMObjectList::const_reverse_iterator av_rend() const { return _candidateList.rend(); }
00214 
00215   public:
00216 
00224     PMObjectPtr theObject() const {
00225       if ( _candidateObj )
00226         return _candidateObj;
00227       if ( _installedObj )
00228         return _installedObj;
00229       PMObjectPtr ret = bestCandidate();
00230       if ( ret )
00231         return ret;
00232       if ( availableObjs() )
00233         return *av_begin();
00234       return PMObjectPtr();
00235     }
00236 
00241     PMObjectPtr autoCandidate() const;
00242 
00246     PMObjectPtr userCandidate() const {
00247       return _userCandidateObj;
00248     }
00249 
00253     PMObjectPtr bestCandidate() const {
00254       PMObjectPtr ret = userCandidate();
00255       if ( ret )
00256         return ret;
00257       return autoCandidate();
00258     }
00259 
00263     bool setUserCandidate( const PMObjectPtr & obj_r );
00264 
00268     bool clrUserCandidate() { return setUserCandidate( PMObjectPtr() ); }
00269 
00270   public:
00271 
00272     // Keep this order, it's used for sorting package lists!
00273     // Dangerous / noteworthy states are sorted first.
00274     //
00275     // sh@suse.de
00276 
00277     enum UI_Status {
00278       S_Protected,           // Keep this unmodified ( have installedObj && S_Protected )
00279       S_Taboo,               // Keep this unmodified ( have no installedObj && S_Taboo)
00280       // requested by user:
00281       S_Del,                 // delete  installedObj ( clears S_Protected if set )
00282       S_Update,              // install candidateObj ( have installedObj, clears S_Protected if set )
00283       S_Install,             // install candidateObj ( have no installedObj, clears S_Taboo if set )
00284       // not requested by user:
00285       S_AutoDel,             // delete  installedObj
00286       S_AutoUpdate,          // install candidateObj ( have installedObj )
00287       S_AutoInstall,         // install candidateObj ( have no installedObj )
00288       // no modification:
00289       S_KeepInstalled,       // no modification      ( have installedObj && !S_Protected, clears S_Protected if set )
00290       S_NoInst,              // no modification      ( have no installedObj && !S_Taboo, clears S_Taboo if set )
00291     };
00292 
00293     friend std::ostream & operator<<( std::ostream & str, UI_Status obj );
00294 
00295   private:
00296 
00300     bool intern_set_status( const UI_Status state_r, const bool doit );
00301 
00302   public:
00303 
00307     bool set_status( const UI_Status state_r );
00308 
00312     bool test_set_status( const UI_Status state_r ) { return intern_set_status( state_r, false ); }
00313 
00317     UI_Status status() const;
00318 
00319   public:
00320 
00324     bool providesSources() const;
00325 
00329     bool set_source_install( const bool install_r );
00330 
00334     bool source_install() const { return _state.is_srcins(); }
00335 
00336   public:
00337 
00338     enum Fate {
00339       TO_DELETE  = -1,
00340       UNMODIFIED = 0,
00341       TO_INSTALL = 1
00342     };
00343 
00344     static Fate fate( SelState state_r ) {
00345       if ( state_r.to_modify() ) {
00346         return( state_r.to_delete() ? TO_DELETE : TO_INSTALL );
00347       }
00348       return UNMODIFIED;
00349     }
00350 
00351     Fate fate() const { return fate( _state ); }
00352 
00356     void setNothingSelected() { _observedState( &SelState::user_unset, true ); }
00357 
00364     bool downgrade_condition() const;
00365 
00367     // public usable part of SelState
00369 
00373     bool has_object()    const { return _state.has_object(); }
00374 
00378     bool has_installed() const { return _state.has_installed(); }
00379 
00383     bool has_candidate() const { return _state.has_candidate(); }
00384 
00388     bool has_both_objects() const { return _state.has_both_objects(); }
00389 
00393     bool has_installed_only() const { return _state.has_installed_only(); }
00394 
00398     bool has_candidate_only() const { return _state.has_candidate_only(); }
00399 
00400   public:
00401 
00405     bool to_modify()     const { return _state.to_modify(); }
00406 
00410     bool to_delete()     const { return _state.to_delete(); }
00411 
00415     bool to_install()    const { return _state.to_install(); }
00416 
00417   public:
00418 
00422     bool by_user()       const { return _state.by_user(); }
00423 
00427     bool by_appl()       const { return _state.by_appl(); }
00428 
00432     bool by_auto()       const { return _state.by_auto(); }
00433 
00434   public:
00435 
00439     bool is_taboo()      const { return _state.is_taboo(); }
00440 
00441   public:
00442 
00447     bool user_unset() { return _observedState( &SelState::user_unset, true ); }
00448 
00453     bool user_set_delete() { return _observedState( &SelState::user_set_delete, true ); }
00454 
00459     bool user_set_install() { return _observedState( &SelState::user_set_install, true ); }
00460 
00464     bool user_set_taboo() { return _observedState( &SelState::user_set_taboo, true ); }
00465 
00469     bool user_clr_taboo() { return _observedState( &SelState::user_clr_taboo, true ); }
00470 
00471   public:
00472 
00477     bool appl_unset() { return _observedState( &SelState::appl_unset, true ); }
00478 
00483     bool appl_set_delete() { return _observedState( &SelState::appl_set_delete, true ); }
00484 
00490     bool appl_force_install() { return _observedState( &SelState::appl_set_install, true ); }
00491 
00497     bool appl_set_install() { return !downgrade_condition() && appl_force_install(); }
00498 
00499   public:
00500 
00505     bool auto_unset() { return _observedState( &SelState::auto_unset, true ); }
00506 
00511     bool auto_set_delete() { return _observedState( &SelState::auto_set_delete, true ); }
00512 
00518     bool auto_force_install() { return _observedState( &SelState::auto_set_install, true ); }
00519 
00525     bool auto_set_install() { return !downgrade_condition() && auto_force_install(); }
00526 
00527   private:
00528 
00536     bool do_set_onSystem( bool (PMSelectable::*fnc_unset)(), bool (PMSelectable::*fnc_install)() ) {
00537       if ( has_installed() ) {
00538         if ( ! to_delete() )
00539           return true; // already onSystem
00540         return (this->*fnc_unset)();
00541       } else {
00542         if ( to_install() )
00543           return true; // already onSystem
00544         return (this->*fnc_install)();
00545       }
00546     }
00547 
00555     bool do_set_offSystem( bool (PMSelectable::*fnc_unset)(), bool (PMSelectable::*fnc_delete)() ) {
00556       if ( has_installed() ) {
00557         if ( to_delete() )
00558           return true; // already offSystem
00559         return (this->*fnc_delete)();
00560       } else {
00561         if ( ! to_install() )
00562           return true; // already offSystem
00563         return (this->*fnc_unset)();
00564       }
00565     }
00566 
00567   public:
00568 
00575     bool is_onSystem() const {
00576       if ( has_installed() )
00577         return( ! to_delete() );
00578       else
00579         return( to_install() );
00580     }
00581 
00588     bool is_offSystem() const { return( ! is_onSystem() ); }
00589 
00593     bool user_set_onSystem() { return do_set_onSystem( &PMSelectable::user_unset, &PMSelectable::user_set_install ); }
00594 
00598     bool user_set_offSystem() { return do_set_offSystem( &PMSelectable::user_unset, &PMSelectable::user_set_delete ); }
00599 
00603     bool appl_set_onSystem() { return do_set_onSystem( &PMSelectable::appl_unset, &PMSelectable::appl_set_install ); }
00604 
00608     bool appl_set_offSystem() { return do_set_offSystem( &PMSelectable::appl_unset, &PMSelectable::appl_set_delete ); }
00609 
00613     bool auto_set_onSystem() { return do_set_onSystem( &PMSelectable::auto_unset, &PMSelectable::auto_set_install ); }
00614 
00618     bool auto_set_offSystem() { return do_set_offSystem( &PMSelectable::auto_unset, &PMSelectable::auto_set_delete ); }
00619 
00620   public:
00621 
00625     virtual std::ostream & dumpOn( std::ostream & str ) const;
00626 
00630     std::ostream & dumpStateOn( std::ostream & str ) const;
00631 
00632   public:
00633 
00637     typedef bool (PMSelectable::*Test_method)() const;
00638 
00642     typedef bool (*Test_fnc)( const constPMSelectablePtr & sel_r );
00643 
00647     static bool test( const constPMSelectablePtr & sel_r, Test_method fnc_r ) {
00648       if ( sel_r && fnc_r )
00649         return (sel_r.operator->()->*fnc_r)();
00650       return false;
00651     }
00652 };
00653 
00655 
00656 #endif // PMSelectable_h
00657 

Generated on Mon Mar 21 22:08:01 2005 for yast2-packagemanager by  doxygen 1.4.1