Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / plugins / autoreplace / autoreplaceplugin.cpp
bloba0859f3fa9bc902e37c53c685a96710edd767d56
1 /***************************************************************************
2 autoreplaceplugin.cpp - description
3 -------------------
4 begin : 20030425
5 copyright : (C) 2003 by Roberto Pariset
6 email : victorheremita@fastwebnet.it
7 ***************************************************************************/
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 ***************************************************************************/
18 #include <kgenericfactory.h>
20 #include <kopetecontact.h>
22 #include "kopetechatsessionmanager.h"
23 #include "kopetesimplemessagehandler.h"
25 #include "autoreplaceplugin.h"
26 #include "autoreplaceconfig.h"
28 K_PLUGIN_FACTORY(AutoReplacePluginFactory, registerPlugin<AutoReplacePlugin>();)
29 K_EXPORT_PLUGIN(AutoReplacePluginFactory( "kopete_autoreplace" ))
32 AutoReplacePlugin * AutoReplacePlugin::pluginStatic_ = 0L;
34 AutoReplacePlugin::AutoReplacePlugin( QObject *parent, const QVariantList & )
35 : Kopete::Plugin( AutoReplacePluginFactory::componentData(), parent )
37 if( !pluginStatic_ )
38 pluginStatic_ = this;
40 m_prefs = new AutoReplaceConfig;
42 // intercept inbound messages
43 mInboundHandler = new Kopete::SimpleMessageHandlerFactory ( Kopete::Message::Inbound,
44 Kopete::MessageHandlerFactory::InStageToDesired, this, SLOT ( slotInterceptMessage ( Kopete::Message& ) ) );
46 connect( Kopete::ChatSessionManager::self(), SIGNAL( aboutToSend( Kopete::Message & ) ),
47 this, SLOT( slotInterceptMessage( Kopete::Message & ) ) );
49 connect( this, SIGNAL( settingsChanged() ), this, SLOT( slotSettingsChanged() ) );
52 AutoReplacePlugin::~AutoReplacePlugin()
54 pluginStatic_ = 0L;
56 delete mInboundHandler;
57 delete m_prefs;
60 AutoReplacePlugin * AutoReplacePlugin::plugin()
62 return pluginStatic_ ;
65 void AutoReplacePlugin::slotSettingsChanged()
67 m_prefs->load();
70 void AutoReplacePlugin::slotInterceptMessage( Kopete::Message &msg )
72 if ( ( msg.direction() == Kopete::Message::Outbound && m_prefs->autoReplaceOutgoing() ) ||
73 ( msg.direction() == Kopete::Message::Inbound && m_prefs->autoReplaceIncoming() ) )
75 QString replaced_message = msg.plainBody();
76 AutoReplaceConfig::WordsToReplace map = m_prefs->map();
78 // replaces all matched words --> try to find a more 'economic' way
79 // "\\b(%1)\\b" doesn't work when substituting /me.
80 QString match = "(^|\\s|\\.|\\;|\\,|\\:)(%1)(\\b)";
81 AutoReplaceConfig::WordsToReplace::Iterator it;
82 bool isReplaced=false;
83 for ( it = map.begin(); it != map.end(); ++it )
85 QRegExp re( match.arg( QRegExp::escape( it.key() ) ) );
86 if( re.indexIn( replaced_message ) != -1 )
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;
95 if ( m_prefs->dotEndSentence() )
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;
103 if( m_prefs->capitalizeBeginningSentence() )
105 // eventually start each sent line with capital letter
106 // TODO ". " "? " "! "
107 replaced_message[ 0 ] = replaced_message.at( 0 ).toUpper();
108 isReplaced=true;
111 // the message is now the one with replaced words
112 if(isReplaced)
113 msg.setPlainBody( replaced_message );
117 #include "autoreplaceplugin.moc"
119 // vim: set noet ts=4 sts=4 sw=4: