Debian Codegraph @

src/jobs/job.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2019 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <jobs/job.h>
14 #include <dialogs/error.h>
15
16 #include <time.h>
17
18 Array<freac::Job *> freac::Job::scheduled;
19 Array<freac::Job *> freac::Job::planned;
20 Array<freac::Job *> freac::Job::running;
21
22 Array<freac::Job *> freac::Job::all;
23
24 Threads::Mutex freac::Job::mutex;
25
26 Signal0<Void> freac::Job::onChange;
27
28 Signal1<Void, freac::Job *> freac::Job::onPlanJob;
29 Signal1<Void, freac::Job *> freac::Job::onRunJob;
30 Signal1<Void, freac::Job *> freac::Job::onFinishJob;
31
32 freac::Job::Job() : ListEntry("Job")
33 {
34 BoCA::I18n *i18n = BoCA::I18n::Get();
35
36 i18n->SetContext("Jobs");
37
38 configuration = BoCA::Config::Copy();
39
40 progressLabel = new Text(i18n->TranslateString("%1:", "Characters").Replace("%1", i18n->TranslateString("Progress")), Point(7, 23));
41
42 progress = new Progressbar(Point(progressLabel->GetX() + progressLabel->GetUnscaledTextWidth() + 7, progressLabel->GetY() - 3), Size(200, 0), OR_HORZ, PB_NOTEXT, 0, 1000, 0);
43
44 timeValue = new EditBox("00:00", Point(42, progress->GetY()), Size(34, 0), 0);
45 timeValue->SetWidth(Math::Max(34, timeValue->GetUnscaledTextWidth() + 6));
46 timeValue->SetOrientation(OR_UPPERRIGHT);
47 timeValue->Deactivate();
48
49 timeLabel = new Text(i18n->TranslateString("%1:", "Characters").Replace("%1", i18n->TranslateString("Time left")), Point(0, progressLabel->GetY()));
50 timeLabel->SetX(timeLabel->GetUnscaledTextWidth() + timeValue->GetWidth() + 15);
51 timeLabel->SetOrientation(OR_UPPERRIGHT);
52
53 progressValue = new EditBox(i18n->TranslateString("%1%", "Technical").Replace("%1", "0"), Point(timeLabel->GetX() + 41, progress->GetY()), Size(34, 0), 0);
54 progressValue->SetOrientation(OR_UPPERRIGHT);
55 progressValue->Deactivate();
56
57 closeHotspot = new Hotspot(Point(12, 3), Size(9, 9));
58 closeHotspot->SetOrientation(OR_UPPERRIGHT);
59 closeHotspot->Deactivate();
60 closeHotspot->onLeftButtonClick.Connect(&Job::OnClickToClose, this);
61
62 Add(progressLabel);
63 Add(progress);
64 Add(progressValue);
65
66 Add(timeLabel);
67 Add(timeValue);
68
69 Add(closeHotspot);
70
71 SetHeight(51);
72
73 onChangeSize.Connect(&Job::OnChangeSize, this);
74 onLeftButtonDoubleClick.Connect(&Job::OnDoubleClick, this);
75
76 all.Add(this, GetHandle());
77
78 errors.EnableLocking();
79
80 startTicks = 0;
81 previousSecondsLeft = 0;
82
83 waiting = False;
84 abort = False;
85
86 /* Notify about jobs change.
87 */
88 mutex.Lock();
89
90 onChange.Emit();
91
92 mutex.Release();
93 }
94
95 freac::Job::~Job()
96 {
97 BoCA::Config::Free(configuration);
98
99 scheduled.Remove(GetHandle());
100 planned.Remove(GetHandle());
101 running.Remove(GetHandle());
102
103 all.Remove(GetHandle());
104
105 /* Notify about jobs change.
106 */
107 mutex.Lock();
108
109 onChange.Emit();
110
111 mutex.Release();
112
113 /* Delete sub-objects.
114 */
115 DeleteObject(progressLabel);
116 DeleteObject(progress);
117 DeleteObject(progressValue);
118
119 DeleteObject(timeLabel);
120 DeleteObject(timeValue);
121
122 DeleteObject(closeHotspot);
123 }
124
125 Int freac::Job::Schedule()
126 {
127 scheduled.InsertAtPos(0, this, GetHandle());
128
129 return Success();
130 }
131
132 Int freac::Job::RunPrecheck()
133 {
134 if (Precheck() != Success())
135 {
136 scheduled.Remove(GetHandle());
137
138 return Error();
139 }
140
141 planned.Add(this, GetHandle());
142 scheduled.Remove(GetHandle());
143
144 /* Notify about planned job.
145 */
146 EnterProtectedRegion();
147
148 mutex.Lock();
149
150 onPlanJob.Emit(this);
151
152 mutex.Release();
153
154 LeaveProtectedRegion();
155
156 return Success();
157 }
158
159 Int freac::Job::Run()
160 {
161 running.Add(this, GetHandle());
162 planned.Remove(GetHandle());
163
164 /* Notify about running job.
165 */
166 EnterProtectedRegion();
167
168 mutex.Lock();
169
170 onRun.Emit();
171 onRunJob.Emit(this);
172
173 mutex.Release();
174
175 LeaveProtectedRegion();
176
177 /* Actually run job.
178 */
179 startTicks = S::System::System::Clock();
180 previousSecondsLeft = 0;
181
182 Perform();
183
184 running.Remove(GetHandle());
185
186 closeHotspot->Activate();
187
188 /* Notify about finished job.
189 */
190 EnterProtectedRegion();
191
192 mutex.Lock();
193
194 onFinish.Emit();
195 onFinishJob.Emit(this);
196
197 mutex.Release();
198
199 LeaveProtectedRegion();
200
201 return Success();
202 }
203
204 Int freac::Job::RequestAbort()
205 {
206 abort = True;
207
208 return Success();
209 }
210
211 Error freac::Job::Precheck()
212 {
213 return Success();
214 }
215
216 Bool freac::Job::ReadyToRun()
217 {
218 return True;
219 }
220
221 Void freac::Job::OnChangeSize(const Size &nSize)
222 {
223 Rect clientRect = Rect(GetPosition(), GetSize());
224 Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
225
226 Surface *surface = (IsVisible() ? GetDrawSurface() : NIL);
227
228 if (surface) surface->StartPaint(GetVisibleArea());
229
230 progress->Hide();
231 progressValue->Hide();
232
233 timeLabel->Hide();
234 timeValue->Hide();
235
236 progress->SetWidth(clientSize.cx - progressLabel->GetUnscaledTextWidth() - progressValue->GetWidth() - timeLabel->GetUnscaledTextWidth() - timeValue->GetWidth() - 36);
237 progressValue->SetX(timeLabel->GetUnscaledTextWidth() + timeValue->GetWidth() + 56);
238
239 timeLabel->SetX(timeLabel->GetUnscaledTextWidth() + timeValue->GetWidth() + 15);
240 timeValue->SetX(timeValue->GetWidth() + 8);
241
242 progress->Show();
243 progressValue->Show();
244
245 timeLabel->Show();
246 timeValue->Show();
247
248 if (surface) surface->EndPaint();
249 }
250
251 Void freac::Job::OnDoubleClick()
252 {
253 if (closeHotspot->IsActive() && closeHotspot->IsMouseOver()) return;
254
255 ErrorDialog dialog(GetErrors());
256
257 dialog.ShowDialog();
258 }
259
260 Void freac::Job::OnClickToClose()
261 {
262 all.Remove(GetHandle());
263
264 /* Notify about jobs change.
265 */
266 EnterProtectedRegion();
267
268 mutex.Lock();
269
270 onChange.Emit();
271
272 mutex.Release();
273
274 Object::DeleteObject(this);
275
276 LeaveProtectedRegion();
277 }
278
279 Int freac::Job::Paint(Int message)
280 {
281 if (!IsRegistered()) return Error();
282 if (!IsVisible()) return Success();
283
284 Surface *surface = GetDrawSurface();
285 Rect frame = Rect(GetRealPosition(), GetRealSize());
286
287 switch (message)
288 {
289 case SP_PAINT:
290 case SP_MOUSEIN:
291 case SP_MOUSEOUT:
292 surface->StartPaint(GetVisibleArea());
293
294 SetVisibleDirect(False);
295
296 if (IsSelected() || IsMouseOver()) SetBackgroundColor(Setup::BackgroundColor.Average(Setup::ClientColor));
297 else SetBackgroundColor(Setup::BackgroundColor);
298
299 SetVisibleDirect(True);
300
301 surface->Box(frame - Size(0, 1), GetBackgroundColor(), Rect::Filled);
302 surface->SetText(text, frame + Point(1, 1) - Size(2, 2), font);
303
304 if (IsMouseOver() && closeHotspot->IsActive())
305 {
306 surface->Line(Point(frame.right - 10, frame.top + 4), Point(frame.right - 4, frame.top + 10), Setup::DividerDarkColor);
307 surface->Line(Point(frame.right - 11, frame.top + 4), Point(frame.right - 4, frame.top + 11), Setup::TextColor);
308 surface->Line(Point(frame.right - 11, frame.top + 5), Point(frame.right - 5, frame.top + 11), Setup::DividerDarkColor);
309
310 surface->Line(Point(frame.right - 11, frame.top + 9), Point(frame.right - 5, frame.top + 3), Setup::DividerDarkColor);
311 surface->Line(Point(frame.right - 11, frame.top + 10), Point(frame.right - 4, frame.top + 3), Setup::TextColor);
312 surface->Line(Point(frame.right - 10, frame.top + 10), Point(frame.right - 4, frame.top + 4), Setup::DividerDarkColor);
313 }
314
315 Widget::Paint(SP_PAINT);
316
317 surface->EndPaint();
318
319 return Success();
320 }
321
322 return Widget::Paint(message);
323 }
324
325 Int freac::Job::SetText(const String &newText)
326 {
327 if (text == newText) return Success();
328
329 Surface *surface = (IsVisible() ? GetDrawSurface() : NIL);
330
331 if (surface) surface->StartPaint(GetVisibleArea());
332
333 Widget::SetText(newText);
334
335 if (surface) surface->EndPaint();
336
337 return Success();
338 }
339
340 Int freac::Job::SetProgress(Int nValue)
341 {
342 Surface *surface = (IsVisible() ? GetDrawSurface() : NIL);
343
344 if (surface) surface->StartPaint(GetVisibleArea());
345
346 progress->SetValue(nValue);
347 progressValue->SetText(BoCA::I18n::Get()->TranslateString("%1%", "Technical").Replace("%1", String::FromInt(Math::Round(Float(nValue) / 10.0))));
348
349 Int64 totalTicks = S::System::System::Clock() - startTicks;
350 Int secondsLeft = (Int) (totalTicks * ((1000.0 - nValue) / nValue)) / 1000 + (nValue < 1000 ? 1 : 0);
351
352 if (secondsLeft < previousSecondsLeft || secondsLeft >= previousSecondsLeft + 2)
353 {
354 String secondsString = SecondsToString(secondsLeft);
355
356 timeValue->SetText(secondsString);
357
358 if (timeValue->GetWidth() != Math::Max(34, timeValue->GetUnscaledTextWidth() + 6))
359 {
360 timeValue->SetWidth(Math::Max(34, timeValue->GetUnscaledTextWidth() + 6));
361
362 OnChangeSize(GetSize());
363 }
364
365 previousSecondsLeft = secondsLeft;
366 }
367
368 if (surface) surface->EndPaint();
369
370 return Success();
371 }
372
373 Int freac::Job::GetProgress() const
374 {
375 return progress->GetValue();
376 }
377
378 String freac::Job::SecondsToString(UnsignedInt seconds)
379 {
380 if (seconds >= 360000) return "??:??:??";
381
382 static String zeroString = "0";
383 static String colonString = ":";
384
385 String buffer;
386 String text;
387
388 /* Append hours.
389 */
390 if (seconds >= 3600)
391 {
392 buffer = String::FromInt(seconds / 3600);
393
394 if (buffer.Length() == 1) text.Append(zeroString);
395
396 text.Append(buffer);
397 text.Append(colonString);
398 }
399
400 /* Append minutes.
401 */
402 buffer = String::FromInt(seconds % 3600 / 60);
403
404 if (buffer.Length() == 1) text.Append(zeroString);
405
406 text.Append(buffer);
407 text.Append(colonString);
408
409 /* Append seconds.
410 */
411 buffer = String::FromInt(seconds % 3600 % 60);
412
413 if (buffer.Length() == 1) text.Append(zeroString);
414
415 text.Append(buffer);
416
417 return text;
418 }