Skip to content
Permalink
Browse files

add notifications on mac (#7673)

  • Loading branch information
3nids committed Aug 22, 2018
1 parent 05fb0f4 commit 66dd6768efec0077fbb95154328cf9d842b86f13
Showing with 87 additions and 1 deletion.
  1. +3 −0 CMakeLists.txt
  2. +6 −0 src/native/CMakeLists.txt
  3. +10 −1 src/native/mac/qgsmacnative.h
  4. +68 −0 src/native/mac/qgsmacnative.mm
@@ -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)
@@ -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)

@@ -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
@@ -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()
@@ -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.
You can’t perform that action at this time.