#ifndef GLOBALHOTKEYS_H
	#define GLOBALHOTKEYS_H


#include <qguardedptr.h>
#include <qmap.h>
#include <qstring.h>
#include <qthread.h>

#include "configuration_aware_object.h"
#include "configuration_window_widgets.h"
#include "main_configuration_window.h"

#include "globalhotkeys.h"

#include <X11/Xlib.h>
#include <X11/keysym.h>


#define  GLOBALHOTKEYS_CONFIGURATIONCLEARKEY          XK_BackSpace
#define  GLOBALHOTKEYS_X11SHIFTMASK                      ShiftMask
#define  GLOBALHOTKEYS_X11CONTROLMASK                  ControlMask
#define  GLOBALHOTKEYS_X11ALTMASK                         Mod1Mask
#define  GLOBALHOTKEYS_X11ALTGRMASK                       Mod5Mask
#define  GLOBALHOTKEYS_X11SUPERMASK                       Mod4Mask
#define  GLOBALHOTKEYS_X11CAPSLOCKMASK                    LockMask
#define  GLOBALHOTKEYS_X11NUMLOCKMASK                     Mod2Mask
#define  GLOBALHOTKEYS_X11LSHIFT                        XK_Shift_L
#define  GLOBALHOTKEYS_X11RSHIFT                        XK_Shift_R
#define  GLOBALHOTKEYS_X11LCONTROL                    XK_Control_L
#define  GLOBALHOTKEYS_X11RCONTROL                    XK_Control_R
#define  GLOBALHOTKEYS_X11LALT                            XK_Alt_L
#define  GLOBALHOTKEYS_X11RALT                            XK_Alt_R
#define  GLOBALHOTKEYS_X11ALTGR                XK_ISO_Level3_Shift
#define  GLOBALHOTKEYS_X11LSUPER                        XK_Super_L
#define  GLOBALHOTKEYS_X11RSUPER                        XK_Super_R


static int messageshandled = 0;
void EmptyMsgHandler( QtMsgType type, const char *msg );


class Hotkey
{
	public:
		Hotkey( bool _shift, bool _control, bool _alt, bool _altgr, bool _super, int _keycode, QString _comment = "" );
		Hotkey( QString hotkeystring );
		bool equals( Hotkey *h );
		bool shift;
		bool control;
		bool alt;
		bool altgr;
		bool super;
		int keycode;
		QString comment;
};


class HotkeyThread : public QThread
{
	public:
		void setDisplay( Display *_display );
		void setHotkeys( QMap<QString,Hotkey*> _hotkeys );
		void run();
	private:
		Display *display;
		QMap<QString,Hotkey*> hotkeys;
		QGuardedPtr<OpenChatWith> openchatwithwindow;
};


class HotkeyEdit : public ConfigLineEdit
{
	public:
		HotkeyEdit( const QString &section, const QString &item, const QString &widgetCaption, const QString &toolTip, ConfigGroupBox *parentConfigGroupBox, char *name=0 )
			: ConfigLineEdit( widgetCaption, toolTip, section, item, parentConfigGroupBox, name ) {}
///			: ConfigLineEdit( section, item, widgetCaption, toolTip, parentConfigGroupBox, name ) {}
/// TODO: parameters order will change after patching bug http://www.kadu.net/forum/viewtopic.php?p=82864#82864 - in 0.6.0.1 (?)
		HotkeyEdit( ConfigGroupBox *parentConfigGroupBox, char *name=0 )
			: ConfigLineEdit( parentConfigGroupBox, name ) {}
	protected:
		virtual bool x11Event( XEvent *event );
		virtual void focusInEvent( QFocusEvent *event );
		virtual void focusOutEvent( QFocusEvent *event );
	private:
		QString lastvalidvalue;
};


class GlobalHotkeys : public ConfigurationUiHandler, ConfigurationAwareObject
{
	Q_OBJECT
	public:
		GlobalHotkeys();
		~GlobalHotkeys();
		void mainConfigurationWindowCreated( MainConfigurationWindow *mainConfigurationWindow );
	protected:
		void configurationUpdated();
	private:
		void createDefaultConfiguration();
		void grabHotkeys( QMap<QString,Hotkey*> hotkeys );
		HotkeyThread hotkeyThread;
		Display *display;
		QString showKadusMainWindow;           // configuration value
		QString hideKadusMainWindow;           // configuration value
		QString showHideKadusMainWindow;       // configuration value
		QString openIncomingChatWindow;        // configuration value
		QString openAllIncomingChatWindows;    // configuration value
		QString minimizeOpenedChatWindows;     // configuration value
		QString restoreMinimizedChatWindows;   // configuration value
		QString closeAllChatWindows;           // configuration value
		QString openChatWith;                  // configuration value
};


extern GlobalHotkeys *globalHotkeys;


#endif
