summaryrefslogtreecommitdiffstats
path: root/kdelirc/kdelirc/profileserver.cpp
blob: 484deace32968f8a109977b454c776b3e54ac5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
//
// C++ Implementation: $MODULE$
//
// Description:
//
//
// Author: Gav Wood <gav@kde.org>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <qfile.h>
#include <qxml.h>

#include <kglobal.h>
#include <kstandarddirs.h>
#include <kdebug.h>

#include "profileserver.h"

ProfileServer *ProfileServer::theInstance = 0;

ProfileServer::ProfileServer()
{
	theProfiles.setAutoDelete(true);
	loadProfiles();
}

ProfileServer::~ProfileServer()
{
}

void ProfileServer::loadProfiles()
{
	QStringList theFiles = KGlobal::dirs()->findAllResources("data", "profiles/*.profile.xml");
	for(QStringList::iterator i = theFiles.begin(); i != theFiles.end(); ++i)
	{	kdDebug() << "Found data file: " << *i << endl;
		Profile *p = new Profile();
		p->loadFromFile(*i);
		theProfiles.insert(p->id(), p);
	}
}

Profile::Profile()
{
	// set up defaults
	theUnique = true;
	theIfMulti = IM_DONTSEND;

	theActions.setAutoDelete(true);
}

const ProfileAction *Profile::searchClass(const QString &c) const
{
	for(QDictIterator<ProfileAction> i(theActions); i.current(); ++i)
		if(i.current()->getClass() == c) return i;
	return 0;
}

void Profile::loadFromFile(const QString &fileName)
{
	charBuffer = "";
	curPA = 0;
	curPAA = 0;

	QFile xmlFile(fileName);
	QXmlInputSource source(&xmlFile);
	QXmlSimpleReader reader;
	reader.setContentHandler(this);
	reader.parse(source);
}

const ProfileAction *ProfileServer::getAction(const QString &appId, const QString &actionId) const
{
	if(theProfiles[appId])
		if(theProfiles[appId]->theActions[actionId])
			return theProfiles[appId]->theActions[actionId];
	return 0;
}

const QString &ProfileServer::getServiceName(const QString &appId) const
{
	if(theProfiles[appId])
		return theProfiles[appId]->serviceName();
	return QString::null;
}

const ProfileAction *ProfileServer::getAction(const QString &appId, const QString &objId, const QString &prototype) const
{
	return getAction(appId, objId + "::" + prototype);
}

bool Profile::characters(const QString &data)
{
	charBuffer += data;
	return true;
}

bool Profile::startElement(const QString &, const QString &, const QString &name, const QXmlAttributes &attributes)
{
	if(name == "profile")
	{	theId = attributes.value("id");
		theServiceName = attributes.value("servicename");
	}
	else if(name == "action")
	{	curPA = new ProfileAction;
		curPA->setObjId(attributes.value("objid"));
		curPA->setPrototype(attributes.value("prototype"));
		curPA->setClass(attributes.value("class"));
		curPA->setMultiplier(attributes.value("multiplier").isEmpty() ? 1.0 : attributes.value("multiplier").toFloat());
		curPA->setRepeat(attributes.value("repeat") == "1");
		curPA->setAutoStart(attributes.value("autostart") == "1");
	}
	else if(name == "instances")
	{	theUnique = attributes.value("unique") == "1";
		theIfMulti = attributes.value("ifmulti") == "sendtotop" ? IM_SENDTOTOP : attributes.value("ifmulti") == "sendtobottom" ? IM_SENDTOBOTTOM : attributes.value("ifmulti") == "sendtoall" ? IM_SENDTOALL : IM_DONTSEND;
	}
	else if(name == "argument")
	{	curPA->theArguments.append(ProfileActionArgument());
		curPAA = &(curPA->theArguments.last());
		curPAA->setAction(curPA);
		curPAA->setType(attributes.value("type"));
	}
	else if(name == "range" && curPAA)
		curPAA->setRange(qMakePair(attributes.value("min").toInt(), attributes.value("max").toInt()));

	charBuffer = "";
	return true;
}

bool Profile::endElement(const QString &, const QString &, const QString &name)
{
	if(name == "name")
		if(curPA)
			curPA->setName(charBuffer);
		else
			theName = charBuffer;
	else if(name == "author")
		theAuthor = charBuffer;
	else if(name == "comment" && curPA && !curPAA)
		curPA->setComment(charBuffer);
	else if(name == "default" && curPA && curPAA)
		curPAA->setDefault(charBuffer);
	else if(name == "comment" && curPA && curPAA)
		curPAA->setComment(charBuffer);
	else if(name == "action")
	{
		curPA->setProfile(this);
		theActions.insert(curPA->objId() + "::" + curPA->prototype(), curPA);
		curPA = 0;
	}
	else if(name == "argument")
		curPAA = 0;

	charBuffer = "";
	return true;
}