openlp/openlp/core/ui/exceptionform.py

253 lines
11 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-12-29 09:35:24 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
# --------------------------------------------------------------------------- #
# 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 of the License. #
# #
# 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. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2013-02-01 19:58:18 +00:00
"""
The actual exception dialog form.
"""
2010-12-21 19:39:35 +00:00
import logging
import os
2010-12-11 00:26:41 +00:00
import platform
2016-04-04 19:53:54 +00:00
import re
2013-06-26 18:54:21 +00:00
import bs4
2010-12-11 00:26:41 +00:00
import sqlalchemy
2016-04-04 19:53:54 +00:00
from PyQt5 import Qt, QtCore, QtGui, QtWebKit, QtWidgets
from lxml import etree
2013-07-19 20:56:31 +00:00
try:
import migrate
2013-08-31 18:17:38 +00:00
MIGRATE_VERSION = getattr(migrate, '__version__', '< 0.7')
except ImportError:
2013-08-31 18:17:38 +00:00
MIGRATE_VERSION = '-'
try:
import chardet
CHARDET_VERSION = chardet.__version__
except ImportError:
2013-08-31 18:17:38 +00:00
CHARDET_VERSION = '-'
try:
import enchant
ENCHANT_VERSION = enchant.__version__
except ImportError:
2013-08-31 18:17:38 +00:00
ENCHANT_VERSION = '-'
try:
import mako
MAKO_VERSION = mako.__version__
except ImportError:
2013-08-31 18:17:38 +00:00
MAKO_VERSION = '-'
try:
import icu
try:
2013-04-16 16:37:26 +00:00
ICU_VERSION = icu.VERSION
except AttributeError:
2013-08-31 18:17:38 +00:00
ICU_VERSION = 'OK'
except ImportError:
2013-08-31 18:17:38 +00:00
ICU_VERSION = '-'
2011-12-12 22:10:37 +00:00
try:
WEBKIT_VERSION = QtWebKit.qWebKitVersion()
except AttributeError:
2013-08-31 18:17:38 +00:00
WEBKIT_VERSION = '-'
2013-03-14 10:46:19 +00:00
try:
from openlp.core.ui.media.vlcplayer import VERSION
VLC_VERSION = VERSION
except ImportError:
2013-08-31 18:17:38 +00:00
VLC_VERSION = '-'
2011-12-12 22:10:37 +00:00
from openlp.core.common import RegistryProperties, Settings, UiStrings, is_linux, translate
2016-04-04 19:53:54 +00:00
from openlp.core.common.versionchecker import get_application_version
from openlp.core.ui.lib.filedialog import FileDialog
2013-08-31 18:17:38 +00:00
from .exceptiondialog import Ui_ExceptionDialog
2010-12-21 19:39:35 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
2015-11-07 00:49:40 +00:00
class ExceptionForm(QtWidgets.QDialog, Ui_ExceptionDialog, RegistryProperties):
"""
The exception dialog
"""
2013-07-19 20:56:31 +00:00
def __init__(self):
2013-02-01 19:58:18 +00:00
"""
Constructor.
"""
2016-01-09 16:26:14 +00:00
super(ExceptionForm, self).__init__(None, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint)
self.setupUi(self)
2013-08-31 18:17:38 +00:00
self.settings_section = 'crashreport'
2015-08-26 08:20:55 +00:00
self.report_text = '**OpenLP Bug Report**\n' \
'Version: {version}\n\n' \
'--- Details of the Exception. ---\n\n{description}\n\n ' \
'--- Exception Traceback ---\n{traceback}\n' \
'--- System information ---\n{system}\n' \
'--- Library Versions ---\n{libs}\n'
2015-11-07 00:49:40 +00:00
def exec(self):
2013-02-01 19:58:18 +00:00
"""
Show the dialog.
"""
2013-08-31 18:17:38 +00:00
self.description_text_edit.setPlainText('')
2013-03-05 14:05:19 +00:00
self.on_description_updated()
self.file_attachment = None
2015-11-07 00:49:40 +00:00
return QtWidgets.QDialog.exec(self)
2011-02-03 17:11:20 +00:00
2013-03-05 14:14:37 +00:00
def _create_report(self):
2013-02-01 19:58:18 +00:00
"""
Create an exception report.
"""
2011-03-28 17:29:25 +00:00
openlp_version = get_application_version()
2013-03-05 14:02:56 +00:00
description = self.description_text_edit.toPlainText()
traceback = self.exception_text_edit.toPlainText()
2016-05-20 16:22:06 +00:00
system = translate('OpenLP.ExceptionForm', 'Platform: {platform}\n').format(platform=platform.platform())
libraries = ('Python: {python}\nQt5: {qt5}\nPyQt5: {pyqt5}\nQtWebkit: {qtwebkit}\nSQLAlchemy: {sqalchemy}\n'
'SQLAlchemy Migrate: {migrate}\nBeautifulSoup: {soup}\nlxml: {etree}\nChardet: {chardet}\n'
'PyEnchant: {enchant}\nMako: {mako}\npyICU: {icu}\npyUNO bridge: {uno}\n'
'VLC: {vlc}\n').format(python=platform.python_version(), qt5=Qt.qVersion(),
pyqt5=Qt.PYQT_VERSION_STR, qtwebkit=WEBKIT_VERSION,
sqalchemy=sqlalchemy.__version__, migrate=MIGRATE_VERSION,
soup=bs4.__version__, etree=etree.__version__, chardet=CHARDET_VERSION,
enchant=ENCHANT_VERSION, mako=MAKO_VERSION, icu=ICU_VERSION,
uno=self._pyuno_import(), vlc=VLC_VERSION)
if is_linux():
2013-08-31 18:17:38 +00:00
if os.environ.get('KDE_FULL_SESSION') == 'true':
system += 'Desktop: KDE SC\n'
elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
system += 'Desktop: GNOME\n'
elif os.environ.get('DESKTOP_SESSION') == 'xfce':
system += 'Desktop: Xfce\n'
# NOTE: Keys match the expected input for self.report_text.format()
return {'version': openlp_version, 'description': description, 'traceback': traceback,
'system': system, 'libs': libraries}
2010-12-13 14:24:16 +00:00
2013-03-05 14:14:37 +00:00
def on_save_report_button_clicked(self):
"""
2012-12-29 09:35:24 +00:00
Saving exception log and system information to a file.
"""
file_path, filter_used = FileDialog.getSaveFileName(
2014-03-20 19:10:31 +00:00
self,
translate('OpenLP.ExceptionForm', 'Save Crash Report'),
2013-08-31 18:17:38 +00:00
Settings().value(self.settings_section + '/last directory'),
translate('OpenLP.ExceptionForm', 'Text files (*.txt *.log *.text)'))
if file_path:
Settings().setValue(self.settings_section + '/last directory', file_path.parent)
opts = self._create_report()
report_text = self.report_text.format(version=opts['version'], description=opts['description'],
2016-06-25 11:39:57 +00:00
traceback=opts['traceback'], libs=opts['libs'], system=opts['system'])
filename = str(file_path)
try:
2013-08-31 18:17:38 +00:00
report_file = open(filename, 'w')
try:
2011-06-01 05:42:56 +00:00
report_file.write(report_text)
except UnicodeError:
2011-06-01 05:42:56 +00:00
report_file.close()
2013-08-31 18:17:38 +00:00
report_file = open(filename, 'wb')
report_file.write(report_text.encode('utf-8'))
finally:
2011-06-01 05:42:56 +00:00
report_file.close()
except IOError:
2013-08-31 18:17:38 +00:00
log.exception('Failed to write crash report')
finally:
2011-06-01 05:42:56 +00:00
report_file.close()
2013-03-05 14:14:37 +00:00
def on_send_report_button_clicked(self):
"""
2013-03-14 10:46:19 +00:00
Opening systems default email client and inserting exception log and system information.
"""
2013-03-05 14:14:37 +00:00
content = self._create_report()
2013-08-31 18:17:38 +00:00
source = ''
exception = ''
for line in content['traceback'].split('\n'):
if re.search(r'[/\\]openlp[/\\]', line):
source = re.sub(r'.*[/\\]openlp[/\\](.*)".*', r'\1', line)
2013-08-31 18:17:38 +00:00
if ':' in line:
exception = line.split('\n')[-1].split(':')[0]
2016-05-20 16:22:06 +00:00
subject = 'Bug report: {error} in {source}'.format(error=exception, source=source)
mail_urlquery = QtCore.QUrlQuery()
mail_urlquery.addQueryItem('subject', subject)
mail_urlquery.addQueryItem('body', self.report_text.format(version=content['version'],
description=content['description'],
traceback=content['traceback'],
system=content['system'],
libs=content['libs']))
2013-03-05 14:05:19 +00:00
if self.file_attachment:
mail_urlquery.addQueryItem('attach', self.file_attachment)
mail_to_url = QtCore.QUrl('mailto:bugs@openlp.org')
mail_to_url.setQuery(mail_urlquery)
2013-12-24 11:32:38 +00:00
QtGui.QDesktopServices.openUrl(mail_to_url)
2011-02-03 17:11:20 +00:00
2013-03-05 14:05:19 +00:00
def on_description_updated(self):
2013-02-01 19:58:18 +00:00
"""
Update the minimum number of characters needed in the description.
"""
2013-03-05 14:02:56 +00:00
count = int(20 - len(self.description_text_edit.toPlainText()))
2011-02-03 17:11:20 +00:00
if count < 0:
2013-03-05 14:14:37 +00:00
self.__button_state(True)
self.description_word_count.setText(
translate('OpenLP.ExceptionDialog', '<strong>Thank you for your description!</strong>'))
elif count == 20:
self.__button_state(False)
self.description_word_count.setText(
translate('OpenLP.ExceptionDialog', '<strong>Tell us what you were doing when this happened.</strong>'))
2011-02-03 19:07:27 +00:00
else:
2013-03-05 14:14:37 +00:00
self.__button_state(False)
self.description_word_count.setText(
translate('OpenLP.ExceptionDialog', '<strong>Please enter a more detailed description of the situation'
'</strong>'))
2011-02-03 17:11:20 +00:00
2013-03-05 14:14:37 +00:00
def on_attach_file_button_clicked(self):
2013-02-01 19:58:18 +00:00
"""
Attach files to the bug report e-mail.
"""
file_path, filter_used = \
FileDialog.getOpenFileName(self,
translate('ImagePlugin.ExceptionDialog', 'Select Attachment'),
Settings().value(self.settings_section + '/last directory'),
'{text} (*)'.format(text=UiStrings().AllFiles))
log.info('New file {file}'.format(file=file_path))
if file_path:
self.file_attachment = str(file_path)
2011-02-03 19:07:27 +00:00
2013-03-05 14:14:37 +00:00
def __button_state(self, state):
2013-02-01 19:58:18 +00:00
"""
Toggle the button state.
"""
2013-03-05 14:02:56 +00:00
self.save_report_button.setEnabled(state)
self.send_report_button.setEnabled(state)
2013-07-19 20:56:31 +00:00
def _pyuno_import(self):
2013-07-21 14:10:21 +00:00
"""
Added here to define only when the form is actioned. The uno interface spits out lots of exception messages
if the import is at a file level. If uno import is changed this could be reverted.
This happens in other classes but there it is localised here it is across the whole system and hides real
errors.
"""
2013-07-19 20:56:31 +00:00
try:
import uno
2013-08-31 18:17:38 +00:00
arg = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
arg.Name = 'nodepath'
arg.Value = '/org.openoffice.Setup/Product'
2013-07-19 20:56:31 +00:00
context = uno.getComponentContext()
2013-08-31 18:17:38 +00:00
provider = context.ServiceManager.createInstance('com.sun.star.configuration.ConfigurationProvider')
node = provider.createInstanceWithArguments('com.sun.star.configuration.ConfigurationAccess', (arg,))
return node.getByName('ooSetupVersion')
2013-07-19 20:56:31 +00:00
except ImportError:
2013-08-31 18:17:38 +00:00
return '-'
2013-07-19 20:56:31 +00:00
except:
2013-08-31 18:17:38 +00:00
return '- (Possible non-standard UNO installation)'