示例#1
0
        void Notify(object iTrack)
        {
            if (!Properties.Settings.Default.DisableNotifications)
            {
                IITTrack song = (IITTrack)iTrack;
                string title = Escape(song.Name);
                string text = String.Format("{0}\n{1}", Escape(song.Artist), Escape(song.Album));
                // this handles streaming radio stations
                if (this.CurrentStreamTitle != null)
                {
                    title = Escape(this.CurrentStreamTitle);
                    text = String.Format("Station: {0}", Escape(song.Name));
                }

                // TODO:
                string artworkFilePath = null;
                byte[] artworkData = null;
                if (song.Artwork != null && song.Artwork.Count > 0)
                {
                    string safeAlbumName = GetSafeFileName(song.Album);
                    string filename = String.Format("{0}.jpg", safeAlbumName);
                    artworkFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
                    if (!System.IO.File.Exists(artworkFilePath))
                        song.Artwork[1].SaveArtworkToFile(artworkFilePath);
                    artworkData = System.IO.File.ReadAllBytes(artworkFilePath);
                }

                // old
                if (Properties.Settings.Default.SendUDPNotifications)
                {
                    udpGrowl.Notify(udpNotificationType, title, text, Growl.Connector.Priority.Normal, false);
                }

                // new
                if (Properties.Settings.Default.SendGNTPNotifications)
                {
                    int sourceID;
                    int playlistID;
                    int trackID;
                    int databaseID;
                    song.GetITObjectIDs(out sourceID, out playlistID, out trackID, out databaseID);

                    Growl.Connector.CallbackContext callback = new Growl.Connector.CallbackContext("song", String.Format("{0}|{1}|{2}|{3}", sourceID, playlistID, trackID, databaseID));

                    //Growl.CoreLibrary.Resource albumIcon = artworkFilePath;
                    Growl.CoreLibrary.Resource albumIcon = (artworkData != null ? new Growl.CoreLibrary.BinaryData(artworkData) : null);
                    Growl.Connector.Notification notification = new Growl.Connector.Notification(application.Name, nt1.Name, id, title, text, albumIcon, false, Growl.Connector.Priority.Normal, id);

                    notification.CustomTextAttributes.Add("iTunes-Artist", song.Artist);
                    notification.CustomTextAttributes.Add("iTunes-Album", song.Album);
                    notification.CustomTextAttributes.Add("iTunes-Duration", song.Duration.ToString());
                    notification.CustomTextAttributes.Add("iTunes-PlayedCount", song.PlayedCount.ToString());
                    notification.CustomTextAttributes.Add("iTunes-Genre", song.Genre);
                    notification.CustomTextAttributes.Add("iTunes-Rating", song.Rating.ToString());

                    growl.Notify(notification, callback);
                }
            }
        }
示例#2
0
        public void sendNotification(string track, string artist)
        {
            string CoverUrl = getAlbumArt(track, artist);

            //Notification specific icon.
            Growl.CoreLibrary.Resource Icon = !String.IsNullOrEmpty(CoverUrl) ? CoverUrl : getCurrentWorkingDirectory();
            Priority priority = 0;

            //If no artist metadata is found set Artist=UNKNOWN
            if (String.IsNullOrEmpty(artist))
            {
                artist = "UNKNOWN";
            }

            Notification notification = new Notification("Spotify", "SPOTIFYNOTIFICATION", null, artist, track, Icon, false, priority, null);


            //Notify growl.
            growl.Notify(notification);
        }
