Move the qml/* files to the src folder. Now it is the main version
[mussorgsky] / src / 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 coverModel import CoversModel 
17
18 class DownloadThread (QtCore.QThread):
19
20     def __init__ (self, model, album):
21         QtCore.QThread.__init__ (self)
22         self.downloader = MussorgskyAlbumArt ()
23         self.model = model
24         self.album = album
25
26     def run (self):
27         print "Running the thread"
28         MAX_OPTIONS = 4
29         counter = 0
30         for img in self.downloader.get_alternatives (self.album.artist,
31                                                      self.album.title, MAX_OPTIONS):
32             if counter >= MAX_OPTIONS:
33                 break
34
35             self.model.updateData (counter, img)
36             counter += 1
37
38
39 class MassiveDownloadsThread (QtCore.QThread):
40
41     def __init__ (self, albumModel):
42         QtCore.QThread.__init__ (self)
43         self.downloader = MussorgskyAlbumArt ()
44         self.albumModel = albumModel
45
46     def run (self):
47         print "Download one cover per-album in a thread"
48         import time
49         time.sleep (4)
50         for albumItem in self.albumModel.get_albums ():
51             if albumItem.require_download:
52                 self.downloader.get_album_art (albumItem)
53             
54
55 class MussorgskyController (QtCore.QObject):
56
57     def __init__ (self, rootContext):
58         QtCore.QObject.__init__ (self)
59         self.download = None
60         self.ctx = rootContext 
61         self.tracker = TrackerBackend ()
62         self.__is_downloading = False
63
64     def _is_downloading (self):
65         return self.__is_downloading
66
67     def _set_is_downloading (self, value):
68         if (value != self.__is_downloading):
69             self.__is_downloading = value
70             self.is_downloading_changed.emit ()
71
72     is_downloading_changed = QtCore.Signal ()
73     is_downloading = QtCore.Property (bool, _is_downloading, notify=is_downloading_changed)
74
75     @QtCore.Slot (QtCore.QObject)
76     def download_all (self, albumModel):
77         self._set_is_downloading (True)
78         self.download_all_thread = MassiveDownloadsThread (albumModel)
79         self.download_all_thread.finished.connect (self.download_all_finished)
80         self.download_all_thread.start ()
81         print "now we are downloading..."
82
83     @QtCore.Slot ()
84     def download_all_finished (self):
85         self._set_is_downloading (False)
86         
87
88     @QtCore.Slot (QtCore.QObject)
89     def get_options_for (self, albumItem):
90         print "Getting options for", albumItem.title
91         m = CoversModel (albumItem)
92         print m.rowCount ()
93         self.ctx.setContextProperty ("coversModel", m)
94         self.download = DownloadThread (m, albumItem)
95         self.download.start ()
96
97     @QtCore.Slot (QtCore.QObject, int)
98     def save_option_for (self, coversModel, index):
99         print "Saving option", index
100         coverItem = coversModel.getData (index)
101         coverItem.save (coversModel.albumItem.get_aa ().get_media_art_path ())
102         # Update the main model. Is this enough?
103         if not coverItem.initialImage and not coverItem.deleteAction:
104             coversModel.albumItem.album_art = None
105             coversModel.albumItem.album_art = coversModel.albumItem.get_aa().get_media_art_path ()
106         elif coverItem.deleteAction:
107             coversModel.albumItem.resetAlbumArt ()
108
109         coversModel.cleanCache ()
110
111
112     @QtCore.Slot ()
113     def stop_pending_jobs (self):
114         if self.download :
115             self.download.quit ()
116
117     def get_all_albums (self):
118         """
119         Return a list of AlbumItem objects to build the model
120         This is not called from QML, no need to make it a slot
121         """
122         results = []
123         for album_title, album_artist in self.tracker.get_all_albums ():
124             results.append (AlbumItem (album_title, album_artist))
125         return results
126