template_manager.py 7.32 KB
Newer Older
Gaute Hope's avatar
Gaute Hope committed
1
# template_manager.py: the template manager window
2 3
# Copyright (C) 2004 - 2009 Gaute Hope <eg at gaute dot vetsj dot com>
#
Gaute Hope's avatar
Gaute Hope committed
4 5 6 7 8 9 10 11 12 13 14 15
# 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 Library 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
Gaute Hope's avatar
Gaute Hope committed
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Gaute Hope's avatar
Gaute Hope committed
17 18 19 20 21 22

# pygtk/python modules
import gtk
import gobject

class TemplateManager:
23 24 25
    def __init__ (self, parent, template):
        self.parent = parent
        self.template = template
Gaute Hope's avatar
Gaute Hope committed
26

27 28 29 30
        # setup window
        self.xml = self.parent.xml
        self.widget = self.xml.get_widget ("template_manager")
        self.widget.connect("delete-event", self.widget.hide_on_delete)
Gaute Hope's avatar
Gaute Hope committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44
        self.treeview = self.xml.get_widget ("tm_treeview")
        self.button_use = self.xml.get_widget ("tm_button_use")
        hbox = gtk.HBox ()
        icon = gtk.Image ()
        icon.set_from_pixbuf (self.parent.normalicontemplate)
        label = gtk.Label (_("Use template"))
        icon.set_alignment (0.5, 0.5)
        label.set_justify (gtk.JUSTIFY_CENTER)
        label.set_alignment (0.5, 0.5)
        hbox.pack_start (icon, True, True, 0)
        hbox.pack_start (label, True, True, 0)
        self.button_use.add (hbox)
        self.button_use.show_all ()
Gaute Hope's avatar
Gaute Hope committed
45

46
        self.button_cancel = self.xml.get_widget ("tm_button_cancel")
Gaute Hope's avatar
Gaute Hope committed
47

48 49 50 51 52 53
        self.xml.signal_connect ("on_tm_button_new_clicked", self.on_new_clicked)
        self.xml.signal_connect ("on_tm_button_use_clicked", self.on_use_clicked)
        self.xml.signal_connect ("on_tm_button_cancel_clicked", self.on_cancel_clicked)
        self.xml.signal_connect ("on_tm_button_edit_clicked", self.on_edit_clicked)
        self.xml.signal_connect ("on_tm_button_delete_clicked", self.on_delete_clicked)
        self.xml.signal_connect ("on_tm_treeview_button_press_event", self.on_tv_pressed)
Gaute Hope's avatar
Gaute Hope committed
54

55 56
        self.button_edit = self.xml.get_widget ("tm_button_edit")
        self.button_delete = self.xml.get_widget ("tm_button_delete")
Gaute Hope's avatar
Gaute Hope committed
57

58
        self.treeview.get_selection().connect("changed", self.on_tv_changed)
Gaute Hope's avatar
Gaute Hope committed
59

60 61 62
        # setup liststore
        # [template id, type, type-string, formatted text, icon/pixbuf]
        self.treemodel = gtk.ListStore (gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gtk.gdk.Pixbuf)
Gaute Hope's avatar
Gaute Hope committed
63

64 65 66
        # setup treeview
        self.treeview.set_model (self.treemodel)
        self.treeview.set_headers_visible (True)
Gaute Hope's avatar
Gaute Hope committed
67 68


69 70
        rend1 = gtk.CellRendererPixbuf ()
        rend2 = gtk.CellRendererText ()
Gaute Hope's avatar
Gaute Hope committed
71

72 73 74 75 76 77
        column = gtk.TreeViewColumn(_("Task"))
        column.pack_start (rend1, True)
        column.pack_end (rend2, True)
        column.add_attribute (rend1, "pixbuf", 4)
        column.add_attribute (rend2, "text", 2)
        self.treeview.append_column(column)
Gaute Hope's avatar
Gaute Hope committed
78 79


80 81
        rend = gtk.CellRendererText ()
        column = gtk.TreeViewColumn(_("Description"), rend, markup=3)
Gaute Hope's avatar
Gaute Hope committed
82
        self.treeview.append_column(column)
Gaute Hope's avatar
Gaute Hope committed
83

84 85 86 87 88 89 90 91
    def on_tv_changed (self, *args):
        if self.treeview.get_selection().count_selected_rows() > 0 :
            value = True
        else:
            value = False
        self.button_use.set_sensitive (value)
        self.button_edit.set_sensitive (value)
        self.button_delete.set_sensitive (value)
