2020-11-02 16:30:14 +03:00
/*
* Fedora Media Writer
* Copyright ( C ) 2016 Martin Bříza < mbriza @ redhat . com >
*
* 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 2
* 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 , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*/
# include "release.h"
# include "releasemanager.h"
# include "image_type.h"
# include "architecture.h"
# include "variant.h"
# include <QDebug>
2020-11-03 11:27:55 +03:00
Release : : Release ( const QString & name , const QString & display_name , const QString & summary , const QString & description , const QString & icon , const QStringList & screenshots , QObject * parent )
2020-11-02 16:30:14 +03:00
: QObject ( parent )
, m_name ( name )
, m_displayName ( display_name )
, m_summary ( summary )
, m_description ( description )
, m_icon ( icon )
, m_screenshots ( screenshots )
2020-11-02 17:40:37 +03:00
, m_isCustom ( false )
2020-11-02 16:30:14 +03:00
{
2020-11-03 11:27:55 +03:00
2020-11-02 16:30:14 +03:00
}
2020-11-03 11:27:55 +03:00
Release * Release : : custom ( QObject * parent ) {
auto customRelease = new Release ( QString ( ) , tr ( " Custom image " ) , QT_TRANSLATE_NOOP ( " Release " , " Pick a file from your drive(s) " ) , { QT_TRANSLATE_NOOP ( " Release " , " <p>Here you can choose a OS image from your hard drive to be written to your flash disk</p><p>Currently it is only supported to write raw disk images (.iso or .bin)</p> " ) } , " qrc:/logo/custom " , { } , parent ) ;
2020-11-02 17:40:37 +03:00
customRelease - > m_isCustom = true ;
customRelease - > setLocalFile ( QString ( ) ) ;
2020-11-02 16:30:14 +03:00
2020-11-02 17:40:37 +03:00
return customRelease ;
2020-11-02 16:30:14 +03:00
}
2020-11-03 12:31:45 +03:00
void Release : : addVariant ( Variant * variant ) {
// Check if variant already exists for debugging purposes
2020-11-02 16:30:14 +03:00
Variant * variant_in_list =
[ = ] ( ) - > Variant * {
2020-11-03 12:31:45 +03:00
for ( auto current : m_variants ) {
const bool arch_equals = ( current - > arch ( ) = = variant - > arch ( ) ) ;
const bool imageType_equals = ( current - > imageType ( ) = = variant - > imageType ( ) ) ;
const bool board_equals = ( current - > board ( ) = = variant - > board ( ) ) ;
const bool live_equals = ( current - > live ( ) = = variant - > live ( ) ) ;
if ( arch_equals & & imageType_equals & & board_equals & & live_equals ) {
return current ;
2020-11-02 16:30:14 +03:00
}
}
return nullptr ;
} ( ) ;
if ( variant_in_list ! = nullptr ) {
2020-11-03 12:31:45 +03:00
qWarning ( ) < < " Duplicate variant for release " < < m_name ;
qWarning ( ) < < " \t current= " < < variant_in_list - > url ( ) ;
qWarning ( ) < < " \t new= " < < variant - > url ( ) ;
2020-11-02 16:30:14 +03:00
return ;
}
2020-11-03 12:31:45 +03:00
// Otherwise add this variant
2020-11-02 16:30:14 +03:00
// NOTE: preserve the order from the Architecture::Id enum (to not have ARM first, etc.)
const int insert_index =
2020-11-03 12:31:45 +03:00
[ this , variant ] ( ) {
2020-11-02 16:30:14 +03:00
int out = 0 ;
2020-11-03 12:31:45 +03:00
for ( auto current : m_variants ) {
2020-11-02 16:30:14 +03:00
// NOTE: doing pointer comparison because architectures are a singleton pointers
2020-11-03 12:31:45 +03:00
if ( current - > arch ( ) > variant - > arch ( ) ) {
2020-11-02 16:30:14 +03:00
return out ;
}
out + + ;
}
return out ;
} ( ) ;
2020-11-03 12:31:45 +03:00
m_variants . insert ( insert_index , variant ) ;
2020-11-02 16:30:14 +03:00
emit variantsChanged ( ) ;
2020-11-03 12:31:45 +03:00
2020-11-02 16:30:14 +03:00
// Select first variant by default
if ( m_variants . count ( ) = = 1 ) {
m_selectedVariant = 0 ;
emit selectedVariantChanged ( ) ;
}
}
2020-11-02 17:40:37 +03:00
void Release : : setLocalFile ( const QString & path ) {
2020-11-03 11:27:55 +03:00
// Delete old custom variant (there's really only one, but iterate anyway)
2020-11-02 17:40:37 +03:00
for ( auto variant : m_variants ) {
variant - > deleteLater ( ) ;
}
m_variants . clear ( ) ;
// Add new variant
2020-11-03 11:27:55 +03:00
auto customVariant = Variant : : custom ( path , this ) ;
2020-11-02 17:40:37 +03:00
m_variants . append ( customVariant ) ;
emit variantsChanged ( ) ;
emit selectedVariantChanged ( ) ;
}
2020-11-02 16:30:14 +03:00
QString Release : : name ( ) const {
return m_name ;
}
QString Release : : displayName ( ) const {
return m_displayName ;
}
QString Release : : summary ( ) const {
return tr ( m_summary . toUtf8 ( ) ) ;
}
QString Release : : description ( ) const {
return m_description ;
}
2020-11-02 17:40:37 +03:00
bool Release : : isCustom ( ) const {
return m_isCustom ;
2020-11-02 16:30:14 +03:00
}
QString Release : : icon ( ) const {
return m_icon ;
}
QStringList Release : : screenshots ( ) const {
return m_screenshots ;
}
Variant * Release : : selectedVariant ( ) const {
if ( m_selectedVariant > = 0 & & m_selectedVariant < m_variants . count ( ) )
return m_variants [ m_selectedVariant ] ;
return nullptr ;
}
int Release : : selectedVariantIndex ( ) const {
return m_selectedVariant ;
}
void Release : : setSelectedVariantIndex ( int o ) {
if ( m_selectedVariant ! = o & & m_selectedVariant > = 0 & & m_selectedVariant < m_variants . count ( ) ) {
m_selectedVariant = o ;
emit selectedVariantChanged ( ) ;
}
}
QQmlListProperty < Variant > Release : : variants ( ) {
return QQmlListProperty < Variant > ( this , m_variants ) ;
}
QList < Variant * > Release : : variantList ( ) const {
return m_variants ;
}