/*
 * databaseinfodialog.cc
 *
 * Copyright (c) 2002, 2003 Frerich Raabe <raabe@kde.org>
 *
 * 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. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "application.h"
#include "databaseinfodialog.h"
#include "portmgr.h"

#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include <qdir.h>
#include <qgrid.h>
#include <qlabel.h>

using namespace Barry;

DatabaseInfoDialog::DatabaseInfoDialog( QWidget *parent )
	: KDialogBase( parent, "DatabaseInfoDialog", true,
	               i18n( "Database information" ), Close | User1 | User2, Close,
	               true, i18n( "&Clear cache" ), i18n( "&Rebuild cache" ) )
{
	QGrid *mainWidget = makeGridMainWidget( 2, Horizontal );

	new QLabel( i18n("Port database timestamp:" ), mainWidget );
	m_portTimeStamp = new QLabel( mainWidget );

	new QLabel( i18n( "Package database timestamp:" ), mainWidget );
	m_packageTimeStamp = new QLabel( mainWidget );

	new QLabel( i18n( "Cached items:" ), mainWidget );
	m_cachedItems = new QLabel( mainWidget );

	new QLabel( i18n( "Cache size:" ), mainWidget );
	m_cacheSize = new QLabel( mainWidget );

	showData();
}

void DatabaseInfoDialog::showData()
{
	QDateTime timeStamp = PortMgr::self().portDatabaseTimestamp();
	QString s = KGlobal::locale()->formatDateTime( timeStamp, false, true );
	m_portTimeStamp->setText( s );

	timeStamp = PortMgr::self().packageDatabaseTimestamp();
	s = KGlobal::locale()->formatDateTime( timeStamp, false, true );
	m_packageTimeStamp->setText( s );

	m_cachedItems->setText( QString::number( cachedItems() ) );

	m_cacheSize->setText( i18n( "%1 bytes" ).arg( cacheSize() ) );
}

unsigned int DatabaseInfoDialog::cachedItems() const
{
	const QDir cacheDir( cacheDirName() );

	return cacheDir.entryList( QDir::Files ).count();
}

QString DatabaseInfoDialog::cacheDirName() const
{
	return ::locateLocal( "appdata", QString::fromLatin1( "cache/" ) );
}

unsigned long DatabaseInfoDialog::cacheSize() const
{
	unsigned long size = 0;

	const QDir cacheDir( cacheDirName() );

	const QStringList items = cacheDir.entryList( QDir::Files );
	QStringList::ConstIterator it = items.begin();
	QStringList::ConstIterator end = items.end();
	for ( ; it != end; ++it ) {
		const QFileInfo fi( cacheDir.absPath() + QString::fromLatin1( "/" ) + *it );
		size += fi.size();
	}

	return size;
}

void DatabaseInfoDialog::slotUser1()
{
	QDir cacheDir( cacheDirName() );

	const QStringList items = cacheDir.entryList( QDir::Files );
	QStringList::ConstIterator it = items.begin();
	QStringList::ConstIterator end = items.end();
	for ( ; it != end; ++it ) {
		if ( !cacheDir.remove( *it ) )
			kdWarning() << "Failed to delete file '" << *it << "'." << endl;
	}

	showData();
}

void DatabaseInfoDialog::slotUser2()
{
	static_cast<Application *>( kapp )->rebuildCache();
	showData();
}

#include "databaseinfodialog.moc"
// vim:ts=4:sw=4:noet:list
