1
/***************************************************************************
2
* Copyright (C) 2001-2002 by Bernd Gehrmann *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
***************************************************************************/
12
#include "importdlg.h"
14
#include <qcombobox.h>
19
#include <qlineedit.h>
20
#include <qpushbutton.h>
22
#include <qtextstream.h>
24
#include <qcheckbox.h>
25
#include <kbuttonbox.h>
28
#include <kfiledialog.h>
29
#include <kinstance.h>
31
#include <kmessagebox.h>
32
#include <kstandarddirs.h>
35
#include <kurlrequester.h>
37
#include <kparts/componentfactory.h>
41
#include "kdevversioncontrol.h"
43
#include "appwizardfactory.h"
44
#include "appwizardpart.h"
48
ImportDialog::ImportDialog(AppWizardPart *part, QWidget *parent, const char *name)
49
: ImportDialogBase(parent, name, true), m_part(part)
51
QString author, email;
52
AppWizardUtil::guessAuthorAndEmail(&author, &email);
53
author_edit->setText(author);
54
email_edit->setText(email);
55
QToolTip::add( urlinput_edit->button(), i18n("Choose directory to import") );
56
urlinput_edit->setMode(KFile::Directory|KFile::ExistingOnly|KFile::LocalOnly);
58
KStandardDirs *dirs = AppWizardFactory::instance()->dirs();
59
importNames = dirs->findAllResources("appimports", QString::null, false, true);
62
QStringList::ConstIterator it;
63
for (it = importNames.begin(); it != importNames.end(); ++it) {
64
KConfig config(KGlobal::dirs()->findResource("appimports", *it));
65
config.setGroup("General");
66
QString type = config.readEntry("Comment");
67
project_combo->insertItem(type);
69
if (config.hasGroup("Infrastructure"))
71
config.setGroup("Infrastructure");
72
m_infrastructure[type].isOn = true;
73
m_infrastructure[type].comment = config.readEntry("Comment");
74
m_infrastructure[type].command = config.readEntry("Command");
75
m_infrastructure[type].existingPattern = config.readEntry("ExistingProjectPattern");
78
m_infrastructure[type].isOn = false;
81
infrastructureBox->setEnabled(false);
83
connect( name_edit, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotProjectNameChanged( const QString & ) ) );
84
// scanAvailableVCS();
85
connect( fetchModuleButton, SIGNAL(clicked()),
86
this, SLOT(slotFetchModulesFromRepository()) );
87
connect(urlinput_edit, SIGNAL(urlSelected(const QString& )), this, SLOT(dirChanged()));
88
slotProjectNameChanged( name_edit->text() );
92
ImportDialog::~ImportDialog()
95
void ImportDialog::slotProjectNameChanged( const QString &_text )
97
ok_button->setEnabled( !_text.isEmpty() );
100
void ImportDialog::accept()
102
QDir dir(urlinput_edit->url());
103
if (urlinput_edit->url().isEmpty() || !dir.exists()) {
104
KMessageBox::sorry(this, i18n("You have to choose a directory."));
108
QString projectName = name_edit->text();
109
if (projectName.isEmpty()) {
110
KMessageBox::sorry(this, i18n("You have to choose a project name."));
114
for (uint i=0; i < projectName.length(); ++i)
115
if (!projectName[i].isLetterOrNumber() && projectName[i] != '_') {
116
KMessageBox::sorry(this, i18n("Your application name should only contain letters and numbers."));
120
if (infrastructureBox->isVisible() && infrastructureBox->isChecked())
121
createProjectInfrastructure();
123
QString author = author_edit->text();
124
QString email = email_edit->text();
126
QFileInfo finfo(importNames[project_combo->currentItem()]);
127
QDir importdir(finfo.dir());
129
QFile src(importdir.filePath("importfiles/" + finfo.fileName() + ".kdevelop"));
130
kdDebug(9010) << "Import template " << src.name() << endl;
131
if (!src.open(IO_ReadOnly)) {
132
KMessageBox::sorry(this, i18n("Cannot open project template."));
136
QFile dest(dir.filePath(projectName + ".kdevelop"));
137
if (!dest.open(IO_WriteOnly)) {
138
KMessageBox::sorry(this, i18n("Cannot write the project file."));
142
QTextStream srcstream(&src);
143
QTextStream deststream(&dest);
145
while (!srcstream.atEnd()) {
146
QString line = srcstream.readLine();
147
line.replace(QRegExp("\\$APPNAMELC\\$"), projectName);
148
line.replace(QRegExp("\\$AUTHOR\\$"), author);
149
line.replace(QRegExp("\\$EMAIL\\$"), email);
150
deststream << line << endl;
156
m_part->core()->openProject(dir.filePath(projectName + ".kdevelop"));
158
kdDebug(9010) << "OPENING PROJECT: " << dir.filePath(projectName + ".kdevelop") << endl;
164
// Checks if the directory dir and all of its subdirectories
165
// (one level recursion) have files that follow patterns
166
// patterns is comma-separated
167
static bool dirHasFiles(QDir &dir, const QString &patterns)
169
QStringList::ConstIterator pit, sit;
171
QStringList patternList = QStringList::split(",", patterns);
172
for (pit = patternList.begin(); pit != patternList.end(); ++pit) {
173
if (!dir.entryList(*pit, QDir::Files).isEmpty()) {
174
kdDebug(9010) << "Has files " << (*pit) << endl;
179
QStringList subdirList = dir.entryList("*", QDir::Dirs);
180
for (sit = subdirList.begin(); sit != subdirList.end(); ++sit) {
183
for (pit = patternList.begin(); pit != patternList.end(); ++pit) {
184
if (!subdir.entryList(*pit, QDir::Files).isEmpty()) {
185
kdDebug(9010) << "Has files " << (*pit) << " in " << (*sit) << endl;
195
void ImportDialog::dirChanged()
197
kdDebug(9010) << "ImportDialog::dirChanged" << endl;
198
QString dirName = urlinput_edit->url();
203
// KDevelop legacy project?
204
QStringList files = dir.entryList("*.kdevprj");
205
if (!files.isEmpty()) {
206
scanLegacyKDevelopProject(dir.absFilePath(files.first()));
210
// Studio legacy project?
211
files = dir.entryList("*.studio");
212
if (!files.isEmpty()) {
213
scanLegacyStudioProject(dir.absFilePath(files.first()));
218
if (dir.exists("config.guess") || dir.exists("configure.in.in")) {
219
scanAutomakeProject(dirName);
223
name_edit->setText(dir.dirName());
226
files = dir.entryList("*.pro");
227
if (!files.isEmpty()) {
228
setProjectType("qtqmake");
233
if (dirHasFiles(dir, "*.cpp,*.c++,*.cxx,*.C,*.cc,*.ocl")) {
234
setProjectType("cpp");
239
if (dirHasFiles(dir, "*.f77,*.f,*.for,*.ftn")) {
240
setProjectType("fortran");
245
if (dirHasFiles(dir, "*.py")) {
246
setProjectType("python");
251
if (dirHasFiles(dir, "*.pl,*.pm")) {
252
setProjectType("perl");
258
void ImportDialog::scanLegacyKDevelopProject(const QString &fileName)
260
kdDebug(9010) << "Scanning legacy KDevelop project file " << fileName << endl;
262
KSimpleConfig config(fileName, true);
263
config.setGroup("General");
264
author_edit->setText(config.readEntry("author"));
265
email_edit->setText(config.readEntry("email"));
266
name_edit->setText(config.readEntry("project_name"));
268
QString legacyType = config.readEntry("project_type");
269
if (QStringList::split(",", "normal_kde,normal_kde2,kde2_normal,mdi_kde2").contains(legacyType))
270
setProjectType("kde");
271
else if (legacyType == "normal_gnome")
272
setProjectType("gnome");
273
else if (legacyType == "normal_empty")
274
setProjectType("cpp-auto");
276
setProjectType("cpp");
280
void ImportDialog::scanLegacyStudioProject(const QString &fileName)
282
kdDebug(9010) << "Scanning legacy studio project file " << fileName << endl;
284
// Not much to do here...
285
KSimpleConfig config(fileName, true);
286
config.setGroup("kdestudio");
287
name_edit->setText(config.readEntry("Name"));
291
void ImportDialog::scanAutomakeProject(const QString &dirName)
293
kdDebug(9010) << "Scanning automake project directory " << dirName << endl;
296
if (QFile::exists(dirName + "/admin/am_edit")) {
297
setProjectType("kde");
299
} else if (QFile::exists(dirName + "/macros/gnome.m4")) {
300
setProjectType("gnome");
303
setProjectType("c-auto");
306
QFile af(dirName + "/AUTHORS");
307
if (!af.open(IO_ReadOnly))
309
QTextStream astream(&af);
311
QRegExp authorre("(.*)<(.*)>");
312
while (!astream.atEnd()) {
313
QString s = astream.readLine();
314
if (authorre.search(s) != -1) {
315
author_edit->setText(authorre.cap(1).stripWhiteSpace());
316
email_edit->setText(authorre.cap(2).stripWhiteSpace());
322
QFile cf(dirName + "/configure.in");
323
if (!cf.open(IO_ReadOnly))
325
QTextStream cstream(&cf);
327
QRegExp namere("\\s*AM_INIT_AUTOMAKE\\((.*),.*\\).*");
328
QRegExp cppre("\\s*AC_PROG_CXX");
329
QRegExp f77re("\\s*AC_PROG_F77");
330
while (!cstream.atEnd()) {
331
QString s = cstream.readLine();
332
if (namere.search(s) == 0)
333
name_edit->setText(namere.cap(1).stripWhiteSpace());
336
else if (cppre.search(s) == 0)
337
setProjectType("cpp-auto");
338
else if (f77re.search(s) == 0)
339
setProjectType("fortran-auto");
345
void ImportDialog::setProjectType(const QString &type)
347
kdDebug(9010) << "Setting project type " << type << endl;
348
QString suffix = "/" + type;
349
int suffixLength = suffix.length();
352
QStringList::ConstIterator it;
353
for (it = importNames.begin(); it != importNames.end(); ++it) {
354
if ((*it).right(suffixLength) == suffix) {
355
project_combo->setCurrentItem(i);
362
void ImportDialog::scanAvailableVCS()
364
// vcsCombo->insertStringList( m_part->registeredVersionControls() );
366
KTrader::OfferList offers = KTrader::self()->query("KDevelop/VersionControl");
367
KTrader::OfferList::const_iterator it = offers.begin();
368
while( it != offers.end() )
370
vcsCombo->insertItem( (*it)->genericName(), i++ );
376
void ImportDialog::slotFinishedCheckout( QString destinationDir )
378
urlinput_edit->setURL( destinationDir );
380
setCursor( KCursor::arrowCursor() );
381
// setEnabled( true );
385
void ImportDialog::slotFetchModulesFromRepository()
388
KDevVersionControl *vcs = m_part->versionControlByName( vcsCombo->currentText() );
392
setCursor( KCursor::waitCursor() );
393
// setEnabled( false );
395
connect( vcs, SIGNAL(finishedFetching(QString)),
396
this, SLOT(slotFinishedCheckout(QString)) );
398
//restore cursor if we can't fetch repository
399
if ( !vcs->fetchFromRepository() )
400
setCursor( KCursor::arrowCursor() );
404
void ImportDialog::projectTypeChanged( const QString &type )
406
if (m_infrastructure[type].isOn)
408
infrastructureBox->setEnabled(true);
409
infrastructureBox->setText(m_infrastructure[type].comment);
413
infrastructureBox->setEnabled(false);
414
infrastructureBox->setText(i18n("Generate build system infrastrucure"));
418
void ImportDialog::createProjectInfrastructure( )
420
kdDebug(9010) << "ImportDialog::createProjectInfrastructure" << endl;
421
InfrastructureCmd cmd = m_infrastructure[project_combo->currentText()];
425
QDir dir (urlinput_edit->url());
426
QStringList files = dir.entryList(cmd.existingPattern);
427
if (!files.isEmpty()) {
428
if (KMessageBox::questionYesNo(this, i18n("Project infrastrucure already exists in target directory.\nGenerate new project infrastructure and overwrite old?"), QString::null, i18n("Generate"), i18n("Do Not Generate")) == KMessageBox::No)
432
QString command = "cd " + urlinput_edit->url() + " && " + cmd.command;
433
kdDebug(9010) << "executing " << command.ascii() << endl;
434
system(command.ascii());
437
void ImportDialog::projectTypeChanged( int type )
439
projectTypeChanged(project_combo->text(type));
443
#include "importdlg.moc"