Esempio n. 1
0
        public Controller() : base()
        {
            using (var a = new NSAutoreleasePool())
            {
                NSApplication.Init();
            }

            // We get the Default notification Center
            notificationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;

            // Clear old notifications
            foreach (var n in notificationCenter.DeliveredNotifications)
            {
                notificationCenter.RemoveDeliveredNotification(n);
            }

            notificationCenter.DidDeliverNotification += (s, e) =>
            {
                Console.WriteLine("Notification Delivered");
            };

            // If the notification is clicked, displays the entire message.
            notificationCenter.DidActivateNotification += (object sender, UNCDidActivateNotificationEventArgs e) =>
            {
                var notification = (UserNotification)e.Notification;

                if (notification.Kind == UserNotification.NotificationKind.Normal)
                {
                    notificationCenter.RemoveDeliveredNotification(e.Notification);
                    string  msg   = notificationMessages[notification.Id];
                    NSAlert alert = NSAlert.WithMessage(notification.Title, "OK", null, null, msg);
                    notificationMessages.Remove(notification.Id);
                    alert.Icon = new NSImage(System.IO.Path.Combine(NSBundle.MainBundle.ResourcePath, "Pixmaps", "process-syncing-error.icns"));
                    alert.Window.OrderFrontRegardless();
                    alert.RunModal();
                }
                else
                {
                    LocalFolderClicked(Path.GetDirectoryName(e.Notification.InformativeText));
                }
            };

            // If we return true here, Notification will show up even if your app is TopMost.
            notificationCenter.ShouldPresentNotification = (c, n) => { return(true); };

            AlertNotificationRaised += delegate(string title, string message) {
                var alert = new NSAlert {
                    MessageText = message,
                    AlertStyle  = NSAlertStyle.Informational
                };

                alert.AddButton("OK");

                alert.RunModal();
            };

            Utils.SetUserNotificationListener(this);
            PathRepresentationConverter.SetConverter(new OSXPathRepresentationConverter());
        }
Esempio n. 2
0
        public Controller() : base()
        {
            using (var a = new NSAutoreleasePool())
            {
                NSApplication.Init();
            }

            // We get the Default notification Center
            notificationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;

            // Clear old notifications
            foreach (var n in notificationCenter.DeliveredNotifications)
            {
                notificationCenter.RemoveDeliveredNotification(n);
            }

            notificationCenter.DidDeliverNotification += (s, e) =>
            {
                Console.WriteLine("Notification Delivered");
            };

            // If the notification is clicked, displays the entire message.
            notificationCenter.DidActivateNotification += (object sender, UNCDidActivateNotificationEventArgs e) =>
            {
                var notification = (UserNotification)e.Notification;

                if (notification.Kind == UserNotification.NotificationKind.Normal)
                {
                    notificationCenter.RemoveDeliveredNotification(e.Notification);
                    string  msg   = notificationMessages[notification.Id];
                    NSAlert alert = NSAlert.WithMessage(notification.Title, "OK", null, null, msg);
                    notificationMessages.Remove(notification.Id);
                    alert.Icon = new NSImage(System.IO.Path.Combine(NSBundle.MainBundle.ResourcePath, "Pixmaps", "process-syncing-error.icns"));
                    alert.Window.OrderFrontRegardless();
                    alert.RunModal();
                }
                else
                {
                    LocalFolderClicked(Path.GetDirectoryName(e.Notification.InformativeText));
                }
            };

            // If we return true here, Notification will show up even if your app is TopMost.
            notificationCenter.ShouldPresentNotification = (c, n) => { return(true); };

            OnTransmissionListChanged += delegate {
                using (var a = new NSAutoreleasePool()) {
                    notificationCenter.BeginInvokeOnMainThread(delegate {
                        lock (transmissionLock) {
                            List <FileTransmissionEvent> transmissions      = ActiveTransmissions();
                            NSUserNotification[] notifications              = notificationCenter.DeliveredNotifications;
                            List <NSUserNotification> finishedNotifications = new List <NSUserNotification> ();
                            foreach (NSUserNotification notification in notifications)
                            {
                                FileTransmissionEvent transmission = transmissions.Find((FileTransmissionEvent e) => { return(e.Path == notification.InformativeText); });
                                if (transmission == null)
                                {
                                    finishedNotifications.Add(notification);
                                }
                                else
                                {
                                    if (transmissionFiles.ContainsKey(transmission.Path))
                                    {
                                        transmissions.Remove(transmission);
                                    }
                                    else
                                    {
                                        notificationCenter.RemoveDeliveredNotification(notification);
                                    }
                                }
                            }
                            finishedNotifications.Sort(new ComparerNSUserNotification());
                            for (int i = 0; i < (notifications.Length - notificationKeep) && i < finishedNotifications.Count; ++i)
                            {
                                notificationCenter.RemoveDeliveredNotification(finishedNotifications[i]);
                            }
                            foreach (FileTransmissionEvent transmission in transmissions)
                            {
                                if (transmission.Status.Aborted == true)
                                {
                                    continue;
                                }
                                if (transmission.Status.Completed == true)
                                {
                                    continue;
                                }
                                if (transmission.Status.FailedException != null)
                                {
                                    continue;
                                }
                                var notification = new UserNotification(UserNotification.NotificationKind.Transmission);
                                // NSUserNotification notification = new NSUserNotification();
                                notification.Title           = Path.GetFileName(transmission.Path);
                                notification.Subtitle        = TransmissionStatus(transmission);
                                notification.InformativeText = transmission.Path;
                                notificationMessages.Add(notification.Id, transmission.Path);
//                                notification.SoundName = NSUserNotification.NSUserNotificationDefaultSoundName;
                                transmission.TransmissionStatus += TransmissionReport;
                                // notification.DeliveryDate = NSDate.Now;
                                notificationCenter.DeliverNotification(notification);
                                transmissionFiles.Add(transmission.Path, notification.DeliveryDate);
                                UpdateFileStatus(transmission, null);
                            }
                        }
                    });
                }
            };

            AlertNotificationRaised += delegate(string title, string message) {
                var alert = new NSAlert {
                    MessageText = message,
                    AlertStyle  = NSAlertStyle.Informational
                };

                alert.AddButton("OK");

                alert.RunModal();
            };

            Utils.SetUserNotificationListener(this);
            PathRepresentationConverter.SetConverter(new OSXPathRepresentationConverter());
        }