• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kremotecontrol

  • sources
  • kde-4.12
  • kdeutils
  • kremotecontrol
  • libkremotecontrol
profileserver.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Michael Zanetti <michael_zanetti@gmx.net>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 */
19 
20 #include "profileserver.h"
21 #include "profileactiontemplate.h"
22 #include "executionengine.h"
23 #include "dbusaction.h"
24 #include "remote.h"
25 
26 #include <kdebug.h>
27 #include <kglobal.h>
28 #include <kstandarddirs.h>
29 
30 #include <QtCore/QFileInfo>
31 #include <QtGui/QTextDocument>
32 #include <QtXml/QXmlSimpleReader>
33 #include <QtXmlPatterns/QXmlSchemaValidator>
34 #include <QtXmlPatterns/QXmlSchema>
35 #include <klocalizedstring.h>
36 
37 class ProfileServerPrivate
38 {
39  private:
40  QList<Profile*> m_allProfiles;
41 
42  public:
43  ProfileServerPrivate();
44  void addProfile(Profile* profile);
45  QList<Profile*> allProfiles();
46 
47  ~ProfileServerPrivate() {
48  while (!m_allProfiles.isEmpty()){
49  delete m_allProfiles.takeFirst();
50  }
51  }
52 };
53 
54 K_GLOBAL_STATIC(ProfileServerPrivate, instance)
55 
56 QList<Profile*> ProfileServerPrivate::allProfiles() {
57  return m_allProfiles;
58 }
59 
60 ProfileServerPrivate::ProfileServerPrivate() {
61  ProfileServer::ProfileXmlContentHandler *handler = new ProfileServer::ProfileXmlContentHandler(KUrl::fromLocalFile(KGlobal::dirs()->findResource("data",QLatin1String( "kremotecontrol/profiles/profile.xsd" ))));
62  foreach( Profile *profile, handler->loadProfilesFromFiles(KGlobal::dirs()->findAllResources("data", QLatin1String( "kremotecontrol/profiles/*.profile.xml" )))){
63  addProfile(profile);
64  }
65 }
66 
67 void ProfileServerPrivate::addProfile( Profile* profile) {
68  for(int i =0; i< m_allProfiles.size(); i++){
69  Profile *tProfile = m_allProfiles.at(i);
70  if(profile->profileId() == tProfile->profileId()){
71  if( 1 == profile->compareVersion(tProfile)){
72  // new profileversion is greater as current -> replace
73  m_allProfiles.replace(i, profile);
74  return;
75  } else {
76  // in this case keep profile (first come first served...)
77  return;
78  }
79  }
80  }
81  // Profile is no in list. Append
82  m_allProfiles.append(profile);
83 }
84 
85 KREMOTECONTROL_EXPORT void ProfileServer::addProfile(Profile* profile) {
86  instance->addProfile(profile);
87 }
88 
89 KREMOTECONTROL_EXPORT QList< Profile*> ProfileServer::allProfiles() {
90  return instance->allProfiles();
91 }
92 
93 KREMOTECONTROL_EXPORT Profile* ProfileServer::profile(const QString& profileId) {
94  foreach(Profile *profile, instance->allProfiles()){
95  if(profile->profileId() == profileId){
96  return profile;
97  }
98  }
99  kDebug() << "Profile" << profileId<< "not found.";
100  return 0;
101 }
102 
103 KREMOTECONTROL_EXPORT QList<ProfileActionTemplate> ProfileServer::actionTemplateList(const QString& remote, Profile* profile) {
104  QList<ProfileActionTemplate> retList;
105  foreach(const ProfileActionTemplate &actionTemplate, profile->actionTemplates()){
106  kDebug() << "got template" << actionTemplate.actionTemplateId() << "with button" << actionTemplate.buttonName();
107  foreach(const RemoteControlButton &button, RemoteControl(remote).buttons()){
108  kDebug() << "got button" << button.name();
109  if(button.name() == actionTemplate.buttonName()){
110  retList.append(actionTemplate);
111  }
112  }
113  }
114  return retList;
115 }
116 
117 ProfileServer::ProfileXmlContentHandler::ProfileXmlContentHandler(const QUrl &schemaFile) {
118  m_schema = new QXmlSchema();
119  m_schema->setMessageHandler(this);
120  m_schema->load(schemaFile);
121 }
122 
123 ProfileServer::ProfileXmlContentHandler::~ProfileXmlContentHandler() {
124  delete m_schema;
125 }
126 
127 bool ProfileServer::ProfileXmlContentHandler::validateFile(const QString& fileName) {
128  if ( m_schema->isValid() ) {
129  const QStringList theFiles = KGlobal::dirs()->findAllResources("data", QLatin1String( "kremotecontrol/profiles/*.profile.xml" ));
130  QXmlSchemaValidator validator(*m_schema);
131  return validator.validate( QUrl::fromLocalFile(fileName));
132  }
133  return false;
134 }
135 
136 void ProfileServer::ProfileXmlContentHandler::handleMessage(QtMsgType type, const QString& description, const QUrl& identifier, const QSourceLocation& sourceLocation) {
137  Q_UNUSED(type);
138  Q_UNUSED(identifier);
139  Q_UNUSED(sourceLocation);
140  QTextDocument document;
141  document.setHtml(description);
142  kDebug() << "Error validating xml file " << sourceLocation.uri().toString() << " Message " << document.toPlainText();
143 }
144 
145 ProfileServer::ProfileSupportedByRemote KREMOTECONTROL_EXPORT ProfileServer::isProfileAvailableForRemote(Profile* profile, const Remote& remote) {
146  QStringList tProfilActionNames;
147  foreach(const ProfileActionTemplate profileAction, profile->actionTemplates()) {
148  if (! profileAction.buttonName().isEmpty()) {
149  tProfilActionNames << profileAction.buttonName();
150  }
151  }
152  if (tProfilActionNames.size() == 0) {
153  return ProfileServer::NO_ACTIONS_DEFINED;
154  }
155  int found=0;
156  foreach(const QString & tProfilActionName, tProfilActionNames) {
157  foreach(const RemoteControlButton &button, RemoteControl(remote.name()).buttons()) {
158  if(button.name() == tProfilActionName){
159  found++;
160  }
161  }
162  }
163  if (found == 0) {
164  return ProfileServer::NOT_SUPPORTED;
165  } else if (found != tProfilActionNames.size()) {
166  return ProfileServer::PARTIAL_SUPPORTED;
167  }
168  return ProfileServer::FULL_SUPPORTED;
169 }
170 
171 /*
172 ********************************************************************
173 * ProfileServer::ProfileXmlContentHandler
174 *
175 ********************************************************************
176 */
177 
178 QList<Profile*> ProfileServer::ProfileXmlContentHandler::loadProfilesFromFiles(const QStringList& files) {
179  QList<Profile*> profileList;
180  foreach(const QString &file, files) {
181  if(validateFile(file)) {
182  Profile *profile = parseFile(file);
183  if(profile){
184  profileList.append(profile);
185  }
186  }
187  }
188  return profileList;
189 }
190 
191 Profile * ProfileServer::ProfileXmlContentHandler::parseFile(const QString& fileName) {
192  //QString id = fileName.left(filename.indexOf(".profile.xml"));
193  QFile file( fileName );
194 
195  QString profileId = QFileInfo(fileName).fileName();
196  profileId = profileId.left(profileId.indexOf(QLatin1String( ".profile.xml" )));
197  QDomDocument doc;
198  QString errorMsg;
199  int errorLine, errorColumn;
200  if(doc.setContent( &file, &errorMsg, &errorLine, &errorColumn)) {
201 
202  QDomElement rootElement = doc.namedItem(QLatin1String( "profile" )).toElement();
203  QString name = rootElement.namedItem(QLatin1String( "name" )).toElement().text().trimmed();
204  QString description;
205  if(!rootElement.namedItem(QLatin1String( "description" )).isNull()) {
206  description = rootElement.namedItem(QLatin1String( "description" )).toElement().text().trimmed();
207  }
208  QString author = rootElement.namedItem(QLatin1String( "author" )).toElement().text().trimmed();
209  QString version = rootElement.namedItem(QLatin1String( "version" )).toElement().text().trimmed();
210 
211  Profile *profile = new Profile(profileId, i18n(name.toUtf8().data()) ,version, author, i18n(description.toUtf8().data()));
212 
213  QDomNodeList actionNodeList = rootElement.elementsByTagName(QLatin1String( "action" ));
214  for(int count = 0; count < actionNodeList.size(); ++count){
215  profile->addTemplate(parseAction(actionNodeList.at(count), profileId));
216  }
217  return profile;
218  } else {
219  kDebug() << "Could not parse xml file " << fileName;
220  kDebug() << " Error on line " << errorLine << "Column " << errorColumn << " Message" << errorMsg;
221  return 0;
222  }
223 }
224 
225 ProfileActionTemplate ProfileServer::ProfileXmlContentHandler::parseAction(QDomNode actionNode, const QString& profileId) {
226  QString buttonName;
227  bool autostart = true;
228  bool repeat = false;
229  QString actionId = actionNode.attributes().namedItem(QLatin1String( "id" )).nodeValue().trimmed();
230  if (!actionNode.namedItem(QLatin1String( "button" )).isNull()){
231  buttonName = actionNode.namedItem(QLatin1String( "button" )).toElement().text().trimmed();
232  }
233  if (!actionNode.namedItem(QLatin1String( "autostart" )).isNull()){
234  QString value = actionNode.namedItem(QLatin1String( "autostart" )).toElement().text().trimmed();
235  autostart = value == QLatin1String( "true" )? true: false;
236  }
237  if (!actionNode.namedItem(QLatin1String( "repeat" )).isNull()){
238  repeat = QVariant(actionNode.namedItem(QLatin1String( "repeat" )).toElement().text().trimmed()).toBool();
239  }
240 
241  QString actionName = actionNode.attributes().namedItem(QLatin1String( "name" )).nodeValue().trimmed();
242 
243  QString description = actionNode.attributes().namedItem(QLatin1String( "description" )).nodeValue().trimmed();
244 
245  DBusAction::ActionDestination actionType;
246  QString ifMultiTag = actionNode.namedItem(QLatin1String( "ifmulti" )).toElement().text().trimmed();
247  if(ifMultiTag == QLatin1String( "sendtotop" )){
248  actionType = DBusAction::Top;
249  } else if(ifMultiTag == QLatin1String( "sendtobottom" )) {
250  actionType = DBusAction::Bottom;
251  } else if(ifMultiTag == QLatin1String( "sendtoall" )) {
252  actionType = DBusAction::All;
253  } else if(ifMultiTag == QLatin1String( "dontsend" )) {
254  actionType = DBusAction::None;
255  } else {
256  actionType = DBusAction::Unique;
257  }
258 
259  QDomElement execNode;
260  QString serviceName;
261  QString nodeName;
262  QString interfaceName;
263  QString functionName;
264  if(!actionNode.namedItem(QLatin1String( "prototype" )).isNull()){
265  execNode = actionNode.namedItem(QLatin1String( "prototype" )).toElement();
266  interfaceName = execNode.namedItem(QLatin1String( "interface" )).toElement().text().trimmed();
267  functionName = execNode.namedItem(QLatin1String( "function" )).toElement().text().trimmed();
268  } else if(!actionNode.namedItem(QLatin1String( "script" )).isNull()){
269  execNode = actionNode.namedItem(QLatin1String( "script" )).toElement();
270  functionName = "script:" + execNode.namedItem(QLatin1String( "scripttext" )).toElement().text().trimmed();
271  } else {
272  kDebug() << "neither prototype nor script present... discarding action" << actionName;
273  }
274  serviceName = execNode.namedItem(QLatin1String( "serviceName" )).toElement().text().trimmed();
275  nodeName = execNode.namedItem(QLatin1String( "node" )).toElement().text().trimmed();
276 
277 
278  QList<Argument> arguments;
279  if(!execNode.namedItem(QLatin1String( "arguments" )).isNull()){
280  QDomNodeList attributeNodes = execNode.namedItem(QLatin1String( "arguments" )).toElement().elementsByTagName(QLatin1String( "argument" ));
281  for(int attributeCount = 0; attributeCount < attributeNodes.size(); ++ attributeCount){
282  QDomNode attributeNode = attributeNodes.at(attributeCount);
283  QString typeString = attributeNode.attributes().namedItem(QLatin1String( "type" )).nodeValue().trimmed();
284  QVariant argValue(QVariant::nameToType(typeString.toLocal8Bit()));
285  QString description = attributeNode.toElement().attributes().namedItem(QLatin1String( "comment" )).nodeValue().trimmed();
286  if(!attributeNode.toElement().namedItem(QLatin1String( "default" )).isNull()){
287  QVariant qVariant = QVariant::nameToType(typeString.toLocal8Bit());
288  QString value = attributeNode.toElement().namedItem(QLatin1String( "default" )).toElement().text().trimmed();
289  if(argValue.type() == QVariant::StringList){
290  QStringList stringList;
291  foreach(const QString &tListValue, value.split(QLatin1Char( ',' ), QString::SkipEmptyParts)){
292  stringList << tListValue.trimmed();
293  }
294  argValue = QVariant(stringList);
295  } else {
296  argValue = QVariant(value);
297  }
298  argValue.convert(QVariant::nameToType(typeString.toLocal8Bit()));
299  }
300  arguments.append(Argument(argValue, i18n(description.toUtf8().data())));
301  }
302  }
303 
304  Prototype function(functionName, arguments);
305 
306  kDebug() << "creating action template" << actionName << serviceName << nodeName << interfaceName << functionName << actionType;
307 
308  return ProfileActionTemplate(profileId,
309  actionId,
310  i18n(actionName.toUtf8().data()),
311  serviceName,
312  nodeName,
313  interfaceName,
314  function,
315  actionType,
316  autostart,
317  repeat,
318  i18n(description.toUtf8().data()),
319  buttonName);
320 }
Argument
Definition: argument.h:27
Action::Bottom
Definition: action.h:34
ProfileServer::ProfileXmlContentHandler::validateFile
bool validateFile(const QString &fileName)
Definition: profileserver.cpp:127
ProfileActionTemplate
Definition: profileactiontemplate.h:31
Remote::name
QString name() const
Definition: remote.cpp:247
ProfileServer::FULL_SUPPORTED
Definition: profileserver.h:36
ProfileServer::ProfileXmlContentHandler::ProfileXmlContentHandler
ProfileXmlContentHandler(const QUrl &schema)
Definition: profileserver.cpp:117
profileactiontemplate.h
ProfileServer::ProfileXmlContentHandler
Definition: profileserver.h:49
KREMOTECONTROL_EXPORT
#define KREMOTECONTROL_EXPORT
Definition: kremotecontrol_export.h:37
ProfileServer::allProfiles
QList< Profile * > allProfiles()
Definition: profileserver.cpp:89
Profile::addTemplate
void addTemplate(const ProfileActionTemplate &actionTemplate)
Definition: profile.cpp:72
dbusaction.h
profileserver.h
Action::All
Definition: action.h:34
Action::ActionDestination
ActionDestination
Definition: action.h:34
ProfileActionTemplate::actionTemplateId
QString actionTemplateId() const
Definition: profileactiontemplate.cpp:62
ProfileServer::ProfileSupportedByRemote
ProfileSupportedByRemote
Definition: profileserver.h:35
ProfileServer::ProfileXmlContentHandler::~ProfileXmlContentHandler
~ProfileXmlContentHandler()
Definition: profileserver.cpp:123
Profile::compareVersion
int compareVersion(Profile *other) const
Definition: profile.cpp:102
Action::Unique
Definition: action.h:34
Prototype
Definition: prototype.h:28
RemoteControl
Definition: remotecontrol.h:35
ProfileServer::ProfileXmlContentHandler::handleMessage
virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation)
Definition: profileserver.cpp:136
ProfileServer::NOT_SUPPORTED
Definition: profileserver.h:38
RemoteControlButton::name
QString name() const
Retrieves the name of the Button.
Definition: remotecontrolbutton.cpp:315
Action::Top
Definition: action.h:34
ProfileServer::addProfile
void addProfile(Profile *profile)
Definition: profileserver.cpp:85
ProfileActionTemplate::buttonName
QString buttonName() const
Definition: profileactiontemplate.cpp:103
ProfileServer::isProfileAvailableForRemote
ProfileServer::ProfileSupportedByRemote isProfileAvailableForRemote(Profile *profile, const Remote &remote)
Definition: profileserver.cpp:145
executionengine.h
Profile::actionTemplates
QList< ProfileActionTemplate > actionTemplates() const
Definition: profile.cpp:76
Profile::profileId
QString profileId() const
Definition: profile.cpp:68
ProfileServer::ProfileXmlContentHandler::loadProfilesFromFiles
QList< Profile * > loadProfilesFromFiles(const QStringList &files)
Definition: profileserver.cpp:178
ProfileServer::NO_ACTIONS_DEFINED
Definition: profileserver.h:39
ProfileServer::ProfileXmlContentHandler::parseFile
Profile * parseFile(const QString &fileName)
Definition: profileserver.cpp:191
Action::None
Definition: action.h:34
remote.h
ProfileServer::profile
Profile * profile(const QString &profileId)
Definition: profileserver.cpp:93
ProfileServer::PARTIAL_SUPPORTED
Definition: profileserver.h:37
Profile
Definition: profile.h:26
RemoteControlButton
Definition: remotecontrolbutton.h:30
Remote
Definition: remote.h:32
ProfileServer::actionTemplateList
QList< ProfileActionTemplate > actionTemplateList(const QString &remote, Profile *profile)
Definition: profileserver.cpp:103
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kremotecontrol

Skip menu "kremotecontrol"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal