-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed mac specific compiler errors and cleaned up Objective C code. Closes #7252 #7253
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,11 @@ win32|macx { | |
SOURCES += $$PWD/programupdater.cpp | ||
} | ||
|
||
macx { | ||
HEADERS += $$PWD/macutilities.h | ||
OBJECTIVE_SOURCES += $$PWD/macutilities.mm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't "macutilities.h" be in project too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, I forgot to add it. |
||
} | ||
|
||
FORMS += \ | ||
$$PWD/mainwindow.ui \ | ||
$$PWD/about.ui \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2017 Brian Kendall <brian@briankendall.net> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* In addition, as a special exception, the copyright holders give permission to | ||
* link this program with the OpenSSL project's "OpenSSL" library (or with | ||
* modified versions of it that use the same license as the "OpenSSL" library), | ||
* and distribute the linked executables. You must obey the GNU General Public | ||
* License in all respects for all of the code used other than "OpenSSL". If you | ||
* modify file(s), you may extend this exception to your version of the file(s), | ||
* but you are not obligated to do so. If you do not wish to do so, delete this | ||
* exception statement from your version. | ||
*/ | ||
|
||
#ifndef MACUTILITIES_H | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to put standard project header in your files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this change. Please let me know if I got the header correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should specify the email after your name. You don't want to do it because of some reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed Mike used his github name and went with that but I can change it to my email. |
||
#define MACUTILITIES_H | ||
|
||
#include <QPixmap> | ||
#include <QSize> | ||
#include <objc/objc.h> | ||
|
||
QPixmap pixmapForExtension(const QString &ext, const QSize &size); | ||
void overrideDockClickHandler(bool (*dockClickHandler)(id, SEL, ...)); | ||
|
||
#endif // MACUTILITIES_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2017 Brian Kendall <brian@briankendall.net> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* In addition, as a special exception, the copyright holders give permission to | ||
* link this program with the OpenSSL project's "OpenSSL" library (or with | ||
* modified versions of it that use the same license as the "OpenSSL" library), | ||
* and distribute the linked executables. You must obey the GNU General Public | ||
* License in all respects for all of the code used other than "OpenSSL". If you | ||
* modify file(s), you may extend this exception to your version of the file(s), | ||
* but you are not obligated to do so. If you do not wish to do so, delete this | ||
* exception statement from your version. | ||
*/ | ||
|
||
#include "macutilities.h" | ||
|
||
#include <QtMac> | ||
#include <objc/message.h> | ||
#import <Cocoa/Cocoa.h> | ||
|
||
QPixmap pixmapForExtension(const QString &ext, const QSize &size) | ||
{ | ||
@autoreleasepool { | ||
NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:ext.toNSString()]; | ||
if (image) { | ||
NSRect rect = NSMakeRect(0, 0, size.width(), size.height()); | ||
CGImageRef cgImage = [image CGImageForProposedRect:&rect context:nil hints:nil]; | ||
return QtMac::fromCGImageRef(cgImage); | ||
} | ||
|
||
return QPixmap(); | ||
} | ||
} | ||
|
||
void overrideDockClickHandler(bool (*dockClickHandler)(id, SEL, ...)) | ||
{ | ||
NSApplication *appInst = [NSApplication sharedApplication]; | ||
|
||
if (!appInst) | ||
return; | ||
|
||
Class delClass = [[appInst delegate] class]; | ||
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"); | ||
|
||
if (class_getInstanceMethod(delClass, shouldHandle)) { | ||
if (class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:")) | ||
qDebug("Registered dock click handler (replaced original method)"); | ||
else | ||
qWarning("Failed to replace method for dock click handler"); | ||
} | ||
else { | ||
if (class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:")) | ||
qDebug("Registered dock click handler"); | ||
else | ||
qWarning("Failed to register dock click handler"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,6 @@ | |
#include "mainwindow.h" | ||
|
||
#ifdef Q_OS_MAC | ||
#include <objc/objc.h> | ||
#include <objc/message.h> | ||
#include <QtMacExtras> | ||
#include <QtMac> | ||
#endif | ||
|
@@ -107,7 +105,11 @@ | |
#include "hidabletabwidget.h" | ||
#include "ui_mainwindow.h" | ||
|
||
#ifdef Q_OS_MAC | ||
#if defined (Q_OS_MAC) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls add an empty line above this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's keep it the same in the same file #ifdef Q_OS_MAC There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change it to |
||
#include "macutilities.h" | ||
#endif | ||
|
||
#if defined (Q_OS_MAC) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
void qt_mac_set_dock_menu(QMenu *menu); | ||
#endif | ||
|
||
|
@@ -1294,29 +1296,9 @@ static bool dockClickHandler(id self, SEL cmd, ...) | |
} | ||
|
||
void MainWindow::setupDockClickHandler() | ||
{ | ||
Class cls = objc_getClass("NSApplication"); | ||
objc_object *appInst = objc_msgSend(reinterpret_cast<objc_object *>(cls), sel_registerName("sharedApplication")); | ||
|
||
if (!appInst) | ||
return; | ||
|
||
{ | ||
dockMainWindowHandle = this; | ||
objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate")); | ||
Class delClass = reinterpret_cast<Class>(objc_msgSend(delegate, sel_registerName("class"))); | ||
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"); | ||
if (class_getInstanceMethod(delClass, shouldHandle)) { | ||
if (class_replaceMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:")) | ||
qDebug("Registered dock click handler (replaced original method)"); | ||
else | ||
qWarning("Failed to replace method for dock click handler"); | ||
} | ||
else { | ||
if (class_addMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:")) | ||
qDebug("Registered dock click handler"); | ||
else | ||
qWarning("Failed to register dock click handler"); | ||
} | ||
overrideDockClickHandler(dockClickHandler); | ||
} | ||
|
||
#endif | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,14 +32,12 @@ | |
#include <QFileIconProvider> | ||
#include <QFileInfo> | ||
#include <QIcon> | ||
#include <QMap> | ||
|
||
#if defined(Q_OS_WIN) | ||
#include <Windows.h> | ||
#include <Shellapi.h> | ||
#include <QtWin> | ||
#elif defined(Q_OS_MAC) | ||
#include <objc/objc.h> | ||
#include <objc/message.h> | ||
#else | ||
#include <QMimeDatabase> | ||
#include <QMimeType> | ||
|
@@ -52,13 +50,8 @@ | |
#include "torrentcontentmodelitem.h" | ||
#include "torrentcontentmodelfolder.h" | ||
#include "torrentcontentmodelfile.h" | ||
|
||
#ifdef Q_OS_MAC | ||
struct NSImage; | ||
// This function is a private QtGui library export on macOS | ||
// See src/gui/painting/qcoregraphics_p.h for more details | ||
// QtMac::fromCGImageRef takes a CGImageRef and thus requires a double conversion | ||
QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size); | ||
#if defined(Q_OS_MAC) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused. In torrentcontentmodel.cpp it's consistently written as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer |
||
#include "macutilities.h" | ||
#endif | ||
|
||
namespace | ||
|
@@ -113,31 +106,23 @@ namespace | |
{ | ||
const QString ext = info.suffix(); | ||
if (!ext.isEmpty()) { | ||
const QPixmap pixmap = pixmapForExtension(ext, QSize(32, 32)); | ||
if (!pixmap.isNull()) | ||
return QIcon(pixmap); | ||
auto cacheIter = m_iconCache.find(ext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should come in a separate commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not in a position to build or test in Windows right now so I'd prefer to leave it for a separate PR. |
||
|
||
if (cacheIter != m_iconCache.end()) | ||
return *cacheIter; | ||
|
||
QIcon icon = QIcon(pixmapForExtension(ext, QSize(32, 32))); | ||
if (!icon.isNull()) { | ||
m_iconCache.insert(ext, icon); | ||
return icon; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, one last thing, move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to avoid returning a null icon. If it's null, it'll exit the if block and hit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn, I missed the |
||
} | ||
} | ||
|
||
return UnifiedFileIconProvider::icon(info); | ||
} | ||
|
||
private: | ||
QPixmap pixmapForExtension(const QString &ext, const QSize &size) const | ||
{ | ||
QMacAutoReleasePool pool; | ||
objc_object *woskspaceCls = reinterpret_cast<objc_object *>(objc_getClass("NSWorkspace")); | ||
SEL sharedWorkspaceSel = sel_registerName("sharedWorkspace"); | ||
SEL iconForFileTypeSel = sel_registerName("iconForFileType:"); | ||
|
||
objc_object *sharedWorkspace = objc_msgSend(woskspaceCls, sharedWorkspaceSel); | ||
if (sharedWorkspace) { | ||
objc_object *image = objc_msgSend(sharedWorkspace, iconForFileTypeSel, ext.toNSString()); | ||
if (image) | ||
return qt_mac_toQPixmap(reinterpret_cast<NSImage *>(image), size); | ||
} | ||
|
||
return QPixmap(); | ||
} | ||
mutable QMap<QString, QIcon> m_iconCache; | ||
}; | ||
#else | ||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to add
macutilities.h
to QBT_GUI_HEADERS as well, since the code below does it at least.Ugh, don't tell me github did not send it right away :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I forgot to add that.