Beispiel #1
0
def run_main(app, opts, args, gui_debug, si):
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            else:
                return run_gui(opts, args, listener, app, gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app, gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #2
0
def main(args=sys.argv):
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        from calibre.utils.lock import singleinstance
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(
            None,
            _('Cannot start calibre'),
            _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information'
              ),
            det_msg=traceback.format_exc(),
            show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            else:
                return run_gui(opts, args, listener, app, gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app, gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #3
0
def main(args=sys.argv):
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    try:
        app, opts, args, actions = init_qt(args)
    except AbortInit:
        return 1
    from calibre.utils.lock import singleinstance
    from multiprocessing.connection import Listener
    si = singleinstance('calibre GUI')
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = Listener(address=gui_socket_address())
        except socket.error:
            if iswindows:
                cant_start()
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = Listener(address=gui_socket_address())
            except socket.error:
                cant_start()
            else:
                return run_gui(opts,
                               args,
                               actions,
                               listener,
                               app,
                               gui_debug=gui_debug)
        else:
            return run_gui(opts,
                           args,
                           actions,
                           listener,
                           app,
                           gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = Listener(address=gui_socket_address())
    except socket.error:  # Good si is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #4
0
def main(args=sys.argv):
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        from calibre.utils.lock import singleinstance
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(None, _('Cannot start calibre'), _(
            'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'),
                     det_msg=traceback.format_exc(), show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            else:
                return run_gui(opts, args, listener, app,
                        gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app,
                    gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #5
0
def cant_start(
        msg=_('If you are sure it is not running') + ', ',
        det_msg=_('Timed out waiting for response from running calibre'),
        listener_failed=False):
    base = '<p>%s</p><p>%s %s'
    where = __appname__ + ' ' + _(
        'may be running in the system tray, in the') + ' '
    if isosx:
        where += _('upper right region of the screen.')
    else:
        where += _('lower right region of the screen.')
    if iswindows or islinux:
        what = _('try rebooting your computer.')
    else:
        if listener_failed:
            path = gui_socket_address()
        else:
            from calibre.utils.lock import singleinstance_path
            path = singleinstance_path(singleinstance_name)
        what = _('try deleting the file: "%s"') % path

    info = base % (where, msg, what)
    error_dialog(None,
                 _('Cannot start ') + __appname__,
                 '<p>' + (_('%s is already running.') % __appname__) + '</p>' +
                 info,
                 det_msg=det_msg,
                 show=True)

    raise SystemExit(1)
Beispiel #6
0
def send_message_in_process(msg, address=None, timeout=5):
    address = address or gui_socket_address()
    if isinstance(msg, str):
        msg = msg.encode('utf-8')
    s = QLocalSocket()
    qt_timeout = int(timeout * 1000)
    if address.startswith('\0'):
        ps = unix_socket(timeout)
        ps.connect(address)
        s.setSocketDescriptor(ps.detach())
    else:
        s.connectToServer(address)
        if not s.waitForConnected(qt_timeout):
            raise OSError(
                f'Failed to connect to Listener at: {address} with error: {s.errorString()}'
            )
    data = QByteArray(msg)
    while True:
        written = s.write(data)
        if not s.waitForBytesWritten(qt_timeout):
            raise OSError(
                f'Failed to write data to address: {s.serverName()} with error: {s.errorString()}'
            )
        if written >= len(data):
            break
        data = data.right(len(data) - written)
Beispiel #7
0
def main(args=sys.argv):
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    try:
        app, opts, args, actions = init_qt(args)
    except AbortInit:
        return 1
    from calibre.utils.lock import singleinstance
    from multiprocessing.connection import Listener
    si = singleinstance('calibre GUI')
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = Listener(address=gui_socket_address())
        except socket.error:
            if iswindows:
                cant_start()
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = Listener(address=gui_socket_address())
            except socket.error:
                cant_start()
            else:
                return run_gui(opts, args, actions, listener, app,
                        gui_debug=gui_debug)
        else:
            return run_gui(opts, args, actions, listener, app,
                    gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = Listener(address=gui_socket_address())
    except socket.error:  # Good si is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #8
0
 def __init__(self, address=None, parent=None):
     QLocalServer.__init__(self, parent)
     self.address = address or gui_socket_address()
     self.uses_filesystem = self.address[0] not in '\0\\'
     self.setSocketOptions(QLocalServer.SocketOption.UserAccessOption)
     self.newConnection.connect(self.on_new_connection)
     self.connection_id = count()
     self.pending_messages = {}
Beispiel #9
0
def send_message(msg='', timeout=3):
  print >>sys.stderr, 'info: Notifying the Calibre GUI of the change.'
  try:
    t = multiprocessing.connection.Client(ipc.gui_socket_address())
  except socket.error, e:
    if e[0] != errno.ENOENT:  # TODO(pts): Make this work on Windows.
      raise
    # We ignore the exception if Calibre is not running.
    return
Beispiel #10
0
def cant_start(msg=_('If you are sure it is not running')+', ',
               det_msg=_('Timed out waiting for response from running calibre')):
    base = '<p>%s</p><p>%s %s'
    where = __appname__ + ' '+_('may be running in the system tray, in the')+' '
    if isosx:
        where += _('upper right region of the screen.')
    else:
        where += _('lower right region of the screen.')
    if iswindows or islinux:
        what = _('try rebooting your computer.')
    else:
        what = _('try deleting the file')+': '+ gui_socket_address()

    info = base%(where, msg, what)
    error_dialog(None, _('Cannot Start ')+__appname__,
        '<p>'+(_('%s is already running.')%__appname__)+'</p>'+info, det_msg=det_msg, show=True)

    raise SystemExit(1)
Beispiel #11
0
def cant_start(msg=_('If you are sure it is not running')+', ',
        what=None):
    base = '<p>%s</p><p>%s %s'
    where = __appname__ + ' '+_('may be running in the system tray, in the')+' '
    if isosx:
        where += _('upper right region of the screen.')
    else:
        where += _('lower right region of the screen.')
    if what is None:
        if iswindows:
            what = _('try rebooting your computer.')
        else:
            what = _('try deleting the file')+': '+ gui_socket_address()

    info = base%(where, msg, what)
    error_dialog(None, _('Cannot Start ')+__appname__,
        '<p>'+(_('%s is already running.')%__appname__)+'</p>'+info, show=True)

    raise SystemExit(1)
Beispiel #12
0
def cant_start(msg=_("If you are sure it is not running") + ", ", what=None):
    base = "<p>%s</p><p>%s %s"
    where = __appname__ + " " + _("may be running in the system tray, in the") + " "
    if isosx:
        where += _("upper right region of the screen.")
    else:
        where += _("lower right region of the screen.")
    if what is None:
        if iswindows:
            what = _("try rebooting your computer.")
        else:
            what = _("try deleting the file") + ": " + gui_socket_address()

    info = base % (where, msg, what)
    error_dialog(
        None,
        _("Cannot Start ") + __appname__,
        "<p>" + (_("%s is already running.") % __appname__) + "</p>" + info,
        show=True,
    )

    raise SystemExit(1)
Beispiel #13
0
def cant_start(msg=_('If you are sure it is not running')+', ',
               det_msg=_('Timed out waiting for response from running calibre'),
               listener_failed=False):
    base = '<p>%s</p><p>%s %s'
    where = __appname__ + ' '+_('may be running in the system tray, in the')+' '
    if isosx:
        where += _('upper right region of the screen.')
    else:
        where += _('lower right region of the screen.')
    if iswindows or islinux:
        what = _('try rebooting your computer.')
    else:
        if listener_failed:
            path = gui_socket_address()
        else:
            from calibre.utils.lock import singleinstance_path
            path = singleinstance_path(singleinstance_name)
        what = _('try deleting the file: "%s"') % path

    info = base%(where, msg, what)
    error_dialog(None, _('Cannot Start ')+__appname__,
        '<p>'+(_('%s is already running.')%__appname__)+'</p>'+info, det_msg=det_msg, show=True)

    raise SystemExit(1)
Beispiel #14
0
def main(args=sys.argv):
    if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ:
        windows_repair()
        return 0
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    if iswindows:
        # Ensure that all ebook editor instances are grouped together in the task
        # bar. This prevents them from being grouped with viewer process when
        # launched from within calibre, as both use calibre-parallel.exe
        import ctypes
        try:
            ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('com.calibre-ebook.main-gui')
        except Exception:
            pass  # Only available on windows 7 and newer

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        from calibre.utils.lock import singleinstance
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(None, _('Cannot start calibre'), _(
            'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'),
                     det_msg=traceback.format_exc(), show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            else:
                return run_gui(opts, args, listener, app,
                        gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app,
                    gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #15
0
def main(args=sys.argv):
    if os.environ.pop('CALIBRE_RESTARTING_FROM_GUI',
                      None) == environ_item('1'):
        time.sleep(2)  # give the parent process time to cleanup and close
    if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ:
        windows_repair()
        return 0
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(
            None,
            _('Cannot start calibre'),
            _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information'
              ),
            det_msg=traceback.format_exc(),
            show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            else:
                return run_gui(opts, args, listener, app, gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app, gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #16
0
def create_listener():
    if islinux:
        from calibre.utils.ipc.server import LinuxListener as Listener
    else:
        from multiprocessing.connection import Listener
    return Listener(address=gui_socket_address())
Beispiel #17
0
def create_listener():
    if islinux:
        from calibre.utils.ipc.server import LinuxListener as Listener
    else:
        from multiprocessing.connection import Listener
    return Listener(address=gui_socket_address())
Beispiel #18
0
def main(args=sys.argv):
    if os.environ.pop(b'CALIBRE_RESTARTING_FROM_GUI', None) == b'1':
        time.sleep(2)  # give the parent process time to cleanup and close
    if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ:
        windows_repair()
        return 0
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    if iswindows:
        # Ensure that all ebook editor instances are grouped together in the task
        # bar. This prevents them from being grouped with viewer process when
        # launched from within calibre, as both use calibre-parallel.exe
        set_app_uid(MAIN_APP_UID)

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(None, _('Cannot start calibre'), _(
            'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'),
                     det_msg=traceback.format_exc(), show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(), listener_failed=True)
            else:
                return run_gui(opts, args, listener, app,
                        gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app,
                    gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0
Beispiel #19
0
def main(args=sys.argv):
    if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ:
        windows_repair()
        return 0
    gui_debug = None
    if args[0] == '__CALIBRE_GUI_DEBUG__':
        gui_debug = args[1]
        args = ['calibre']

    if iswindows:
        # Ensure that all ebook editor instances are grouped together in the task
        # bar. This prevents them from being grouped with viewer process when
        # launched from within calibre, as both use calibre-parallel.exe
        import ctypes
        try:
            ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
                'com.calibre-ebook.main-gui')
        except Exception:
            pass  # Only available on windows 7 and newer

    try:
        app, opts, args = init_qt(args)
    except AbortInit:
        return 1
    try:
        from calibre.utils.lock import singleinstance
        si = singleinstance(singleinstance_name)
    except Exception:
        error_dialog(
            None,
            _('Cannot start calibre'),
            _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information'
              ),
            det_msg=traceback.format_exc(),
            show=True)
        return 1
    if si and opts.shutdown_running_calibre:
        return 0
    if si:
        try:
            listener = create_listener()
        except socket.error:
            if iswindows or islinux:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            if os.path.exists(gui_socket_address()):
                os.remove(gui_socket_address())
            try:
                listener = create_listener()
            except socket.error:
                cant_start(det_msg=traceback.format_exc(),
                           listener_failed=True)
            else:
                return run_gui(opts, args, listener, app, gui_debug=gui_debug)
        else:
            return run_gui(opts, args, listener, app, gui_debug=gui_debug)
    otherinstance = False
    try:
        listener = create_listener()
    except socket.error:  # Good singleinstance is correct (on UNIX)
        otherinstance = True
    else:
        # On windows only singleinstance can be trusted
        otherinstance = True if iswindows else False
    if not otherinstance and not opts.shutdown_running_calibre:
        return run_gui(opts, args, listener, app, gui_debug=gui_debug)

    communicate(opts, args)

    return 0