Wednesday, 7 January 2015

QtSingleApplication replacement for Qt5

Kadu uses QtSingleApplication to ensure that only one instance can run on one profile at a time.

Unfortunately this solution is not compatible with Qt 5, so I've decided to update it a bit. My solution is available at gitorious. It uses code from Nokia solution, so I hope I got the licencing and copyright right (it is LGPL - one of the licences from Nokia's original files).

It has different interface from original one - 3 lambdas are accepted as constructor parameters. One for running first instance, one for running second and one for accepting messages. Not very Qt-ish but possibly less error prone.

11 comments:

  1. What about this:

    https://qt.gitorious.org/qt-solutions/qt-solutions/commit/c50d0f3f142606b823f9106c7cd8b9a7d066e6ff

    ReplyDelete
    Replies
    1. I was not aware of it. But for Qt5 it should use QLockFile instead of its own QtLockedFile.

      Delete
    2. Might be worth coordinating with the qt-solutions folks to merge the best of both implementations into the one. Probably makes sense for it to live in the qt-solutions repo since that's the original home of QtSingleApplication.

      Delete
  2. What's the lockfile for exactly? I'm currently simply using a QLocalServer/QLocalSocket without lock files and I've not noticed any problems so far.

    Also you could use QLocalServer::removeServer to remove a stale file instead of using QDir/QFile I believe.

    ReplyDelete
    Replies
    1. Not sure, it is based on QtSolutions (cargo-cult programming), so I only replaced QtLockedFile with QLockFile and changed main class to have different workflow.

      Liunx version has also this line to remove stale files, so it looks like it requires another lock file: https://gitorious.org/kadu/kadu/source/ac0b7e03ccf15511c27fcc1776fee5e1743ba21d:kadu-core/os/single-application/single-application.cpp#L142

      QLocalServer::removeServer does not seem to check if existing server is still running. From docs: Warning: Be careful to avoid removing sockets of running instances.

      Delete
  3. There's also KDE's KDBusService library in Frameworks 5 that implements single-instance behavior. It doesn't have any dependencies other than Qt.

    ReplyDelete
    Replies
    1. It has dependency on DBus. And Kadu has Windows version. I do not want to pull DBus to Windows.

      Delete
  4. QtSingleApplication is BSD not LGPL.

    ReplyDelete
    Replies
    1. https://qt.gitorious.org/qt-creator/qt-creator/source/f61e8672db50445a3f84d37e657d38fbaa0d5083:shared/qtsingleapplication/qtsingleapplication.cpp

      I see GPL and LGPL here.

      Delete
    2. I accidentally posted a new comment instead of a reply, it is shown below.

      Delete
  5. That's the creator fork. For QtSingleApplication see:

    https://qt.gitorious.org/qt-solutions/qt-solutions/source/38e79e3f04d6fd5a3df585a60b2aec95e8e68368:qtsingleapplication/src/qtsingleapplication.cpp

    ReplyDelete