diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp index aecec515729..3326cc3ca4f 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp @@ -199,7 +199,9 @@ bool MaemoDeployableListModel::setData(const QModelIndex &index, if (!isEditable(index) || role != Qt::EditRole) return false; const QString &remoteDir = value.toString(); - if (!addTargetPath(remoteDir)) + if (!addLinesToProFile(QStringList() + << QString::fromLocal8Bit("target.path = %1").arg(remoteDir) + << QLatin1String("INSTALLS += target"))) return false; m_deployables.first().remoteDir = remoteDir; emit dataChanged(index, index); @@ -262,13 +264,97 @@ bool MaemoDeployableListModel::isEditable(const QModelIndex &index) const && m_deployables.first().remoteDir.isEmpty(); } -bool MaemoDeployableListModel::addTargetPath(const QString &remoteDir) +bool MaemoDeployableListModel::canAddDesktopFile() const +{ + if (m_projectType == LibraryTemplate) + return false; + foreach (const MaemoDeployable &d, m_deployables) { + if (QFileInfo(d.localFilePath).fileName() == m_projectName + QLatin1String(".desktop")) + return false; + } + return true; +} + +bool MaemoDeployableListModel::addDesktopFile(QString &error) +{ + if (!canAddDesktopFile()) + return true; + const QString desktopFilePath = QFileInfo(m_proFilePath).path() + + QLatin1Char('/') + m_projectName + QLatin1String(".desktop"); + QFile desktopFile(desktopFilePath); + const bool existsAlready = desktopFile.exists(); + if (!desktopFile.open(QIODevice::ReadWrite)) { + error = tr("Failed to open '%1': %2") + .arg(desktopFilePath, desktopFile.errorString()); + return false; + } + + const QByteArray desktopTemplate("[Desktop Entry]\nEncoding=UTF-8\n" + "Version=1.0\nType=Application\nTerminal=false\nName=%1\nExec=%2\n" + "Icon=%1\nX-Window-Icon=\nX-HildonDesk-ShowInToolbar=true\n" + "X-Osso-Type=application/x-executable\n"); + const QString contents = existsAlready + ? QString::fromUtf8(desktopFile.readAll()) + : QString::fromLocal8Bit(desktopTemplate) + .arg(m_projectName, remoteExecutableFilePath()); + desktopFile.resize(0); + const QByteArray &contentsAsByteArray = contents.toUtf8(); + if (desktopFile.write(contentsAsByteArray) != contentsAsByteArray.count() + || !desktopFile.flush()) { + error = tr("Could not write '%1': %2") + .arg(desktopFilePath, desktopFile.errorString()); + return false; + } + + const MaemoToolChain *const tc = maemoToolchain(); + QTC_ASSERT(tc, return false); + QString remoteDir = QLatin1String("/usr/share/applications"); + if (tc->version() == MaemoToolChain::Maemo5) + remoteDir += QLatin1String("/hildon"); + const QLatin1String filesLine("desktopfile.files = $${TARGET}.desktop"); + const QString pathLine = QLatin1String("desktopfile.path = ") + remoteDir; + const QLatin1String installsLine("INSTALLS += desktopfile"); + if (!addLinesToProFile(QStringList() << filesLine << pathLine + << installsLine)) { + error = tr("Error writing project file."); + return false; + } + + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + m_deployables << MaemoDeployable(desktopFilePath, remoteDir); + endInsertRows(); + return true; +} + +bool MaemoDeployableListModel::addLinesToProFile(const QStringList &lines) { QFile projectFile(m_proFilePath); if (!projectFile.open(QIODevice::WriteOnly | QIODevice::Append)) { qWarning("Error opening .pro file for writing."); return false; } + QString proFileScope; + const MaemoToolChain *const tc = maemoToolchain(); + QTC_ASSERT(tc, return false); + if (tc->version() == MaemoToolChain::Maemo5) + proFileScope = QLatin1String("maemo5"); + else + proFileScope = QLatin1String("unix:!symbian:!maemo5"); + const QLatin1String separator("\n "); + const QString proFileString = QString(QLatin1Char('\n') + proFileScope + + QLatin1String(" {") + separator + lines.join(separator) + + QLatin1String("\n}\n")); + const QByteArray &proFileByteArray = proFileString.toLocal8Bit(); + if (projectFile.write(proFileByteArray) != proFileByteArray.count() + || !projectFile.flush()) { + qWarning("Error updating .pro file."); + return false; + } + return true; +} + +const MaemoToolChain *MaemoDeployableListModel::maemoToolchain() const +{ const ProjectExplorer::Project *const activeProject = ProjectExplorer::ProjectExplorerPlugin::instance()->session()->startupProject(); QTC_ASSERT(activeProject, return false); @@ -281,20 +367,7 @@ bool MaemoDeployableListModel::addTargetPath(const QString &remoteDir) const MaemoToolChain *const tc = dynamic_cast(bc->toolChain()); QTC_ASSERT(tc, return false); - QString proFileScope; - if (tc->version() == MaemoToolChain::Maemo5) - proFileScope = QLatin1String("maemo5"); - else - proFileScope = QLatin1String("unix:!symbian:!maemo5"); - const QString proFileString = QString(QLatin1Char('\n') + proFileScope - + QLatin1String(" {\n target.path = %1\n INSTALLS += target\n}\n")) - .arg(remoteDir); - if (!projectFile.write(proFileString.toLocal8Bit()) - || !projectFile.flush()) { - qWarning("Error updating .pro file."); - return false; - } - return true; + return tc; } } // namespace Qt4ProjectManager diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h index 3bb92b6cf07..c8e32704e9c 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h @@ -48,6 +48,7 @@ QT_END_NAMESPACE namespace Qt4ProjectManager { namespace Internal { class MaemoProFileWrapper; +class MaemoToolChain; class MaemoDeployableListModel : public QAbstractTableModel { @@ -75,6 +76,8 @@ public: QString projectDir() const; QString proFilePath() const { return m_proFilePath; } bool hasTargetPath() const { return m_hasTargetPath; } + bool canAddDesktopFile() const; + bool addDesktopFile(QString &error); ProFileUpdateSetting proFileUpdateSetting() const { return m_proFileUpdateSetting; } @@ -92,7 +95,8 @@ private: bool isEditable(const QModelIndex &index) const; bool buildModel(); - bool addTargetPath(const QString &remoteDir); + bool addLinesToProFile(const QStringList &lines); + const MaemoToolChain *maemoToolchain() const; const Qt4ProjectType m_projectType; const QString m_proFilePath; diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp index f2349bf9cc9..2c8d5800152 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp @@ -11,6 +11,8 @@ #include #include +#include + namespace Qt4ProjectManager { namespace Internal { @@ -32,6 +34,8 @@ MaemoDeployStepWidget::MaemoDeployStepWidget(MaemoDeployStep *step) : connect(ui->modelComboBox, SIGNAL(currentIndexChanged(int)), SLOT(setModel(int))); + connect(ui->addDesktopFileButton, SIGNAL(clicked()), + SLOT(addDesktopFile())); handleModelListReset(); } @@ -100,6 +104,7 @@ void MaemoDeployStepWidget::handleModelListToBeReset() { ui->tableView->reset(); // Otherwise we'll crash if the user is currently editing. ui->tableView->setModel(0); + ui->addDesktopFileButton->setEnabled(false); } void MaemoDeployStepWidget::handleModelListReset() @@ -115,10 +120,31 @@ void MaemoDeployStepWidget::handleModelListReset() void MaemoDeployStepWidget::setModel(int row) { + bool canAddDesktopFile = false; if (row != -1) { - ui->tableView->setModel(m_step->deployables()->modelAt(row)); + MaemoDeployableListModel *const model + = m_step->deployables()->modelAt(row); + ui->tableView->setModel(model); ui->tableView->resizeRowsToContents(); + canAddDesktopFile = model->canAddDesktopFile(); } + ui->addDesktopFileButton->setEnabled(canAddDesktopFile); +} + +void MaemoDeployStepWidget::addDesktopFile() +{ + const int modelRow = ui->modelComboBox->currentIndex(); + if (modelRow == -1) + return; + MaemoDeployableListModel *const model + = m_step->deployables()->modelAt(modelRow); + QString error; + if (!model->addDesktopFile(error)) { + QMessageBox::warning(this, tr("Could not create desktop file"), + tr("Error creating desktop file: %1").arg(error)); + } + ui->addDesktopFileButton->setEnabled(model->canAddDesktopFile()); + ui->tableView->resizeRowsToContents(); } } // namespace Internal diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h index 28c0f00d30d..79101b46df2 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h @@ -29,6 +29,7 @@ private: Q_SLOT void setModel(int row); Q_SLOT void handleModelListToBeReset(); Q_SLOT void handleModelListReset(); + Q_SLOT void addDesktopFile(); virtual void init(); virtual QString summaryText() const; diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui index 9c35f1be744..72ae6855646 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui @@ -13,7 +13,7 @@ Form - + @@ -107,26 +107,54 @@ - - - false - - - false - - - 400 - - - 100 - - - true - - - false - - + + + + + false + + + false + + + 400 + + + 100 + + + true + + + false + + + + + + + + + Add Desktop File + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + +