gajim

view src/common/check_paths.py @ 10773:1076fc9700f5

merge elghinn's branch (roster versioning) to trunk. Fixes #4661, #3190
author Yann Leboulanger <asterix@lagaule.org>
date Fri, 10 Jul 2009 15:05:01 +0200
parents 107bcb358046 b67198acb52f
children b765732974a5 d07ff24ba073
line source
1 # -*- coding:utf-8 -*-
2 ## src/common/check_paths.py
3 ##
4 ## Copyright (C) 2005-2006 Travis Shirk <travis AT pobox.com>
5 ## Nikos Kouremenos <kourem AT gmail.com>
6 ## Copyright (C) 2005-2008 Yann Leboulanger <asterix AT lagaule.org>
7 ## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
8 ## Copyright (C) 2007 Tomasz Melcer <liori AT exroot.org>
9 ## Copyright (C) 2008 Jean-Marie Traissard <jim AT lapin.org>
10 ##
11 ## This file is part of Gajim.
12 ##
13 ## Gajim is free software; you can redistribute it and/or modify
14 ## it under the terms of the GNU General Public License as published
15 ## by the Free Software Foundation; version 3 only.
16 ##
17 ## Gajim is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
21 ##
22 ## You should have received a copy of the GNU General Public License
23 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
24 ##
26 import os
27 import sys
28 import stat
30 import exceptions
31 from common import gajim
32 import logger
34 # DO NOT MOVE ABOVE OF import gajim
35 try:
36 import sqlite3 as sqlite # python 2.5
37 except ImportError:
38 try:
39 from pysqlite2 import dbapi2 as sqlite
40 except ImportError:
41 raise exceptions.PysqliteNotAvailable
43 def create_log_db():
44 print _('creating logs database')
45 con = sqlite.connect(logger.LOG_DB_PATH)
46 os.chmod(logger.LOG_DB_PATH, 0600) # rw only for us
47 cur = con.cursor()
48 # create the tables
49 # kind can be
50 # status, gcstatus, gc_msg, (we only recv for those 3),
51 # single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
52 # to meet all our needs
53 # logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code
54 # jids.jid text column will be JID if TC-related, room_jid if GC-related,
55 # ROOM_JID/nick if pm-related.
56 # also check optparser.py, which updates databases on gajim updates
57 cur.executescript(
58 '''
59 CREATE TABLE jids(
60 jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
61 jid TEXT UNIQUE,
62 type INTEGER
63 );
65 CREATE TABLE unread_messages(
66 message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
67 jid_id INTEGER
68 );
70 CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
72 CREATE TABLE transports_cache (
73 transport TEXT UNIQUE,
74 type INTEGER
75 );
77 CREATE TABLE logs(
78 log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
79 jid_id INTEGER,
80 contact_name TEXT,
81 time INTEGER,
82 kind INTEGER,
83 show INTEGER,
84 message TEXT,
85 subject TEXT
86 );
88 CREATE INDEX idx_logs_jid_id_kind ON logs (jid_id, kind);
90 CREATE TABLE caps_cache (
91 hash_method TEXT,
92 hash TEXT,
93 data BLOB);
95 CREATE TABLE rooms_last_message_time(
96 jid_id INTEGER PRIMARY KEY UNIQUE,
97 time INTEGER
98 );
100 CREATE TABLE IF NOT EXISTS roster_entry(
101 account_jid_id INTEGER,
102 jid_id INTEGER,
103 name TEXT,
104 subscription INTEGER,
105 ask BOOLEAN,
106 PRIMARY KEY (account_jid_id, jid_id)
107 );
109 CREATE TABLE IF NOT EXISTS roster_group(
110 account_jid_id INTEGER,
111 jid_id INTEGER,
112 group_name TEXT,
113 PRIMARY KEY (account_jid_id, jid_id, group_name)
114 );
115 '''
116 )
118 con.commit()
119 con.close()
121 def check_and_possibly_create_paths():
122 LOG_DB_PATH = logger.LOG_DB_PATH
123 VCARD_PATH = gajim.VCARD_PATH
124 AVATAR_PATH = gajim.AVATAR_PATH
125 dot_gajim = os.path.dirname(VCARD_PATH)
126 if os.path.isfile(dot_gajim):
127 print _('%s is a file but it should be a directory') % dot_gajim
128 print _('Gajim will now exit')
129 sys.exit()
130 elif os.path.isdir(dot_gajim):
131 s = os.stat(dot_gajim)
132 if s.st_mode & stat.S_IROTH: # others have read permission!
133 os.chmod(dot_gajim, 0700) # rwx------
135 if not os.path.exists(VCARD_PATH):
136 create_path(VCARD_PATH)
137 elif os.path.isfile(VCARD_PATH):
138 print _('%s is a file but it should be a directory') % VCARD_PATH
139 print _('Gajim will now exit')
140 sys.exit()
142 if not os.path.exists(AVATAR_PATH):
143 create_path(AVATAR_PATH)
144 elif os.path.isfile(AVATAR_PATH):
145 print _('%s is a file but it should be a directory') % AVATAR_PATH
146 print _('Gajim will now exit')
147 sys.exit()
149 if not os.path.exists(LOG_DB_PATH):
150 create_log_db()
151 gajim.logger.init_vars()
152 elif os.path.isdir(LOG_DB_PATH):
153 print _('%s is a directory but should be a file') % LOG_DB_PATH
154 print _('Gajim will now exit')
155 sys.exit()
157 else: # dot_gajim doesn't exist
158 if dot_gajim: # is '' on win9x so avoid that
159 create_path(dot_gajim)
160 if not os.path.isdir(VCARD_PATH):
161 create_path(VCARD_PATH)
162 if not os.path.exists(AVATAR_PATH):
163 create_path(AVATAR_PATH)
164 if not os.path.isfile(LOG_DB_PATH):
165 create_log_db()
166 gajim.logger.init_vars()
168 def create_path(directory):
169 print _('creating %s directory') % directory
170 os.mkdir(directory, 0700)
172 # vim: se ts=3: