1 /* 2 * Copyright 2011-2013, 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 <package/Job.h> 12 13 #include <Errors.h> 14 15 #include <package/Context.h> 16 17 18 namespace BPackageKit { 19 20 21 BJobStateListener::~BJobStateListener() 22 { 23 } 24 25 26 void 27 BJobStateListener::JobStarted(BJob* job) 28 { 29 } 30 31 32 void 33 BJobStateListener::JobProgress(BJob* job) 34 { 35 } 36 37 38 void 39 BJobStateListener::JobSucceeded(BJob* job) 40 { 41 } 42 43 44 void 45 BJobStateListener::JobFailed(BJob* job) 46 { 47 } 48 49 50 void 51 BJobStateListener::JobAborted(BJob* job) 52 { 53 } 54 55 56 BJob::BJob(const BContext& context, const BString& title) 57 : 58 fContext(context), 59 fTitle(title), 60 fState(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 70 BJob::~BJob() 71 { 72 } 73 74 75 status_t 76 BJob::InitCheck() const 77 { 78 return fInitStatus; 79 } 80 81 82 const BString& 83 BJob::Title() const 84 { 85 return fTitle; 86 } 87 88 89 BJobState 90 BJob::State() const 91 { 92 return fState; 93 } 94 95 96 status_t 97 BJob::Result() const 98 { 99 return fResult; 100 } 101 102 103 const BString& 104 BJob::ErrorString() const 105 { 106 return fErrorString; 107 } 108 109 110 uint32 111 BJob::TicketNumber() const 112 { 113 return fTicketNumber; 114 } 115 116 117 void 118 BJob::_SetTicketNumber(uint32 ticketNumber) 119 { 120 fTicketNumber = ticketNumber; 121 } 122 123 124 void 125 BJob::_ClearTicketNumber() 126 { 127 fTicketNumber = 0xFFFFFFFFUL; 128 } 129 130 131 void 132 BJob::SetErrorString(const BString& error) 133 { 134 fErrorString = error; 135 } 136 137 138 status_t 139 BJob::Run() 140 { 141 if (fState != JOB_STATE_WAITING_TO_RUN) 142 return B_NOT_ALLOWED; 143 144 fState = JOB_STATE_STARTED; 145 NotifyStateListeners(); 146 147 fState = JOB_STATE_IN_PROGRESS; 148 fResult = Execute(); 149 Cleanup(fResult); 150 151 fState = fResult == B_OK 152 ? JOB_STATE_SUCCEEDED 153 : fResult == B_CANCELED 154 ? JOB_STATE_ABORTED 155 : JOB_STATE_FAILED; 156 NotifyStateListeners(); 157 158 return fResult; 159 } 160 161 162 void 163 BJob::Cleanup(status_t /*jobResult*/) 164 { 165 } 166 167 168 status_t 169 BJob::AddStateListener(BJobStateListener* listener) 170 { 171 return fStateListeners.AddItem(listener) ? B_OK : B_ERROR; 172 } 173 174 175 status_t 176 BJob::RemoveStateListener(BJobStateListener* listener) 177 { 178 return fStateListeners.RemoveItem(listener) ? B_OK : B_ERROR; 179 } 180 181 182 status_t 183 BJob::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 196 BJob::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 209 BJob::IsRunnable() const 210 { 211 return fDependencies.IsEmpty(); 212 } 213 214 215 int32 216 BJob::CountDependencies() const 217 { 218 return fDependencies.CountItems(); 219 } 220 221 222 BJob* 223 BJob::DependantJobAt(int32 index) const 224 { 225 return fDependantJobs.ItemAt(index); 226 } 227 228 229 void 230 BJob::NotifyStateListeners() 231 { 232 int32 count = fStateListeners.CountItems(); 233 for (int i = 0; i < count; ++i) { 234 BJobStateListener* listener = fStateListeners.ItemAt(i); 235 if (listener == NULL) 236 continue; 237 switch (fState) { 238 case JOB_STATE_STARTED: 239 listener->JobStarted(this); 240 break; 241 case JOB_STATE_IN_PROGRESS: 242 listener->JobProgress(this); 243 break; 244 case JOB_STATE_SUCCEEDED: 245 listener->JobSucceeded(this); 246 break; 247 case JOB_STATE_FAILED: 248 listener->JobFailed(this); 249 break; 250 case JOB_STATE_ABORTED: 251 listener->JobAborted(this); 252 break; 253 default: 254 break; 255 } 256 } 257 } 258 259 260 } // namespace BPackageKit 261