From 81de0f86ba9cdbe52d2732e7c7407fb6ae25cc32 Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Thu, 22 Oct 2009 17:59:17 +0200 Subject: [PATCH] Workaround for Linux Distro Qt where examples live in write-protected dirs. Offer to copy the project into an alternative root. --- .../gettingstartedwelcomepagewidget.cpp | 87 ++++++++++++++++++- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/src/plugins/qt4projectmanager/gettingstartedwelcomepagewidget.cpp b/src/plugins/qt4projectmanager/gettingstartedwelcomepagewidget.cpp index dca4ab11936..a85023a2404 100644 --- a/src/plugins/qt4projectmanager/gettingstartedwelcomepagewidget.cpp +++ b/src/plugins/qt4projectmanager/gettingstartedwelcomepagewidget.cpp @@ -33,6 +33,8 @@ #include #include +#include + #include #include @@ -42,8 +44,12 @@ #include #include #include +#include #include +#include #include +#include +#include namespace Qt4ProjectManager { namespace Internal { @@ -159,17 +165,92 @@ void GettingStartedWelcomePageWidget::slotEnableExampleButton(int index) ui->openExampleButton->setEnabled(!fileName.isEmpty()); } +namespace { +void copyRecursive(const QDir& from, const QDir& to, const QString& dir) +{ + QDir dest(to); + dest.mkdir(dir); + dest.cd(dir); + QDir src(from); + src.cd(dir); + foreach(const QFileInfo& roFile, src.entryInfoList(QDir::Files)) { + QFile::copy(roFile.absoluteFilePath(), dest.absolutePath() + '/' + roFile.fileName()); + } + foreach(const QString& roDir, src.entryList(QDir::NoDotAndDotDot|QDir::Dirs)) { + copyRecursive(src, dest, QDir(roDir).dirName()); + } +} +} // namespace + void GettingStartedWelcomePageWidget::slotOpenExample() { QComboBox *box = ui->examplesComboBox; QString proFile = box->itemData(box->currentIndex(), Qt::UserRole).toString(); QString helpFile = box->itemData(box->currentIndex(), Qt::UserRole + 1).toString(); QStringList files; - QFileInfo fi(proFile); - QString tryFile = fi.path() + "/main.cpp"; + QFileInfo proFileInfo(proFile); + // If the Qt is a distro Qt on Linux, it will not be writable, hence compilation will fail + if (!proFileInfo.isWritable()) + { + QDialog d; + QGridLayout *lay = new QGridLayout(&d); + QLabel *descrLbl = new QLabel; + d.setWindowTitle(tr("Copy Project to writable Location?")); + descrLbl->setTextFormat(Qt::RichText); + descrLbl->setWordWrap(true); + descrLbl->setText(tr("

The project you are about to open is located in the " + "write-protected location:

%1
" + "

Please select a writable location below and click \"Copy Project and Open\" " + "to open a modifiable copy of the project or click \"Keep Project and Open\" " + "to open the project in location.

Note: You will not " + "be able to alter or compile your project in the current location.

") + .arg(QDir::toNativeSeparators(proFileInfo.dir().absolutePath()))); + lay->addWidget(descrLbl, 0, 0, 1, 2); + QLabel *txt = new QLabel(tr("&Location:")); + Utils::PathChooser *chooser = new Utils::PathChooser; + txt->setBuddy(chooser); + chooser->setExpectedKind(Utils::PathChooser::Directory); + QSettings *settings = Core::ICore::instance()->settings(); + chooser->setPath(settings->value( + QString::fromLatin1("General/ProjectsFallbackRoot"), QDir::homePath()).toString()); + lay->addWidget(txt, 1, 0); + lay->addWidget(chooser, 1, 1); + QDialogButtonBox *bb = new QDialogButtonBox; + connect(bb, SIGNAL(accepted()), &d, SLOT(accept())); + connect(bb, SIGNAL(rejected()), &d, SLOT(reject())); + QPushButton *copyBtn = bb->addButton(tr("&Copy Project and Open"), QDialogButtonBox::AcceptRole); + copyBtn->setDefault(true); + bb->addButton(tr("&Keep Project and Open"), QDialogButtonBox::RejectRole); + lay->addWidget(bb, 2, 0, 1, 2); + connect(chooser, SIGNAL(validChanged(bool)), copyBtn, SLOT(setEnabled(bool))); + if (d.exec() == QDialog::Accepted) { + QString exampleDirName = proFileInfo.dir().dirName(); + QString toDir = chooser->path(); + settings->setValue(QString::fromLatin1("General/ProjectsFallbackRoot"), toDir); + QDir toDirWithExamplesDir(toDir); + if (toDirWithExamplesDir.cd(exampleDirName)) { + toDirWithExamplesDir.cdUp(); // step out, just to not be in the way + QMessageBox::warning(topLevelWidget(), tr("Warning"), + tr("The specified location already exists. " + "Please specify a valid location."), + QMessageBox::Ok, QMessageBox::NoButton); + return; + } else { + QDir from = proFileInfo.dir(); + from.cdUp(); + copyRecursive(from, toDir, exampleDirName); + // set vars to new location + proFileInfo = QFileInfo(toDir + '/'+ exampleDirName + '/' + proFileInfo.fileName()); + proFile = proFileInfo.absoluteFilePath(); + } + } + } + + + QString tryFile = proFileInfo.path() + "/main.cpp"; files << proFile; if(!QFile::exists(tryFile)) - tryFile = fi.path() + '/' + fi.baseName() + ".cpp"; + tryFile = proFileInfo.path() + '/' + proFileInfo.baseName() + ".cpp"; if(QFile::exists(tryFile)) files << tryFile; Core::ICore::instance()->openFiles(files);