Skip to content

Commit

Permalink
feat: backup window-layout (#5647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Nov 24, 2024
1 parent 9b7007b commit c8bafbd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Minor: Mentions of FrankerFaceZ and BetterTTV in settings are standardized as such. (#5698)
- Minor: Emote names are no longer duplicated when using smarter emote completion. (#5705)
- Minor: Added a setting to hide the scrollbar thumb (the handle you can drag). Hiding the scrollbar thumb will disable mouse click & drag interactions in the scrollbar. (#5731)
- Minor: The window layout is now backed up like the other settings. (#5647)
- Bugfix: Fixed tab move animation occasionally failing to start after closing a tab. (#5426, #5612)
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Bugfix: Fixed restricted users usernames not being clickable. (#5405)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ set(SOURCE_FILES
util/DisplayBadge.cpp
util/DisplayBadge.hpp
util/Expected.hpp
util/FilesystemHelpers.hpp
util/FormatTime.cpp
util/FormatTime.hpp
util/FunctionEventFilter.cpp
Expand Down
40 changes: 28 additions & 12 deletions src/singletons/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "util/CombinePath.hpp"
#include "util/FilesystemHelpers.hpp"
#include "util/SignalListener.hpp"
#include "widgets/AccountSwitchPopup.hpp"
#include "widgets/dialogs/SettingsDialog.hpp"
Expand All @@ -21,6 +22,7 @@
#include "widgets/splits/SplitContainer.hpp"
#include "widgets/Window.hpp"

#include <pajlada/settings/backup.hpp>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
Expand Down Expand Up @@ -516,19 +518,33 @@ void WindowManager::save()
document.setObject(obj);

// save file
QSaveFile file(this->windowLayoutFilePath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);

QJsonDocument::JsonFormat format =
#ifdef _DEBUG
QJsonDocument::JsonFormat::Compact
#else
(QJsonDocument::JsonFormat)0
#endif
;
std::error_code ec;
pajlada::Settings::Backup::saveWithBackup(
qStringToStdPath(this->windowLayoutFilePath),
{.enabled = true, .numSlots = 9},
[&](const auto &path, auto &ec) {
QSaveFile file(stdPathToQString(path));
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
ec = std::make_error_code(std::errc::io_error);
return;
}

file.write(document.toJson(QJsonDocument::Indented));
if (!file.commit() || file.error() != QFile::NoError)
{
ec = std::make_error_code(std::errc::io_error);
}
},
ec);

file.write(document.toJson(format));
file.commit();
if (ec)
{
// TODO(Qt 6.5): drop fromStdString
qCWarning(chatterinoWindowmanager)
<< "Failed to save windowlayout"
<< QString::fromStdString(ec.message());
}
}

void WindowManager::sendAlert()
Expand Down
26 changes: 26 additions & 0 deletions src/util/FilesystemHelpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <QFileDevice>
#include <QString>

#include <filesystem>

namespace chatterino {

inline QString stdPathToQString(const std::filesystem::path &path)
{
#ifdef Q_OS_WIN
return QString::fromStdWString(path.native());
#else
return QString::fromStdString(path.native());
#endif
}

inline std::filesystem::path qStringToStdPath(const QString &path)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto *ptr = reinterpret_cast<const char16_t *>(path.utf16());
return {ptr, ptr + path.size()};
}

} // namespace chatterino

0 comments on commit c8bafbd

Please sign in to comment.