/***************************************************************************
 *
 * Copyright (C) 2007 Elad Lahav (elad_lahav@users.sourceforge.net)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, m_pWindow list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, m_pWindow list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include <tdelocale.h>
#include "kscope.h"
#include "kscopeactions.h"
#include "kscopeconfig.h"
#include "filelist.h"
#include "editortabs.h"
#include "querywidget.h"

KScopeActions::KScopeActions(KScope* pWindow) : TQObject(),
		m_pWindow(pWindow),
		m_pCollection(pWindow->actionCollection())
{
}

KScopeActions::~KScopeActions()
{
}

/**
 * Connects menu bar and toolbar commands with class members. These members
 * handle the actions associated with each command.
 */
void KScopeActions::init()
{
	// File menu
	KStdAction::openNew(m_pWindow, TQ_SLOT(slotNewFile()), m_pCollection);
	KStdAction::open(m_pWindow, TQ_SLOT(slotOpenFile()), m_pCollection);
	KStdAction::close(m_pWindow, TQ_SLOT(slotCloseEditor()), m_pCollection);
	KStdAction::quit(m_pWindow, TQ_SLOT(slotClose()), m_pCollection);

	addAction(i18n("Go to File List"),
		NULL,
		"Ctrl+Shift+O",
		m_pWindow->m_pFileList,
		TQ_SLOT(slotSetFocus()),
		"file_open_file_from_list",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("Save Al&l"),
		"save_all",
		"Ctrl+L",
		m_pWindow->m_pEditTabs,
		TQ_SLOT(slotSaveAll()),
		"file_save_all",
		NULL);
	
	// Edit menu
	m_pExtEditAction = addAction(i18n("Edit in E&xternal Editor"),
		NULL,
		"Ctrl+E",
		m_pWindow,
		TQ_SLOT(slotExtEdit()),
		"edit_external_editor",
		TQ_SIGNAL(toggleFile(bool)));
		
	addAction(i18n("Go To Tag"),
		NULL,
		"Ctrl+Shift+T",
		m_pWindow,
		TQ_SLOT(slotGotoTag()),
		"edit_goto_tag",
		TQ_SIGNAL(toggleFile(bool)));
	
	addAction(i18n("Complete Symbol"),
		NULL,
		"Ctrl+Space",
		m_pWindow,
		TQ_SLOT(slotCompleteSymbol()),
		"edit_comp_symbol",
		TQ_SIGNAL(toggleFile(bool)));
		
	// Project menu
	addAction(i18n("&New Project..."),
		NULL,
		NULL,
		m_pWindow,
		TQ_SLOT(slotCreateProject()),
		"project_new",
		NULL);
		
	addAction(i18n("&Open Project..."),
		"project_open",
		NULL,
		m_pWindow,
		TQ_SLOT(slotOpenProject()),
		"project_open",
		NULL);
		
	addAction(i18n("Open &Cscope.out..."),
		NULL,
		NULL,
		m_pWindow,
		TQ_SLOT(slotProjectCscopeOut()),
		"project_cscope_out",
		NULL);
		
	addAction(i18n("Add/Remove &Files..."),
		NULL,
		NULL,
		m_pWindow,
		TQ_SLOT(slotProjectFiles()),
		"project_add_rem_files",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Properties..."),
		NULL,
		NULL,
		m_pWindow,
		TQ_SLOT(slotProjectProps()),
		"project_properties",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Make Project"),
		"make_tdevelop",
		"Ctrl+M",
		m_pWindow,
		TQ_SLOT(slotProjectMake()),
		"project_make",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Remake Project"),
		"rebuild",
		"Ctrl+Shift+M",
		m_pWindow,
		TQ_SLOT(slotProjectRemake()),
		"project_remake",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("&Close Project"),
		"window-close",
		NULL,
		m_pWindow,
		TQ_SLOT(slotCloseProject()),
		"project_close",
		TQ_SIGNAL(toggleProject(bool)));

	// Cscope menu
	addAction(i18n("&References..."),
		NULL,
		"Ctrl+0",
		m_pWindow,
		TQ_SLOT(slotQueryReference()),
		"cscope_references",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Definition..."),
		NULL,
		"Ctrl+1",
		m_pWindow,
		TQ_SLOT(slotQueryDefinition()),
		"cscope_definition",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("&Called Functions..."),
		NULL,
		"Ctrl+2",
		m_pWindow,
		TQ_SLOT(slotQueryCalled()),
		"cscope_called",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("C&alling Functions..."),
		NULL,
		"Ctrl+3",
		m_pWindow,
		TQ_SLOT(slotQueryCalling()),
		"cscope_calling",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("Find &Text..."),
		NULL,
		"Ctrl+4",
		m_pWindow,
		TQ_SLOT(slotQueryText()),
		"cscope_text",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("Find &EGrep Pattern..."),
		NULL,
		"Ctrl+5",
		m_pWindow,
		TQ_SLOT(slotQueryPattern()),
		"cscope_pattern",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("Find &File..."),
		NULL,
		"Ctrl+7",
		m_pWindow,
		TQ_SLOT(slotQueryFile()),
		"cscope_file",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Including Files..."),
		NULL,
		"Ctrl+8",
		m_pWindow,
		TQ_SLOT(slotQueryIncluding()),
		"cscope_including",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Quick Definition"),
		NULL,
		"Ctrl+]",
		m_pWindow,
		TQ_SLOT(slotQueryQuickDef()),
		"cscope_quick_def",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("Call &Graph..."),
		NULL,
		"Ctrl+\\",
		m_pWindow,
		TQ_SLOT(slotCallTree()),
		"cscope_call_tree",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("Re&build database"),
		"vcs_update",
		NULL,
		m_pWindow,
		TQ_SLOT(slotRebuildDB()),
		"cscope_rebuild",
		TQ_SIGNAL(toggleProject(bool)));

	// Go menu
	addAction(i18n("P&revious Result"),
		"go-up",
		"Alt+Up", 
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotPrevResult()),
		"go_prev_result",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("N&ext Result"),
		"go-down",
		"Alt+Down",
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotNextResult()),
		"go_next_result",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("&Previous Position"),
		"back", 
		"Alt+Left",
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotHistoryPrev()),
		"go_prev_pos",
		NULL);
	
	addAction(i18n("&Next Position"),
		"forward", 
		"Alt+Right",
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotHistoryNext()),
		"go_next_pos",
		NULL);
		
	addAction(i18n("Position &History"),
		"history",
		"Ctrl+h",
		m_pWindow,
		TQ_SLOT(slotHistoryShow()),
		"go_history",
		NULL);
		
	addAction(i18n("Global &Bookmarks"),
		"bookmark", 
		"Ctrl+Shift+G",
		m_pWindow,
		TQ_SLOT(slotShowBookmarks()),
		"go_bookmarks",
		NULL);

	// View menu
	m_pToggleFileViewAction = addToggle(i18n("Toggle File List"),
		"view_sidetree",
		"Ctrl+/",
		m_pWindow->m_pFileViewDock,
		TQ_SLOT(changeHideShowState()),
		"view_toggle_filelist_dock",
		NULL);
	
	m_pToggleQueryWindowAction = addToggle(i18n("Toggle Query Window"),
		"view_top_bottom",
		"Ctrl+.",
		m_pWindow->m_pQueryDock,
		TQ_SLOT(changeHideShowState()),
		"view_toggle_query_dock",
		NULL);
	
	m_pToggleTagListAction = addToggle(i18n("Toggle Tag List"),
		"view_detailed",
		"Ctrl+'",
		m_pWindow->m_pEditTabs,
		TQ_SLOT(slotToggleTagList()),
		"view_toggle_tag_list",
		NULL);
			
	// Window menu
	addAction(i18n("Close &All"),
		"window-close",
		NULL,
		m_pWindow,
		TQ_SLOT(slotCloseAllWindows()),
		"window_close_all",
		NULL);
		
	addAction(i18n("Go &Left"),
		"back", 
		"Alt+Shift+Left",
		m_pWindow->m_pEditTabs,
		TQ_SLOT(slotGoLeft()),
		"window_go_left",
		NULL);
		
	addAction(i18n("Go &Right"),
		"forward",
		"Alt+Shift+Right",
		m_pWindow->m_pEditTabs,
		TQ_SLOT(slotGoRight()),
		"window_go_right",
		NULL);
		
	// Settings menu
	KStdAction::preferences(m_pWindow, TQ_SLOT(slotConfigure()), m_pCollection);
	KStdAction::keyBindings(m_pWindow, TQ_SLOT(slotShortcuts()), m_pCollection);
	
	// Help menu
	addAction(i18n("Show &Welcome Message..."),
		NULL,
		NULL,
		m_pWindow,
		TQ_SLOT(slotShowWelcome()),
		"help_welcome",
		NULL);
	
	// Query widget popup menu
	addAction(i18n("&New"),
		"document-new",
		NULL,
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotNewQueryPage()),
		"query_new",
		TQ_SIGNAL(toggleProject(bool)));
	
	addAction(i18n("&Refresh"),
		"reload",
		NULL,
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotRefreshCurrent()),
		"query_refresh",
		TQ_SIGNAL(toggleProject(bool)));
		
	m_pLockAction = addToggle(i18n("&Lock/Unlock"),
		"encrypted",
		NULL,
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotLockCurrent()),
		"query_toggle_locked",
		TQ_SIGNAL(toggleProject(bool)));
		
	addAction(i18n("&Close"),
		"window-close",
		NULL,
		m_pWindow->m_pQueryWidget,
		TQ_SLOT(slotCloseCurrent()),
		"query_close",
		TQ_SIGNAL(toggleProject(bool)));
	
	m_pExtEditAction->setEnabled(Config().useExtEditor());
}

