summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/addsubprojectdlg.cpp
blob: 8011886b533c80f554dea6a803594bc3bdb12780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
 *   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 <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qstringlist.h>
#include <qtextstream.h>
#include <kbuttonbox.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <kmessagebox.h>

#include "autolistviewitems.h"

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


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

    connect( createButton, SIGNAL(clicked()), this, SLOT(accept()) );
    connect( cancelButton, SIGNAL(clicked()), this, SLOT(reject()) );

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


AddSubprojectDialog::~AddSubprojectDialog()
{}


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

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

    QListViewItem *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( !QFileInfo(m_part->projectDirectory(), "config.status").exists() ){
        KMessageBox::sorry(this, i18n("There is no config.status in the project root directory. Run 'Configure' first"));
        QDialog::accept();
	return;
    }
#endif

    QDir      dir( m_subProject->path );
    QFileInfo file( dir, name );

    if( file.exists() && !file.isDir() ) {
        KMessageBox::sorry(this, i18n("A file named %1 already exists.").arg(name));
        QDialog::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 ){
            QDialog::accept();
            return;
	}
    } else if (!dir.mkdir(name)) {
        KMessageBox::sorry(this, i18n("Could not create subdirectory %1.").arg(name));
        QDialog::accept();
        return;
    }

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

    // Adjust SUBDIRS variable in containing Makefile.am
    if (m_subProject->variables["SUBDIRS"].find("$(TOPSUBDIRS)") != -1)
    {
        QFile subdirsfile( m_subProject->path + "/subdirs" );
        if ( subdirsfile.open( IO_WriteOnly | IO_Append ) )
        {
            QTextStream subdirsstream( &subdirsfile );
            subdirsstream << name << endl;
            subdirsfile.close();
        }
    }
    else if (m_subProject->variables["SUBDIRS"].find("$(AUTODIRS)") != -1)
    {
    }
    else
    {
        m_subProject->variables["SUBDIRS"] += (" " + name);
        QMap<QString,QString> 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
    QListViewItem *lastItem = m_subProject->firstChild();
    while (lastItem->nextSibling())
        lastItem = lastItem->nextSibling();
    if (lastItem != newitem)
        newitem->moveItem(lastItem);

    // Create a Makefile in the new subdirectory

    QFile 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;
        }
        QTextStream 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() ) {
        QString projroot = m_part->projectDirectory() + "/";
        QString subdirectory = dir.path();
        QString relpath = subdirectory.replace(0, projroot.length(),"");

        QString configureFile = m_part->getAutoConfFile(projroot);

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

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

    QString cmdline = "cd ";
    cmdline += KProcess::quote(m_part->projectDirectory());
    cmdline += " && automake ";
    cmdline += KProcess::quote(relmakefile);
    cmdline += " && CONFIG_HEADERS=config.h CONFIG_FILES=";
    cmdline += KProcess::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();

    QDialog::accept();
}

#include "addsubprojectdlg.moc"