/*
 * mainwindow.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 "databaseinfodialog.h"
#include "configdialog.h"
#include "history.h"
#include "mainwidget.h"
#include "mainwindow.h"
#include "orphanedfilesdialog.h"
#include "searchdialog.h"
#include "systemtraywin.h"
#include "unusedpackagesdialog.h"
#include "updatedpackagesdialog.h"

#include <kaction.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <kkeydialog.h>
#include <klocale.h>
#include <kmenubar.h>
#include <knotifydialog.h>
#include <krun.h>
#include <kstandarddirs.h>
#include <kstatusbar.h>

using namespace Barry;

MainWindow::MainWindow(): KMainWindow( 0, "MainWindow" )
{
	m_mainWidget = new MainWidget( this );
	connect( m_mainWidget, SIGNAL( statusMessage( const QString & ) ),
	         statusBar(), SLOT( message( const QString & ) ) );
	connect( m_mainWidget, SIGNAL( processFinished() ),
	         this, SLOT( processFinished() ) );
	connect( m_mainWidget, SIGNAL( findDialogRequest() ),
	         this, SLOT( find() ) );
	connect( m_mainWidget, SIGNAL( portSelected( const Port & ) ),
	         this, SLOT( portSelected( const Port & ) ) );
	setCentralWidget( m_mainWidget );

	resize( 600, 400 );

	setupActions();

	SystemTrayWin *systemTrayWin = new SystemTrayWin( this );
	systemTrayWin->show();

	History::self().installMenuBarHook( this );

	setAutoSaveSettings();
}

void MainWindow::setup( const SearchQuery &query )
{
	if ( loadDatabase() )
		find( query );
}

bool MainWindow::loadDatabase()
{
	statusBar()->message( i18n( "Loading database..." ) );

	if ( !PortMgr::self().load() ) {
		actionCollection()->action( "database_info" )->setEnabled( false );
		actionCollection()->action( "find_unused_pkgs" )->setEnabled( false );
		actionCollection()->action( "find_updated_pkgs" )->setEnabled( false );
		actionCollection()->action( "find_orphaned_files" )->setEnabled( false );
		statusBar()->message( i18n( "Aborted!" ), 2000 );
		return false;
	}

	statusBar()->message( i18n( "Finished." ), 2000 );
	m_mainWidget->reset();
	return true;
}

void MainWindow::setupActions()
{
	new KAction( i18n( "Database &information..." ), SmallIcon( QString::fromLatin1( "info" ) ),
	             CTRL+Key_I, this, SLOT( databaseInfo() ), actionCollection(),
	             "database_info" );
	new KAction( i18n( "Find &unused packages..." ), SmallIcon( QString::fromLatin1( "find" ) ),
	             CTRL+Key_U, this, SLOT( findUnusedPackages() ),
	             actionCollection(), "find_unused_pkgs" );
	new KAction( i18n( "Find u&pdates for packages..." ), SmallIcon( QString::fromLatin1( "find" ) ),
	             CTRL+Key_P, this, SLOT( findUpdatedPackages() ),
	             actionCollection(), "find_updated_pkgs" );
	new KAction( i18n( "Find orphaned files..." ), SmallIcon( QString::fromLatin1( "find" ) ),
	             CTRL+Key_O, this, SLOT( findOrphanedFiles() ),
	             actionCollection(), "find_orphaned_files" );

	QString icon = ::locate( "appdata",
	                         QString::fromLatin1( "install-port.png" ) );
	KAction *action = new KAction( i18n( "Install port..." ), icon, 0,
	                               this, SLOT( installPort() ),
	                               actionCollection(), "port_install" );
	action->setEnabled( false );

	icon = ::locate( "appdata",
	                 QString::fromLatin1( "upgrade-port.png" ) );
	action = new KAction( i18n( "Upgrade port..." ), icon, 0,
	                      this, SLOT( upgradePort() ),
	                      actionCollection(), "port_upgrade" );
	action->setEnabled( false );

	icon = ::locate( "appdata",
	                 QString::fromLatin1( "deinstall-port.png" ) );
	action = new KAction( i18n( "Deinstall port..." ), icon, 0,
	                      this, SLOT( deinstallPort() ),
	                      actionCollection(), "port_deinstall" );
	action->setEnabled( false );

	action = new KAction( i18n( "View WebCVS Entry" ), 0,
	                      this, SLOT( viewWebCVS() ),
	                      actionCollection(), "view_webcvs" );
	action->setEnabled( false );

	KStdAction::find( this, SLOT( find() ), actionCollection() );
	KStdAction::showMenubar( this, SLOT( toggleMenubar() ), actionCollection() );
	KStdAction::showStatusbar( this, SLOT( toggleStatusbar() ), actionCollection() );
	KStdAction::showToolbar( this, SLOT( toggleToolbars() ), actionCollection() );
	KStdAction::keyBindings( this, SLOT( configureKeybindings() ), actionCollection() );
	KStdAction::configureNotifications( this, SLOT( configureNotifications() ),
	                                    actionCollection() );
	KStdAction::preferences( this, SLOT( preferences() ), actionCollection() );
	KStdAction::quit( this, SLOT( close() ), actionCollection() );

	History::self().setupActions( actionCollection() );

	createGUI();
}

void MainWindow::processFinished()
{
	statusBar()->message( i18n( "Finished." ), 1000 );
}

void MainWindow::preferences()
{
	ConfigDialog dlg( this );
	if ( dlg.exec() == QDialog::Accepted ) {
		PortMgr::self().reparseConfig();
		History::self().reparseConfig();
		loadDatabase();
	}
}

void MainWindow::find()
{
	SearchDialog searchDlg( this );
	if ( searchDlg.exec() != QDialog::Accepted )
		return;

	find( searchDlg.searchQuery() );
}

void MainWindow::find( const SearchQuery &searchQuery )
{
	if ( searchQuery.isNull() )
		return;

	SearchProgressDialog dlg( searchQuery, this );
	connect( &dlg, SIGNAL( portSelected( const Port & ) ),
	         m_mainWidget, SLOT( selectPort( const Port & ) ) );
	dlg.exec();
}

void MainWindow::databaseInfo()
{
	DatabaseInfoDialog dlg( this );
	dlg.exec();
}

void MainWindow::findUnusedPackages()
{
	UnusedPackagesDialog dlg( this );
	if ( dlg.exec() != QDialog::Accepted )
		return;

	UnusedPackagesProgressDialog progressDlg( dlg.daysNotAccessed(),
	                                          dlg.leafPackagesOnly(),
	                                          this );
	connect( &progressDlg, SIGNAL( portSelected( const Port & ) ),
	         m_mainWidget, SLOT( selectPort( const Port & ) ) );
	progressDlg.exec();
}

void MainWindow::findUpdatedPackages()
{
	UpdatedPackagesProgressDialog dlg( this );
	connect( &dlg, SIGNAL( portSelected( const Port & ) ),
	         m_mainWidget, SLOT( selectPort( const Port & ) ) );
	dlg.exec();
}

void MainWindow::toggleMenubar()
{
	menuBar()->isVisible() ? menuBar()->hide() : menuBar()->show();
}

void MainWindow::toggleStatusbar()
{
	statusBar()->isVisible() ? statusBar()->hide() : statusBar()->show();
}

void MainWindow::toggleToolbars()
{
	QPtrListIterator<KToolBar> tbIt = toolBarIterator();
	KToolBar *tb;

	while ( ( tb = tbIt.current() ) != 0 ) {
		++tbIt;
		tb->isVisible() ? tb->hide() : tb->show();
	}
}

void MainWindow::configureKeybindings()
{
	KKeyDialog::configure( actionCollection() );
}

void MainWindow::findOrphanedFiles()
{
	OrphanedFilesDialog dlg( this );
	dlg.exec();
}

void MainWindow::configureNotifications()
{
	KNotifyDialog::configure( this );
}

void MainWindow::portSelected( const Port &port )
{
	m_port = port;
	const bool installed = port.installed();
	actionCollection()->action( "port_install" )->setEnabled( !installed );
	actionCollection()->action( "port_upgrade" )->setEnabled( installed );
	actionCollection()->action( "port_deinstall" )->setEnabled( installed );
	actionCollection()->action( "view_webcvs" )->setEnabled( true );
}

void MainWindow::installPort()
{
	kdDebug() << "MainWindow::installPort()" << endl;
}

void MainWindow::upgradePort()
{
	kdDebug() << "MainWindow::upgradePort()" << endl;
}

void MainWindow::deinstallPort()
{
	kdDebug() << "MainWindow::deinstallPort()" << endl;
}

void MainWindow::viewWebCVS()
{
	KConfig *cfg = kapp->config();
	cfg->setGroup( "General" );
	const QString mirror = cfg->readEntry( QString::fromLatin1( "CVSweb mirror" ), QString::fromLatin1( "http://www.FreeBSD.org/cgi/cvsweb.cgi") );

	( void )new KRun( QString::fromLatin1( "%1/ports/%2/" )
	                  .arg( mirror )
	                  .arg( m_port.origin() ) );
}

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