Beispiel #1
0
        private void ShowTrackNotification()
        {
            // This has to happen before the next if, otherwise the last_* members aren't set correctly.
            if (current_track == null || (notify_last_title == current_track.DisplayTrackTitle &&
                                          notify_last_artist == current_track.DisplayArtistName))
            {
                return;
            }

            notify_last_title  = current_track.DisplayTrackTitle;
            notify_last_artist = current_track.DisplayArtistName;

            if (!show_notifications)
            {
                return;
            }

            foreach (var window in elements_service.ContentWindows)
            {
                if (window.HasToplevelFocus)
                {
                    return;
                }
            }

            bool is_notification_daemon = false;

            try {
                var name = Notifications.Global.ServerInformation.Name;
                is_notification_daemon = name == "notification-daemon" || name == "Notification Daemon";
            } catch {
                // This will be reached if no notification daemon is running
                return;
            }


            string message = GetByFrom(
                current_track.ArtistName, current_track.DisplayArtistName,
                current_track.AlbumTitle, current_track.DisplayAlbumTitle);

            string image = null;

            image = is_notification_daemon
                ? CoverArtSpec.GetPathForSize(current_track.ArtworkId, icon_size)
                : CoverArtSpec.GetPath(current_track.ArtworkId);

            if (!File.Exists(new SafeUri(image)))
            {
                if (artwork_manager_service != null)
                {
                    // artwork does not exist, try looking up the pixbuf to trigger scaling or conversion
                    Gdk3.Pixbuf tmp_pixbuf = is_notification_daemon
                        ? artwork_manager_service.LookupScalePixbuf(current_track.ArtworkId, icon_size)
                        : artwork_manager_service.LookupPixbuf(current_track.ArtworkId);

                    if (tmp_pixbuf == null)
                    {
                        image = "audio-x-generic";
                    }
                    else
                    {
                        tmp_pixbuf.Dispose();
                    }
                }
            }
            try {
                if (current_nf == null)
                {
                    current_nf = new Notification(current_track.DisplayTrackTitle, message, image);
                }
                else
                {
                    current_nf.Summary  = current_track.DisplayTrackTitle;
                    current_nf.Body     = message;
                    current_nf.IconName = image;
                }

                current_nf.Urgency = Urgency.Low;
                current_nf.Timeout = 4500;

                if (!current_track.IsLive && ActionsSupported && interface_action_service.PlaybackActions["NextAction"].Sensitive)
                {
                    current_nf.AddAction("skip-song", AddinManager.CurrentLocalizer.GetString("Skip this item"), OnSongSkipped);
                }
                current_nf.Show();
            } catch (Exception e) {
                Hyena.Log.Warning("Cannot show notification", e.Message, false);
            }
        }
Beispiel #2
0
        public Pixbuf LookupScalePixbuf(string id, int size)
        {
            if (id == null || (size != 0 && size < 10))
            {
                return(null);
            }

            if (null_artwork_ids.Contains(id))
            {
                return(null);
            }

            // Find the scaled, cached file
            string path = CoverArtSpec.GetPathForSize(id, size);

            if (File.Exists(new SafeUri(path)))
            {
                try {
                    return(new Pixbuf(path));
                } catch {
                    null_artwork_ids.Add(id);
                    return(null);
                }
            }

            string orig_path   = CoverArtSpec.GetPathForSize(id, 0);
            bool   orig_exists = File.Exists(new SafeUri(orig_path));

            if (!orig_exists)
            {
                // It's possible there is an image with extension .cover that's waiting
                // to be converted into a jpeg
                string unconverted_path = System.IO.Path.ChangeExtension(orig_path, "cover");
                if (File.Exists(new SafeUri(unconverted_path)))
                {
                    try {
                        Pixbuf pixbuf = new Pixbuf(unconverted_path);
                        if (pixbuf.Width < 50 || pixbuf.Height < 50)
                        {
                            Hyena.Log.DebugFormat("Ignoring cover art {0} because less than 50x50", unconverted_path);
                            null_artwork_ids.Add(id);
                            return(null);
                        }

                        pixbuf.Save(orig_path, "jpeg");
                        orig_exists = true;
                    } catch {
                    } finally {
                        File.Delete(new SafeUri(unconverted_path));
                    }
                }
            }

            if (orig_exists && size >= 10)
            {
                try {
                    Pixbuf pixbuf = new Pixbuf(orig_path);

                    // Make it square if width and height difference is within 20%
                    const double max_ratio = 1.2;
                    double       ratio = (double)pixbuf.Height / pixbuf.Width;
                    int          width = size, height = size;
                    if (ratio > max_ratio)
                    {
                        width = (int)Math.Round(size / ratio);
                    }
                    else if (ratio < 1d / max_ratio)
                    {
                        height = (int)Math.Round(size * ratio);
                    }

                    Pixbuf scaled_pixbuf = pixbuf.ScaleSimple(width, height, Gdk.InterpType.Bilinear);

                    if (IsCachedSize(size))
                    {
                        Directory.Create(System.IO.Path.GetDirectoryName(path));
                        scaled_pixbuf.Save(path, "jpeg");
                    }
                    else
                    {
                        Log.InformationFormat("Uncached artwork size {0} requested", size);
                    }

                    DisposePixbuf(pixbuf);
                    return(scaled_pixbuf);
                } catch {}
            }

            null_artwork_ids.Add(id);
            return(null);
        }