A bunch of work on loading plugins.

Also switched to C++11.
This commit is contained in:
Teo Mrnjavac 2014-06-18 18:05:04 +02:00
parent 1da7ba446d
commit 13fcf387c7
15 changed files with 424 additions and 17 deletions

View File

@ -1,6 +1,8 @@
project( calamares )
cmake_minimum_required( VERSION 2.8.12 )
set( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wl,--no-undefined" )
#set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
cmake_policy( SET CMP0023 OLD )

View File

@ -8,8 +8,11 @@ set( calamaresSources
main.cpp
CalamaresApplication.cpp
CalamaresWindow.cpp
Module.cpp
ModuleLoader.cpp
ViewManager.cpp
Settings.cpp
YamlUtils.cpp
viewpages/PagePlugin.cpp
viewpages/AbstractPage.cpp
@ -43,6 +46,7 @@ add_executable( calamares_bin ${final_src} )
SET_TARGET_PROPERTIES(calamares_bin
PROPERTIES
AUTOMOC TRUE
ENABLE_EXPORTS TRUE
RUNTIME_OUTPUT_NAME calamares
)

120
src/calamares/Module.cpp Normal file
View File

@ -0,0 +1,120 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#include "Module.h"
#include "YamlUtils.h"
#include "utils/Logger.h"
#include <yaml-cpp/yaml.h>
#include <QFile>
#include <QString>
// Example module.conf
/*
---
type: "core" #core or view
name: "foo" #the module name. must be unique and same as the parent directory
interface: "qtplugin" #can be: qtplugin, python, process, ...
requires: [] #list of module names that must also be loaded. only applies to
#binary plugins! these are actual link-time dependencies, not
#conceptual dependencies for the setup procedure
*/
void
operator>>( const YAML::Node& node, Calamares::Module& m )
{
m.m_name = QString::fromStdString( node[ "name" ].as< std::string >() );
QString typeString = QString::fromStdString( node[ "type" ].as< std::string >() );
if ( typeString == "core" )
{
m.m_type = Calamares::Module::Core;
}
else if ( typeString == "view" )
{
m.m_type = Calamares::Module::View;
m.m_interface = Calamares::Module::QtPlugin;
}
if ( m.m_type != Calamares::Module::View )
{
QString interfaceString = QString::fromStdString( node[ "interface" ].as< std::string >() );
if ( interfaceString == "qtplugin" )
{
m.m_interface = Calamares::Module::QtPlugin;
}
else if ( interfaceString == "python" )
{
m.m_interface = Calamares::Module::Python;
}
else if ( interfaceString == "process" )
{
m.m_interface = Calamares::Module::Process;
}
}
if ( node[ "requires" ] && node[ "requires" ].IsSequence() )
{
node[ "requires" ] >> m.m_requiredModules;
}
}
Calamares::Module*
Calamares::Module::loadFromFile( const QString& path )
{
QFile metadataFile( path );
if ( metadataFile.exists() && metadataFile.open( QFile::ReadOnly | QFile::Text ) )
{
QByteArray ba = metadataFile.readAll();
cDebug() << Q_FUNC_INFO << "module metadata file: " << ba;
try
{
YAML::Node moduleDocument = YAML::Load( ba.constData() );
if ( !moduleDocument.IsMap() )
{
cDebug() << Q_FUNC_INFO << "bad module metadata format"
<< path;
return nullptr;
}
Module* m = new Module();
moduleDocument >> *m;
return m;
}
catch ( YAML::Exception& e )
{
cDebug() << "WARNING: YAML parser error " << e.what();
return nullptr;
}
}
return nullptr;
}
QString
Calamares::Module::name()
{
return m_name;
}

72
src/calamares/Module.h Normal file
View File

@ -0,0 +1,72 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#ifndef CALAMARESMODULE_H
#define CALAMARESMODULE_H
#include "DllMacro.h"
#include <QStringList>
namespace YAML
{
class Node;
}
namespace Calamares
{
class Module;
}
void operator>>( const YAML::Node& node, Calamares::Module& m );
namespace Calamares
{
class UIDLLEXPORT Module
{
public:
static Module* loadFromFile( const QString& path );
QString name();
enum Type
{
Core,
View
};
enum Interface
{
QtPlugin,
Python,
Process
};
private:
QString m_name;
Type m_type;
Interface m_interface;
QStringList m_requiredModules;
friend void ::operator>>( const YAML::Node& node, Calamares::Module& m );
};
}
#endif // CALAMARESMODULE_H

View File

@ -0,0 +1,103 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#include "ModuleLoader.h"
#include "utils/Logger.h"
#include <yaml-cpp/yaml.h>
#include <QDir>
#define MODULE_CONFIG_FILENAME "module.conf"
namespace Calamares
{
ModuleLoader::ModuleLoader( const QStringList& paths, QObject* parent )
: QObject( parent )
, m_paths( paths )
{
}
ModuleLoader::~ModuleLoader()
{
foreach ( Module* m, m_availableModules )
{
delete m;
}
}
void
ModuleLoader::exec()
{
// We start from a list of paths in m_paths. Each of those is a directory that
// might (should) contain Calamares modules of any type/interface.
// For each modules search path (directory), it is expected that each module
// lives in its own subdirectory. This subdirectory must have the same name as
// the module name, and must contain a settings file named module.conf.
// If at any time the module loading procedure finds something unexpected, it
// silently skips to the next module or search path. --Teo 6/2014
foreach ( const QString& path, m_paths )
{
QDir currentDir( path );
if ( currentDir.exists() && currentDir.isReadable() )
{
QStringList subdirs = currentDir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot );
foreach ( const QString& subdir, subdirs )
{
bool success = currentDir.cd( subdir );
if ( success && currentDir.isReadable() )
{
QFileInfo metadataFileInfo( currentDir.absoluteFilePath( MODULE_CONFIG_FILENAME ) );
if ( ! ( metadataFileInfo.exists() && metadataFileInfo.isReadable() ) )
{
cDebug() << Q_FUNC_INFO << "unreadable file: "
<< metadataFileInfo.absoluteFilePath();
continue;
}
Module* moduleInfo = Module::loadFromFile( metadataFileInfo.absoluteFilePath() );
if ( moduleInfo &&
( moduleInfo->name() == currentDir.dirName() ) &&
( !m_availableModules.contains( moduleInfo->name() ) ) )
{
m_availableModules.insert( moduleInfo->name(), moduleInfo );
}
currentDir.cdUp();
}
else
{
cDebug() << Q_FUNC_INFO << "cannot cd into module directory "
<< path << "/" << subdir;
}
}
}
else
{
cDebug() << Q_FUNC_INFO << "bad search path " << path;
}
}
// At this point m_availableModules is filled with whatever was found in the
// search paths.
}
}

View File

@ -0,0 +1,54 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#ifndef MODULELOADER_H
#define MODULELOADER_H
#include "Module.h"
#include <QMap>
#include <QObject>
#include <QStringList>
namespace Calamares
{
class Module;
class ModuleLoader : public QObject
{
Q_OBJECT
public:
explicit ModuleLoader( const QStringList& paths, QObject* parent = 0 );
virtual ~ModuleLoader();
void exec();
signals:
void done();
private:
QMap< QString, Module* > m_availableModules;
QStringList m_paths;
};
}
#endif // MODULELOADER_H

View File

@ -21,6 +21,7 @@
#include "CalamaresApplication.h"
#include "utils/CalamaresUtils.h"
#include "utils/Logger.h"
#include "YamlUtils.h"
#include <QDir>
#include <QFile>
@ -28,16 +29,6 @@
#include <yaml-cpp/yaml.h>
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
{

View File

@ -0,0 +1,30 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#include "YamlUtils.h"
#include <yaml-cpp/yaml.h>
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 >() ) );
}
}

