From fe5283de4a4241432cd0fc0988848f975cd09ee6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 29 Jan 2010 08:06:26 -0700 Subject: [PATCH] Basic support for associating file types on OS X --- setup/installer/osx/app/main.py | 5 ++++- src/calibre/gui2/__init__.py | 13 ++++++++++++- src/calibre/gui2/main.py | 5 ++++- src/calibre/gui2/ui.py | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/setup/installer/osx/app/main.py b/setup/installer/osx/app/main.py index 85533717b4..2a10d09998 100644 --- a/setup/installer/osx/app/main.py +++ b/setup/installer/osx/app/main.py @@ -355,6 +355,7 @@ class Py2App(object): @flush def create_plist(self): + from calibre.ebooks import BOOK_EXTENSIONS env = dict(**ENV) env['CALIBRE_LAUNCHED_FROM_BUNDLE']='1'; @@ -367,10 +368,11 @@ class Py2App(object): CFBundlePackageType='APPL', CFBundleSignature='????', CFBundleExecutable='calibre', + CFBundleTypeExtensions=list(BOOK_EXTENSIONS), LSMinimumSystemVersion='10.4.2', LSRequiresNativeExecution=True, NSAppleScriptEnabled=False, - NSHumanReadableCopyright='Copyright 2008, Kovid Goyal', + NSHumanReadableCopyright='Copyright 2010, Kovid Goyal', CFBundleGetInfoString=('calibre, an E-book management ' 'application. Visit http://calibre-ebook.com for details.'), CFBundleIconFile='library.icns', @@ -594,6 +596,7 @@ class Py2App(object): if x == 'Info.plist': plist = plistlib.readPlist(join(self.contents_dir, x)) plist['LSUIElement'] = '1' + plist.pop('CFBundleTypeExtensions') plistlib.writePlist(plist, join(cc_dir, x)) else: os.symlink(join('../..', x), diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 34f9f57161..7181c16329 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -3,7 +3,8 @@ __copyright__ = '2008, Kovid Goyal ' """ The GUI """ import os from PyQt4.QtCore import QVariant, QFileInfo, QObject, SIGNAL, QBuffer, Qt, QSize, \ - QByteArray, QTranslator, QCoreApplication, QThread + QByteArray, QTranslator, QCoreApplication, QThread, \ + QEvent from PyQt4.QtGui import QFileDialog, QMessageBox, QPixmap, QFileIconProvider, \ QIcon, QTableView, QApplication, QDialog, QPushButton @@ -524,6 +525,7 @@ class Application(QApplication): def __init__(self, args): qargs = [i.encode('utf-8') if isinstance(i, unicode) else i for i in args] QApplication.__init__(self, qargs) + self.file_event_hook = None global gui_thread, qt_app gui_thread = QThread.currentThread() self._translator = None @@ -549,6 +551,15 @@ class Application(QApplication): if set_qt_translator(self._translator): self.installTranslator(self._translator) + def event(self, e): + if callable(self.file_event_hook) and e.type() == QEvent.FileOpen: + path = unicode(e.file()) + if os.access(path, os.R_OK): + self.file_event_hook(path) + return True + else: + return QApplication.event(self, e) + _store_app = None def is_ok_to_use_qt(): diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index cf62508751..fef0853a23 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -2,6 +2,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' import sys, os, time, socket, traceback +from functools import partial from PyQt4.Qt import QCoreApplication, QIcon, QMessageBox @@ -52,10 +53,12 @@ def run_gui(opts, args, actions, listener, app): wizard().exec_() dynamic.set('welcome_wizard_was_run', True) main = Main(listener, opts, actions) + add_filesystem_book = partial(main.add_filesystem_book, allow_device=False) sys.excepthook = main.unhandled_exception if len(args) > 1: args[1] = os.path.abspath(args[1]) - main.add_filesystem_book(args[1]) + add_filesystem_book(args[1]) + app.file_event_hook = add_filesystem_book ret = app.exec_() if getattr(main, 'run_wizard_b4_shutdown', False): from calibre.gui2.wizard import wizard diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 28efdc93ff..7f3ca297fd 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -987,10 +987,10 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): self.cover_cache.refresh([cid]) self.library_view.model().current_changed(current_idx, current_idx) - def add_filesystem_book(self, path): + def add_filesystem_book(self, path, allow_device=True): if os.access(path, os.R_OK): books = [os.path.abspath(path)] - to_device = self.stack.currentIndex() != 0 + to_device = allow_device and self.stack.currentIndex() != 0 self._add_books(books, to_device) if to_device: self.status_bar.showMessage(\