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 16namespace BSupportKit { 17 18 19BJobStateListener::~BJobStateListener() 20{ 21} 22 23 24void 25BJobStateListener::JobStarted(BJob* job) 26{ 27} 28 29 30void 31BJobStateListener::JobProgress(BJob* job) 32{ 33} 34 35 36void 37BJobStateListener::JobSucceeded(BJob* job) 38{ 39} 40 41 42void 43BJobStateListener::JobFailed(BJob* job) 44{ 45} 46 47 48void 49BJobStateListener::JobAborted(BJob* job) 50{ 51} 52 53 54// #pragma mark - 55 56 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 70BJob::~BJob() 71{ 72} 73 74 75status_t 76BJob::InitCheck() const 77{ 78 return fInitStatus; 79} 80 81 82const BString& 83BJob::Title() const 84{ 85 return fTitle; 86} 87 88 89BJobState 90BJob::State() const 91{ 92 return fState; 93} 94 95 96status_t 97BJob::Result() const 98{ 99 return fResult; 100} 101 102 103const BString& 104BJob::ErrorString() const 105{ 106 return fErrorString; 107} 108 109 110uint32 111BJob::TicketNumber() const 112{ 113 return fTicketNumber; 114} 115 116 117void 118BJob::_SetTicketNumber(uint32 ticketNumber) 119{ 120 fTicketNumber = ticketNumber; 121} 122 123 124void 125BJob::_ClearTicketNumber() 126{ 127 fTicketNumber = 0xFFFFFFFFUL; 128} 129 130 131void 132BJob::SetErrorString(const BString& error) 133{ 134 fErrorString = error; 135} 136 137 138status_t 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 162void 163BJob::Cleanup(status_t /*jobResult*/) 164{ 165} 166 167 168status_t 169BJob::AddStateListener(BJobStateListener* listener) 170{ 171 return fStateListeners.AddItem(listener) ? B_OK : B_ERROR; 172} 173 174 175status_t 176BJob::RemoveStateListener(BJobStateListener* listener) 177{ 178 return fStateListeners.RemoveItem(listener) ? B_OK : B_ERROR; 179} 180 181 182status_t 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 195status_t 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 208bool 209BJob::IsRunnable() const 210{ 211 return fDependencies.IsEmpty(); 212} 213 214 215int32 216BJob::CountDependencies() const 217{ 218 return fDependencies.CountItems(); 219} 220 221 222BJob* 223BJob::DependantJobAt(int32 index) const 224{ 225 return fDependantJobs.ItemAt(index); 226} 227 228 229void 230BJob::SetState(BJobState state) 231{ 232 fState = state; 233} 234 235 236void 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