defuze.me  Client
paramscore.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "paramscore.hpp"
00012 #include "exception.hpp"
00013 #include <QtCore/QRegExp>
00014 #include <QDebug>
00015 
00016 using namespace Params;
00017 
00018 ParamsCore::ParamsCore(QStringList &arguments)
00019 {
00020     handleCmdLineParameters(arguments);
00021     //_printCmdLineParameters();
00022 }
00023 
00024 ParamsCore::~ParamsCore()
00025 {
00026 }
00027 
00028 void ParamsCore::init(Cores *cores)
00029 {
00030     setParamsName("params");
00031     setParamsBackEnd(Parameterizable::SETTINGS);
00032     registerToParamsCore(cores->params());
00033 }
00034 
00035 void ParamsCore::aboutToQuit()
00036 {
00037 }
00038 
00039 const QHash<QString, QVariant> ParamsCore::cmdLineParameters(QString paramsName) const
00040 {
00041     if (!_cmdLineParameters.contains(paramsName))
00042         _cmdLineParameters[paramsName];
00043     return _cmdLineParameters[paramsName];
00044 }
00045 
00046 void ParamsCore::handleCmdLineParameters(QStringList &arguments)
00047 {
00048     QRegExp paramsPattern("--([a-z_]+)-([a-z0-9_-]+)(=(.+))?");
00049 
00050     foreach (QString argument, arguments)
00051     {
00052         if (paramsPattern.exactMatch(argument))
00053         {
00054             QStringList matches = paramsPattern.capturedTexts();
00055             if (matches[4] != "")
00056                 _cmdLineParameters[matches[1]][matches[2]] = matches[4];
00057             else
00058                 _cmdLineParameters[matches[1]][matches[2]] = true;
00059         }
00060     }
00061 }
00062 
00063 void ParamsCore::registerParameterizable(Parameterizable *parameterizable)
00064 {
00065     if (_parameterizables.contains(parameterizable->paramsName()))
00066         throw_exception(0x01, "Can't register a Parameterizable (name is already used)");
00067     _parameterizables[parameterizable->paramsName()] = parameterizable;
00068 }
00069 
00070 void ParamsCore::_printCmdLineParameters()
00071 {
00072     QHash<QString, QHash<QString, QVariant> >::const_iterator i = _cmdLineParameters.constBegin();
00073     while (i != _cmdLineParameters.constEnd())
00074     {
00075         qDebug() << i.key();
00076         QHash<QString, QVariant>::const_iterator j = i.value().constBegin();
00077         while (j != i.value().constEnd())
00078         {
00079             qDebug() << "  " << j.key() << "=" << j.value().toString();
00080             ++j;
00081         }
00082         ++i;
00083     }
00084 }
00085 
00086 void ParamsCore::defineParams()
00087 {
00088     setParameter("first_launch", true);
00089 }