From c5daec10f188931ac56e752a35e599394b24e09c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 14 Feb 2008 02:53:49 +0000 Subject: [PATCH] Add option to save books to single output folder --- src/libprs500/gui2/library.py | 4 ++-- src/libprs500/gui2/main.py | 15 +++++++++++++-- src/libprs500/library/database.py | 22 +++++++++++++--------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/libprs500/gui2/library.py b/src/libprs500/gui2/library.py index ff5e0aa57d..27935a6461 100644 --- a/src/libprs500/gui2/library.py +++ b/src/libprs500/gui2/library.py @@ -138,9 +138,9 @@ class BooksModel(QAbstractTableModel): ''' Return list indices of all cells in index.row()''' return [ self.index(index.row(), c) for c in range(self.columnCount(None))] - def save_to_disk(self, rows, path): + def save_to_disk(self, rows, path, single_dir=False): rows = [row.row() for row in rows] - self.db.export_to_dir(path, rows, self.sorted_on[0] == 1) + self.db.export_to_dir(path, rows, self.sorted_on[0] == 1, single_dir=single_dir) def delete_books(self, indices): ids = [ self.id(i) for i in indices ] diff --git a/src/libprs500/gui2/main.py b/src/libprs500/gui2/main.py index bea7b5f7a7..e01e4c0137 100644 --- a/src/libprs500/gui2/main.py +++ b/src/libprs500/gui2/main.py @@ -125,10 +125,17 @@ class Main(MainWindow, Ui_MainWindow): QObject.connect(self.action_sync, SIGNAL("triggered(bool)"), self.sync_to_main_memory) QObject.connect(sm.actions()[0], SIGNAL('triggered(bool)'), self.sync_to_main_memory) QObject.connect(sm.actions()[1], SIGNAL('triggered(bool)'), self.sync_to_card) + self.save_menu = QMenu() + self.save_menu.addAction(_('Save to disk')) + self.save_menu.addAction(_('Save to disk in a single directory')) + QObject.connect(self.action_save, SIGNAL("triggered(bool)"), self.save_to_disk) + QObject.connect(self.save_menu.actions()[0], SIGNAL("triggered(bool)"), self.save_to_disk) + QObject.connect(self.save_menu.actions()[1], SIGNAL("triggered(bool)"), self.save_to_single_dir) QObject.connect(self.action_view, SIGNAL("triggered(bool)"), self.view_book) self.action_sync.setMenu(sm) self.action_edit.setMenu(md) + self.action_save.setMenu(self.save_menu) self.news_menu = NewsMenu(self.customize_feeds) self.action_news.setMenu(self.news_menu) QObject.connect(self.news_menu, SIGNAL('fetch_news(PyQt_PyObject)'), self.fetch_news) @@ -147,6 +154,7 @@ class Main(MainWindow, Ui_MainWindow): self.tool_bar.widgetForAction(self.action_edit).setPopupMode(QToolButton.MenuButtonPopup) self.tool_bar.widgetForAction(self.action_sync).setPopupMode(QToolButton.MenuButtonPopup) self.tool_bar.widgetForAction(self.action_convert).setPopupMode(QToolButton.MenuButtonPopup) + self.tool_bar.widgetForAction(self.action_save).setPopupMode(QToolButton.MenuButtonPopup) self.tool_bar.setContextMenuPolicy(Qt.PreventContextMenu) QObject.connect(self.config_button, SIGNAL('clicked(bool)'), self.do_config) @@ -515,7 +523,10 @@ class Main(MainWindow, Ui_MainWindow): ############################################################################ ############################## Save to disk ################################ - def save_to_disk(self, checked): + def save_to_single_dir(self, checked): + self.save_to_disk(checked, True) + + def save_to_disk(self, checked, single_dir=False): rows = self.current_view().selectionModel().selectedRows() if not rows or len(rows) == 0: d = error_dialog(self, _('Cannot save to disk'), _('No books selected')) @@ -525,7 +536,7 @@ class Main(MainWindow, Ui_MainWindow): if not dir: return if self.current_view() == self.library_view: - self.current_view().model().save_to_disk(rows, dir) + self.current_view().model().save_to_disk(rows, dir, single_dir=single_dir) else: paths = self.current_view().model().paths(rows) self.job_manager.run_device_job(self.books_saved, diff --git a/src/libprs500/library/database.py b/src/libprs500/library/database.py index f0172b760a..d7783905e6 100644 --- a/src/libprs500/library/database.py +++ b/src/libprs500/library/database.py @@ -1281,7 +1281,7 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE; self.conn.execute('VACUUM;') self.conn.commit() - def export_to_dir(self, dir, indices, byauthor=False): + def export_to_dir(self, dir, indices, byauthor=False, single_dir=False): if not os.path.exists(dir): raise IOError('Target directory does not exist: '+dir) by_author = {} @@ -1311,22 +1311,26 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE; os.mkdir(tpath) mi = OPFCreator(self.get_metadata(idx)) cover = self.cover(idx) - if cover is not None: - f = open(os.path.join(tpath, 'cover.jpg'), 'wb') - f.write(cover) - mi.cover = 'cover.jpg' + if not single_dir: + if cover is not None: + f = open(os.path.join(tpath, 'cover.jpg'), 'wb') + f.write(cover) + mi.cover = 'cover.jpg' + f.close() + f = open(os.path.join(tpath, 'metadata.opf'), 'wb') + mi.write(f) f.close() - f = open(os.path.join(tpath, 'metadata.opf'), 'wb') - mi.write(f) - f.close() for fmt in self.formats(idx).split(','): data = self.format(idx, fmt) name = au + ' - ' + title if byauthor else title + ' - ' + au fname = name +'_'+id+'.'+fmt.lower() - f = open(os.path.join(tpath, sanitize_file_name(fname)), 'w+b') + fname = sanitize_file_name(fname) + base = dir if single_dir else tpath + f = open(os.path.join(base, fname), 'w+b') f.write(data) f.flush() + f.seek(0) try: set_metadata(f, mi, fmt.lower()) except: