2016-06-30 18:39:47 +01:00
/*************************************************************************
* Copyright ( C ) 2016 by Chantara Tith < tith . chantara @ gmail . com > *
2016-11-04 14:14:05 +00:00
* Copyright ( C ) 2016 by Andrius Š tikonas < andrius @ stikonas . eu > *
2016-06-30 18:39:47 +01:00
* *
* This program 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 . *
* *
* This program 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 this program . If not , see < http : //www.gnu.org/licenses/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-09-04 12:25:43 +01:00
# include "gui/volumegroupdialog.h"
# include "gui/volumegroupwidget.h"
2016-06-30 18:39:47 +01:00
2016-07-03 18:15:50 +01:00
# include <core/partitiontable.h>
2016-06-30 18:39:47 +01:00
# include <core/lvmdevice.h>
2016-07-03 18:15:50 +01:00
# include <fs/lvm2_pv.h>
2016-06-30 18:39:47 +01:00
# include <util/capacity.h>
# include <util/helpers.h>
2016-08-23 16:40:16 +01:00
# include <KConfigGroup>
2016-06-30 18:39:47 +01:00
# include <KLocalizedString>
# include <KSharedConfig>
2017-10-09 13:54:31 +01:00
# include <QtGlobal>
2016-07-09 18:45:32 +01:00
# include <QListWidgetItem>
2017-09-05 12:55:44 +01:00
# include <QRegularExpressionValidator>
2016-06-30 18:39:47 +01:00
2016-09-04 12:25:43 +01:00
/** Creates a new VolumeGroupDialog
2016-06-30 18:39:47 +01:00
@ param parent pointer to the parent widget
2016-08-23 16:40:16 +01:00
@ param vgName Volume Group name
@ param pvList List of LVM Physical Volumes used to create Volume Group
2016-06-30 18:39:47 +01:00
*/
2017-08-31 10:34:58 +01:00
VolumeGroupDialog : : VolumeGroupDialog ( QWidget * parent , QString & vgName , QVector < const Partition * > & pvList ) :
2016-06-30 18:39:47 +01:00
QDialog ( parent ) ,
2016-09-04 12:25:43 +01:00
m_DialogWidget ( new VolumeGroupWidget ( this ) ) ,
2016-08-23 16:40:16 +01:00
m_TargetName ( vgName ) ,
m_TargetPVList ( pvList ) ,
2016-07-31 23:25:11 +01:00
m_IsValidSize ( false ) ,
2016-08-08 04:49:41 +01:00
m_IsValidName ( true ) ,
2016-08-01 16:19:19 +01:00
m_TotalSize ( 0 ) ,
m_TotalUsedSize ( 0 ) ,
m_ExtentSize ( 0 )
2016-06-30 18:39:47 +01:00
{
mainLayout = new QVBoxLayout ( this ) ;
setLayout ( mainLayout ) ;
mainLayout - > addWidget ( & dialogWidget ( ) ) ;
dialogButtonBox = new QDialogButtonBox ;
okButton = dialogButtonBox - > addButton ( QDialogButtonBox : : Ok ) ;
cancelButton = dialogButtonBox - > addButton ( QDialogButtonBox : : Cancel ) ;
mainLayout - > addWidget ( dialogButtonBox ) ;
2016-08-26 23:16:56 +01:00
2016-06-30 18:39:47 +01:00
cancelButton - > setFocus ( ) ;
cancelButton - > setDefault ( true ) ;
setupDialog ( ) ;
2016-07-02 09:27:20 +01:00
setupConstraints ( ) ;
2016-06-30 18:39:47 +01:00
setupConnections ( ) ;
}
2016-09-04 12:25:43 +01:00
/** Destroys a VolumeGroupDialog */
VolumeGroupDialog : : ~ VolumeGroupDialog ( )
2016-06-30 18:39:47 +01:00
{
2016-09-04 12:25:43 +01:00
KConfigGroup kcg ( KSharedConfig : : openConfig ( ) , " createVolumeGroupDialog " ) ;
2016-06-30 18:39:47 +01:00
kcg . writeEntry ( " Geometry " , saveGeometry ( ) ) ;
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : setupDialog ( )
2016-06-30 18:39:47 +01:00
{
2017-10-09 17:08:27 +01:00
/* LVM Volume group name can consist of: letters numbers _ . - +
2017-10-06 23:13:08 +01:00
* It cannot start with underscore _ and must not be equal to . or . . or any entry in / dev /
* QLineEdit accepts QValidator : : Intermediate , so we just disable . at the beginning */
2017-10-06 23:48:52 +01:00
QRegularExpression re ( QStringLiteral ( R " (^(?!_| \ .) [ \ w \ - . + ] + ) " )) ;
2017-09-05 12:55:44 +01:00
QRegularExpressionValidator * validator = new QRegularExpressionValidator ( re , this ) ;
2016-12-07 21:51:22 +00:00
dialogWidget ( ) . vgName ( ) . setValidator ( validator ) ;
2016-07-09 00:59:46 +01:00
dialogWidget ( ) . vgName ( ) . setText ( targetName ( ) ) ;
2016-07-03 18:15:50 +01:00
dialogWidget ( ) . volumeType ( ) . addItem ( QStringLiteral ( " LVM " ) ) ;
dialogWidget ( ) . volumeType ( ) . addItem ( QStringLiteral ( " RAID " ) ) ;
dialogWidget ( ) . volumeType ( ) . setCurrentIndex ( 0 ) ;
2016-06-30 18:39:47 +01:00
setMinimumSize ( dialogWidget ( ) . size ( ) ) ;
resize ( dialogWidget ( ) . size ( ) ) ;
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : setupConnections ( )
2016-06-30 18:39:47 +01:00
{
2016-09-04 12:25:43 +01:00
connect ( dialogButtonBox , & QDialogButtonBox : : accepted , this , & VolumeGroupDialog : : accept ) ;
connect ( dialogButtonBox , & QDialogButtonBox : : rejected , this , & VolumeGroupDialog : : reject ) ;
2017-10-09 13:54:31 +01:00
connect ( & dialogWidget ( ) . volumeType ( ) , qOverload < int > ( & QComboBox : : currentIndexChanged ) , this , & VolumeGroupDialog : : onVolumeTypeChanged ) ;
2016-07-09 18:45:32 +01:00
connect ( & dialogWidget ( ) . listPV ( ) . listPhysicalVolumes ( ) , & QListWidget : : itemChanged ,
this , [ = ] ( QListWidgetItem * ) {
updateSizeInfos ( ) ;
} ) ;
2016-06-30 18:39:47 +01:00
}
2016-07-02 09:27:20 +01:00
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : setupConstraints ( )
2016-07-02 09:27:20 +01:00
{
2016-07-09 00:59:46 +01:00
updateSizeInfos ( ) ;
updateOkButtonStatus ( ) ;
2016-07-02 09:27:20 +01:00
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : updateOkButtonStatus ( )
2016-07-02 09:27:20 +01:00
{
2016-07-17 19:22:42 +01:00
bool enable = isValidSize ( ) ;
2016-07-02 09:27:20 +01:00
2016-08-08 04:49:41 +01:00
if ( dialogWidget ( ) . vgName ( ) . text ( ) . isEmpty ( ) | | ! isValidName ( ) ) {
2016-07-02 09:27:20 +01:00
enable = false ;
}
if ( dialogWidget ( ) . spinPESize ( ) . value ( ) < = 0 ) {
enable = false ;
}
okButton - > setEnabled ( enable ) ;
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : updateSectorInfos ( )
2016-07-02 09:27:20 +01:00
{
2016-07-09 15:37:19 +01:00
qint32 totalSectors = 0 ;
2018-12-09 18:38:44 +00:00
// we can't use LvmDevice method here because pv that is not in any VG will return 0
2018-04-09 15:14:48 +01:00
m_ExtentSize = dialogWidget ( ) . spinPESize ( ) . value ( ) * Capacity : : unitFactor ( Capacity : : Unit : : Byte , Capacity : : Unit : : MiB ) ;
2016-08-01 16:19:19 +01:00
if ( m_ExtentSize > 0 ) {
totalSectors = m_TotalSize / m_ExtentSize ;
}
dialogWidget ( ) . totalSectors ( ) . setText ( QString : : number ( totalSectors ) ) ;
}
2016-07-09 15:37:19 +01:00
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : updateSizeInfos ( )
2016-08-01 16:19:19 +01:00
{
2017-08-31 10:34:58 +01:00
const QVector < const Partition * > checkedPartitions = dialogWidget ( ) . listPV ( ) . checkedItems ( ) ;
2016-09-18 01:55:14 +01:00
m_TotalSize = 0 ;
for ( const auto & p : checkedPartitions )
2018-04-09 15:14:48 +01:00
m_TotalSize + = p - > capacity ( ) - p - > capacity ( ) % ( dialogWidget ( ) . spinPESize ( ) . value ( ) * Capacity : : unitFactor ( Capacity : : Unit : : Byte , Capacity : : Unit : : MiB ) ) ; // subtract space which is too small to hold PE
2016-09-18 01:55:14 +01:00
2016-08-01 16:19:19 +01:00
dialogWidget ( ) . totalSize ( ) . setText ( Capacity : : formatByteSize ( m_TotalSize ) ) ;
2016-07-15 22:29:34 +01:00
//Probably a bad design for updating state here; the state should be changed inside the update button function.
2016-11-07 18:57:30 +00:00
m_IsValidSize = m_TotalSize > = m_TotalUsedSize ;
2016-08-01 16:19:19 +01:00
updateSectorInfos ( ) ;
2016-07-15 22:29:34 +01:00
updateOkButtonStatus ( ) ;
2016-07-02 09:27:20 +01:00
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : updatePartitionList ( )
2016-07-03 18:15:50 +01:00
{
}
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : onPartitionListChanged ( )
2016-07-02 09:27:20 +01:00
{
}
2016-07-03 18:15:50 +01:00
2016-09-04 12:25:43 +01:00
void VolumeGroupDialog : : onVolumeTypeChanged ( int index )
2016-07-03 18:15:50 +01:00
{
2016-07-09 18:45:32 +01:00
Q_UNUSED ( index )
2016-07-03 18:15:50 +01:00
updatePartitionList ( ) ;
}