[go: up one dir, main page]

File: preferences.py

package info (click to toggle)
cameramonitor 0.2-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 476 kB
  • ctags: 78
  • sloc: sh: 570; python: 398; makefile: 46
file content (295 lines) | stat: -rw-r--r-- 9,892 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: UTF-8 -*-

"""
Copyright (C) 2005-07 Adolfo González Blázquez <code@infinicode.org>

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

If you find any bugs or have any suggestions email: code@infinicode.org
"""

import pygtk
pygtk.require("2.0")
import gtk
import gconf

from cameramonitor import camglobals as glob
from cameramonitor import autostart

class CameraMonitorPreferences:

	# Class init
	def __init__(self, state):

		self.state = state

		self.video_device = '/dev/video0'
		self.timeout = 5    #seconds
		self.notify = True
		self.autostart = False

		self.gconf_path = '/apps/cameramonitor'
		self.gconf_key_video_device = self.gconf_path + '/video_device'
		self.gconf_key_timeout = self.gconf_path + '/timeout'
		self.gconf_key_notify = self.gconf_path + '/notify'
		self.gconf_key_autostart = self.gconf_path + '/autostart'
			
		self.preferences_read()
		self.preferences_monitor_gconf()

		self.prefs = self.preferences_create()

	# Returns video device value
	def preferences_get_video_device(self):
		return self.video_device

	# Returns video device value
	def preferences_get_timeout(self):
		return self.timeout

	# Returns notify enabled?    
	def preferences_get_notify(self):
		return self.notify
		
	# Returns autostart enabled?    
	def preferences_get_autostart(self):
		return self.autostart

	# Save video device
	def preferences_save_video_device(self):
		client = gconf.client_get_default()
		client.set_string(self.gconf_key_video_device, self.video_device)

	# Save timeout
	def preferences_save_timeout(self):
		client = gconf.client_get_default()
		client.set_int(self.gconf_key_timeout, self.timeout)

	# Save notification preferences
	def preferences_save_notify(self):
		client = gconf.client_get_default()
		client.set_bool(self.gconf_key_notify, self.notify)

	# Save autostart preferences
	def preferences_save_autostart(self):
		client = gconf.client_get_default()
		client.set_bool(self.gconf_key_autostart, self.autostart)

	# Save preferences using GConf
	def preferences_save(self):
		self.preferences_save_video_device()
		self.preferences_save_timeout()
		self.preferences_save_notify()
		self.preferences_save_autostart()

	# Read preferences using GConf
	def preferences_read(self):
		client = gconf.client_get_default()
		video = client.get_string(self.gconf_key_video_device)
		timeout = client.get_int(self.gconf_key_timeout)
		notify = client.get_bool(self.gconf_key_notify)
		autostart = client.get_bool(self.gconf_key_autostart)

		if video != None: self.video_device = video
		else: client.set_string(self.gconf_key_video_device, self.video_device)
		if timeout > 0: self.timeout = timeout
		else: client.set_int(self.gconf_key_timeout, self.timeout)
		if notify != None: self.notify = notify
		else: client.set_bool(self.gconf_key_notify, self.notify)
		if autostart != None: self.autostart = autostart
		else: client.set_bool(self.gconf_key_autostart, self.autostart)        

	# Video device changed
	def device_changed(self, widget):
		self.video_device = widget.get_text()
						
	# Video device change finished
	def device_change_end(self, widget, event):
		self.preferences_save_video_device()
		
	# Timeout changed
	def timeout_changed(self, widget):
		self.timeout = widget.get_value_as_int()
		self.preferences_save_timeout()
		
	# Notify changed
	def notify_changed(self, widget):
		self.notify = widget.get_active()
		self.preferences_save_notify()

	# Autostart changed
	def autostart_changed(self, widget):
		self.autostart = widget.get_active()
		self.preferences_save_autostart()
		if self.autostart: autostart.create_autostart_file()
		else: autostart.delete_autostart_file()
		
	# This monitors gconf keys
	def preferences_monitor_gconf(self):
		client = gconf.client_get_default()
		client.add_dir(self.gconf_path, gconf.CLIENT_PRELOAD_NONE)
		client.notify_add(self.gconf_key_video_device, self.preferences_video_key_changed);
		client.notify_add(self.gconf_key_timeout, self.preferences_timeout_key_changed);
		client.notify_add(self.gconf_key_notify, self.preferences_notify_key_changed);
		client.notify_add(self.gconf_key_autostart, self.preferences_autostart_key_changed);

	# Callback: someone's changed the gconf key for video_device
	def preferences_video_key_changed(self, client, connection_id, entry, args):
		self.video_device = entry.get_value().get_string()
		self.dev_entry.set_text(self.video_device)

	# Callback: someone's changed the gconf key for video_device
	def preferences_timeout_key_changed(self, client, connection_id, entry, args):
		self.timeout = entry.get_value().get_int()
		self.tim_spin.set_value(self.timeout)

	# Callback: someone's changed the gconf key for notification
	def preferences_notify_key_changed(self, client, connection_id, entry, args):
		self.notify = entry.get_value().get_bool()
		self.notify_check.set_active(self.notify)

	# Callback: someone's changed the gconf key for autostart
	def preferences_autostart_key_changed(self, client, connection_id, entry, args):
		self.autostart = entry.get_value().get_bool()
		self.autostart_check.set_active(self.autostart)

	# Callback: display preferences dialog
	def preferences_show(self, widget):
		if self.prefs.get_children() == []: self.prefs = self.preferences_create()
		self.dev_entry.set_text(self.video_device)
		self.tim_spin.set_value(self.timeout)
		self.notify_check.set_active(self.notify)
		self.autostart_check.set_active(self.autostart)
		self.prefs.show_all()
	

	# Callback: button close pressed on preferences
	def preferences_close_clicked(self, widget):
		self.preferences_save()
		self.prefs.destroy()
		if self.state == 'alone': gtk.main_quit()

	# Creates the preferences dialog
	def preferences_create(self):

		prefs = gtk.Window()
		prefs.set_title("Camera Monitor Preferences")
		prefs.set_icon_from_file(glob.image)
		prefs.set_position(gtk.WIN_POS_CENTER_ALWAYS)
		prefs.set_resizable(False)
		prefs.connect("destroy", self.preferences_close_clicked)

		# Video Device Prefs
		dev_desc = gtk.Label('<b>Webcam device path</b>')
		dev_desc.set_use_markup(True)
		dev_desc.set_alignment(xalign=0, yalign=0.5)

		dev_label = gtk.Label("Video device:")
		dev_label.set_alignment(xalign=0, yalign=0.5)
		self.dev_entry = gtk.Entry()
		self.dev_entry.set_text(self.video_device)
		self.dev_entry.set_activates_default(True)
		self.dev_entry.connect('changed',self.device_changed)
		self.dev_entry.connect('focus-out-event',self.device_change_end)

		dev_hbox = gtk.HBox()
		dev_hbox.set_spacing(10)
		dev_hbox.pack_start(dev_label)
		dev_hbox.pack_start(self.dev_entry)

		dev_vbox = gtk.VBox()
		dev_vbox.set_border_width(10)
		dev_vbox.set_spacing(10)
		dev_vbox.pack_start(dev_desc)
		dev_vbox.pack_start(dev_hbox)

		# Timeout Prefs
		tim_desc = gtk.Label('<b>Select time between checks</b>')
		tim_desc.set_use_markup(True)
		tim_desc.set_alignment(xalign=0, yalign=0.5)

		tim_label = gtk.Label("Timeout (seconds):")
		tim_label.set_alignment(xalign=0, yalign=0.5)
		tim_adj = gtk.Adjustment(self.timeout, 1.0, 60.0, 1.0, 5.0, 0.0)
		self.tim_spin = gtk.SpinButton(tim_adj, 0, 0)
		self.tim_spin.set_activates_default(True)
		self.tim_spin.connect('changed',self.timeout_changed)

		tim_hbox = gtk.HBox()
		tim_hbox.set_spacing(10)
		tim_hbox.pack_start(tim_label)
		tim_hbox.pack_start(self.tim_spin)

		tim_vbox = gtk.VBox()
		tim_vbox.set_border_width(10)
		tim_vbox.set_spacing(10)
		tim_vbox.pack_start(tim_desc)
		tim_vbox.pack_start(tim_hbox)

		# Notification Prefs
		notify_desc = gtk.Label('<b>Notification</b>')
		notify_desc.set_use_markup(True)
		notify_desc.set_alignment(xalign=0, yalign=0.5)

		self.notify_check = gtk.CheckButton(label="Show notification when camera is switched")
		self.notify_check.set_alignment(xalign=0, yalign=0.5)
		self.notify_check.connect('toggled',self.notify_changed)

		notify_vbox = gtk.VBox()
		notify_vbox.set_border_width(10)
		notify_vbox.set_spacing(10)
		notify_vbox.pack_start(notify_desc)
		notify_vbox.pack_start(self.notify_check)

		# Autostart Prefs
		autostart_desc = gtk.Label('<b>Autostart</b>')
		autostart_desc.set_use_markup(True)
		autostart_desc.set_alignment(xalign=0, yalign=0.5)

		self.autostart_check = gtk.CheckButton(label="Autostart Camera Monitor with every session")
		self.autostart_check.set_alignment(xalign=0, yalign=0.5)
		self.autostart_check.connect('toggled',self.autostart_changed)

		autostart_vbox = gtk.VBox()
		autostart_vbox.set_border_width(10)
		autostart_vbox.set_spacing(10)
		autostart_vbox.pack_start(autostart_desc)
		autostart_vbox.pack_start(self.autostart_check)

		# OK and Cancel buttons
		closebutton = gtk.Button(stock=gtk.STOCK_CLOSE)
		closebutton.connect('clicked', self.preferences_close_clicked)
		closebutton.set_flags(gtk.CAN_DEFAULT)

		buttonbox = gtk.HButtonBox()
		buttonbox.set_spacing(10)
		buttonbox.set_layout(gtk.BUTTONBOX_END)
		buttonbox.pack_start(closebutton)

		# Packing everythin into a vbox
		vbox = gtk.VBox()
		vbox.set_border_width(10)
		vbox.set_spacing(10)
		vbox.pack_start(dev_vbox)
		vbox.pack_start(tim_vbox)
		vbox.pack_start(notify_vbox)
		vbox.pack_start(autostart_vbox)
		vbox.pack_start(buttonbox)

		prefs.add(vbox)
		self.dev_entry.grab_focus()
		closebutton.grab_default()

		return prefs