summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/choosetargetdialog.cpp
blob: fc67a82ee14e9cccbecd90480d52fd31637ca3fe (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***************************************************************************
                             -------------------
    begin                : 29.11.2002
    copyright            : (C) 2002 by Victor Rder
    email                : victor_roeder@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "choosetargetdialog.h"

#include <tqcheckbox.h>
#include <tqwidget.h>
#include <tqgroupbox.h>
#include <tqheader.h>
#include <tqlistview.h>
#include <tqptrlist.h>
#include <tqradiobutton.h>
#include <tqstringlist.h>
#include <tqfileinfo.h>

#include <kcombobox.h>
#include <kdialog.h>
#include <kdebug.h>
#include <tdelistview.h>
#include <tdemessagebox.h>
#include <kprocess.h>
#include <ksqueezedtextlabel.h>
#include <kurl.h>

#include "choosetargetdlgbase.h"
#include "autodetailsview.h"
#include "autolistviewitems.h"
#include "autosubprojectview.h"
#include "misc.h"
#include "autoprojectwidget.h"
#include "autoprojectpart.h"

#include "kdevpartcontroller.h"

class ChooseTargetDialog::Private
{
public:
	AutoProjectWidget* widget;
	AutoProjectPart* part;
	TQStringList fileList;
	TQPtrList<SubprojectItem> subprojectList;
	SubprojectItem* chosenSubproject;
	TargetItem* chosenTarget;
	ChooseTargetDlgBase* baseUI;
};

ChooseTargetDialog::ChooseTargetDialog ( AutoProjectWidget* widget, AutoProjectPart* part,
                                         TQStringList fileList, TQWidget* parent, const char* name )
: KDialogBase( parent, name, false, i18n("Automake Manager - Choose Target"),
               Ok | Cancel, KDialogBase::Ok, true /* seperator */ )

{
	Q_UNUSED( parent );
	Q_UNUSED( name );
	d = new ChooseTargetDialog::Private;
	d->widget = widget;
	d->part = part;
	d->fileList = fileList;
	d->subprojectList = widget->allSubprojectItems();
	d->baseUI = new ChooseTargetDlgBase( this, "base ui" );
	setMainWidget( d->baseUI );
	
	d->baseUI->subprojectComboBox->setAutoCompletion( true );
	d->baseUI->targetComboBox->setAutoCompletion( true );
	d->baseUI->newFileList->header()->hide();
	d->baseUI->newFileList->addColumn( TQString() );
	d->baseUI->newFileList->setSorting(-1);

	setIcon ( SmallIcon ( "target_tdevelop" ) );
	
	
	TQPtrListIterator<SubprojectItem> sit(d->subprojectList);
	for ( ; (*sit); ++sit )
	{
		TQPtrList<TargetItem> targetList = (*sit)->targets;
		TQPtrListIterator<TargetItem> targetIt(targetList);

		// Only insert Subproject which have a "regular" target
		for ( ; (*targetIt); ++targetIt )
		{
			TQString titemPrimary = (*targetIt)->primary;
			if ( titemPrimary == "PROGRAMS" || titemPrimary == "LIBRARIES" ||
			     titemPrimary == "LTLIBRARIES" || titemPrimary == "JAVA" )
			{
				d->baseUI->subprojectComboBox->insertItem ( SmallIcon ( "folder" ), (*sit)->subdir );
			}
		}
	}

	if ( d->widget->activeTarget() && d->widget->activeSubproject() )
	{
		d->chosenTarget = d->widget->activeTarget();
		//kdDebug ( 9020 ) << "1) Chosen target is " << d->chosenTarget->name << endl;
		d->chosenSubproject = widget->activeSubproject();
		d->baseUI->chosenTargetLabel->setText( ( widget->activeSubproject()->path + "/<b>" +
		                                         d->widget->activeTarget()->name + "</b>" )
		                                       .mid( d->part->projectDirectory().length() + 1 ) );
		d->baseUI->subprojectComboBox->setEnabled( false );
		d->baseUI->targetComboBox->setEnabled( false );

		d->baseUI->subprojectComboBox->setCurrentItem( widget->activeSubproject()->subdir );
		slotSubprojectChanged( widget->activeSubproject()->subdir );
	}
	else
	{
		d->baseUI->activeTargetRadioButton->toggle();
		d->baseUI->activeTargetRadioButton->setEnabled ( false );
		d->baseUI->neverAskAgainCheckbox->setEnabled ( false );

		slotSubprojectChanged ( d->baseUI->subprojectComboBox->text(0) );
	}

	TQStringList::iterator it;
	TQString fileName;

	for ( it = fileList.begin(); it != fileList.end(); ++it )
	{
		int pos = ( *it ).findRev('/');
		if (pos != -1)
			fileName = ( *it ).mid(pos+1);
		else
			fileName = ( *it );


		d->baseUI->newFileList->insertItem( new TQListViewItem( d->baseUI->newFileList, fileName ) );
	}
	
	connect ( d->baseUI->subprojectComboBox, TQT_SIGNAL ( activated ( const TQString& ) ),
	          this, TQT_SLOT( slotSubprojectChanged ( const TQString& ) ) );
	connect ( d->baseUI->targetComboBox, TQT_SIGNAL ( activated ( const TQString& ) ),
	          this, TQT_SLOT( slotTargetChanged ( const TQString& ) ) );
}


ChooseTargetDialog::~ChooseTargetDialog()
{
}

void ChooseTargetDialog::slotSubprojectChanged ( const TQString& name )
{
	d->chosenTarget = 0;
	SubprojectItem* spitem = d->subprojectList.first();

	for ( ; spitem; spitem = d->subprojectList.next() )
	{
		if ( spitem->subdir == name )
		{
			TQPtrList <TargetItem> targetList = spitem->targets;
			TargetItem* titem = targetList.first();

			d->baseUI->targetComboBox->clear();

/*			choosenSubprojectLabel->setText ( ( spitem->path + "<b>" + ->name + "</b>" ).mid ( d->widget->projectDirectory().length() + 1 ) );*/

			d->chosenSubproject = spitem;

			for ( ; titem; titem = targetList.next() )
			{
				if ( titem->primary == "PROGRAMS" || titem->primary == "LIBRARIES" ||
						titem->primary == "LTLIBRARIES" || titem->primary == "JAVA" )
				{
					d->baseUI->targetComboBox->insertItem ( SmallIcon ( "target_tdevelop" ), titem->name );

					//d->baseUI->targetComboBox->addToHistory ( titem->name );

					// if the Active Target is in the currently selected Subproject
					if ( d->widget->activeTarget() &&
						titem->name == d->widget->activeTarget()->name )
					{
						d->baseUI->targetComboBox->setCurrentItem( titem->name );
						d->baseUI->chosenTargetLabel->setText( ( spitem->path + "/<b>" + titem->name + "</b>" ).mid( d->part->projectDirectory().length() + 1 ) );
						d->chosenTarget = titem;
						//kdDebug ( 9020 ) << "2) Chosen target is " << d->chosenTarget->name << endl;
					}
					else
					{
						//d->baseUI->targetComboBox->setCurrentItem ( 0 );
						if ( !d->chosenTarget )
						{
							d->baseUI->chosenTargetLabel->setText( ( spitem->path + "/<b>" + titem->name + "</b>")
							                                       .mid( d->part->projectDirectory().length() + 1 ) );
							//choosenSubprojectLabel->setText ( ( spitem->path + "<b>" + titem->name + "</b>" ).mid ( d->widget->projectDirectory().length() + 1 ) );

							d->chosenTarget = titem;
						}
						//kdDebug ( 9020 ) << "2a) Chosen target is " << d->chosenTarget->name << endl;
					}
				}
			}

			break;
		}
	}
}

void ChooseTargetDialog::slotTargetChanged( const TQString& name )
{
	d->baseUI->chosenTargetLabel->setText( ( d->chosenSubproject->path + "/<b>" + name + "</b>" )
	                                        .mid( d->part->projectDirectory().length() + 1 ) );

	TQPtrList <TargetItem> targetList = d->chosenSubproject->targets;
	TargetItem* titem = targetList.first();

	for ( ; titem; titem = targetList.next() )
	{
		if ( titem->name == name )
		{
			d->chosenTarget = titem;
			//kdDebug ( 9020 ) << "4) Chosen target is " << d->chosenTarget->name << endl;

			break;
		}
	}
}

void ChooseTargetDialog::slotOk()
{
	if ( d->baseUI->activeTargetRadioButton->isChecked() )
	{
		d->chosenTarget = d->widget->activeTarget();
		d->chosenSubproject = d->widget->activeSubproject();
	}
	if ( !d->chosenSubproject || !d->chosenTarget )
		return;

	//kdDebug ( 9020 ) << "3) Chosen target is " << d->chosenTarget->name << endl;

	TQStringList newFileList;
	TQStringList::iterator it;
	TQString directory, fileName;

	for ( it = d->fileList.begin(); it != d->fileList.end(); ++it )
	{
		bool found = false;

		int pos = ( *it ).findRev('/');
		if (pos != -1)
		{
			directory = ( *it ).left(pos);
			fileName = ( *it ).mid(pos+1);
		}
		else
		{
			fileName = ( *it );
		}

		FileItem * fitem = d->chosenTarget->sources.first();
		for ( ; fitem; fitem = d->chosenTarget->sources.next() )
		{
			if ( fitem->name == fileName )
			{
				KMessageBox::error ( this, i18n ( "The file %1 already exists in the chosen target.\nThe file will be created but will not be added to the target.\n"
				                                  "Rename the file and select 'Add Existing Files' from the Automake Manager." ).arg ( fitem->name ),
				                     i18n ( "Error While Adding Files" ) );
				found = true;
			}
		}

		if ( !found )
		{
            /// \FIXME a quick hack to prevent adding header files to _SOURCES
            /// and display them in noinst_HEADERS
			if (AutoProjectPrivate::isHeader(fileName) &&
			    ( d->chosenTarget->primary == "PROGRAMS" || d->chosenTarget->primary == "LIBRARIES" ||  d->chosenTarget->primary == "LTLIBRARIES" ) )
			{
				kdDebug ( 9020 ) << "Ignoring header file and adding it to noinst_HEADERS: " << fileName << endl;
				TargetItem* noinst_HEADERS_item = d->widget->getSubprojectView()->findNoinstHeaders(d->chosenSubproject);
				FileItem *fitem = d->widget->createFileItem( fileName, d->chosenSubproject );
				noinst_HEADERS_item->sources.append( fitem );
				noinst_HEADERS_item->insertItem( fitem );
				
				TQString varname = "noinst_HEADERS";
				d->chosenSubproject->variables[ varname ] += ( " " + fileName );
				
				TQMap<TQString, TQString> replaceMap;
				replaceMap.insert( varname, d->chosenSubproject->variables[ varname ] );
				
				AutoProjectTool::addToMakefileam( d->chosenSubproject->path + "/Makefile.am", replaceMap );
			}
			else
			{
				fitem = d->widget->createFileItem( fileName,d->chosenSubproject );
				d->chosenTarget->sources.append( fitem );
				d->chosenTarget->insertItem( fitem );
				
				TQString canontargetname = AutoProjectTool::canonicalize( d->chosenTarget->name );
				TQString varname = canontargetname + "_SOURCES";
				d->chosenSubproject->variables[ varname ] += ( " " + fileName );
				
				TQMap<TQString, TQString> replaceMap;
				replaceMap.insert( varname, d->chosenSubproject->variables[ varname ] );
				
				AutoProjectTool::addToMakefileam( d->chosenSubproject->path + "/Makefile.am", replaceMap );
			}
			newFileList.append ( d->chosenSubproject->path.mid ( d->part->projectDirectory().length() + 1 ) + "/" + fileName );
		}

		if ( directory.isEmpty() || directory != d->chosenSubproject->subdir )
		{
			KShellProcess proc("/bin/sh");

			proc << "mv";
			proc << KShellProcess::quote( d->part->projectDirectory() + "/" + directory + "/" + fileName );
			proc << KShellProcess::quote( d->chosenSubproject->path + "/" + fileName );
			proc.start(TDEProcess::DontCare);
		}

        // why open the files?!
//		d->part->partController()->editDocument ( KURL ( d->chosenSubproject->path + "/" + fileName ) );

		found = false;
	}

	d->widget->emitAddedFiles( newFileList );
	KDialogBase::slotOk();
}

TargetItem* ChooseTargetDialog::chosenTarget()
{
	return d->chosenTarget;
}

SubprojectItem* ChooseTargetDialog::chosenSubproject()
{
	return d->chosenSubproject;
}

bool ChooseTargetDialog::alwaysUseActiveTarget() const
{
	return d->baseUI->neverAskAgainCheckbox->isChecked();
}


#include "choosetargetdialog.moc"