/***************************************************************************
 *   Copyright (C) 2001 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 "addsubprojectdlg.h"

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqstringlist.h>
#include <tqtextstream.h>
#include <kbuttonbox.h>
#include <kdebug.h>
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <tdemessagebox.h>

#include "autolistviewitems.h"

#include "kdevmakefrontend.h"
#include "misc.h"
#include "autoprojectpart.h"
#include "autosubprojectview.h"


AddSubprojectDialog::AddSubprojectDialog(AutoProjectPart *part, AutoSubprojectView *view,
                                         SubprojectItem *item, TQWidget *parent, const char *name)
    : AddSubprojectDlgBase(parent, name, true)
{
    setIcon(SmallIcon("folder_new.png"));

    connect( createButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(accept()) );
    connect( cancelButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(reject()) );

    m_subProject = item;
    m_subprojectView = view;
    m_part = part;
}


AddSubprojectDialog::~AddSubprojectDialog()
{}


void AddSubprojectDialog::accept()
{
    TQString name = spEdit->text().stripWhiteSpace();

    if (name.isEmpty()) {
        KMessageBox::sorry(this, i18n("You have to give the subproject a name."));
        return;
    }

    TQListViewItem *childItem = m_subProject->firstChild();
    while (childItem) {
        if (name == static_cast<SubprojectItem*>(childItem)->subdir) {
            KMessageBox::sorry(this, i18n("A subproject with this name already exists."));
            return;
        }
        childItem = childItem->nextSibling();
    }


#if 0
    // check for config.status
    if( !TQFileInfo(m_part->projectDirectory(), "config.status").exists() ){
        KMessageBox::sorry(this, i18n("There is no config.status in the project root directory. Run 'Configure' first"));
        TQDialog::accept();
	return;
    }
#endif

    TQDir      dir( m_subProject->path );
    TQFileInfo file( dir, name );

    if( file.exists() && !file.isDir() ) {
        KMessageBox::sorry(this, i18n("A file named %1 already exists.").arg(name));
        TQDialog::accept();
        return;
    } else if( file.isDir() ) {
        if( KMessageBox::warningContinueCancel(this,
               i18n("A subdirectory %1 already exists. "
                    "Do you wish to add it as a subproject?").arg(name))
            == KMessageBox::Cancel ){
            TQDialog::accept();
            return;
	}
    } else if (!dir.mkdir(name)) {
        KMessageBox::sorry(this, i18n("Could not create subdirectory %1.").arg(name));
        TQDialog::accept();
        return;
    }

    if(!dir.cd(name)) {
       KMessageBox::sorry(this, i18n("Could not access the subdirectory %1.").arg(name));
       TQDialog::accept();
       return;
    }

    // Adjust SUBDIRS variable in containing Makefile.am
    if (m_subProject->variables["SUBDIRS"].find("$(TOPSUBDIRS)") != -1)
    {
        TQFile subdirsfile( m_subProject->path + "/subdirs" );
        if ( subdirsfile.open( IO_WriteOnly | IO_Append ) )
        {
            TQTextStream subdirsstream( &subdirsfile );
            subdirsstream << name << endl;
            subdirsfile.close();
        }
    }
    else if (m_subProject->variables["SUBDIRS"].find("$(AUTODIRS)") != -1)
    {
    }
    else
    {
        m_subProject->variables["SUBDIRS"] += (" " + name);
        TQMap<TQString,TQString> replaceMap;
        replaceMap.insert("SUBDIRS", m_subProject->variables["SUBDIRS"]);
        AutoProjectTool::addToMakefileam(m_subProject->path + "/Makefile.am", replaceMap);
    }

    // Create new item in tree view
    SubprojectItem *newitem = new SubprojectItem(m_subProject, name);
    newitem->subdir = name;
    newitem->path = m_subProject->path + "/" + name;
    newitem->variables["INCLUDES"] = m_subProject->variables["INCLUDES"];
    newitem->setOpen(true);

    // Move to the bottom of the list
    TQListViewItem *lastItem = m_subProject->firstChild();
    while (lastItem->nextSibling())
        lastItem = lastItem->nextSibling();
    if (lastItem != newitem)
        newitem->moveItem(lastItem);

    // Create a Makefile in the new subdirectory

    TQFile f( dir.filePath("Makefile.am") );
    if (f.exists()) {
        m_subprojectView->parse( newitem );
    } else {
        if (!f.open(IO_WriteOnly)) {
            KMessageBox::sorry(this, i18n("Could not create Makefile.am in subdirectory %1.").arg(name));
            return;
        }
        TQTextStream stream(&f);
        stream << "INCLUDES = " << newitem->variables["INCLUDES"] << endl << "METASOURCES = AUTO" << endl;
        f.close();
    }



    // if !isKDE: add the new sub-proj to configure.in
    if ( !m_part->isKDE() ) {
        TQString projroot = m_part->projectDirectory() + "/";
        TQString subdirectory = dir.path();
        TQString relpath = subdirectory.replace(0, projroot.length(),"");

        TQString configureFile = m_part->getAutoConfFile(projroot);

        TQStringList list = AutoProjectTool::configureinLoadMakefiles(configureFile);
        if ( !list.isEmpty() )
        {
            list.push_back( relpath + "/Makefile" );
            AutoProjectTool::configureinSaveMakefiles(configureFile, list);
        }
    }

#if 0
    TQString relmakefile = (m_subProject->path + "/" + name + "/Makefile").mid(m_part->projectDirectory().length()+1);
    kdDebug(9020) << "Relative makefile path: " << relmakefile << endl;

    TQString cmdline = "cd ";
    cmdline += TDEProcess::quote(m_part->projectDirectory());
    cmdline += " && automake ";
    cmdline += TDEProcess::quote(relmakefile);
    cmdline += " && CONFIG_HEADERS=config.h CONFIG_FILES=";
    cmdline += TDEProcess::quote(relmakefile);
    cmdline += " ./config.status";

    m_part->makeFrontend()->queueCommand( m_part->projectDirectory(), cmdline );
    m_part->makeFrontend()->queueCommand( m_part->projectDirectory(), m_part->configureCommand() );
#endif

    m_part->needMakefileCvs();

    TQDialog::accept();
}

#include "addsubprojectdlg.moc"
