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>
43 #include <QPixmapCache>
46 #include <QRegularExpression>
55 #include "base/global.h"
56 #include "base/path.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
);
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
);
91 QPoint
Utils::Gui::screenCenter(const QWidget
*w
)
93 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
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();
103 return primaryScreenCenter
;
105 const QWindow
*window
= parent
->window()->windowHandle();
107 return primaryScreenCenter
;
109 const QScreen
*screen
= window
->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());
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
);
137 thread
->setObjectName("Utils::Gui::openPath thread");
138 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
141 QDesktopServices::openUrl(url
);
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
152 openPath(path
.parentPath());
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());
165 ::SHOpenFolderAndSelectItems(pidl
, 0, nullptr, 0);
172 thread
->setObjectName("Utils::Gui::openFolderSelect thread");
173 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
175 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
176 const int lineMaxLength
= 64;
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()});
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()});
212 // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
213 openPath(path
.parentPath());
216 openPath(path
.parentPath());
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 '&'
229 cleanedText
.reserve(text
.size());
231 for (const QChar c
: text
)
240 cleanedText
.append(c
);
243 return Tag(cleanedText
);