31
src/calamares/YamlUtils.h Normal file
View File

@ -0,0 +1,31 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* 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/>.
*/
#ifndef YAMLUTILS_H
#define YAMLUTILS_H
#include <QStringList>
namespace YAML
{
class Node;
}
void operator>>( const YAML::Node& node, QStringList& v );
#endif // YAMLUTILS_H

View File

@ -31,10 +31,8 @@ class UIDLLEXPORT AbstractPage : public QWidget
Q_OBJECT
public:
explicit AbstractPage(QWidget *parent = 0);
virtual ~AbstractPage() {}
signals:
public slots:
};

View File

@ -33,6 +33,7 @@ class UIDLLEXPORT PagePlugin : public QObject
Q_OBJECT
public:
explicit PagePlugin( QObject *parent = 0 );
virtual ~PagePlugin() {}
signals:
void done();

View File

@ -17,8 +17,8 @@ set( libSources
kdsingleapplicationguard/kdtoolsglobal.cpp
kdsingleapplicationguard/kdlockedsharedmemorypointer.cpp
utils/Logger.cpp
utils/CalamaresUtils.cpp
utils/Logger.cpp
)
include_directories(

View File

@ -8,5 +8,6 @@ calamares_add_plugin( greeting
UI
LINK_LIBRARIES
${CALAMARES_LIBRARIES}
calamares_bin
SHARED_LIB
)

View File

@ -20,6 +20,6 @@
GreetingPage::GreetingPage( QWidget* parent )
: AbstractPage( parent )
: Calamares::AbstractPage( parent )
{
}

View File

@ -19,6 +19,6 @@
#include "GreetingPagePlugin.h"
GreetingPagePlugin::GreetingPagePlugin( QObject *parent )
: PagePlugin( parent )
: Calamares::PagePlugin( parent )
{
}