Commit 62d5cae0 authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Maemo: Implement on-demand adding of desktop files.

Task-number: QTCREATORBUG-2704
parent 76448418
......@@ -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
......
......@@ -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;
......
......@@ -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
......
......@@ -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;
......
......@@ -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>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment