KDevelop API Documentation

parts/appwizard/importdlg.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2001-2002 by Bernd Gehrmann * 00003 * bernd@kdevelop.org * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include "importdlg.h" 00013 #include <qcombobox.h> 00014 #include <qdir.h> 00015 #include <qfile.h> 00016 #include <qlabel.h> 00017 #include <qlayout.h> 00018 #include <qlineedit.h> 00019 #include <qpushbutton.h> 00020 #include <qregexp.h> 00021 #include <qtextstream.h> 00022 #include <qtooltip.h> 00023 #include <kbuttonbox.h> 00024 #include <kdebug.h> 00025 #include <kdialog.h> 00026 #include <kfiledialog.h> 00027 #include <kinstance.h> 00028 #include <klocale.h> 00029 #include <kmessagebox.h> 00030 #include <kstandarddirs.h> 00031 #include <kcursor.h> 00032 #include <kfile.h> 00033 #include <kurlrequester.h> 00034 00035 #include "kdevcore.h" 00036 #include "kdevversioncontrol.h" 00037 00038 #include "appwizardfactory.h" 00039 #include "appwizardpart.h" 00040 #include "misc.h" 00041 00042 00043 ImportDialog::ImportDialog(AppWizardPart *part, QWidget *parent, const char *name) 00044 : ImportDialogBase(parent, name, true), m_part(part) 00045 { 00046 QString author, email; 00047 AppWizardUtil::guessAuthorAndEmail(&author, &email); 00048 author_edit->setText(author); 00049 email_edit->setText(email); 00050 QToolTip::add( urlinput_edit->button(), i18n("Choose directory to import") ); 00051 urlinput_edit->setMode(KFile::Directory|KFile::ExistingOnly); 00052 00053 KStandardDirs *dirs = AppWizardFactory::instance()->dirs(); 00054 importNames = dirs->findAllResources("appimports", QString::null, false, true); 00055 importNames.sort(); 00056 00057 QStringList::ConstIterator it; 00058 for (it = importNames.begin(); it != importNames.end(); ++it) { 00059 KConfig config(KGlobal::dirs()->findResource("appimports", *it)); 00060 config.setGroup("General"); 00061 project_combo->insertItem(config.readEntry("Comment")); 00062 } 00063 00064 setProjectType("c"); 00065 connect( name_edit, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotProjectNameChanged( const QString & ) ) ); 00066 scanAvailableVCS(); 00067 connect( fetchModuleButton, SIGNAL(clicked()), 00068 this, SLOT(slotFetchModulesFromRepository()) ); 00069 slotProjectNameChanged( name_edit->text() ); 00070 } 00071 00072 00073 ImportDialog::~ImportDialog() 00074 {} 00075 00076 void ImportDialog::slotProjectNameChanged( const QString &_text ) 00077 { 00078 ok_button->setEnabled( !_text.isEmpty() ); 00079 } 00080 00081 void ImportDialog::accept() 00082 { 00083 QDir dir(urlinput_edit->url()); 00084 if (urlinput_edit->url().isEmpty() || !dir.exists()) { 00085 KMessageBox::sorry(this, i18n("You have to choose a directory.")); 00086 return; 00087 } 00088 00089 QString projectName = name_edit->text(); 00090 if (projectName.isEmpty()) { 00091 KMessageBox::sorry(this, i18n("You have to choose a project name.")); 00092 return; 00093 } 00094 00095 for (uint i=0; i < projectName.length(); ++i) 00096 if (!projectName[i].isLetterOrNumber() && projectName[i] != '_') { 00097 KMessageBox::sorry(this, i18n("Your application name should only contain letters and numbers.")); 00098 return; 00099 } 00100 00101 QString author = author_edit->text(); 00102 QString email = email_edit->text(); 00103 00104 QFileInfo finfo(importNames[project_combo->currentItem()]); 00105 QDir importdir(finfo.dir()); 00106 importdir.cdUp(); 00107 QFile src(importdir.filePath("importfiles/" + finfo.fileName() + ".kdevelop")); 00108 kdDebug(9010) << "Import template " << src.name() << endl; 00109 if (!src.open(IO_ReadOnly)) { 00110 KMessageBox::sorry(this, i18n("Can not open project template.")); 00111 return; 00112 } 00113 00114 QFile dest(dir.filePath(projectName + ".kdevelop")); 00115 if (!dest.open(IO_WriteOnly)) { 00116 KMessageBox::sorry(this, i18n("Can not write the project file.")); 00117 return; 00118 } 00119 00120 QTextStream srcstream(&src); 00121 QTextStream deststream(&dest); 00122 00123 while (!srcstream.atEnd()) { 00124 QString line = srcstream.readLine(); 00125 line.replace(QRegExp("\\$APPNAMELC\\$"), projectName); 00126 line.replace(QRegExp("\\$AUTHOR\\$"), author); 00127 line.replace(QRegExp("\\$EMAIL\\$"), email); 00128 deststream << line << endl; 00129 } 00130 00131 dest.close(); 00132 src.close(); 00133 00134 m_part->core()->openProject(dir.filePath(projectName + ".kdevelop")); 00135 00136 kdDebug(9010) << "OPENING PROJECT: " << dir.filePath(projectName + ".kdevelop") << endl; 00137 00138 QDialog::accept(); 00139 } 00140 00141 00142 // Checks if the directory dir and all of its subdirectories 00143 // (one level recursion) have files that follow patterns 00144 // patterns is comma-separated 00145 static bool dirHasFiles(QDir &dir, const QString &patterns) 00146 { 00147 QStringList::ConstIterator pit, sit; 00148 00149 QStringList patternList = QStringList::split(",", patterns); 00150 for (pit = patternList.begin(); pit != patternList.end(); ++pit) { 00151 if (!dir.entryList(*pit, QDir::Files).isEmpty()) { 00152 kdDebug() << "Has files " << (*pit) << endl; 00153 return true; 00154 } 00155 } 00156 00157 QStringList subdirList = dir.entryList("*", QDir::Dirs); 00158 for (sit = subdirList.begin(); sit != subdirList.end(); ++sit) { 00159 QDir subdir(dir); 00160 subdir.cd(*sit); 00161 for (pit = patternList.begin(); pit != patternList.end(); ++pit) { 00162 if (!subdir.entryList(*pit, QDir::Files).isEmpty()) { 00163 kdDebug() << "Has files " << (*pit) << " in " << (*sit) << endl; 00164 return true; 00165 } 00166 } 00167 } 00168 00169 return false; 00170 } 00171 00172 00173 void ImportDialog::dirChanged() 00174 { 00175 QString dirName = urlinput_edit->url(); 00176 QDir dir(dirName); 00177 if (!dir.exists()) 00178 return; 00179 00180 // KDevelop legacy project? 00181 QStringList files = dir.entryList("*.kdevprj"); 00182 if (!files.isEmpty()) { 00183 scanLegacyKDevelopProject(dir.absFilePath(files.first())); 00184 return; 00185 } 00186 00187 // Studio legacy project? 00188 files = dir.entryList("*.studio"); 00189 if (!files.isEmpty()) { 00190 scanLegacyStudioProject(dir.absFilePath(files.first())); 00191 return; 00192 } 00193 00194 // Automake based? 00195 if (dir.exists("config.guess") || dir.exists("configure.in.in")) { 00196 scanAutomakeProject(dirName); 00197 return; 00198 } 00199 00200 name_edit->setText(dir.dirName()); 00201 00202 // QMake based? 00203 files = dir.entryList("*.pro"); 00204 if (!files.isEmpty()) { 00205 setProjectType("qtqmake"); 00206 return; 00207 } 00208 00209 // C++? 00210 if (dirHasFiles(dir, "*.cpp,*.c++,*.cxx,*.C,*.cc")) { 00211 setProjectType("cpp"); 00212 return; 00213 } 00214 00215 // Fortran? 00216 if (dirHasFiles(dir, "*.f77,*.f,*.for,*.ftn")) { 00217 setProjectType("fortran"); 00218 return; 00219 } 00220 00221 // Python? 00222 if (dirHasFiles(dir, "*.py")) { 00223 setProjectType("python"); 00224 return; 00225 } 00226 00227 // Perl? 00228 if (dirHasFiles(dir, "*.pl,*.pm")) { 00229 setProjectType("perl"); 00230 return; 00231 } 00232 } 00233 00234 00235 void ImportDialog::scanLegacyKDevelopProject(const QString &fileName) 00236 { 00237 kdDebug(9010) << "Scanning legacy KDevelop project file " << fileName << endl; 00238 00239 KSimpleConfig config(fileName, true); 00240 config.setGroup("General"); 00241 author_edit->setText(config.readEntry("author")); 00242 email_edit->setText(config.readEntry("email")); 00243 name_edit->setText(config.readEntry("project_name")); 00244 00245 QString legacyType = config.readEntry("project_type"); 00246 if (QStringList::split(",", "normal_kde,normal_kde2,kde2_normal,mdi_kde2").contains(legacyType)) 00247 setProjectType("kde"); 00248 else if (legacyType == "normal_gnome") 00249 setProjectType("gnome"); 00250 else if (legacyType == "normal_empty") 00251 setProjectType("cpp-auto"); 00252 else 00253 setProjectType("cpp"); 00254 } 00255 00256 00257 void ImportDialog::scanLegacyStudioProject(const QString &fileName) 00258 { 00259 kdDebug(9010) << "Scanning legacy studio project file " << fileName << endl; 00260 00261 // Not much to do here... 00262 KSimpleConfig config(fileName, true); 00263 config.setGroup("kdestudio"); 00264 name_edit->setText(config.readEntry("Name")); 00265 } 00266 00267 00268 void ImportDialog::scanAutomakeProject(const QString &dirName) 00269 { 00270 kdDebug(9010) << "Scanning automake project directory " << dirName << endl; 00271 00272 bool stop = false; 00273 if (QFile::exists(dirName + "/admin/am_edit")) { 00274 setProjectType("kde"); 00275 stop = true; 00276 } else if (QFile::exists(dirName + "/macros/gnome.m4")) { 00277 setProjectType("gnome"); 00278 stop = true; 00279 } else { 00280 setProjectType("c-auto"); 00281 } 00282 00283 QFile af(dirName + "/AUTHORS"); 00284 if (!af.open(IO_ReadOnly)) 00285 return; 00286 QTextStream astream(&af); 00287 00288 QRegExp authorre("(.*)<(.*)>"); 00289 while (!astream.atEnd()) { 00290 QString s = astream.readLine(); 00291 if (authorre.search(s) != -1) { 00292 author_edit->setText(authorre.cap(1).stripWhiteSpace()); 00293 email_edit->setText(authorre.cap(2).stripWhiteSpace()); 00294 break; 00295 } 00296 } 00297 af.close(); 00298 00299 QFile cf(dirName + "/configure.in"); 00300 if (!cf.open(IO_ReadOnly)) 00301 return; 00302 QTextStream cstream(&cf); 00303 00304 QRegExp namere("\\s*AM_INIT_AUTOMAKE\\((.*),.*\\).*"); 00305 QRegExp cppre("\\s*AC_PROG_CXX"); 00306 QRegExp f77re("\\s*AC_PROG_F77"); 00307 while (!cstream.atEnd()) { 00308 QString s = cstream.readLine(); 00309 if (namere.search(s) == 0) 00310 name_edit->setText(namere.cap(1).stripWhiteSpace()); 00311 if (!stop) 00312 continue; 00313 else if (cppre.search(s) == 0) 00314 setProjectType("cpp-auto"); 00315 else if (f77re.search(s) == 0) 00316 setProjectType("fortran-auto"); 00317 } 00318 cf.close(); 00319 } 00320 00321 00322 void ImportDialog::setProjectType(const QString &type) 00323 { 00324 kdDebug(9010) << "Setting project type " << type << endl; 00325 QString suffix = "/" + type; 00326 int suffixLength = suffix.length(); 00327 00328 int i=0; 00329 QStringList::ConstIterator it; 00330 for (it = importNames.begin(); it != importNames.end(); ++it) { 00331 if ((*it).right(suffixLength) == suffix) { 00332 project_combo->setCurrentItem(i); 00333 break; 00334 } 00335 ++i; 00336 } 00337 } 00338 00339 void ImportDialog::scanAvailableVCS() 00340 { 00341 vcsCombo->insertStringList( m_part->registeredVersionControls() ); 00342 } 00343 00344 void ImportDialog::slotFinishedCheckout( QString destinationDir ) 00345 { 00346 urlinput_edit->setURL( destinationDir ); 00347 00348 setCursor( KCursor::arrowCursor() ); 00349 // setEnabled( true ); 00350 } 00351 00352 void ImportDialog::slotFetchModulesFromRepository() 00353 { 00354 KDevVersionControl *vcs = m_part->versionControlByName( vcsCombo->currentText() ); 00355 if (!vcs) 00356 return; 00357 00358 setCursor( KCursor::waitCursor() ); 00359 // setEnabled( false ); 00360 00361 connect( vcs, SIGNAL(finishedFetching(QString)), 00362 this, SLOT(slotFinishedCheckout(QString)) ); 00363 00364 //restore cursor if we can't fetch repository 00365 if ( !vcs->fetchFromRepository() ) 00366 setCursor( KCursor::arrowCursor() ); 00367 } 00368 00369 00370 #include "importdlg.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:09 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003