From 8d6b7672bcf6152b1be41c77c3b26a5b6bffb045 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= <agateau@kde.org>
Date: Fri, 27 Jun 2014 17:25:39 +0200
Subject: [PATCH] Start PartitionPage

---
 src/modules/partition/CMakeLists.txt          | 42 ++++++++++
 src/modules/partition/DeviceModel.cpp         | 78 ++++++++++++++++++
 src/modules/partition/DeviceModel.h           | 41 ++++++++++
 src/modules/partition/PartitionModel.cpp      | 82 +++++++++++++++++++
 src/modules/partition/PartitionModel.h        | 43 ++++++++++
 src/modules/partition/PartitionPage.cpp       | 61 ++++++++++++++
 src/modules/partition/PartitionPage.h         | 50 +++++++++++
 src/modules/partition/PartitionPage.ui        | 79 ++++++++++++++++++
 src/modules/partition/PartitionViewPlugin.cpp | 32 ++++++++
 src/modules/partition/PartitionViewPlugin.h   | 40 +++++++++
 src/modules/partition/main.cpp                | 12 +++
 src/modules/partition/module.conf             | 10 +++
 12 files changed, 570 insertions(+)
 create mode 100644 src/modules/partition/CMakeLists.txt
 create mode 100644 src/modules/partition/DeviceModel.cpp
 create mode 100644 src/modules/partition/DeviceModel.h
 create mode 100644 src/modules/partition/PartitionModel.cpp
 create mode 100644 src/modules/partition/PartitionModel.h
 create mode 100644 src/modules/partition/PartitionPage.cpp
 create mode 100644 src/modules/partition/PartitionPage.h
 create mode 100644 src/modules/partition/PartitionPage.ui
 create mode 100644 src/modules/partition/PartitionViewPlugin.cpp
 create mode 100644 src/modules/partition/PartitionViewPlugin.h
 create mode 100644 src/modules/partition/main.cpp
 create mode 100644 src/modules/partition/module.conf

