summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/removefiledlg.cpp
blob: 2c3bc37fe44147cf913d6bff258b81dcf0357f2b (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
/***************************************************************************
 *   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 "removefiledlg.h"

#include <qcheckbox.h>
#include <qgroupbox.h>
#include <qfile.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qregexp.h>

#include <kbuttonbox.h>
#include <kdebug.h>
#include <kdialog.h>
#include <ksqueezedtextlabel.h>

#include "autolistviewitems.h"

#include "misc.h"
#include "autoprojectpart.h"
#include "autoprojectwidget.h"
#include "autodetailsview.h"

static bool fileListContains(const QPtrList<FileItem> &list, const QString &name)
{
	QPtrListIterator<FileItem> it(list);
	for (; it.current(); ++it)
		if ((*it)->text(0) == name)
			return true;
	return false;
}


RemoveFileDialog::RemoveFileDialog(AutoProjectWidget *widget, AutoProjectPart* part, SubprojectItem *spitem,
								TargetItem *item, const QString &filename,
								QWidget *parent, const char *name)
	: RemoveFileDlgBase(parent, name, true)
{
	removeFromTargetsCheckBox = 0;

	QStringList targets;

	QPtrListIterator<TargetItem> it(spitem->targets);
	for (; it.current(); ++it)
		if (fileListContains((*it)->sources, filename))
			targets.append((*it)->name);

	if (targets.count() > 1)
	{
		removeFromTargetsCheckBox = new QCheckBox( fileGroupBox, "removeFromTargetsCheckBox" );
		removeFromTargetsCheckBox->setMinimumSize( QSize( 0, 45 ) );
		fileLayout->addWidget( removeFromTargetsCheckBox );

		QString joinedtargets = "    *" + targets.join("\n    *");
		removeFromTargetsCheckBox->setText ( i18n ( "The file %1 is still used by the following targets:\n%2\n"
												"Remove it from all of them?").arg(filename).arg(joinedtargets) );
        setMinimumSize(QSize(size().width(), size().height() + removeFromTargetsCheckBox->size().height()*2) );
	}

	removeLabel->setText ( i18n ( "Do you really want to remove <b>%1</b>?" ).arg ( filename ) );

	directoryLabel->setText ( spitem->path );
	if ( item->name.isEmpty() )
		targetLabel->setText ( i18n ( "%1 in %2" ).arg ( item->primary ).arg ( item->prefix ) );
	else
		targetLabel->setText ( item->name );

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

	setIcon ( SmallIcon ( "editdelete.png" ) );

	m_widget = widget;
	m_part = part;
	subProject = spitem;
	target = item;
	fileName = filename;
}


RemoveFileDialog::~RemoveFileDialog()
{}


void RemoveFileDialog::accept()
{
	m_widget->emitRemovedFile ( subProject->path.mid ( m_part->projectDirectory().length() + 1 ) + "/" + fileName );

	QMap<QString,QString> replaceMap;

	if (removeFromTargetsCheckBox && removeFromTargetsCheckBox->isChecked()) {
		QPtrListIterator<TargetItem> it(subProject->targets);
		for (; it.current(); ++it) {
			if ((*it) != target && fileListContains((*it)->sources, fileName)) {
				FileItem *fitem = static_cast<FileItem*>((*it)->firstChild());
				while (fitem) {
					FileItem *nextitem = static_cast<FileItem*>(fitem->nextSibling());
					if (fitem->text(0) == fileName) {
						QListView *lv = fitem->listView();
						lv->setSelected(fitem, false);
						(*it)->sources.remove(fitem);
					}
					fitem = nextitem;
				}
				QString canontargetname = AutoProjectTool::canonicalize((*it)->name);
                                QString varname;
                                if( (*it)->primary == "PROGRAMS" || (*it)->primary == "LIBRARIES" || (*it)->primary == "LTLIBRARIES" )
                                    varname = canontargetname + "_SOURCES";
                                else
                                    varname = (*it)->prefix + "_" + (*it)->primary;
				QStringList sources = QStringList::split(QRegExp("[ \t\n]"), subProject->variables[varname]);
				sources.remove(fileName);
				subProject->variables[varname] = sources.join(" ");
				replaceMap.insert(varname, fileName);
			}
		}
	}

	QString fileItemName;
	FileItem *fitem = static_cast<FileItem*>(target->firstChild());
	while (fitem) {
		if (fitem->text(0) == fileName) {
			QListView *lv = fitem->listView();
			lv->setSelected(fitem, false);
			fileItemName = fitem->name;
			target->sources.remove(fitem);
			break;
		}
		fitem = static_cast<FileItem*>(fitem->nextSibling());
	}
	QString canontargetname = AutoProjectTool::canonicalize(target->name);
	QString varname;
	if( target->primary == "PROGRAMS" || target->primary == "LIBRARIES" || target->primary == "LTLIBRARIES" )
	    varname = canontargetname + "_SOURCES";
	else
	    varname = target->prefix + "_" + target->primary;
	QStringList sources = QStringList::split(QRegExp("[ \t\n]"), subProject->variables[varname]);
	sources.remove(fileName);
	subProject->variables[varname] = sources.join(" ");
	replaceMap.insert(varname, fileName);

	AutoProjectTool::removeFromMakefileam(subProject->path + "/Makefile.am", replaceMap);

//  review configuration cleanup in the project file after removing subclassing related source
	QDomDocument &dom = *(m_part->projectDom());

	QDomElement el = dom.documentElement();
	QDomNode el2 = el.namedItem("kdevautoproject");
	QDomNode el3 = el2.namedItem("subclassing");

	QDomNode n = el3.firstChild();
	QValueList<QDomNode> nodesToRemove;
	while ( !n.isNull() ) {
		QDomNamedNodeMap attr = n.attributes();
		QString fpath = subProject->path + QString("/") + fileItemName;
		QString relpath = fpath.remove(0, m_part->projectDirectory().length());
		if ((attr.item(0).nodeValue() == relpath)
			|| (attr.item(1).nodeValue() == relpath) )
			nodesToRemove.append(n);
		n = n.nextSibling();
	}
	QValueList<QDomNode>::iterator it;
	for ( it = nodesToRemove.begin(); it != nodesToRemove.end(); ++it )
		el3.removeChild(*it);

	if (removeCheckBox->isChecked())
		QFile::remove(subProject->path + "/" + fileName);

	QDialog::accept();
}

#include "removefiledlg.moc"