From fca9ca46021f8bd614d660cc81bcd90676e72183 Mon Sep 17 00:00:00 2001
From: ck <qt-info@nokia.com>
Date: Fri, 23 Jul 2010 14:37:31 +0200
Subject: [PATCH] Maemo: Prepare "mount local directories on device" feature.

Not used yet.

Reviewed-by: kh1
---
 .../qt-maemo/maemoconstants.h                 |   3 +
 .../qt-maemo/maemoremotemountsmodel.cpp       | 206 ++++++++++++++++++
 .../qt-maemo/maemoremotemountsmodel.h         |  96 ++++++++
 .../qt-maemo/maemorunconfiguration.cpp        |   4 +
 .../qt-maemo/maemorunconfiguration.h          |   2 +
 .../qt4projectmanager/qt-maemo/qt-maemo.pri   |   6 +-
 6 files changed, 315 insertions(+), 2 deletions(-)
 create mode 100644 src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.cpp
 create mode 100644 src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.h

diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemoconstants.h b/src/plugins/qt4projectmanager/qt-maemo/maemoconstants.h
index 54973696e60..c0b3d610b48 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemoconstants.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemoconstants.h
@@ -66,6 +66,9 @@ static const QLatin1String LastDeployedFilesKey(PREFIX ".LastDeployedFiles");
 static const QLatin1String LastDeployedRemotePathsKey(PREFIX ".LastDeployedRemotePaths");
 static const QLatin1String LastDeployedTimesKey(PREFIX ".LastDeployedTimes");
 static const QLatin1String ProFileKey(".ProFile");
