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>
39 #include "base/path.h"
41 QImage qt_mac_toQImage(CGImageRef image);
45 QPixmap pixmapForExtension(const QString &ext, const QSize &size)
49 NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:ext.toNSString()];
52 NSRect rect = NSMakeRect(0, 0, size.width(), size.height());
53 CGImageRef cgImage = [image CGImageForProposedRect:&rect context:nil hints:nil];
54 return QPixmap::fromImage(qt_mac_toQImage(cgImage));
61 void overrideDockClickHandler(bool (*dockClickHandler)(id, SEL, ...))
63 NSApplication *appInst = [NSApplication sharedApplication];
68 Class delClass = [[appInst delegate] class];
69 SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
71 if (class_getInstanceMethod(delClass, shouldHandle))
73 if (class_replaceMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
74 qDebug("Registered dock click handler (replaced original method)");
76 qWarning("Failed to replace method for dock click handler");
80 if (class_addMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
81 qDebug("Registered dock click handler");
83 qWarning("Failed to register dock click handler");
87 void displayNotification(const QString &title, const QString &message)
91 NSUserNotification *notification = [[NSUserNotification alloc] init];
92 notification.title = title.toNSString();
93 notification.informativeText = message.toNSString();
94 notification.soundName = NSUserNotificationDefaultSoundName;
96 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
100 void openFiles(const PathList &pathList)
104 NSMutableArray *pathURLs = [NSMutableArray arrayWithCapacity:pathList.size()];
106 for (const auto &path : pathList)
107 [pathURLs addObject:[NSURL fileURLWithPath:path.toString().toNSString()]];
109 // In some unknown way, the next line affects Qt's main loop causing the crash
110 // in QApplication::exec() on processing next event after this call.
111 // Even crash doesn't happen exactly after this call, it will happen on
112 // application exit. Call stack and disassembly are the same in all cases.
113 // But running it in another thread (aka in background) solves the issue.
114 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
116 [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:pathURLs];
121 QString badgeLabelText()
123 return QString::fromNSString(NSApp.dockTile.badgeLabel);
126 void setBadgeLabelText(const QString &text)
128 NSApp.dockTile.badgeLabel = text.toNSString();