67e111b681fac2c27dad8ca54f0a2a3b6a1ddaa6
[drlaunch] / src / config.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of wifieye.
7 #
8 # wifieye is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # wifieye is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with wifieye.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 import os
26 import pickle
27
28 version = "0.2"
29
30 try:
31     from glib import get_user_config_dir
32 except:
33     def get_user_config_dir():
34         home=os.environ['HOME']
35         if home=='':
36             home="/home/user"
37         cfg="%s/.config" % (home)
38
39         return(cfg)
40
41 size = (2,2)
42 iconsize = 64
43 iconspace = 42
44 apps=None
45 indiv=False
46 id=None         # Applet ID
47 longpress=True
48
49 maxsz=(4,4)
50
51 def setSize(sz):
52     global size
53
54     size=sz
55
56 def getSize():
57     global size
58
59     return(size)
60
61 def getMaxSize():
62     global maxsz
63
64     return(maxsz)
65
66 def setIndiv(_indiv):
67     global indiv
68
69     indiv=_indiv
70
71 def getIndiv():
72     global indiv
73
74     return(indiv)
75
76 def setLongpress(_lp):
77     global longpress
78
79     longpress=_lp
80
81 def getLongpress():
82     global longpress
83
84     return(longpress)
85
86 def setApps(aps):
87     """ apps is a dictionary of (x,y)=>appname """
88     global apps
89
90     apps=aps
91
92 def getApps():
93     global apps
94
95     if apps==None:
96         tmp={
97             (0,0):  'rtcom-call-ui',
98             (0,1):  'rtcom-messaging-ui',
99             (1,0):  'browser',
100             (1,1):  'osso-addressbook',
101             }
102         setApps(tmp)
103
104     return(apps)
105
106 def ensure_dir():
107     dir0=get_user_config_dir()
108     dir=dir0+'/drlaunch'
109     if not os.path.exists(dir):
110         os.mkdir(dir)
111     if not os.path.isdir(dir):
112         raise Exception('Failed to ensure directory' + dir)
113
114     return(dir)
115
116 def get_config_fn():
117     dir=ensure_dir()
118     ret=dir + '/config'
119
120     return(ret)
121
122 def save():
123     global id
124
125     check_init()
126
127     dt=load_all()
128
129     if dt==None:
130         dt={
131             'version':  3,
132             'data':     {},
133             }
134
135     dt['data'][id]={
136         'size':         getSize(),
137         'apps':         getApps(),
138         'indiv':        getIndiv(),
139         'longpress':    getLongpress(),
140         }
141
142     fn=get_config_fn()
143
144     st=pickle.dumps(dt)
145     f=file(fn, 'w')
146     f.write(st)
147     f.close()
148
149 def parse_v1(dt0):
150     """ Convert a v1 config to v2 """
151     global id
152
153     ret={
154         'version':  2,
155         'data':     {},
156         }
157
158     ret['data'][id]={
159         'size':     dt0['size'],
160         'apps':     dt0['apps'],
161         }
162
163     return(ret)
164
165 def parse_v2(dt):
166     global id
167
168     # Perhaps copy dt?
169
170     dt['version']=3
171
172     for i in dt['data']:
173         dt['data'][i]['indiv']=False
174         dt['data'][i]['size']=(dt['data'][i]['size'], dt['data'][i]['size'])
175         dt['data'][i]['longpress']=True
176
177     return(dt)
178
179 def load_all():
180     fn=get_config_fn()
181
182     try:
183         f=file(fn, 'r')
184         st=f.read()
185         f.close()
186         ret=pickle.loads(st)
187     except:
188         ret=None
189
190     return(ret)
191
192 def load():
193     global id
194
195     check_init()
196
197     fn=get_config_fn()
198
199     dt0=load_all()
200     if dt0==None:
201         return
202
203     if dt0['version']==1:
204         dt0=parse_v1(dt0)
205
206     if dt0['version']==2:
207         dt0=parse_v2(dt0)
208
209     if not dt0['data'].has_key(id):
210         return
211
212     dt=dt0['data'][id]
213
214     setSize(dt['size'])
215     setApps(dt['apps'])
216     setIndiv(dt['indiv'])
217     setLongpress(dt['longpress'])
218
219 def init(_id):
220     global id
221
222     id=_id
223
224 def check_init():
225     global id
226
227     if id==None:
228         import sys
229
230         print "config.init() not done"
231         sys.exit(1)
232
233 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
234