Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/timer.py
blob: c631c72d8f2c093f390aaae39862c1b1e50a082d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
#       TerronesWeeper Activity - timer.py
#
#       Copyright 2011 Daniel Francis <santiago.danielfrancis@gmail.com>,
#                      Agustín Zubiaga <aguzubiaga97@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#
#

import gobject


class Timer(gobject.GObject):

    __gsignals__ = {"time-changed": (gobject.SIGNAL_RUN_LAST,
                                     gobject.TYPE_NONE,
                                     (gobject.TYPE_STRING,))}

    def __init__(self):

        gobject.GObject.__init__(self)

        self.time = "00:00:00"
        self.seconds = 0
        self.minutes = 0

        self.timeout = None

    def start(self):
        self.timeout = gobject.timeout_add(1000, self.update_time)

    def stop(self):
        if self.timeout:
            gobject.source_remove(self.timeout)

    def update_time(self):
        self.seconds += 1

        if int(str(float(self.seconds / 60)).split(".")[-1]) > 0:
            self.minutes += 1
            self.seconds = 0

        else:
            if self.seconds / 60 >= 1:
                self.minutes += 1
                self.seconds = 0

        minutes = str(self.minutes)
        seconds = str(self.seconds)

        if self.minutes < 10:
            minutes = "0" + str(self.minutes)

        if self.seconds < 10:
            seconds = "0" + str(self.seconds)

        self.time = "%s:%s" % (minutes, seconds)

        self.emit("time-changed", self.time)

        return True

    def reset_timer(self, start=False):
        self.stop()
        self.minutes = 0
        self.seconds = 0
        self.time = "00:00:00"

        if start:
            self.start()