2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2017 Mike Tzou
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
31 #include <QtSystemDetection>
39 #include <QApplication>
40 #include <QDesktopServices>
43 #include <QPixmapCache>
46 #include <QRegularExpression>
55 #include "base/global.h"
56 #include "base/path.h"
57 #include "base/utils/fs.h"
58 #include "base/utils/version.h"
60 QPixmap
Utils::Gui::scaledPixmap(const QIcon
&icon
, const int height
)
64 return icon
.pixmap(height
);
67 QPixmap
Utils::Gui::scaledPixmap(const Path
&path
, const int height
)
69 Q_ASSERT(height
>= 0);
71 const QPixmap pixmap
{path
.data()};
72 return (height
== 0) ? pixmap
: pixmap
.scaledToHeight(height
, Qt::SmoothTransformation
);
75 QSize
Utils::Gui::smallIconSize(const QWidget
*widget
)
77 // Get DPI scaled icon size (device-dependent), see QT source
78 // under a 1080p screen is usually 16x16
79 const int s
= QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize
, nullptr, widget
);
83 QSize
Utils::Gui::mediumIconSize(const QWidget
*widget
)
85 // under a 1080p screen is usually 24x24
86 return ((smallIconSize(widget
) + largeIconSize(widget
)) / 2);
89 QSize
Utils::Gui::largeIconSize(const QWidget
*widget
)
91 // Get DPI scaled icon size (device-dependent), see QT source
92 // under a 1080p screen is usually 32x32
93 const int s
= QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize
, nullptr, widget
);
97 QPoint
Utils::Gui::screenCenter(const QWidget
*w
)
99 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
104 QRect r
= QGuiApplication::primaryScreen()->availableGeometry();
105 const QPoint primaryScreenCenter
{(r
.x() + (r
.width() - w
->frameSize().width()) / 2), (r
.y() + (r
.height() - w
->frameSize().height()) / 2)};
107 const QWidget
*parent
= w
->parentWidget();
109 return primaryScreenCenter
;
111 const QWindow
*window
= parent
->window()->windowHandle();
113 return primaryScreenCenter
;
115 const QScreen
*screen
= window
->screen();
117 return primaryScreenCenter
;
119 r
= screen
->availableGeometry();
120 return {(r
.x() + (r
.width() - w
->frameSize().width()) / 2), (r
.y() + (r
.height() - w
->frameSize().height()) / 2)};
123 // Open the given path with an appropriate application
124 void Utils::Gui::openPath(const Path
&path
)
126 // Hack to access samba shares with QDesktopServices::openUrl
127 const QUrl url
= path
.data().startsWith(u
"//")
128 ? QUrl(u
"file:" + path
.data())
129 : QUrl::fromLocalFile(path
.data());
132 auto *thread
= QThread::create([path
]()
134 if (SUCCEEDED(::CoInitializeEx(NULL
, (COINIT_APARTMENTTHREADED
| COINIT_DISABLE_OLE1DDE
))))
136 const std::wstring pathWStr
= path
.toString().toStdWString();
138 ::ShellExecuteW(nullptr, nullptr, pathWStr
.c_str(), nullptr, nullptr, SW_SHOWNORMAL
);
143 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
146 QDesktopServices::openUrl(url
);
150 // Open the parent directory of the given path with a file manager and select
151 // (if possible) the item at the given path
152 void Utils::Gui::openFolderSelect(const Path
&path
)
154 // If the item to select doesn't exist, try to open its parent
157 openPath(path
.parentPath());
162 auto *thread
= QThread::create([path
]()
164 if (SUCCEEDED(::CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
| COINIT_DISABLE_OLE1DDE
)))
166 const std::wstring pathWStr
= path
.toString().toStdWString();
167 PIDLIST_ABSOLUTE pidl
= ::ILCreateFromPathW(pathWStr
.c_str());
170 ::SHOpenFolderAndSelectItems(pidl
, 0, nullptr, 0);
177 QObject::connect(thread
, &QThread::finished
, thread
, &QObject::deleteLater
);
179 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
180 const int lineMaxLength
= 64;
183 proc
.start(u
"xdg-mime"_s
, {u
"query"_s
, u
"default"_s
, u
"inode/directory"_s
});
184 proc
.waitForFinished();
185 const auto output
= QString::fromLocal8Bit(proc
.readLine(lineMaxLength
).simplified());
186 if ((output
== u
"dolphin.desktop") || (output
== u
"org.kde.dolphin.desktop"))
188 proc
.startDetached(u
"dolphin"_s
, {u
"--select"_s
, path
.toString()});
190 else if ((output
== u
"nautilus.desktop") || (output
== u
"org.gnome.Nautilus.desktop")
191 || (output
== u
"nautilus-folder-handler.desktop"))
193 proc
.start(u
"nautilus"_s
, {u
"--version"_s
});
194 proc
.waitForFinished();
195 const auto nautilusVerStr
= QString::fromLocal8Bit(proc
.readLine(lineMaxLength
)).remove(QRegularExpression(u
"[^0-9.]"_s
));
196 using NautilusVersion
= Utils::Version
<3>;
197 if (NautilusVersion::fromString(nautilusVerStr
, {1, 0, 0}) > NautilusVersion(3, 28, 0))
198 proc
.startDetached(u
"nautilus"_s
, {(Fs::isDir(path
) ? path
.parentPath() : path
).toString()});
200 proc
.startDetached(u
"nautilus"_s
, {u
"--no-desktop"_s
, (Fs::isDir(path
) ? path
.parentPath() : path
).toString()});
202 else if (output
== u
"nemo.desktop")
204 proc
.startDetached(u
"nemo"_s
, {u
"--no-desktop"_s
, (Fs::isDir(path
) ? path
.parentPath() : path
).toString()});
206 else if ((output
== u
"konqueror.desktop") || (output
== u
"kfmclient_dir.desktop"))
208 proc
.startDetached(u
"konqueror"_s
, {u
"--select"_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());