00001 #include "themeloader.h"
00002
00003 #include <controllerfactory.h>
00004
00005 #include <domain/resourcetype.h>
00006 #include <domain/usertype.h>
00007 #include <domain/tasktype.h>
00008 #include <domain/taskmanagerdata.h>
00009
00010 #include <tools/stringexception.h>
00011
00012 #include <QDomDocument>
00013 #include <QFile>
00014
00015 #include <limits>
00016
00017 using namespace control;
00018
00019 QString ThemeLoader::description() const
00020 {
00021 return "Load theme";
00022 }
00023
00024 ControllerInterface::ActionType ThemeLoader::actionType() const
00025 {
00026 return LoadThemeAction;
00027 }
00028
00029 ControllerInterface::DataType ThemeLoader::dataType() const
00030 {
00031 return FileData;
00032 }
00033
00034 QString ThemeLoader::name() const
00035 {
00036 return "ThemeLoader";
00037 }
00038
00039 bool ThemeLoader::userIsValid() const
00040 {
00041 return true;
00042 }
00043
00044 #define FOREACH_ELEMENT(element,node) \
00045 for (QDomElement element = node.firstChildElement(); !element.isNull(); \
00046 element = element.nextSiblingElement())
00047
00048 void ThemeLoader::execute()
00049 {
00050 if(data->hasData())
00051 {
00052
00053
00054
00055
00056
00057 data->clear();
00058 }
00059 data->clearTheme();
00060
00061 QString fileName = ui->getOpenFileName("", "XML files(*.xml)");
00062 if (fileName.isEmpty())
00063 return;
00064
00065 QDomDocument doc;
00066 QFile file(fileName);
00067
00068 if(!file.open(QIODevice::ReadOnly))
00069 {
00070 ui->showError("Could not open the specified file.");
00071 return;
00072 }
00073
00074 QString errorString;
00075 int errorLine, errorColumn;
00076
00077 if (!doc.setContent(&file, &errorString, &errorLine, &errorColumn))
00078 {
00079 file.close();
00080 QString error("The file could not be loaded\nLine %1, Column %2: %3");
00081 ui->showError(error.arg(errorLine).arg(errorColumn).arg(errorString));
00082 return;
00083 }
00084
00085 file.close();
00086
00087 QDomElement root = doc.documentElement();
00088 if (root.tagName() != "t:theme")
00089 {
00090 ui->showError("This is not a correct XML file.");
00091 return;
00092 }
00093
00094 QDomElement resourceTypes = root.firstChildElement("t:resourceTypes");
00095
00096 FOREACH_ELEMENT(type, resourceTypes)
00097 {
00098 QString id = type.attribute("id");
00099 QString name = type.attribute("name");
00100
00101 data->createResourceType(id, name);
00102 }
00103
00104 QDomElement userTypes = root.firstChildElement("t:userTypes");
00105
00106 FOREACH_ELEMENT(type, userTypes)
00107 {
00108 QString id = type.attribute("id");
00109 QString name = type.attribute("name");
00110
00111 data->createUserType(id, name);
00112 }
00113
00114 QDomElement taskTypes = root.firstChildElement("t:taskTypes");
00115
00116 FOREACH_ELEMENT(type, taskTypes)
00117 {
00118 QString id = type.attribute("id");
00119 QString name = type.attribute("name");
00120 domain::TaskType* taskType = data->createTaskType(id, name);
00121
00122 FOREACH_ELEMENT(element, type)
00123 {
00124 QString tag = element.tagName();
00125
00126 if (tag == "t:fields")
00127 {
00128 FOREACH_ELEMENT(field, element)
00129 {
00130 QString id = field.attribute("id");
00131 QString name = field.attribute("name");
00132 QString nature = field.attribute("nature");
00133
00134 domain::Field f;
00135 f.id = id;
00136 f.name = name;
00137
00138 if (nature == "textual")
00139 f.nature = domain::Field::Textual;
00140 else
00141 f.nature = domain::Field::Numerical;
00142
00143 taskType->addField(f);
00144 }
00145 }
00146 else if (tag == "t:requires")
00147 {
00148 FOREACH_ELEMENT(requirement, element)
00149 {
00150 QString type = requirement.attribute("type");
00151 QString min = requirement.attribute("min");
00152 QString max = requirement.attribute("max");
00153
00154 domain::Type* typePtr = data->typeById(type);
00155 domain::UserType* userType = qobject_cast<domain::UserType*>(typePtr);
00156 domain::ResourceType* resourceType =
00157 qobject_cast<domain::ResourceType*>(typePtr);
00158
00159 unsigned minValue, maxValue;
00160 minValue = min.toUInt();
00161
00162 if (max == "*")
00163 maxValue = std::numeric_limits<unsigned>::max();
00164 else
00165 maxValue = max.toUInt();
00166
00167 if (typePtr == 0)
00168 {
00169 ui->showError(QString("Type %1 not defined").arg(type));
00170 data->clearTheme();
00171 return;
00172 }
00173 else if (userType != 0)
00174 taskType->addRequirement(userType, minValue, maxValue);
00175 else if (resourceType != 0)
00176 taskType->addRequirement(resourceType, minValue, maxValue);
00177 else
00178 {
00179 ui->showError(QString("Type %1 may not be a task type").arg(type));
00180 data->clearTheme();
00181 return;
00182 }
00183 }
00184 }
00185 else if (tag == "t:owners")
00186 {
00187 FOREACH_ELEMENT(owner, element)
00188 {
00189 QString typeStr = owner.attribute("type");
00190 domain::Type* type = data->typeById(typeStr);
00191 domain::UserType* userType = qobject_cast<domain::UserType*>(type);
00192
00193 if (type == 0)
00194 {
00195 ui->showError(QString("Type %1 not defined").arg(typeStr));
00196 data->clearTheme();
00197 return;
00198 }
00199 else if (userType == 0)
00200 {
00201 ui->showError(QString("Type %1 should be a user type").arg(typeStr));
00202 data->clearTheme();
00203 return;
00204 }
00205
00206 taskType->addPotentialOwner(userType);
00207 }
00208 }
00209 }
00210 }
00211
00212 ui->showMessage("Theme successfully loaded");
00213 }
00214
00215 void ThemeLoader::init(domain::TaskManagerData* data, ui::UiInterface* ui,
00216 ControllerFactory* factory)
00217 {
00218 ControllerInterface::init(data, ui, factory);
00219
00220
00221
00222
00223
00224 }
00225
00226 Q_EXPORT_PLUGIN2(controller_dataloader, ThemeLoader)
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254