/*
** 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _BROWSEVIEWHANDLERS_H_
#define _BROWSEVIEWHANDLERS_H_

#include "CvsCommands.h"
#include "CPStr.h"
//! Command selection type enum
typedef enum
{
	kSelNone,	/*!< Not specified */
	kSelFiles,	/*!< Selection contains files */
	kSelDir		/*!< Selection contains directory */
} kSelType;

// Forward declaration
class MultiFiles;

/*!
	\class KiSelectionHandler
	\brief Interface describing a handler to process a selection-related command

   KiSelectionHandler is an abstract class designed to aid processing of a selection-related commands.
   Create command handlers by deriving from KiSelectionHandler and overriding virtual methods 
   KiSelectionHandler::OnFolder and KiSelectionHandler::OnFiles.
   
   The default value of KiSelectionHandler::m_needSelection can be changed if the command does not require selection
   to work properly (e.g checkout, import or rtag commands). However it's important to supply the selection whenever
   available. 

   The goal of passing the selection is to enable support for additional data retrieval and present the selection 
   information for the user as needed. For example to show the files selected for commit command and enable
   to perform review of changes using diff command. There are also commands that use the selection information to 
   retrieve the repository data such as modules or tags.
*/
class KiSelectionHandler
{
private:
	CStr m_strCommand;	/*!< Command name, used when browsing for selection */

	MultiFiles* m_pmf;	/*!< Command selection (files or directory) */
	kSelType m_selType;	/*!< Command selection type */

	bool m_autoDelete;	/*!< Indicates whether to delete the command selection allocated memory */

protected:
	bool m_needSelection;	/*!< Indicates whether the selection is needed for command */

public:
	KiSelectionHandler(const char* strCommand, bool needSelection = true);
	virtual ~KiSelectionHandler();

	kSelType GetSelType() const;
	bool GetNeedSelection() const;
	
	virtual void OnFolder(const char* fullpath) = 0;
	virtual void OnFiles(MultiFiles* pmf) = 0;
	virtual MultiFiles* GetSelection();
};

/// Handles moving files to trash
class KoMoveToTrashHandler : public KiSelectionHandler
{
public:
	KoMoveToTrashHandler()
		: KiSelectionHandler("Move To Trash")
	{}

	virtual void OnFolder(const char* fullpath)
	{ /* ignore */ }

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdMoveToTrash(*this);
	}
};

/// Handles figuring out if moving files to trash is enabled
class KoUpdateMoveToTrashHandler : public KiSelectionHandler
{
// Attributes
	bool m_isFolders;
	bool m_isFiles;

public:
	KoUpdateMoveToTrashHandler()
		: KiSelectionHandler("Update Move To Trash"), m_isFolders(false), m_isFiles(false)
	{}

	virtual void OnFolder(const char* fullpath)
	{ 
		m_isFolders = true;
	}

	virtual void OnFiles(MultiFiles* /*pmf*/)
	{
		m_isFiles = true;
	}

	bool IsEnabled() const
	{
		return !m_isFolders && m_isFiles;
	}
};

/// Handles create tag request on the selected items
class KoCreateTagHandler : public KiSelectionHandler
{
public:
	KoCreateTagHandler()
		: KiSelectionHandler("Create Tag")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdTagCreate(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdTagCreate(*this);
	}
};

/// Handles delete tag request on the selected items
class KoDeleteTagHandler : public KiSelectionHandler
{
public:
	KoDeleteTagHandler()
		: KiSelectionHandler("Delete Tag")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdTagDelete(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdTagDelete(*this);
	}
};

/// Handles branch request on the selected items
class KoBranchTagHandler : public KiSelectionHandler
{
public:
	KoBranchTagHandler()
		: KiSelectionHandler("Branch")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdTagBranch(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdTagBranch(*this);
	}
};

/// Handles edit start request on the selected items
class KoEditHandler : public KiSelectionHandler
{
public:
	KoEditHandler()
		: KiSelectionHandler("Edit")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdEdit(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdEdit(*this);
	}
};

