[gnome-music/playlists: 8/10] clean duplicated code



commit 5de9bdcde357a408fce02d9b466ef043e1feb492
Author: Eslam Mostafa <me eslammostafa com>
Date:   Tue Jul 2 02:58:40 2013 +0200

    clean duplicated code

 src/view.js    |   41 ++++++----------------------------
 src/widgets.js |   66 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 52 deletions(-)
---
diff --git a/src/view.js b/src/view.js
index 5b7a616..f058c1a 100644
--- a/src/view.js
+++ b/src/view.js
@@ -319,43 +319,16 @@ const Songs = new Lang.Class({
         this.countQuery = Query.songs_count;
         this._items = {};
         this.isStarred = null;
-        this.view = new Widgets.SongsList(player);
+        this.view = new Widgets.SongsList(player, this._model);
         this._iconHeight = 32;
         this._iconWidth = 32;
         this._symbolicIcon = albumArtCache.makeDefaultIcon(this._iconHeight, this._iconWidth)
         this.player = player;
     },
 
-    _addItem: function(source, param, item) {
-        if (item != null) {
-            this._offset += 1;
-            var iter = this._model.append();
-            if ((item.get_title() == null) && (item.get_url() != null)) {
-                item.set_title (extractFileName(item.get_url()));
-            }
-            try{
-                if (item.get_url())
-                    this.player.discoverer.discover_uri(item.get_url());
-                this._model.set(
-                        iter,
-                        [5, 8, 9, 10],
-                        [item, nowPlayingIconName, false, false]
-                    );
-            } catch(err) {
-                log(err.message);
-                log("failed to discover url " + item.get_url());
-                this._model.set(
-                        iter,
-                        [5, 8, 9, 10],
-                        [item, errorIconName, false, true]
-                    );
-            }
-        }
-    },
-
     populate: function() {
         if (grilo.tracker != null)
-            grilo.populateSongs (this._offset, Lang.bind(this, this._addItem, null));
+            grilo.populateSongs (this._offset, Lang.bind(this, this.view.addItem, null));
     },
 
 });
@@ -482,18 +455,18 @@ const Playlists = new Lang.Class({
         this.view.set_hexpand(false);
         this.view.get_style_context().add_class("artist-panel");
         this.view.get_generic_view().get_selection().set_mode(Gtk.SelectionMode.SINGLE);
-        this._songsListWidget = new Widgets.SongsList(this.player);
+        this._songsListWidget = new Widgets.SongsList(this.player, null);
         let builder = new Gtk.Builder();
-        builder.add_from_resource('/org/gnome/music/PlaylistControls.ui');
-        let controls = builder.get_object('container');
+        //builder.add_from_resource('/org/gnome/music/PlaylistControls.ui');
+        //let controls = builder.get_object('container');
         builder.add_from_resource('/org/gnome/music/PlaylistSongs.ui');
         let songsFrame = builder.get_object('container');
         let viewport = builder.get_object('viewport');
         viewport.add(this._songsListWidget);
         this._grid.attach(new Gtk.Separator(), 0, 1, 1, 1);
         //this._grid.attach(controls, 0, 2, 1, 1);
-        this._grid.attach(new Gtk.Separator({orientation: Gtk.Orientation.VERTICAL}), 1, 0, 1, 3);
-        this._grid.attach(songsFrame, 2, 0, 2, 3);
+        this._grid.attach(new Gtk.Separator({orientation: Gtk.Orientation.VERTICAL}), 1, 0, 1, 2);
+        this._grid.attach(songsFrame, 2, 0, 2, 2);
         this._addListRenderers();
         if(Gtk.Settings.get_default().gtk_application_prefer_dark_theme) {
             this.view.get_generic_view().get_style_context().add_class("artist-panel-dark");
diff --git a/src/widgets.js b/src/widgets.js
index 2c86766..422f39d 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -637,34 +637,64 @@ const SongsList = new Lang.Class({
     Name: "SongsList",
     Extends: Gd.MainView,
 
-    _init: function(player){
+    _init: function(player, model){
         this.parent();
         this.set_shadow_type(Gtk.ShadowType.NONE);
         this.player = player;
         this.set_view_type(Gd.MainViewType.LIST);
         this.get_generic_view().get_style_context().add_class("songs-list")
-        this._model = Gtk.ListStore.new([
-            GObject.TYPE_STRING,
-            GObject.TYPE_STRING,
-            GObject.TYPE_STRING,
-            GObject.TYPE_STRING,
-            GdkPixbuf.Pixbuf,
-            GObject.TYPE_OBJECT,
-            GObject.TYPE_BOOLEAN,
-            GObject.TYPE_STRING,
-            GObject.TYPE_BOOLEAN,
-            GObject.TYPE_BOOLEAN
-        ]);
-        this.set_model(this._model);
+        this.model = model;
+        if (this.model == null) {
+            this.model = Gtk.ListStore.new([
+                GObject.TYPE_STRING,
+                GObject.TYPE_STRING,
+                GObject.TYPE_STRING,
+                GObject.TYPE_STRING,
+                GdkPixbuf.Pixbuf,
+                GObject.TYPE_OBJECT,
+                GObject.TYPE_BOOLEAN,
+                GObject.TYPE_STRING,
+                GObject.TYPE_BOOLEAN,
+                GObject.TYPE_BOOLEAN
+            ]);
+        }
+        this.set_model(this.model);
         this._addListRenderers();
         this.show_all();
         this.connect('item-activated', Lang.bind(this, this._onItemActivated));
         this.player.connect('playlist-item-changed', Lang.bind(this, this.updateModel));
     },
 
+    addItem: function(source, param, item) {
+        if (item != null) {
+            this._offset += 1;
+            var iter = this._model.append();
+            if ((item.get_title() == null) && (item.get_url() != null)) {
+                item.set_title (extractFileName(item.get_url()));
+            }
+            try{
+                if (item.get_url())
+                    this.player.discoverer.discover_uri(item.get_url());
+                this._model.set(
+                        iter,
+                        [5, 8, 9, 10],
+                        [item, nowPlayingIconName, false, false]
+                    );
+            } catch(err) {
+                log(err.message);
+                log("failed to discover url " + item.get_url());
+                this._model.set(
+                        iter,
+                        [5, 8, 9, 10],
+                        [item, errorIconName, false, true]
+                    );
+            }
+        }
+    },
+
     update: function(title, playlist) {
         this.playlist = playlist;
-        this._model.clear();
+        this.model.clear();
         this.show_all();
     },
 
@@ -680,9 +710,9 @@ const SongsList = new Lang.Class({
     },
 
     _onItemActivated: function (widget, id, path) {
-        var iter = this._model.get_iter(path)[1]
-        if (this._model.get_value(iter, 7) != errorIconName) {
-            this.player.setPlaylist("Playlist", null, this._model, iter, 5);
+        var iter = this.model.get_iter(path)[1]
+        if (this.model.get_value(iter, 7) != errorIconName) {
+            this.player.setPlaylist("Playlist", null, this.model, iter, 5);
             this.player.setPlaying(true);
         }
     },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]