示例#3
0
        public static int Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                Console.WriteLine("Invalid arguments. See /? for usage.");
                return(-1);
            }

            if (args[0] == "/?")
            {
                Console.WriteLine();
                string usage = global::growlnotify.Properties.Resources.usage;
                Console.WriteLine(usage);
                return(0);
            }

            // parse parameters
            Dictionary <string, Parameter> parameters = new Dictionary <string, Parameter>();

            try
            {
                foreach (string arg in args)
                {
                    Parameter p = GetParameterValue(arg);
                    parameters.Add(p.Argument, p);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Bad arguments : " + ex.Message);
                return(-1);
            }

            // set default values
            string messageText     = "";
            string title           = "growlnotify";
            string id              = "";
            string coalescingid    = null;
            bool   sticky          = false;
            int    priorityInt     = 0;
            string iconFile        = null;
            string applicationName = "growlnotify";
            string appIconFile     = null;

            string[] notificationTypesToRegister = null;
            string   notificationType            = "General Notification";
            string   callbackUrl = null;
            string   protocol    = "GNTP";
            string   host        = "localhost";
            string   password    = null;

            Cryptography.SymmetricAlgorithmType encryptionAlgorithm = Cryptography.SymmetricAlgorithmType.PlainText;
            Cryptography.HashAlgorithmType      hashAlgorithm       = Cryptography.HashAlgorithmType.MD5;
            int port = GrowlConnector.TCP_PORT;

            // validate required parameters
            if (!parameters.ContainsKey("messagetext"))
            {
                Console.WriteLine("Missing 'messagetext' argument. See /? for usage");
                return(-1);
            }
            else
            {
                messageText = parameters["messagetext"].Value;
            }
            if (parameters.ContainsKey("/t"))
            {
                title = parameters["/t"].Value;
            }
            if (parameters.ContainsKey("/id"))
            {
                id = parameters["/id"].Value;
            }
            if (parameters.ContainsKey("/s"))
            {
                string s = parameters["/s"].Value.ToLower();
                if (s == "true")
                {
                    sticky = true;
                }
            }
            if (parameters.ContainsKey("/p"))
            {
                priorityInt = Convert.ToInt32(parameters["/p"].Value);
            }
            if (parameters.ContainsKey("/i"))
            {
                iconFile = parameters["/i"].Value;
                if (iconFile.StartsWith("."))
                {
                    string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    iconFile = System.IO.Path.Combine(root, iconFile);
                }
            }
            if (parameters.ContainsKey("/c"))
            {
                coalescingid = parameters["/c"].Value;
            }
            if (parameters.ContainsKey("/a"))
            {
                applicationName = parameters["/a"].Value;
            }
            if (parameters.ContainsKey("/ai"))
            {
                appIconFile = parameters["/ai"].Value;
                if (appIconFile.StartsWith("."))
                {
                    string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    appIconFile = System.IO.Path.Combine(root, iconFile);
                }
            }
            if (parameters.ContainsKey("/r"))
            {
                string[] parts = parameters["/r"].Value.Split(',');
                if (parts != null && parts.Length > 0)
                {
                    notificationTypesToRegister = new string[parts.Length];
                    for (int p = 0; p < parts.Length; p++)
                    {
                        string val = parts[p];
                        if (val.StartsWith("\""))
                        {
                            val = val.Substring(1, val.Length - 1);
                        }
                        if (val.EndsWith("\""))
                        {
                            val = val.Substring(0, val.Length - 1);
                        }
                        notificationTypesToRegister[p] = val;
                    }
                }
            }
            if (parameters.ContainsKey("/n"))
            {
                notificationType = parameters["/n"].Value;
            }
            if (parameters.ContainsKey("/cu"))
            {
                callbackUrl = parameters["/cu"].Value;
            }
            if (parameters.ContainsKey("/protocol"))
            {
                protocol = parameters["/protocol"].Value;
            }
            if (parameters.ContainsKey("/host"))
            {
                host = parameters["/host"].Value;
            }
            if (parameters.ContainsKey("/port"))
            {
                port = Convert.ToInt32(parameters["/port"].Value);
            }
            else
            {
                if (protocol == "UDP")
                {
                    port = 9887;
                }
            }
            if (parameters.ContainsKey("/pass"))
            {
                password = parameters["/pass"].Value;
            }
            if (parameters.ContainsKey("/enc"))
            {
                switch (parameters["/enc"].Value.ToUpper())
                {
                case "DES":
                    encryptionAlgorithm = Cryptography.SymmetricAlgorithmType.DES;
                    break;

                case "3DES":
                    encryptionAlgorithm = Cryptography.SymmetricAlgorithmType.TripleDES;
                    break;

                case "AES":
                    encryptionAlgorithm = Cryptography.SymmetricAlgorithmType.AES;
                    break;

                default:
                    encryptionAlgorithm = Cryptography.SymmetricAlgorithmType.PlainText;
                    break;
                }
            }
            if (parameters.ContainsKey("/hash"))
            {
                switch (parameters["/hash"].Value.ToUpper())
                {
                case "SHA1":
                    hashAlgorithm = Cryptography.HashAlgorithmType.SHA1;
                    break;

                case "SHA256":
                    hashAlgorithm = Cryptography.HashAlgorithmType.SHA256;
                    break;

                case "SHA512":
                    hashAlgorithm = Cryptography.HashAlgorithmType.SHA512;
                    break;

                default:
                    hashAlgorithm = Cryptography.HashAlgorithmType.MD5;
                    break;
                }
            }
            if (parameters.ContainsKey("/silent"))
            {
                string s = parameters["/silent"].Value.ToLower();
                if (s == "true")
                {
                    silent = true;
                }
            }

            // set up a waithandle so we can wait for responses
            ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

            // set up growl connector
            growl = new GrowlConnector(password, host, port);
            growl.EncryptionAlgorithm = encryptionAlgorithm;
            growl.KeyHashAlgorithm    = hashAlgorithm;
            growl.OKResponse         += new GrowlConnector.ResponseEventHandler(growl_Response);
            growl.ErrorResponse      += new GrowlConnector.ResponseEventHandler(growl_Response);

            // do any registration first
            if (notificationTypesToRegister != null || applicationName == "growlnotify")
            {
                Growl.CoreLibrary.Resource appIcon = null;
                if (!String.IsNullOrEmpty(appIconFile))
                {
                    Uri uri = new Uri(appIconFile);
                    if (uri.IsFile && System.IO.File.Exists(uri.LocalPath))
                    {
                        appIcon = Growl.CoreLibrary.ImageConverter.ImageFromUrl(uri.LocalPath);
                    }
                    else
                    {
                        appIcon = appIconFile;
                    }
                }

                if (notificationTypesToRegister == null)
                {
                    notificationTypesToRegister = new string[] { "General Notification" };
                }
                NotificationType[] types = new NotificationType[notificationTypesToRegister.Length];
                for (int t = 0; t < types.Length; t++)
                {
                    string           nttr = notificationTypesToRegister[t];
                    NotificationType type = new NotificationType(nttr);
                    types[t] = type;
                }
                Application application = new Application(applicationName);
                application.Icon = appIcon;
                growl.Register(application, types);
                ewh.WaitOne();  // wait just to be sure the registration gets there first
            }

            // handle any callback information
            CallbackContext callback = null;

            if (!String.IsNullOrEmpty(callbackUrl))
            {
                callback = new CallbackContext(callbackUrl);
            }

            ewh.Reset();

            // handle icons (local icons will be sent as binary data, url-based icons will be sent as urls)
            Growl.CoreLibrary.Resource icon = null;
            if (!String.IsNullOrEmpty(iconFile))
            {
                Uri uri = new Uri(iconFile);
                if (uri.IsFile && System.IO.File.Exists(uri.LocalPath))
                {
                    icon = Growl.CoreLibrary.ImageConverter.ImageFromUrl(uri.LocalPath);
                }
                else
                {
                    icon = iconFile;
                }
            }

            // send the notification
            Priority     priority     = (Enum.IsDefined(typeof(Priority), priorityInt) ? (Priority)priorityInt : Priority.Normal);
            Notification notification = new Notification(applicationName, notificationType, id, title, messageText, icon, sticky, priority, coalescingid);

            growl.Notify(notification, callback);
            ewh.WaitOne();

            Console.WriteLine();
            return(r);
        }
示例#4
0
 private void Notify(string title, string message, string cover, NotifyType notificationType)
 {
     Growl.CoreLibrary.Resource Icon = cover;
     growl.Notify(new Notification(applicationName, notificationType.Type, DateTime.Now.Ticks.ToString(), message, title, Icon, false, Priority.VeryLow, null));
 }