diff --git a/src/modules/partition/CMakeLists.txt b/src/modules/partition/CMakeLists.txt
new file mode 100644
index 0000000000..69e6c62da6
--- /dev/null
+++ b/src/modules/partition/CMakeLists.txt
@@ -0,0 +1,42 @@
+add_definitions( -DCALAMARES )
+
+include_directories( ${PROJECT_BINARY_DIR}/src/calamares )
+calamares_add_plugin( partition
+    TYPE viewmodule
+    EXPORT_MACRO PLUGINDLLEXPORT_PRO
+    CONFIG_FILE module.conf
+    SOURCES
+        DeviceModel.cpp
+        PartitionModel.cpp
+        PartitionPage.cpp
+        PartitionViewPlugin.cpp
+    UI
+        PartitionPage.ui
+    LINK_LIBRARIES
+        calapm
+        ${CALAMARES_LIBRARIES}
+        calamares_bin
+    SHARED_LIB
+)
+
+# Temporary, until views are integrated
+set( partview_SRCS
+    DeviceModel.cpp
+    PartitionModel.cpp
+    PartitionPage.cpp
+    ${calamares_SOURCE_DIR}/viewpages/AbstractPage.cpp
+    main.cpp
+)
+qt5_wrap_ui( partview_SRCS PartitionPage.ui )
+
+include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
+
+add_executable( partview ${partview_SRCS} )
+target_link_libraries( partview
+    calapm
+    ${CALAMARES_LIBRARIES}
+    Qt5::Widgets
+    Qt5::Gui
+    Qt5::Core
+)
+set_target_properties( partview PROPERTIES AUTOMOC TRUE )
diff --git a/src/modules/partition/DeviceModel.cpp b/src/modules/partition/DeviceModel.cpp
new file mode 100644
index 0000000000..ed57e10475
--- /dev/null
+++ b/src/modules/partition/DeviceModel.cpp
@@ -0,0 +1,78 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <DeviceModel.h>
+
+// CalaPM
+#include <core/device.h>
+
+DeviceModel::DeviceModel( QObject* parent )
+    : QAbstractListModel( parent )
+{
+}
+
+void
+DeviceModel::init( const QList< Device* >& devices )
+{
+    beginResetModel();
+    m_devices = devices;
+    endResetModel();
+}
+
+int
+DeviceModel::rowCount( const QModelIndex& parent ) const
+{
+    return parent.isValid() ? 0 : m_devices.count();
+}
+
+QVariant
+DeviceModel::data( const QModelIndex& index, int role ) const
+{
+    int row = index.row();
+    if ( row < 0 || row >= m_devices.count() )
+    {
+        return QVariant();
+    }
+
+    Device* device = m_devices.at( row );
+
+    switch ( role )
+    {
+    case Qt::DisplayRole:
+        if ( device->name().isEmpty() )
+        {
+            return device->deviceNode();
+        }
+        else
+        {
+            return device->name() + " " + device->deviceNode();
+        }
+    default:
+        return QVariant();
+    }
+}
+
+Device*
+DeviceModel::deviceForIndex( const QModelIndex& index ) const
+{
+    int row = index.row();
+    if ( row < 0 || row >= m_devices.count() )
+    {
+        return nullptr;
+    }
+    return m_devices.at( row );
+}
diff --git a/src/modules/partition/DeviceModel.h b/src/modules/partition/DeviceModel.h
new file mode 100644
index 0000000000..51fce7c5d2
--- /dev/null
+++ b/src/modules/partition/DeviceModel.h
@@ -0,0 +1,41 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef DEVICEMODEL_H
+#define DEVICEMODEL_H
+
+#include <QAbstractListModel>
+#include <QList>
+
+class Device;
+
+class DeviceModel : public QAbstractListModel
+{
+public:
+    DeviceModel( QObject* parent = 0 );
+    void init( const QList< Device* >& devices );
+
+    int rowCount( const QModelIndex& parent = QModelIndex() ) const Q_DECL_OVERRIDE;
+    QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const Q_DECL_OVERRIDE;
+
+    Device* deviceForIndex( const QModelIndex& index ) const;
+
+private:
+    QList< Device* > m_devices;
+};
+
+#endif /* DEVICEMODEL_H */
diff --git a/src/modules/partition/PartitionModel.cpp b/src/modules/partition/PartitionModel.cpp
new file mode 100644
index 0000000000..2b75ba1244
--- /dev/null
+++ b/src/modules/partition/PartitionModel.cpp
@@ -0,0 +1,82 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <PartitionModel.h>
+
+// CalaPM
+#include <core/device.h>
+#include <core/partition.h>
+#include <core/partitiontable.h>
+#include <fs/filesystem.h>
+
+PartitionModel::PartitionModel( QObject* parent )
+    : QAbstractListModel( parent )
+{
+}
+
+void
+PartitionModel::init( Device* device )
+{
+    beginResetModel();
+    m_device = device;
+    m_partitionList.clear();
+    if ( device )
+    {
+        fillPartitionList( m_device->partitionTable() );
+    }
+    endResetModel();
+}
+
+int
+PartitionModel::rowCount( const QModelIndex& parent ) const
+{
+    return parent.isValid() ? 0 : m_partitionList.count();
+}
+
+QVariant
+PartitionModel::data( const QModelIndex& index, int role ) const
+{
+    int row = index.row();
+    if ( row < 0 || row >= m_partitionList.count() )
+    {
+        return QVariant();
+    }
+
+    Partition* partition = m_partitionList.at( row );
+
+    switch ( role )
+    {
+    case Qt::DisplayRole:
+        if ( partition->partitionPath().isEmpty() )
+        {
+            return tr( "Free Space" );
+        }
+        return partition->partitionPath() + " " + partition->fileSystem().name() + " " + partition->mountPoint();
+    default:
+        return QVariant();
+    }
+}
+
+void
+PartitionModel::fillPartitionList( PartitionNode* parent )
+{
+    for ( auto partition : parent->children() )
+    {
+        m_partitionList << partition;
+        fillPartitionList( partition );
+    }
+}
diff --git a/src/modules/partition/PartitionModel.h b/src/modules/partition/PartitionModel.h
new file mode 100644
index 0000000000..a9bd3e4035
--- /dev/null
+++ b/src/modules/partition/PartitionModel.h
@@ -0,0 +1,43 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef PARTITIONMODEL_H
+#define PARTITIONMODEL_H
+
+#include <QAbstractListModel>
+
+class Device;
+class Partition;
+class PartitionNode;
+
+class PartitionModel : public QAbstractListModel
+{
+public:
+    PartitionModel( QObject* parent = 0 );
+    void init( Device* device );
+
+    int rowCount( const QModelIndex& parent = QModelIndex() ) const Q_DECL_OVERRIDE;
+    QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const Q_DECL_OVERRIDE;
+
+private:
+    Device* m_device;
+    QList< Partition* > m_partitionList;
+
+    void fillPartitionList( PartitionNode* parent );
+};
+
+#endif /* PARTITIONMODEL_H */
diff --git a/src/modules/partition/PartitionPage.cpp b/src/modules/partition/PartitionPage.cpp
new file mode 100644
index 0000000000..61fd54ebcf
--- /dev/null
+++ b/src/modules/partition/PartitionPage.cpp
@@ -0,0 +1,61 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "PartitionPage.h"
+
+// Local
+#include <DeviceModel.h>
+#include <PartitionModel.h>
+#include <ui_PartitionPage.h>
+
+// CalaPM
+#include <CalaPM.h>
+#include <backend/corebackend.h>
+#include <backend/corebackendmanager.h>
+
+// Qt
+#include <QDebug>
+
+PartitionPage::PartitionPage( QWidget* parent )
+    : Calamares::AbstractPage( parent )
+    , m_ui( new Ui_PartitionPage )
+    , m_deviceModel( new DeviceModel( this ) )
+    , m_partitionModel( new PartitionModel( this ) )
+{
+    // FIXME: Should be done at startup
+    if ( !CalaPM::init() )
+    {
+        qFatal( "Failed to init CalaPM" );
+    }
+    m_backend = CoreBackendManager::self()->backend();
+    m_ui->setupUi( this );
+    m_ui->deviceListView->setModel( m_deviceModel );
+    m_ui->partitionListView->setModel( m_partitionModel );
+
+    m_deviceModel->init( m_backend->scanDevices() );
+
+    connect( m_ui->deviceListView, &QListView::clicked, [ this ]( const QModelIndex & index )
+    {
+        Device* device = m_deviceModel->deviceForIndex( index );
+        m_partitionModel->init( device );
+    } );
+}
+
+PartitionPage::~PartitionPage()
+{
+}
diff --git a/src/modules/partition/PartitionPage.h b/src/modules/partition/PartitionPage.h
new file mode 100644
index 0000000000..24f4d00c55
--- /dev/null
+++ b/src/modules/partition/PartitionPage.h
@@ -0,0 +1,50 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PARTITIONPAGE_H
+#define PARTITIONPAGE_H
+
+#include "viewpages/AbstractPage.h"
+
+#include <QScopedPointer>
+
+class CoreBackend;
+class Ui_PartitionPage;
+
+class DeviceModel;
+class PartitionModel;
+
+class PartitionPage : public Calamares::AbstractPage
+{
+    Q_OBJECT
+public:
+    explicit PartitionPage( QWidget* parent = 0 );
+    ~PartitionPage();
+
+Q_SIGNALS:
+
+public Q_SLOTS:
+
+private:
+    QScopedPointer< Ui_PartitionPage > m_ui;
+    DeviceModel* m_deviceModel;
+    PartitionModel* m_partitionModel;
+    CoreBackend* m_backend;
+};
+
+#endif // PARTITIONPAGE_H
diff --git a/src/modules/partition/PartitionPage.ui b/src/modules/partition/PartitionPage.ui
new file mode 100644
index 0000000000..cc5b34fea1
--- /dev/null
+++ b/src/modules/partition/PartitionPage.ui
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>PartitionPage</class>
+ <widget class="QWidget" name="PartitionPage">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>568</width>
+    <height>304</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="1">
+    <widget class="QLabel" name="label">
+     <property name="text">
+      <string>[Disk preview goes here]</string>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="1">
+    <widget class="QListView" name="partitionListView"/>
+   </item>
+   <item row="2" column="1">
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <spacer name="horizontalSpacer">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item>
+      <widget class="QPushButton" name="createButton">
+       <property name="text">
+        <string>Create</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="editButton">
+       <property name="text">
+        <string>Edit</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="deleteButton">
+       <property name="text">
+        <string>Delete</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item row="0" column="0" rowspan="3">
+    <widget class="QListView" name="deviceListView">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/src/modules/partition/PartitionViewPlugin.cpp b/src/modules/partition/PartitionViewPlugin.cpp
new file mode 100644
index 0000000000..69f54d9d8f
--- /dev/null
+++ b/src/modules/partition/PartitionViewPlugin.cpp
@@ -0,0 +1,32 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "PartitionViewPlugin.h"
+
+
+PartitionViewPlugin::PartitionViewPlugin( QObject* parent )
+    : Calamares::ViewPlugin( parent )
+{
+}
+
+
+QString
+PartitionViewPlugin::prettyName()
+{
+    return tr( "Welcome" );
+}
diff --git a/src/modules/partition/PartitionViewPlugin.h b/src/modules/partition/PartitionViewPlugin.h
new file mode 100644
index 0000000000..fe7b2d8925
--- /dev/null
+++ b/src/modules/partition/PartitionViewPlugin.h
@@ -0,0 +1,40 @@
+/* === This file is part of Calamares - <http://github.com/calamares> ===
+ *
+ *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
+ *
+ *   Calamares is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   Calamares is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PARTITIONPAGEPLUGIN_H
+#define PARTITIONPAGEPLUGIN_H
+
+#include <QObject>
+
+#include "viewpages/ViewPlugin.h"
+#include "PluginDllMacro.h"
+
+class PLUGINDLLEXPORT PartitionViewPlugin : public Calamares::ViewPlugin
+{
+    Q_OBJECT
+    Q_PLUGIN_METADATA( IID "calamares.ViewPlugin/1.0" )
+    //FILE "module.json" )
+    Q_INTERFACES( Calamares::ViewPlugin )
+
+public:
+    explicit PartitionViewPlugin( QObject* parent = 0 );
+
+    QString prettyName() override;
+};
+
+#endif // PARTITIONPAGEPLUGIN_H
diff --git a/src/modules/partition/main.cpp b/src/modules/partition/main.cpp
new file mode 100644
index 0000000000..c3390fc1c1
--- /dev/null
+++ b/src/modules/partition/main.cpp
@@ -0,0 +1,12 @@
+#include <PartitionPage.h>
+
+#include <QApplication>
+
+int
+main( int argc, char* argv[] )
+{
+    QApplication app( argc, argv );
+    PartitionPage page;
+    page.show();
+    return app.exec();
+}
diff --git a/src/modules/partition/module.conf b/src/modules/partition/module.conf
new file mode 100644
index 0000000000..2c8dd6c6fb
--- /dev/null
+++ b/src/modules/partition/module.conf
@@ -0,0 +1,10 @@
+# Module metadata file for dummy plugin
+# Syntax is YAML 1.2
+---
+type:      "view"     #core or view
+name:      "partition" #the module name. must be unique and same as the parent directory
+interface: "qtplugin" #can be: qtplugin, python, process, ...
+requires:  []         #list of module names that must also be loaded. only applies to
+                      #binary plugins! these are actual link-time dependencies, not
+                      #conceptual dependencies for the setup procedure
+load:      "libcalamares_viewmodule_partition.so"
-- 
GitLab