~doctormo/gnome-wallchanger/trunk

« back to all changes in this revision

Viewing changes to gwc/old.py

  • Committer: Martin Owens (DoctorMO)
  • Date: 2009-10-11 19:32:33 UTC
  • Revision ID: doctormo@gmail.com-20091011193233-15p70kim7zsjyhqx
Add new modules for ver 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright (C) 2009 Martin Owens
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
"""
 
20
Gnome application for controlling a random wallpaper via crontab.
 
21
"""
 
22
 
 
23
# Generate python libs
 
24
import sys, os
 
25
import gtk, gobject
 
26
import gtk.glade
 
27
import gettext, locale
 
28
 
 
29
sys.path.append('lib')
 
30
 
 
31
from crontab import CronTab
 
32
from gwc.config import config
 
33
 
 
34
APP        = 'wallchanger'
 
35
RAND_APP   = '/usr/bin/random-wallpaper'
 
36
GLADE_DIR  = os.path.abspath('glade')
 
37
LOCALE_DIR = os.path.abspath('po')
 
38
 
 
39
print "Looking for translations in %s" % LOCALE_DIR
 
40
 
 
41
locale.setlocale(locale.LC_ALL)
 
42
for module in gtk.glade, gettext:
 
43
    module.bindtextdomain(APP, LOCALE_DIR)
 
44
    module.textdomain(APP)
 
45
gettext.install(APP)
 
46
 
 
47
#type_widgets = {
 
48
#    'gnome'   : [],
 
49
#    'folder'  : [ 'localdir', 'folder_label', 'recursive', 'recursive_label' ],
 
50
#    'rssfeed' : [ 'rssurl', 'rss_label' ],
 
51
#    'deviant' : [ 'deviant_search', 'deviant_label' ],
 
52
#    'earth'   : [],
 
53
#}
 
54
 
 
55
class SettingsManager(object):
 
56
    """
 
57
    Manage the settings of this feature,
 
58
    will save to crontab and to xml files.
 
59
    """
 
60
    def __init__(self):
 
61
        self.init_gui()
 
62
        self.finish_gui()
 
63
 
 
64
    def glade_xml(self, file, inital):
 
65
        """Load a glade XML File"""
 
66
        return gtk.glade.XML(os.path.join(GLADE_DIR, file), inital, APP)
 
67
 
 
68
    def init_gui(self):
 
69
        """Initalise the gnome signals"""
 
70
        self.signals = {
 
71
            'destroyWindow'    : gtk.main_quit,
 
72
            'changeChange'     : self.change_change,
 
73
            'everyChanged'     : self.every_changed,
 
74
            'timeChanged'      : self.time_changed,
 
75
#            'localdirChanged'  : self.ldir_changed,
 
76
#            'recursiveChanged' : self.recur_changed,
 
77
            'refreshWall'      : self.refresh_wall,
 
78
#            'storedirChanged'  : self.sdir_changed,
 
79
#            'searchChanged'    : self.search_changed,
 
80
#            'rssChanged'       : self.rss_changed,
 
81
#            'oldremoval'       : self.old_removal,
 
82
        }
 
83
 
 
84
        self.wtree  = self.glade_xml('wallchanger.glade', 'window')
 
85
        self.widget = self.wtree.get_widget
 
86
        self.window = self.widget('window')
 
87
        self.window.show()
 
88
 
 
89
#        type = str(config['type'])
 
90
#        for key in type_widgets.keys():
 
91
#            widget = self.widget('type_'+key)
 
92
#            if not widget:
 
93
#                raise ValueError, 'Missing Type Widget %s' % key
 
94
#            widget.connect( 'toggled', self.change_type, key, type_widgets[key] )
 
95
#            if key == type:
 
96
#                widget.set_active(True)
 
97
#                self.change_type( widget, key, type_widgets[key] )
 
98
 
 
99
        change = str(config['change'])
 
100
        if self.widget('change_'+change):
 
101
            self.widget('change_'+change).set_active(True)
 
102
            self.change_change(self.widget('change_'+change))
 
103
 
 
104
#        folder = config['localfolder']
 
105
#        if folder:
 
106
#            folder = os.path.expanduser(str(folder))
 
107
#            if os.path.exists(folder):
 
108
#                self.widget('localdir').set_current_folder(folder)
 
109
#
 
110
#        recur = str(config['recursive']) == 'True'
 
111
#        self.widget('recursive').set_active(recur)
 
112
#
 
113
#        folder = config['folder']
 
114
#        if folder:
 
115
#            folder = os.path.expanduser(str(folder))
 
116
#            if os.path.exists(folder):
 
117
#                self.widget('storedir').set_current_folder(folder)
 
118
#
 
119
#        rssurl = config['rssurl']
 
120
#        if rssurl:
 
121
#            self.widget('rssurl').set_text(str(rssurl))
 
