1#
2# This file is part of the LibreOffice project.
3#
4# This Source Code Form is subject to the terms of the Mozilla Public
5# License, v. 2.0. If a copy of the MPL was not distributed with this
6# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7#
8# This file incorporates work covered by the following license notice:
9#
10#   Licensed to the Apache Software Foundation (ASF) under one or more
11#   contributor license agreements. See the NOTICE file distributed
12#   with this work for additional information regarding copyright
13#   ownership. The ASF licenses this file to you under the Apache
14#   License, Version 2.0 (the "License"); you may not use this file
15#   except in compliance with the License. You may obtain a copy of
16#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17import traceback
18
19from .TaskEvent import TaskEvent
20
21class Task:
22    successful = 0
23    failed = 0
24    maximum = 0
25    taskName = ""
26    listeners = []
27    subtaskName = ""
28
29    def __init__(self, taskName_, subtaskName_, max_):
30        self.taskName = taskName_
31        self.subtaskName = subtaskName_
32        self.maximum = max_
33
34    def start(self):
35        self.fireTaskStarted()
36
37    def fail(self):
38        self.fireTaskFailed()
39
40    def getMax(self):
41        return self.maximum
42
43    def setMax(self, max_):
44        self.maximum = max_
45        self.fireTaskStatusChanged()
46
47    def advance(self, success_):
48        if success_:
49            self.successful += 1
50            print ("Success :", self.successful)
51        else:
52            self.failed += 1
53            print ("Failed :", self.failed)
54        self.fireTaskStatusChanged()
55        if (self.failed + self.successful == self.maximum):
56            self.fireTaskFinished()
57
58    def fireTaskStatusChanged(self):
59        te = TaskEvent(self, TaskEvent.TASK_STATUS_CHANGED)
60        for i in range(len(self.listeners)):
61            self.listeners[i].taskStatusChanged(te)
62
63    def fireTaskStarted(self):
64        te = TaskEvent(self, TaskEvent.TASK_STARTED)
65        for i in range(len(self.listeners)):
66            self.listeners[i].taskStarted(te)
67
68    def fireTaskFailed(self):
69        te = TaskEvent(self, TaskEvent.TASK_FAILED)
70        for i in range(len(self.listeners)):
71            self.listeners[i].taskFinished(te)
72
73    def fireTaskFinished(self):
74        te = TaskEvent(self, TaskEvent.TASK_FINISHED)
75        for i in range(len(self.listeners)):
76            self.listeners[i].taskFinished(te)
77