From 4d20e70ea1c8a57265f2cfca690b4bf57ae03f33 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 Oct 2023 20:15:00 +0530 Subject: [PATCH] Edit book: When adding dictionaries allow directly a LibreOffice adding the dictionary just by choosing the language Cleanup previous PR that implement this functionality --- src/calibre/gui2/tweak_book/spell.py | 31 +++++++++++++++------------- src/calibre/spell/import_from.py | 7 +++++-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/calibre/gui2/tweak_book/spell.py b/src/calibre/gui2/tweak_book/spell.py index 4f16b73bc1..12fce21c2c 100644 --- a/src/calibre/gui2/tweak_book/spell.py +++ b/src/calibre/gui2/tweak_book/spell.py @@ -12,10 +12,10 @@ from itertools import chain from qt.core import ( QT_VERSION_STR, QAbstractItemView, QAbstractTableModel, QAction, QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox, QFont, QFormLayout, QGridLayout, - QHBoxLayout, QIcon, QInputDialog, QKeySequence, QLabel, QLineEdit, - QListWidget,QListWidgetItem, QMenu, QModelIndex, QPlainTextEdit, QPushButton, - QSize, QStackedLayout, Qt, QTableView, QTimer, QToolButton, QTreeWidget, - QTabWidget, QTreeWidgetItem, QVBoxLayout, QWidget, pyqtSignal, + QHBoxLayout, QIcon, QInputDialog, QKeySequence, QLabel, QLineEdit, QListWidget, + QListWidgetItem, QMenu, QModelIndex, QPlainTextEdit, QPushButton, QSize, + QStackedLayout, Qt, QTableView, QTabWidget, QTimer, QToolButton, QTreeWidget, + QTreeWidgetItem, QVBoxLayout, QWidget, pyqtSignal, ) from threading import Thread @@ -33,6 +33,7 @@ from calibre.gui2.tweak_book import ( current_container, dictionaries, editors, set_book_locale, tprefs, ) from calibre.gui2.tweak_book.widgets import Dialog +from calibre.gui2.widgets import BusyCursor from calibre.gui2.widgets2 import FlowLayout from calibre.spell import DictionaryLocale from calibre.spell.break_iterator import split_into_words @@ -40,7 +41,7 @@ from calibre.spell.dictionary import ( best_locale_for_language, builtin_dictionaries, catalog_online_dictionaries, custom_dictionaries, dprefs, get_dictionary, remove_dictionary, rename_dictionary, ) -from calibre.spell.import_from import import_from_oxt, import_from_online +from calibre.spell.import_from import import_from_online, import_from_oxt from calibre.startup import connect_lambda from calibre.utils.icu import contains, primary_contains, primary_sort_key, sort_key from calibre.utils.localization import ( @@ -90,8 +91,8 @@ class AddDictionary(QDialog): # {{{ self.web_download = QWidget(self) self.oxt_import = QWidget(self) - tabs.addTab(self.web_download, _('Download online')) - tabs.addTab(self.oxt_import, _('Import from OXT file')) + tabs.setTabIcon(tabs.addTab(self.web_download, _('&Download')), QIcon.ic('download-metadata.png')) + tabs.setTabIcon(tabs.addTab(self.oxt_import, _('&Import from OXT file')), QIcon.ic('unpack-book.png')) tabs.currentChanged.connect(self.tab_changed) # Download online tab @@ -100,16 +101,16 @@ class AddDictionary(QDialog): # {{{ self.web_download.setLayout(l) la = QLabel('

' + _( - '''{0} supports the use of LibreOffice dictionaries for spell checking. You can - download some of them from the LibreOffice dictionaries repository.''' - ).format(__appname__, 'https://github.com/LibreOffice/dictionaries')+'

') # noqa + '''{0} supports the use of LibreOffice dictionaries for spell checking. Choose the language you + want below and click OK to download the dictionary from the LibreOffice dictionaries repository.''' + ).format(__appname__, 'https://github.com/LibreOffice/dictionaries')+'

') la.setWordWrap(True) la.setOpenExternalLinks(True) la.setMinimumWidth(450) l.addRow(la) self.combobox_online = c = QComboBox(self) - l.addRow(_('Langue to download:'), c) + l.addRow(_('&Language to download:'), c) c.addItem('', None) languages = current_languages_dictionaries(reread=False) @@ -218,8 +219,9 @@ class AddDictionary(QDialog): # {{{ def accept(self): idx = self.tabs.currentIndex() - if idx== 0: - self._process_online_download() + if idx == 0: + with BusyCursor(): + self._process_online_download() elif idx == 1: self._process_oxt_import() QDialog.accept(self) @@ -1603,7 +1605,8 @@ def find_next_error(current_editor, current_editor_name, gui_parent, show_editor if __name__ == '__main__': - app = QApplication([]) + from calibre.gui2 import Application + app = Application([]) dictionaries.initialize() ManageDictionaries.test() del app diff --git a/src/calibre/spell/import_from.py b/src/calibre/spell/import_from.py index 8b6a1e8b4f..be4be5d98b 100644 --- a/src/calibre/spell/import_from.py +++ b/src/calibre/spell/import_from.py @@ -137,6 +137,7 @@ def _import_from_virtual_directory(read_file_func, name, dest_dir=None, prefix=' num += 1 return num + def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): with ZipFile(source_path) as zf: def read_file(key): @@ -153,13 +154,15 @@ def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): return _import_from_virtual_directory(read_file, name, dest_dir=dest_dir, prefix=prefix) + def import_from_online(directory, name, dest_dir=None, prefix='dic-'): - br = browser() + br = browser(timeout=30) def read_file(key): - rp = br.open('/'.join([ONLINE_DICTIONARY_BASE_URL, directory, key])) + rp = br.open('/'.join((ONLINE_DICTIONARY_BASE_URL, directory, key))) return rp.read() return _import_from_virtual_directory(read_file, name, dest_dir=dest_dir, prefix=prefix) + if __name__ == '__main__': import_from_libreoffice_source_tree(sys.argv[-1])