/* This file is part of the KDE project
   Copyright (C) 2004 Lucijan Busch <lucijan@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <kdebug.h>
#include <kiconloader.h>

#include <kexi.h>
#include <kexiproject.h>
#include <keximainwindow.h>
#include <kexipart.h>
#include <kexipartmanager.h>
#include <kexipartinfo.h>
#include <kexipartitem.h>
#include <kexipartdatasource.h>

#include "kexidatasourcecombo.h"

KexiDataSourceCombo::KexiDataSourceCombo(KexiMainWindow *win, QWidget *parent, const char *name)
 : QComboBox(parent, name)
{
	m_win = win;

	connect(this, SIGNAL(activated(int)), this, SLOT(activated(int)));
	populate();
}

void
KexiDataSourceCombo::populate()
{
	int id=0;
	KexiPart::DataSourceList *l = Kexi::partManager().dataSources();
	for(KexiPart::DataSource *it = l->first(); it; it = l->next())
	{
		KexiPart::ItemDict *pitems = m_win->project()->items(it->part()->info());

		for(QIntDictIterator<KexiPart::Item> item(*pitems); item.current(); ++item)
		{
			insertItem(SmallIcon(it->part()->info()->itemIcon()), item.current()->name());
			m_list.insert(id, DSInfo(it, *item.current()));
			id++;
		}

		delete pitems;
	}
}

void
KexiDataSourceCombo::activated(int id)
{
	DSInfo info = m_list[id];
	emit dataSourceChanged(info.first, info.second);
}

DSInfo
KexiDataSourceCombo::selectedSource()
{
	return m_list[currentItem()];
}

KexiDataSourceCombo::~KexiDataSourceCombo()
{
}

#include "kexidatasourcecombo.moc"