/// Handles reserved edit start request on the selected items
class KoReservedEditHandler : public KiSelectionHandler
{
public:
	KoReservedEditHandler()
		: KiSelectionHandler("Reserved Edit")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdEdit(*this, kEditReserved);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdEdit(*this, kEditReserved);
	}
};

/// Handles force edit start request on the selected items
class KoForceEditHandler : public KiSelectionHandler
{
public:
	KoForceEditHandler()
		: KiSelectionHandler("Force Edit")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdEdit(*this, kEditForce);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdEdit(*this, kEditForce);
	}
};

/// Handles edit stop request on the selected items
class KoUneditHandler : public KiSelectionHandler
{
public:
	KoUneditHandler()
		: KiSelectionHandler("Unedit")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdUnedit(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdUnedit(*this);
	}
};

/// Handles releasing on the selected items
class KoReleaseHandler : public KiSelectionHandler
{
public:
	KoReleaseHandler()
		: KiSelectionHandler("Release")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdRelease(*this);
	}

	virtual void OnFiles(MultiFiles* /*pmf*/)
	{	}
};

/// Handles watchers on the selected items
class KoWatchersHandler : public KiSelectionHandler
{
public:
	KoWatchersHandler()
		: KiSelectionHandler("Watchers")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdWatchers(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdWatchers(*this);
	}
};

/// Handles editors on the selected items
class KoEditorsHandler : public KiSelectionHandler
{
public:
	KoEditorsHandler()
		: KiSelectionHandler("Editors")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdEditors(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdEditors(*this);
	}
};

/// Handles watch on on the selected items
class KoWatchOnHandler : public KiSelectionHandler
{
public:
	KoWatchOnHandler()
		: KiSelectionHandler("Watch On")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdWatchOn(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdWatchOn(*this);
	}
};

/// Handles watch off on the selected items
class KoWatchOffHandler : public KiSelectionHandler
{
public:
	KoWatchOffHandler()
		: KiSelectionHandler("Watch Off")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdWatchOff(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdWatchOff(*this);
	}
};

/// Handles diffing on the selected items
class KoDiffHandler : public KiSelectionHandler
{
public:
	KoDiffHandler()
		: KiSelectionHandler("Diff")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdDiff(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdDiff(*this);
	}
};

/// Handles log queries on the selected items
class KoLogHandler : public KiSelectionHandler
{
public:
	KoLogHandler()
		: KiSelectionHandler("Log")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdLog(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdLog(*this);
	}
};

/// Handles graph log queries on the selected items
class KoGraphHandler : public KiSelectionHandler
{
public:
	KoGraphHandler()
		: KiSelectionHandler("Graph")
	{}

	virtual void OnFolder(const char* /*fullpath*/)
	{
		/*we avoid processing directory*/
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdLog(*this, true);
	}
};

/// Handles status queries on the selected items
class KoStatusHandler : public KiSelectionHandler
{
public:
	KoStatusHandler()
		: KiSelectionHandler("Status")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdStatus(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdStatus(*this);
	}
};

/// Handles file remove requests
class KoFileRemoveHandler : public KiSelectionHandler
{
public:
	KoFileRemoveHandler()
		: KiSelectionHandler("Remove")
	{}

	virtual void OnFolder(const char* fullpath)
	{ /* folder cannot be removed */ }

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdRemove(*this);
	}
};

/// Handles locking on the selected items
class KoLockHandler : public KiSelectionHandler
{
public:
	KoLockHandler()
		: KiSelectionHandler("Lock")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdLock(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdLock(*this);
	}
};

/// Handles unlocking on the selected items
class KoUnlockHandler : public KiSelectionHandler
{
public:
	KoUnlockHandler()
		: KiSelectionHandler("Unlock")
	{}
	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdUnlock(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdUnlock(*this);
	}
};

