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 Haiku { 18 19 namespace Package { 20 21 22 JobStateListener::~JobStateListener() 23 { 24 } 25 26 27 void 28 JobStateListener::JobStarted(Job* job) 29 { 30 } 31 32 33 void 34 JobStateListener::JobSucceeded(Job* job) 35 { 36 } 37 38 39 void 40 JobStateListener::JobFailed(Job* job) 41 { 42 } 43 44 45 void 46 JobStateListener::JobAborted(Job* job) 47 { 48 } 49 50 51 Job::Job(const Context& context, const BString& title) 52 : 53 fContext(context), 54 fTitle(title), 55 fState(JOB_STATE_WAITING_TO_RUN) 56 { 57 if (fTitle.Length() == 0) 58 fInitStatus = B_BAD_VALUE; 59 else 60 fInitStatus = B_OK; 61 } 62 63 64 Job::~Job() 65 { 66 } 67 68 69 status_t 70 Job::InitCheck() const 71 { 72 return fInitStatus; 73 } 74 75 76 const BString& 77 Job::Title() const 78 { 79 return fTitle; 80 } 81 82 83 JobState 84 Job::State() const 85 { 86 return fState; 87 } 88 89 90 status_t 91 Job::Result() const 92 { 93 return fResult; 94 } 95 96 97 const BString& 98 Job::ErrorString() const 99 { 100 return fErrorString; 101 } 102 103 104 void 105 Job::SetErrorString(const BString& error) 106 { 107 fErrorString = error; 108 } 109 110 111 status_t 112 Job::Run() 113 { 114 if (fState != JOB_STATE_WAITING_TO_RUN) 115 return B_NOT_ALLOWED; 116 117 fState = JOB_STATE_RUNNING; 118 NotifyStateListeners(); 119 120 fResult = Execute(); 121 Cleanup(fResult); 122 123 fState = fResult == B_OK 124 ? JOB_STATE_SUCCEEDED 125 : fResult == B_CANCELED 126 ? JOB_STATE_ABORTED 127 : JOB_STATE_FAILED; 128 NotifyStateListeners(); 129 130 return fResult; 131 } 132 133 134 void 135 Job::Cleanup(status_t /*jobResult*/) 136 { 137 } 138 139 140 status_t 141 Job::AddStateListener(JobStateListener* listener) 142 { 143 return fStateListeners.AddItem(listener) ? B_OK : B_ERROR; 144 } 145 146 147 status_t 148 Job::RemoveStateListener(JobStateListener* listener) 149 { 150 return fStateListeners.RemoveItem(listener) ? B_OK : B_ERROR; 151 } 152 153 154 status_t 155 Job::AddDependency(Job* job) 156 { 157 if (fDependencies.HasItem(job)) 158 return B_ERROR; 159 160 if (fDependencies.AddItem(job) && job->fDependantJobs.AddItem(this)) 161 return B_OK; 162 163 return B_ERROR; 164 } 165 166 167 status_t 168 Job::RemoveDependency(Job* job) 169 { 170 if (!fDependencies.HasItem(job)) 171 return B_ERROR; 172 173 if (fDependencies.RemoveItem(job) && job->fDependantJobs.RemoveItem(this)) 174 return B_OK; 175 176 return B_ERROR; 177 } 178 179 180 int32 181 Job::CountDependencies() const 182 { 183 return fDependencies.CountItems(); 184 } 185 186 187 Job* 188 Job::DependantJobAt(int32 index) const 189 { 190 return fDependantJobs.ItemAt(index); 191 } 192 193 194 void 195 Job::NotifyStateListeners() 196 { 197 int32 count = fStateListeners.CountItems(); 198 for (int i = 0; i < count; ++i) { 199 JobStateListener* listener = fStateListeners.ItemAt(i); 200 if (listener == NULL) 201 continue; 202 switch (fState) { 203 case JOB_STATE_RUNNING: 204 listener->JobStarted(this); 205 break; 206 case JOB_STATE_SUCCEEDED: 207 listener->JobSucceeded(this); 208 break; 209 case JOB_STATE_FAILED: 210 listener->JobFailed(this); 211 break; 212 case JOB_STATE_ABORTED: 213 listener->JobAborted(this); 214 break; 215 default: 216 break; 217 } 218 } 219 } 220 221 222 } // namespace Package 223 224 } // namespace Haiku 225