NotificationAreaIconPlugin.cs 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/***************************************************************************
 *  NotificationAreaIconPlugin.cs
 *
 *  Copyright (C) 2005-2006 Novell, Inc. (Aaron Bockover)
 *  Copyright (C) 2006 Sebastian Dröge <slomo@ubuntu.com>
 * 
 *  Written by Sebastian Dröge <slomo@ubuntu.com>
 *  
 *  Parts of the file copied from the old TrayIcon code,
 *  written by Aaron Bockover <aaron@aaronbock.net>
 *  
 ****************************************************************************/

/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),  
 *  to deal in the Software without restriction, including without limitation  
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
 *  and/or sell copies of the Software, and to permit persons to whom the  
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in 
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 *  DEALINGS IN THE SOFTWARE.
 */

using System;
using Gtk;
using GLib;
using GConf;
using Banshee.Base;
using Banshee.MediaEngine;
using Banshee.Widgets;
using Mono.Unix;

namespace Banshee.Plugins.NotificationAreaIcon {

    public class NotificationAreaIconPlugin : Banshee.Plugins.Plugin {

        protected override string ConfigurationName { get { return "NotificationAreaIcon"; } }
        public override string DisplayName { get { return Catalog.GetString("Notification Area Icon"); } }

        public override string Description {
            get {
                return Catalog.GetString("Shows the Notification Area Icon");
            }
        }

        public override string[] Authors {
            get {
                return new string[] {
                    "Sebastian Dr\u00f6ge",
                    "Aaron Bockover"
                };
            }
        }

        private EventBox event_box;
        private NotificationArea notif_area;
        private Menu menu;
        private uint ui_manager_id;

        private TrackInfoPopup popup;
        private bool can_show_popup = false;
Aaron Bockover's avatar
Aaron Bockover committed
73
        private bool cursor_over_trayicon = false;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

        private static readonly uint SkipDelta = 10;
        private static readonly int VolumeDelta = 10;
        
        protected override void PluginInitialize() {
            Init();

            ui_manager_id = Globals.ActionManager.UI.AddUiFromResource("NotificationAreaIconMenu.xml");        
            menu = (Menu) Globals.ActionManager.UI.GetWidget("/NotificationAreaIconMenu");
            menu.ShowAll();
        
            popup = new TrackInfoPopup();
            PlayerEngineCore.EventChanged += OnPlayerEngineEventChanged;

            // When we're already playing fill the TrackInfoPopup with the current track
            if (PlayerEngineCore.CurrentState == PlayerEngineState.Playing) {
                FillPopup();
            }
        }

        protected override void PluginDispose() {
            if (notif_area != null) {
                notif_area.Destroy();
                event_box = null;
                notif_area = null;
            }
            PlayerEngineCore.EventChanged -= OnPlayerEngineEventChanged;
            Globals.ActionManager.UI.RemoveUi(ui_manager_id);
        }

        private void Init() {
            notif_area = new NotificationArea(Catalog.GetString("Banshee"));
            notif_area.DestroyEvent += OnDestroyEvent;

            event_box = new EventBox();
            event_box.ButtonPressEvent += OnNotificationAreaIconClick;
            event_box.EnterNotifyEvent += OnEnterNotifyEvent;
            event_box.LeaveNotifyEvent += OnLeaveNotifyEvent;
            event_box.ScrollEvent += OnMouseScroll;
            
            event_box.Add(new Gtk.Image(IconThemeUtils.LoadIcon(22, "music-player-banshee", "tray-icon")));
            
            notif_area.Add(event_box);
            notif_area.ShowAll();
        }

        private void OnDestroyEvent(object o, DestroyEventArgs args) {
            Init();
        }

        private void OnNotificationAreaIconClick(object o, ButtonPressEventArgs args) {
            switch(args.Event.Button) {
                case 1:
                    InterfaceElements.MainWindow.Visible = !InterfaceElements.MainWindow.Visible;
                    ResizeMoveWindow();
                    break;
                case 3:
                    menu.Popup(null, null,
                            new MenuPositionFunc(PositionMenu),
                            args.Event.Button, args.Event.Time);
                    break;
            }
        }

        private void PositionMenu(Menu menu, out int x, out int y, out bool push_in) {
            PositionWidget(menu, out x, out y, 0);
            push_in = true;
        }

        private void PositionWidget(Widget widget, out int x, out int y, int yPadding) {
            int button_y, panel_width, panel_height;
            Gtk.Requisition requisition = widget.SizeRequest();
            
            event_box.GdkWindow.GetOrigin(out x, out button_y);
            (event_box.Toplevel as Gtk.Window).GetSize(out panel_width, out panel_height);

            y = (button_y + panel_height + requisition.Height >= event_box.Screen.Height) 
                ? button_y - requisition.Height - yPadding
                : button_y + panel_height + yPadding;
        }

