~ubuntu-branches/ubuntu/raring/ktp-text-ui/raring-proposed

« back to all changes in this revision

Viewing changes to lib/message-processor.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-08-26 12:46:37 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20120826124637-lhv0slddscxlwzbw
Tags: 0.5.0-0ubuntu1
* New upstream release
  - Drop 03_no_redundant_glib_calls.diff 
  - Update 02_ktpchat_soversion.diff
  - Disable tests
  - Update install file

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include "message-processor.h"
21
21
#include "filters.h"
 
22
#include "plugin-config-manager.h"
 
23
 
 
24
#include <QMutex>
 
25
 
 
26
#include <KDebug>
 
27
#include <KService>
 
28
#include <KServiceTypeTrader>
 
29
#include <KPluginFactory>
22
30
 
23
31
MessageProcessor* MessageProcessor::s_instance = 0;
24
32
 
25
 
AbstractMessageFilter::AbstractMessageFilter(QObject* parent)
26
 
    : QObject(parent)
27
 
{
28
 
}
29
 
 
30
 
AbstractMessageFilter::~AbstractMessageFilter()
31
 
{
32
 
}
33
 
 
34
33
MessageProcessor* MessageProcessor::instance()
35
34
{
 
35
    kDebug();
 
36
 
36
37
    static QMutex mutex;
37
 
    if (!s_instance)
38
 
    {
39
 
        mutex.lock();
40
 
        if (!s_instance) {
41
 
            s_instance = new MessageProcessor;
42
 
        }
43
 
        mutex.unlock();
 
38
    mutex.lock();
 
39
    if (!s_instance) {
 
40
        s_instance = new MessageProcessor;
44
41
    }
 
42
    mutex.unlock();
 
43
 
45
44
    return s_instance;
46
45
}
47
46
 
48
47
 
49
48
MessageProcessor::MessageProcessor()
50
49
{
51
 
    m_filters << new EscapeFilter(this) << new UrlFilter(this) << new EmoticonFilter(this);
 
50
    m_filters.append(new EscapeFilter(this));
 
51
    m_filters.append(new UrlFilter(this));
 
52
 
 
53
    loadFilters();
52
54
}
53
55
 
54
56
 
56
58
{
57
59
}
58
60
 
59
 
Message MessageProcessor::processIncomingMessage(const Tp::ReceivedMessage &receivedMessage)
60
 
{
61
 
    Message message(receivedMessage);
62
 
    Q_FOREACH(AbstractMessageFilter *filter, MessageProcessor::m_filters) {
63
 
        filter->filterMessage(message);
64
 
    }
65
 
    return message;
66
 
}
67
 
 
68
 
Message MessageProcessor::processOutgoingMessage(const Tp::Message &sentMessage)
69
 
{
70
 
    Message message(sentMessage);
71
 
    Q_FOREACH(AbstractMessageFilter *filter, MessageProcessor::m_filters) {
72
 
        filter->filterMessage(message);
73
 
    }
74
 
    return message;
75
 
}
76
 
 
77
 
 
 
61
Message MessageProcessor::processIncomingMessage(Message receivedMessage)
 
62
{
 
63
    Q_FOREACH(AbstractMessageFilter *filter, MessageProcessor::m_filters) {
 
64
        kDebug() << "running filter :" << filter->metaObject()->className();
 
65
        filter->filterMessage(receivedMessage);
 
66
    }
 
67
    return receivedMessage;
 
68
}
 
69
 
 
70
Message MessageProcessor::processOutgoingMessage(Message sentMessage)
 
71
{
 
72
    Q_FOREACH(AbstractMessageFilter *filter, MessageProcessor::m_filters) {
 
73
        filter->filterMessage(sentMessage);
 
74
    }
 
75
    return sentMessage;
 
76
}
 
77
 
 
78
void MessageProcessor::loadFilters() {
 
79
    kDebug() << "Starting loading filters...";
 
80
 
 
81
    Q_FOREACH (const KPluginInfo &plugin, PluginConfigManager::self()->enabledPlugins()) {
 
82
        KService::Ptr service = plugin.service();
 
83
 
 
84
        KPluginFactory *factory = KPluginLoader(service->library()).factory();
 
85
        if(factory) {
 
86
            kDebug() << "loaded factory :" << factory;
 
87
            AbstractMessageFilter *filter = factory->create<AbstractMessageFilter>(this);
 
88
 
 
89
            if(filter) {
 
90
                kDebug() << "loaded message filter : " << filter;
 
91
                m_filters.append(filter);
 
92
            }
 
93
        } else {
 
94
            kError() << "error loading plugin :" << service->library();
 
95
        }
 
96
    }
 
97
}