https://bugs.kde.org/show_bug.cgi?id=407547

--- Comment #38 from Wodsfort Dragon <[email protected]> ---
(In reply to Leon from comment #37)
> Hello! I noticed this bug has been open for a while regarding Okular not
> opening tabs via File Explorer on Windows.
> 
> Since KDBusService and D-Bus IPC don't translate natively or reliably to
> Windows, it seems double-clicking a file in Explorer forces a new instance
> to spawn rather than communicating with the existing one.
> 
> A potential solution to fix this Windows-specific IPC issue would be to
> bypass KDBusService for Windows builds (#ifdef Q_OS_WIN) and implement a
> QLocalServer and QLocalSocket check in main.cpp. (Since QLocalServer uses
> native Named Pipes on Windows, it bypasses the D-Bus requirement entirely).
> 
> Here is a C++ boilerplate concept of how this could be implemented safely
> using Qt's native cross-process communication:
> 
> C++
> #ifdef Q_OS_WIN
> #include <QLocalServer>
> #include <QLocalSocket>
> #include <QSharedMemory>
> #include <QCoreApplication>
> #include <QStringList>
> 
> // 1. Check if Okular is already running using shared memory.
> // (Required on Windows because QLocalServer allows multiple listeners).
> QSharedMemory shared("OkularSingleInstanceMemory");
> if (!shared.create(512, QSharedMemory::ReadWrite)) {
>     // 2. Okular is already running! Connect to the existing background
> server.
>     QLocalSocket socket;
>     socket.connectToServer("OkularLocalServer");
>     if (socket.waitForConnected(500)) {
>         // 3. Safely grab the file path argument and send it to the running
> app.
>         QStringList args = QCoreApplication::arguments();
>         if (args.count() > 1) {
>             socket.write(args.last().toUtf8()); 
>             socket.waitForBytesWritten();
>         }
>         return 0; // Close this duplicate window silently
>     }
> }
> 
> // 4. If this is the FIRST time opening Okular, start the Local Server.
> QLocalServer* server = new QLocalServer(qApp);
> server->listen("OkularLocalServer");
> QObject::connect(server, &QLocalServer::newConnection, [server]() {
>     QLocalSocket *client = server->nextPendingConnection();
>     QObject::connect(client, &QLocalSocket::readyRead, [client]() {
>         // 5. Read the incoming file path and pass it to the main Shell.
>         QString newFilePath = QString::fromUtf8(client->readAll());
>         
>         // --> TODO: Hook into Okular's internal Shell/Part logic here 
>         // to open 'newFilePath' in a new tab.
>         
>         client->deleteLater();
>     });
> });
> #endif
> 
> I am not a C++ developer so I cannot easily submit a Merge Request on GitLab
> myself, but I wanted to share this architectural concept in hopes it gives
> someone the boilerplate needed to finally patch this for Windows users!
> 
> Thank you to everyone for all your hard work on Okular!


I am not the developer, but looks good. Do you want to open MR, for correcting
it?

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to