~oem-solutions-group/unity-2d/indicator-sound

« back to all changes in this revision

Viewing changes to src/player-controller.vala

  • Committer: Ken VanDine
  • Date: 2010-09-14 19:13:49 UTC
  • mfrom: (28.2.108 indicator-sound)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: ken.vandine@canonical.com-20100914191349-pp888e13xbemy0kc
Tags: upstream-0.4.5
ImportĀ upstreamĀ versionĀ 0.4.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
This service primarily controls PulseAudio and is driven by the sound indicator menu on the panel.
3
 
Copyright 2010 Canonical Ltd.
4
 
 
5
 
Authors:
6
 
    Conor Curran <conor.curran@canonical.com>
7
 
 
8
 
This program is free software: you can redistribute it and/or modify it 
9
 
under the terms of the GNU General Public License version 3, as published 
10
 
by the Free Software Foundation.
11
 
 
12
 
This program is distributed in the hope that it will be useful, but 
13
 
WITHOUT ANY WARRANTY; without even the implied warranties of 
14
 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
15
 
PURPOSE.  See the GNU General Public License for more details.
16
 
 
17
 
You should have received a copy of the GNU General Public License along 
18
 
with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
*/
20
 
 
21
 
using Dbusmenu;
22
 
using Gee;
23
 
 
24
 
public class PlayerController : GLib.Object
25
 
