1#!/usr/local/bin/python3.8
2
3from gi.repository import Gio, Gtk
4
5from util import trackers, settings
6from baseWindow import BaseWindow
7from floating import Floating
8from widgets.framedImage import FramedImage
9
10import singletons
11import status
12
13class AlbumArt(Floating, BaseWindow):
14    """
15    AlbumArt
16
17    It is a child of the Stage's GtkOverlay, and its placement is
18    controlled by the overlay's child positioning function.
19
20    When not Awake, it positions itself around all monitors
21    using a timer which randomizes its halign and valign properties
22    as well as its current monitor.
23    """
24    def __init__(self, away_message=None, initial_monitor=0):
25        super(AlbumArt, self).__init__(initial_monitor)
26        self.get_style_context().add_class("albumart")
27        self.set_halign(Gtk.Align.END)
28
29        if not settings.get_show_albumart():
30            return
31
32        self.watcher = singletons.MediaPlayerWatcher
33        self.player = self.watcher.get_best_player()
34
35        self.current_url = None
36
37        self.image = FramedImage(status.screen.get_low_res_mode(), scale_up=True)
38        self.image.show()
39        self.image.set_opacity(0.0)
40        self.add(self.image)
41
42        trackers.con_tracker_get().connect(self.image,
43                                           "surface-changed",
44                                           self.on_surface_changed)
45
46        if self.player != None:
47            trackers.con_tracker_get().connect(self.player,
48                                               "metadata-changed",
49                                               self.on_metadata_changed)
50            self.on_metadata_changed(self.player)
51
52    def on_surface_changed(self, image, surface):
53        if surface != None:
54            self.image.set_opacity(1.0)
55        else:
56            self.image.set_opacity(0.0)
57
58    def on_metadata_changed(self, player):
59        self.update_image()
60
61    def update_image(self):
62        url = self.player.get_albumart_url()
63
64        if self.player.get_identity() == "spotify":
65            url = url.replace("open.spotify.com", "i.scdn.co");
66
67        if url == self.current_url:
68            return
69
70        self.current_url = url
71
72        if url == "":
73            self.image.clear_image()
74
75        f = Gio.File.new_for_uri(url)
76
77        if f.get_uri_scheme() == "file":
78            self.image.set_from_path(f.get_path())
79        elif f.get_uri_scheme() == "http":
80            self.image.set_from_file(f)
81