        private void OnMouseScroll(object o, ScrollEventArgs args) {
            switch(args.Event.Direction) {
                case Gdk.ScrollDirection.Up:
                    if((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
                        PlayerEngineCore.Volume += (ushort)VolumeDelta;
                    } else if((args.Event.State & Gdk.ModifierType.ShiftMask) != 0) {
                        PlayerEngineCore.Position += SkipDelta;
                    } else {
                        Globals.ActionManager["NextAction"].Activate();
                    }
                    break;
                case Gdk.ScrollDirection.Down:
                    if((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
                        if (PlayerEngineCore.Volume < (ushort)VolumeDelta)
                            PlayerEngineCore.Volume = 0;
                        else
                            PlayerEngineCore.Volume -= (ushort)VolumeDelta;
                    } else if((args.Event.State & Gdk.ModifierType.ShiftMask) != 0) {
                        PlayerEngineCore.Position -= SkipDelta;
                    } else {
                        Globals.ActionManager["PreviousAction"].Activate();
                    }
                    break;
            }

        }

        private void HidePopup() {
            popup.Hide();
        }
        
        private void ShowPopup() {
            PositionPopup();
            popup.Show();
        }
        
        private void PositionPopup() {
            int x, y;
            Gtk.Requisition event_box_req = event_box.SizeRequest();
            Gtk.Requisition popup_req = popup.SizeRequest();
            PositionWidget(popup, out x, out y, 5);
            x = x - (popup_req.Width / 2) + (event_box_req.Width / 2);     
            if(x + popup_req.Width >= event_box.Screen.Width) { 
                x = event_box.Screen.Width - popup_req.Width - 5;
            }
            
            popup.Move(x, y);
        }
        
        private void OnEnterNotifyEvent(object o, EnterNotifyEventArgs args) {
Aaron Bockover's avatar
Aaron Bockover committed
205 206 207 208 209 210 211 212 213 214
            cursor_over_trayicon = true;
            if(can_show_popup) {
                // only show the popup when the cursor is still over the
                // tray icon after 500ms
                GLib.Timeout.Add(500, delegate {
                    if ((cursor_over_trayicon) && (can_show_popup)) {
                        ShowPopup();
                    }
                    return false;
                });
215 216 217 218
            }
        }
        
        private void OnLeaveNotifyEvent(object o, LeaveNotifyEventArgs args) {
Aaron Bockover's avatar
Aaron Bockover committed
219
            cursor_over_trayicon = false;
220 221 222 223 224 225
            HidePopup();
        }

        private void OnPlayerEngineEventChanged(object o, PlayerEngineEventArgs args) {
            switch (args.Event) {
                case PlayerEngineEvent.Iterate:
226 227 228 229 230 231 232
                    if(PlayerEngineCore.CurrentTrack != null) {
                        popup.Duration = (uint)PlayerEngineCore.CurrentTrack.Duration.TotalSeconds;
                        popup.Position = PlayerEngineCore.Position;
                    } else {
                        popup.Duration = 0;
                        popup.Position = 0;
                    }
233 234 235 236 237
                    break;
                case PlayerEngineEvent.StartOfStream:
                    FillPopup();
                    break;
                case PlayerEngineEvent.EndOfStream:
Aaron Bockover's avatar
Aaron Bockover committed
238 239 240 241 242 243 244 245 246 247
                    // only hide the popup when we don't play again after 250ms
                    GLib.Timeout.Add(250, delegate {
                        if (PlayerEngineCore.CurrentState != PlayerEngineState.Playing) {
                            popup.Duration = 0;
                            popup.Position = 0;
                            can_show_popup = false;
                            popup.Hide();
                         }
                         return false;
                    });
248 249 250 251 252 253
                    break;
            }
        }

        private void FillPopup() {
            can_show_popup = true;
254 255 256
            popup.Artist = PlayerEngineCore.CurrentTrack.DisplayArtist;
            popup.Album = PlayerEngineCore.CurrentTrack.DisplayAlbum;
            popup.TrackTitle = PlayerEngineCore.CurrentTrack.DisplayTitle;
257
            popup.CoverArtFileName = PlayerEngineCore.CurrentTrack.CoverArtFileName;
Aaron Bockover's avatar
Aaron Bockover committed
258
            popup.QueueDraw();
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            if (!popup.Visible) {
                PositionPopup();
            }
        }



        //FIXME: GO AWAY UGLY COPY!!!
        private void ResizeMoveWindow() {
            Window WindowPlayer = InterfaceElements.MainWindow;
            int x = 0, y = 0, width = 0, height = 0;
            try {
                x = (int)Globals.Configuration.Get(GConfKeys.WindowX);
                y = (int)Globals.Configuration.Get(GConfKeys.WindowY);
                
                width = (int)Globals.Configuration.Get(GConfKeys.WindowWidth);
                height = (int)Globals.Configuration.Get(GConfKeys.WindowHeight);
            } catch(GConf.NoSuchKeyException) {
                width = 800;
                height = 600;
                x = 0;
                y = 0;
            }
            
            if(width != 0 && height != 0) {
                WindowPlayer.Resize(width, height);
            }
            
            if(x == 0 && y == 0) {
                WindowPlayer.SetPosition(Gtk.WindowPosition.Center);
            } else {
                WindowPlayer.Move(x, y);
            }
            
            try {
                if((bool)Globals.Configuration.Get(GConfKeys.WindowMaximized)) {
                    WindowPlayer.Maximize();
                } else {
                    WindowPlayer.Unmaximize();
                }
            } catch(GConf.NoSuchKeyException) {
            }
        }
        
    }
}