122
#
 
123
#        search = config['search']
 
124
#        if search:
 
125
#            self.widget('deviant_search').set_text(str(search))
 
126
#
 
127
#        rmold = str(config['removeold']) == 'True'
 
128
#        self.widget('removeold').set_active(rmold)
 
129
#
 
130
        every = config['everyunit']
 
131
        if not every:
 
132
            every = 0
 
133
        self.widget('crontype').set_active(int(every))
 
134
 
 
135
        time = config['everystep']
 
136
        if not time:
 
137
            time = 10
 
138
        self.widget('num').set_value(int(time))
 
139
 
 
140
    def change_type(self, widget, text, deps):
 
141
        """Signal indicating a change in source image type"""
 
142
        if widget.get_active():
 
143
            config['type'] = str(text)
 
144
        for key in deps:
 
145
            if widget.get_active():
 
146
                self.widget(key).show()
 
147
            else:
 
148
                self.widget(key).hide()
 
149
 
 
150
    def change_change(self, widget):
 
151
        """Signal indicating a change in time type"""
 
152
        l = widget.get_label()
 
153
        if widget.get_active():
 
154
            if l[0] == 'N':
 
155
                config['change'] = 'never'
 
156
            elif l[0] == 'O':
 
157
                config['change'] = 'onboot'
 
158
            elif l[0] == 'E':
 
159
                config['change'] = 'every'
 
160
                self.widget('num').set_sensitive(True)
 
161
                self.widget('crontype').set_sensitive(True)
 
162
        else:
 
163
            if l[0] == 'E':
 
164
                self.widget('num').set_sensitive(False)
 
165
                self.widget('crontype').set_sensitive(False)
 
166
        self.update_cron()
 
167
 
 
168
    def every_changed(self, widget):
 
169
        """Signal for the every numberal box"""
 
170
        config['everyunit'] = widget.get_active()
 
171
        self.update_cron()
 
172
 
 
173
    def time_changed(self, widget):
 
174
        """Signal for the step widget"""
 
175
        config['everystep'] = int(widget.get_value())
 
176
        self.update_cron()
 
177
 
 
178
    def ldir_changed(self, widget):
 
179
        """When the local directory is updated"""
 
180
        config['localfolder'] = str(widget.get_current_folder())
 
181
 
 
182
    def recur_changed(self, widget):
 
183
        """When the recursive checkbox is changed"""
 
184
        config['recursive'] = str(widget.get_active())
 
185
 
 
186
    def sdir_changed(self, widget):
 
187
        """When the source directory is updated"""
 
188
        config['folder'] = str(widget.get_current_folder())
 
189
 
 
190
    def search_changed(self, widget, text):
 
191
        """When the search field has updated"""
 
192
        config['search'] = str(widget.get_text())
 
193
 
 
194
    def rss_changed(self, widget, text):
 
195
        """When the RSS field has changed"""
 
196
        config['rssurl'] = str(widget.get_text())
 
197
 
 
198
    def old_removal(self, widget):
 
199
        """Whent he remove old images checkbox is changed"""
 
200
        config['removeold'] = str(widget.get_active())
 
201
 
 
202
    def update_cron(self):
 
203
        """Update the saved crontab"""
 
204
        unit = config['everyunit']
 
205
        if not unit:
 
206
            unit = 0
 
207
        step = config['everystep']
 
208
        if not step:
 
209
            step = 10
 
210
 
 
211
        tab = CronTab()
 
212
        l = tab.find_command(RAND_APP.split('/')[-1])
 
213
        if l:
 
214
            e = l[0]
 
215
        else:
 
216
            e = tab.new(RAND_APP)
 
217
 
 
218
        e.command._command = RAND_APP
 
219
        e.special = None
 
220
        type = str(config['change'])
 
221
        if type == 'every':
 
222
            i = 0
 
223
            for s in e.slices:
 
224
                if i == int(unit):
 
225
                    s.every(step)
 
226
                elif i < int(unit):
 
227
                    s.parts = []
 
228
                    s.on(0)
 
229
                else:
 
230
                    s.every(1)
 
231
                i += 1
 
232
        elif type == 'onboot':
 
233
            e.every_reboot()
 
234
        else:
 
235
            tab.remove(e)
 
236
        tab.write()
 
237
 
 
238
    def finish_gui(self):
 
239
        """Finalisation of the gnome GUI"""
 
240
        self.wtree.signal_autoconnect(self.signals)
 
241
 
 
242
    def refresh_wall(self, widget):
 
243
        """Signal for loading a new random image for the background"""
 
244
        config.save()
 
245
        for line in os.popen(RAND_APP):
 
246
            print line
 
247
 
 
248
 
 
249
if __name__ == "__main__":
 
250
    manager = SettingsManager()
 
251
    gtk.main()
 
252