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