/// Handles addition of the selected items
class KoAddHandler : public KiSelectionHandler
{
public:
	KoAddHandler()
		: KiSelectionHandler("Add")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdAdd(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdAdd(*this);
	}
};

/// Handles binary addition of the selected items
class KoAddBinaryHandler : public KiSelectionHandler
{
public:
	KoAddBinaryHandler() 
		: KiSelectionHandler("Add Binary")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdAdd(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdAdd(*this, kAddBinary);
	}
};

/// Handles unicode addition of the selected items
class KoAddUnicodeHandler : public KiSelectionHandler
{
public:
	KoAddUnicodeHandler()
		: KiSelectionHandler("Add Unicode")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdAdd(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdAdd(*this, kAddUnicode);
	}
};

/// Handles commits of the selected items
class KoCommitHandler : public KiSelectionHandler
{
public:
	KoCommitHandler()
		: KiSelectionHandler("Commit")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdCommit(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdCommit(*this);
	}
};

/// Handles updates on the selected items
class KoUpdateHandler : public KiSelectionHandler
{
public:
	KoUpdateHandler()
		: KiSelectionHandler("Update")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdUpdate(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdUpdate(*this);
	}
};

/// Handles query updates on the selected items
class KoQueryUpdateHandler : public KiSelectionHandler
{
public:
	KoQueryUpdateHandler()
		: KiSelectionHandler("Query Update")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdUpdate(*this, true);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdUpdate(*this, true);
	}
};

/// Handles annotate on the selected items
class KoAnnotateHandler : public KiSelectionHandler
{
public:
	KoAnnotateHandler()
		: KiSelectionHandler("Annotate")
	{}

	virtual void OnFolder(const char* /*fullpath*/)
	{
		/*we avoid processing directory*/
	}
	
	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdAnnotate(*this);
	}
};

/// Handles cancel changes on the selected items
class KoCancelChangesHandler : public KiSelectionHandler
{
public:
	KoCancelChangesHandler()
		: KiSelectionHandler("Cancel Changes")
	{}

	virtual void OnFolder(const char* /*fullpath*/)
	{
		/*we avoid processing directory*/
	}
	
	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdCancelChanges(*this);
	}
};

/// Handles Command Line
class KoCommandLineHandler : public KiSelectionHandler
{
public:
	KoCommandLineHandler()
		: KiSelectionHandler("Command Line", false)
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdCommandLine(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdCommandLine(*this);
	}
};

/// Handles Import
class KoImportHandler : public KiSelectionHandler
{
public:
	KoImportHandler()
		: KiSelectionHandler("Import")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdImportModule(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdImportModule(*this);
	}
};

/// Handles Checkout
class KoCheckoutHandler : public KiSelectionHandler
{
public:
	KoCheckoutHandler()
		: KiSelectionHandler("Checkout")
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdCheckoutModule(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdCheckoutModule(*this);
	}
};

/// Handles Rtag Create
class KoRtagCreateHandler : public KiSelectionHandler
{
public:
	KoRtagCreateHandler()
		: KiSelectionHandler("Rtag Create", false)
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdRtagCreate(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdRtagCreate(*this);
	}
};

/// Handles Rtag Delete
class KoRtagDeleteHandler : public KiSelectionHandler
{
public:
	KoRtagDeleteHandler()
		: KiSelectionHandler("Rtag Delete", false)
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdRtagDelete(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdRtagDelete(*this);
	}
};

/// Handles Rtag Branch
class KoRtagBranchHandler : public KiSelectionHandler
{
public:
	KoRtagBranchHandler()
		: KiSelectionHandler("Rtag Branch", false)
	{}

	virtual void OnFolder(const char* fullpath)
	{
		KiSelectionHandler::OnFolder(fullpath);
		CvsCmdRtagBranch(*this);
	}

	virtual void OnFiles(MultiFiles* pmf)
	{
		KiSelectionHandler::OnFiles(pmf);
		CvsCmdRtagBranch(*this);
	}
};

#endif
// OWSEVIEWHANDLERS_H_
