qBittorrent
macutilities.mm
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 Brian Kendall <brian@briankendall.net>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "macutilities.h"
30 
31 #import <Cocoa/Cocoa.h>
32 #include <objc/message.h>
33 
34 #include <QPixmap>
35 #include <QSet>
36 #include <QSize>
37 #include <QString>
38 
39 QImage qt_mac_toQImage(CGImageRef image);
40 
41 namespace MacUtils
42 {
43  QPixmap pixmapForExtension(const QString &ext, const QSize &size)
44  {
45  @autoreleasepool
46  {
47  NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:ext.toNSString()];
48  if (image)
49  {
50  NSRect rect = NSMakeRect(0, 0, size.width(), size.height());
51  CGImageRef cgImage = [image CGImageForProposedRect:&rect context:nil hints:nil];
52  return QPixmap::fromImage(qt_mac_toQImage(cgImage));
53  }
54 
55  return QPixmap();
56  }
57  }
58 
59  void overrideDockClickHandler(bool (*dockClickHandler)(id, SEL, ...))
60  {
61  NSApplication *appInst = [NSApplication sharedApplication];
62 
63  if (!appInst)
64  return;
65 
66  Class delClass = [[appInst delegate] class];
67  SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
68 
69  if (class_getInstanceMethod(delClass, shouldHandle))
70  {
71  if (class_replaceMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
72  qDebug("Registered dock click handler (replaced original method)");
73  else
74  qWarning("Failed to replace method for dock click handler");
75  }
76  else
77  {
78  if (class_addMethod(delClass, shouldHandle, reinterpret_cast<IMP>(dockClickHandler), "B@:"))
79  qDebug("Registered dock click handler");
80  else
81  qWarning("Failed to register dock click handler");
82  }
83  }
84 
85  void displayNotification(const QString &title, const QString &message)
86  {
87  @autoreleasepool
88  {
89  NSUserNotification *notification = [[NSUserNotification alloc] init];
90  notification.title = title.toNSString();
91  notification.informativeText = message.toNSString();
92  notification.soundName = NSUserNotificationDefaultSoundName;
93 
94  [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
95  }
96  }
97 
98  void openFiles(const QSet<QString> &pathsList)
99  {
100  @autoreleasepool
101  {
102  NSMutableArray *pathURLs = [NSMutableArray arrayWithCapacity:pathsList.size()];
103 
104  for (const auto &path : pathsList)
105  [pathURLs addObject:[NSURL fileURLWithPath:path.toNSString()]];
106 
107  [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:pathURLs];
108  }
109  }
110 
111  QString badgeLabelText()
112  {
113  return QString::fromNSString(NSApp.dockTile.badgeLabel);
114  }
115 
116  void setBadgeLabelText(const QString &text)
117  {
118  NSApp.dockTile.badgeLabel = text.toNSString();
119  }
120 }
QImage qt_mac_toQImage(CGImageRef image)
void overrideDockClickHandler(bool(*dockClickHandler)(id, SEL,...))
Definition: macutilities.mm:59
void openFiles(const QSet< QString > &pathsList)
Definition: macutilities.mm:98
void displayNotification(const QString &title, const QString &message)
Definition: macutilities.mm:85
void setBadgeLabelText(const QString &text)
QPixmap pixmapForExtension(const QString &ext, const QSize &size)
Definition: macutilities.mm:43
QString badgeLabelText()