Skip to content

Commit

Permalink
add notifications on mac (#7673)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Aug 22, 2018
1 parent 05fb0f4 commit 66dd676
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Expand Up @@ -332,6 +332,9 @@ IF(WITH_CORE)
FIND_PACKAGE(Qt53DExtras REQUIRED)
SET(HAVE_3D TRUE) # used in qgsconfig.h
ENDIF (WITH_3D)
IF (APPLE)
FIND_PACKAGE(Qt5MacExtras REQUIRED)
ENDIF (APPLE)
INCLUDE("cmake/modules/ECMQt4To5Porting.cmake")
MESSAGE(STATUS "Found Qt version: ${Qt5Core_VERSION_STRING}")
IF (WITH_QUICK)
Expand Down
6 changes: 6 additions & 0 deletions src/native/CMakeLists.txt
Expand Up @@ -152,6 +152,12 @@ IF (UNIX AND NOT APPLE AND NOT ANDROID)
TARGET_LINK_LIBRARIES(qgis_native Qt5::DBus)
ENDIF (UNIX AND NOT APPLE AND NOT ANDROID)

IF (APPLE)
FIND_PACKAGE(Qt5MacExtras)

TARGET_LINK_LIBRARIES(qgis_native Qt5::MacExtras)
ENDIF (APPLE)

IF (WIN32)
FIND_PACKAGE(Qt5WinExtras)

Expand Down
11 changes: 10 additions & 1 deletion src/native/mac/qgsmacnative.h
Expand Up @@ -26,11 +26,20 @@ class QString;
class NATIVE_EXPORT QgsMacNative : public QgsNative
{
public:
virtual ~QgsMacNative();
explicit QgsMacNative();
~QgsMacNative() override;

virtual const char *currentAppLocalizedName();
void currentAppActivateIgnoringOtherApps() override;
void openFileExplorerAndSelectFile( const QString &path ) override;
QgsNative::Capabilities capabilities() const override;
QgsNative::NotificationResult showDesktopNotification( const QString &summary, const QString &body, const NotificationSettings &settings ) override;

private:
class QgsUserNotificationCenter;
QgsUserNotificationCenter *mQgsUserNotificationCenter = nullptr;

};


#endif // QGSMACNATIVE_H
68 changes: 68 additions & 0 deletions src/native/mac/qgsmacnative.mm
Expand Up @@ -18,10 +18,43 @@
#include "qgsmacnative.h"

#include <Cocoa/Cocoa.h>
#include <QtMacExtras/QtMac>

#include <QString>
#include <QPixmap>


@interface QgsUserNotificationCenterDelegate : NSObject <NSUserNotificationCenterDelegate>
@end

@implementation QgsUserNotificationCenterDelegate

- ( BOOL )userNotificationCenter:( NSUserNotificationCenter * )center shouldPresentNotification:( NSUserNotification * )notification
{
#pragma unused (notification)
#pragma unused (center)
return YES;
}

@end

class QgsMacNative::QgsUserNotificationCenter
{
public:
QgsUserNotificationCenterDelegate *_qgsUserNotificationCenter;
};

QgsMacNative::QgsMacNative()
: mQgsUserNotificationCenter( new QgsMacNative::QgsUserNotificationCenter() )
{
mQgsUserNotificationCenter->_qgsUserNotificationCenter = [[QgsUserNotificationCenterDelegate alloc] init];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: mQgsUserNotificationCenter->_qgsUserNotificationCenter];
}

QgsMacNative::~QgsMacNative()
{
[mQgsUserNotificationCenter->_qgsUserNotificationCenter dealloc];
delete mQgsUserNotificationCenter;
}

const char *QgsMacNative::currentAppLocalizedName()
Expand All @@ -41,3 +74,38 @@
NSArray *fileURLs = [NSArray arrayWithObjects:[NSURL fileURLWithPath:pathStr], nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
}

QgsNative::Capabilities QgsMacNative::capabilities() const
{
return NativeDesktopNotifications;
}

QgsNative::NotificationResult QgsMacNative::showDesktopNotification( const QString &summary,
const QString &body,
const QgsNative::NotificationSettings &settings )
{
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = summary.toNSString();
notification.informativeText = body.toNSString();
notification.soundName = NSUserNotificationDefaultSoundName; //Will play a default sound
const QPixmap px = QPixmap::fromImage( settings.image );
NSImage *image = nil;
if ( settings.image.isNull() )
{
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
}
else
{
image = QtMac::toNSImage( px );
}
notification.contentImage = image;

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notification];
[notification autorelease];

//[userCenterDelegate dealloc];

NotificationResult result;
result.successful = true;
return result;
}

0 comments on commit 66dd676

Please sign in to comment.