// Copyright (c) 2003-2005 Charles Samuels <charles@kde.org>
// See the file COPYING for redistribution terms.

#include "view.h"
#include "oblique.h"
#include "menu.h"

#include <noatun/app.h>

#include <kstdaction.h>
#include <tdelocale.h>
#include <kedittoolbar.h>
#include <tdefiledialog.h>
#include <tqlabel.h>
#include <klineeditdlg.h>
#include <tdemessagebox.h>
#include <ktabwidget.h>
#include <tqtabbar.h>

class TabWidget : public KTabWidget
{
public:
	TabWidget(TQWidget *parent)
		: KTabWidget(parent)
	{
	}
public:
	TQTabBar *tabBar() const { return KTabWidget::tabBar(); }
};


View::View(Oblique *oblique)
	: TDEMainWindow(0, 0)
{
	mOblique = oblique;
	mTree = 0;

	mTabs = new TabWidget(this);
	mTabs->tabBar()->hide();
	connect(mTabs, TQ_SIGNAL(currentChanged(TQWidget*)), TQ_SLOT(currentTabChanged(TQWidget*)));

	setCentralWidget(mTabs);

	TDEAction *ac;
	ac = new TDEAction(i18n("Add &Files..."), "queue", 0, this, TQ_SLOT(addFiles()), actionCollection(), "add_files");
	ac->setWhatsThis(i18n("Add a reference to a media file on disk to this collection."));
	ac = new TDEAction(i18n("Add Fol&ders..."), "folder", 0, this, TQ_SLOT(addDirectory()), actionCollection(), "add_dir");
	
	
//	ac = new TDEAction(i18n("&Reload"), "reload", 0, oblique, TQ_SLOT(reload()), actionCollection(), "reload");
//	ac->setWhatsThis(i18n("Reread the collection and meta-information from its files."));

	ac = new SliceListAction(
			i18n("&Slices"), oblique,
			this, TQ_SLOT(use(Slice*)), TQValueList<File>(), actionCollection(), "slices"
		);
	ac->setWhatsThis(i18n("Select a sub-collection to display"));

	mSchemaListAction = new SchemaListAction(
			i18n("&Schemas"), this, TQ_SLOT(setSchema(const TQString&)), actionCollection(), "schemas"
		);
	mSchemaListAction->setWhatsThis(i18n("Select a schema to use to collate the tree."));

	ac = new TDEAction(
			i18n("&New Tab"), "tab_new", "CTRL+SHIFT+N;CTRL+T", this, TQ_SLOT(addTab()),
			actionCollection(), "newtab"
		);

	mRemoveTabAction = new TDEAction(
			i18n("&Close Current Tab"), "tab_remove", CTRL+Key_W, this, TQ_SLOT(removeTab()),
			actionCollection(), "removecurrenttab"
		);

	{
		TQLabel *l = new TQLabel(i18n("&Jump:"), 0, "tde toolbar widget");
		l->setBackgroundMode( TQt::PaletteButton );
		l->setAlignment(
				(TQApplication::reverseLayout() ? TQt::AlignRight : TQt::AlignLeft) |
					TQt::AlignVCenter | TQt::ShowPrefix
			);
		l->adjustSize();
		new KWidgetAction(l, i18n("&Jump:"), TDEShortcut(ALT + Key_J), 0, 0, actionCollection(), "jump_label");

		LineEditAction *jumpAction = new LineEditAction(i18n("Jump Bar"), 0, 0, actionCollection(), "jump_text");
		jumpAction->setWhatsThis(i18n("Only display items which contain this string"));
		l->setBuddy(jumpAction->lineEdit());
		connect(jumpAction->lineEdit(), TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(jumpTextChanged(const TQString&)));
	}

	KStdAction::configureToolbars(this, TQ_SLOT(configureToolBars()), actionCollection());

	applyMainWindowSettings(TDEGlobal::config(), "Oblique View");
	createGUI("obliqueui.rc");

	TDEConfigGroup g(TDEGlobal::config(), "oblique");
	TQStringList tabids = g.readListEntry("tabids");
	
	for (TQStringList::Iterator i(tabids.begin()); i != tabids.end(); ++i)
	{
		TQString t = *i;
		int sliceid = t.section(':', 0, 0).toInt();
		TQString fileOfQuery = t.section(':', 1, 1);
		
		Slice *slice = oblique->base()->sliceById(sliceid);
		if (!slice)
			slice = oblique->base()->defaultSlice();
		Tree *tree = new Tree(oblique, mTabs);
		mTrees.append(tree);
		tree->setSlice(slice);
		tree->setSchema(fileOfQuery);
		mTabs->addTab(tree, slice->name());
	}
	
	if (mTabs->count() == 0)
	{ // no tabs, so use the "normal" route
		addTab();
	}
	else
	{
		// ok, there's a tab, so make it present
		if (mTabs->count() >= 1)
		{
			mTree = static_cast<Tree*>(mTrees.first());
			currentTabChanged(mTrees.first());
		}
	
		if (mTabs->count() > 1)
			mTabs->tabBar()->show();
	}
}

