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