2014-07-22 14:25:21 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
2015-04-24 16:51:34 +02:00
|
|
|
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2017-06-14 17:39:53 +02:00
|
|
|
* Copyright 2017, Adriaan de Groot <groot@kde.org>
|
2014-07-22 14:25:21 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-08-08 13:25:56 +02:00
|
|
|
#include <core/PartitionIterator.h>
|
2014-07-22 14:25:21 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
// KPMcore
|
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
|
|
|
#include <kpmcore/core/partitiontable.h>
|
2014-07-22 14:25:21 +02:00
|
|
|
|
2015-04-24 16:51:34 +02:00
|
|
|
PartitionIterator::PartitionIterator( PartitionTable* table )
|
|
|
|
: m_table( table )
|
2014-07-22 14:25:21 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
Partition*
|
|
|
|
PartitionIterator::operator*() const
|
|
|
|
{
|
|
|
|
return m_current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionIterator::operator++()
|
|
|
|
{
|
|
|
|
if ( !m_current )
|
|
|
|
return;
|
|
|
|
if ( m_current->hasChildren() )
|
|
|
|
{
|
|
|
|
// Go to the first child
|
|
|
|
m_current = static_cast< Partition* >( m_current->children().first() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PartitionNode* parent = m_current->parent();
|
|
|
|
Partition* successor = parent->successor( *m_current );
|
|
|
|
if ( successor )
|
|
|
|
{
|
|
|
|
// Go to the next sibling
|
|
|
|
m_current = successor;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( parent->isRoot() )
|
|
|
|
{
|
|
|
|
// We reached the end
|
|
|
|
m_current = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Try to go to the next sibling of our parent
|
|
|
|
|
|
|
|
PartitionNode* grandParent = parent->parent();
|
|
|
|
Q_ASSERT( grandParent );
|
|
|
|
// If parent is not root, then it's not a PartitionTable but a
|
|
|
|
// Partition, we can static_cast it.
|
|
|
|
m_current = grandParent->successor( *static_cast< Partition* >( parent ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PartitionIterator::operator==( const PartitionIterator& other ) const
|
|
|
|
{
|
2015-04-24 16:51:34 +02:00
|
|
|
return m_table == other.m_table && m_current == other.m_current;
|
2014-07-22 14:25:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PartitionIterator::operator!=( const PartitionIterator& other ) const
|
|
|
|
{
|
|
|
|
return ! ( *this == other );
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::begin( Device* device )
|
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
if ( !device )
|
|
|
|
return PartitionIterator( nullptr );
|
2016-09-09 11:00:05 +02:00
|
|
|
Q_ASSERT(device);
|
2014-07-22 14:25:21 +02:00
|
|
|
PartitionTable* table = device->partitionTable();
|
|
|
|
if ( !table )
|
2015-04-24 16:51:34 +02:00
|
|
|
return PartitionIterator( nullptr );
|
|
|
|
return PartitionIterator::begin( table );
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::begin( PartitionTable* table )
|
|
|
|
{
|
|
|
|
auto it = PartitionIterator( table );
|
2014-07-22 14:25:21 +02:00
|
|
|
QList< Partition* > children = table->children();
|
|
|
|
// Does not usually happen, but it did happen on a 10MB disk with an MBR
|
|
|
|
// partition table.
|
|
|
|
if ( children.isEmpty() )
|
|
|
|
return it;
|
|
|
|
it.m_current = children.first();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::end( Device* device )
|
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
if ( !device )
|
|
|
|
return PartitionIterator( nullptr );
|
2015-04-24 16:51:34 +02:00
|
|
|
PartitionTable* table = device->partitionTable();
|
|
|
|
if ( !table )
|
|
|
|
return PartitionIterator( nullptr );
|
|
|
|
|
|
|
|
return PartitionIterator::end( table );
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::end( PartitionTable* table )
|
|
|
|
{
|
|
|
|
return PartitionIterator( table );
|
2014-07-22 14:25:21 +02:00
|
|
|
}
|