/***************************************************************************
 *  MMKeysPlugin.cs
 *
 *  Written by Danilo Reinhardt (danilo.reinhardt@gmx.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 System.IO;
using Gtk;
using Gdk;
using Mono.Unix;

using NDesk.DBus;

using Banshee.Base;
using Banshee.Configuration;

public static class PluginModuleEntry
{
	public static Type [] GetTypes()
	{
		return new Type [] {
			typeof(Banshee.Plugins.MMKeys.MMKeysPlugin)
		};
	}
}

namespace Banshee.Plugins.MMKeys
{
	public delegate void MediaPlayerKeyPressedHandler(string application, string key);

	[Interface ("org.gnome.MultimediaKeys")]
	public interface IMultimediaKeys
	{
		void GrabMediaPlayerKeys(string application, uint time);
		void ReleaseMediaPlayerKeys(string application);
		event MediaPlayerKeyPressedHandler MediaPlayerKeyPressed;
	}

	public class MMKeysPlugin : Banshee.Plugins.Plugin
	{
        private const string BusName = "org.gnome.MultimediaKeys";
        private const string ObjectPath = "/org/gnome/MultimediaKeys";

		protected override string ConfigurationName { get { return "mmkeys"; } }
		public override string DisplayName { get { return Catalog.GetString("Multimedia Keys"); } }

		public override string Description {
			get {
				return Catalog.GetString(
						"Adds support for multimedia keys configured through GNOME."
						);
			}
		}

		public override string [] Authors {
			get {
				return new string [] {
					"Danilo Reinhardt",
						"Jan Arne Petersen"
				};
			}
		}

		private IMultimediaKeys multimedia_keys;

		protected override void PluginInitialize()
		{
			multimedia_keys = Bus.Session.GetObject<IMultimediaKeys>(BusName, new ObjectPath(ObjectPath));;

			multimedia_keys.GrabMediaPlayerKeys("Banshee", 0);
			multimedia_keys.MediaPlayerKeyPressed += OnMediaPlayerKeyPressed;

			// InterfaceElements.MainWindow.FocusInEvent += OnFocusInEvent; 
		}

		private void OnMediaPlayerKeyPressed(string application, string key)
		{
			if (application != "Banshee") {
				return;
			}
			switch(key) {
				case "Play":
					Globals.ActionManager["PlayPauseAction"].Activate();
				break;
				case "Next":
					Globals.ActionManager["NextAction"].Activate();
				break;
				case "Previous":
					Globals.ActionManager["PreviousAction"].Activate();
				break;
			}
		}

		private void OnFocusInEvent(object o, FocusInEventArgs args)
		{
			if (args.Event.In) {
				multimedia_keys.GrabMediaPlayerKeys("Banshee", 0);
			}
		}


		protected override void PluginDispose()
		{
			// InterfaceElements.MainWindow.FocusInEvent -= OnFocusInEvent; 
			multimedia_keys.MediaPlayerKeyPressed -= OnMediaPlayerKeyPressed;
			multimedia_keys.ReleaseMediaPlayerKeys("Banshee");
		}

		public override Gtk.Widget GetConfigurationWidget()
		{
			return new MMKeysConfigPage();
		}

		public static readonly SchemaEntry<bool> EnabledSchema = new SchemaEntry<bool>(
				"plugins.mmkeys", "enabled",
				false,
				"Plugin enabled",
				"Multimedia Keys plugin enabled"
				);
	}
}
