Prevent open file duplication, Add files passed as parameters to the database

This commit is contained in:
BasioMeusPuga
2018-02-19 15:34:06 +05:30
parent 5db202e62f
commit aed615ecaf
2 changed files with 47 additions and 19 deletions

3
TODO
View File

@@ -42,6 +42,7 @@ TODO
✓ View modes for QGraphicsView ✓ View modes for QGraphicsView
✓ View and hide toolbar actions in a list ✓ View and hide toolbar actions in a list
✓ Line spacing ✓ Line spacing
✓ Record progress
Search document using QTextCursor? Search document using QTextCursor?
Use embedded fonts Use embedded fonts
Graphical themes Graphical themes
@@ -49,7 +50,6 @@ TODO
Comic view modes Comic view modes
Continuous paging Continuous paging
Double pages Double pages
Record progress
Bookmarks Bookmarks
Pagination Pagination
Set context menu for definitions and the like Set context menu for definitions and the like
@@ -72,4 +72,3 @@ TODO
Shift to logging instead of print statements Shift to logging instead of print statements
Bugs: Bugs:
If there are files open and the database is deleted, TypeErrors result If there are files open and the database is deleted, TypeErrors result
The auto scan doesn't work on a new database

View File

@@ -21,6 +21,7 @@
import os import os
import sys import sys
import hashlib
from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5 import QtWidgets, QtGui, QtCore
import sorter import sorter
@@ -219,7 +220,8 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Open last... open books. # Open last... open books.
# Then set the value to None for the next run # Then set the value to None for the next run
if self.settings['last_open_books']: if self.settings['last_open_books']:
self.open_files(self.settings['last_open_books']) files_to_open = {i: None for i in self.settings['last_open_books']}
self.open_files(files_to_open)
else: else:
self.settings['last_open_tab'] = None self.settings['last_open_tab'] = None
@@ -227,9 +229,22 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
cl_parser = QtCore.QCommandLineParser() cl_parser = QtCore.QCommandLineParser()
cl_parser.process(QtWidgets.qApp) cl_parser.process(QtWidgets.qApp)
my_args = cl_parser.positionalArguments() my_args = cl_parser.positionalArguments()
input_files = [ if my_args:
QtCore.QFileInfo(i).absoluteFilePath() for i in my_args] file_list = [QtCore.QFileInfo(i).absoluteFilePath() for i in my_args]
self.open_files(input_files) books = sorter.BookSorter(
file_list,
'addition',
self.database_path,
self.settings['auto_tags'])
parsed_books = books.initiate_threads()
database.DatabaseFunctions(self.database_path).add_to_database(parsed_books)
self.lib_ref.generate_model('addition', parsed_books, True)
file_dict = {QtCore.QFileInfo(i).absoluteFilePath(): None for i in my_args}
self.open_files(file_dict)
self.move_on()
# Scan the library @ startup # Scan the library @ startup
if self.settings['scan_library']: if self.settings['scan_library']:
@@ -594,21 +609,35 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
elif sender == 'tableView': elif sender == 'tableView':
metadata = self.lib_ref.table_proxy_model.data(index, QtCore.Qt.UserRole) metadata = self.lib_ref.table_proxy_model.data(index, QtCore.Qt.UserRole)
# Shift focus to the tab that has the book open (if there is one) path = {metadata['path']: metadata['hash']}
for i in range(1, self.tabWidget.count()): self.open_files(path)
tab_metadata = self.tabWidget.widget(i).metadata
if tab_metadata['hash'] == metadata['hash']:
self.tabWidget.setCurrentIndex(i)
return
path = metadata['path'] def open_files(self, path_hash_dictionary):
self.open_files([path]) # file_paths is expected to be a dictionary
def open_files(self, file_paths):
# file_paths is expected to be a list
# This allows for threading file opening # This allows for threading file opening
# Which should speed up multiple file opening # Which should speed up multiple file opening
# especially @ application start # especially @ application start
file_paths = [i for i in path_hash_dictionary]
for filename in path_hash_dictionary.items():
file_md5 = filename[1]
if not file_md5:
with open(filename[0], 'rb') as current_book:
first_bytes = current_book.read(1024 * 32) # First 32KB of the file
file_md5 = hashlib.md5(first_bytes).hexdigest()
# Remove any already open files
# Set focus to last file in case only one is open
for i in range(1, self.tabWidget.count()):
tab_metadata = self.tabWidget.widget(i).metadata
if tab_metadata['hash'] == file_md5:
file_paths.remove(filename[0])
if not file_paths:
self.tabWidget.setCurrentIndex(i)
return
if not file_paths: if not file_paths:
return return