More logic to edit the song metadata
[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 songItem import SongItem
17 from coverModel import CoversModel 
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 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)
37             counter += 1
38
39
40 class MassiveDownloadsThread (QtCore.QThread):
41
42     def __init__ (self, albumModel):
43         QtCore.QThread.__init__ (self)
44         self.downloader = MussorgskyAlbumArt ()
45         self.albumModel = albumModel
46
47     def run (self):
48         print "Download one cover per-album in a thread"
49         for albumItem in self.albumModel.get_albums ():
50             if albumItem.require_download:
51                 self.downloader.get_album_art (albumItem)
52             
53
54 class FreeSearchThread (QtCore.QThread):
55
56     def __init__ (self, model, search_str):
57         QtCore.QThread.__init__ (self)
58         self.downloader = MussorgskyAlbumArt ()
59         self.model = model
60         self.search_str = search_str
61
62     def run (self):
63         print "Free search thread:", self.search_str
64         MAX_OPTIONS = 4
65         counter = 0
66         for img in self.downloader.get_alternatives_free_text (self.search_str):
67             if counter >= MAX_OPTIONS:
68                 break
69
70             self.model.updateData (counter, img)
71             counter += 1
72
73 class MussorgskyController (QtCore.QObject):
74
75     def __init__ (self, rootContext):
76         QtCore.QObject.__init__ (self)
77         self.download = None
78         self.ctx = rootContext 
79         self.tracker = TrackerBackend ()
80         self.__is_downloading = False
81
82     def _is_downloading (self):
83         return self.__is_downloading
84
85     def _set_is_downloading (self, value):
86         if (value != self.__is_downloading):
87             self.__is_downloading = value
88             self.is_downloading_changed.emit ()
89
90     is_downloading_changed = QtCore.Signal ()
91     is_downloading = QtCore.Property (bool, _is_downloading, notify=is_downloading_changed)
92
93     @QtCore.Slot (QtCore.QObject)
94     def download_all (self, albumModel):
95         self._set_is_downloading (True)
96         self.download_all_thread = MassiveDownloadsThread (albumModel)
97         self.download_all_thread.finished.connect (self.download_all_finished)
98         self.download_all_thread.start ()
99         print "now we are downloading..."
100
101     @QtCore.Slot ()
102     def download_all_finished (self):
103         self._set_is_downloading (False)
104         
105
106     @QtCore.Slot (QtCore.QObject)
107     def get_options_for (self, albumItem):
108         print "Getting options for", albumItem.title
109         m = CoversModel (albumItem)
110         print m.rowCount ()
111         self.ctx.setContextProperty ("coversModel", m)
112         if self.download:
113             self.download.quit ()
114         self.download = DownloadThread (m, albumItem)
115         self.download.start ()
116
117
118     @QtCore.Slot (QtCore.QObject, str)
119     def get_options_for_text (self, coversModel, search_str):
120         if self.download:
121             print "Finishing the previous work"
122             self.download.quit ()
123         coversModel.reset ()
124         self.download = FreeSearchThread (coversModel, search_str)
125         self.download.start ()
126
127
128
129     @QtCore.Slot (QtCore.QObject, int)
130     def save_option_for (self, coversModel, index):
131         print "Saving option", index
132         coverItem = coversModel.getData (index)
133
134         if coverItem.initialImage:
135             # Same image as before, nothing to do
136             pass
137         elif coverItem.deleteAction:
138             coversModel.albumItem.unset_image ()
139         else:
140             coversModel.albumItem.set_image (coverItem.url)
141
142         coversModel.cleanCache ()
143
144
145     @QtCore.Slot ()
146     def stop_pending_jobs (self):
147         if self.download :
148             self.download.quit ()
149             self.download = None
150
151     @QtCore.Slot (QtCore.QObject)
152     def save_song (self, songItem):
153         if songItem.dirty:
154             print "The song was modified and we should write the file!"
155             print " **** Implement me! ****"
156         else:
157             print "Nothing to write into the file"
158
159     def get_all_albums (self):
160         """
161         Return a list of AlbumItem objects to build the model
162         This is not called from QML, no need to make it a slot
163         """
164         results = []
165         for album_title, album_artist in self.tracker.get_all_albums ():
166             results.append (AlbumItem (album_title, album_artist))
167         return results
168
169
170     def get_all_songs (self):
171         results = []
172         for urn, title, artist, album in self.tracker.get_all_songs ():
173             results.append (SongItem (urn, title, album, artist))
174         return results