Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

formattingbackend.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2007 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "formattingbackend.h"
00021 #include "formattingbackend.moc"
00022 
00023 #include "editentrywidget.h"
00024 #include "htmlentrytextedit.h"
00025 
00026 #include <kdebug.h>
00027 
00028 #include <QRegExp>
00029 
00030 class Blokkal::Ui::FormattingBackend::Private
00031 {
00032 public:
00033         Private( Blokkal::Ui::EditEntryWidget * widget ) :
00034         widget( widget ),
00035         textEdit( 0 ) {}
00036         Blokkal::Ui::EditEntryWidget * widget;
00037         HtmlEntryTextEdit * textEdit;
00038 };
00039 
00040 Blokkal::Ui::FormattingBackend::FormattingBackend( Blokkal::Ui::EditEntryWidget * parent ):
00041 QObject( parent ),
00042 KXMLGUIClient(),
00043 d( new Private( parent ) )
00044 {}
00045 
00046 Blokkal::Ui::FormattingBackend::~FormattingBackend( void )
00047 {
00048         delete d;
00049 }
00050 
00051 void Blokkal::Ui::FormattingBackend::setTextEdit( HtmlEntryTextEdit * textEdit )
00052 {
00053         d->textEdit = textEdit;
00054 }
00055 
00056 void Blokkal::Ui::FormattingBackend::insertTextAtCursor( const QString & text )
00057 {
00058         if( !d->textEdit ) {
00059                 return;
00060         }
00061 
00062         d->textEdit->insertTextAtCursor ( text );
00063 }
00064 
00065 void Blokkal::Ui::FormattingBackend::frameSelection( const QString & front, const QString & tail )
00066 {
00067         if( !d->textEdit ) {
00068                 return;
00069         }
00070 
00071         d->textEdit->frameSelection( front, tail );
00072 }
00073 
00074 QString & Blokkal::Ui::FormattingBackend::formatForPreview( QString & richText ) const
00075 {
00076         return formatLineBreaks( formatLinks( richText ) );
00077 }
00078 
00079 Blokkal::Ui::EditEntryWidget * Blokkal::Ui::FormattingBackend::editEntryWidget( void ) const
00080 {
00081         return d->widget;
00082 }
00083 
00084 QString & Blokkal::Ui::FormattingBackend::formatLinks( QString & richText ) const
00085 {
00086         QRegExp linkexp( "(^|\\s)(http|https|ftp)://[\\S]+" );
00087         const QString linkStart = QString::fromLatin1( "<a href=\"" );
00088         const QString linkEnd = QString::fromLatin1( "\">" );
00089         const QString linkClose = QString::fromLatin1( "</a>" );
00090         int offset = 0;
00091         while( ( offset = linkexp.indexIn( richText, offset ) ) != -1 ) {
00092                 if( offset > 0 && richText.at( offset - 1 ) == '\"' ) {
00093                         offset += linkexp.matchedLength();
00094                         continue;
00095                 }
00096                 
00097                 bool leadingWhiteSpace = linkexp.cap().at( 0 ).isSpace();
00098                 richText.insert( offset + linkexp.matchedLength(), linkEnd );
00099                 richText.insert( offset + ( leadingWhiteSpace ? 1 : 0 ), linkStart );
00100                 richText.insert( offset
00101                                  + linkexp.matchedLength()
00102                                  + linkStart.length()
00103                                  + linkEnd.length(),
00104                                  ( leadingWhiteSpace ? linkexp.cap().mid( 1 ) : linkexp.cap() ) + linkClose );
00105                 if( leadingWhiteSpace ) {
00106                         offset--;
00107                 }
00108                 offset += 2 * linkexp.matchedLength() + linkStart.length() + linkEnd.length() + linkClose.length();
00109         }
00110         return richText;
00111 }
00112 
00113 QString & Blokkal::Ui::FormattingBackend::formatLineBreaks( QString & richText ) const
00114 {
00115         return richText.replace( '\n', "<br/>" );
00116 }