00001
00002
00003
00004
00005
00006
00007
00008
00012 #include <iostream>
00013
00014
00015 #include "zypp/ui/SelectableImpl.h"
00016
00017 using std::endl;
00018
00020 namespace zypp
00021 {
00022
00023 namespace ui
00024 {
00025
00027
00028
00029
00032 struct StatusHelper
00033 {
00034 StatusHelper( const Selectable::Impl & impl )
00035 : _impl( impl )
00036 , inst( impl.installedObj() )
00037 , cand( impl.candidateObj() )
00038 {}
00039
00040 typedef Selectable::Impl::availableItem_const_iterator availableItem_const_iterator;
00041
00042
00043
00044
00045 bool hasInstalled() const
00046 { return inst; }
00047
00048 bool hasCandidate() const
00049 { return cand; }
00050
00051 bool hasInstalledOnly() const
00052 { return inst && !cand; }
00053
00054 bool hasCandidateOnly() const
00055 { return cand && !inst; }
00056
00057 bool hasBoth() const
00058 { return inst && cand; }
00059
00060
00061
00062
00063 void resetTransactingCandidates() const
00064 {
00065 for ( availableItem_const_iterator it = _impl.availableBegin();
00066 it != _impl.availableEnd(); ++it )
00067 {
00068 if ( (*it).status().transacts() )
00069 (*it).status().setTransact( false, ResStatus::USER );
00070 }
00071 }
00072 void unlockCandidates() const
00073 {
00074 for ( availableItem_const_iterator it = _impl.availableBegin();
00075 it != _impl.availableEnd(); ++it )
00076 {
00077 (*it).status().setTransact( false, ResStatus::USER );
00078 (*it).status().setLock( false, ResStatus::USER );
00079 }
00080 }
00081 void lockCandidates() const
00082 {
00083 for ( availableItem_const_iterator it = _impl.availableBegin();
00084 it != _impl.availableEnd(); ++it )
00085 {
00086 (*it).status().setTransact( false, ResStatus::USER );
00087 (*it).status().setLock( true, ResStatus::USER );
00088 }
00089 }
00090
00091 bool setInstall() const
00092 {
00093 if ( cand )
00094 {
00095 if ( inst ) {
00096 inst.status().setTransact( false, ResStatus::USER );
00097 inst.status().setLock( false, ResStatus::USER );
00098 }
00099 unlockCandidates();
00100 return cand.status().setTransact( true, ResStatus::USER );
00101 }
00102 return false;
00103 }
00104
00105 bool setDelete() const
00106 {
00107 if ( inst )
00108 {
00109 resetTransactingCandidates();
00110 inst.status().setLock( false, ResStatus::USER );
00111 return inst.status().setTransact( true, ResStatus::USER );
00112 }
00113 return false;
00114 }
00115
00116 bool unset() const
00117 {
00118 if ( inst ) {
00119 inst.status().setTransact( false, ResStatus::USER );
00120 inst.status().setLock( false, ResStatus::USER );
00121 }
00122 unlockCandidates();
00123 return true;
00124 }
00125
00126 bool setProtected() const
00127 {
00128 if ( inst ) {
00129 inst.status().setTransact( false, ResStatus::USER );
00130 return inst.status().setLock( true, ResStatus::USER );
00131 } else
00132 return false;
00133 }
00134
00135 bool setTaboo() const
00136 {
00137 if ( cand ) {
00138 lockCandidates();
00139 return true;
00140 } else
00141 return false;
00142 }
00143
00144
00145 public:
00146 const Selectable::Impl & _impl;
00147 PoolItem inst;
00148 PoolItem cand;
00149 };
00151
00153
00154
00155
00157
00158 Status Selectable::Impl::status() const
00159 {
00160 PoolItem cand( candidateObj() );
00161 if ( cand && cand.status().transacts() )
00162 {
00163 if ( cand.status().isByUser() )
00164 return( installedObj() ? S_Update : S_Install );
00165 else
00166 return( installedObj() ? S_AutoUpdate : S_AutoInstall );
00167 }
00168
00169 if ( installedObj() && installedObj().status().transacts() )
00170 {
00171 return( installedObj().status().isByUser() ? S_Del : S_AutoDel );
00172 }
00173
00174 if ( installedObj() && installedObj().status().isLocked() )
00175 return S_Protected;
00176
00177 if ( !installedObj() && allCandidatesLocked() )
00178 return S_Taboo;
00179
00180 return( installedObj() ? S_KeepInstalled : S_NoInst );
00181 }
00182
00183 bool Selectable::Impl::set_status( const Status state_r )
00184 {
00185 StatusHelper self( *this );
00186
00187 switch ( state_r )
00188 {
00189 case S_Protected:
00190 return self.setProtected();
00191 case S_Taboo:
00192 return self.setTaboo();
00193 case S_AutoDel:
00194 case S_AutoInstall:
00195 case S_AutoUpdate:
00196
00197
00198 break;
00199
00200 case S_Del:
00201 return self.setDelete();
00202 break;
00203
00204 case S_Install:
00205 return self.hasCandidateOnly() && self.setInstall();
00206 break;
00207
00208 case S_Update:
00209 return self.hasBoth() && self.setInstall();
00210 break;
00211
00212 case S_KeepInstalled:
00213 return self.hasInstalled() && self.unset();
00214 break;
00215
00216 case S_NoInst:
00217 return !self.hasInstalled() && self.unset();
00218 break;
00219 }
00220
00221 return false;
00222 }
00223
00224 PoolItem Selectable::Impl::setCandidate( ResObject::constPtr byUser_r )
00225 {
00226 _candidate = PoolItem();
00227
00228 if ( byUser_r )
00229 {
00230 for ( availableItem_const_iterator it = availableBegin();
00231 it != availableEnd(); ++it )
00232 {
00233 if ( it->resolvable() == byUser_r )
00234 {
00235 _candidate = *it;
00236 break;
00237 }
00238 }
00239 }
00240
00241 if ( _candidate )
00242 {
00243 PoolItem trans( transactingCandidate() );
00244 if ( trans && trans != _candidate )
00245 {
00246
00247 trans.status().setTransact( false, ResStatus::USER );
00248 _candidate.status().setTransact( true, ResStatus::USER );
00249 }
00250 }
00251
00252 return _candidate;
00253 }
00254
00255 ResStatus::TransactByValue Selectable::Impl::modifiedBy() const
00256 {
00257 PoolItem cand( candidateObj() );
00258 if ( cand && cand.status().transacts() )
00259 return cand.status().getTransactByValue();
00260
00261 if ( installedObj() && installedObj().status().transacts() )
00262 return installedObj().status().getTransactByValue();
00263
00264 if ( cand )
00265 return cand.status().getTransactByValue();
00266
00267 if ( installedObj() )
00268 return installedObj().status().getTransactByValue();
00269
00270 return ResStatus::SOLVER;
00271 }
00272
00274 }
00277 }