diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp index aecec515729665ec0948328b930fe40c548a5952..3326cc3ca4f93c91d41b2b8a329ea38e702b2da1 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<MaemoToolChain *>(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 3bb92b6cf07ac286764f0e19127768a8715b7973..c8e32704e9cd5de34103829e7a999c5a91413858 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 f2349bf9cc99015e5601d55f26d4a2283289852b..2c8d58001528e22e91e41cec176bd230996bc6d9 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp @@ -11,6 +11,8 @@ #include <projectexplorer/target.h> #include <utils/qtcassert.h> +#include <QtGui/QMessageBox> + 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 28c0f00d30d2b674cd2ef6ef3892f1c5ea3695b7..79101b46df2b84bf3f36d3a8425e345bd6d1a9bf 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 9c35f1be744ece0aab3f585e7a8f95477959f9f0..72ae6855646181fda7c531ad001d8d66b9cba007 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui @@ -13,7 +13,7 @@ <property name="windowTitle"> <string>Form</string> </property> - <layout class="QVBoxLayout" name="verticalLayout"> + <layout class="QVBoxLayout" name="verticalLayout_2"> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> @@ -107,26 +107,54 @@ </layout> </item> <item> - <widget class="QTableView" name="tableView"> - <property name="showGrid"> - <bool>false</bool> - </property> - <property name="wordWrap"> - <bool>false</bool> - </property> - <attribute name="horizontalHeaderDefaultSectionSize"> - <number>400</number> - </attribute> - <attribute name="horizontalHeaderMinimumSectionSize"> - <number>100</number> - </attribute> - <attribute name="horizontalHeaderStretchLastSection"> - <bool>true</bool> - </attribute> - <attribute name="verticalHeaderVisible"> - <bool>false</bool> - </attribute> - </widget> + <layout class="QHBoxLayout" name="horizontalLayout_4"> + <item> + <widget class="QTableView" name="tableView"> + <property name="showGrid"> + <bool>false</bool> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + <attribute name="horizontalHeaderDefaultSectionSize"> + <number>400</number> + </attribute> + <attribute name="horizontalHeaderMinimumSectionSize"> + <number>100</number> + </attribute> + <attribute name="horizontalHeaderStretchLastSection"> + <bool>true</bool> + </attribute> + <attribute name="verticalHeaderVisible"> + <bool>false</bool> + </attribute> + </widget> + </item> + <item> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QPushButton" name="addDesktopFileButton"> + <property name="text"> + <string>Add Desktop File</string> + </property> + </widget> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + </layout> </item> </layout> </widget>