/***************************************************************************
 *   Copyright (C) 2004 by Jesper K. Pedersen                              *
 *   blackie@kde.org                                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "capitalizeplugin.h"
#include <kgenericfactory.h>
#include <kaction.h>
#include <qregexp.h>

// Description of the K_EXPORT_COMPONENT_FACTORY
// The macro looks like this:
//
// #define K_EXPORT_COMPONENT_FACTORY( libname, factory )
//    extern "C" { void *init_##libname() { return new factory; } }
//
// - The first argument is the name of library which this code resists in.
// - The part instantiating the factory is the name of the class which
//   implements the plugin.
// - The argument to KGenericFactory should be a uniq name for the plugin,
//   that is given to KInstance.
K_EXPORT_COMPONENT_FACTORY( plugindemo_capitalize,
                            KGenericFactory<CapitalizePlugin>( "plugindemo_capitalize" ) )

CapitalizePlugin::CapitalizePlugin(QObject *parent, const char* name, const QStringList&)
            : PluginDemo::Plugin(parent, name )
{
    // Tell the host application to load my GUI component
    setInstance(KGenericFactory<CapitalizePlugin>::instance());
    setXMLFile("plugindemo_capitalizeui.rc");

    // For ease announce that we have been loaded.
    kdDebug() << "CapitalizePlugin plugin loaded" << endl;

    // Create the actions of thus plugin
    _action = new KAction (i18n("Capitalize Words"),    // Menu message.
                           "capitalize_words",          // Menu icon.
                           ALT+CTRL+Key_C,              // default shortcut.
                           this,
                           SLOT(slotCapitalize()),
                           actionCollection(),
                           "capitalize");

    // Connect a signal from the interface to a slot of this plugin, which enabled the action
    // only to be available when a selection is active.
    connect( selectionInterface(), SIGNAL( selectionActive( bool ) ), this, SLOT( slotSetEnabled( bool ) ) );
}

void CapitalizePlugin::slotCapitalize()
{
    QString text = selectionInterface()->text();
    for ( int i=0; i != -1; ) {
        text.at(i) = text.at(i).upper();
        i = text.find( QRegExp("\\b\\w"), i+1 );
    }
    selectionInterface()->setText( text );

}


void CapitalizePlugin::slotSetEnabled( bool b)
{
    _action->setEnabled( b );
}

#include "capitalizeplugin.moc"
