QML: QObjects for model and albums
authorIvan Frade <ivan.frade@nokia.com>
Fri, 1 Apr 2011 09:18:14 +0000 (12:18 +0300)
committerIvan Frade <ivan.frade@nokia.com>
Fri, 1 Apr 2011 09:18:14 +0000 (12:18 +0300)
src/qml/__init__.py [new file with mode: 0644]
src/qml/albumItem.py [new file with mode: 0644]
src/qml/albumModel.py [new file with mode: 0644]
src/qml/controller.py [new file with mode: 0644]
src/qml/logic.py [new file with mode: 0644]
src/qml/tracker_backend_gi.py [new file with mode: 0644]

diff --git a/src/qml/__init__.py b/src/qml/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/qml/albumItem.py b/src/qml/albumItem.py
new file mode 100644 (file)
index 0000000..377d324
--- /dev/null
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+import os
+import sys
+from PySide import QtCore
+from PySide import QtGui
+from PySide import QtDeclarative
+
+
+class AlbumItem (QtCore.QObject):
+
+    def __init__ (self, title, artist, album_art):
+        QtCore.QObject.__init__(self)
+        self._title = title
+        self._artist = artist
+        self._album_art = album_art
+
+    def _title (self):
+        return self._title
+
+    def _artist (self):
+        return self._artist
+
+    def _album_art (self):
+        return self._album_art
+
+    @QtCore.Signal
+    def changed (self): pass
+
+    title = QtCore.Property (unicode, _title, notify=changed)
+    artist = QtCore.Property (unicode, _artist, notify=changed)
+    album_art = QtCore.Property (unicode, _album_art, notify=changed)
+    
diff --git a/src/qml/albumModel.py b/src/qml/albumModel.py
new file mode 100644 (file)
index 0000000..12ef53f
--- /dev/null
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+import os
+import sys
+from PySide import QtCore
+from PySide import QtGui
+from PySide import QtDeclarative
+
+from albumItem import AlbumItem
+
+class AlbumModel (QtCore.QAbstractListModel):
+    COLUMNS = ('album', )
+
+    def __init__ (self, data):
+        QtCore.QAbstractListModel.__init__ (self)
+        self._albums = data
+        self.setRoleNames (dict(enumerate(AlbumModel.COLUMNS)))
+
+    def rowCount (self, parent=QtCore.QModelIndex()):
+        return len (self._albums)
+
+    def data (self, index, role):
+        if index.isValid () and role == AlbumModel.COLUMNS.index ('album'):
+            return self._albums[index.row ()]
+        return None
diff --git a/src/qml/controller.py b/src/qml/controller.py
new file mode 100644 (file)
index 0000000..48cabf6
--- /dev/null
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+import os
+import sys
+from PySide import QtCore
+from PySide import QtGui
+from PySide import QtDeclarative
+
+
+class Controller (QtCore.QObject):
+
+    @QtCore.Slot (QtCore.QObject)
+    def albumSelected (self, wrapper):
+        print "Called controller, but nothing to do here yet"
+
diff --git a/src/qml/logic.py b/src/qml/logic.py
new file mode 100644 (file)
index 0000000..567ddc3
--- /dev/null
@@ -0,0 +1,31 @@
+import os
+
+from tracker_backend_gi import TrackerBackendGI
+
+from albumItem import AlbumItem
+from album_art_spec import getCoverArtThumbFileName
+
+
+class MussorgskyLogic:
+
+    def __init__ (self):
+        self.tracker = TrackerBackendGI ()
+
+
+    def get_all_albums (self):
+        results = []
+        for album_title, album_artist in self.tracker.get_all_albums ():
+            album_art = getCoverArtThumbFileName (album_title)
+            if (not os.path.exists (album_art)):
+                album_art = None
+            
+            results.append (AlbumItem (album_title, album_artist, album_art))
+        return results
+
+
+# To test everything is fine here
+if __name__ == "__main__":
+    l = MussorgskyLogic ()
+    l.get_all_albums ()
+    
+
diff --git a/src/qml/tracker_backend_gi.py b/src/qml/tracker_backend_gi.py
new file mode 100644 (file)
index 0000000..f5329a0
--- /dev/null
@@ -0,0 +1,40 @@
+import gi
+from gi.repository import Tracker
+
+# (album name, artist1|artist2|... , number of artists)
+ALBUM_ARTISTS_COUNTER = """
+SELECT nie:title (?album) nmm:artistName (?artist) COUNT (?artist)
+WHERE
+    {
+     ?album a nmm:MusicAlbum ; 
+            nmm:albumArtist ?artist .
+    }
+GROUP BY ?album
+"""
+
+class TrackerBackendGI:
+
+    def __init__ (self):
+        self.conn = Tracker.SparqlConnection.get (None)
+
+    def get_all_albums (self):
+        """
+        Generator returning (album_title, album_artist)
+        If the album has more than one artist, "Various artists"
+        """
+        cursor = self.conn.query (ALBUM_ARTISTS_COUNTER, None)
+        while cursor.next (None):
+            album_title = cursor.get_string (0)[0]
+            if cursor.get_integer(2) > 1:
+                album_artist = "Various artists"
+            else:
+                album_artist = cursor.get_string (1)[0]
+
+            yield (album_title, album_artist)
+            
+
+
+if __name__ == "__main__":
+
+    tracker = TrackerBackendGI()
+    tracker.get_all_albums ()