summaryrefslogtreecommitdiffstats
path: root/buildtools/autotools/addfiledlg.cpp
blob: 465290865fbdb16fb4a718b0b20bf796820e0d96 (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                             *
 *   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 "addfiledlg.h"

#include <qcheckbox.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtextstream.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,
							QWidget *parent, const char *name)
	: AddFileDlgBase(parent, name, true)
{
	connect ( createButton, SIGNAL ( clicked() ), this, SLOT ( accept() ) );
	connect ( cancelButton, SIGNAL ( clicked() ), this, 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()
{
	QString name = fileEdit->text();
	if (name.find('/') != -1) {
		KMessageBox::sorry(this, i18n("Please enter the file name without '/' and so on."));
		return;
	}

	QListViewItem *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()) {
		QString srcdir = m_part->projectDirectory();
		QString destdir = subProject->path;
		QString destpath = destdir + "/" + name;
		if (QFileInfo(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, QFileInfo(name).extension(), destpath) )
		    kdDebug(9020) << "cannot create file " << destpath << endl;
	} else {
		// create an empty file
		QString srcdir = m_part->projectDirectory();
		QString destdir = subProject->path;
		QString destpath = destdir + "/" + name;

		if (QFileInfo(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;
		}

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

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

	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;
	subProject->variables[varname] += (" " + name);

	QMap<QString,QString> 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 ) );

	QDialog::accept();
}

#include "addfiledlg.moc"