/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/



#include "csearchresultview.h"

#include "backend/cswordversekey.h"

#include "frontend/cdragdropmgr.h"
#include "frontend/cexportmanager.h"

#include "util/cresmgr.h"

//TQt includes

//TDE includes
#include <tdelocale.h>
#include <tdeaction.h>
#include <tdepopupmenu.h>

namespace Search {
	namespace Result {

CSearchResultView::CSearchResultView(TQWidget* parent, const char* name) :
	TDEListView(parent, name), m_module(0) {
	initView();
	initConnections();
}

CSearchResultView::~CSearchResultView() {}

/** Initializes the view of this widget. */
void CSearchResultView::initView() {
	addColumn(i18n("Results"));
	setFullWidth(true);
	
	setSorting(-1);
	setDragEnabled(true);
	setSelectionModeExt(TDEListView::Extended);

	//setup the popup menu
	m_popup = new TDEPopupMenu(this);

	m_actions.copyMenu = new TDEActionMenu(i18n("Copy..."), CResMgr::searchdialog::result::foundItems::copyMenu::icon, m_popup);
	m_actions.copyMenu->setDelayed(false);
	m_actions.copy.result = new TDEAction(i18n("Reference only"), TDEShortcut(0), this, TQ_SLOT(copyItems()), this);
	m_actions.copyMenu->insert(m_actions.copy.result);
	m_actions.copy.resultWithText = new TDEAction(i18n("Reference with text"), TDEShortcut(0), this, TQ_SLOT(copyItemsWithText()), this);
	m_actions.copyMenu->insert(m_actions.copy.resultWithText);
	m_actions.copyMenu->plug(m_popup);

	m_actions.saveMenu = new TDEActionMenu(i18n("Save..."),CResMgr::searchdialog::result::foundItems::saveMenu::icon, m_popup);
	m_actions.saveMenu->setDelayed( false );
	m_actions.save.result = new TDEAction(i18n("Reference only"), TDEShortcut(0), this, TQ_SLOT(saveItems()), this);
	m_actions.saveMenu->insert(m_actions.save.result);
	m_actions.save.resultWithText = new TDEAction(i18n("Reference with text"), TDEShortcut(0), this, TQ_SLOT(saveItemsWithText()), this);
	m_actions.saveMenu->insert(m_actions.save.resultWithText);
	m_actions.saveMenu->plug(m_popup);

	m_actions.printMenu = new TDEActionMenu(i18n("Print..."),CResMgr::searchdialog::result::foundItems::printMenu::icon, m_popup);
	m_actions.printMenu->setDelayed(false);
	m_actions.print.result = new TDEAction(i18n("Reference with text"), TDEShortcut(0), this, TQ_SLOT(printItems()), this);
	m_actions.printMenu->insert(m_actions.print.result);
	m_actions.printMenu->plug(m_popup);
}

/** No descriptions */
void CSearchResultView::initConnections() {
	//  connect(this, TQ_SIGNAL(executed(TQListViewItem*)),
	//   this, TQ_SLOT(executed(TQListViewItem*)));
	connect(this, TQ_SIGNAL(currentChanged(TQListViewItem*)),
			this, TQ_SLOT(executed(TQListViewItem*)));

	connect(this, TQ_SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)),
			this, TQ_SLOT(showPopup(TDEListView*, TQListViewItem*, const TQPoint&)));
}

/** Setups the list with the given module. */
void CSearchResultView::setupTree(CSwordModuleInfo* m) {
	clear();
	
	if (!m) {
		return;
	}

	m_module = m;

	sword::ListKey& result = m->searchResult();
	const int count = result.Count();
	if (!count) {
		return;
	}

	setUpdatesEnabled(false);

	TQListViewItem* oldItem = 0;
	TDEListViewItem* item = 0;
	for (int index = 0; index < count; index++) {
		item = new TDEListViewItem(this, oldItem);
		item->setText(0, TQString::fromUtf8(result.GetElement(index)->getText()));
		
		oldItem = item;
	}

	setUpdatesEnabled(true);

	setSelected(firstChild(), true);
	executed(currentItem());
}

void CSearchResultView::setupStrongsTree(CSwordModuleInfo* m, TQStringList* vList) {
   clear();
   if (!m) {
      return;
   }

   m_module = m;

   if (vList->count() <= 0) {
      return;
   }

   setUpdatesEnabled(false);

   TDEListViewItem* oldItem = 0;
   TDEListViewItem* item = 0;

   for ( TQStringList::Iterator it = vList->begin(); it != vList->end(); ++it ) {
      item = new TDEListViewItem(this, oldItem);
      item->setText(0, (*it));
	  
      oldItem = item;
   }
   
   setUpdatesEnabled(true);

   setSelected(firstChild(), true);
   executed(currentItem());
}