void KScopeActions::initLayoutActions()
{
	m_pToggleFileViewAction->setChecked(m_pWindow->m_pFileViewDock->isShown());
	m_pToggleQueryWindowAction->setChecked(m_pWindow->m_pQueryDock->isShown());
	m_pToggleTagListAction->setChecked(Config().getShowTagList());
}

/**
 * Enables/disables the "Edit in External Editor" command.
 * @param	bEnable	true to enable the command, false to disable it
 */
void KScopeActions::enableExtEditor(bool bEnable)
{
	m_pExtEditAction->setEnabled(bEnable);
}
 
void KScopeActions::slotQueryDockToggled(bool bVisible)
{
	m_pToggleQueryWindowAction->setChecked(bVisible);
}

/**
 * Ensures the "Show/Hide Query Window" action is unchecked when the dock
 * is closed through its close button.
 * This slot is conncted to the headerCloseButtonClicked() signal of the
 * query window dock widget.
 */
void KScopeActions::slotQueryDockClosed()
{
	m_pToggleQueryWindowAction->setChecked(false);
}

/**
 * Ensures the "Show/Hide File View" action is unchecked when the dock
 * is closed through its close button.
 * This slot is conncted to the headerCloseButtonClicked() signal of the
 * file view dock widget.
 */
void KScopeActions::slotFileViewDockClosed()
{
	m_pToggleFileViewAction->setChecked(false);
}
 
