Fixed bug #952533: Invalid file name characters in song name causes OpenLyrics to crash.

bzr-revno: 1916
Fixes: https://launchpad.net/bugs/952533
This commit is contained in:
Philip Ridout 2012-03-23 13:09:51 +02:00 committed by Raoul Snyman
commit b4e8b48c6d
2 changed files with 18 additions and 4 deletions

View File

@ -53,6 +53,8 @@ APPLICATION_VERSION = {}
IMAGES_FILTER = None IMAGES_FILTER = None
UNO_CONNECTION_TYPE = u'pipe' UNO_CONNECTION_TYPE = u'pipe'
#UNO_CONNECTION_TYPE = u'socket' #UNO_CONNECTION_TYPE = u'socket'
CONTROL_CHARS = re.compile(r'[\x00-\x1F\x7F-\x9F]', re.UNICODE)
INVALID_FILE_CHARS = re.compile(r'[\\/:\*\?"<>\|\+\[\]%]', re.UNICODE)
VERSION_SPLITTER = re.compile(r'([0-9]+).([0-9]+).([0-9]+)(?:-bzr([0-9]+))?') VERSION_SPLITTER = re.compile(r'([0-9]+).([0-9]+).([0-9]+)(?:-bzr([0-9]+))?')
class VersionThread(QtCore.QThread): class VersionThread(QtCore.QThread):
@ -400,7 +402,7 @@ def clean_filename(filename):
""" """
if not isinstance(filename, unicode): if not isinstance(filename, unicode):
filename = unicode(filename, u'utf-8') filename = unicode(filename, u'utf-8')
return re.sub(r'[/\\?*|<>\[\]":<>+%\n]+', u'_', filename).strip(u'_') return INVALID_FILE_CHARS.sub(u'_', CONTROL_CHARS.sub(u'', filename))
def delete_file(file_path_name): def delete_file(file_path_name):
""" """

View File

@ -29,6 +29,7 @@ import re
from PyQt4 import QtGui from PyQt4 import QtGui
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.core.utils import CONTROL_CHARS
from db import Author from db import Author
from ui import SongStrings from ui import SongStrings
@ -256,6 +257,13 @@ def clean_string(string):
Strips punctuation from the passed string to assist searching Strips punctuation from the passed string to assist searching
""" """
return WHITESPACE.sub(u' ', APOSTROPHE.sub(u'', string)).lower() return WHITESPACE.sub(u' ', APOSTROPHE.sub(u'', string)).lower()
def clean_title(title):
"""
Cleans the song title by removing Unicode control chars groups C0 & C1,
as well as any trailing spaces
"""
return CONTROL_CHARS.sub(u'', title).rstrip()
def clean_song(manager, song): def clean_song(manager, song):
""" """
@ -275,10 +283,14 @@ def clean_song(manager, song):
song.alternate_title = unicode(song.alternate_title) song.alternate_title = unicode(song.alternate_title)
if isinstance(song.lyrics, buffer): if isinstance(song.lyrics, buffer):
song.lyrics = unicode(song.lyrics) song.lyrics = unicode(song.lyrics)
song.title = song.title.rstrip() if song.title else u'' if song.title:
if song.alternate_title is None: song.title = clean_title(song.title)
else:
song.title = u''
if song.alternate_title:
song.alternate_title = clean_title(song.alternate_title)
else:
song.alternate_title = u'' song.alternate_title = u''
song.alternate_title = song.alternate_title.strip()
song.search_title = clean_string(song.title) + u'@' + \ song.search_title = clean_string(song.title) + u'@' + \
clean_string(song.alternate_title) clean_string(song.alternate_title)
# Only do this, if we the song is a 1.9.4 song (or older). # Only do this, if we the song is a 1.9.4 song (or older).