From 2ea08319f8f53b666cd7a7d4a855ae5e56ad1168 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 25 Nov 2015 21:55:05 +0100 Subject: [PATCH] GUI: recode log file encoding of host name to UTF-8 See https://bugreports.qt.io/browse/QTBUG-49640 for the bug description and rationale. --- ChangeLog | 8 ++++++++ src/mkvtoolnix-gui/app.cpp | 41 +++++++++++++++++++++++++++++++++++++- src/mkvtoolnix-gui/app.h | 2 ++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 45fc26776..1a28b2cbe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2015-11-25 Moritz Bunkus + + * MKVToolNix GUI: bug fix: implemented a workaround for a bug in + Qt which caused the GUI not to start anymore due to failing to + detect a stale lock file if the GUI had crashed before on a + computer with a host name that included non-ASCII characters. See + https://bugreports.qt.io/browse/QTBUG-49640 + 2015-11-22 Moritz Bunkus * mkvmerge: bug fix: a track's number of bits per audio sample diff --git a/src/mkvtoolnix-gui/app.cpp b/src/mkvtoolnix-gui/app.cpp index d3a9fdf5c..c561df258 100644 --- a/src/mkvtoolnix-gui/app.cpp +++ b/src/mkvtoolnix-gui/app.cpp @@ -1,6 +1,7 @@ #include "common/common_pch.h" #include +#include #include #include #include @@ -65,6 +66,41 @@ App::App(int &argc, App::~App() { } +void +App::fixLockFileHostName(QString const &lockFilePath) { + // Due to a bug in Qt up to and including v5.5.1 the lock file + // contains the host name in the wrong encoding. If the host name + // contains non-ASCII characters stale lock file detection will + // fail. See https://bugreports.qt.io/browse/QTBUG-49640 + + if (!QFileInfo{lockFilePath}.exists()) + return; + + QFile lockFile{lockFilePath}; + if (!lockFile.open(QIODevice::ReadWrite)) + return; + + auto const alreadyFixed = Q("alreadyFixedByMKVToolNix"); + auto lines = QList{}; + + for (int idx = 0; idx < 4; ++idx) + lines << lockFile.readLine(); + + if (QString::fromLocal8Bit(lines[3]) == alreadyFixed) + return; + + lines[2].chop(1); + lines[2] = Q("%1\n").arg(QString::fromLocal8Bit(lines[2])).toUtf8(); + lines[3] = alreadyFixed.toLocal8Bit(); + + lockFile.seek(0); + + for (auto const &line : lines) + lockFile.write(line); + + lockFile.resize(lockFile.pos()); +} + QString App::communicatorSocketName() { return Q("MKVToolNix-GUI-Instance-Communicator"); @@ -74,7 +110,10 @@ void App::setupInstanceCommunicator() { auto socketName = communicatorSocketName(); auto lockFilePath = QDir{QDir::tempPath()}.filePath(Q("%1.lock").arg(socketName)); - m_instanceLock = std::make_unique(lockFilePath); + + fixLockFileHostName(lockFilePath); + + m_instanceLock = std::make_unique(lockFilePath); m_instanceLock->setStaleLockTime(0); if (!m_instanceLock->tryLock(0)) { diff --git a/src/mkvtoolnix-gui/app.h b/src/mkvtoolnix-gui/app.h index 4853656dc..df2dad677 100644 --- a/src/mkvtoolnix-gui/app.h +++ b/src/mkvtoolnix-gui/app.h @@ -87,6 +87,8 @@ public: static QString communicatorSocketName(); static QString settingsBaseGroupName(); + + static void fixLockFileHostName(QString const &lockFilePath); }; }}