~ubuntu-branches/ubuntu/trusty/indicator-sound-gtk2/trusty-proposed

« back to all changes in this revision

Viewing changes to src/playlists-menu-item.vala

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2012-09-10 00:09:01 UTC
  • Revision ID: package-import@ubuntu.com-20120910000901-q6469svv5d3pmn0y
Tags: upstream-12.10.0.1
Import upstream version 12.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2010 Canonical Ltd.
 
3
 
 
4
Authors:
 
5
    Conor Curran <conor.curran@canonical.com>
 
6
 
 
7
This program is free software: you can redistribute it and/or modify it 
 
8
under the terms of the GNU General Public License version 3, as published 
 
9
by the Free Software Foundation.
 
10
 
 
11
This program is distributed in the hope that it will be useful, but 
 
12
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
13
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
14
PURPOSE.  See the GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License along 
 
17
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
using Config;
 
21
using Dbusmenu;
 
22
using DbusmenuPlaylists;
 
23
using DbusmenuPlaylist;
 
24
using Gee;
 
25
 
 
26
 
 
27
public class PlaylistsMenuitem : PlayerItem
 
28
{
 
29
  private HashMap<int, Dbusmenu.Menuitem> current_playlists;    
 
30
  public Menuitem root_item;
 
31
  
 
32
  public PlaylistsMenuitem ( PlayerController parent )
 
33
  {
 
34
    Object ( item_type: MENUITEM_TYPE, owner: parent );
 
35
  }
 
36
 
 
37
  construct{
 
38
    this.current_playlists = new HashMap<int, Dbusmenu.Menuitem>();
 
39
    this.root_item = new Menuitem();
 
40
    this.root_item.property_set ( MENUITEM_PROP_LABEL, _("Choose Playlist") );
 
41
    this.root_item.property_set ( MENUITEM_PATH, "" );
 
42
  }
 
43
 
 
44
  public new void update (PlaylistDetails[] playlists)
 
45
  {
 
46
    foreach ( PlaylistDetails detail in playlists ){
 
47
      // We don't want to list playlists which are for videos)'
 
48
      if (this.already_observed(detail) || this.is_video_related(detail))
 
49
        continue;
 
50
      
 
51
      Dbusmenu.Menuitem menuitem = new Menuitem();
 
52
      menuitem.property_set (MENUITEM_PROP_LABEL,
 
53
                             truncate_item_label_if_needs_be (detail.name));
 
54
      menuitem.property_set (MENUITEM_PROP_ICON_NAME, "playlist-symbolic");
 
55
 
 
56
      menuitem.property_set (MENUITEM_PATH, (string)detail.path);
 
57
      menuitem.property_set_bool (MENUITEM_PROP_VISIBLE, true);
 
58
      menuitem.property_set_bool (MENUITEM_PROP_ENABLED, true);
 
59
      
 
60
      menuitem.item_activated.connect(() => {
 
61
        submenu_item_activated (menuitem.id );
 
62
        }
 
63
      );
 
64
      this.current_playlists.set( menuitem.id, menuitem ); 
 
65
      this.root_item.child_append( menuitem );
 
66
      debug ("populating valid playlists %s", detail.name);
 
67
    }
 
68
    // Finally remove any that might have been deleted
 
69
    foreach (Dbusmenu.Menuitem item in this.current_playlists.values) {
 
70
      bool within = false;
 
71
      foreach (PlaylistDetails detail in playlists){
 
72
        if (detail.path == item.property_get (MENUITEM_PATH)) {
 
73
          within = true;
 
74
          break;
 
75
        }
 
76
      }
 
77
      if (within == false){
 
78
        if (this.root_item.property_get (MENUITEM_PATH) == item.property_get (MENUITEM_PATH)){
 
79
          this.root_item.property_set (MENUITEM_PROP_LABEL, _("Choose Playlist"));        
 
80
        }
 
81
        this.root_item.child_delete (item);
 
82
      }
 
83
    }
 
84
  }
 
85
 
 
86
  public void update_individual_playlist (PlaylistDetails new_detail)
 
87
  {
 
88
    foreach ( Dbusmenu.Menuitem item in this.current_playlists.values ){
 
89
      if (new_detail.path == item.property_get (MENUITEM_PATH)){
 
90
        item.property_set (MENUITEM_PROP_LABEL, 
 
91
                           truncate_item_label_if_needs_be (new_detail.name));
 
92
      }
 
93
    }
 
94
    // If its active make sure the name is updated on the root item.
 
95
    if (this.root_item.property_get (MENUITEM_PATH) == new_detail.path) {
 
96
      this.root_item.property_set (MENUITEM_PROP_LABEL,
 
97
                                   truncate_item_label_if_needs_be (new_detail.name));          
 
98
    }
 
99
  }
 
100
                                                  
 
101
  private bool already_observed (PlaylistDetails new_detail)
 
102
  {
 
103
    foreach ( Dbusmenu.Menuitem item in this.current_playlists.values ){
 
104
      var path = item.property_get (MENUITEM_PATH);
 
105
      if (new_detail.path == path) return true;
 
106
    }
 
107
    return false;
 
108
  }
 
109
 
 
110
  private bool is_video_related (PlaylistDetails new_detail)
 
111
  {
 
112
    var location = (string)new_detail.path;
 
113
    if (location.contains ("/VideoLibrarySource/")) return true;
 
114
    return false;
 
115
  }
 
116
 
 
117
  public void active_playlist_update (PlaylistDetails detail)
 
118
  {
 
119
    var update = detail.name; 
 
120
    if ( update == "" ) update = _("Choose Playlist");
 
121
    this.root_item.property_set (MENUITEM_PROP_LABEL,
 
122
                                 truncate_item_label_if_needs_be(update));  
 
123
    this.root_item.property_set (MENUITEM_PATH, detail.path);  
 
124
  }
 
125
  
 
126
  private void submenu_item_activated (int menu_item_id)
 
127
  {
 
128
    if (!this.current_playlists.has_key(menu_item_id)) {
 
129
      warning( "item %i was activated but we don't have a corresponding playlist",
 
130
               menu_item_id );
 
131
      return;
 
132
    }
 
133
    this.owner.mpris_bridge.activate_playlist ( (GLib.ObjectPath)this.current_playlists[menu_item_id].property_get (MENUITEM_PATH) );
 
134
  }
 
135
  
 
136
  private string truncate_item_label_if_needs_be(string item_label)
 
137
  {
 
138
    var result = item_label;
 
139
    if (item_label.char_count(-1) > 17){
 
140
      result = item_label.slice ((long)0, (long)15);
 
141
      result += "…";
 
142
    }
 
143
    return result;
 
144
  }
 
145
  
 
146
  public static HashSet<string> attributes_format()
 
147
  {
 
148
    HashSet<string> attrs = new HashSet<string>();    
 
149
    attrs.add(MENUITEM_TITLE);
 
150
    attrs.add(MENUITEM_PLAYLISTS);
 
151
    return attrs;
 
152
  } 
 
153
  
 
154
}