From 70ddfa25ddcf89f031764e442e8822d0dad18245 Mon Sep 17 00:00:00 2001
From: Teo Mrnjavac <teo@kde.org>
Date: Sun, 27 Sep 2015 12:38:35 +0200
Subject: [PATCH] Disable Alongside if primary partition limit reached on all
 devices.

---
 src/modules/partition/gui/ChoicePage.cpp | 55 ++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/src/modules/partition/gui/ChoicePage.cpp b/src/modules/partition/gui/ChoicePage.cpp
index e00af4cf57..864478841d 100644
--- a/src/modules/partition/gui/ChoicePage.cpp
+++ b/src/modules/partition/gui/ChoicePage.cpp
@@ -22,6 +22,8 @@
 #include "core/DeviceModel.h"
 #include "core/PartitionModel.h"
 #include "OsproberEntry.h"
+#include "core/partitiontable.h"
+#include "core/device.h"
 
 #include "PrettyRadioButton.h"
 
@@ -111,6 +113,24 @@ ChoicePage::init( PartitionCoreModule* core, const OsproberEntryList& osproberEn
     m_itemsLayout->addWidget( eraseButton );
     m_itemsLayout->setSpacing( CalamaresUtils::defaultFontHeight() / 2 );
 
+    // TODO 2.0: move this to a Utils namespace.
+    // Iterate over devices in devicemodel, foreach device, if it's DOS MBR and limit is
+    // reached and we have an osprober entry inside it, then disable alongside.
+    QStringList pathsOfDevicesWithPrimariesLimitReached;
+    for ( int row = 0; row < m_core->deviceModel()->rowCount(); ++row )
+    {
+        const QModelIndex& deviceIndex = m_core->deviceModel()->index( row );
+
+        Device* dev = m_core->deviceModel()->deviceForIndex( deviceIndex );
+        if ( ( dev->partitionTable()->type() == PartitionTable::msdos ||
+               dev->partitionTable()->type() == PartitionTable::msdos_sectorbased ) &&
+             dev->partitionTable()->numPrimaries() == dev->partitionTable()->maxPrimaries() )
+        {
+            // Primaries limit reached!
+            pathsOfDevicesWithPrimariesLimitReached.append( dev->deviceNode() );
+        }
+    }
+
     if ( osproberEntries.count() == 0 )
     {
         CALAMARES_RETRANSLATE(
@@ -137,6 +157,18 @@ ChoicePage::init( PartitionCoreModule* core, const OsproberEntryList& osproberEn
     {
         QString osName = osproberEntries.first().prettyName;
 
+        // Find out if our osprober entry (which is the shrink candidate) is on a
+        // device that can accommodate more partitions.
+        bool cantCreatePartitions = false;
+        foreach ( const QString& devicePath, pathsOfDevicesWithPrimariesLimitReached )
+        {
+            if ( osproberEntries.first().path.startsWith( devicePath ) )
+            {
+                cantCreatePartitions = true;
+                break;
+            }
+        }
+
         if ( !osName.isEmpty() )
         {
             CALAMARES_RETRANSLATE(
@@ -207,6 +239,8 @@ ChoicePage::init( PartitionCoreModule* core, const OsproberEntryList& osproberEn
         }
         if ( !osproberEntries.first().canBeResized )
             alongsideButton->hide();
+        if ( cantCreatePartitions )
+            alongsideButton->hide();
     }
     else
     {
@@ -223,6 +257,27 @@ ChoicePage::init( PartitionCoreModule* core, const OsproberEntryList& osproberEn
             }
         }
 
+        // Find out if at least one of our osprober entries is on a
+        // device that can accommodate more partitions.
+        bool atLeastOneCanCreatePartitions = false;
+        foreach ( const OsproberEntry& entry, osproberEntries )
+        {
+            bool cantCreatePartitions = false;
+            foreach ( const QString& devicePath, pathsOfDevicesWithPrimariesLimitReached )
+            {
+                if ( entry.path.startsWith( devicePath ) )
+                {
+                    cantCreatePartitions = true;
+                    break;
+                }
+            }
+            if ( !cantCreatePartitions )
+            {
+                atLeastOneCanCreatePartitions = true;
+                break;
+            }
+        }
+
         CALAMARES_RETRANSLATE(
             m_messageLabel->setText( tr( "This computer currently has multiple operating systems on it. "
                                          "What would you like to do?" ) );
-- 
GitLab