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