/***************************************************************************
 *   Copyright (C) 2005 by Michael Goettsche                               *
 *   michael.goettsche@kdemail.net                                         *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <klocale.h>
#include <kdebug.h>
#include <kservice.h>
#include <kdesktopfile.h>
#include <kiconloader.h>
#include <kprocess.h>
#include <kapplication.h>

#include <qiconview.h>
#include <qpushbutton.h>

#include "kedulaunch.h"
#include "kedulaunchbase.h"


AppIconViewItem::AppIconViewItem(QIconView* parent, QString name, QStringList categories, 
                                 QString exec, QString icon, QString genericName)
                :QIconViewItem(parent)
{
    m_name = name;
    m_categories = categories;
    m_exec = exec;
    m_icon = icon;
    m_genericName = genericName;
    
    setPixmap(DesktopIcon(icon, 64));
    
    setText(name + " - \n" + genericName);
} 

const QString& AppIconViewItem::name()
{
    return m_name;
}

const QStringList& AppIconViewItem::categories()
{
    return m_categories;
}

const QString& AppIconViewItem::exec()
{
    return m_exec;
}


KEduLaunch::KEduLaunch(QWidget* parent, const char* name)
    : KDialogBase(parent, name, false, "KEduLaunch", 0)
{
    m_mainWidget = new KEduLaunchBase(this);
    setMainWidget(m_mainWidget);

    resize(900, 700);
    
    connect(m_mainWidget->categoryIconView, SIGNAL(clicked(QIconViewItem*)), this, SLOT(itemClicked(QIconViewItem*)));
    connect(m_mainWidget->startAppButton, SIGNAL(clicked()), this, SLOT(startAppClicked()));
    connect(m_mainWidget->closeButton, SIGNAL(clicked()), KApplication::kApplication(), SLOT(quit()));
    
    retrieveAppList();
    populateCategoryView();
    populateIconView();    
}

KEduLaunch::~KEduLaunch()
{
}

void KEduLaunch::populateCategoryView()
{
    KIconLoader iconLoader;
    m_allItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("All"));
    m_allItem->setPixmap(iconLoader.loadIcon("exec", KIcon::NoGroup, 64));
    m_mathsItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("Maths"));
    m_mathsItem->setPixmap(iconLoader.loadIcon("edu_mathematics", KIcon::NoGroup, 64));
    m_languagesItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("Languages"));
    m_languagesItem->setPixmap(iconLoader.loadIcon("edu_languages", KIcon::NoGroup, 64));
    m_scienceItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("Science"));
    m_scienceItem->setPixmap(iconLoader.loadIcon("edu_science", KIcon::NoGroup, 64));
    m_miscellaneousItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("Miscellaneous"));
    m_miscellaneousItem->setPixmap(iconLoader.loadIcon("edu_miscellaneous", KIcon::NoGroup, 64));
    m_teachingItem = new QIconViewItem(m_mainWidget->categoryIconView, i18n("Teaching"));
    m_teachingItem->setPixmap(iconLoader.loadIcon("package", KIcon::NoGroup, 64));
}

void KEduLaunch::retrieveAppList()
{
    availableApps = KTrader::self()->query("Application", "(Type == 'Application') and ('Education' in Categories) and ('KDE' in Categories)");
}

void KEduLaunch::populateIconView()
{
    m_mainWidget->appsIconView->clear();
    for(QValueList<KService::Ptr>::Iterator it = availableApps.begin(); it != availableApps.end(); ++it) {
        AppIconViewItem* item = new AppIconViewItem(m_mainWidget->appsIconView, (*it)->name(),
                                 (*it)->categories(), (*it)->exec(), (*it)->icon(), (*it)->genericName());
        kdDebug() << (*it)->name() << endl;
    }
}

void KEduLaunch::itemClicked(QIconViewItem* item)
{
    //Yes, QIconView sucks.
    
    if (item) {
        QString filter = QString::null;
        if (item == m_allItem)
            filter = "Education";
        else if (item == m_mathsItem)
            filter = "Math";
        else if (item == m_languagesItem)
            filter = "X-KDE-Edu-Language";
        else if (item == m_scienceItem)
            filter = "Science";
        else if (item == m_teachingItem)
            filter = "X-KDE-Edu-Teaching";

        QValueList<AppIconViewItem*> toDelete;  
    
        populateIconView();
    
        for (QIconViewItem* it = m_mainWidget->appsIconView->firstItem(); it; it = it->nextItem()) {
            AppIconViewItem* it2 = (AppIconViewItem*) it;
            if (!filter == QString::null) {
                if (!it2->categories().contains(filter)) {
                    toDelete.append(it2);
                }
            } else {
                if (it2->categories().contains("Math") || 
                    it2->categories().contains("X-KDE-Edu-Language") ||
                    it2->categories().contains("Science") ||
                    it2->categories().contains("X-KDE-Edu-Teaching")) {
                        toDelete.append(it2);
                    }
                }                
        }
    
        for (QValueList<AppIconViewItem*>::Iterator it = toDelete.begin(); it != toDelete.end(); ++it) {
            delete *it;
        }

        m_mainWidget->appsIconView->sort();
    }
}

void KEduLaunch::startAppClicked()
{
    AppIconViewItem* currentItem = (AppIconViewItem*) m_mainWidget->appsIconView->currentItem();
    QString exec = currentItem->exec();
    exec = exec.left(exec.find(" "));
    
    KProcess* process = new KProcess;
    *process << exec;
    kdDebug() << exec << endl;
    process->start();
}
    

#include "kedulaunch.moc"
