kdelirc
profileserver.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <QFile>
00015 #include <qxml.h>
00016
00017 #include <kglobal.h>
00018 #include <kstandarddirs.h>
00019 #include <kdebug.h>
00020
00021 #include "profileserver.h"
00022
00023 ProfileServer *ProfileServer::theInstance = 0;
00024
00025 ProfileServer::ProfileServer()
00026 {
00027 theProfiles.setAutoDelete(true);
00028 loadProfiles();
00029 }
00030
00031 ProfileServer::~ProfileServer()
00032 {
00033 }
00034
00035 void ProfileServer::loadProfiles()
00036 {
00037 QStringList theFiles = KGlobal::dirs()->findAllResources("data", "profiles/*.profile.xml");
00038 for(QStringList::iterator i = theFiles.begin(); i != theFiles.end(); ++i)
00039 { kDebug() << "Found data file: " << *i ;
00040 Profile *p = new Profile();
00041 p->loadFromFile(*i);
00042 theProfiles.insert(p->id(), p);
00043 }
00044 }
00045
00046 Profile::Profile()
00047 {
00048
00049 theUnique = true;
00050 theIfMulti = IM_DONTSEND;
00051
00052 theActions.setAutoDelete(true);
00053 }
00054
00055 const ProfileAction *Profile::searchClass(const QString &c) const
00056 {
00057 for(Q3DictIterator<ProfileAction> i(theActions); i.current(); ++i)
00058 if(i.current()->getClass() == c) return i;
00059 return 0;
00060 }
00061
00062 void Profile::loadFromFile(const QString &fileName)
00063 {
00064 charBuffer = "";
00065 curPA = 0;
00066 curPAA = 0;
00067
00068 QFile xmlFile(fileName);
00069 QXmlInputSource source(&xmlFile);
00070 QXmlSimpleReader reader;
00071 reader.setContentHandler(this);
00072 reader.parse(source);
00073 }
00074
00075 const ProfileAction *ProfileServer::getAction(const QString &appId, const QString &actionId) const
00076 {
00077 if(theProfiles[appId])
00078 if(theProfiles[appId]->theActions[actionId])
00079 return theProfiles[appId]->theActions[actionId];
00080 return 0;
00081 }
00082
00083 const QString &ProfileServer::getServiceName(const QString &appId) const
00084 {
00085 if(theProfiles[appId])
00086 return theProfiles[appId]->serviceName();
00087 return QString();
00088 }
00089
00090 const ProfileAction *ProfileServer::getAction(const QString &appId, const QString &objId, const QString &prototype) const
00091 {
00092 return getAction(appId, objId + "::" + prototype);
00093 }
00094
00095 bool Profile::characters(const QString &data)
00096 {
00097 charBuffer += data;
00098 return true;
00099 }
00100
00101 bool Profile::startElement(const QString &, const QString &, const QString &name, const QXmlAttributes &attributes)
00102 {
00103 if(name == "profile")
00104 { theId = attributes.value("id");
00105 theServiceName = attributes.value("servicename");
00106 }
00107 else if(name == "action")
00108 { curPA = new ProfileAction;
00109 curPA->setObjId(attributes.value("objid"));
00110 curPA->setPrototype(attributes.value("prototype"));
00111 curPA->setClass(attributes.value("class"));
00112 curPA->setMultiplier(attributes.value("multiplier").isEmpty() ? 1.0 : attributes.value("multiplier").toFloat());
00113 curPA->setRepeat(attributes.value("repeat") == "1");
00114 curPA->setAutoStart(attributes.value("autostart") == "1");
00115 }
00116 else if(name == "instances")
00117 { theUnique = attributes.value("unique") == "1";
00118 theIfMulti = attributes.value("ifmulti") == "sendtotop" ? IM_SENDTOTOP : attributes.value("ifmulti") == "sendtobottom" ? IM_SENDTOBOTTOM : attributes.value("ifmulti") == "sendtoall" ? IM_SENDTOALL : IM_DONTSEND;
00119 }
00120 else if(name == "argument")
00121 { curPA->theArguments.append(ProfileActionArgument());
00122 curPAA = &(curPA->theArguments.last());
00123 curPAA->setAction(curPA);
00124 curPAA->setType(attributes.value("type"));
00125 }
00126 else if(name == "range" && curPAA)
00127 curPAA->setRange(qMakePair(attributes.value("min").toInt(), attributes.value("max").toInt()));
00128
00129 charBuffer = "";
00130 return true;
00131 }
00132
00133 bool Profile::endElement(const QString &, const QString &, const QString &name)
00134 {
00135 if(name == "name")
00136 if(curPA)
00137 curPA->setName(charBuffer);
00138 else
00139 theName = charBuffer;
00140 else if(name == "author")
00141 theAuthor = charBuffer;
00142 else if(name == "comment" && curPA && !curPAA)
00143 curPA->setComment(charBuffer);
00144 else if(name == "default" && curPA && curPAA)
00145 curPAA->setDefault(charBuffer);
00146 else if(name == "comment" && curPA && curPAA)
00147 curPAA->setComment(charBuffer);
00148 else if(name == "action")
00149 {
00150 curPA->setProfile(this);
00151 theActions.insert(curPA->objId() + "::" + curPA->prototype(), curPA);
00152 curPA = 0;
00153 }
00154 else if(name == "argument")
00155 curPAA = 0;
00156
00157 charBuffer = "";
00158 return true;
00159 }