Correctly handle "torrent finished" events
[qBittorrent.git] / src / gui / utils.cpp
blobc14bb4ed1c5db244057488739565ca26c948a63b
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 <QPixmap>
43 #include <QPixmapCache>
44 #include <QPoint>
45 #include <QProcess>
46 #include <QRegularExpression>
47 #include <QScreen>
48 #include <QSize>
49 #include <QStyle>
50 #include <QThread>
51 #include <QUrl>
52 #include <QWidget>
53 #include <QWindow>
55 #include "base/global.h"
56 #include "base/path.h"
57 #include "base/tag.h"
58 #include "base/utils/fs.h"
59 #include "base/utils/version.h"
61 QPixmap Utils::Gui::scaledPixmap(const Path &path, const int height)
63 Q_ASSERT(height >= 0);
65 const QPixmap pixmap {path.data()};
66 return (height == 0) ? pixmap : pixmap.scaledToHeight(height, Qt::SmoothTransformation);
69 QSize Utils::Gui::smallIconSize(const QWidget *widget)
71 // Get DPI scaled icon size (device-dependent), see QT source
72 // under a 1080p screen is usually 16x16
73 const int s = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget);
74 return {s, s};
77 QSize Utils::Gui::mediumIconSize(const QWidget *widget)
79 // under a 1080p screen is usually 24x24
80 return ((smallIconSize(widget) + largeIconSize(widget)) / 2);
83 QSize Utils::Gui::largeIconSize(const QWidget *widget)
85 // Get DPI scaled icon size (device-dependent), see QT source
86 // under a 1080p screen is usually 32x32
87 const int s = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, widget);
88 return {s, s};
91 QPoint Utils::Gui::screenCenter(const QWidget *w)
93 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
95 if (!w)
96 return {};
98 QRect r = QGuiApplication::primaryScreen()->availableGeometry();
99 const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
101 const QWidget *parent = w->parentWidget();
102 if (!parent)
103 return primaryScreenCenter;
105 const QWindow *window = parent->window()->windowHandle();
106 if (!window)
107 return primaryScreenCenter;
109 const QScreen *screen = window->screen();
110 if (!screen)
111 return primaryScreenCenter;
113 r = screen->availableGeometry();
114 return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
117 // Open the given path with an appropriate application
118 void Utils::Gui::openPath(const Path &path)
120 // Hack to access samba shares with QDesktopServices::openUrl
121 const QUrl url = path.data().startsWith(u"//")
122 ? QUrl(u"file:" + path.data())
123 : QUrl::fromLocalFile(path.data());
125 #ifdef Q_OS_WIN
126 auto *thread = QThread::create([path]()
128 if (SUCCEEDED(::CoInitializeEx(NULL, (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))))
130 const std::wstring pathWStr = path.toString().toStdWString();
132 ::ShellExecuteW(nullptr, nullptr, pathWStr.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
134 ::CoUninitialize();
137 thread->setObjectName("Utils::Gui::openPath thread");
138 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
139 thread->start();
140 #else
141 QDesktopServices::openUrl(url);
142 #endif
145 // Open the parent directory of the given path with a file manager and select
146 // (if possible) the item at the given path
147 void Utils::Gui::openFolderSelect(const Path &path)
149 // If the item to select doesn't exist, try to open its parent
150 if (!path.exists())
152 openPath(path.parentPath());
153 return;
156 #ifdef Q_OS_WIN
157 auto *thread = QThread::create([path]()
159 if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)))
161 const std::wstring pathWStr = path.toString().toStdWString();
162 PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(pathWStr.c_str());
163 if (pidl)
165 ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
166 ::ILFree(pidl);
169 ::CoUninitialize();
172 thread->setObjectName("Utils::Gui::openFolderSelect thread");
173 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
174 thread->start();
175 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
176 const int lineMaxLength = 64;
178 QProcess proc;
179 proc.start(u"xdg-mime"_s, {u"query"_s, u"default"_s, u"inode/directory"_s});
180 proc.waitForFinished();
181 const auto output = QString::fromLocal8Bit(proc.readLine(lineMaxLength).simplified());
182 if ((output == u"dolphin.desktop") || (output == u"org.kde.dolphin.desktop"))
184 proc.startDetached(u"dolphin"_s, {u"--select"_s, path.toString()});
186 else if ((output == u"nautilus.desktop") || (output == u"org.gnome.Nautilus.desktop")
187 || (output == u"nautilus-folder-handler.desktop"))
189 proc.start(u"nautilus"_s, {u"--version"_s});
190 proc.waitForFinished();
191 const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine(lineMaxLength)).remove(QRegularExpression(u"[^0-9.]"_s));
192 using NautilusVersion = Utils::Version<3>;
193 if (NautilusVersion::fromString(nautilusVerStr, {1, 0, 0}) > NautilusVersion(3, 28, 0))
194 proc.startDetached(u"nautilus"_s, {(Fs::isDir(path) ? path.parentPath() : path).toString()});
195 else
196 proc.startDetached(u"nautilus"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
198 else if (output == u"nemo.desktop")
200 proc.startDetached(u"nemo"_s, {u"--no-desktop"_s, (Fs::isDir(path) ? path.parentPath() : path).toString()});
202 else if ((output == u"konqueror.desktop") || (output == u"kfmclient_dir.desktop"))
204 proc.startDetached(u"konqueror"_s, {u"--select"_s, path.toString()});
206 else if (output == u"thunar.desktop")
208 proc.startDetached(u"thunar"_s, {path.toString()});
210 else
212 // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
213 openPath(path.parentPath());
215 #else
216 openPath(path.parentPath());
217 #endif
220 QString Utils::Gui::tagToWidgetText(const Tag &tag)
222 return tag.toString().replace(u'&', u"&&"_s);
225 Tag Utils::Gui::widgetTextToTag(const QString &text)
227 // replace pairs of '&' with single '&' and remove non-paired occurrences of '&'
228 QString cleanedText;
229 cleanedText.reserve(text.size());
230 bool amp = false;
231 for (const QChar c : text)
233 if (c == u'&')
235 amp = !amp;
236 if (amp)
237 continue;
240 cleanedText.append(c);
243 return Tag(cleanedText);