diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index edda1c8502..f76fb5bba7 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -11,7 +11,10 @@ import sys, os from textwrap import TextWrapper from calibre import OptionParser, Settings, terminal_controller, preferred_encoding -from calibre.gui2 import SingleApplication +try: + from calibre.utils.single_qt_application import send_message +except: + send_message = None from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.opf import OPFCreator, OPFReader from calibre.library.database import LibraryDatabase, text_to_tokens @@ -181,10 +184,9 @@ def do_add(db, paths, one_book_per_directory, recurse, add_duplicates): title = title.encode(preferred_encoding) print '\t', title+':' print '\t\t ', path - - if SingleApplication is not None: - sa = SingleApplication('calibre GUI') - sa.send_message('refreshdb:') + + if send_message is not None: + send_message('refreshdb:', 'calibre GUI') finally: sys.stdout = sys.__stdout__ @@ -222,10 +224,10 @@ def do_remove(db, ids): for y in x: db.delete_book(y) - if SingleApplication is not None: - sa = SingleApplication('calibre GUI') - sa.send_message('refreshdb:') - + if send_message is not None: + send_message('refreshdb:', 'calibre GUI') + + def command_remove(args, dbpath): parser = get_parser(_( '''\ @@ -337,10 +339,9 @@ def do_set_metadata(db, id, stream): mi = OPFReader(stream) db.set_metadata(id, mi) do_show_metadata(db, id, False) - if SingleApplication is not None: - sa = SingleApplication('calibre GUI') - sa.send_message('refreshdb:') - + if send_message is not None: + send_message('refreshdb:', 'calibre GUI') + def command_set_metadata(args, dbpath): parser = get_parser(_( ''' diff --git a/src/calibre/utils/single_qt_application.py b/src/calibre/utils/single_qt_application.py index 78171a3dec..846736c507 100644 --- a/src/calibre/utils/single_qt_application.py +++ b/src/calibre/utils/single_qt_application.py @@ -104,7 +104,12 @@ class LocalServer(QLocalServer): return True - +def send_message(msg, name, server_name='calibre_server', timeout=5000): + socket = QLocalSocket() + socket.connectToServer(server_name) + if socket.waitForConnected(timeout_connect): + if read_message(socket) == name: + write_message(socket, name+':'+msg, timeout) class SingleApplication(QObject):