From 05d355f21ff0356f056d6f8062475b9d009b65aa Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 13 Jun 2014 16:40:42 +0200 Subject: [PATCH] Config files are YAML, not JSON. We depend on yaml-cpp for parsing. --- CMakeLists.txt | 1 + src/calamares/CMakeLists.txt | 1 + src/calamares/Config.h.in | 1 + src/calamares/Settings.cpp | 76 ++++++++++++----------- src/libcalamares/utils/CalamaresUtils.cpp | 13 ++-- src/libcalamares/utils/Logger.cpp | 4 +- 6 files changed, 50 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a90c41af..c1791b217 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules" ) cmake_policy( SET CMP0023 OLD ) find_package( Qt5 5.3.0 CONFIG REQUIRED Core Gui Widgets LinguistTools ) +find_package( YamlCpp 0.5.1 REQUIRED ) ### ### Calamares application info diff --git a/src/calamares/CMakeLists.txt b/src/calamares/CMakeLists.txt index b2555bce1..bd1c7a7ff 100644 --- a/src/calamares/CMakeLists.txt +++ b/src/calamares/CMakeLists.txt @@ -53,6 +53,7 @@ target_link_libraries( calamares_bin ${CALAMARES_LIBRARIES} Qt5Core Qt5Widgets + yaml-cpp ) install( TARGETS calamares_bin diff --git a/src/calamares/Config.h.in b/src/calamares/Config.h.in index fc57470ec..c139949ee 100644 --- a/src/calamares/Config.h.in +++ b/src/calamares/Config.h.in @@ -4,6 +4,7 @@ #define CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" #define CMAKE_INSTALL_FULL_LIBEXECDIR "${CMAKE_INSTALL_FULL_LIBEXECDIR}" #define CMAKE_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}" +#define CMAKE_INSTALL_FULL_DATADIR "${CMAKE_INSTALL_FULL_DATADIR}" //cmakedefines for CMake variables (e.g. for optdepends) go here diff --git a/src/calamares/Settings.cpp b/src/calamares/Settings.cpp index e0fa03cf4..ccef36f14 100644 --- a/src/calamares/Settings.cpp +++ b/src/calamares/Settings.cpp @@ -17,14 +17,26 @@ */ #include "Settings.h" + +#include "CalamaresApplication.h" #include "utils/CalamaresUtils.h" #include "utils/Logger.h" #include #include -#include -#include -#include + +#include + + +void +operator>>( const YAML::Node& node, QStringList& v ) +{ + for ( int i = 0; i < node.size(); ++i ) + { + v.append( QString::fromStdString( node[ i ].as< std::string >() ) ); + } +} + namespace Calamares { @@ -41,55 +53,45 @@ Settings::instance() Settings::Settings( QObject* parent ) : QObject( parent ) { - QFile file( CalamaresUtils::appDataDir().absoluteFilePath( "settings.json" ) ); - if ( file.exists() && file.canReadLine() ) + QFileInfo settingsFile( CalamaresUtils::appDataDir().absoluteFilePath( "settings.conf" ) ); + if ( APP->isDebug() ) + { + QFileInfo localFile( QDir( QDir::currentPath() ).absoluteFilePath( "settings.conf" ) ); + if ( localFile.exists() && localFile.isReadable() ) + settingsFile.setFile( localFile.absoluteFilePath() ); + } + QFile file( settingsFile.absoluteFilePath() ); + + if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) ) { QByteArray ba = file.readAll(); - QJsonParseError* err = 0; - QJsonDocument document = QJsonDocument::fromJson( ba, err ); - if ( !err && !document.isNull() && !document.isEmpty() ) + cDebug() << ba; + + try { - QJsonObject json = document.object(); + YAML::Node config = YAML::Load( ba.constData() ); + Q_ASSERT( config.IsMap() ); - foreach ( const QJsonValue& val, json[ "modules-search" ].toArray() ) + QStringList rawPaths; + config[ "modules-search" ] >> rawPaths; + for ( int i = 0; i < rawPaths.length(); ++i ) { - if ( !val.isString() || val.toString().isEmpty() ) - continue; - - QString entry = val.toString(); - - if ( entry == "local" ) - { + if ( rawPaths[ i ] == "local" ) m_modulesSearchPaths.append( CalamaresUtils::appDataDir().absolutePath() + QDir::separator() + "modules" ); - } else { - QDir path( entry ); + QDir path( rawPaths[ i ] ); if ( path.exists() && path.isReadable() ) m_modulesSearchPaths.append( path.absolutePath() ); } } - foreach ( const QJsonValue& val, json[ "modules-prepare" ].toArray() ) - { - if ( !val.isString() || val.toString().isEmpty() ) - continue; - - m_viewModulesPrepareList.append( val.toString() ); - } - - foreach ( const QJsonValue& val, json[ "modules-postinstall" ].toArray() ) - { - if ( !val.isString() || val.toString().isEmpty() ) - continue; - - m_viewModulesPostInstallList.append( val.toString() ); - } + config[ "modules-prepare" ] >> m_viewModulesPrepareList; + config[ "modules-postinstall" ] >> m_viewModulesPostInstallList; } - else + catch( YAML::Exception& e ) { - cDebug() << "WARNING: Invalid document " << file.fileName() - << " error: " << err->errorString(); + cDebug() << "WARNING: YAML parser error " << e.what(); } } else diff --git a/src/libcalamares/utils/CalamaresUtils.cpp b/src/libcalamares/utils/CalamaresUtils.cpp index 3cd7cdc3f..761f3134b 100644 --- a/src/libcalamares/utils/CalamaresUtils.cpp +++ b/src/libcalamares/utils/CalamaresUtils.cpp @@ -23,6 +23,8 @@ #include "CalamaresUtils.h" +#include "config.h" + #include #include #include @@ -35,14 +37,11 @@ namespace CalamaresUtils QDir appDataDir() { - QString path; - path = QDir::home().filePath( ".local/share" ); - - path += "/" + QCoreApplication::organizationName(); - QDir d( path ); - d.mkpath( path ); + QDir path( CMAKE_INSTALL_FULL_DATADIR ); + if ( !path.exists() || !path.isReadable() ) + path.mkpath( path.absolutePath() ); - return d; + return path; } diff --git a/src/libcalamares/utils/Logger.cpp b/src/libcalamares/utils/Logger.cpp index 8ddc1f34f..32a5c0c98 100644 --- a/src/libcalamares/utils/Logger.cpp +++ b/src/libcalamares/utils/Logger.cpp @@ -50,8 +50,8 @@ log( const char *msg, unsigned int debugLevel, bool toDisk = true ) { if ( s_threshold < 0 ) { - if ( qApp->arguments().contains( "--verbose" ) || - qApp->arguments().contains( "-v" ) ) + if ( qApp->arguments().contains( "--debug" ) || + qApp->arguments().contains( "-d" ) ) s_threshold = LOGVERBOSE; else #ifdef QT_NO_DEBUG