/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "veneer/standardaction.h"
#include "veneer/actioncollection.h"
#include "veneer/iconloader.h"

#include <kicon.h>
#include <kaction.h>

#include <QKeySequence>
#include <QDebug>
#include <QCoreApplication>
#include <QApplication>

enum WorkoutStandardActionId
{
	idOpen,
	idOpenNew,
	idSave,
	idClose,
	idQuit,
	idSelectAll,
	idPreferences,
	idShowStatusbar,
	idAboutApp,

	idPrint,
	idUndo,
	idRedo,
	idCut,
	idCopy,
	idPaste,
	idFind,
	idFindNext,
	idFindPrev,
	idShowMenubar
};

struct WorkoutStandardActionInfo
{
	WorkoutStandardActionId id;
	QKeySequence accel;
	const char* name;
	QString label;
	QString tooltip;
	KIcon icon;
	bool checkable;
};

static KAction* makeWorkoutAction(WorkoutStandardActionId id, const QObject* recv, const char* slot, QObject* parent)
{
	static const WorkoutStandardActionInfo actionInfo[] =
	{
		{ idOpen, QKeySequence::Open, "file_open", QObject::tr("&Open..."), QObject::tr("Open profile"), BarIcon("document-open"), false },
		{ idOpenNew, QKeySequence::New, "file_new", QObject::tr("&New"), QObject::tr("New profile"), BarIcon("document-new"), false },
		{ idSave, QKeySequence::Save, "file_save", QObject::tr("&Save"), QObject::tr("Save profile"), BarIcon("document-save"), false },
		{ idClose, QKeySequence::Close, "file_close", QObject::tr("&Close"), QObject::tr("Close profile"), BarIcon("window-close"), false },
		{ idQuit, QKeySequence::Quit, "file_quit", QObject::tr("&Quit"), QObject::tr("Quit the application"), BarIcon("application-exit"), false },
		{ idSelectAll, QKeySequence::SelectAll, "edit_select_all", QObject::tr("Select &All"), QObject::tr("Select all entries"), BarIcon("edit-select-all"), false },
		{ idPreferences, QKeySequence::Preferences, "options_configure", QObject::tr("&Configure Workout ..."), QObject::tr("Modify the application's settings"), BarIcon("configure"), false },
		{ idShowStatusbar, QKeySequence(), "options_show_statusbar", QObject::tr("Show St&atusbar"), QObject::tr("Show the status bar"), KIcon(), true },
		{ idAboutApp, QKeySequence(), "help_about_app", QObject::tr("&About %1").arg(QCoreApplication::applicationName()), QString(), KIcon(), false },

// 		{ idPrint, QKeySequence::Print, "file_print", QObject::tr("&Print"), QObject::tr("&Print..."), "document-print", false },
// 		{ idUndo, QKeySequence::Undo, "edit_undo", QObject::tr("&Undo"), QObject::tr("Undo operation"), "edit-undo", false },
// 		{ idRedo, QKeySequence::Redo, "edit_redo", QObject::tr("Re&do"), QObject::tr("Redo operation"), "edit-redo", false },
// 		{ idCut, QKeySequence::Cut, "edit_cut", QObject::tr("Cu&t"), QObject::tr("Cut selection"), "edit-cut", false },
// 		{ idCopy, QKeySequence::Copy, "edit_copy", QObject::tr("&Copy"), QObject::tr("Copy selection to clipboard"), "edit-copy", false },
// 		{ idPaste, QKeySequence::Paste, "edit_paste", QObject::tr("&Paste"), QObject::tr("Paste clipboard contents"), "edit-paste", false },
// 		{ idFind, QKeySequence::Find, "edit_find", QObject::tr("&Find..."), QObject::tr("Search entries"), "edit-content", false },
// 		{ idFindNext, QKeySequence::FindNext, "edit_find_next", QObject::tr("Find &Next"), QObject::tr("Find next match"), "go-down-search", false },
// 		{ idFindPrev, QKeySequence::FindPrevious, "edit_find_prev", QObject::tr("Find Pre&vious"), QObject::tr("Find previous match"), "go-up-search", false },
// 		{ idShowMenubar, QKeySequence(Qt::CTRL + Qt::Key_M), "options_show_menubar", QObject::tr("Show &Menubar"), QObject::tr("Toggle the menu bar visibility"), "show-menu", true },
	};

	KAction* rval = NULL;
	const WorkoutStandardActionInfo* info = NULL;

	for (quint32 i = 0; i < sizeof(actionInfo) / sizeof(actionInfo[0]); i++)
		if (actionInfo[i].id == id)
		{
			info = &actionInfo[i];
			break;
		}

	if (info != NULL)
	{
		rval = new KAction(info->icon, info->label, parent);
		rval->setObjectName(info->name);
		rval->setToolTip(info->tooltip);
		rval->setStatusTip(info->tooltip);
		rval->setCheckable(info->checkable);

		if (!info->accel.isEmpty())
			rval->setShortcut(info->accel);

		if (recv != NULL && slot != NULL)
			QObject::connect(rval, SIGNAL(triggered()), recv, slot);

		Veneer::ActionCollection* collection = qobject_cast<Veneer::ActionCollection *>(parent);
		if (rval && collection)
			collection->addAction(rval->objectName(), rval);
	}
	else
		qWarning() << "could not find standard action with id " << id;

	return rval;
}

namespace Veneer
{

	namespace StandardAction
	{

		KAction* open(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idOpen, recvr, slot, parent);
		}

		KAction* openNew(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idOpenNew, recvr, slot, parent);
		}

		KAction* save(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idSave, recvr, slot, parent);
		}

		KAction* close(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idClose, recvr, slot, parent);
		}

		KAction* quit(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idQuit, recvr, slot, parent);
		}

		KAction* selectAll(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idSelectAll, recvr, slot, parent);
		}

		KAction* preferences(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idPreferences, recvr, slot, parent);
		}

		KAction* showStatusbar(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idShowStatusbar, recvr, slot, parent);
		}

		KAction* aboutApp(const QObject* recvr, const char* slot, QObject* parent)
		{
			KAction* rval = makeWorkoutAction(idAboutApp, recvr, slot, parent);
			rval->setIcon(QApplication::windowIcon());
			return rval;
		}



/*
		KAction* print(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idPrint, recvr, slot, parent);
		}

		KAction* undo(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idUndo,recvr, slot, parent);
		}

		KAction* redo(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idRedo, recvr, slot, parent);
		}

		KAction* cut(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idCut, recvr, slot, parent);
		}

		KAction* copy(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idCopy, recvr, slot, parent);
		}

		KAction* paste(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idPaste, recvr, slot, parent);
		}

		KAction* find(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idFind, recvr, slot, parent);
		}

		KAction* findNext(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idFindNext, recvr, slot, parent);
		}

		KAction* findPrev(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idFindPrev, recvr, slot, parent);
		}

		KAction* showMenubar(const QObject* recvr, const char* slot, QObject* parent)
		{
			return makeWorkoutAction(idShowMenubar, recvr, slot, parent);
		}
*/

	}

}
