summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/addfiledlg.cpp
blob: 407e510985dbce5ba0ae5409853d96da3552f61e (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
/***************************************************************************
 *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
 *   [email protected]                                                    *
 *                                                                         *
 *   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 "addfiledlg.h"

#include <tqcheckbox.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqtextstream.h>

#include <kbuttonbox.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klineedit.h>
#include <kmessagebox.h>
#include <ksqueezedtextlabel.h>
#include <kurl.h>

#include "autolistviewitems.h"

#include "filetemplate.h"
#include "misc.h"
#include "urlutil.h"
#include "autoprojectpart.h"
#include "autoprojectwidget.h"

#include "kdevpartcontroller.h"

AddFileDialog::AddFileDialog(AutoProjectPart *part, AutoProjectWidget *widget,
							SubprojectItem *spitem, TargetItem *item,
							TQWidget *tqparent, const char *name)
	: AddFileDlgBase(tqparent, name, true)
{
	connect ( createButton, TQT_SIGNAL ( clicked() ), this, TQT_SLOT ( accept() ) );
	connect ( cancelButton, TQT_SIGNAL ( clicked() ), this, TQT_SLOT ( reject() ) );

	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 );

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

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


AddFileDialog::~AddFileDialog()
{}


void AddFileDialog::accept()
{
	TQString name = fileEdit->text();
	if (name.tqfind('/') != -1) {
		KMessageBox::sorry(this, i18n("Please enter the file name without '/' and so on."));
		return;
	}

	TQListViewItem *child = target->firstChild();
	while (child) {
		FileItem *item = static_cast<FileItem*>(child);
		if (name == item->name) {
			KMessageBox::sorry(this, i18n("This file is already in the target."));
			return;
		}
		child = child->nextSibling();
	}

	if (templateCheckBox->isChecked()) {
		TQString srcdir = m_part->projectDirectory();
		TQString destdir = subProject->path;
		TQString destpath = destdir + "/" + name;
		if (TQFileInfo(destpath).exists()) {
			KMessageBox::sorry(this, i18n("<b>A file with this name already exists.</b><br><br>Please use the \"Add existing file\" dialog."));
			return;
		}
		if( !FileTemplate::copy(m_part, TQFileInfo(name).extension(), destpath) )
		    kdDebug(9020) << "cannot create file " << destpath << endl;
	} else {
		// create an empty file
		TQString srcdir = m_part->projectDirectory();
		TQString destdir = subProject->path;
		TQString destpath = destdir + "/" + name;

		if (TQFileInfo(destpath).exists()) {
			KMessageBox::sorry(this, i18n("<b>A file with this name already exists.</b><br><br>Please use the \"Add existing file\" dialog."));
			return;
		}

		TQFile f( destpath );
		if( f.open(IO_WriteOnly) )
		    f.close();
	}

	FileItem *fitem = m_widget->createFileItem(name, subProject);
	target->sources.append(fitem);
	target->insertItem(fitem);

	TQString canontargetname = AutoProjectTool::canonicalize(target->name);
	TQString varname;
        if( target->primary == "PROGRAMS" || target->primary == "LIBRARIES" || target->primary == "LTLIBRARIES" )
            varname = canontargetname + "_SOURCES";
        else
            varname = target->prefix + "_" + target->primary;
	subProject->variables[varname] += (" " + name);

	TQMap<TQString,TQString> replaceMap;
	replaceMap.insert(varname, subProject->variables[varname]);

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

	m_widget->emitAddedFile( subProject->path.mid ( m_part->project()->projectDirectory().length() + 1 ) + "/" + name );
	m_part->partController()->editDocument ( KURL ( subProject->path + "/" + name ) );

	TQDialog::accept();
}

#include "addfiledlg.moc"