Translated using Weblate.
[wammu.git] / Wammu / GammuSettings.py
blob04c9f72c63c0986811655605cdfa75597a01b083
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/device' % path)
81 if device == '':
82 device = self.config.Read('/%s/port' % path)
83 connection = self.config.Read('/%s/connection' % path)
84 model = self.config.Read('/%s/model' % path)
85 name = self.config.Read('/%s/name' % path)
86 return {
87 'Name': name,
88 'Device': device,
89 'Connection': connection,
90 'Model': model
93 def SetConfig(self, position, device, connection,
94 name = None, model = None):
95 '''
96 Set configuration at defined position.
97 '''
98 found = False
99 if position == 0:
100 path = 'gammu'
101 else:
102 path = 'gammu%s' % position
103 for config in self.list:
104 if config['Id'] == position:
105 path = config['Path']
106 config['Name'] = name
107 found = True
108 break
109 self.config.Write('/%s/port' % path, device)
110 self.config.Write('/%s/connection' % path, connection)
111 if name is not None:
112 self.config.Write('/%s/name' % path, name)
113 if model is not None:
114 self.config.Write('/%s/model' % path, model)
115 if not found:
116 self.list.append({'Id': position, 'Name': name, 'Path': path})
117 self.config.Flush()
119 def FirstFree(self):
121 Find first free entry in configuration file.
123 idxmap = {}
124 first_free = None
125 for config in self.list:
126 idxmap[config['Id']] = 1
127 for i in range(1000):
128 if not idxmap.has_key(i):
129 first_free = i
130 break
131 if first_free is None:
132 raise Exception('Could not find free configuration entry!')
133 return first_free
135 def GetConfigList(self, new = False):
137 Returns list of available configurations as tuple of (details, verbose).
139 lst = []
140 if new:
141 lst.append({
142 'Id': self.FirstFree(),
143 'Path': None,
144 'Name': _('Create new configuration')
146 lst += self.list
148 choices = []
149 for config in lst:
150 # l10n: %(name)s is name of current configuration or 'Create new
151 # configuration', %(position) d is position of this config in .gammurc
152 choices.append(_('%(name)s (position %(position)d)') %
153 {'name': config['Name'], 'position': config['Id']})
154 return lst, choices
156 def SelectConfig(self, parent = None, force = False, new = False):
158 Shows dialog (if needed) to select configuration.
160 lst, choices = self.GetConfigList(new = new)
162 if len(choices) == 1 and not force:
163 return lst[0]['Id']
165 dlg = wx.SingleChoiceDialog(parent,
166 _('Select which configration you want to modify.'),
167 _('Select configuration section'),
168 choices)
169 if dlg.ShowModal() == wx.ID_OK:
170 return lst[dlg.GetSelection()]['Id']
171 else:
172 return None