~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins/autoreplace/autoreplaceplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          autoreplaceplugin.cpp  -  description
 
3
                             -------------------
 
4
    begin                : 20030425
 
5
    copyright            : (C) 2003 by Roberto Pariset
 
6
    email                : victorheremita@fastwebnet.it
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include <kgenericfactory.h>
 
19
 
 
20
#include <kopetecontact.h>
 
21
 
 
22
#include "kopetechatsessionmanager.h"
 
23
#include "kopetesimplemessagehandler.h"
 
24
 
 
25
#include "autoreplaceplugin.h"
 
26
#include "autoreplaceconfig.h"
 
27
 
 
28
K_PLUGIN_FACTORY(AutoReplacePluginFactory, registerPlugin<AutoReplacePlugin>();)
 
29
K_EXPORT_PLUGIN(AutoReplacePluginFactory( "kopete_autoreplace" ))
 
30
 
 
31
 
 
32
AutoReplacePlugin * AutoReplacePlugin::pluginStatic_ = 0L;
 
33
 
 
34
AutoReplacePlugin::AutoReplacePlugin( QObject *parent, const QVariantList & )
 
35
: Kopete::Plugin( AutoReplacePluginFactory::componentData(), parent )
 
36
{
 
37
        if( !pluginStatic_ )
 
38
                pluginStatic_ = this;
 
39
 
 
40
        m_prefs = new AutoReplaceConfig;
 
41
 
 
42
        // intercept inbound messages
 
43
        mInboundHandler = new Kopete::SimpleMessageHandlerFactory ( Kopete::Message::Inbound,
 
44
                Kopete::MessageHandlerFactory::InStageToDesired, this, SLOT (slotInterceptMessage(Kopete::Message&)) );
 
45
 
 
46
        connect( Kopete::ChatSessionManager::self(), SIGNAL(aboutToSend(Kopete::Message&)),
 
47
                this, SLOT(slotInterceptMessage(Kopete::Message&)) );
 
48
 
 
49
        connect( this, SIGNAL(settingsChanged()), this, SLOT(slotSettingsChanged()) );
 
50
}
 
51
 
 
52
AutoReplacePlugin::~AutoReplacePlugin()
 
53
{
 
54
        pluginStatic_ = 0L;
 
55
 
 
56
        delete mInboundHandler;
 
57
        delete m_prefs;
 
58
}
 
59
 
 
60
AutoReplacePlugin * AutoReplacePlugin::plugin()
 
61
{
 
62
        return pluginStatic_ ;
 
63
}
 
64
 
 
65
void AutoReplacePlugin::slotSettingsChanged()
 
66
{
 
67
        m_prefs->load();
 
68
}
 
69
 
 
70
void AutoReplacePlugin::slotInterceptMessage( Kopete::Message &msg )
 
71
{
 
72
        if ( ( msg.direction() == Kopete::Message::Outbound && m_prefs->autoReplaceOutgoing() ) ||
 
73
                ( msg.direction() == Kopete::Message::Inbound && m_prefs->autoReplaceIncoming() ) )
 
74
        {
 
75
                QString replaced_message = msg.plainBody();
 
76
                AutoReplaceConfig::WordsToReplace map = m_prefs->map();
 
77
 
 
78
                // replaces all matched words --> try to find a more 'economic' way
 
79
                // "\\b(%1)\\b" doesn't work when substituting /me.
 
80
                const QString match = "(^|\\s|\\.|\\;|\\,|\\:)(%1)(\\b)";
 
81
                AutoReplaceConfig::WordsToReplace::Iterator it;
 
82
                bool isReplaced=false;
 
83
                for ( it = map.begin(); it != map.end(); ++it )
 
84
                {
 
85
                        QRegExp re( match.arg( QRegExp::escape( it.key() ) ) );
 
86
                        if( re.indexIn( replaced_message ) != -1 )
 
87
                        {
 
88
                                QString before = re.cap(1);
 
89
                                QString after = re.cap(3);
 
90
                                replaced_message.replace( re, before + map.find( it.key() ).value() + after );
 
91
                                isReplaced=true;
 
92
                        }
 
93
                }
 
94
 
 
95
                if ( m_prefs->dotEndSentence() )
 
96
                {
 
97
                        // eventually add . at the end of the lines, sent lines only
 
98
                        replaced_message.replace( QRegExp( "([a-z])$" ), "\\1." );
 
99
                        // replaced_message.replace(QRegExp( "([\\w])$" ), "\\1." );
 
100
                        isReplaced=true;
 
101
                }
 
102
 
 
103
                if( m_prefs->capitalizeBeginningSentence() )
 
104
                {
 
105
                        // eventually start each sent line with capital letter
 
106
                        // TODO         ". "     "? "    "! " 
 
107
                        replaced_message[ 0 ] = replaced_message.at( 0 ).toUpper();
 
108
                        isReplaced=true;
 
109
                }
 
110
 
 
111
                // the message is now the one with replaced words
 
112
                if(isReplaced)
 
113
                        msg.setPlainBody( replaced_message );
 
114
        }
 
115
}
 
116
 
 
117
#include "autoreplaceplugin.moc"
 
118
 
 
119
// vim: set noet ts=4 sts=4 sw=4:
 
120