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 import os
27 import sys
28 import stat
29
30 from common import gajim
31 import logger
32
33
34 import sqlite3 as sqlite
35
37 print _('creating logs database')
38 con = sqlite.connect(logger.LOG_DB_PATH)
39 os.chmod(logger.LOG_DB_PATH, 0600)
40 cur = con.cursor()
41
42
43
44
45
46
47
48
49
50 cur.executescript(
51 '''
52 CREATE TABLE jids(
53 jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
54 jid TEXT UNIQUE,
55 type INTEGER
56 );
57
58 CREATE TABLE unread_messages(
59 message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
60 jid_id INTEGER,
61 shown BOOLEAN default 0
62 );
63
64 CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
65
66 CREATE TABLE logs(
67 log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
68 jid_id INTEGER,
69 contact_name TEXT,
70 time INTEGER,
71 kind INTEGER,
72 show INTEGER,
73 message TEXT,
74 subject TEXT
75 );
76
77 CREATE INDEX idx_logs_jid_id_time ON logs (jid_id, time DESC);
78 '''
79 )
80
81 con.commit()
82 con.close()
83
85 print _('creating cache database')
86 con = sqlite.connect(logger.CACHE_DB_PATH)
87 os.chmod(logger.CACHE_DB_PATH, 0600)
88 cur = con.cursor()
89 cur.executescript(
90 '''
91 CREATE TABLE transports_cache (
92 transport TEXT UNIQUE,
93 type INTEGER
94 );
95
96 CREATE TABLE caps_cache (
97 hash_method TEXT,
98 hash TEXT,
99 data BLOB,
100 last_seen INTEGER);
101
102 CREATE TABLE rooms_last_message_time(
103 jid_id INTEGER PRIMARY KEY UNIQUE,
104 time INTEGER
105 );
106
107 CREATE TABLE IF NOT EXISTS roster_entry(
108 account_jid_id INTEGER,
109 jid_id INTEGER,
110 name TEXT,
111 subscription INTEGER,
112 ask BOOLEAN,
113 PRIMARY KEY (account_jid_id, jid_id)
114 );
115
116 CREATE TABLE IF NOT EXISTS roster_group(
117 account_jid_id INTEGER,
118 jid_id INTEGER,
119 group_name TEXT,
120 PRIMARY KEY (account_jid_id, jid_id, group_name)
121 );
122 '''
123 )
124
125 con.commit()
126 con.close()
127
129 print 'spliting database'
130 if os.name == 'nt':
131 try:
132 import configpaths
133 OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse(
134 os.environ[u'appdata']), u'Gajim')
135 except KeyError:
136 OLD_LOG_DB_FOLDER = u'.'
137 else:
138 OLD_LOG_DB_FOLDER = os.path.expanduser(u'~/.gajim')
139
140 tmp = logger.CACHE_DB_PATH
141 logger.CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, 'cache.db')
142 create_cache_db()
143 back = os.getcwd()
144 os.chdir(OLD_LOG_DB_FOLDER)
145 con = sqlite.connect('logs.db')
146 os.chdir(back)
147 cur = con.cursor()
148 cur.execute('''SELECT name FROM sqlite_master WHERE type = 'table';''')
149 tables = cur.fetchall()
150 tables = [t[0] for t in tables]
151 cur.execute("ATTACH DATABASE '%s' AS cache" % logger.CACHE_DB_PATH)
152 for table in ('caps_cache', 'rooms_last_message_time', 'roster_entry',
153 'roster_group', 'transports_cache'):
154 if table not in tables:
155 continue
156 try:
157 cur.executescript(
158 'INSERT INTO cache.%s SELECT * FROM %s;' % (table, table))
159 con.commit()
160 cur.executescript('DROP TABLE %s;' % table)
161 con.commit()
162 except sqlite.OperationalError, e:
163 print >> sys.stderr, 'error moving table %s to cache.db: %s' % \
164 (table, str(e))
165 con.close()
166 logger.CACHE_DB_PATH = tmp
167
169 LOG_DB_PATH = logger.LOG_DB_PATH
170 CACHE_DB_PATH = logger.CACHE_DB_PATH
171 vars = {}
172 vars['VCARD_PATH'] = gajim.VCARD_PATH
173 vars['AVATAR_PATH'] = gajim.AVATAR_PATH
174 vars['MY_EMOTS_PATH'] = gajim.MY_EMOTS_PATH
175 vars['MY_ICONSETS_PATH'] = gajim.MY_ICONSETS_PATH
176 vars['MY_MOOD_ICONSETS_PATH'] = gajim.MY_MOOD_ICONSETS_PATH
177 vars['MY_ACTIVITY_ICONSETS_PATH'] = gajim.MY_ACTIVITY_ICONSETS_PATH
178 import configpaths
179 MY_DATA = configpaths.gajimpaths['MY_DATA']
180 MY_CONFIG = configpaths.gajimpaths['MY_CONFIG']
181 MY_CACHE = configpaths.gajimpaths['MY_CACHE']
182
183 if os.path.exists(LOG_DB_PATH):
184
185 return
186
187 if os.name == 'nt':
188 try:
189 OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse(
190 os.environ[u'appdata']), u'Gajim')
191 except KeyError:
192 OLD_LOG_DB_FOLDER = u'.'
193 else:
194 OLD_LOG_DB_FOLDER = os.path.expanduser(u'~/.gajim')
195 if not os.path.exists(OLD_LOG_DB_FOLDER):
196 return
197 OLD_LOG_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, u'logs.db')
198 OLD_CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, u'cache.db')
199 vars['OLD_VCARD_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'vcards')
200 vars['OLD_AVATAR_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'avatars')
201 vars['OLD_MY_EMOTS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'emoticons')
202 vars['OLD_MY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'iconsets')
203 vars['OLD_MY_MOOD_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'moods')
204 vars['OLD_MY_ACTIVITY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER,
205 u'activities')
206 OLD_CONFIG_FILES = []
207 OLD_DATA_FILES = []
208 for f in os.listdir(OLD_LOG_DB_FOLDER):
209 if f == 'config' or f.startswith('config.'):
210 OLD_CONFIG_FILES.append(f)
211 if f == 'secrets' or f.startswith('secrets.'):
212 OLD_DATA_FILES.append(f)
213 if f == 'cacerts.pem':
214 OLD_DATA_FILES.append(f)
215
216 if not os.path.exists(OLD_LOG_DB_PATH):
217 return
218
219 if not os.path.exists(OLD_CACHE_DB_PATH):
220
221 split_db()
222
223 to_move = {}
224 to_move[OLD_LOG_DB_PATH] = LOG_DB_PATH
225 to_move[OLD_CACHE_DB_PATH] = CACHE_DB_PATH
226
227 for folder in ('VCARD_PATH', 'AVATAR_PATH', 'MY_EMOTS_PATH',
228 'MY_ICONSETS_PATH', 'MY_MOOD_ICONSETS_PATH', 'MY_ACTIVITY_ICONSETS_PATH'):
229 src = vars['OLD_' + folder]
230 dst = vars[folder]
231 to_move[src] = dst
232
233
234 for f in OLD_CONFIG_FILES:
235 src = os.path.join(OLD_LOG_DB_FOLDER, f)
236 dst = os.path.join(MY_CONFIG, f)
237 to_move[src] = dst
238
239
240 for f in OLD_DATA_FILES:
241 src = os.path.join(OLD_LOG_DB_FOLDER, f)
242 dst = os.path.join(MY_DATA, f)
243 to_move[src] = dst
244
245 for src, dst in to_move.items():
246 if os.path.exists(dst):
247 continue
248 if not os.path.exists(src):
249 continue
250 print 'moving %s to %s' % (src, dst)
251 os.renames(src, dst)
252 gajim.logger.init_vars()
253 gajim.logger.attach_cache_database()
254
256 check_and_possibly_move_config()
257
258 LOG_DB_PATH = logger.LOG_DB_PATH
259 LOG_DB_FOLDER, LOG_DB_FILE = os.path.split(LOG_DB_PATH)
260
261 CACHE_DB_PATH = logger.CACHE_DB_PATH
262 CACHE_DB_FOLDER, CACHE_DB_FILE = os.path.split(CACHE_DB_PATH)
263
264 VCARD_PATH = gajim.VCARD_PATH
265 AVATAR_PATH = gajim.AVATAR_PATH
266 import configpaths
267 MY_DATA = configpaths.gajimpaths['MY_DATA']
268 MY_CONFIG = configpaths.gajimpaths['MY_CONFIG']
269 MY_CACHE = configpaths.gajimpaths['MY_CACHE']
270
271 PLUGINS_CONFIG_PATH = gajim.PLUGINS_CONFIG_DIR
272
273 if not os.path.exists(MY_DATA):
274 create_path(MY_DATA)
275 elif os.path.isfile(MY_DATA):
276 print _('%s is a file but it should be a directory') % MY_DATA
277 print _('Gajim will now exit')
278 sys.exit()
279
280 if not os.path.exists(MY_CONFIG):
281 create_path(MY_CONFIG)
282 elif os.path.isfile(MY_CONFIG):
283 print _('%s is a file but it should be a directory') % MY_CONFIG
284 print _('Gajim will now exit')
285 sys.exit()
286
287 if not os.path.exists(MY_CACHE):
288 create_path(MY_CACHE)
289 elif os.path.isfile(MY_CACHE):
290 print _('%s is a file but it should be a directory') % MY_CACHE
291 print _('Gajim will now exit')
292 sys.exit()
293
294 if not os.path.exists(VCARD_PATH):
295 create_path(VCARD_PATH)
296 elif os.path.isfile(VCARD_PATH):
297 print _('%s is a file but it should be a directory') % VCARD_PATH
298 print _('Gajim will now exit')
299 sys.exit()
300
301 if not os.path.exists(AVATAR_PATH):
302 create_path(AVATAR_PATH)
303 elif os.path.isfile(AVATAR_PATH):
304 print _('%s is a file but it should be a directory') % AVATAR_PATH
305 print _('Gajim will now exit')
306 sys.exit()
307
308 if not os.path.exists(LOG_DB_FOLDER):
309 create_path(LOG_DB_FOLDER)
310 elif os.path.isfile(LOG_DB_FOLDER):
311 print _('%s is a file but it should be a directory') % LOG_DB_FOLDER
312 print _('Gajim will now exit')
313 sys.exit()
314
315 if not os.path.exists(LOG_DB_PATH):
316 create_log_db()
317 gajim.logger.init_vars()
318 elif os.path.isdir(LOG_DB_PATH):
319 print _('%s is a directory but should be a file') % LOG_DB_PATH
320 print _('Gajim will now exit')
321 sys.exit()
322
323 if not os.path.exists(CACHE_DB_FOLDER):
324 create_path(CACHE_DB_FOLDER)
325 elif os.path.isfile(CACHE_DB_FOLDER):
326 print _('%s is a file but it should be a directory') % CACHE_DB_FOLDER
327 print _('Gajim will now exit')
328 sys.exit()
329
330 if not os.path.exists(CACHE_DB_PATH):
331 create_cache_db()
332 gajim.logger.attach_cache_database()
333 elif os.path.isdir(CACHE_DB_PATH):
334 print _('%s is a directory but should be a file') % CACHE_DB_PATH
335 print _('Gajim will now exit')
336 sys.exit()
337
338 if not os.path.exists(PLUGINS_CONFIG_PATH):
339 create_path(PLUGINS_CONFIG_PATH)
340 elif os.path.isfile(PLUGINS_CONFIG_PATH):
341 print _('%s is a file but it should be a directory') % PLUGINS_CONFIG_PATH
342 print _('Gajim will now exit')
343 sys.exit()
344
346 head, tail = os.path.split(directory)
347 if not os.path.exists(head):
348 create_path(head)
349 if os.path.exists(directory):
350 return
351 print _('creating %s directory') % directory
352 os.mkdir(directory, 0700)
353