~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/gui2/add.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mfrom: (1.5.10)
  • Revision ID: package-import@ubuntu.com-20140514181750-xyrxqa47dbw0qfhu
Tags: 1.36.0+dfsg-1
* New upstream release:
  - Fixes editing of metadata (Closes: #741638)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
from PyQt4.Qt import QThread, QObject, Qt, QProgressDialog, pyqtSignal, QTimer
9
9
 
 
10
from calibre.ptempfile import PersistentTemporaryDirectory
10
11
from calibre.gui2.dialogs.progress import ProgressDialog
11
12
from calibre.gui2 import (error_dialog, info_dialog, gprefs,
12
13
        warning_dialog, available_width)
54
55
    update = pyqtSignal(object)
55
56
    found  = pyqtSignal(object)
56
57
 
57
 
    def __init__(self, parent, db, root, single):
 
58
    def __init__(self, parent, db, root, single, tdir=None):
58
59
        QThread.__init__(self, parent)
59
60
        self.db = db
60
61
        self.path = root
 
62
        self.tdir = tdir
61
63
        self.single_book_per_directory = single
62
64
        self.canceled = False
63
65
 
72
74
            self.books += list(self.db.find_books_in_directory(dirpath[0],
73
75
                                            self.single_book_per_directory))
74
76
 
 
77
    def extract(self):
 
78
        if self.path.lower().endswith('.zip'):
 
79
            from calibre.utils.zipfile import ZipFile
 
80
            try:
 
81
                with ZipFile(self.path) as zf:
 
82
                    zf.extractall(self.tdir)
 
83
            except Exception:
 
84
                prints('Corrupt ZIP file, trying to use local headers')
 
85
                from calibre.utils.localunzip import extractall
 
86
                extractall(self.path, self.tdir)
 
87
        elif self.path.lower().endswith('.rar'):
 
88
            from calibre.utils.unrar import extract
 
89
            extract(self.path, self.tdir)
 
90
        else:
 
91
            raise ValueError('Can only process ZIP or RAR archives')
 
92
 
75
93
    def run(self):
 
94
        if self.tdir is not None:
 
95
            try:
 
96
                self.extract()
 
97
            except Exception as err:
 
98
                import traceback
 
99
                traceback.print_exc()
 
100
                msg = as_unicode(err)
 
101
                self.found.emit(msg)
 
102
                return
 
103
            self.path = self.tdir
 
104
 
76
105
        root = os.path.abspath(self.path)
77
106
        try:
78
107
            self.walk(root)
263
292
        self.pd.canceled_signal.connect(self.canceled)
264
293
 
265
294
    def add_recursive(self, root, single=True):
266
 
        self.path = root
 
295
        if os.path.exists(root) and os.path.isfile(root) and root.lower().rpartition('.')[-1] in {'zip', 'rar'}:
 
296
            self.path = tdir = PersistentTemporaryDirectory('_arcv_')
 
297
        else:
 
298
            self.path = root
 
299
            tdir = None
267
300
        self.pd.set_msg(_('Searching in all sub-directories...'))
268
301
        self.pd.set_min(0)
269
302
        self.pd.set_max(0)
270
303
        self.pd.value = 0
271
 
        self.rfind = RecursiveFind(self, self.db, root, single)
 
304
        self.rfind = RecursiveFind(self, self.db, root, single, tdir=tdir)
272
305
        self.rfind.update.connect(self.pd.set_msg, type=Qt.QueuedConnection)
273
306
        self.rfind.found.connect(self.add, type=Qt.QueuedConnection)
274
307
        self.rfind.start()
391
424
            return self.duplicates_processed()
392
425
        self.pd.hide()
393
426
        from calibre.gui2.dialogs.duplicates import DuplicatesQuestion
394
 
        d = DuplicatesQuestion(self.db, duplicates, self._parent)
 
427
        self.__d_q = d = DuplicatesQuestion(self.db, duplicates, self._parent)
395
428
        duplicates = tuple(d.duplicates)
396
429
        if duplicates:
397
430
            pd = QProgressDialog(_('Adding duplicates...'), '', 0, len(duplicates),