~slonua/+junk/teatime

« back to all changes in this revision

Viewing changes to teatime.py

  • Committer: Pavel Rojtberg
  • Date: 2011-05-11 23:44:40 UTC
  • Revision ID: pavel@rojtberg.net-20110511234440-v2hgje94hp0yhm3t
* adding/ removing of timers
* correct paths
* load icon from desktop theme

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import time
4
4
import json
 
5
import gettext
5
6
 
6
7
import os.path
7
8
 
8
9
from gi.repository import Unity, GObject, Gtk, Notify, Gdk, Pango
9
10
 
10
 
BASE = os.path.expanduser("~/workspace/teatime/")
 
11
gettext.install("teatime")
 
12
 
 
13
from xdg.BaseDirectory import xdg_data_home
 
14
 
 
15
DATA = os.path.expanduser("~/workspace/teatime/")
 
16
 
 
17
if not os.path.exists(DATA):
 
18
    DATA = "/usr/share/teatime/"
11
19
 
12
20
class Notification(Notify.Notification):
13
21
    def __init__(self):
47
55
        return progress
48
56
 
49
57
class TreeView:
50
 
    def __init__(self, obj):
 
58
    def __init__(self, obj, model):
51
59
        self._obj = obj
52
60
        
53
 
        transl = (("name", ("Name")), ("duration", ("Duration")))
54
 
 
55
 
        cell = Gtk.CellRendererText()
56
 
        cell.set_property("ellipsize", Pango.EllipsizeMode.END)
 
61
        self._model = model
 
62
        
 
63
        self._obj.connect("key-press-event", self._on_key_press)
 
64
        
 
65
        transl = (("name", _("Name")), ("duration", _("Duration")))
57
66
 
58
67
        for key, title in transl:
 
68
            cell = Gtk.CellRendererText()
 
69
            cell.set_property("ellipsize", Pango.EllipsizeMode.END)
 
70
            cell.set_property("editable", True)
 
71
            cell.connect('edited', self._edited_cb, key)
 
72
        
59
73
            col = Gtk.TreeViewColumn(title, cell)
60
74
            col.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
61
75
            col.set_min_width(100)
62
76
            col.set_fixed_width(200)
63
77
            col.set_cell_data_func(cell, self._data_func, key)
64
78
            self._obj.append_column(col)
 
79
    
 
80
    def _on_key_press(self, caller, ev):
 
81
        key = Gdk.keyval_name(ev.keyval)
65
82
 
 
83
        if key == "Delete":
 
84
            model, paths = self._obj.get_selection().get_selected_rows()
 
85
            paths = [model.get_iter(p) for p in paths]
 
86
    
 
87
            model.remove(paths[0])
 
88
    
 
89
    def add_addline(self):
 
90
        self._model.append({"name": _("New Entry"), "duration":0})
 
91
    
 
92
    def _edited_cb(self, cell, itr, value, key):
 
93
        if key == "duration":
 
94
            t = time.strptime(value, "%M:%S")
 
95
            value = t.tm_sec + 60*t.tm_min
 
96
            
 
97
        self._model[itr][key] = value
 
98
                
 
99
        last = int(itr) == (len(self._model._obj)-1)
 
100
        
 
101
        if last:
 
102
            self.add_addline()
 
103
    
66
104
    def _data_func(self, col, cell, model, itr, key):
67
105
        v = model[itr][0][key]
68
106
        
69
107
        if key == "duration":
70
108
            v = time.strftime("%M:%S", time.localtime(v))
71
109
        
 
110
        last = int(str(model.get_path(itr))) == (len(model)-1)
 
111
 
 
112
        cell.set_property("style", Pango.Style.ITALIC if last else Pango.Style.NORMAL)
 
113
        
72
114
        cell.set_property("text", v)
73
115
        
74
116
class ListStore:
75
 
    FILE = BASE+"timers.json"
 
117
    FILE = xdg_data_home+"/teatime.js"
76
118
    
77
119
    def __init__(self, obj):
78
120
        self._obj = obj
79
121
        
80
122
        self.load()
81
123
    
82
 
    def load(self):
83
 
        f = file(self.FILE)
84
 
        
85
 
        for t in json.load(f):
86
 
            self.append(t)
 
124
    def load(self):        
 
125
        try:
 
126
            f = file(self.FILE)
87
127
            
88
 
        f.close()
89
 
        
 
128
            for t in json.load(f):
 
129
                self.append(t)
 
130
        except:
 
131
            pass
 
132
        else:
 
133
            f.close()
 
134
                
90
135
    def save(self):
91
136
        f = file(self.FILE, "w")
92
137
 
93
 
        json.dump([t[0] for t in self._obj], f)
 
138
        json.dump([t[0] for t in self._obj][0:-1], f)
94
139
        
95
140
        f.close()
96
141
        
111
156
        Notify.init("Tea Time")
112
157
        
113
158
        xml = Gtk.Builder()
114
 
        xml.add_from_file(BASE+"window.ui")
 
159
        xml.add_from_file(DATA+"window.ui")
115
160
        
116
161
        self.le = Unity.LauncherEntry.get_for_desktop_file("teatime.desktop")
117
162
        
119
164
        
120
165
        self.start_button = xml.get_object("button1")
121
166
        self.start_button.connect("clicked", self.on_button_click)
122
 
        
123
 
        self.list = TreeView(xml.get_object("treeview1"))
124
 
        self.store = ListStore(xml.get_object("liststore1"))
 
167
 
 
168
        self.store = ListStore(xml.get_object("liststore1"))        
 
169
        self.list = TreeView(xml.get_object("treeview1"), self.store)
 
170
        self.list.add_addline()
125
171
                
126
172
        self.window = xml.get_object("window1")
127
173
        self.window.connect("delete-event", self.end)
140
186
    def set_label_text(self):
141
187
        name = self.timer.obj["name"]
142
188
        remaining = time.strftime("%M:%S", time.localtime(self.timer.end - time.time()))
143
 
        self.label.set_text("%s: %s remaining" % (name, remaining))
 
189
        self.label.set_text(_("%s: %s remaining") % (name, remaining))
144
190
            
145
191
    def start(self):
146
192
        sel = self.list._obj.get_cursor()[0]
151
197
        self.le.set_property("progress_visible", True)
152
198
        self.le.set_property("progress", 0)
153
199
        
154
 
        self.start_button.set_label("Stop Timer")
 
200
        self.start_button.set_label(_("Stop Timer"))
155
201
        self.list._obj.set_sensitive(False)
156
202
        
157
203
        self.set_label_text()
161
207
    def stop(self):
162
208
        self.le.set_property("urgent", False)
163
209
        self.le.set_property("progress_visible", False)
164
 
        self.start_button.set_label("Start Timer")
 
210
        self.start_button.set_label(_("Start Timer"))
165
211
        self.list._obj.set_sensitive(True)
166
212
        self.timer = None
167
 
        self.label.set_text("No Running Timers")
 
213
        self.label.set_text(_("No Running Timers"))
168
214
             
169
215
    def run(self):
170
216
        self.main.run()