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.
32 #include <QtSystemDetection>
40 #include <QApplication>
41 #include <QDesktopServices>
44 #include <QPixmapCache>
47 #include <QRegularExpression>
56 #include "base/global.h"
57 #include "base/path.h"
59 #include "base/utils/fs.h"
60 #include "base/utils/version.h"
62 QPixmap
Utils::Gui::scaledPixmap(const QIcon
&icon
, const int height
)
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
);
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
);
99 QPoint
Utils::Gui::screenCenter(const QWidget
*w
)
101 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
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();
111 return primaryScreenCenter
;
113 const QWindow
*window
= parent
->window()->windowHandle();
115 return primaryScreenCenter
;
117 const QScreen
*screen
= window
->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());
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
);
145 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
148 QDesktopServices::openUrl(url
);
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
159 openPath(path
.parentPath());
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());
172 ::SHOpenFolderAndSelectItems(pidl
, 0, nullptr, 0);
179 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
181 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
182 const int lineMaxLength
= 64;
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()});
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()});
214 // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
215 openPath(path
.parentPath());
218 openPath(path
.parentPath());
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 '&'
231 cleanedText
.reserve(text
.size());
233 for (const QChar c
: text
)
242 cleanedText
.append(c
);
245 return Tag(cleanedText
);