22 #include "signature.h"
25 #include <klocalizedstring.h>
26 #include <kmessagebox.h>
27 #include <kconfiggroup.h>
30 #include <KRichTextEdit>
31 #include <kpimutils/kfileio.h>
34 #include <QSharedPointer>
38 #include <QtCore/QDir>
39 #include <kpimtextedit/textedit.h>
41 using namespace KPIMIdentities;
43 class SignaturePrivate
68 return stream << img->image << img->name;
73 return stream >> img->image >> img->name;
82 static SignaturePrivate* d( const
Signature *sig )
84 SignaturePrivate *ret = d_func()->
value( sig, 0 );
86 ret =
new SignaturePrivate;
87 d_func()->insert( sig, ret );
92 static void delete_d(
const Signature* sig )
94 SignaturePrivate *ret = d_func()->value( sig, 0 );
96 d_func()->remove( sig );
101 mInlinedHtml( false )
107 mInlinedHtml( false )
112 mType( isExecutable ? FromCommand : FromFile ),
113 mInlinedHtml( false )
119 mInlinedHtml = that.mInlinedHtml;
122 d(
this )->enabled = d( &that )->enabled;
123 d(
this )->saveLocation = d( &that )->saveLocation;
124 d(
this )->embeddedImages = d( &that )->embeddedImages;
134 if (
this == &that ) {
161 return textFromFile( ok );
163 return textFromCommand( ok );
165 kFatal( 5325 ) <<
"Signature::type() returned unknown value!";
169 QString Signature::textFromCommand(
bool *ok )
const
171 assert( mType == FromCommand );
183 proc.setOutputChannelMode( KProcess::SeparateChannels );
184 proc.setShellCommand( mUrl );
185 int rc = proc.execute();
192 QString wmsg = i18n(
"<qt>Failed to execute signature script<p><b>%1</b>:</p>"
193 "<p>%2</p></qt>", mUrl,
QLatin1String( proc.readAllStandardError() ) );
194 KMessageBox::error( 0, wmsg );
204 QByteArray output = proc.readAllStandardOutput();
210 QString Signature::textFromFile(
bool *ok )
const
212 assert( mType == FromFile );
215 if ( !KUrl( mUrl ).isLocalFile() &&
218 kDebug( 5325 ) <<
"Signature::textFromFile:"
219 <<
"non-local URLs are unsupported";
231 const QByteArray ba = KPIMUtils::kFileToByteArray( mUrl,
false );
238 if ( ok && ( *ok ) ==
false ) {
246 const bool htmlSig = (
isInlinedHtml() && mType == Inlined );
248 if ( htmlSig && signature.
startsWith( QLatin1String(
"<p" ) ) ) {
265 mType = isExecutable ? FromCommand : FromFile;
270 mInlinedHtml = isHtml;
279 static const char sigTypeKey[] =
"Signature Type";
280 static const char sigTypeInlineValue[] =
"inline";
281 static const char sigTypeFileValue[] =
"file";
282 static const char sigTypeCommandValue[] =
"command";
283 static const char sigTypeDisabledValue[] =
"disabled";
284 static const char sigTextKey[] =
"Inline Signature";
285 static const char sigFileKey[] =
"Signature File";
286 static const char sigCommandKey[] =
"Signature Command";
287 static const char sigTypeInlinedHtmlKey[] =
"Inlined Html";
288 static const char sigImageLocation[] =
"Image Location";
289 static const char sigEnabled[] =
"Signature Enabled";
297 KPIMTextEdit::TextEdit edit;
298 edit.setHtml( htmlCode );
299 foreach (
const KPIMTextEdit::ImageWithNamePtr &image, edit.imagesWithName() ) {
305 void Signature::cleanupImages()
const
311 foreach (
const QString &imageInHtml, findImageNames( mText ) ) {
312 if ( imageInHtml == imageInList->name ) {
318 d(
this )->embeddedImages.removeAll( imageInList );
324 if ( !d(
this )->saveLocation.isEmpty() ) {
325 QDir dir( d(
this )->saveLocation );
326 foreach (
const QString &fileName, dir.entryList( QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks ) ) {
328 kDebug() <<
"Deleting old image" << dir.path() + fileName;
335 void Signature::saveImages()
const
337 if (
isInlinedHtml() && !d(
this )->saveLocation.isEmpty() ) {
340 if ( !image->image.save( location,
"PNG" ) ) {
341 kWarning() <<
"Failed to save image" << location;
347 void Signature::readConfig(
const KConfigGroup &config )
349 QString sigType = config.readEntry( sigTypeKey );
352 mInlinedHtml = config.readEntry( sigTypeInlinedHtmlKey,
false );
355 mUrl = config.readPathEntry( sigFileKey,
QString() );
356 }
else if ( sigType ==
QLatin1String(sigTypeCommandValue) ) {
358 mUrl = config.readPathEntry( sigCommandKey,
QString() );
359 }
else if ( sigType ==
QLatin1String(sigTypeDisabledValue) ) {
360 d(
this )->enabled =
false;
362 if ( mType != Disabled ) {
363 d(
this )->enabled = config.readEntry( sigEnabled,
true );
366 mText = config.readEntry( sigTextKey );
367 d(
this )->saveLocation = config.readEntry( sigImageLocation );
369 if (
isInlinedHtml() && !d(
this )->saveLocation.isEmpty() ) {
370 QDir dir( d(
this )->saveLocation );
371 foreach (
const QString &fileName, dir.entryList( QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks ) ) {
378 kWarning() <<
"Unable to load image" << dir.path() +
QLatin1Char(
'/') + fileName;
385 void Signature::writeConfig( KConfigGroup &config )
const
389 config.writeEntry( sigTypeKey, sigTypeInlineValue );
390 config.writeEntry( sigTypeInlinedHtmlKey, mInlinedHtml );
393 config.writeEntry( sigTypeKey, sigTypeFileValue );
394 config.writePathEntry( sigFileKey, mUrl );
397 config.writeEntry( sigTypeKey, sigTypeCommandValue );
398 config.writePathEntry( sigCommandKey, mUrl );
403 config.writeEntry( sigTextKey, mText );
404 config.writeEntry( sigImageLocation, d(
this )->saveLocation );
405 config.writeEntry( sigEnabled, d(
this )->enabled );
411 static bool isCursorAtEndOfLine(
const QTextCursor &cursor )
414 testCursor.
movePosition( QTextCursor::EndOfLine, QTextCursor::KeepAnchor );
418 static void insertSignatureHelper(
const QString &signature,
419 KRichTextEdit *textEdit,
428 bool isModified = textEdit->document()->isModified();
442 textEdit->setTextCursor( cursor );
455 int newCursorPos = -1;
464 if ( oldCursor.
position() == textEdit->toPlainText().length() ) {
465 newCursorPos = oldCursor.
position();
473 headSep = lineSep + lineSep;
474 if ( !isCursorAtEndOfLine( cursor ) ) {
478 if ( !isCursorAtEndOfLine( cursor ) ) {
483 const QString full_signature = headSep + signature + tailSep;
485 textEdit->insertHtml( full_signature );
487 textEdit->insertPlainText( full_signature );
492 if ( newCursorPos != -1 ) {
496 textEdit->setTextCursor( oldCursor );
497 textEdit->ensureCursorVisible();
499 textEdit->document()->setModified( isModified );
502 textEdit->enableRichTextMode();
510 if ( !isEnabledSignature() ) {
514 if ( addSeparator ) {
519 insertSignatureHelper( signature, textEdit, placement,
521 type() == KPIMIdentities::Signature::Inlined ),
526 KPIMTextEdit::TextEdit *textEdit )
const
528 insertSignatureText( placement, addedText, textEdit,
false );
532 KPIMTextEdit::TextEdit *textEdit,
bool forceDisplay )
const
534 insertSignatureText( placement, addedText, textEdit, forceDisplay );
537 void Signature::insertSignatureText(Placement placement, AddedText addedText, KPIMTextEdit::TextEdit *textEdit,
bool forceDisplay)
const
539 if ( !forceDisplay ) {
540 if ( !isEnabledSignature() ) {
550 insertSignatureHelper( signature, textEdit, placement,
552 type() == KPIMIdentities::Signature::Inlined ),
558 textEdit->loadImage( image->image, image->name, image->name );
567 insertSignatureHelper( signature, textEdit, placement, isHtml,
true );
575 return stream << static_cast<quint8>( sig.mType ) << sig.mUrl << sig.mText
576 << d( &sig )->saveLocation << d( &sig )->embeddedImages << d( &sig )->enabled;
583 stream >> s >> sig.mUrl >> sig.mText >> d( &sig )->saveLocation >> d( &sig )->embeddedImages >>d( &sig )->enabled;
590 if ( mType != other.mType ) {
594 if ( d(
this )->enabled != d( &other )->enabled ) {
598 if ( mType == Inlined && mInlinedHtml ) {
599 if ( d(
this )->saveLocation != d( &other )->saveLocation ) {
602 if ( d(
this )->embeddedImages != d( &other )->embeddedImages ) {
609 return mText == other.mText;
612 return mUrl == other.mUrl;
635 Q_ASSERT( !( d(
this )->saveLocation.isEmpty() ) );
637 image->image = imageData;
638 image->name = imageName;
639 d(
this )->embeddedImages.append( image );
644 d(
this )->saveLocation = path;
649 QString Signature::text()
const
672 void Signature::setType( Type type )
680 d(
this )->enabled = enabled;
683 bool Signature::isEnabledSignature()
const
685 return d(
this )->enabled;
The signature is placed at the current cursor position.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
bool load(QIODevice *device, const char *format)
Add a newline character in front or after the signature, depending on the placement.
void insertHtml(const QString &html)
The signature is placed at the start of the textedit.
void KPIMIDENTITIES_DEPRECATED insertIntoTextEdit(KRichTextEdit *textEdit, Placement placement=End, bool addSeparator=true)
void setText(const QString &text)
Set the signature text and mark this signature as being of "inline text" type.
Signature()
Constructor for disabled signature.
QString & remove(int position, int n)
void setImageLocation(const QString &path)
Sets the location where the copies of the signature images will be stored.
QString rawText(bool *ok=0) const
bool movePosition(MoveOperation operation, MoveMode mode, int n)
bool hasSelection() const
QString fromLocal8Bit(const char *str, int size)
void setEnabledSignature(bool enabled)
setEnabledSignature
bool isInlinedHtml() const
void setInlinedHtml(bool isHtml)
Sets the inlined signature to text or html.
Signature & operator=(const Signature &that)
Assignment operator.
Abstraction of a signature (aka "footer").
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
const T value(const Key &key) const
The signature is placed at the end of the textedit.
static void KPIMIDENTITIES_DEPRECATED insertPlainSignatureIntoTextEdit(const QString &signature, KRichTextEdit *textEdit, Placement placement=End, bool isHtml=false)
Inserts this given signature into the given text edit.
The separator '– ' will be added in front of the signature.
Type
Type of signature (ie.
QString toPlainText() const
QString toPlainText() const
Returns the text of the signature.
QString withSeparator(bool *ok=0) const
QString fromLatin1(const char *str, int size)
bool operator==(const Signature &other) const
Used for comparison.
void setUrl(const QString &url, bool isExecutable=false)
Set the signature URL and mark this signature as being of "from file" resp.
void addImage(const QImage &image, const QString &imageName)
Adds the given image to the signature.
Placement
Describes the placement of the signature text when it is to be inserted into a text edit...
void setPosition(int pos, MoveMode m)