QML: Click on album shows the alternatives and saves them
[mussorgsky] / src / qml / controller.py
1 # -*- coding: utf-8 -*-
2 import os
3 import sys
4 from PySide import QtCore
5 from PySide import QtGui
6 from PySide import QtDeclarative
7
8 from aa_search import MussorgskyAlbumArt
9
10 from tracker_backend_gi import TrackerBackendGI
11
12 from albumItem import AlbumItem
13 from aa_spec import getCoverArtThumbFileName, getCoverArtFileName
14
15
16 class DownloadThread (QtCore.QThread):
17
18     def __init__ (self, model, album):
19         QtCore.QThread.__init__ (self)
20         self.downloader = MussorgskyAlbumArt ()
21         self.model = model
22         self.album = album
23
24     def run (self):
25         print "Running the thread"
26         MAX_OPTIONS = 4
27         counter = 0
28         for img, thumb in self.downloader.get_alternatives (self.album.artist,
29                                                             self.album.title, MAX_OPTIONS):
30             if counter >= MAX_OPTIONS:
31                 break
32
33             self.model.updateData (counter, img, thumb)
34             counter += 1
35
36
37 class MussorgskyController (QtCore.QObject):
38
39     def __init__ (self):
40         QtCore.QObject.__init__ (self)
41         self.download = None
42         self.tracker = TrackerBackendGI ()
43
44     @QtCore.Slot (QtCore.QObject, QtCore.QObject)
45     def albumSelected (self, model, album):
46         """
47         Starts a thread to look for possible images online.
48         The thread will update the model (and the changes are visible in the UI)
49         """
50         print "clicked on", album.title
51         self.download = DownloadThread (model, album)
52         self.download.start ()
53
54     @QtCore.Slot (QtCore.QObject, QtCore.QObject)
55     def albumSelected (self, model, album):
56         """
57         Starts a thread to look for possible images online.
58         The thread will update the model (and the changes are visible in the UI)
59         """
60         print "clicked on", album.title
61         self.download = DownloadThread (model, album)
62         self.download.start ()
63
64     @QtCore.Slot (str, QtCore.QObject)
65     def coverSelected (self, coverObject, albumObject):
66         """
67         The user has clicked in one cover!
68         """
69         print "Selected cover"
70         filename = getCoverArtFileName (albumObject.title)
71         thumbnail = getCoverArtThumbFileName (albumObject.title)
72
73         os.rename (cover.url, filename)
74         os.rename (cover.thumb, thumbnail)
75
76         albumObject.album_art = thumbnail
77         albumObject.album_art_changed.emit ()
78
79     @QtCore.Slot (QtCore.QObject)
80     def resetAlternatives (self, coversModel):
81         print "Reseting alternatives", coversModel
82         QtGui.QPixmapCache.clear ()
83         coversModel.resetAlternatives ()
84
85
86     def get_all_albums (self):
87         """
88         Return a list of AlbumItem objects to build the model
89         This is not called from QML, no need to make it a slot
90         """
91         results = []
92         for album_title, album_artist in self.tracker.get_all_albums ():
93             album_art = getCoverArtThumbFileName (album_title)
94             if (not os.path.exists (album_art)):
95                 album_art = None
96             
97             results.append (AlbumItem (album_title, album_artist, album_art))
98         return results