Skip to content

Commit

Permalink
FIX(client): Fix Windows unicode paths on writing files with ofstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartmnt committed Nov 30, 2024
1 parent 9ee2f4b commit e12ea49
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/QtUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <QStringList>
#include <QUrl>

#include <boost/filesystem.hpp>

namespace Mumble {
namespace QtUtils {
void deleteQObject(QObject *object) { object->deleteLater(); }
Expand All @@ -23,6 +25,9 @@ namespace QtUtils {
return QString();
}

boost::filesystem::path qstring_to_path(const QString &input) {
return boost::filesystem::path(input.toUtf8().data());
}

} // namespace QtUtils
} // namespace Mumble
7 changes: 7 additions & 0 deletions src/QtUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <QString>
#include <QStringList>

#include <boost/filesystem.hpp>

#include <memory>

class QObject;
Expand All @@ -34,6 +36,11 @@ namespace QtUtils {
*/
QString decode_first_utf8_qssl_string(const QStringList &list);

/**
* Creates a platform agnostic path from a QString
*/
boost::filesystem::path qstring_to_path(const QString &input);

} // namespace QtUtils
} // namespace Mumble

Expand Down
2 changes: 2 additions & 0 deletions src/mumble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ if(WIN32)

find_pkg(Boost
COMPONENTS
filesystem
system
thread
REQUIRED
Expand Down Expand Up @@ -630,6 +631,7 @@ if(WIN32)

target_link_libraries(mumble_client_object_lib
PUBLIC
Boost::filesystem
Boost::system
Boost::thread
)
Expand Down
7 changes: 5 additions & 2 deletions src/mumble/PluginInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "PluginInstaller.h"
#include "PluginManager.h"
#include "PluginManifest.h"
#include "QtUtils.h"
#include "Global.h"

#include <QMessageBox>
Expand All @@ -18,9 +19,10 @@
#include <QtGui/QIcon>

#include <exception>
#include <fstream>
#include <string>

#include <boost/filesystem/fstream.hpp>

#include <Poco/Exception.h>
#include <Poco/FileStream.h>
#include <Poco/StreamCopier.h>
Expand Down Expand Up @@ -127,7 +129,8 @@ void PluginInstaller::init() {

zipInput.clear();
Poco::Zip::ZipInputStream zipin(zipInput, pluginIt->second);
std::ofstream out(tmpPluginPath.toStdString(), std::ios::out | std::ios::binary);
boost::filesystem::ofstream out(Mumble::QtUtils::qstring_to_path(tmpPluginPath),
std::ios::out | std::ios::binary);
Poco::StreamCopier::copyStream(zipin, out);

m_pluginSource = QFileInfo(tmpPluginPath);
Expand Down
18 changes: 10 additions & 8 deletions src/mumble/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "EnvUtils.h"
#include "JSONSerialization.h"
#include "Log.h"
#include "QtUtils.h"
#include "SSL.h"
#include "SettingsKeys.h"
#include "SettingsMacros.h"
Expand All @@ -33,12 +34,12 @@
#include <QStandardPaths>
#include <QSystemTrayIcon>

#include <boost/filesystem/fstream.hpp>
#include <boost/typeof/typeof.hpp>

#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <limits>
#include <memory>

Expand Down Expand Up @@ -155,11 +156,12 @@ void Settings::save(const QString &path) const {
QFile tmpFile(QString::fromLatin1("%1/mumble_settings.json.tmp")
.arg(QStandardPaths::writableLocation(QStandardPaths::TempLocation)));

{
// The separate scope makes sure, the stream is closed again after the write has finished
std::ofstream stream(tmpFile.fileName().toUtf8());
boost::filesystem::ofstream stream(Mumble::QtUtils::qstring_to_path(tmpFile.fileName()));
stream << settingsJSON.dump(4) << std::endl;
stream.close();

stream << settingsJSON.dump(4) << std::endl;
if (stream.fail()) {
qWarning("Failed at writing temporary settings file: %s", qUtf8Printable(tmpFile.fileName()));
}

QFile targetFile(path);
Expand Down Expand Up @@ -222,7 +224,7 @@ void Settings::load(const QString &path) {
settingsLocation = path;
}

std::ifstream stream(path.toUtf8());
boost::filesystem::ifstream stream(Mumble::QtUtils::qstring_to_path(path));

nlohmann::json settingsJSON;
try {
Expand Down Expand Up @@ -611,13 +613,13 @@ void OverlaySettings::savePresets(const QString &filename) {
settingsJSON.erase(SettingsKeys::OVERLAY_LAUNCHERS_KEY);
settingsJSON.erase(SettingsKeys::OVERLAY_LAUNCHERS_EXCLUDE_KEY);

std::ofstream stream(filename.toUtf8());
boost::filesystem::ofstream stream(Mumble::QtUtils::qstring_to_path(filename));

stream << settingsJSON.dump(4) << std::endl;
}

void OverlaySettings::load(const QString &filename) {
std::ifstream stream(filename.toUtf8());
boost::filesystem::ifstream stream(Mumble::QtUtils::qstring_to_path(filename));

nlohmann::json settingsJSON;
try {
Expand Down
11 changes: 11 additions & 0 deletions src/murmur/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ if(WIN32)

find_pkg(Qt6 COMPONENTS Widgets REQUIRED)

find_pkg(Boost
COMPONENTS
filesystem
REQUIRED
)

target_link_libraries(mumble-server
PRIVATE
Qt6::Widgets
Expand All @@ -135,6 +141,11 @@ if(WIN32)
rpcrt4.lib
)

target_link_libraries(mumble-server
PUBLIC
Boost::filesystem
)

if(static AND TARGET Qt6::QWindowsIntegrationPlugin)
include_qt_plugin(mumble-server PRIVATE QWindowsIntegrationPlugin)
target_link_libraries(mumble-server PRIVATE Qt6::QWindowsIntegrationPlugin)
Expand Down

0 comments on commit e12ea49

Please sign in to comment.