Bump to 4.6.7
[qBittorrent.git] / src / gui / utils.cpp
blob4a775689f393785e77e4a82210892d32073c6eac
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2017 Mike Tzou
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "utils.h"
32 #include <QtGlobal>
34 #ifdef Q_OS_WIN
35 #include <Objbase.h>
36 #include <Shlobj.h>
37 #include <Shellapi.h>
38 #endif
40 #include <QApplication>
41 #include <QDesktopServices>
42 #include <QIcon>
43 #include <QPixmap>
44 #include <QPixmapCache>
45 #include <QPoint>
46 #include <QProcess>
47 #include <QRegularExpression>
48 #include <QScreen>
49 #include <QSize>
50 #include <QStyle>
51 #include <QThread>
52 #include <QUrl>
53 #include <QWidget>
54 #include <QWindow>
56 #include "base/global.h"
57 #include "base/path.h"
58 #include "base/utils/fs.h"
59 #include "base/utils/version.h"
61 QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const QWidget *widget, const int height)
63 Q_UNUSED(widget); // TODO: remove it
64 Q_ASSERT(height > 0);
66 return icon.pixmap(height);
69 QPixmap Utils::Gui::scaledPixmap(const Path &path, const QWidget *widget, const int height)
71 Q_UNUSED(widget);
72 Q_ASSERT(height >= 0);
74 const QPixmap pixmap {path.data()};
75 return (height == 0) ? pixmap : pixmap.scaledToHeight(height, Qt::SmoothTransformation);
78 QSize Utils::Gui::smallIconSize(const QWidget *widget)
80 // Get DPI scaled icon size (device-dependent), see QT source
81 // under a 1080p screen is usually 16x16
82 const int s = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget);
83 return {s, s};
86 QSize Utils::Gui::mediumIconSize(const QWidget *widget)
88 // under a 1080p screen is usually 24x24
89 return ((smallIconSize(widget) + largeIconSize(widget)) / 2);
92 QSize Utils::Gui::largeIconSize(const QWidget *widget)
94 // Get DPI scaled icon size (device-dependent), see QT source
95 // under a 1080p screen is usually 32x32
96 const int s = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, widget);
97 return {s, s};
100 QPoint Utils::Gui::screenCenter(const QWidget *w)
102 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
104 if (!w)
105 return {};
107 QRect r = QGuiApplication::primaryScreen()->availableGeometry();
108 const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
110 const QWidget *parent = w->parentWidget();
111 if (!parent)
112 return primaryScreenCenter;
114 const QWindow *window = parent->window()->windowHandle();
115 if (!window)
116 return primaryScreenCenter;
118 const QScreen *screen = window->screen();
119 if (!screen)
120 return primaryScreenCenter;
122 r = screen->availableGeometry();
123 return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
126 // Open the given path with an appropriate application
127 void Utils::Gui::openPath(const Path &path)
129 // Hack to access samba shares with QDesktopServices::openUrl
130 const QUrl url = path.data().startsWith(u"//")
131 ? QUrl(u"file:" + path.data())
132 : QUrl::fromLocalFile(path.data());
134 #ifdef Q_OS_WIN
135 auto *thread = QThread::create([path]()
137 if (SUCCEEDED(::CoInitializeEx(NULL, (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))))
139 const std::wstring pathWStr = path.toString().toStdWString();
141 ::ShellExecuteW(nullptr, nullptr, pathWStr.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
143 ::CoUninitialize();
146 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
147 thread->start();
148 #else
149 QDesktopServices::openUrl(url);
150 #endif
153 // Open the parent directory of the given path with a file manager and select
154 // (if possible) the item at the given path
155 void Utils::Gui::openFolderSelect(const Path &path)
157 // If the item to select doesn't exist, try to open its parent
158 if (!path.exists())
160 openPath(path.parentPath());
161 return;
164 #ifdef Q_OS_WIN
165 auto *thread = QThread::create([path]()
167 if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)))
169 const std::wstring pathWStr = path.toString().toStdWString();
170 PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(pathWStr.c_str());
171 if (pidl)
173 ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
174 ::ILFree(pidl);
177 ::CoUninitialize();
180 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
181 thread->start();
182 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
183 const int lineMaxLength = 64;
185 QProcess proc;
186 proc.start(u"xdg-mime"_s, {u"query"_s, u"default"_s, u"inode/directory"_s});
187 proc.waitForFinished();
188 const auto output = QString::fromLocal8Bit(proc.readLine(lineMaxLength).simplified());
189 if ((output == u"dolphin.desktop") || (output == u"org.kde.dolphin.desktop"))
191 proc.startDetached(u"dolphin"_s, {u"--select"_s, path.toString()});
193 else if ((output == u"nautilus.desktop") || (output == u"org.gnome.Nautilus.desktop")
194 || (output == u"nautilus-folder-handler.desktop"))
196 proc.start(u"nautilus"_s, {u"--version"_s});
197 proc.waitForFinished();
198 const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine(lineMaxLength)).remove(QRegularExpression(u"[^0-9.]"_s));
199 using NautilusVersion = Utils::Version<3>;
200 if (NautilusVersion::fromString(nautilusVerStr, {1, 0, 0}) > NautilusVersion(3, 28, 0))
201 proc.startDetached(u"nautilus"_s, {(Fs::isDir(path) ? path.parentPath() : path).toString()});
202 else
203 proc.startDetached(u"nautilus"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
205 else if (output == u"nemo.desktop")
207 proc.startDetached(u"nemo"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
209 else if ((output == u"konqueror.desktop") || (output == u"kfmclient_dir.desktop"))
211 proc.startDetached(u"konqueror"_s, {u"--select"_s, path.toString()});
213 else
215 // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
216 openPath(path.parentPath());
218 #else
219 openPath(path.parentPath());
220 #endif
223 QString Utils::Gui::tagToWidgetText(const QString &tag)
225 return QString(tag).replace(u'&', u"&&"_s);
228 QString Utils::Gui::widgetTextToTag(const QString &text)
230 // replace pairs of '&' with single '&' and remove non-paired occurrences of '&'
231 QString cleanedText;
232 cleanedText.reserve(text.size());
233 bool amp = false;
234 for (const QChar c : text)
236 if (c == u'&')
238 amp = !amp;
239 if (amp)
240 continue;
243 cleanedText.append(c);
246 return cleanedText;