Okay, it is still needed.
[wammu.git] / Wammu / WammuSettings.py
blobe587212635f1c50190d053da5db63b4f6de01052
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Config handler wrapper with various defaults, which might be platform dependant.
7 @var DEFAULT_CONFIG: Dictionary of default values.
8 @var EXPANDABLE_CONFIGS: List of variables where path expansion should happen.
9 '''
10 __author__ = 'Michal Čihař'
11 __email__ = 'michal@cihar.com'
12 __license__ = '''
13 Copyright © 2003 - 2008 Michal Čihař
15 This program is free software; you can redistribute it and/or modify it
16 under the terms of the GNU General Public License version 2 as published by
17 the Free Software Foundation.
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 more details.
24 You should have received a copy of the GNU General Public License along with
25 this program; if not, write to the Free Software Foundation, Inc.,
26 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 '''
29 import os
30 import wx
31 import Wammu.GammuSettings
32 import Wammu.OSUtils
34 DEFAULT_CONFIG = {
35 '/Main/X': 0,
36 '/Main/Y': 0,
37 '/Main/Split': 160,
38 '/Main/SplitRight': -200,
39 '/Main/Width': 640,
40 '/Main/Height': 480,
41 '/Defaults/SearchType': 0,
42 '/Defaults/Type-contact-MemoryType': 'SM',
43 '/Defaults/Type-calendar-Type': 'MEETING',
44 '/Defaults/Type-todo-Priority': 'Medium',
45 '/Defaults/Entry-contact-0': 'Text_Name',
46 '/Defaults/Entry-contact-1': 'Number_General',
47 '/Defaults/Entry-todo-0': 'TEXT',
48 '/Defaults/Entry-calendar-0': 'TEXT',
49 '/Defaults/Entry-calendar-1': 'START_DATETIME',
50 '/Defaults/Entry-calendar-2': 'END_DATETIME',
51 '/Wammu/AutoConnect': 'no',
52 '/Gammu/LockDevice': 'no',
53 '/Debug/Show': 'no',
54 '/Wammu/PhonePrefix': 'Auto',
55 '/Wammu/LastPhonePrefix': '',
56 '/Wammu/RefreshState': 30000,
57 '/Debug/X': 0,
58 '/Debug/Y': 0,
59 '/Debug/Width': 400,
60 '/Debug/Height': 200,
61 '/Message/ScaleImage': 1,
62 '/Message/Format': 'yes',
63 '/Message/Concatenated': 'yes',
64 '/Message/Unicode': 'no',
65 '/Message/DeliveryReport': 'no',
66 '/Message/16bitId': 'yes',
67 '/Gammu/SyncTime': 'yes',
68 '/Gammu/StartInfo': 'no',
69 '/Wammu/ConfirmDelete': 'yes',
70 '/Wammu/DefaultTime': '09:00:00',
71 '/Wammu/DefaultDateOffset': 1,
72 '/Wammu/DefaultEntries': 3,
73 '/Wammu/FirstRun': -1,
74 '/Wammu/RunCounter': 0,
75 '/Wammu/TalkbackDone': 'no',
76 '/Wammu/NameFormat': 'auto',
77 '/Wammu/NameFormatString': '%(FirstName)s %(LastName)s (%(Company)s)',
78 '/IMAP/Server': '',
79 '/IMAP/Login': '',
80 '/IMAP/Password': '',
81 '/MesageExport/From': 'Wammu <wammu@wammu.sms>',
82 '/Gammu/Section': 0,
83 '/User/Name': Wammu.OSUtils.GetUserFullName(),
84 '/Gammu/Gammurc': os.path.join(u'~', u'.gammurc'),
85 '/Hacks/MaxEmptyGuess': 50,
86 '/Hacks/MaxEmptyKnown': 100,
90 EXPANDABLE_CONFIGS = [
91 '/Gammu/Gammurc',
94 class WammuConfig:
95 '''
96 Wrapper class for wx.Config, which handles automatically defaults
97 and allows some automatic conversion of read values (like expanding
98 ~ in path).
99 '''
100 def __init__(self):
101 # We don't want to subclass from wx.Config to hide it's API
102 self.cfg = wx.Config(appName = 'Wammu',
103 style = wx.CONFIG_USE_LOCAL_FILE)
104 self.gammu = None
105 self.InitGammu()
107 def Flush(self):
108 self.cfg.Flush()
110 def InitGammu(self, path = None):
112 Initializes gammu configuration as sub part of this class.
114 self.gammu = Wammu.GammuSettings.GammuSettings(self, path)
116 def Read(self, path, expand = True):
118 Reads string option from configuration.
120 try:
121 result = self.cfg.Read(path, DEFAULT_CONFIG[path])
122 except KeyError:
123 # Following line is for debugging purposes only
124 #print 'Warning: no default value for %s' % path
125 result = self.cfg.Read(path, '')
126 if expand and path in EXPANDABLE_CONFIGS:
127 result = Wammu.OSUtils.ExpandPath(result)
128 return result
130 def ReadInt(self, path):
132 Reads integer option from configuration.
134 try:
135 result = self.cfg.ReadInt(path, DEFAULT_CONFIG[path])
136 except KeyError:
137 # Following line is for debugging purposes only
138 #print 'Warning: no default value for %s' % path
139 result = self.cfg.ReadInt(path, 0)
140 return result
142 def ReadFloat(self, path):
144 Reads float option from configuration.
146 try:
147 result = self.cfg.ReadFloat(path, DEFAULT_CONFIG[path])
148 except KeyError:
149 # Following line is for debugging purposes only
150 #print 'Warning: no default value for %s' % path
151 result = self.cfg.ReadFloat(path, 0)
152 return result
154 def Write(self, path, value):
156 Writes string option to configuration.
158 self.cfg.Write(path, value)
160 def WriteInt(self, path, value):
162 Writes integer option to configuration.
164 self.cfg.WriteInt(path, value)
166 def WriteFloat(self, path, value):
168 Writes float option to configuration.
170 self.cfg.WriteFloat(path, value)
172 def HasEntry(self, path):
174 Checks whether configuration has some entry.
176 return self.cfg.HasEntry(path)