| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/QtUtils/QtUtils.h" |
| 5 | |
| 6 | #include <QDateTimeEdit> |
| 7 | #include <QDesktopServices> |
| 8 | #include <QDir> |
| 9 | #include <QHBoxLayout> |
| 10 | #include <QLabel> |
| 11 | #include <QProcess> |
| 12 | #include <QScreen> |
| 13 | |
| 14 | #if defined(QT_DBUS_LIB) |
| 15 | #include <QDBusConnectionInterface> |
| 16 | #include <QDBusInterface> |
| 17 | #endif |
| 18 | |
| 19 | #include "Common/Logging/Log.h" |
| 20 | #include "Common/StringUtil.h" |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | void ShowFolderOfFile(std::string_view file_path) |
| 26 | { |
| 27 | // Remove everything after the last separator in the game's path, resulting in the parent |
| 28 | // directory path with a trailing separator. Keeping the trailing separator prevents Windows from |
| 29 | // mistakenly opening a .bat or .exe file in the grandparent folder when that file has the same |
| 30 | // base name as the folder (See https://bugs.dolphin-emu.org/issues/12411). |
| 31 | std::string parent_directory_path; |
| 32 | SplitPath(file_path, &parent_directory_path, nullptr, nullptr); |
| 33 | if (parent_directory_path.empty()) |
| 34 | { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | const QUrl url = QUrl::fromLocalFile(QString::fromStdString(parent_directory_path)); |
| 39 | QDesktopServices::openUrl(url); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | namespace QtUtils |
| 45 | { |
| 46 | |
| 47 | void ShowFourDigitYear(QDateTimeEdit* widget) |
| 48 | { |
| 49 | if (!widget->displayFormat().contains(QStringLiteral("yyyy"))) |
| 50 | { |
| 51 | // Always show the full year, no matter what the locale specifies. Otherwise, two-digit years |
| 52 | // will always be interpreted as in the 21st century. |
| 53 | widget->setDisplayFormat( |
| 54 | widget->displayFormat().replace(QStringLiteral("yy"), QStringLiteral("yyyy"))); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | QWidget* CreateIconWarning(QWidget* parent, QStyle::StandardPixmap standard_pixmap, QLabel* label) |
| 59 | { |
| 60 | const auto size = QFontMetrics(parent->font()).height() * 5 / 4; |
| 61 | |
| 62 | auto* const icon = new QLabel{}; |
| 63 | icon->setPixmap(parent->style()->standardIcon(standard_pixmap).pixmap(size, size)); |
| 64 | |
| 65 | auto* const widget = new QWidget; |
| 66 | auto* const layout = new QHBoxLayout{widget}; |
| 67 | layout->addWidget(icon); |
| 68 | layout->addWidget(label, 1); |
| 69 | return widget; |
| 70 | } |
| 71 | |
| 72 | void AdjustSizeWithinScreen(QWidget* widget) |
| 73 | { |
| 74 | const auto screen_size = widget->screen()->availableSize(); |
| 75 | |
| 76 | const auto adj_screen_size = screen_size * 9 / 10; |
| 77 | |
| 78 | widget->resize(widget->sizeHint().boundedTo(adj_screen_size)); |
| 79 | |
| 80 | CenterOnParentWindow(widget); |
| 81 | } |
| 82 | |
| 83 | void CenterOnParentWindow(QWidget* const widget) |
| 84 | { |
| 85 | // Find the top-level window. |
| 86 | const QWidget* const parent_widget{widget->parentWidget()}; |
| 87 | if (!parent_widget) |
| 88 | return; |
| 89 | const QWidget* const window{parent_widget->window()}; |
| 90 | |
| 91 | // Calculate position based on the widgets' size and position. |
| 92 | const QRect window_geometry{window->geometry()}; |
| 93 | const QSize window_size{window_geometry.size()}; |
| 94 | const QPoint window_pos{window_geometry.topLeft()}; |
| 95 | const QRect geometry{widget->geometry()}; |
| 96 | const QSize size{geometry.size()}; |
| 97 | const QPoint offset{(window_size.width() - size.width()) / 2, |
| 98 | (window_size.height() - size.height()) / 2}; |
| 99 | const QPoint pos{window_pos + offset}; |
| 100 | |
| 101 | widget->setGeometry(QRect(pos, size)); |
| 102 | } |
| 103 | |
| 104 | void ShowFileInFolder(std::string_view file_path) |
| 105 | { |
| 106 | #if defined(QT_DBUS_LIB) |
| 107 | QDBusInterface dbus{QString::fromLatin1("org.freedesktop.DBus"), |
| 108 | QString::fromLatin1("/org/freedesktop/DBus"), |
| 109 | QString::fromLatin1("org.freedesktop.DBus")}; |
| 110 | |
| 111 | dbus.call(QString::fromLatin1("StartServiceByName"), |
| 112 | QString::fromLatin1("org.freedesktop.FileManager1"), 0u); |
| 113 | |
| 114 | QDBusInterface iface{QString::fromLatin1("org.freedesktop.FileManager1"), |
| 115 | QString::fromLatin1("/org/freedesktop/FileManager1"), |
| 116 | QString::fromLatin1("org.freedesktop.FileManager1")}; |
| 117 | if (iface.isValid()) |
| 118 | { |
| 119 | QStringList urls; |
| 120 | urls << QUrl::fromLocalFile(QString::fromUtf8(file_path)).toString(); |
| 121 | |
| 122 | const QDBusReply<void> reply = iface.call(QString::fromLatin1("ShowItems"), urls, QString{}); |
| 123 | if (reply.isValid()) |
| 124 | return; |
| 125 | |
| 126 | const auto& err = reply.error(); |
| 127 | WARN_LOG_FMT(COMMON, "DBus call failed: {} - {}", err.name().toStdString(), |
| 128 | err.message().toStdString()); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | WARN_LOG_FMT(COMMON, "Invalid DBus interface: {}", iface.lastError().message().toStdString()); |
| 133 | } |
| 134 | |
| 135 | #elif defined(_WIN32) |
| 136 | if (QProcess{}.startDetached(QString::fromLatin1("explorer.exe"), |
| 137 | {QString::fromLatin1("/select"), QString::fromLatin1(","), |
| 138 | QDir::toNativeSeparators(QString::fromUtf8(file_path))})) |
| 139 | { |
| 140 | return; |
| 141 | } |
| 142 | #elif defined(__APPLE__) |
| 143 | if (QProcess{}.startDetached(QString::fromLatin1("open"), |
| 144 | {QString::fromLatin1("-R"), QString::fromUtf8(file_path)})) |
| 145 | { |
| 146 | return; |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | // Fall back on failure or unsupported platforms. |
| 151 | ShowFolderOfFile(file_path); |
| 152 | } |
| 153 | |
| 154 | } // namespace QtUtils |