 /***************************************************************************
   This file is part of LinTV
   lintvtools.cpp
   Some miscellaneous tools for LinTV.
   Code taken from K9Copy source code in file k9tools.h.

   Author: Jean-Michel PETIT <k9copy@free.fr>, (C) 2007, revised 
      for use in LinTV by Jesse L. Zamora <xtremek2008@aim.com>

   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.
 ***************************************************************************/

/* LinTV Includes */
#include "lintvtools.h"

/* System Includes */
#ifdef __FreeBSD__
  #include <sys.mount.h>
#else
  #include <sys/vfs.h>
#endif

/* Qt Includes */
#include <QDir>
#include <QMap>

/* KDE Includes */
#include <kstandarddirs.h>
#include <kservice.h>
#include <kfileitem.h>
#include <kdesu/su.h>
#include <kmimetypetrader.h>
#include <kmessagebox.h>
#include <kapplication.h>
#include <kdialog.h>
#include <kmacroexpander.h>

bool LinTVTools::checkProgram(QString programName, bool msg)
{
    bool res = KStandardDirs::findExe(programName, NULL, false) != NULL;
    if (msg && !res) {
        KMessageBox::error(kapp->activeWindow(), i18n("Could not find the %1 executable. " 
                                  "Please compile or install the program to run LinTV.",
                                                                             programName));
	// run a dialog showing the error
    }
    return res;
}

QString LinTVTools::fullFilePath(QString path)
{
    QString fullPath;
    if (path.contains("~")) {
        fullPath = path.replace("~", QDir::homePath());
    } else {
        fullPath = path;
    }

    return fullPath;
}

QMap<QString, KService::Ptr> LinTVTools::serviceForName(const KFileItem &item)
{
    QMap<QString, KService::Ptr> serviceForName;
    const QString mimeType = item.mimetype();

    QString constraint;
    constraint = QString("'%1' in ServiceTypes").arg(mimeType);

    KService::List services = KMimeTypeTrader::self()->query(mimeType, "Application",
                                                             constraint);

    // Update map
    serviceForName.clear();
    foreach (const KService::Ptr &service, services) {
        serviceForName[service->name()] = service;
    }

    return serviceForName;
}


QString LinTVTools::duration(const QTime & start, const QTime &end)
{
    int hours, minutes;
    int seconds;

    // Get the duration in seconds and convert it to minutes...
    seconds = start.secsTo(end);

    minutes = seconds / 60;

    // Now if the minutes are more than
    if (minutes >= 60) {
        hours = minutes / 60;
        minutes -= (hours * 60);

        return QString("%1 hr %2 min").arg(hours).arg(minutes);
    }
    return QString("%1 min").arg(minutes);
}

QString LinTVTools::loadStyleSheet(const QString& stylesheet, const QString & picsDir)
{
    QFile file(stylesheet);
    
    QString str;
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        str = QString::fromUtf8(file.readAll());
        QHash<QString, QString> macros;
        macros["picsDir"] = picsDir;
        return KMacroExpander::expandMacros(str, macros, QLatin1Char('$'));
    }

    return QString("Could not find file!");
}
