summaryrefslogtreecommitdiff
path: root/timer-applet/src/timerapplet/core/PresetsStore.py
blob: edd5655a703cc5a43151f4ae9994e19bd63fdd14 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (C) 2008 Jimmy Do <jimmydo@users.sourceforge.net>
# Copyright (C) 2010 Kenny Meyer <knny.myer@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 2 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

try:
    from xml.etree import ElementTree as et
except:
    from elementtree import ElementTree as et

import os
from os import path
import gobject
import gtk
import timerapplet.utils as utils

from timerapplet.utils import (serialize_bool,
                               deserialize_bool,
                               seconds_to_hms,
                               hms_to_seconds)
from timerapplet.defs import VERSION

class PersistentStore(gtk.ListStore):
    def __init__(self, load_func, save_func, *args):
        gtk.ListStore.__init__(self, *args)
        load_func(self)
        
        self.connect('row-deleted', lambda model, row_path: save_func(self))
        self.connect('row-changed', lambda model, row_path, row_iter: save_func(self))

class PresetsStore(gobject.GObject):
    (_NAME_COL,
     _HOURS_COL,
     _MINUTES_COL,
     _SECONDS_COL,
     _COM_COL,
     _NEXT_COL,
     _AUTO_START_COL) = xrange(7)
    
    def __init__(self, filename):
        object.__init__(self)
        self._model = PersistentStore(lambda model: PresetsStore._load_presets(model, filename),
                                      lambda model: PresetsStore._save_presets(model, filename),
                                      gobject.TYPE_STRING,
                                      gobject.TYPE_INT,
                                      gobject.TYPE_INT,
                                      gobject.TYPE_INT,
                                      gobject.TYPE_STRING,
                                      gobject.TYPE_STRING,
                                      gobject.TYPE_BOOLEAN,
                                     )
        
    def get_model(self):
        """Return GtkTreeModel.
        
        Should not rely on it being any particular subtype of GtkTreeModel.
        
        """
        return self._model
    
    def get_preset(self, row_iter):
        return self._model.get(row_iter,
                               PresetsStore._NAME_COL,
                               PresetsStore._HOURS_COL,
                               PresetsStore._MINUTES_COL,
                               PresetsStore._SECONDS_COL,
                               PresetsStore._COM_COL,
                               PresetsStore._NEXT_COL,
                               PresetsStore._AUTO_START_COL,
                              )
    
    def add_preset(self, name, hours, minutes, seconds, command, next_timer,
                   auto_start):
        self._model.append((name, hours, minutes, seconds, command, next_timer,
                           auto_start))
        
    def modify_preset(self, row_iter, name, hours, minutes, seconds, command,
                      next_timer, auto_start):
        self._model.set(row_iter,
                        PresetsStore._NAME_COL, name,
                        PresetsStore._HOURS_COL, hours,
                        PresetsStore._MINUTES_COL, minutes,
                        PresetsStore._SECONDS_COL, seconds,
                        PresetsStore._COM_COL, command,
                        PresetsStore._NEXT_COL, next_timer,
                        PresetsStore._AUTO_START_COL, auto_start
                       )
    
    def remove_preset(self, row_iter):
        self._model.remove(row_iter)
    
    def preset_name_exists_case_insensitive(self, preset_name):
        preset_name = preset_name.lower()
        for preset in self._model:
            if preset_name == preset[PresetsStore._NAME_COL].lower():
                return True
        return False
    
    def _load_presets(model, file_path):
        try:
            tree = et.parse(file_path)
        except:
            return
            
        root = tree.getroot()
        
        for node in root:
            name = node.get('name')
            (hours, minutes, seconds) = seconds_to_hms(int(node.get('duration')))
            command = node.get('command')
            next_timer = node.get('next_timer')
            auto_start = node.get('auto_start')
            model.append((name, hours, minutes, seconds, command, next_timer,
                          deserialize_bool(auto_start)))
    _load_presets = staticmethod(_load_presets)

    def _save_presets(model, file_path):
        root = et.Element('timerapplet')
        root.set('version', VERSION)
        
        def add_xml_node(model, path, row_iter):
            (name, hours, minutes, seconds, command, next_timer, auto_start) = \
                    model.get(row_iter, 
                              PresetsStore._NAME_COL,
                              PresetsStore._HOURS_COL,
                              PresetsStore._MINUTES_COL,
                              PresetsStore._SECONDS_COL, 
                              PresetsStore._COM_COL,
                              PresetsStore._NEXT_COL,
                              PresetsStore._AUTO_START_COL
                             )
            node = et.SubElement(root, 'preset')
            node.set('name', name)
            node.set('duration', str(hms_to_seconds(hours, minutes, seconds)))
            node.set('command', command or '')
            node.set('next_timer', next_timer or '')
            node.set('auto_start', serialize_bool(auto_start))
        
        model.foreach(add_xml_node)
        tree = et.ElementTree(root)
        
        file_dir = path.dirname(file_path)
        if not path.exists(file_dir):
            print 'Creating config directory: %s' % file_dir
            os.makedirs(file_dir, 0744)
        tree.write(file_path)
    _save_presets = staticmethod(_save_presets)