Generate Dutch translations.
[wammu.git] / Wammu / GammuSettings.py
blobc3c7d50e870ea9b46a2891d07355ac3f6be67c38
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Wrapper for Gammu configuration
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import wx
27 import re
29 class GammuSettings:
30 '''
31 Class wrapping gammu configuration file for reading and writing.
32 '''
33 def __init__(self, wammu_cfg, path = None):
34 '''
35 Reads gammu configuration and prepares it for use.
36 '''
37 self.wammu_cfg = wammu_cfg
38 if path is not None:
39 self.filename = path
40 else:
41 self.filename = self.wammu_cfg.Read('/Gammu/Gammurc')
42 self.config = wx.FileConfig(
43 localFilename = self.filename,
44 style = wx.CONFIG_USE_LOCAL_FILE
46 self.list = []
48 cont, val, idx = self.config.GetFirstGroup()
49 matcher = re.compile('gammu(\d*)')
50 while cont:
51 match = matcher.match(val)
52 if match is not None:
53 index = match.groups(1)[0]
54 if index == '':
55 index = 0
56 else:
57 index = int(index)
58 name = self.config.Read('%s/name' % val, val)
59 self.list.append({'Id': index, 'Name': name, 'Path': val})
60 cont, val, idx = self.config.GetNextGroup(idx)
62 def GetConfigs(self):
63 '''
64 Returns copy of list of configuration settings.
65 '''
66 # we need deep copy here
67 result = []
68 for config in self.list:
69 result.append(config)
70 return result
72 def GetConfig(self, position):
73 '''
74 Returns complete configuration.
75 '''
76 if position == 0:
77 path = 'gammu'
78 else:
79 path = 'gammu%s' % position
80 device = self.config.Read('/%s/port' % path)
81 connection = self.config.Read('/%s/connection' % path)
82 model = self.config.Read('/%s/model' % path)
83 name = self.config.Read('/%s/name' % path)
84 return {
85 'Name': name,
86 'Device': device,
87 'Connection': connection,
88 'Model': model
91 def SetConfig(self, position, device, connection,
92 name = None, model = None):
93 '''
94 Set configuration at defined position.
95 '''
96 found = False
97 if position == 0:
98 path = 'gammu'
99 else:
100 path = 'gammu%s' % position
101 for config in self.list:
102 if config['Id'] == position:
103 path = config['Path']
104 config['Name'] = name
105 found = True
106 break
107 self.config.Write('/%s/port' % path, device)
108 self.config.Write('/%s/connection' % path, connection)
109 if name is not None:
110 self.config.Write('/%s/name' % path, name)
111 if model is not None:
112 self.config.Write('/%s/model' % path, model)
113 if not found:
114 self.list.append({'Id': position, 'Name': name, 'Path': path})
115 self.config.Flush()
117 def FirstFree(self):
119 Find first free entry in configuration file.
121 idxmap = {}
122 first_free = None
123 for config in self.list:
124 idxmap[config['Id']] = 1
125 for i in range(1000):
126 if not idxmap.has_key(i):
127 first_free = i
128 break
129 if first_free is None:
130 raise Exception('Could not find free configuration entry!')
131 return first_free
133 def GetConfigList(self, new = False):
135 Returns list of available configurations as tuple of (details, verbose).
137 lst = []
138 if new:
139 lst.append({
140 'Id': self.FirstFree(),
141 'Path': None,
142 'Name': _('Create new configuration')
144 lst += self.list
146 choices = []
147 for config in lst:
148 # l10n: %(name)s is name of current configuration or 'Create new
149 # configuration', %(position) d is position of this config in .gammurc
150 choices.append(_('%(name)s (position %(position)d)') %
151 {'name': config['Name'], 'position': config['Id']})
152 return lst, choices
154 def SelectConfig(self, parent = None, force = False, new = False):
156 Shows dialog (if needed) to select configuration.
158 lst, choices = self.GetConfigList(new = new)
160 if len(choices) == 1 and not force:
161 return lst[0]['Id']
163 dlg = wx.SingleChoiceDialog(parent,
164 _('Select which configration you want to modify.'),
165 _('Select configuration section'),
166 choices)
167 if dlg.ShowModal() == wx.ID_OK:
168 return lst[dlg.GetSelection()]['Id']
169 else:
170 return None