1 /* 2 * Copyright 2011-2015, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Tappe <zooey@hirschkaefer.de> 7 * Rene Gollent <rene@gollent.com> 8 */ 9 10 11 #include <Job.h> 12 13 #include <Errors.h> 14 15 16 namespace BSupportKit { 17 18 ~BJobStateListener()19BJobStateListener::~BJobStateListener() 20 { 21 } 22 23 24 void JobStarted(BJob * job)25BJobStateListener::JobStarted(BJob* job) 26 { 27 } 28 29 30 void JobProgress(BJob * job)31BJobStateListener::JobProgress(BJob* job) 32 { 33 } 34 35 36 void JobSucceeded(BJob * job)37BJobStateListener::JobSucceeded(BJob* job) 38 { 39 } 40 41 42 void JobFailed(BJob * job)43BJobStateListener::JobFailed(BJob* job) 44 { 45 } 46 47 48 void JobAborted(BJob * job)49BJobStateListener::JobAborted(BJob* job) 50 { 51 } 52 53 54 // #pragma mark - 55 56 BJob(const BString & title)57BJob::BJob(const BString& title) 58 : 59 fTitle(title), 60 fState(B_JOB_STATE_WAITING_TO_RUN), 61 fTicketNumber(0xFFFFFFFFUL) 62 { 63 if (fTitle.Length() == 0) 64 fInitStatus = B_BAD_VALUE; 65 else 66 fInitStatus = B_OK; 67 } 68 69 ~BJob()70BJob::~BJob() 71 { 72 } 73 74 75 status_t InitCheck() const76BJob::InitCheck() const 77 { 78 return fInitStatus; 79 } 80 81 82 const BString& Title() const83BJob::Title() const 84 { 85 return fTitle; 86 } 87 88 89 BJobState State() const90BJob::State() const 91 { 92 return fState; 93 } 94 95 96 status_t Result() const97BJob::Result() const 98 { 99 return fResult; 100 } 101 102 103 const BString& ErrorString() const104BJob::ErrorString() const 105 { 106 return fErrorString; 107 } 108 109 110 uint32 TicketNumber() const111BJob::TicketNumber() const 112 { 113 return fTicketNumber; 114 } 115 116 117 void _SetTicketNumber(uint32 ticketNumber)118BJob::_SetTicketNumber(uint32 ticketNumber) 119 { 120 fTicketNumber = ticketNumber; 121 } 122 123 124 void _ClearTicketNumber()125BJob::_ClearTicketNumber() 126 { 127 fTicketNumber = 0xFFFFFFFFUL; 128 } 129 130 131 void SetErrorString(const BString & error)132BJob::SetErrorString(const BString& error) 133 { 134 fErrorString = error; 135 } 136 137 138 status_t Run()139BJob::Run() 140 { 141 if (fState != B_JOB_STATE_WAITING_TO_RUN) 142 return B_NOT_ALLOWED; 143 144 fState = B_JOB_STATE_STARTED; 145 NotifyStateListeners(); 146 147 fState = B_JOB_STATE_IN_PROGRESS; 148 fResult = Execute(); 149 Cleanup(fResult); 150 151 fState = fResult == B_OK 152 ? B_JOB_STATE_SUCCEEDED 153 : fResult == B_CANCELED 154 ? B_JOB_STATE_ABORTED 155 : B_JOB_STATE_FAILED; 156 NotifyStateListeners(); 157 158 return fResult; 159 } 160 161 162 void Cleanup(status_t)163BJob::Cleanup(status_t /*jobResult*/) 164 { 165 } 166 167 168 status_t AddStateListener(BJobStateListener * listener)169BJob::AddStateListener(BJobStateListener* listener) 170 { 171 return fStateListeners.AddItem(listener) ? B_OK : B_ERROR; 172 } 173 174 175 status_t RemoveStateListener(BJobStateListener * listener)176BJob::RemoveStateListener(BJobStateListener* listener) 177 { 178 return fStateListeners.RemoveItem(listener) ? B_OK : B_ERROR; 179 } 180 181 182 status_t AddDependency(BJob * job)183BJob::AddDependency(BJob* job) 184 { 185 if (fDependencies.HasItem(job)) 186 return B_ERROR; 187 188 if (fDependencies.AddItem(job) && job->fDependantJobs.AddItem(this)) 189 return B_OK; 190 191 return B_ERROR; 192 } 193 194 195 status_t RemoveDependency(BJob * job)196BJob::RemoveDependency(BJob* job) 197 { 198 if (!fDependencies.HasItem(job)) 199 return B_ERROR; 200 201 if (fDependencies.RemoveItem(job) && job->fDependantJobs.RemoveItem(this)) 202 return B_OK; 203 204 return B_ERROR; 205 } 206 207 208 bool IsRunnable() const209BJob::IsRunnable() const 210 { 211 return fDependencies.IsEmpty(); 212 } 213 214 215 int32 CountDependencies() const216BJob::CountDependencies() const 217 { 218 return fDependencies.CountItems(); 219 } 220 221 222 BJob* DependantJobAt(int32 index) const223BJob::DependantJobAt(int32 index) const 224 { 225 return fDependantJobs.ItemAt(index); 226 } 227 228 229 void SetState(BJobState state)230BJob::SetState(BJobState state) 231 { 232 fState = state; 233 } 234 235 236 void NotifyStateListeners()237BJob::NotifyStateListeners() 238 { 239 int32 count = fStateListeners.CountItems(); 240 for (int i = 0; i < count; ++i) { 241 BJobStateListener* listener = fStateListeners.ItemAt(i); 242 if (listener == NULL) 243 continue; 244 switch (fState) { 245 case B_JOB_STATE_STARTED: 246 listener->JobStarted(this); 247 break; 248 case B_JOB_STATE_IN_PROGRESS: 249 listener->JobProgress(this); 250 break; 251 case B_JOB_STATE_SUCCEEDED: 252 listener->JobSucceeded(this); 253 break; 254 case B_JOB_STATE_FAILED: 255 listener->JobFailed(this); 256 break; 257 case B_JOB_STATE_ABORTED: 258 listener->JobAborted(this); 259 break; 260 default: 261 break; 262 } 263 } 264 } 265 266 267 } // namespace BPackageKit 268