Facility for acking/unacking log entries
[gregoa/zavai.git] / src / app_music.vala
1 /*
2  * app_music - zavai music player UI functions
3  *
4  * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace ui {
25 namespace music {
26
27 public class PlayPauseButton : Gtk.Button
28 {
29     protected Gtk.Image img_play;
30     protected Gtk.Image img_pause;
31     protected bool is_play;
32
33     construct
34     {
35         img_play = new Gtk.Image.from_stock(Gtk.STOCK_MEDIA_PLAY, Gtk.IconSize.BUTTON);
36         img_pause = new Gtk.Image.from_stock(Gtk.STOCK_MEDIA_PAUSE, Gtk.IconSize.BUTTON);
37
38         clicked += on_clicked;
39
40         is_play = audio.musicplayer.get_state() == Gst.State.PAUSED;
41
42         audio.musicplayer.state_changed += (state) => {
43             if (is_play != (state == Gst.State.PAUSED))
44             {
45                 is_play = !is_play;
46                 update_image();
47             }
48         };
49
50         update_image();
51     }
52
53     protected void update_image()
54     {
55         if (is_play)
56         {
57             set_image(img_play);
58         } else {
59             set_image(img_pause);
60         }
61     }
62
63     void on_clicked(Gtk.Button b)
64     {
65         if (is_play)
66             audio.musicplayer.pause();
67         else
68             audio.musicplayer.resume();
69     }
70 }
71
72 public class Player : Gtk.VBox
73 {
74     protected Gtk.Label l_status;
75     protected PlayPauseButton b_playpause;
76     protected Gtk.Button b_stop;
77     protected Gtk.Button b_prev;
78     protected Gtk.Button b_next;
79
80     construct
81     {
82         l_status = new Gtk.Label("");
83         //l_date.modify_font(Pango.FontDescription.from_string("Sans 40"));
84         b_playpause = new PlayPauseButton();
85         b_stop = new Gtk.Button.from_stock(Gtk.STOCK_MEDIA_STOP);
86         b_stop.clicked += (b) => { audio.musicplayer.stop(); };
87         b_prev = new Gtk.Button.from_stock(Gtk.STOCK_MEDIA_PREVIOUS);
88         b_next = new Gtk.Button.from_stock(Gtk.STOCK_MEDIA_NEXT);
89         pack_start(l_status, false, false, 0);
90         var buttons = new Gtk.HButtonBox();
91         buttons.add(b_prev);
92         buttons.add(b_playpause);
93         buttons.add(b_stop);
94         buttons.add(b_next);
95         pack_start(buttons, false, false, 0);
96
97         //zavai.clock.clock.minute_changed += on_minute_changed;
98         //zavai.clock.clock.schedule_changed += on_schedule_changed;
99         //on_schedule_changed(zavai.clock.clock.next_alarm());
100     }
101
102 #if 0
103     private void on_date_clicked(Gtk.Button b)
104     {
105         zavai.app.push_applet(zavai.ui.calendar.calendar);
106     }
107
108     private void on_schedule_changed(zavai.clock.Alarm? next)
109     {
110         if (next == null)
111         {
112             last_deadline = 0;
113             last_deadline_label = "";
114         } else {
115             last_deadline = next.ev.deadline;
116             last_deadline_label = next.label;
117         }
118         on_minute_changed((long)last_time, last_time_type);
119     }
120
121     private void on_minute_changed(long ts, zavai.clock.SourceType type)
122     {
123         last_time = (time_t)ts;
124         last_time_type = type;
125
126         string typetag = "unknown";
127         switch (type)
128         {
129             case zavai.clock.SourceType.GPS:
130                 typetag = "gps";
131                 break;
132             case zavai.clock.SourceType.SYSTEM:
133                 typetag = "sys";
134                 break;
135         }
136
137         var t = Time.local((time_t)ts);
138         l_date.set_text(t.format("%a %d %b"));
139         l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, typetag));
140
141         if (last_deadline == 0)
142             l_deadline.set_text("");
143         else
144         {
145             int remaining = (int)(last_deadline - last_time);
146             int hours = remaining / 3600;
147             int minutes = (remaining % 3600) / 60;
148             if (hours == 0 && minutes == 0)
149                 l_deadline.set_text(last_deadline_label + "\nanytime now");
150             else
151                 l_deadline.set_text("%s\nin %02dh %02dm".printf(last_deadline_label, hours, minutes));
152         }
153     }
154 #endif
155 }
156
157 public Player player = null;
158
159 public void init()
160 {
161     player = new Player();
162     // Menus
163     //zavai.menu_gsm.add_service_toggle(zavai.gsm.gsm, "Start GSM", "Stop GSM");
164     //zavai.menu_gsm.add_service_toggle(zavai.gsm.gprs, "Start GPRS", "Stop GPRS");
165 }
166
167 }
168 }
169 }