Send cwd over IPC. Fixes #254.

This commit is contained in:
Florian Bruhin 2014-11-30 19:22:35 +01:00
parent 9327c2a9ae
commit e3dfaa6a4b
3 changed files with 24 additions and 10 deletions

View File

@ -258,7 +258,7 @@ class Application(QApplication):
self.alert(window_to_raise)
return win_id
def process_args(self, args, via_ipc=False):
def process_args(self, args, via_ipc=False, cwd=None):
"""Process commandline args.
URLs to open have no prefix, commands to execute begin with a colon.
@ -266,6 +266,7 @@ class Application(QApplication):
Args:
args: A list of arguments to process.
via_ipc: Whether the arguments were transmitted over IPC.
cwd: The cwd to use for fuzzy_url.
"""
if ipc and not args:
win_id = self._get_window(via_ipc, force_window=True)
@ -288,7 +289,7 @@ class Application(QApplication):
window=win_id)
log.init.debug("Startup URL {}".format(cmd))
try:
url = urlutils.fuzzy_url(cmd)
url = urlutils.fuzzy_url(cmd, cwd)
except urlutils.FuzzyUrlError as e:
message.error(0, "Error in startup argument '{}': "
"{}".format(cmd, e))

View File

@ -19,6 +19,7 @@
"""Utilities for IPC with existing instances."""
import os
import json
import getpass
import binascii
@ -138,15 +139,22 @@ class IPCServer(QObject):
log.ipc.debug("invalid data: {}".format(
binascii.hexlify(data)))
return
log.ipc.debug("Processing: {}".format(decoded))
try:
args = json.loads(decoded)
json_data = json.loads(decoded)
except ValueError:
log.ipc.error("Ignoring invalid IPC data.")
log.ipc.debug("invalid json: {}".format(decoded.strip()))
return
log.ipc.debug("Processing: {}".format(decoded))
try:
args = json_data['args']
cwd = json_data['cwd']
except KeyError:
log.ipc.error("Ignoring invalid IPC data.")
log.ipc.debug("no args/cwd: {}".format(decoded.strip()))
return
app = objreg.get('app')
app.process_args(args, via_ipc=True)
app.process_args(args, via_ipc=True, cwd=cwd)
@pyqtSlot()
def on_timeout(self):
@ -199,7 +207,8 @@ def send_to_running_instance(cmdlist):
connected = socket.waitForConnected(100)
if connected:
log.ipc.info("Opening in existing instance")
line = json.dumps(cmdlist) + '\n'
json_data = {'args': cmdlist, 'cwd': os.getcwd()}
line = json.dumps(json_data) + '\n'
data = line.encode('utf-8')
log.ipc.debug("Writing: {}".format(data))
socket.writeData(data)

View File

@ -124,27 +124,31 @@ def _is_url_dns(url):
return not info.error()
def fuzzy_url(urlstr):
def fuzzy_url(urlstr, cwd=None):
"""Get a QUrl based on an user input which is URL or search term.
Args:
urlstr: URL to load as a string.
cwd: The current working directory, or None.
Return:
A target QUrl to a searchpage or the original URL.
"""
path = os.path.abspath(os.path.expanduser(urlstr))
if cwd:
path = os.path.join(cwd, os.path.expanduser(urlstr))
else:
path = os.path.abspath(os.path.expanduser(urlstr))
stripped = urlstr.strip()
if os.path.exists(path):
log.url.debug("URL is a local file")
url = QUrl.fromLocalFile(path)
elif (not _has_explicit_scheme(QUrl(urlstr)) and
os.path.exists(os.path.abspath(path))):
os.path.exists(path)):
# We do this here rather than in the first block because we first want
# to make sure it's not an URL like http://, because os.path.abspath
# would mangle that.
log.url.debug("URL is a relative local file")
url = QUrl.fromLocalFile(os.path.abspath(path))
url = QUrl.fromLocalFile(path)
elif is_url(stripped):
# probably an address
log.url.debug("URL is a fuzzy address")