00001 #include "controllerfactory.h"
00002
00003 #include <tools/stringexception.h>
00004
00005 #include <QDir>
00006 #include <QMap>
00007 #include <QPluginLoader>
00008 #include <QDebug>
00009
00010 #ifdef Q_OS_WIN32
00011 #define FILTER "controller_*.dll"
00012 #else
00013 #define FILTER "libcontroller_*.so"
00014 #endif
00015
00016 namespace control
00017 {
00018
00019 struct ControllerFactoryPrivate
00020 {
00021 ControllerFactoryPrivate(ControllerFactory* qq);
00022
00023 domain::TaskManagerData* data;
00024 ui::UiInterface* ui;
00025
00026 QList<ControllerInterface*> controllers;
00027 QMap<ControllerInterface*, QString> errorMap;
00028
00029 ControllerFactory* q;
00030
00031 void loadControllers();
00032 };
00033
00034 ControllerFactoryPrivate::ControllerFactoryPrivate(ControllerFactory* qq) : q(qq)
00035 {
00036 }
00037
00038 void ControllerFactoryPrivate::loadControllers()
00039 {
00040 Q_ASSERT(data != 0);
00041 Q_ASSERT(ui != 0);
00042
00043 QDir pluginsDir = PLUGINS_PATH;
00044 pluginsDir.setNameFilters(QStringList() << FILTER);
00045
00046
00047 QStringList files = pluginsDir.entryList(QDir::Files);
00048
00049 Q_FOREACH (QString fileName, files)
00050 {
00051 QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
00052
00053 if (pluginLoader.load())
00054 {
00055 ControllerInterface* controller =
00056 qobject_cast<ControllerInterface*>(pluginLoader.instance());
00057
00058 if (controller != 0)
00059 errorMap[controller] = "Not initted";
00060 }
00061 else
00062 {
00063 qDebug() << "Failed to load" << fileName
00064 << "because" << pluginLoader.errorString();
00065 }
00066 }
00067
00068
00069
00070
00071 bool addedOne;
00072 do
00073 {
00074 addedOne = false;
00075
00076 Q_FOREACH (ControllerInterface* controller, errorMap.keys())
00077 {
00078 try
00079 {
00080 controller->init(data, ui, q);
00081 controllers << controller;
00082 errorMap.remove(controller);
00083 addedOne = true;
00084 }
00085 catch (tools::StringException e)
00086 {
00087 errorMap[controller] = e.message();
00088 }
00089 catch (...)
00090 {
00091 errorMap[controller] = "Unknown error.";
00092 }
00093 }
00094 }
00095 while (addedOne);
00096
00097 if (!errorMap.isEmpty())
00098 {
00099 qDebug() << "The following plugins failed to load:";
00100
00101 Q_FOREACH (ControllerInterface* c, errorMap.keys())
00102 qDebug() << " " << c->name() << "because" << errorMap[c];
00103 }
00104 }
00105
00106 ControllerFactory::ControllerFactory(domain::TaskManagerData* data,
00107 ui::UiInterface* ui) :
00108 d(new ControllerFactoryPrivate(this))
00109 {
00110 d->data = data;
00111 d->ui = ui;
00112
00113 d->loadControllers();
00114 }
00115
00116 ControllerFactory::~ControllerFactory()
00117 {
00118 delete d;
00119 }
00120
00121 QList<ControllerInterface*> ControllerFactory::controllers() const
00122 {
00123 return d->controllers;
00124 }
00125
00126 ControllerInterface* ControllerFactory::controllerByType(
00127 ControllerInterface::DataType dataType,
00128 ControllerInterface::ActionType actionType) const
00129 {
00130 Q_FOREACH (ControllerInterface* c, d->controllers)
00131 {
00132 if (c->dataType() == dataType && c->actionType() == actionType)
00133 return c;
00134 }
00135
00136 return 0;
00137 }
00138
00139 QList<ControllerInterface*> ControllerFactory::controllersByAction(
00140 ControllerInterface::ActionType actionType) const
00141 {
00142 QList<ControllerInterface*> ret;
00143
00144 Q_FOREACH (ControllerInterface* c, d->controllers)
00145 {
00146 if (c->actionType() == actionType)
00147 ret << c;
00148 }
00149
00150 return ret;
00151 }
00152
00153 QMap<ControllerInterface*, QString> ControllerFactory::failedControllers() const
00154 {
00155 return d->errorMap;
00156 }
00157
00158 }