IPC socket, single instance, open in existing instance
parent
ba6683cd2d
commit
3b5bdd2e07
2
notes
2
notes
|
@ -37,7 +37,7 @@ TODO {
|
|||
- quit (just trigger close on all sequentially)
|
||||
- if opening project already in other window, focus/flash that window
|
||||
- open project from command line
|
||||
IPC to open/new window in existing instance
|
||||
- IPC to open/new window in existing instance
|
||||
revert action
|
||||
|
||||
xdg mime package + desktop file association
|
||||
|
|
|
@ -9,9 +9,15 @@
|
|||
#include <QDebug>
|
||||
#include <QApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <QLocalServer>
|
||||
#include <QLocalSocket>
|
||||
#include <QSurfaceFormat>
|
||||
#include <QFontDatabase>
|
||||
|
||||
namespace {
|
||||
const QString socketName = QStringLiteral("Xybrid.IPC");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
qRegisterMetaType<Xybrid::Data::Port>();
|
||||
// enable antialiasing on accelerated graphicsview
|
||||
|
@ -24,11 +30,40 @@ int main(int argc, char *argv[]) {
|
|||
QCommandLineParser cl;
|
||||
cl.addHelpOption();
|
||||
cl.addVersionOption();
|
||||
cl.addPositionalArgument("project", QApplication::translate("main", "Project file to open."));
|
||||
cl.addPositionalArgument("[project]", QApplication::translate("main", "Project file to open."));
|
||||
|
||||
cl.process(a);
|
||||
auto args = cl.positionalArguments();
|
||||
QString fileName = args.size() >= 1 ? args.at(0) : QString();
|
||||
if (!fileName.isEmpty()) fileName = QFileInfo(fileName).absoluteFilePath(); // canonicize
|
||||
|
||||
QLocalSocket tryc;
|
||||
tryc.connectToServer(socketName);
|
||||
tryc.waitForConnected(1000); // wait for connection attempt (can't hang on local)
|
||||
if (tryc.isOpen()) { // if server already exists, give it the signal and exit
|
||||
tryc.write(fileName.toUtf8());
|
||||
tryc.waitForBytesWritten();
|
||||
tryc.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
QLocalServer srv;
|
||||
srv.listen(socketName);
|
||||
QObject::connect(&srv, &QLocalServer::newConnection, &srv, [&]() {
|
||||
auto s = srv.nextPendingConnection();
|
||||
s->waitForDisconnected();
|
||||
|
||||
auto fileName = QString::fromUtf8(s->readAll());
|
||||
|
||||
if (auto w = Xybrid::MainWindow::projectWindow(fileName); w) w->tryFocus();
|
||||
else {
|
||||
w = new Xybrid::MainWindow(nullptr, fileName);
|
||||
if (w->getProject()) w->show();
|
||||
else (w->deleteLater());
|
||||
}
|
||||
|
||||
s->deleteLater();
|
||||
});
|
||||
|
||||
// make sure bundled fonts are loaded
|
||||
QFontDatabase::addApplicationFont(":/fonts/iosevka-term-light.ttf");
|
||||
|
|
|
@ -523,6 +523,7 @@ MainWindow::~MainWindow() {
|
|||
}
|
||||
|
||||
MainWindow* MainWindow::projectWindow(const QString &fileName) {
|
||||
if (fileName.isEmpty()) return nullptr;
|
||||
for (auto w : openWindows) if (w->project && w->project->fileName == fileName) return w;
|
||||
return nullptr;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue