/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "gui/syncsourcedialog.h"

#include "core/profile.h"

#include "devicesync/syncsource.h"
#include "devicesync/sync.h"

#include "veneer/settings.h"
#include "veneer/iconloader.h"

#include <QDebug>
#include <QPushButton>

SyncSourceDialog::SyncSourceDialog(QWidget* parent, Sync* sync) :
	QDialog(parent),
	m_Sync(sync)
{
	setupUi(this);

	setupDialog(m_Sync->sources());

	setupConnections();

	Veneer::restoreDialogSize("syncSourceDialog", this);
}

SyncSourceDialog::~SyncSourceDialog()
{
	Veneer::saveDialogSize("syncSourceDialog", this);
}

void SyncSourceDialog::setupDialog(const QList< SyncSource* >& sources)
{
	refreshButton->setIcon(BarIcon("view-refresh"));
	buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
	syncSourcesList->clear();

	foreach(const SyncSource* s, sources)
	{
		QListWidgetItem* item = new QListWidgetItem();
		item->setText(s->name());
		if (!s->iconName().isEmpty())
			item->setIcon(DesktopIcon(s->iconName()));
		item->setSizeHint(QSize(0, 32));
		syncSourcesList->addItem(item);
	}
}

void SyncSourceDialog::setupConnections()
{
}

void SyncSourceDialog::on_syncSourcesList_itemSelectionChanged()
{
	buttonBox->button(QDialogButtonBox::Ok)->setEnabled(syncSourcesList->selectedItems().size() > 0);
}

void SyncSourceDialog::on_syncSourcesList_itemActivated(QListWidgetItem*)
{
	accept();
}

void SyncSourceDialog::on_refreshButton_clicked(bool)
{
	m_Sync->scan();
	setupDialog(m_Sync->sources());
}

int SyncSourceDialog::selectedRow() const
{
	return syncSourcesList->currentRow();
}
