/*
 * history.h
 *
 * Copyright (c) 2002, 2003 Frerich Raabe <raabe@kde.org>
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#ifndef HISTORY_H
#define HISTORY_H

#include <qobject.h>
#include <qptrlist.h>

class KActionCollection;
class KMainWindow;
class KToolBarPopupAction;

class QPopupMenu;

namespace Barry
{
	class History;

	class Command
	{
		public:
			enum CreationFlags {
				None = 0x0, Run = 0x1, Register = 0x2
			};
			virtual ~Command();

			template <class T>
			static T *create( CreationFlags flags = None )
			{
				T *cmd = new T;
				if ( flags & Register )
					History::self().registerCommand( cmd );
				if ( flags & Run )
					cmd->exec();
				return cmd;
			}

			void unexec();
			void exec();
			virtual QString text() const = 0;

		protected:
			Command();
			bool executed() const { return m_executed; }

		private:
			Command( const Command &rhs );
			Command &operator=( const Command &rhs );

			virtual void doExec() = 0;
			virtual void doUnexec() = 0;

			bool m_executed;
	};

	class History : public QObject
	{
		Q_OBJECT
		friend class Command;
		public:
			static History &self();

			void setupActions( KActionCollection *coll );
			void installMenuBarHook( KMainWindow *mainWindow );
			void reparseConfig();

		private slots:
			void back();
			void forward();
			void fillBackMenu();
			void fillForwardMenu();
			void fillGoMenu();
			void commandActivated( int id );

		private:
			enum Action {
				Back, Forward
			};

			History();
			History( const History &rhs );
			History &operator=( const History &rhs );

			void registerCommand( Command *cmd );
			void truncateHistory();
			bool canBack() const;
			bool canForward() const;

			static History *m_instance;
			QPtrList<Command> m_commands;
			unsigned int m_idx;
			unsigned int m_maxEntries;
			KToolBarPopupAction *m_backAction;
			KToolBarPopupAction *m_forwardAction;
	};
};

#endif // COMMAND_H
// vim:ts=4:sw=4:noet:list
