/gtk/prep-images

To get this branch, use:
bzr branch http://bzr.ed.am/gtk/prep-images
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
# preferences.py
#
# Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
#
# This file is part of prep-images (hereafter referred to as "this program").
# See http://www.waxworlds.org/edam/software/gtk/prep-images for more
# information.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.


import os
import pygtk
pygtk.require( '2.0' );
import gtk


class Preferences:

	RCFILE = '~/.config/prep-images.rc'

	def __init__( self ):

		# default values
		self.copyright_filename = ''
		self.resize = False
		self.resize_width = 1000
		self.resize_height = 1000
		self.export_inplace = False
		self.export_append = ".publish"

		# load preferences
		rclines = []
		try:
			f = open( os.path.expanduser( self.RCFILE ), 'r' )
			try:
				rclines = f.readlines()
			except IOError as e:
				dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, \
					gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, \
					e.strerror )
				dialog.run()
				dialog.destroy()
				exit( 1 )
		except IOError as e:
			pass

		# parse prefs
		for line in rclines:
			parts = line.strip().split( ' ', 2 )
			if len( parts ) == 2:
				if parts[ 0 ] == 'copyright-filename':
					self.copyright_filename = parts[ 1 ].strip()
				elif parts[ 0 ] == 'resize':
					self.resize = bool( int( parts[ 1 ] ) )
				elif parts[ 0 ] == 'resize-width':
					self.resize_width = int( parts[ 1 ] )
				elif parts[ 0 ] == 'resize-height':
					self.resize_height = int( parts[ 1 ] )
				elif parts[ 0 ] == 'export-inplace':
					self.export_inplace = bool( int( parts[ 1 ] ) )
				elif parts[ 0 ] == 'export-append':
					self.export_append = parts[ 1 ].strip()

	def save( self ):
		try:
			f = open( os.path.expanduser( self.RCFILE ), 'w' )
			f.write( "copyright-filename " + self.copyright_filename + "\n" )
			f.write( "resize " + str( int( self.resize ) ) + "\n" )
			f.write( "resize-width " + str( self.resize_width ) + "\n" )
			f.write( "resize-height " + str( self.resize_height ) + "\n" )
			f.write( "export-inplace " + str( int( self.export_inplace ) ) + \
				"\n" )
			f.write( "export-append " + self.export_append + "\n" )
		except IOError as e:
			dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, \
				gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, e.strerror )
			dialog.run()
			dialog.destroy()
			exit( 1 )