summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/addsubprojectdlg.cpp
blob: ef61cd3af566a88eebe99b80be1b62dfc579f4df (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@tdevelop.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, TQT_SIGNAL(clicked()), this, TQT_SLOT(accept()) );
    connect( cancelButton, TQT_SIGNAL(clicked()), this, TQT_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"