Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / utils.cpp
blobf310c7f269e3f54e22b8a4c8b6134725e45005ae
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 <QtSystemDetection>
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/tag.h"
59 #include "base/utils/fs.h"
60 #include "base/utils/version.h"
62 QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const int height)
64 Q_ASSERT(height > 0);
66 return icon.pixmap(height);
69 QPixmap Utils::Gui::scaledPixmap(const Path &path, const int height)
71 Q_ASSERT(height >= 0);
73 const QPixmap pixmap {path.data()};
74 return (height == 0) ? pixmap : pixmap.scaledToHeight(height, Qt::SmoothTransformation);
77 QSize Utils::Gui::smallIconSize(const QWidget *widget)
79 // Get DPI scaled icon size (device-dependent), see QT source
80 // under a 1080p screen is usually 16x16
81 const int s = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget);
82 return {s, s};
85 QSize Utils::Gui::mediumIconSize(const QWidget *widget)
87 // under a 1080p screen is usually 24x24
88 return ((smallIconSize(widget) + largeIconSize(widget)) / 2);
91 QSize Utils::Gui::largeIconSize(const QWidget *widget)
93 // Get DPI scaled icon size (device-dependent), see QT source
94 // under a 1080p screen is usually 32x32
95 const int s = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, widget);
96 return {s, s};
99 QPoint Utils::Gui::screenCenter(const QWidget *w)
101 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
103 if (!w)
104 return {};
106 QRect r = QGuiApplication::primaryScreen()->availableGeometry();
107 const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
109 const QWidget *parent = w->parentWidget();
110 if (!parent)
111 return primaryScreenCenter;
113 const QWindow *window = parent->window()->windowHandle();
114 if (!window)
115 return primaryScreenCenter;
117 const QScreen *screen = window->screen();
118 if (!screen)
119 return primaryScreenCenter;
121 r = screen->availableGeometry();
122 return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
125 // Open the given path with an appropriate application
126 void Utils::Gui::openPath(const Path &path)
128 // Hack to access samba shares with QDesktopServices::openUrl
129 const QUrl url = path.data().startsWith(u"//")
130 ? QUrl(u"file:" + path.data())
131 : QUrl::fromLocalFile(path.data());
133 #ifdef Q_OS_WIN
134 auto *thread = QThread::create([path]()
136 if (SUCCEEDED(::CoInitializeEx(NULL, (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))))
138 const std::wstring pathWStr = path.toString().toStdWString();
140 ::ShellExecuteW(nullptr, nullptr, pathWStr.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
142 ::CoUninitialize();
145 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
146 thread->start();
147 #else
148 QDesktopServices::openUrl(url);
149 #endif
152 // Open the parent directory of the given path with a file manager and select
153 // (if possible) the item at the given path
154 void Utils::Gui::openFolderSelect(const Path &path)
156 // If the item to select doesn't exist, try to open its parent
157 if (!path.exists())
159 openPath(path.parentPath());
160 return;
163 #ifdef Q_OS_WIN
164 auto *thread = QThread::create([path]()
166 if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)))
168 const std::wstring pathWStr = path.toString().toStdWString();
169 PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(pathWStr.c_str());
170 if (pidl)
172 ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
173 ::ILFree(pidl);
176 ::CoUninitialize();
179 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
180 thread->start();
181 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
182 const int lineMaxLength = 64;
184 QProcess proc;
185 proc.start(u"xdg-mime"_s, {u"query"_s, u"default"_s, u"inode/directory"_s});
186 proc.waitForFinished();
187 const auto output = QString::fromLocal8Bit(proc.readLine(lineMaxLength).simplified());
188 if ((output == u"dolphin.desktop") || (output == u"org.kde.dolphin.desktop"))
190 proc.startDetached(u"dolphin"_s, {u"--select"_s, path.toString()});
192 else if ((output == u"nautilus.desktop") || (output == u"org.gnome.Nautilus.desktop")
193 || (output == u"nautilus-folder-handler.desktop"))
195 proc.start(u"nautilus"_s, {u"--version"_s});
196 proc.waitForFinished();
197 const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine(lineMaxLength)).remove(QRegularExpression(u"[^0-9.]"_s));
198 using NautilusVersion = Utils::Version<3>;
199 if (NautilusVersion::fromString(nautilusVerStr, {1, 0, 0}) > NautilusVersion(3, 28, 0))
200 proc.startDetached(u"nautilus"_s, {(Fs::isDir(path) ? path.parentPath() : path).toString()});
201 else
202 proc.startDetached(u"nautilus"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
204 else if (output == u"nemo.desktop")
206 proc.startDetached(u"nemo"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
208 else if ((output == u"konqueror.desktop") || (output == u"kfmclient_dir.desktop"))
210 proc.startDetached(u"konqueror"_s, {u"--select"_s, path.toString()});
212 else
214 // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
215 openPath(path.parentPath());
217 #else
218 openPath(path.parentPath());
219 #endif
222 QString Utils::Gui::tagToWidgetText(const Tag &tag)
224 return tag.toString().replace(u'&', u"&&"_s);
227 Tag Utils::Gui::widgetTextToTag(const QString &text)
229 // replace pairs of '&' with single '&' and remove non-paired occurrences of '&'
230 QString cleanedText;
231 cleanedText.reserve(text.size());
232 bool amp = false;
233 for (const QChar c : text)
235 if (c == u'&')
237 amp = !amp;
238 if (amp)
239 continue;
242 cleanedText.append(c);
245 return Tag(cleanedText);