+static const QLatin1String ExportedLocalDirsKey(".ExportedLocalDirs");
+static const QLatin1String RemoteMountPointsKey(".RemoteMountPoints");
+static const QLatin1String MountPortsKey(".MountPorts");
 
 } // namespace Internal
 } // namespace Qt4ProjectManager
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.cpp
new file mode 100644
index 00000000000..991fbea28b8
--- /dev/null
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.cpp
@@ -0,0 +1,206 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://qt.nokia.com/contact.
+**
+**************************************************************************/
+
+#include "maemoremotemountsmodel.h"
+
+#include "maemoconstants.h"
+
+namespace Qt4ProjectManager {
+namespace Internal {
+
+namespace {
+const int LocalDirRow = 0;
+const int RemoteMountPointRow = 1;
+const int PortRow = 2;
+const QLatin1String InvalidMountPoint("/");
+} // anonymous namespace
+
+MaemoRemoteMountsModel::MountSpecification::MountSpecification(const QString &l,
+    const QString &r, int p) : localDir(l), remoteMountPoint(r), port(p) {}
+
+bool MaemoRemoteMountsModel::MountSpecification::isValid() const
+{
+    return remoteMountPoint == InvalidMountPoint;
+}
+
+
+MaemoRemoteMountsModel::MaemoRemoteMountsModel(QObject *parent) :
+    QAbstractTableModel(parent)
+{
+}
+
+void MaemoRemoteMountsModel::addMountSpecification(const QString &localDir)
+{
+    int port = 10000;
+    int i = 0;
+    while (i < rowCount()) {
+        if (mountSpecificationAt(i).port == port) {
+            ++port;
+            i = 0;
+        } else {
+            ++i;
+        }
+    }
+
+    beginInsertRows(QModelIndex(), rowCount(), rowCount());
+    m_mountSpecs << MountSpecification(localDir, InvalidMountPoint, port);
+    endInsertRows();
+}
+
+void MaemoRemoteMountsModel::removeMountSpecificationAt(int pos)
+{
+    Q_ASSERT(pos >= 0 && pos < rowCount());
+    beginRemoveRows(QModelIndex(), pos, pos);
+    m_mountSpecs.removeAt(pos);
+    endRemoveRows();
+}
+
+QVariantMap MaemoRemoteMountsModel::toMap() const
+{
+    QVariantMap map;
+    QVariantList localDirsList;
+    QVariantList remoteMountPointsList;
+    QVariantList mountPortsList;
+    foreach (const MountSpecification &mountSpec, m_mountSpecs) {
+        localDirsList << mountSpec.localDir;
+        remoteMountPointsList << mountSpec.remoteMountPoint;
+        mountPortsList << mountSpec.port;
+    }
+    map.insert(ExportedLocalDirsKey, localDirsList);
+    map.insert(RemoteMountPointsKey, remoteMountPointsList);
+    map.insert(MountPortsKey, mountPortsList);
+    return map;
+}
+
+void MaemoRemoteMountsModel::fromMap(const QVariantMap &map)
+{
+    const QVariantList &localDirsList
+        = map.value(ExportedLocalDirsKey).toList();
+    const QVariantList &remoteMountPointsList
+        = map.value(RemoteMountPointsKey).toList();
+    const QVariantList &mountPortsList = map.value(MountPortsKey).toList();
+    const int count = qMin(qMin(localDirsList.count(),
+        remoteMountPointsList.count()), mountPortsList.count());
+    for (int i = 0; i < count; ++i) {
+        const QString &localDir = localDirsList.at(i).toString();
+        const QString &remoteMountPoint
+            = remoteMountPointsList.at(i).toString();
+        const int port = mountPortsList.at(i).toInt();
+        m_mountSpecs << MountSpecification(localDir, remoteMountPoint, port);
+    }
+}
+
+Qt::ItemFlags MaemoRemoteMountsModel::flags(const QModelIndex &index) const
+{
+    Qt::ItemFlags ourFlags = QAbstractTableModel::flags(index);
+    if (index.column() == RemoteMountPointRow || index.column() == PortRow)
+        ourFlags |= Qt::ItemIsEditable;
+    return ourFlags;
+}
+
+QVariant MaemoRemoteMountsModel::headerData(int section,
+    Qt::Orientation orientation, int role) const
+{
+    if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
+        return QVariant();
+
+    switch (section) {
+    case LocalDirRow: return tr("Local directory");
+    case RemoteMountPointRow: return tr("Remote mount point");
+    case PortRow: return tr("Local port");
+    default: return QVariant();
+    }
+}
+
+QVariant MaemoRemoteMountsModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() || index.row() >= rowCount())
+        return QVariant();
+
+    const MountSpecification &mountSpec = mountSpecificationAt(index.row());
+    switch (index.column()) {
+    case LocalDirRow:
+        if (role == Qt::DisplayRole)
+            return mountSpec.localDir;
+        break;
+    case RemoteMountPointRow:
+        if (role == Qt::DisplayRole || role == Qt::EditRole)
+            return mountSpec.remoteMountPoint;
+        break;
+    case PortRow:
+        if (role == Qt::DisplayRole)
+            return mountSpec.port;
+        break;
+    }
+    return QVariant();
+}
+
+bool MaemoRemoteMountsModel::setData(const QModelIndex &index,
+    const QVariant &value, int role)
+{
+    if (!index.isValid() || index.row() >= rowCount() || role != Qt::EditRole)
+        return false;
+
+    bool set;
+    switch (index.column()) {
+    case RemoteMountPointRow: {
+        const QString &newRemoteMountPoint = value.toString();
+        for (int i = 0; i < m_mountSpecs.count(); ++i) {
+            const MountSpecification &mountSpec = m_mountSpecs.at(i);
+            if (i != index.row() && mountSpec.isValid()
+                && mountSpec.remoteMountPoint == newRemoteMountPoint)
+                return false;
+        }
+        m_mountSpecs[index.row()].remoteMountPoint = newRemoteMountPoint;
+        set = true;
+        break;
+    }
+    case PortRow: {
+        const int newPort = value.toInt();
+        for (int i = 0; i < m_mountSpecs.count(); ++i) {
+            if (i != index.row() && m_mountSpecs.at(i).port == newPort)
+                return false;
+        }
+        m_mountSpecs[index.row()].port = newPort;
+        set = true;
+        break;
+    }
+    case LocalDirRow:
+    default:
+        set = false;
+        break;
+    }
+
+    if (set)
+        emit dataChanged(index, index);
+    return set;
+}
+
+} // namespace Internal
+} // namespace Qt4ProjectManager
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.h b/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.h
new file mode 100644
index 00000000000..479087b5d5c
--- /dev/null
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemoremotemountsmodel.h
@@ -0,0 +1,96 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://qt.nokia.com/contact.
+**
+**************************************************************************/
+
+#ifndef MAEMOREMOTEMOUNTSMODEL_H
+#define MAEMOREMOTEMOUNTSMODEL_H
+
+#include <QtCore/QAbstractTableModel>
+#include <QtCore/QList>
+#include <QtCore/QString>
+#include <QtCore/QVariantMap>
+
+namespace Qt4ProjectManager {
+namespace Internal {
+
+class MaemoRemoteMountsModel : public QAbstractTableModel
+{
+    Q_OBJECT
+public:   
+    struct MountSpecification {
+        MountSpecification(const QString &l, const QString &r, int p);
+        bool isValid() const;
+
+        QString localDir;
+        QString remoteMountPoint;
+        int port;
+    };
+
+    explicit MaemoRemoteMountsModel(QObject *parent = 0);
+    int mountSpecificationCount() const { return m_mountSpecs.count(); }
+    MountSpecification mountSpecificationAt(int pos) const { return m_mountSpecs.at(pos); }
+
+    void addMountSpecification(const QString &localDir);
+    void removeMountSpecificationAt(int pos);
+
+    QVariantMap toMap() const;
+    void fromMap(const QVariantMap &map);
+
+private:
+    virtual int columnCount(const QModelIndex& = QModelIndex()) const;
+    virtual int rowCount(const QModelIndex& = QModelIndex()) const;
+    virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+    virtual QModelIndex parent(const QModelIndex &child) const;
+    virtual QVariant headerData(int section, Qt::Orientation orientation,
+        int role) const;
+    virtual QVariant data(const QModelIndex &index, int role) const;
+    virtual bool setData(const QModelIndex &index, const QVariant &value,
+        int role);
+
+    QList<MountSpecification> m_mountSpecs;
+};
+
+inline int MaemoRemoteMountsModel::columnCount(const QModelIndex &) const
+{
+    return 3;
+}
+
+inline int MaemoRemoteMountsModel::rowCount(const QModelIndex &parent) const
+{
+    return parent.isValid() ? 0 : mountSpecificationCount();
+}
+
+inline QModelIndex MaemoRemoteMountsModel::parent(const QModelIndex &) const
+{
+    return QModelIndex();
+}
+
+} // namespace Internal
+} // namespace Qt4ProjectManager
+
+#endif // MAEMOREMOTEMOUNTSMODEL_H
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.cpp
index 958d2810911..6ab65f99a6a 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.cpp
@@ -33,6 +33,7 @@
 #include "maemodeploystep.h"
 #include "maemodeviceconfiglistmodel.h"
 #include "maemoglobal.h"