/**
 * Enables/disables all actions related to open projects.
 * This slot should be called whenever a project is opened or closed.
 * @param	bEnable	true to enable actions, false to disable
 */
void KScopeActions::slotEnableProjectActions(bool bEnable)
{
	emit toggleProject(bEnable);
}
 
/**
 * Enables/disables all actions related to open files.
 * This slot should be called the first file is opened, or when the last one
 * is closed.
 * @param	bEnable	true to enable actions, false to disable
 */
void KScopeActions::slotEnableFileActions(bool bEnable)
{
	emit toggleFile(bEnable);
}

/**
 * Creates a new action.
 * @param	sCaption	The text to display in the menu item
 * @param	szIcon		Optional icon associated with the action
 * @param	szShortcut	Optional key-combination string
 * @param	pReceiver	The widget to receive the action's signal
 * @param	szSlot		The widget's slot that connect to the signal
 * @param	szName		The XML entry corresponding to the action
 * @param	szSignal	Optional signal to connect to the setEnabled() slot of
 *						the action
 * @return	The newly created action object
 */
TDEAction* KScopeActions::addAction(const TQString& sCaption, const char* szIcon,
	const char* szShortcut, TQWidget* pReceiver, const char* szSlot,
	const char* szName, const char* szSignal)
{
	TDEAction* pAction;
	
	// Create the action
	pAction = new TDEAction(sCaption,
			szIcon,
			szShortcut == NULL ? TDEShortcut() : TDEShortcut(szShortcut),
			pReceiver,
			szSlot,
			m_pCollection,
			szName);
			
	// Add to the given action list, if any
	if (szSignal)
		connect(this, szSignal, pAction, TQ_SLOT(setEnabled(bool)));
		
	return pAction;
}

/**
 * Creates a new toggle action.
 * @param	sCaption	The text to display in the menu item
 * @param	szIcon		Optional icon associated with the action
 * @param	szShortcut	Optional key-combination string
 * @param	pReceiver	The widget to receive the action's signal
 * @param	szSlot		The widget's slot that connect to the signal
 * @param	szName		The XML entry corresponding to the action
 * @param	szSignal	Optional signal to connect to the setEnabled() slot of
 *						the action
 * @return	The newly created action object
 */
TDEToggleAction* KScopeActions::addToggle(const TQString& sCaption,
	const char* szIcon, const char* szShortcut, TQWidget* pReceiver,
	const char* szSlot, const char* szName, const char* szSignal)
{
	TDEToggleAction* pAction;
	
	// Create the action
	pAction = new TDEToggleAction(sCaption,
			szIcon,
			szShortcut == NULL ? TDEShortcut() : TDEShortcut(szShortcut),
			pReceiver,
			szSlot,
			m_pCollection,
			szName);
			
	// Add to the given action list, if any
	if (szSignal)
		connect(this, szSignal, pAction, TQ_SLOT(setEnabled(bool)));
		
	return pAction;
}

#include "kscopeactions.moc"
