mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cleanup previous PR
This commit is contained in:
parent
05352b0233
commit
b57bd18b12
@ -1,26 +1,34 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, posixpath, weakref, sys
|
import os
|
||||||
|
import posixpath
|
||||||
|
import sys
|
||||||
|
import weakref
|
||||||
|
from contextlib import suppress
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from qt.core import (
|
||||||
from qt.core import (QMenu, Qt, QInputDialog, QToolButton, QDialog,
|
QAction, QCoreApplication, QDialog, QDialogButtonBox, QGridLayout, QIcon,
|
||||||
QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QIcon, QSize,
|
QInputDialog, QLabel, QLineEdit, QMenu, QSize, Qt, QTimer, QToolButton,
|
||||||
QCoreApplication, pyqtSignal, QVBoxLayout, QTimer, QAction)
|
QVBoxLayout, pyqtSignal
|
||||||
|
)
|
||||||
|
|
||||||
from calibre import isbytestring, sanitize_file_name
|
from calibre import isbytestring, sanitize_file_name
|
||||||
from calibre.constants import (filesystem_encoding, iswindows, get_portable_base, isportable, config_dir)
|
from calibre.constants import (
|
||||||
|
config_dir, filesystem_encoding, get_portable_base, isportable, iswindows
|
||||||
|
)
|
||||||
|
from calibre.gui2 import (
|
||||||
|
Dispatcher, choose_dir, choose_files, error_dialog, gprefs, info_dialog,
|
||||||
|
open_local_file, pixmap_to_data, question_dialog, warning_dialog
|
||||||
|
)
|
||||||
|
from calibre.gui2.actions import InterfaceAction
|
||||||
from calibre.library import current_library_name
|
from calibre.library import current_library_name
|
||||||
from calibre.utils.config import prefs, tweaks
|
from calibre.utils.config import prefs, tweaks
|
||||||
from calibre.utils.icu import sort_key
|
from calibre.utils.icu import sort_key
|
||||||
from calibre.gui2 import (gprefs, warning_dialog, Dispatcher, error_dialog,
|
|
||||||
question_dialog, info_dialog, open_local_file, choose_dir, choose_files, pixmap_to_data)
|
|
||||||
from calibre.gui2.actions import InterfaceAction
|
|
||||||
|
|
||||||
|
|
||||||
def db_class():
|
def db_class():
|
||||||
@ -28,6 +36,10 @@ def db_class():
|
|||||||
return LibraryDatabase
|
return LibraryDatabase
|
||||||
|
|
||||||
|
|
||||||
|
def library_icon_path(lib_name=''):
|
||||||
|
return os.path.join(config_dir, 'library_icons', sanitize_file_name(lib_name or current_library_name()) + '.png')
|
||||||
|
|
||||||
|
|
||||||
class LibraryUsageStats: # {{{
|
class LibraryUsageStats: # {{{
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -245,7 +257,7 @@ class ChooseLibraryAction(InterfaceAction):
|
|||||||
None, None), attr='action_pick_random')
|
None, None), attr='action_pick_random')
|
||||||
ac.triggered.connect(self.pick_random)
|
ac.triggered.connect(self.pick_random)
|
||||||
|
|
||||||
self.choose_library_icon_menu = QMenu(_('Choose/remove the icon for this library'))
|
self.choose_library_icon_menu = QMenu(_('Change the icon for this library'))
|
||||||
self.choose_library_icon_menu.setIcon(QIcon(I('icon_choose.png')))
|
self.choose_library_icon_menu.setIcon(QIcon(I('icon_choose.png')))
|
||||||
self.choose_library_icon_action = self.create_action(
|
self.choose_library_icon_action = self.create_action(
|
||||||
spec=(_('Choose an icon'), 'icon_choose.png', None, None),
|
spec=(_('Choose an icon'), 'icon_choose.png', None, None),
|
||||||
@ -254,9 +266,10 @@ class ChooseLibraryAction(InterfaceAction):
|
|||||||
spec=(_('Remove current icon'), 'trash.png', None, None),
|
spec=(_('Remove current icon'), 'trash.png', None, None),
|
||||||
attr='action_remove_library_icon')
|
attr='action_remove_library_icon')
|
||||||
self.choose_library_icon_action.triggered.connect(self.get_library_icon)
|
self.choose_library_icon_action.triggered.connect(self.get_library_icon)
|
||||||
self.remove_library_icon_action.triggered.connect(partial(self.remove_library_icon, None))
|
self.remove_library_icon_action.triggered.connect(partial(self.remove_library_icon, ''))
|
||||||
self.choose_library_icon_menu.addAction(self.choose_library_icon_action)
|
self.choose_library_icon_menu.addAction(self.choose_library_icon_action)
|
||||||
self.choose_library_icon_menu.addAction(self.remove_library_icon_action)
|
self.choose_library_icon_menu.addAction(self.remove_library_icon_action)
|
||||||
|
self.original_library_icon = self.qaction.icon()
|
||||||
|
|
||||||
if not os.environ.get('CALIBRE_OVERRIDE_DATABASE_PATH', None):
|
if not os.environ.get('CALIBRE_OVERRIDE_DATABASE_PATH', None):
|
||||||
self.choose_menu.addAction(self.action_choose)
|
self.choose_menu.addAction(self.action_choose)
|
||||||
@ -337,65 +350,47 @@ class ChooseLibraryAction(InterfaceAction):
|
|||||||
try:
|
try:
|
||||||
path = choose_files(self.gui, 'choose_library_icon',
|
path = choose_files(self.gui, 'choose_library_icon',
|
||||||
_('Select icon for library "%s"') % current_library_name(),
|
_('Select icon for library "%s"') % current_library_name(),
|
||||||
filters=[('Images', ['png', 'gif', 'jpg', 'jpeg'])],
|
filters=[('Images', ['png', 'gif', 'jpg', 'jpeg', 'webp'])],
|
||||||
all_files=False, select_only_single_file=True)
|
all_files=False, select_only_single_file=True)
|
||||||
if path:
|
if path:
|
||||||
path = path[0]
|
path = path[0]
|
||||||
p = QIcon(path).pixmap(QSize(128, 128))
|
p = QIcon(path).pixmap(QSize(256, 256))
|
||||||
d = os.path.join(config_dir, 'library_icons')
|
icp = library_icon_path()
|
||||||
if not os.path.exists(d):
|
os.makedirs(os.path.dirname(icp), exist_ok=True)
|
||||||
os.makedirs(d)
|
with open(icp, 'wb') as f:
|
||||||
icon_name = sanitize_file_name(current_library_name())+'.png'
|
|
||||||
with open(os.path.join(d, icon_name), 'wb') as f:
|
|
||||||
f.write(pixmap_to_data(p, format='PNG'))
|
f.write(pixmap_to_data(p, format='PNG'))
|
||||||
path = os.path.basename(f.name)
|
|
||||||
self.set_library_icon()
|
self.set_library_icon()
|
||||||
except:
|
except Exception:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def rename_library_icon(self, old_name, new_name):
|
def rename_library_icon(self, old_name, new_name):
|
||||||
old_icon_name = sanitize_file_name(old_name)+'.png'
|
old_path = library_icon_path(old_name)
|
||||||
new_icon_name = sanitize_file_name(new_name)+'.png'
|
new_path = library_icon_path(new_name)
|
||||||
try:
|
try:
|
||||||
d = os.path.join(config_dir, 'library_icons')
|
if os.path.exists(old_path):
|
||||||
old = os.path.join(d, old_icon_name)
|
os.replace(old_path, new_path)
|
||||||
if os.path.exists(old):
|
except Exception:
|
||||||
os.rename(old, os.path.join(d, new_icon_name))
|
|
||||||
except:
|
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def remove_library_icon(self, name):
|
def remove_library_icon(self, name=''):
|
||||||
if name is None:
|
|
||||||
name = current_library_name()
|
|
||||||
name = sanitize_file_name(name)+'.png'
|
|
||||||
try:
|
try:
|
||||||
d = os.path.join(config_dir, 'library_icons', name)
|
with suppress(FileNotFoundError):
|
||||||
if os.path.exists(d):
|
os.remove(library_icon_path(name or current_library_name()))
|
||||||
os.remove(os.path.join(d, ))
|
self.set_library_icon()
|
||||||
self.set_library_icon()
|
except Exception:
|
||||||
except:
|
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def set_library_icon(self):
|
def set_library_icon(self):
|
||||||
icon = None
|
icon = QIcon(library_icon_path())
|
||||||
icon_name = sanitize_file_name(current_library_name())+'.png'
|
has_icon = not icon.isNull() and len(icon.availableSizes()) > 0
|
||||||
path = os.path.join(config_dir, 'library_icons', icon_name)
|
if not has_icon:
|
||||||
if os.path.exists(path):
|
icon = self.original_library_icon
|
||||||
icon = QIcon(path)
|
self.qaction.setIcon(icon)
|
||||||
if len(icon.availableSizes()) > 0:
|
self.gui.setWindowIcon(icon)
|
||||||
self.qaction.setIcon(icon)
|
self.remove_library_icon_action.setEnabled(has_icon)
|
||||||
self.gui.setWindowIcon(icon)
|
|
||||||
self.remove_library_icon_action.setEnabled(True)
|
|
||||||
else:
|
|
||||||
icon = None
|
|
||||||
if icon is None:
|
|
||||||
icon = QIcon(I('library.png'))
|
|
||||||
self.qaction.setIcon(icon)
|
|
||||||
self.gui.setWindowIcon(icon)
|
|
||||||
self.remove_library_icon_action.setEnabled(False)
|
|
||||||
|
|
||||||
def exim_data(self):
|
def exim_data(self):
|
||||||
if isportable:
|
if isportable:
|
||||||
@ -730,6 +725,7 @@ class ChooseLibraryAction(InterfaceAction):
|
|||||||
|
|
||||||
def debug_leak(self):
|
def debug_leak(self):
|
||||||
import gc
|
import gc
|
||||||
|
|
||||||
from calibre.utils.mem import memory
|
from calibre.utils.mem import memory
|
||||||
ref = self.dbref
|
ref = self.dbref
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user