+#include "maemoremotemountsmodel.h"
 #include "maemorunconfigurationwidget.h"
 #include "maemotoolchain.h"
 #include "qemuruntimemanager.h"
@@ -77,6 +78,7 @@ MaemoRunConfiguration::MaemoRunConfiguration(Qt4Target *parent,
 void MaemoRunConfiguration::init()
 {
     m_devConfigModel = new MaemoDeviceConfigListModel(this);
+    m_remoteMounts = new MaemoRemoteMountsModel(this);
     setDisplayName(QFileInfo(m_proFilePath).completeBaseName());
 
     updateDeviceConfigurations();
@@ -135,6 +137,7 @@ QVariantMap MaemoRunConfiguration::toMap() const
     const QDir dir = QDir(target()->project()->projectDirectory());
     map.insert(ProFileKey, dir.relativeFilePath(m_proFilePath));
     map.unite(m_devConfigModel->toMap());
+    map.unite(m_remoteMounts->toMap());
     return map;
 }
 
@@ -147,6 +150,7 @@ bool MaemoRunConfiguration::fromMap(const QVariantMap &map)
     const QDir dir = QDir(target()->project()->projectDirectory());
     m_proFilePath = dir.filePath(map.value(ProFileKey).toString());
     m_devConfigModel->fromMap(map);
+    m_remoteMounts->fromMap(map);
 
     return true;
 }
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.h b/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.h
index 875c02056c0..9f2f9b2536d 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemorunconfiguration.h
@@ -55,6 +55,7 @@ class MaemoDeviceConfigListModel;
 class MaemoDeployStep;
 class MaemoManager;
 class MaemoPackageCreationStep;
+class MaemoRemoteMountsModel;
 class MaemoRunConfigurationFactory;
 class MaemoToolChain;
 
@@ -110,6 +111,7 @@ private:
     QString m_proFilePath;
     mutable QString m_gdbPath;
     MaemoDeviceConfigListModel *m_devConfigModel;
+    MaemoRemoteMountsModel *m_remoteMounts;
     QStringList m_arguments;
 };
 
diff --git a/src/plugins/qt4projectmanager/qt-maemo/qt-maemo.pri b/src/plugins/qt4projectmanager/qt-maemo/qt-maemo.pri
index 7dfe9acfd73..c6d4a126fb0 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/qt-maemo.pri
+++ b/src/plugins/qt4projectmanager/qt-maemo/qt-maemo.pri
@@ -26,7 +26,8 @@ HEADERS += \
     $$PWD/maemoglobal.h \
     $$PWD/maemosshrunner.h \
     $$PWD/maemodebugsupport.h \
-    $$PWD/maemodeviceconfiglistmodel.h
+    $$PWD/maemodeviceconfiglistmodel.h \
+    $$PWD/maemoremotemountsmodel.h
 
 SOURCES += \
     $$PWD/maemoconfigtestdialog.cpp \
@@ -54,7 +55,8 @@ SOURCES += \
     $$PWD/maemoglobal.cpp \
     $$PWD/maemosshrunner.cpp \
     $$PWD/maemodebugsupport.cpp \
-    $$PWD/maemodeviceconfiglistmodel.cpp
+    $$PWD/maemodeviceconfiglistmodel.cpp \
+    $$PWD/maemoremotemountsmodel.cpp
 
 FORMS += \
     $$PWD/maemoconfigtestdialog.ui \
-- 
GitLab