This commit is contained in:
Kovid Goyal 2011-04-21 19:09:43 -06:00
commit 23e63bd895
3 changed files with 43 additions and 32 deletions

View File

@ -24,11 +24,16 @@ class Dialog(QDialog, Ui_Dialog):
dynamic[confirm_config_name(self.name)] = self.again.isChecked()
def confirm(msg, name, parent=None, pixmap='dialog_warning.png'):
def confirm(msg, name, parent=None, pixmap='dialog_warning.png', title=None,
show_cancel_button=True):
if not dynamic.get(confirm_config_name(name), True):
return True
d = Dialog(msg, name, parent)
d.label.setPixmap(QPixmap(I(pixmap)))
d.setWindowIcon(QIcon(I(pixmap)))
if title is not None:
d.setWindowTitle(title)
if not show_cancel_button:
d.buttonBox.button(d.buttonBox.Cancel).setVisible(False)
d.resize(d.sizeHint())
return d.exec_() == d.Accepted

View File

@ -28,15 +28,15 @@ from calibre.utils.config import DynamicConfig
from calibre.utils.icu import sort_key
class MobileReadStore(BasicStoreConfig, StorePlugin):
def genesis(self):
self.config = DynamicConfig('store_' + self.name)
self.rlock = RLock()
def open(self, parent=None, detail_item=None, external=False):
settings = self.get_settings()
url = 'http://www.mobileread.com/'
if external or settings.get(self.name + '_open_external', False):
open_url(QUrl(detail_item if detail_item else url))
else:
@ -71,7 +71,7 @@ class MobileReadStore(BasicStoreConfig, StorePlugin):
ratio += s.ratio()
if ratio > 0:
matches.append((ratio, x))
# Move the best scorers to head of list.
matches = heapq.nlargest(max_results, matches)
for score, book in matches:
@ -82,31 +82,31 @@ class MobileReadStore(BasicStoreConfig, StorePlugin):
def update_book_list(self, timeout=10):
with self.rlock:
url = 'http://www.mobileread.com/forums/ebooks.php?do=getlist&type=html'
last_download = self.config.get(self.name + '_last_download', None)
# Don't update the book list if our cache is less than one week old.
if last_download and (time.time() - last_download) < 604800:
return
# Download the book list HTML file from MobileRead.
br = browser()
raw_data = None
with closing(br.open(url, timeout=timeout)) as f:
raw_data = f.read()
if not raw_data:
return
# Turn books listed in the HTML file into BookRef's.
# Turn books listed in the HTML file into SearchResults's.
books = []
try:
data = html.fromstring(raw_data)
for book_data in data.xpath('//ul/li'):
book = BookRef()
book = SearchResult()
book.detail_item = ''.join(book_data.xpath('.//a/@href'))
book.formats = ''.join(book_data.xpath('.//i/text()'))
book.formats = book.formats.strip()
text = ''.join(book_data.xpath('.//a/text()'))
if ':' in text:
book.author, q, text = text.partition(':')
@ -115,7 +115,7 @@ class MobileReadStore(BasicStoreConfig, StorePlugin):
books.append(book)
except:
pass
# Save the book list and it's create time.
if books:
self.config[self.name + '_last_download'] = time.time()
@ -126,20 +126,14 @@ class MobileReadStore(BasicStoreConfig, StorePlugin):
return self.config.get(self.name + '_book_list', [])
class BookRef(SearchResult):
def __init__(self):
SearchResult.__init__(self)
class MobeReadStoreDialog(QDialog, Ui_Dialog):
def __init__(self, plugin, *args):
QDialog.__init__(self, *args)
self.setupUi(self)
self.plugin = plugin
self.model = BooksModel()
self.results_view.setModel(self.model)
self.results_view.model().set_books(self.plugin.get_book_list())
@ -149,14 +143,14 @@ class MobeReadStoreDialog(QDialog, Ui_Dialog):
self.search_query.textChanged.connect(self.model.set_filter)
self.results_view.model().total_changed.connect(self.total.setText)
self.finished.connect(self.dialog_closed)
self.restore_state()
def open_store(self, index):
result = self.results_view.model().get_book(index)
if result:
self.plugin.open(self, result.detail_item)
def restore_state(self):
geometry = self.plugin.config['store_mobileread_dialog_geometry']
if geometry:
@ -171,7 +165,7 @@ class MobeReadStoreDialog(QDialog, Ui_Dialog):
else:
for i in xrange(self.results_view.model().columnCount()):
self.results_view.resizeColumnToContents(i)
self.results_view.model().sort_col = self.plugin.config.get('store_mobileread_dialog_sort_col', 0)
self.results_view.model().sort_order = self.plugin.config.get('store_mobileread_dialog_sort_order', Qt.AscendingOrder)
self.results_view.model().sort(self.results_view.model().sort_col, self.results_view.model().sort_order)
@ -188,7 +182,7 @@ class MobeReadStoreDialog(QDialog, Ui_Dialog):
class BooksModel(QAbstractItemModel):
total_changed = pyqtSignal(unicode)
HEADERS = [_('Title'), _('Author(s)'), _('Format')]
@ -204,7 +198,7 @@ class BooksModel(QAbstractItemModel):
def set_books(self, books):
self.books = books
self.all_books = books
self.sort(self.sort_col, self.sort_order)
def get_book(self, index):
@ -213,11 +207,11 @@ class BooksModel(QAbstractItemModel):
return self.books[row]
else:
return None
def set_filter(self, filter):
#self.layoutAboutToBeChanged.emit()
self.beginResetModel()
self.filter = unicode(filter)
self.books = []
if self.filter:
@ -240,7 +234,7 @@ class BooksModel(QAbstractItemModel):
self.endResetModel()
#self.layoutChanged.emit()
def index(self, row, column, parent=QModelIndex()):
return self.createIndex(row, column)
@ -254,7 +248,7 @@ class BooksModel(QAbstractItemModel):
def columnCount(self, *args):
return len(self.HEADERS)
def headerData(self, section, orientation, role):
if role != Qt.DisplayRole:
return NONE
@ -294,7 +288,7 @@ class BooksModel(QAbstractItemModel):
if not self.books:
return
descending = order == Qt.DescendingOrder
descending = order == Qt.DescendingOrder
self.books.sort(None,
lambda x: sort_key(unicode(self.data_as_text(x, col))),
descending)

View File

@ -68,6 +68,18 @@ class NPWebView(QWebView):
filename = get_download_filename(url, cf)
ext = os.path.splitext(filename)[1][1:].lower()
if ext not in BOOK_EXTENSIONS:
if ext == 'acsm':
from calibre.gui2.dialogs.confirm_delete import confirm
confirm('<p>' + _('This ebook is a DRMed EPUB file. '
'You will be prompted to save this file to your '
'computer. Once it is saved, open it with '
'<a href="http://www.adobe.com/products/digitaleditions/">'
'Adobe Digital Editions</a> (ADE).<p>ADE, in turn '
'will download the actual ebook, which will be a '
'.epub file. You can add this book to calibre '
'using "Add Books" and selecting the file from '
'the ADE library folder.'),
'acsm_download', self)
home = os.path.expanduser('~')
name = QFileDialog.getSaveFileName(self,
_('File is not a supported ebook type. Save to disk?'),