View::~View()
{
	TQStringList tabids;
	for (int i=0; i < mTabs->count(); i++)
	{
		Tree *tree = static_cast<Tree*>(mTabs->page(i));
		int slice = tree->slice()->id();
		TQString query = tree->fileOfQuery();

		TQString t = TQString("%1:%2").arg(slice).arg(query);
		tabids.append(t);
	}
	
	TDEConfigGroup g(TDEGlobal::config(), "oblique");
	g.writeEntry("tabids", tabids);
	g.sync();
}

void View::configureToolBars()
{
	saveMainWindowSettings(TDEGlobal::config(), "Oblique View");
	KEditToolbar dlg(actionCollection(), "obliqueui.rc");
	connect(&dlg, TQ_SIGNAL(newToolbarConfig()), TQ_SLOT(newToolBarConfig()));
	dlg.exec();
}

void View::newToolBarConfig()
{
	createGUI("obliqueui.rc");
	applyMainWindowSettings(TDEGlobal::config(), "Oblique View");
}

void View::closeEvent(TQCloseEvent*)
{
	hide();
}


void View::addFiles()
{
	KURL::List files=KFileDialog::getOpenURLs(":mediadir", napp->mimeTypes(), this, i18n("Select Files to Add"));

	for(KURL::List::Iterator it=files.begin(); it!=files.end(); ++it)
		oblique()->addFile(KURL(*it));
}

void View::addDirectory()
{
	TQString folder = KFileDialog::getExistingDirectory(":mediadir:", this,
		i18n("Select Folder to Add"));
	
	if (!folder)
		return;

	KURL url;
	url.setPath(folder);
	oblique()->beginDirectoryAdd(url);
}

void View::addTab()
{
	Tree *t = new Tree(oblique(), mTabs);
	if (!mTree) mTree = t;
	mTrees.append(t);

	mTabs->addTab(t, t->slice()->name());
	mTabs->showPage(t);
	if (mTabs->count() > 1)
		mTabs->tabBar()->show();
	currentTabChanged(t);
}

void View::removeTab()
{
	Tree *current = static_cast<Tree*>(mTabs->currentPage());
	if (current == mTree) return;
	mTabs->removePage(current);
	mTrees.remove(current);
	delete current;

	if (mTabs->count() == 1)
		mTabs->tabBar()->hide();
}

void View::currentTabChanged(TQWidget *w)
{
	mRemoveTabAction->setEnabled(w != mTree);
	mSchemaListAction->setTree(static_cast<Tree*>(w));
}

void View::setSchema(const TQString &file)
{
	Tree *current = static_cast<Tree*>(mTabs->currentPage());
	current->setSchema(file);
}


void View::jumpTextChanged(const TQString &text)
{
	Tree *current = static_cast<Tree*>(mTabs->currentPage());
	current->setLimit(text);
}

void View::use(Slice *s)
{
	Tree *current = static_cast<Tree*>(mTabs->currentPage());
	current->setSlice(s);
	mTabs->setTabLabel(current, current->slice()->name());
}



LineEditAction::LineEditAction(const TQString &text, const TQObject *reciever, const char *slot, TDEActionCollection *parent, const char *name)
	: KWidgetAction(new KLineEdit(0), text, TDEShortcut(0), reciever, slot, parent, name)
{
	setAutoSized(true);
}

#include "view.moc"

