Qt IOC Container 3.5


Util.cpp

Go to the documentation of this file.
00001 
00002 #include <stdlib.h>
00003 
00004 #include <QStringList>
00005 #include <QRegExp>
00006 #include <QTextStream>
00007 #include <QSettings>
00008 
00009 #include "Util.h"
00010 
00011 #include "exception/VariableNotFoundException.h"
00012 #include "exception/DuplicateObjectIdException.h"
00013 #include "exception/PluginLoadingException.h"
00014 #include "exception/InvalidConnectionTypeException.h"
00015 
00016 using namespace qic;
00017 
00018 Qt::ConnectionType
00019 Util::translateConnectionType (QString type)
00020 {
00021   QString test = type.trimmed();
00022   if (test == "auto")
00023   {
00024     return Qt::AutoConnection;
00025   }
00026   if (test == "direct")
00027   {
00028     return Qt::DirectConnection;
00029   }
00030   if (test == "queued")
00031   {
00032     return Qt::QueuedConnection;
00033   }
00034   throw InvalidConnectionTypeException (type);
00035 }
00036 
00037 QString
00038 Util::asXml(QDomNode & node)
00039 {
00040   QString buf;
00041   QTextStream out(&buf);
00042 
00043   out << node;
00044   return buf;
00045 }
00046 
00047 QString
00048 Util::translateVariable (QString name)
00049 {
00050   QString pattern = "\\$\\{([^\\$\\}]+)\\}";
00051   QRegExp rx (pattern);
00052 
00053   QStringList list;
00054   int pos = 0;
00055   int prev = 0;
00056 
00057   while ((pos = rx.indexIn(name, pos)) != -1)
00058   {
00059     list << name.mid(prev, pos-prev);
00060 
00061     QString v = rx.cap(1);
00062     QString env = "";
00063     try
00064     {
00065       env = getEnvVariable(v);
00066     }
00067     catch (VariableNotFoundException &)
00068     {}
00069 
00070     QString setting = getSetting(v, env);
00071     if (setting == "")
00072     {
00073       throw VariableNotFoundException (v);
00074     }
00075 
00076     list << setting;
00077 
00078     pos += rx.matchedLength();
00079     prev = pos;
00080   }
00081   list << name.mid(prev);
00082   return list.join(QString(""));
00083 }
00084 
00085 QString
00086 Util::getSetting (QString name, QString defaultValue)
00087 {
00088   QSettings settings;
00089   return settings.value(name, defaultValue).toString();
00090 }
00091 
00092 QString
00093 Util::getEnvVariable (QString name)
00094 {
00095   char * val = getenv (name.toStdString().c_str());
00096   if (val == NULL)
00097   {
00098     throw VariableNotFoundException (name);
00099   }
00100   else
00101   {
00102     return QString(val);
00103   }
00104 }
00105 
00106 void
00107 Util::loadPlugin (QString id, QString plugin_path,
00108                   QHash <QString, QPluginLoader *> & loader_map,
00109                   QHash <QString, QObject *> & object_map)
00110 {
00111   if (object_map.contains(id))
00112   {
00113     throw DuplicateObjectIdException (id);
00114   }
00115 
00116   QPluginLoader * loader = new QPluginLoader(plugin_path);
00117   if (loader)
00118   {
00119     QObject * plugin = loader->instance();
00120     if (plugin)
00121     {
00122       object_map[id] = plugin;
00123       loader_map[id] = loader;
00124     }
00125     else
00126     {
00127       delete loader;
00128       throw PluginLoadingException(plugin_path);
00129     }
00130   }
00131   else
00132   {
00133     throw PluginLoadingException("Cannot create QPluginLoader to load " + plugin_path);
00134   }
00135 }
00136