osx_notifications: Check if user notifications classes exist to prevent crash on...
authorMarvin Scholz <epirat07@gmail.com>
Fri, 23 Oct 2015 11:11:29 +0000 (13:11 +0200)
committerJean-Baptiste Kempf <jb@videolan.org>
Fri, 23 Oct 2015 11:15:06 +0000 (13:15 +0200)
Add checks for NSUserNotification and NSUserNotificationCenter so we do not
use them on OS X 10.7 and below, to prevent VLC from crashing if it
is compiled on 10.8 or higher but ran on 10.7 or below.

Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
modules/notify/osx_notifications.m

index 914787d..8fb03d8 100644 (file)
@@ -87,6 +87,7 @@
     NSMutableDictionary *registrationDictionary;
     id lastNotification;
     bool isInForeground;
+    bool hasNativeNotifications;
     intf_thread_t *interfaceThread;
 }
 
@@ -299,6 +300,12 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
     // Start in background
     isInForeground = NO;
 
+    // Check for native notification support
+    Class userNotificationClass = NSClassFromString(@"NSUserNotification");
+    Class userNotificationCenterClass = NSClassFromString(@"NSUserNotificationCenter");
+    hasNativeNotifications = (userNotificationClass && userNotificationCenterClass) ? YES : NO;
+
+    lastNotification = nil;
     applicationName = nil;
     notificationType = nil;
     registrationDictionary = nil;
@@ -312,7 +319,7 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
     // Clear the remaining lastNotification in Notification Center, if any
     @autoreleasepool {
-        if (lastNotification) {
+        if (lastNotification && hasNativeNotifications) {
             [NSUserNotificationCenter.defaultUserNotificationCenter
              removeDeliveredNotification:(NSUserNotification *)lastNotification];
             [lastNotification release];
@@ -344,8 +351,10 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
         [GrowlApplicationBridge setGrowlDelegate:self];
 
 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
-        [[NSUserNotificationCenter defaultUserNotificationCenter]
-         setDelegate:(id<NSUserNotificationCenterDelegate>)self];
+        if (hasNativeNotifications) {
+            [[NSUserNotificationCenter defaultUserNotificationCenter]
+             setDelegate:(id<NSUserNotificationCenterDelegate>)self];
+        }
 #endif
     }
 }
@@ -409,7 +418,7 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                                            isSticky:NO
                                        clickContext:nil
                                          identifier:@"VLCNowPlayingNotification"];
-        } else {
+        } else if (hasNativeNotifications) {
 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
             // Make the OS X notification and string
             NSUserNotification *notification = [NSUserNotification new];