Gaute Hope's avatar
Gaute Hope committed
92

93 94 95 96 97 98 99 100 101 102
    def reload_tv (self):
        self.treemodel.clear ()
        at = self.template.gettemplateids ("at")
        if at != None:
            for id in at:
                t = self.template.gettemplate ("at", int (id))
                if t != False:
                    id2, title, command, output = t
                    formatted = self.template.format_at (title, command, output)
                    iter = self.treemodel.append ([int (id), "at", _("One-time"), formatted, self.parent.bigiconat])
Gaute Hope's avatar
Gaute Hope committed
103

104 105 106 107 108 109 110 111
        crontab = self.template.gettemplateids ("crontab")
        if crontab != None:
            for id in crontab:
                t = self.template.gettemplate ("crontab", int (id))
                if t != False:
                    id2, title, command, output, timeexpression = t
                    formatted = self.template.format_crontab (title, command, output, timeexpression)
                    iter = self.treemodel.append ([int (id), "crontab", _("Recurrent"), formatted, self.parent.bigiconcrontab])
Gaute Hope's avatar
Gaute Hope committed
112

113 114 115 116 117 118
    def on_edit_clicked (self, *args):
        store, iter = self.treeview.get_selection().get_selected()
        if iter != None:
            type = self.treemodel.get_value(iter, 1)
            id = self.treemodel.get_value(iter, 0)
            if type == "at":
Gaute Hope's avatar
Gaute Hope committed
119
                t = self.template.gettemplate ("at", int (id))
120 121 122
                if t != False:
                    id2, title, command = t
                    self.parent.at_editor.showedit_template (self.widget, id2, title, command)
Gaute Hope's avatar
Gaute Hope committed
123

124 125 126 127 128 129
            elif type == "crontab":
                t = self.template.gettemplate ("crontab", int (id)  )
                if t != False:
                    id2, title, command, output, timeexpression = t
                    self.parent.crontab_editor.showedit_template (self.widget, id2, title, command, output, timeexpression)
        self.reload_tv ()
Gaute Hope's avatar
Gaute Hope committed
130

131 132
    def on_new_clicked (self, *args):
        self.parent.addWindow.ShowAddWindow (self.widget, 1)
Gaute Hope's avatar
Gaute Hope committed
133

134 135 136 137 138 139 140 141 142
    def on_delete_clicked (self, *args):
        store, iter = self.treeview.get_selection().get_selected()
        if iter != None:
            type = self.treemodel.get_value(iter, 1)
            id = self.treemodel.get_value(iter, 0)
            if type == "at":
                self.template.removetemplate_at (id)
            elif type == "crontab":
                self.template.removetemplate_crontab (id)
Gaute Hope's avatar
Gaute Hope committed
143

144
        self.reload_tv ()
Gaute Hope's avatar
Gaute Hope committed
145 146 147



148 149 150
    def show (self, transient):
        # populate treeview
        self.reload_tv ()
Gaute Hope's avatar
Gaute Hope committed
151

152 153 154
        self.widget.set_transient_for(transient)
        self.widget.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        self.widget.show_all ()
Gaute Hope's avatar
Gaute Hope committed
155

156 157 158
    def on_tv_pressed (self, widget, event):
        if event.type == gtk.gdk._2BUTTON_PRESS:
            self.on_edit_clicked(self, widget)
Gaute Hope's avatar
Gaute Hope committed
159

160 161 162 163 164 165
    def on_use_clicked (self, *args):
        store, iter = self.treeview.get_selection().get_selected()
        if iter != None:
            type = self.treemodel.get_value(iter, 1)
            id = self.treemodel.get_value(iter, 0)
            if type == "at":
Gaute Hope's avatar
Gaute Hope committed
166
                t = self.template.gettemplate ("at", int (id))
167 168 169 170 171 172 173 174
                if t != False:
                    id2, title, command, output = t
                    self.parent.at_editor.showadd_template (self.widget, title, command, output)
            elif type == "crontab":
                t = self.template.gettemplate ("crontab", int (id)  )
                if t != False:
                    id2, title, command, output, timeexpression = t
                    self.parent.crontab_editor.showadd_template (self.widget, title, command, output, timeexpression)
Gaute Hope's avatar
Gaute Hope committed
175

176
            self.widget.hide ()
Gaute Hope's avatar
Gaute Hope committed
177

178 179
    def on_cancel_clicked (self, *args):
        self.widget.hide ()
Gaute Hope's avatar
Gaute Hope committed
180

Gaute Hope's avatar
Gaute Hope committed
181 182 183