/** Is connected to the signal executed, which is emitted when a mew item was chosen. */
void CSearchResultView::executed(TQListViewItem* item) {
	//  Q_ASSERT(item);
	//  tqWarning("executed");
	emit keySelected(item->text(0));
}

/** Reimplementation to show the popup menu. */
void CSearchResultView::showPopup(TDEListView*, TQListViewItem*, const TQPoint& point) {
	m_popup->exec(point);
}

/** No descriptions */
void CSearchResultView::printItems() {
	TQPtrList<TQListViewItem> items = selectedItems();
	CExportManager mgr(i18n("Print search result..."), true, i18n("Printing search result"));

	TQStringList list;
	for (TQListViewItem* k = items.first(); k; k = items.next()) {
		list.append( k->text(0) );
	};
	mgr.printKeyList( list, module(), CBTConfig::getDisplayOptionDefaults(), CBTConfig::getFilterOptionDefaults() );
}

/** No descriptions */
void CSearchResultView::saveItems() {
	CExportManager mgr(i18n("Save search result..."), true, i18n("Saving search result"));

	CSwordModuleInfo* m = module();
	CSwordKey* k = 0;
	TQPtrList<TQListViewItem> items = selectedItems();
	TQPtrList<CSwordKey> keys;
	for (TQListViewItem* i = items.first(); i; i = items.next()) {
		k = CSwordKey::createInstance( m );
		k->key(i->text(0));
		keys.append( k );
	};
	mgr.saveKeyList( keys, CExportManager::Text, false);

	keys.setAutoDelete(true);
	keys.clear(); //delete all the keys we created
}

/** No descriptions */
void CSearchResultView::saveItemsWithText() {
	CExportManager mgr(i18n("Save search result..."), true, i18n("Saving search result"));

	CSwordModuleInfo* m = module();
	CSwordKey* k = 0;
	TQPtrList<TQListViewItem> items = selectedItems();
	TQPtrList<CSwordKey> keys;
	for (TQListViewItem* i = items.first(); i; i = items.next()) {
		k = CSwordKey::createInstance( m );
		k->key(i->text(0));
		keys.append( k );
	};
	mgr.saveKeyList( keys, CExportManager::Text, true);

	keys.setAutoDelete(true);
	keys.clear(); //delete all the keys we created
}

/** No descriptions */
void CSearchResultView::copyItems() {
	CExportManager mgr(i18n("Copy search result..."), true, i18n("Copying search result"));

	CSwordModuleInfo* m = module();
	CSwordKey* k = 0;
	TQPtrList<TQListViewItem> items = selectedItems();
	TQPtrList<CSwordKey> keys;
	for (TQListViewItem* i = items.first(); i; i = items.next()) {
		k = CSwordKey::createInstance( m );
		k->key(i->text(0));
		keys.append( k );
	};
	mgr.copyKeyList( keys, CExportManager::Text, false);

	keys.setAutoDelete(true);
	keys.clear(); //delete all the keys we created
}

/** No descriptions */
void CSearchResultView::copyItemsWithText() {
	CExportManager mgr(i18n("Copy search result..."), true, i18n("Copying search result"));

	CSwordModuleInfo* m = module();
	CSwordKey* k = 0;
	TQPtrList<TQListViewItem> items = selectedItems();
	TQPtrList<CSwordKey> keys;
	for (TQListViewItem* i = items.first(); i; i = items.next()) {
		k = CSwordKey::createInstance( m );
		k->key(i->text(0));
		keys.append( k );
	};
	mgr.copyKeyList( keys, CExportManager::Text, true);

	keys.setAutoDelete(true);
	keys.clear(); //delete all the keys we created
}

/** Returns the module which is currently used. */
CSwordModuleInfo* const CSearchResultView::module() {
	return m_module;
}

TQDragObject* CSearchResultView::dragObject() {
	//return a valid DragObject to make DnD possible!

	/*
	* First get all selected items and fill with them the dndItems list. The return the TQDragObject we got from CDRagDropMgr
	*/
	CDragDropMgr::ItemList dndItems;

	TQPtrList<TQListViewItem> items = selectedItems();
	for (items.first(); items.current(); items.next()) {
		dndItems.append( CDragDropMgr::Item(m_module->name(), items.current()->text(0), TQString()) ); //no description
	};

	return CDragDropMgr::dragObject(dndItems, viewport());
}

	} //end of namespace Search::Result
} //end of namespace

#include "csearchresultview.moc"
