From fd01f7aa4da52ddda0ac62eaff404d69e974dd01 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Sun, 10 May 2009 19:32:57 +0000
Subject: [PATCH] [brubru] better sound file managment, Fixes #5016

---
 configure.ac            |  2 +-
 src/common/config.py    | 18 ++++++++---------
 src/common/defs.py      |  2 +-
 src/common/helpers.py   | 43 ++++++++++++++++++++++++++++++++++++++++-
 src/common/optparser.py | 14 ++++++++++++++
 src/config.py           |  5 +----
 src/dialogs.py          |  3 +++
 7 files changed, 71 insertions(+), 16 deletions(-)

diff --git a/configure.ac b/configure.ac
index 9d4a608bdb..b4e09eeb08 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
 AC_INIT([Gajim - A Jabber Instant Messager],
-		[0.12.1.4-svn],[http://trac.gajim.org/],[gajim])
+		[0.12.1.5-svn],[http://trac.gajim.org/],[gajim])
 AC_PREREQ([2.59])
 
 AC_CONFIG_HEADER(config.h)
diff --git a/src/common/config.py b/src/common/config.py
index 55da5b69a7..6796a83a26 100644
--- a/src/common/config.py
+++ b/src/common/config.py
@@ -457,15 +457,15 @@ class Config:
 	}
 
 	soundevents_default = {
-		'first_message_received': [ True, '../data/sounds/message1.wav' ],
-		'next_message_received_focused': [ True, '../data/sounds/message2.wav' ],
-		'next_message_received_unfocused': [ True, '../data/sounds/message2.wav' ],
-		'contact_connected': [ True, '../data/sounds/connected.wav' ],
-		'contact_disconnected': [ True, '../data/sounds/disconnected.wav' ],
-		'message_sent': [ True, '../data/sounds/sent.wav' ],
-		'muc_message_highlight': [ True, '../data/sounds/gc_message1.wav', _('Sound to play when a group chat message contains one of the words in muc_highlight_words, or when a group chat message contains your nickname.')],
-		'muc_message_received': [ False, '../data/sounds/gc_message2.wav', _('Sound to play when any MUC message arrives.') ],
-		'gmail_received': [ False, '../data/sounds/message1.wav' ],
+		'first_message_received': [ True, 'message1.wav' ],
+		'next_message_received_focused': [ True, 'message2.wav' ],
+		'next_message_received_unfocused': [ True, 'message2.wav' ],
+		'contact_connected': [ True, 'connected.wav' ],
+		'contact_disconnected': [ True, 'disconnected.wav' ],
+		'message_sent': [ True, 'sent.wav' ],
+		'muc_message_highlight': [ True, 'gc_message1.wav', _('Sound to play when a group chat message contains one of the words in muc_highlight_words, or when a group chat message contains your nickname.')],
+		'muc_message_received': [ False, 'gc_message2.wav', _('Sound to play when any MUC message arrives.') ],
+		'gmail_received': [ False, 'message1.wav' ],
 	}
 
 	themes_default = {
diff --git a/src/common/defs.py b/src/common/defs.py
index ec4bf784ac..29101e0c9f 100644
--- a/src/common/defs.py
+++ b/src/common/defs.py
@@ -27,7 +27,7 @@ docdir = '../'
 datadir = '../'
 localedir = '../po'
 
-version = '0.12.1.4-svn'
+version = '0.12.1.5-svn'
 
 import sys, os.path
 for base in ('.', 'common'):
diff --git a/src/common/helpers.py b/src/common/helpers.py
index 6bd9c4ccdf..fff0dde052 100644
--- a/src/common/helpers.py
+++ b/src/common/helpers.py
@@ -773,11 +773,52 @@ def play_sound(event):
 	path_to_soundfile = gajim.config.get_per('soundevents', event, 'path')
 	play_sound_file(path_to_soundfile)
 
+def check_soundfile_path(file,
+								 dirs=(gajim.gajimpaths.root, gajim.DATA_DIR)):
+	'''Check if the sound file exists.
+	:param file: the file to check, absolute or relative to 'dirs' path
+	:param dirs: list of knows paths to fallback if the file doesn't exists
+					 (eg: ~/.gajim/sounds/, DATADIR/sounds...).
+	:return      the path to file or None if it doesn't exists.'''
+	if not file:
+		return None
+	elif os.path.exists(file):
+		return file
+
+	for d in dirs:
+		d = os.path.join(d, 'sounds', file)
+		if os.path.exists(d):
+			return d
+	return None
+
+def strip_soundfile_path(file,
+								 dirs=(gajim.gajimpaths.root, gajim.DATA_DIR),
+								 abs=True):
+	'''Remove knowns paths from a sound file:
+	Filechooser returns absolute path. If path is a known fallback path, we remove it.
+	So config have no hardcoded path	to DATA_DIR and text in textfield is shorther.
+	param: file: the filename to strip.
+	param: dirs: list of knowns paths from which the filename should be stripped.
+	param:  abs: force absolute path on dirs
+	'''
+	if not file:
+		return None
+
+	name = os.path.basename(file)
+	for d in dirs:
+		d = os.path.join(d, 'sounds', name)
+		if abs:
+			d = os.path.abspath(d)
+		if file == d:
+			return name
+	return file
+
 def play_sound_file(path_to_soundfile):
 	if path_to_soundfile == 'beep':
 		exec_command('beep')
 		return
-	if path_to_soundfile is None or not os.path.exists(path_to_soundfile):
+	path_to_soundfile = check_soundfile_path(path_to_soundfile)
+	if path_to_soundfile is None:
 		return
 	if sys.platform == 'darwin':
 		try:
diff --git a/src/common/optparser.py b/src/common/optparser.py
index 13e6a03f4a..9753305181 100644
--- a/src/common/optparser.py
+++ b/src/common/optparser.py
@@ -30,6 +30,7 @@ import os
 import locale
 import re
 from common import gajim
+from common import helpers
 
 import exceptions
 try:
@@ -195,6 +196,8 @@ class OptionsParser:
 			self.update_config_to_01213()
 		if old < [0, 12, 1, 4] and new >= [0, 12, 1, 4]:
 			self.update_config_to_01214()
+		if old < [0, 12, 1, 5] and new >= [0, 12, 1, 5]:
+			self.update_config_to_01215()
 
 		gajim.logger.init_vars()
 		gajim.config.set('version', new_version)
@@ -657,4 +660,15 @@ class OptionsParser:
 				self.old_values['last_status_msg_' + status])
 		gajim.config.set('version', '0.12.1.4')
 
+	def update_config_to_01215(self):
+		'''Remove hardcoded ../data/sounds from config'''
+		dirs = ('../data', gajim.gajimpaths.root, gajim.DATA_DIR)
+		for evt in gajim.config.get_per('soundevents'):
+			path = gajim.config.get_per('soundevents', evt ,'path')
+			# absolute and relative passes are necessary
+			path = helpers.strip_soundfile_path(path, dirs, abs=False)
+			path = helpers.strip_soundfile_path(path, dirs, abs=True)
+			gajim.config.set_per('soundevents', evt, 'path', path)
+		gajim.config.set('version', '0.12.1.5')
+
 # vim: se ts=3:
diff --git a/src/config.py b/src/config.py
index 9b89dbfe39..9894721524 100644
--- a/src/config.py
+++ b/src/config.py
@@ -3647,6 +3647,7 @@ class ManageSoundsWindow:
 				return
 			directory = os.path.dirname(path_to_snd_file)
 			gajim.config.set('last_sounds_dir', directory)
+			path_to_snd_file = helpers.strip_soundfile_path(path_to_snd_file)
 			self.xml.get_widget('sounds_entry').set_text(path_to_snd_file)
 
 			model[iter_][2] = path_to_snd_file # set new path to sounds_model
@@ -3654,12 +3655,8 @@ class ManageSoundsWindow:
 
 		def on_cancel(widget):
 			self.dialog.destroy()
-			model, iter_ = self.sound_tree.get_selection().get_selected()
-			model[iter_][2] = ''
-			model[iter_][0] = False
 
 		path_to_snd_file = model[iter_][2].decode('utf-8')
-		path_to_snd_file = os.path.join(os.getcwd(), path_to_snd_file)
 		self.dialog = dialogs.SoundChooserDialog(path_to_snd_file, on_ok,
 			on_cancel)
 
diff --git a/src/dialogs.py b/src/dialogs.py
index 400057c041..cd056776d6 100644
--- a/src/dialogs.py
+++ b/src/dialogs.py
@@ -3165,7 +3165,10 @@ class SoundChooserDialog(FileChooserDialog):
 		self.add_filter(filter_)
 		self.set_filter(filter_)
 
+		path_to_snd_file = helpers.check_soundfile_path(path_to_snd_file)
 		if path_to_snd_file:
+			# set_filename accept only absolute path
+			path_to_snd_file = os.path.abspath(path_to_snd_file)
 			self.set_filename(path_to_snd_file)
 
 class ImageChooserDialog(FileChooserDialog):
-- 
GitLab