Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
macdockiconhandler.mm
Go to the documentation of this file.
1 #include "macdockiconhandler.h"
2 
3 #include <QMenu>
4 #include <QWidget>
5 #include <QTemporaryFile>
6 #include <QImageWriter>
7 
8 #undef slots
9 #include <Cocoa/Cocoa.h>
10 
11 #if QT_VERSION < 0x050000
12 extern void qt_mac_set_dock_menu(QMenu *);
13 #endif
14 
15 @interface DockIconClickEventHandler : NSObject
16 {
18 }
19 
20 @end
21 
22 @implementation DockIconClickEventHandler
23 
24 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
25 {
26  self = [super init];
27  if (self) {
28  dockIconHandler = aDockIconHandler;
29 
30  [[NSAppleEventManager sharedAppleEventManager]
31  setEventHandler:self
32  andSelector:@selector(handleDockClickEvent:withReplyEvent:)
33  forEventClass:kCoreEventClass
34  andEventID:kAEReopenApplication];
35  }
36  return self;
37 }
38 
39 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
40 {
41  Q_UNUSED(event)
42  Q_UNUSED(replyEvent)
43 
44  if (dockIconHandler) {
45  dockIconHandler->handleDockIconClickEvent();
46  }
47 }
48 
49 @end
50 
52 {
53  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
54 
55  this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
56  this->m_dummyWidget = new QWidget();
57  this->m_dockMenu = new QMenu(this->m_dummyWidget);
58  this->setMainWindow(NULL);
59 #if QT_VERSION < 0x050000
61 #endif
62  [pool release];
63 }
64 
65 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
66  this->mainWindow = window;
67 }
68 
70 {
71  [this->m_dockIconClickEventHandler release];
72  delete this->m_dummyWidget;
73  this->setMainWindow(NULL);
74 }
75 
77 {
78  return this->m_dockMenu;
79 }
80 
81 void MacDockIconHandler::setIcon(const QIcon &icon)
82 {
83  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
84  NSImage *image = nil;
85  if (icon.isNull())
86  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
87  else {
88  // generate NSImage from QIcon and use this as dock icon.
89  QSize size = icon.actualSize(QSize(128, 128));
90  QPixmap pixmap = icon.pixmap(size);
91 
92  // write temp file hack (could also be done through QIODevice [memory])
93  QTemporaryFile notificationIconFile;
94  if (!pixmap.isNull() && notificationIconFile.open()) {
95  QImageWriter writer(&notificationIconFile, "PNG");
96  if (writer.write(pixmap.toImage())) {
97  const char *cString = notificationIconFile.fileName().toUtf8().data();
98  NSString *macString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
99  image = [[NSImage alloc] initWithContentsOfFile:macString];
100  }
101  }
102 
103  if(!image) {
104  // if testnet image could not be created, load std. app icon
105  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
106  }
107  }
108 
109  [NSApp setApplicationIconImage:image];
110  [image release];
111  [pool release];
112 }
113 
115 {
116  static MacDockIconHandler *s_instance = NULL;
117  if (!s_instance)
118  s_instance = new MacDockIconHandler();
119  return s_instance;
120 }
121 
123 {
124  if (this->mainWindow)
125  {
126  this->mainWindow->activateWindow();
127  this->mainWindow->show();
128  }
129 
130  emit this->dockIconClicked();
131 }
QMainWindow * mainWindow
void qt_mac_set_dock_menu(QMenu *)
Macintosh-specific dock icon handler.
void setIcon(const QIcon &icon)
static MacDockIconHandler * instance()
void setMainWindow(QMainWindow *window)
MacDockIconHandler * dockIconHandler
DockIconClickEventHandler * m_dockIconClickEventHandler
int id
Definition: db_test.cc:1709