{
26
 
        public const int WIDGET_QUANTITY = 4;
27
 
 
28
 
        public static enum widget_order{
29
 
                SEPARATOR,
30
 
                TITLE,
31
 
                METADATA,
32
 
                TRANSPORT,
33
 
        }
34
 
 
35
 
        public enum state{
36
 
                OFFLINE,
37
 
                INSTANTIATING,
38
 
                READY,
39
 
                CONNECTED,
40
 
                DISCONNECTED
41
 
        }
42
 
        
43
 
        public int current_state = state.OFFLINE;
44
 
                
45
 
        private Dbusmenu.Menuitem root_menu;
46
 
        public string name { get; set;} 
47
 
        public ArrayList<PlayerItem> custom_items;      
48
 
        public Mpris2Controller mpris_bridge;
49
 
        public AppInfo? app_info { get; set;}
50
 
        public int menu_offset { get; set;}
51
 
                
52
 
        public PlayerController(Dbusmenu.Menuitem root,
53
 
                                string client_name,
54
 
                                int offset,
55
 
                                state initial_state)
56
 
        {
57
 
                this.root_menu = root;
58
 
                this.name = format_client_name(client_name.strip());
59
 
                this.custom_items = new ArrayList<PlayerItem>();
60
 
                this.current_state = initial_state;
61
 
                this.menu_offset = offset;
62
 
                construct_widgets();
63
 
                establish_mpris_connection();
64
 
                this.update_layout();
65
 
        }
66
 
 
67
 
        public void update_state(state new_state)
68
 
        {
69
 
                debug("update_state - player controller %s : new state %i", this.name, new_state);
70
 
                this.current_state = new_state;
71
 
                this.update_layout();
72
 
        }
73
 
        
74
 
        public void activate()
75
 
        {
76
 
                this.establish_mpris_connection();      
77
 
        }
78
 
 
79
 
        /*
80
 
         instantiate()
81
 
         The user should be able to start the app from the transport bar when in an offline state
82
 
         There is a need to wait before the application is on DBus before attempting to access its mpris address
83
 
         Hence only when the it has registered with us via libindicate do we attempt to kick off mpris communication
84
 
         */
85
 
        public void instantiate()
86
 
        {
87
 
                debug("instantiate in player controller for %s", this.name);
88
 
                try{
89
 
                this.app_info.launch(null, null);
90
 
                        this.update_state(state.INSTANTIATING);
91
 
                }
92
 
                catch(GLib.Error error){
93
 
                        warning("Failed to launch app %s with error message: %s", this.name, error.message);
94
 
                }
95
 
        }
96
 
        
97
 
        private void establish_mpris_connection()
98
 
        {               
99
 
                if(this.current_state != state.READY){
100
 
                        debug("establish_mpris_connection - Not ready to connect");
101
 
                        return;
102
 
                }               
103
 
                this.mpris_bridge = new Mpris2Controller(this); 
104
 
                this.determine_state();
105
 
        }
106
 
        
107
 
        public void vanish()
108
 
        {
109
 
                foreach(Dbusmenu.Menuitem item in this.custom_items){
110
 
                        root_menu.child_delete(item);                   
111
 
                }
112
 
        }
113
 
 
114
 
        public void hibernate()
115
 
        {
116
 
                update_state(PlayerController.state.OFFLINE);
117
 
                this.custom_items[widget_order.TRANSPORT].reset(TransportMenuitem.attributes_format());
118
 
                this.custom_items[widget_order.METADATA].reset(MetadataMenuitem.attributes_format());
119
 
                TitleMenuitem title = this.custom_items[widget_order.TITLE] as TitleMenuitem;
120
 
                title.toggle_active_triangle(false);                                    
121
 
        }
122
 
 
123
 
        public void update_layout()
124
 
        {                       
125
 
                if(this.current_state != state.CONNECTED){
126
 
                        this.custom_items[widget_order.TRANSPORT].property_set_bool(MENUITEM_PROP_VISIBLE,
127
 
                                                                                    false);
128
 
                        this.custom_items[widget_order.METADATA].property_set_bool(MENUITEM_PROP_VISIBLE,
129
 
                                                                                   false);
130
 
                        return; 
131
 
                }
132
 
                this.custom_items[widget_order.METADATA].property_set_bool(MENUITEM_PROP_VISIBLE,
133
 
                                                                                this.custom_items[widget_order.METADATA].populated(MetadataMenuitem.attributes_format()));              
134
 
                this.custom_items[widget_order.TRANSPORT].property_set_bool(MENUITEM_PROP_VISIBLE,
135
 
                                                                            true);
136
 
        }
137
 
                
138
 
        private void construct_widgets()
139
 
        {
140
 
                // Separator item
141
 
                this.custom_items.add(new PlayerItem(CLIENT_TYPES_SEPARATOR));
142
 
 
143
 
                // Title item
144
 
                TitleMenuitem title_menu_item = new TitleMenuitem(this);
145
 
                this.custom_items.add(title_menu_item);
146
 
 
147
 
                // Metadata item
148
 
                MetadataMenuitem metadata_item = new MetadataMenuitem();
149
 
                this.custom_items.add(metadata_item);
150
 
 
151
 
                // Transport item
152
 
                TransportMenuitem transport_item = new TransportMenuitem(this);
153
 
                this.custom_items.add(transport_item);
154
 
                                
155
 
                foreach(PlayerItem item in this.custom_items){
156
 
                        root_menu.child_add_position(item, this.menu_offset + this.custom_items.index_of(item));                        
157
 
                }
158
 
        }       
159
 
        
160
 
        private static string format_client_name(string client_name)
161
 
        {
162
 
                string formatted = client_name;
163
 
                if(formatted.len() > 1){
164
 
                        formatted = client_name.up(1).concat(client_name.slice(1, client_name.len()));
165
 
                        debug("PlayerController->format_client_name - : %s", formatted);
166
 
                }               
167
 
                return formatted;
168
 
        }
169
 
 
170
 
        // Temporarily we will need to handle to different mpris implemenations
171
 
        // Do it for now - a couple of weeks should see this messy carry on out of
172
 
        // the codebase.
173
 
        public void determine_state()
174
 
        {
175
 
                if(this.mpris_bridge.connected() == true){
176
 
                        this.update_state(state.CONNECTED);
177
 
                        TitleMenuitem title = this.custom_items[widget_order.TITLE] as TitleMenuitem;
178
 
                        title.toggle_active_triangle(true);                     
179
 
                        TransportMenuitem transport = this.custom_items[widget_order.TRANSPORT] as TransportMenuitem;
180
 
                        transport.change_play_state(TransportMenuitem.state.PAUSED);
181
 
                }
182
 
                else{
183
 
                        this.update_state(state.DISCONNECTED);
184
 
                }
185
 
        }
186
 
}
 
 
b'\\ No newline at end of file'