libcalamares: deal with QVariant

Compatibility header required, and logging was missing
a (transitively included in Qt5) include for QVariant.
This commit is contained in:
Adriaan de Groot 2023-09-03 14:57:02 +02:00
parent 27329a497a
commit 99d012c5ce
10 changed files with 112 additions and 56 deletions

View File

@ -14,6 +14,7 @@
#include "Settings.h"
#include "CalamaresConfig.h"
#include "compat/Variant.h"
#include "utils/Dirs.h"
#include "utils/Logger.h"
#include "utils/Yaml.h"
@ -157,12 +158,12 @@ interpretInstances( const YAML::Node& node, Settings::InstanceDescriptionList& c
if ( node )
{
QVariant instancesV = CalamaresUtils::yamlToVariant( node ).toList();
if ( instancesV.type() == QVariant::List )
if ( typeOf( instancesV ) == ListVariantType )
{
const auto instances = instancesV.toList();
for ( const QVariant& instancesVListItem : instances )
{
if ( instancesVListItem.type() != QVariant::Map )
if ( typeOf( instancesVListItem ) != MapVariantType )
{
continue;
}
@ -185,7 +186,7 @@ interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSeque
if ( node )
{
QVariant sequenceV = CalamaresUtils::yamlToVariant( node );
if ( !( sequenceV.type() == QVariant::List ) )
if ( typeOf( sequenceV ) != ListVariantType )
{
throw YAML::Exception( YAML::Mark(), "sequence key does not have a list-value" );
}
@ -193,7 +194,7 @@ interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSeque
const auto sequence = sequenceV.toList();
for ( const QVariant& sequenceVListItem : sequence )
{
if ( sequenceVListItem.type() != QVariant::Map )
if ( typeOf( sequenceVListItem ) != MapVariantType )
{
continue;
}

View File

@ -0,0 +1,46 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2023 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*
*/
#ifndef CALAMARES_COMPAT_VARIANT_H
#define CALAMARES_COMPAT_VARIANT_H
#include <QVariant>
namespace Calamares
{
/* Compatibility of QVariant between Qt5 and Qt6 */
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
const auto typeOf = []( const QVariant& v ) { return v.type(); };
const auto ListVariantType = QVariant::List;
const auto MapVariantType = QVariant::Map;
const auto StringVariantType = QVariant::String;
const auto CharVariantType = QVariant::Char;
const auto StringListVariantType = QVariant::StringList;
const auto BoolVariantType = QVariant::Bool;
const auto IntVariantType = QVariant::Int;
const auto LongLongVariantType = QVariant::LongLong;
const auto ULongLongVariantType = QVariant::ULongLong;
const auto DoubleVariantType = QVariant::Double;
#else
const auto typeOf = []( const QVariant& v ) { return v.typeId(); };
const auto ListVariantType = QMetaType::Type::QVariantList;
const auto MapVariantType = QMetaType::Type::QVariantMap;
const auto StringVariantType = QMetaType::Type::QString;
const auto CharVariantType = QMetaType::Type::Char;
const auto StringListVariantType = QMetaType::Type::QStringList;
const auto BoolVariantType = QMetaType::Type::Bool;
const auto IntVariantType = QMetaType::Type::Int;
const auto LongLongVariantType = QMetaType::Type::LongLong;
const auto ULongLongVariantType = QMetaType::Type::ULongLong;
const auto DoubleVariantType = QMetaType::Type::Double;
#endif
} // namespace Calamares
#endif

View File

@ -10,6 +10,7 @@
#include "GeoIPJSON.h"
#include "compat/Variant.h"
#include "utils/Logger.h"
#include "utils/Variant.h"
#include "utils/Yaml.h"
@ -64,7 +65,7 @@ GeoIPJSON::rawReply( const QByteArray& data )
YAML::Node doc = YAML::Load( data );
QVariant var = CalamaresUtils::yamlToVariant( doc );
if ( !var.isNull() && var.isValid() && var.type() == QVariant::Map )
if ( !var.isNull() && var.isValid() && Calamares::typeOf( var ) == Calamares::MapVariantType )
{
return selectMap( var.toMap(), m_element.split( '.' ), 0 );
}

View File

@ -8,6 +8,7 @@
#include "AutoMount.h"
#include "compat/Variant.h"
#include "utils/Logger.h"
#include <QtDBus>
@ -114,7 +115,7 @@ querySolidAutoMount( QDBusConnection& dbus, AutoMountInfo& info )
if ( arg.length() == 1 )
{
auto v = arg.at( 0 );
if ( v.isValid() && v.type() == QVariant::Bool )
if ( v.isValid() && Calamares::typeOf( v ) == Calamares::BoolVariantType )
{
result = v.toBool();
}

View File

@ -13,7 +13,7 @@
#include "GlobalStorage.h"
#include "JobQueue.h"
// #include "utils/CalamaresUtils.h"
#include "compat/Variant.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/StringExpander.h"
@ -46,11 +46,11 @@ get_variant_stringlist( const QVariantList& l )
unsigned int count = 0;
for ( const auto& v : l )
{
if ( v.type() == QVariant::String )
if ( Calamares::typeOf( v ) == Calamares::StringVariantType )
{
retl.append( CommandLine( v.toString(), CommandLine::TimeoutNotSet() ) );
}
else if ( v.type() == QVariant::Map )
else if ( Calamares::typeOf( v ) == Calamares::MapVariantType )
{
auto command( get_variant_object( v.toMap() ) );
if ( command.isValid() )
@ -61,7 +61,7 @@ get_variant_stringlist( const QVariantList& l )
}
else
{
cWarning() << "Bad CommandList element" << count << v.type() << v;
cWarning() << "Bad CommandList element" << count << v;
}
++count;
}
@ -119,7 +119,7 @@ CommandList::CommandList( bool doChroot, std::chrono::seconds timeout )
CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, std::chrono::seconds timeout )
: CommandList( doChroot, timeout )
{
if ( v.type() == QVariant::List )
if ( Calamares::typeOf( v ) == Calamares::ListVariantType )
{
const auto v_list = v.toList();
if ( v_list.count() )
@ -131,11 +131,11 @@ CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, std::ch
cWarning() << "Empty CommandList";
}
}
else if ( v.type() == QVariant::String )
else if ( Calamares::typeOf( v ) == Calamares::StringVariantType )
{
append( { v.toString(), m_timeout } );
}
else if ( v.type() == QVariant::Map )
else if ( Calamares::typeOf( v ) == Calamares::MapVariantType )
{
auto c( get_variant_object( v.toMap() ) );
if ( c.isValid() )
@ -146,7 +146,7 @@ CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, std::ch
}
else
{
cWarning() << "CommandList does not understand variant" << v.type();
cWarning() << "CommandList does not understand variant" << Calamares::typeOf( v );
}
}

View File

@ -15,6 +15,7 @@
#include "CalamaresVersionX.h"
#include "compat/Mutex.h"
#include "compat/Variant.h"
#include "utils/Dirs.h"
#include <QCoreApplication>
@ -228,9 +229,9 @@ const constexpr Quote_t Quote {};
QString
toString( const QVariant& v )
{
auto t = v.type();
auto t = Calamares::typeOf( v );
if ( t == QVariant::List )
if ( t == Calamares::ListVariantType )
{
QStringList s;
auto l = v.toList();

View File

@ -17,6 +17,7 @@
#include <QDebug>
#include <QSharedPointer>
#include <QVariant>
#include <memory>

View File

@ -24,6 +24,7 @@
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "compat/Variant.h"
#include <QTemporaryFile>
@ -151,7 +152,7 @@ LibCalamaresTests::testLoadSaveYaml()
auto map = CalamaresUtils::loadYaml( f.fileName() );
QVERIFY( map.contains( "sequence" ) );
QCOMPARE( map[ "sequence" ].type(), QVariant::List );
QCOMPARE( Calamares::typeOf( map[ "sequence" ] ), Calamares::ListVariantType );
// The source-repo example `settings.conf` has a show and an exec phase
auto sequence = map[ "sequence" ].toList();
@ -159,7 +160,7 @@ LibCalamaresTests::testLoadSaveYaml()
for ( const auto& v : sequence )
{
cDebug() << Logger::SubEntry << v;
QCOMPARE( v.type(), QVariant::Map );
QCOMPARE( Calamares::typeOf( v ), Calamares::MapVariantType );
QVERIFY( v.toMap().contains( "show" ) || v.toMap().contains( "exec" ) );
}
@ -553,11 +554,11 @@ LibCalamaresTests::testVariantStringListYAMLDashed()
QTemporaryFile f;
QVERIFY( f.open() );
f.write( R"(---
strings:
- aap
- noot
- mies
)" );
strings:
- aap
- noot
- mies
)" );
f.close();
bool ok = false;
QVariantMap m = loadYaml( f.fileName(), &ok );
@ -581,8 +582,8 @@ LibCalamaresTests::testVariantStringListYAMLBracketed()
QTemporaryFile f;
QVERIFY( f.open() );
f.write( R"(---
strings: [ aap, noot, mies ]
)" );
strings: [ aap, noot, mies ]
)" );
f.close();
bool ok = false;
QVariantMap m = loadYaml( f.fileName(), &ok );
@ -604,24 +605,24 @@ LibCalamaresTests::testStringTruncation()
using namespace Calamares::String;
const QString longString( R"(---
--- src/libcalamares/utils/String.h
+++ src/libcalamares/utils/String.h
@@ -62,15 +62,22 @@ DLLEXPORT QString removeDiacritics( const QString& string );
*/
DLLEXPORT QString obscure( const QString& string );
--- src/libcalamares/utils/String.h
+++ src/libcalamares/utils/String.h
@@ -62,15 +62,22 @@ DLLEXPORT QString removeDiacritics( const QString& string );
*/
DLLEXPORT QString obscure( const QString& string );
+/** @brief Parameter for counting lines at beginning and end of string
+/** @brief Parameter for counting lines at beginning and end of string
+ *
+ * This is used by truncateMultiLine() to indicate how many lines from
+ * the beginning and how many from the end should be kept.
+ */
struct LinesStartEnd
{
- int atStart;
- int atEnd;
+ int atStart = 0;
+ int atEnd = 0;
)" );
struct LinesStartEnd
{
- int atStart;
- int atEnd;
+ int atStart = 0;
+ int atEnd = 0;
)" );
const int sufficientLength = 812;
// There's 18 lines in all
@ -685,8 +686,8 @@ LibCalamaresTests::testStringTruncationShorter()
using namespace Calamares::String;
const QString longString( R"(Some strange string artifacts appeared, leading to `{1?}` being
displayed in various user-facing messages. These have been removed
and the translations updated.)" );
displayed in various user-facing messages. These have been removed
and the translations updated.)" );
const char NEWLINE = '\n';
const int insufficientLength = 42;

View File

@ -17,6 +17,7 @@
#include "Variant.h"
#include "Logger.h"
#include "compat/Variant.h"
#include <QString>
#include <QVariantMap>
@ -29,7 +30,7 @@ getBool( const QVariantMap& map, const QString& key, bool d )
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.type() == QVariant::Bool )
if ( Calamares::typeOf( v ) == Calamares::BoolVariantType )
{
return v.toBool();
}
@ -43,7 +44,7 @@ getString( const QVariantMap& map, const QString& key, const QString& d )
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.type() == QVariant::String )
if ( Calamares::typeOf( v ) == Calamares::StringVariantType )
{
return v.toString();
}
@ -57,7 +58,7 @@ getStringList( const QVariantMap& map, const QString& key, const QStringList& d
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.canConvert( QMetaType::QStringList ) )
if ( v.canConvert< QStringList >() )
{
return v.toStringList();
}
@ -71,7 +72,7 @@ getList( const QVariantMap& map, const QString& key, const QList< QVariant >& d
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.canConvert( QVariant::List ) )
if ( v.canConvert< QVariantList >() )
{
return v.toList();
}
@ -107,11 +108,11 @@ getDouble( const QVariantMap& map, const QString& key, double d )
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.type() == QVariant::Int )
if ( Calamares::typeOf( v ) == Calamares::IntVariantType )
{
return v.toInt();
}
else if ( v.type() == QVariant::Double )
else if ( Calamares::typeOf( v ) == Calamares::DoubleVariantType )
{
return v.toDouble();
}
@ -126,7 +127,7 @@ getSubMap( const QVariantMap& map, const QString& key, bool& success, const QVar
if ( map.contains( key ) )
{
auto v = map.value( key );
if ( v.type() == QVariant::Map )
if ( Calamares::typeOf( v ) == Calamares::MapVariantType )
{
success = true;
return v.toMap();

View File

@ -12,6 +12,7 @@
*/
#include "Yaml.h"
#include "compat/Variant.h"
#include "utils/Logger.h"
#include <QByteArray>
@ -204,7 +205,8 @@ loadYaml( const QString& filename, bool* ok )
}
if ( yamlContents.isValid() && !yamlContents.isNull() && yamlContents.type() == QVariant::Map )
if ( yamlContents.isValid() && !yamlContents.isNull()
&& Calamares::typeOf( yamlContents ) == Calamares::MapVariantType )
{
if ( ok )
{
@ -237,35 +239,36 @@ static const char newline[] = "\n";
static void
dumpYamlElement( QFile& f, const QVariant& value, int indent )
{
if ( value.type() == QVariant::Type::Bool )
const auto t = Calamares::typeOf( value );
if ( t == Calamares::BoolVariantType )
{
f.write( value.toBool() ? "true" : "false" );
}
else if ( value.type() == QVariant::Type::String )
else if ( t == Calamares::StringVariantType )
{
f.write( quote );
f.write( value.toString().toUtf8() );
f.write( quote );
}
else if ( value.type() == QVariant::Type::Int )
else if ( t == Calamares::IntVariantType )
{
f.write( QString::number( value.toInt() ).toUtf8() );
}
else if ( value.type() == QVariant::Type::LongLong )
else if ( t == Calamares::LongLongVariantType )
{
f.write( QString::number( value.toLongLong() ).toUtf8() );
}
else if ( value.type() == QVariant::Type::Double )
else if ( t == Calamares::DoubleVariantType )
{
f.write( QString::number( value.toDouble(), 'f', 2 ).toUtf8() );
}
else if ( value.canConvert( QVariant::Type::ULongLong ) )
else if ( value.canConvert< qulonglong >() )
{
// This one needs to be *after* bool, int, double to avoid this branch
// .. grabbing those convertible types un-necessarily.
f.write( QString::number( value.toULongLong() ).toUtf8() );
}
else if ( value.type() == QVariant::Type::List )
else if ( t == Calamares::ListVariantType )
{
int c = 0;
for ( const auto& it : value.toList() )
@ -281,7 +284,7 @@ dumpYamlElement( QFile& f, const QVariant& value, int indent )
f.write( "[]" );
}
}
else if ( value.type() == QVariant::Type::Map )
else if ( t == Calamares::MapVariantType )
{
f.write( newline );
dumpYaml( f, value.toMap(), indent + 1 );