2003-10-22 20:45:13 +02:00
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-04-27 01:45:25 +02:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2005-08-23 11:17:48 +02:00
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
2005-04-16 19:03:21 +02:00
|
|
|
import os
|
2005-09-02 23:32:23 +02:00
|
|
|
import sys
|
2005-08-28 12:45:29 +02:00
|
|
|
import locale
|
2005-04-16 19:03:21 +02:00
|
|
|
from common import gajim
|
2005-08-01 11:37:41 +02:00
|
|
|
from common import i18n
|
|
|
|
_ = i18n._
|
2003-10-22 20:45:13 +02:00
|
|
|
|
2005-01-22 21:26:43 +01:00
|
|
|
class OptionsParser:
|
2005-04-27 01:45:25 +02:00
|
|
|
def __init__(self, filename):
|
2005-05-20 20:08:24 +02:00
|
|
|
self.__filename = filename
|
2005-11-14 10:43:27 +01:00
|
|
|
self.old_values = {} # values that are saved in the file and maybe
|
|
|
|
# no longer valid
|
2003-10-22 20:45:13 +02:00
|
|
|
|
2005-04-27 01:45:25 +02:00
|
|
|
def read_line(self, line):
|
2005-05-06 10:33:23 +02:00
|
|
|
index = line.find(' = ')
|
2005-04-27 01:45:25 +02:00
|
|
|
var_str = line[0:index]
|
|
|
|
value_str = line[index + 3:-1]
|
|
|
|
|
|
|
|
i_start = var_str.find('.')
|
|
|
|
i_end = var_str.rfind('.')
|
|
|
|
|
|
|
|
if i_start == -1:
|
2005-11-14 10:43:27 +01:00
|
|
|
self.old_values[var_str] = value_str
|
2005-04-27 01:45:25 +02:00
|
|
|
gajim.config.set(var_str, value_str)
|
|
|
|
else:
|
|
|
|
optname = var_str[0:i_start]
|
|
|
|
key = var_str[i_start + 1:i_end]
|
|
|
|
subname = var_str[i_end + 1:]
|
|
|
|
gajim.config.add_per(optname, key)
|
|
|
|
gajim.config.set_per(optname, key, subname, value_str)
|
|
|
|
|
|
|
|
def read(self):
|
2003-10-22 20:45:13 +02:00
|
|
|
try:
|
2005-04-27 01:45:25 +02:00
|
|
|
fd = open(self.__filename)
|
2003-10-22 20:45:13 +02:00
|
|
|
except:
|
2005-05-06 10:33:23 +02:00
|
|
|
if os.path.exists(self.__filename):
|
2005-08-24 14:38:41 +02:00
|
|
|
#we talk about a file
|
2005-08-23 11:17:48 +02:00
|
|
|
print _('error: cannot open %s for reading') % self.__filename
|
2003-10-22 20:45:13 +02:00
|
|
|
return
|
|
|
|
|
2005-11-14 10:43:27 +01:00
|
|
|
new_version = gajim.config.get('version')
|
2005-04-27 01:45:25 +02:00
|
|
|
for line in fd.readlines():
|
2005-08-28 12:45:29 +02:00
|
|
|
try:
|
|
|
|
line = line.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
line = line.decode(locale.getpreferredencoding())
|
2005-04-27 01:45:25 +02:00
|
|
|
self.read_line(line)
|
2005-11-14 10:43:27 +01:00
|
|
|
old_version = gajim.config.get('version')
|
|
|
|
|
|
|
|
self.update_config(old_version, new_version)
|
|
|
|
self.old_values = {} # clean mem
|
2005-04-16 19:03:21 +02:00
|
|
|
|
2005-04-27 01:45:25 +02:00
|
|
|
fd.close()
|
2005-04-16 19:03:21 +02:00
|
|
|
|
2005-04-27 01:45:25 +02:00
|
|
|
def write_line(self, fd, opt, parents, value):
|
2005-08-26 02:52:44 +02:00
|
|
|
if value == None:
|
|
|
|
return
|
|
|
|
value = value[1]
|
2005-08-26 14:34:11 +02:00
|
|
|
# convert to utf8 before writing to file if needed
|
|
|
|
if isinstance(value, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
value = value.encode('utf-8')
|
|
|
|
else:
|
|
|
|
value = str(value)
|
2005-08-26 14:34:11 +02:00
|
|
|
if isinstance(opt, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
opt = opt.encode('utf-8')
|
2005-05-06 10:33:23 +02:00
|
|
|
s = ''
|
2005-04-27 01:45:25 +02:00
|
|
|
if parents:
|
|
|
|
if len(parents) == 1:
|
|
|
|
return
|
|
|
|
for p in parents:
|
2005-08-26 14:34:11 +02:00
|
|
|
if isinstance(p, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
p = p.encode('utf-8')
|
2005-05-06 10:33:23 +02:00
|
|
|
s += p + '.'
|
2005-04-27 01:45:25 +02:00
|
|
|
s += opt
|
2005-08-26 02:52:44 +02:00
|
|
|
fd.write(s + ' = ' + value + '\n')
|
2005-04-27 01:45:25 +02:00
|
|
|
|
|
|
|
def write(self):
|
2005-08-01 11:37:41 +02:00
|
|
|
(base_dir, filename) = os.path.split(self.__filename)
|
2005-10-21 15:21:31 +02:00
|
|
|
try:
|
|
|
|
base_dir = base_dir.decode(sys.getfilesystemencoding())
|
|
|
|
filename = filename.decode(sys.getfilesystemencoding())
|
|
|
|
except:
|
|
|
|
pass
|
2005-08-13 13:17:49 +02:00
|
|
|
self.__tempfile = os.path.join(base_dir, '.' + filename)
|
2003-10-22 20:45:13 +02:00
|
|
|
try:
|
2005-08-01 11:37:41 +02:00
|
|
|
fd = open(self.__tempfile, 'w')
|
2003-10-22 20:45:13 +02:00
|
|
|
except:
|
2005-08-23 11:17:48 +02:00
|
|
|
#chances are we cannot write file in a directory
|
|
|
|
err_str = _('Unable to write file in %s') % base_dir
|
2005-08-01 11:37:41 +02:00
|
|
|
print err_str
|
|
|
|
return err_str
|
|
|
|
try:
|
|
|
|
gajim.config.foreach(self.write_line, fd)
|
|
|
|
except IOError, e:
|
|
|
|
fd.close()
|
2005-08-09 13:17:32 +02:00
|
|
|
return e.errno
|
2005-04-27 01:45:25 +02:00
|
|
|
fd.close()
|
2005-08-02 22:33:44 +02:00
|
|
|
if os.path.exists(self.__filename):
|
2005-08-03 00:29:25 +02:00
|
|
|
# win32 needs this
|
2005-08-02 22:33:44 +02:00
|
|
|
try:
|
|
|
|
os.remove(self.__filename)
|
|
|
|
except:
|
|
|
|
pass
|
2005-08-01 11:37:41 +02:00
|
|
|
try:
|
|
|
|
os.rename(self.__tempfile, self.__filename)
|
2005-08-09 13:17:32 +02:00
|
|
|
except IOError, e:
|
|
|
|
return e.errno
|
2005-08-23 11:30:54 +02:00
|
|
|
os.chmod(self.__filename, 0600)
|
2005-11-14 10:43:27 +01:00
|
|
|
|
|
|
|
def update_config(self, old_version, new_version):
|
|
|
|
if old_version < '0.9' and new_version == '0.9':
|
|
|
|
self.update_config_x_to_09()
|
|
|
|
|
|
|
|
def update_config_x_to_09(self):
|
|
|
|
# Var name that changed:
|
|
|
|
# avatar_width /height -> chat_avatar_width / height
|
|
|
|
if self.old_values.has_key('avatar_width'):
|
|
|
|
gajim.config.set('chat_avatar_width', self.old_values['avatar_width'])
|
|
|
|
if self.old_values.has_key('avatar_height'):
|
|
|
|
gajim.config.set('chat_avatar_height', self.old_values['avatar_height'])
|
2005-11-19 23:03:58 +01:00
|
|
|
if self.old_values.has_key('use_dbus'):
|
|
|
|
gajim.config.set('remote_control', self.old_values['use_dbus'])
|
2005-11-14 10:43:27 +01:00
|
|
|
# always_compact_view -> always_compact_view_chat / _gc
|
|
|
|
if self.old_values.has_key('always_compact_view'):
|
|
|
|
gajim.config.set('always_compact_view_chat',
|
|
|
|
self.old_values['always_compact_view'])
|
|
|
|
gajim.config.set('always_compact_view_gc',
|
|
|
|
self.old_values['always_compact_view'])
|
|
|
|
# new theme: grocery, plain
|
|
|
|
d = ['accounttextcolor', 'accountbgcolor', 'accountfont',
|
|
|
|
'accountfontattrs', 'grouptextcolor', 'groupbgcolor', 'groupfont',
|
|
|
|
'groupfontattrs', 'contacttextcolor', 'contactbgcolor', 'contactfont',
|
|
|
|
'contactfontattrs', 'bannertextcolor', 'bannerbgcolor']
|
2005-11-14 15:19:53 +01:00
|
|
|
for theme_name in ('grocery', 'plain'):
|
2005-11-14 10:43:27 +01:00
|
|
|
if theme_name not in gajim.config.get_per('themes'):
|
|
|
|
gajim.config.add_per('themes', theme_name)
|
|
|
|
theme = gajim.config.themes_default[theme_name]
|
|
|
|
for o in d:
|
|
|
|
gajim.config.set_per('themes', theme_name, o, theme[d.index(o)])
|
2005-11-14 15:19:53 +01:00
|
|
|
# Remove cyan theme if it's not the current theme
|
2005-11-14 10:43:27 +01:00
|
|
|
if gajim.config.get('roster_theme') != 'cyan' and \
|
|
|
|
'cyan' in gajim.config.get_per('themes'):
|
|
|
|
gajim.config.del_per('themes', 'cyan')
|
|
|
|
# new proxies in accounts.name.file_transfer_proxies
|
|
|
|
for account in gajim.config.get_per('accounts'):
|
|
|
|
proxies = gajim.config.get_per('accounts', account,
|
|
|
|
'file_transfer_proxies')
|
2005-11-14 15:19:53 +01:00
|
|
|
for new in ('proxy.netlab.cz', 'proxy65.jabber.ccc.de',
|
|
|
|
'proxy65.unstable.nl'):
|
2005-11-14 10:43:27 +01:00
|
|
|
if proxies.find(new) < 0:
|
|
|
|
proxies += ', ' + new
|
2005-11-14 15:19:53 +01:00
|
|
|
gajim.config.set_per('accounts', account, 'file_transfer_proxies',
|
2005-11-14 10:43:27 +01:00
|
|
|
proxies)
|
|
|
|
# Add some emots :-* :* >:) >:-) <3
|
|
|
|
for emot in [':-*', ':*', '>:)', '>:-)', '<3']:
|
|
|
|
if emot not in gajim.config.get_per('emoticons'):
|
|
|
|
gajim.config.add_per('emoticons', emot)
|
|
|
|
gajim.config.set_per('emoticons', emot, 'path',
|
|
|
|
gajim.config.emoticons_default[emot])
|
|
|
|
gajim.config.set('version', '0.9')
|