2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2017 Brian Kendall <brian@briankendall.net>
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 "macutilities.h"
31 #import <Cocoa/Cocoa.h>
32 #include <objc/message.h>
38 #include "base/path.h"
40 QImage qt_mac_toQImage(CGImageRef image);
44 QPixmap pixmapForExtension(const QString &ext, const QSize &size)
48 NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:ext.toNSString()];
51 NSRect rect = NSMakeRect(0, 0, size.width(), size.height());
52 CGImageRef cgImage = [image CGImageForProposedRect:&rect context:nil hints:nil];
53 return QPixmap::fromImage(qt_mac_toQImage(cgImage));
60 void overrideDockClickHandler(bool (*dockClickHandler)(id, SEL, ...))
62 NSApplication *appInst = [NSApplication sharedApplication];
67 Class delClass = [[appInst delegate] class];
68 SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
70 if (class_getInstanceMethod(delClass, shouldHandle))
72 if (class_replaceMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
73 qDebug("Registered dock click handler (replaced original method)");
75 qWarning("Failed to replace method for dock click handler");
79 if (class_addMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
80 qDebug("Registered dock click handler");
82 qWarning("Failed to register dock click handler");
86 void displayNotification(const QString &title, const QString &message)
90 NSUserNotification *notification = [[NSUserNotification alloc] init];
91 notification.title = title.toNSString();
92 notification.informativeText = message.toNSString();
93 notification.soundName = NSUserNotificationDefaultSoundName;
95 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
99 void openFiles(const PathList &pathList)
103 NSMutableArray *pathURLs = [NSMutableArray arrayWithCapacity:pathList.size()];
105 for (const auto &path : pathList)
106 [pathURLs addObject:[NSURL fileURLWithPath:path.toString().toNSString()]];
108 // In some unknown way, the next line affects Qt's main loop causing the crash
109 // in QApplication::exec() on processing next event after this call.
110 // Even crash doesn't happen exactly after this call, it will happen on
111 // application exit. Call stack and disassembly are the same in all cases.
112 // But running it in another thread (aka in background) solves the issue.
113 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
115 [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:pathURLs];
120 QString badgeLabelText()
122 return QString::fromNSString(NSApp.dockTile.badgeLabel);
125 void setBadgeLabelText(const QString &text)
127 NSApp.dockTile.badgeLabel = text.toNSString();