// 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 <klocale.h>
#include <kedittoolbar.h>
#include <kfiledialog.h>
#include <tqlabel.h>
#include <klineeditdlg.h>
#include <kmessagebox.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)
	: KMainWindow(0, 0)
{
	mOblique = oblique;
	mTree = 0;

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

	setCentralWidget(mTabs);

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

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

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

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

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

	{
		TQLabel *l = new TQLabel(i18n("&Jump:"), 0, "kde toolbar widget");
		l->setBackgroundMode( TQt::PaletteButton );
		l->tqsetAlignment(
				(TQApplication::reverseLayout() ? TQt::AlignRight : TQt::AlignLeft) |
					TQt::AlignVCenter | TQt::ShowPrefix
			);
		l->adjustSize();
		new KWidgetAction(l, i18n("&Jump:"), KShortcut(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(), TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(jumpTextChanged(const TQString&)));
	}

	KStdAction::configureToolbars(TQT_TQOBJECT(this), TQT_SLOT(configureToolBars()), actionCollection());

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

	KConfigGroup g(KGlobal::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").tqarg(slice).tqarg(query);
		tabids.append(t);
	}
	
	KConfigGroup g(KGlobal::config(), "oblique");
	g.writeEntry("tabids", tabids);
	g.sync();
}

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

void View::newToolBarConfig()
{
	createGUI("obliqueui.rc");
	applyMainWindowSettings(KGlobal::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, KActionCollection *parent, const char *name)
	: KWidgetAction(new KLineEdit(0), text, KShortcut(0), reciever, slot, parent, name)
{
	setAutoSized(true);
}

#include "view.moc"

