IGN:Remove uneccessary dependency on Qt mainloop when messaging the GUI from a commandline app

This commit is contained in:
Kovid Goyal 2008-06-19 23:33:22 -07:00
parent 9f1daa37d6
commit 103f8d91d9
2 changed files with 20 additions and 14 deletions

View File

@ -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
@ -182,9 +185,8 @@ def do_add(db, paths, one_book_per_directory, recurse, add_duplicates):
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,9 +224,9 @@ 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,9 +339,8 @@ 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(_(

View File

@ -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):