Don't overwrite stored layout of main window with incorrect one
[qBittorrent.git] / src / gui / utils.cpp
blobc7c1f24c4e76e834a41183518ad8378d8bf66ea2
1 /*
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.
29 #include "utils.h"
31 #include <QtSystemDetection>
33 #ifdef Q_OS_WIN
34 #include <objbase.h>
35 #include <shlobj.h>
36 #include <shellapi.h>
37 #endif
39 #include <QApplication>
40 #include <QDesktopServices>
41 #include <QIcon>
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/utils/fs.h"
58 #include "base/utils/version.h"
60 QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const int height)
62 Q_ASSERT(height > 0);
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);
80 return {s, s};
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);
94 return {s, s};
97 QPoint Utils::Gui::screenCenter(const QWidget *w)
99 // Returns the QPoint which the widget will be placed center on screen (where parent resides)
101 if (!w)
102 return {};
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();
108 if (!parent)
109 return primaryScreenCenter;
111 const QWindow *window = parent->window()->windowHandle();
112 if (!window)
113 return primaryScreenCenter;
115 const QScreen *screen = window->screen();
116 if (!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());
131 #ifdef Q_OS_WIN
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);
140 ::CoUninitialize();
143 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
144 thread->start();
145 #else
146 QDesktopServices::openUrl(url);
147 #endif
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
155 if (!path.exists())
157 openPath(path.parentPath());
158 return;
161 #ifdef Q_OS_WIN
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());
168 if (pidl)
170 ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
171 ::ILFree(pidl);
174 ::CoUninitialize();
177 QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
178 thread->start();
179 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
180 const int lineMaxLength = 64;
182 QProcess proc;
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()});
199 else
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()});
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