/***************************************************************************
 *   Copyright (C) 2002 by Bernd Gehrmann                                  *
 *   bernd@kdevelop.org                                                    *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "addicondlg.h"

#include <tqcombobox.h>
#include <tqlabel.h>
#include <klineedit.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <kprocess.h>
#include <kstandarddirs.h>
#include <kicontheme.h>
#include <tdeversion.h> // fix me!

#include "autolistviewitems.h"

#include "autoprojectpart.h"
#include "autoprojectwidget.h"


const char *type_map[] = {
    "app", "action", "device", "filesys", "mime"
};


const char *size_map[] = {
    "hi16", "hi22", "hi32", "hi48", "hi64", "hi128"
};


AddIconDialog::AddIconDialog(AutoProjectPart *part, AutoProjectWidget *widget,
                             SubprojectItem *spitem, TargetItem *titem,
                             TQWidget *parent, const char *name)
    : AddIconDialogBase(parent, name, true)
{
    type_combo->insertItem(i18n("Application"));
    type_combo->insertItem(i18n("Action"));
    type_combo->insertItem(i18n("Device"));
    type_combo->insertItem(i18n("File System"));
    type_combo->insertItem(i18n("MIME Type"));

    size_combo->insertItem("16");
    size_combo->insertItem("22");
    size_combo->insertItem("32");
    size_combo->insertItem("48");
    size_combo->insertItem("64");
    size_combo->insertItem("128");

    somethingChanged();

    setIcon ( SmallIcon ( "iconadd_tdevelop" ) );

    m_part = part;
    m_widget = widget;
    m_subProject = spitem;
    m_target = titem;
}


AddIconDialog::~AddIconDialog()
{}


void AddIconDialog::somethingChanged()
{
    TQString size = size_map[size_combo->currentItem()];
    TQString type = type_map[type_combo->currentItem()];
    TQString name = name_edit->text();

    filename_edit->setText(size + "-" + type + "-" + name + ".png");
}


void AddIconDialog::accept()
{
    TQString name = filename_edit->text();
    
    TQString destdir = m_subProject->subdir;
    TQString destpath = destdir + "/" + name;

    TQString size = size_combo->currentText();
    TQString unknown = TDEIconTheme::defaultThemeName()+ "/" + size + "x" + size + "/mimetypes/unknown.png";

    TQString templateFileName = locate("icon", unknown);
    kdDebug(9020) << "Unknown: " << unknown << ", template: " << templateFileName << endl;

    if (!templateFileName.isEmpty()) {
        TDEProcess proc;
        proc << "cp";
        proc << templateFileName;
        proc << destpath;
        proc.start(TDEProcess::DontCare);
    }
    
    FileItem *fitem = m_widget->createFileItem(name, m_subProject);
    m_target->sources.append(fitem);
    m_target->insertItem(fitem);

    m_part->startMakeCommand(destdir, TQString::fromLatin1("force-reedit"));
	
	m_widget->emitAddedFile(destpath);

    TQDialog::accept();
}

#include "addicondlg.moc"
