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

Nepomuk-Core

  • KTp
message-processor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Lasath Fernando <kde@lasath.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include "message-processor.h"
21 #include "message-filters-private.h"
22 #include "message-filter-config-manager.h"
23 
24 #include <QMutex>
25 #include <QStringBuilder>
26 
27 #include <KDebug>
28 #include <KService>
29 #include <KServiceTypeTrader>
30 #include <KPluginFactory>
31 #include <KDE/KStandardDirs>
32 
33 using namespace KTp;
34 
35 class MessageProcessor::Private
36 {
37  public:
38  Private(MessageProcessor *parent):
39  q(parent)
40  { }
41 
42  void loadFilters();
43 
44  QList<KTp::AbstractMessageFilter*> filters;
45 
46  private:
47  MessageProcessor *q;
48 };
49 
50 bool pluginWeightLessThan(const KPluginInfo &p1, const KPluginInfo &p2)
51 {
52  bool ok;
53  int weight1 = p1.service()->property(QLatin1String("X-KDE-PluginInfo-Weight"), QVariant::Int).toInt(&ok);
54  if (!ok) {
55  weight1 = 100;
56  }
57  int weight2 = p2.service()->property(QLatin1String("X-KDE-PluginInfo-Weight"), QVariant::Int).toInt(&ok);
58  if (!ok) {
59  weight2 = 100;
60  }
61 
62  return weight1 < weight2;
63 }
64 
65 void MessageProcessor::Private::loadFilters()
66 {
67  kDebug() << "Starting loading filters...";
68 
69  KPluginInfo::List plugins = MessageFilterConfigManager::self()->enabledPlugins();
70 
71  qSort(plugins.begin(), plugins.end(), pluginWeightLessThan);
72 
73  Q_FOREACH (const KPluginInfo &plugin, plugins) {
74  KService::Ptr service = plugin.service();
75 
76  KPluginFactory *factory = KPluginLoader(service->library()).factory();
77  if(factory) {
78  kDebug() << "loaded factory :" << factory;
79  AbstractMessageFilter *filter = factory->create<AbstractMessageFilter>(q);
80 
81  if(filter) {
82  kDebug() << "loaded message filter : " << filter;
83  filters.append(filter);
84  }
85  } else {
86  kError() << "error loading plugin :" << service->library();
87  }
88  }
89 }
90 
91 
92 KTp::MessageProcessor* MessageProcessor::instance()
93 {
94  kDebug();
95 
96  static KTp::MessageProcessor *mp_instance;
97  static QMutex mutex;
98  mutex.lock();
99  if (!mp_instance) {
100  mp_instance= new MessageProcessor;
101  }
102  mutex.unlock();
103 
104  return mp_instance;
105 }
106 
107 
108 MessageProcessor::MessageProcessor():
109  d(new MessageProcessor::Private(this))
110 {
111  d->filters.append(new MessageEscapeFilter(this));
112  d->filters.append(new MessageUrlFilter(this));
113 
114  d->loadFilters();
115 
116  d->filters.append(new MessageBackslashFilter(this));
117 }
118 
119 
120 MessageProcessor::~MessageProcessor()
121 {
122  delete d;
123 }
124 
125 QString MessageProcessor::header()
126 {
127  QStringList scripts;
128  QStringList stylesheets;
129  Q_FOREACH (AbstractMessageFilter *filter, d->filters) {
130  Q_FOREACH (const QString &script, filter->requiredScripts()) {
131  // Avoid duplicates
132  if (!scripts.contains(script)) {
133  scripts << script;
134  }
135  }
136  Q_FOREACH (const QString &stylesheet, filter->requiredStylesheets()) {
137  // Avoid duplicates
138  if (!stylesheets.contains(stylesheet)) {
139  stylesheets << stylesheet;
140  }
141  }
142  }
143 
144  QString out(QLatin1String("\n <!-- The following scripts and stylesheets are injected here by the plugins -->\n"));
145  Q_FOREACH(const QString &script, scripts) {
146  out = out % QLatin1String(" <script type=\"text/javascript\" src=\"")
147  % KGlobal::dirs()->findResource("data", script)
148  % QLatin1String("\"></script>\n");
149  }
150  Q_FOREACH(const QString &stylesheet, stylesheets) {
151  out = out % QLatin1String(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
152  % KGlobal::dirs()->findResource("data", stylesheet)
153  % QLatin1String("\" />\n");
154  }
155 
156  kDebug() << out;
157 
158  return out;
159 }
160 
161 KTp::Message MessageProcessor::processIncomingMessage(const Tp::Message &message, const Tp::AccountPtr &account, const Tp::TextChannelPtr &channel)
162 {
163  KTp::MessageContext context(account, channel);
164  return processIncomingMessage(KTp::Message(message, context), context);
165 }
166 
167 KTp::Message KTp::MessageProcessor::processIncomingMessage(const Tp::ReceivedMessage &message, const Tp::AccountPtr &account, const Tp::TextChannelPtr &channel)
168 {
169  KTp::MessageContext context(account, channel);
170  return processIncomingMessage(KTp::Message(message, context), context);
171 }
172 
173 KTp::Message KTp::MessageProcessor::processIncomingMessage(const Tpl::TextEventPtr &message, const Tp::AccountPtr &account, const Tp::TextChannelPtr &channel)
174 {
175  KTp::MessageContext context(account, channel);
176  return processIncomingMessage(KTp::Message(message, context), context);
177 }
178 
179 
180 KTp::Message MessageProcessor::processIncomingMessage(KTp::Message message, const KTp::MessageContext &context)
181 {
182  Q_FOREACH (AbstractMessageFilter *filter, d->filters) {
183  kDebug() << "running filter :" << filter->metaObject()->className();
184  filter->filterMessage(message, context);
185  }
186  return message;
187 }
188 
189 KTp::OutgoingMessage MessageProcessor::processOutgoingMessage(const QString &messageText, const Tp::AccountPtr &account, const Tp::TextChannelPtr &channel)
190 {
191  KTp::MessageContext context(account, channel);
192  KTp::OutgoingMessage message(messageText);
193 
194  Q_FOREACH (AbstractMessageFilter *filter, d->filters) {
195  kDebug() << "running outgoing filter : " << filter->metaObject()->className();
196  filter->filterOutgoingMessage(message, context);
197  }
198 
199  return message;
200 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
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