• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kpimidentities

signature.cpp

00001 /*
00002     Copyright (c) 2002-2004 Marc Mutz <mutz@kde.org>
00003     Copyright (c) 2007 Tom Albers <tomalbers@kde.nl>
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
00019 */
00020 
00021 #include "signature.h"
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <kconfiggroup.h>
00027 #include <kurl.h>
00028 #include <kprocess.h>
00029 #include <kpimutils/kfileio.h>
00030 
00031 #include <QFileInfo>
00032 #include <assert.h>
00033 
00034 using namespace KPIMIdentities;
00035 
00036 Signature::Signature()
00037   : mType( Disabled ),
00038     mInlinedHtml( false )
00039 {}
00040 
00041 Signature::Signature( const QString &text )
00042   : mText( text ),
00043     mType( Inlined ),
00044     mInlinedHtml( false )
00045 {}
00046 
00047 Signature::Signature( const QString &url, bool isExecutable )
00048   : mUrl( url ),
00049     mType( isExecutable ? FromCommand : FromFile ),
00050     mInlinedHtml( false )
00051 {}
00052 
00053 QString Signature::rawText( bool *ok ) const
00054 {
00055   switch ( mType ) {
00056   case Disabled:
00057     if ( ok ) {
00058       *ok = true;
00059     }
00060     return QString();
00061   case Inlined:
00062     if ( ok ) {
00063       *ok = true;
00064     }
00065     return mText;
00066   case FromFile:
00067     return textFromFile( ok );
00068   case FromCommand:
00069     return textFromCommand( ok );
00070   };
00071   kFatal(5325) << "Signature::type() returned unknown value!";
00072   return QString(); // make compiler happy
00073 }
00074 
00075 QString Signature::textFromCommand( bool *ok ) const
00076 {
00077   assert( mType == FromCommand );
00078 
00079   // handle pathological cases:
00080   if ( mUrl.isEmpty() ) {
00081     if ( ok ) {
00082       *ok = true;
00083     }
00084     return QString();
00085   }
00086 
00087   // create a shell process:
00088   KProcess proc;
00089   proc.setOutputChannelMode( KProcess::SeparateChannels );
00090   proc.setShellCommand( mUrl );
00091   int rc = proc.execute();
00092 
00093   // handle errors, if any:
00094   if ( rc != 0 ) {
00095     if ( ok ) {
00096       *ok = false;
00097     }
00098     QString wmsg = i18n( "<qt>Failed to execute signature script<p><b>%1</b>:</p>"
00099                          "<p>%2</p></qt>", mUrl, QString( proc.readAllStandardError() ) );
00100     KMessageBox::error( 0, wmsg );
00101     return QString();
00102   }
00103 
00104   // no errors:
00105   if ( ok ) {
00106     *ok = true;
00107   }
00108 
00109   // get output:
00110   QByteArray output = proc.readAllStandardOutput();
00111 
00112   // TODO: hmm, should we allow other encodings, too?
00113   return QString::fromLocal8Bit( output.data(), output.size() );
00114 }
00115 
00116 QString Signature::textFromFile( bool *ok ) const
00117 {
00118   assert( mType == FromFile );
00119 
00120   // TODO: Use KIO::NetAccess to download non-local files!
00121   if ( !KUrl( mUrl ).isLocalFile() &&
00122        !( QFileInfo( mUrl ).isRelative() &&
00123           QFileInfo( mUrl ).exists() ) ) {
00124     kDebug(5325) << "Signature::textFromFile:"
00125     << "non-local URLs are unsupported";
00126     if ( ok ) {
00127       *ok = false;
00128     }
00129     return QString();
00130   }
00131 
00132   if ( ok ) {
00133     *ok = true;
00134   }
00135 
00136   // TODO: hmm, should we allow other encodings, too?
00137   const QByteArray ba = KPIMUtils::kFileToByteArray( mUrl, false );
00138   return QString::fromLocal8Bit( ba.data(), ba.size() );
00139 }
00140 
00141 QString Signature::withSeparator( bool *ok ) const
00142 {
00143   QString signature = rawText( ok );
00144   if ( ok && (*ok) == false )
00145     return QString();
00146 
00147   if ( signature.isEmpty() ) {
00148     return signature; // don't add a separator in this case
00149   }
00150 
00151   QString newline = ( isInlinedHtml() && mType == Inlined ) ? "<br>" : "\n";
00152   if ( signature.startsWith( QString::fromLatin1( "-- " ) + newline )
00153     || ( signature.indexOf( newline + QString::fromLatin1( "-- " ) +
00154                             newline ) != -1 ) ) {
00155     // already have signature separator at start of sig or inside sig:
00156     return signature;
00157   } else {
00158     // need to prepend one:
00159     return QString::fromLatin1( "-- " ) + newline + signature;
00160   }
00161 }
00162 
00163 void Signature::setUrl( const QString &url, bool isExecutable )
00164 {
00165   mUrl = url;
00166   mType = isExecutable ? FromCommand : FromFile;
00167 }
00168 
00169 void Signature::setInlinedHtml( bool isHtml )
00170 {
00171   mInlinedHtml = isHtml;
00172 }
00173 
00174 bool Signature::isInlinedHtml() const
00175 {
00176   return mInlinedHtml;
00177 }
00178 
00179 // config keys and values:
00180 static const char sigTypeKey[] = "Signature Type";
00181 static const char sigTypeInlineValue[] = "inline";
00182 static const char sigTypeFileValue[] = "file";
00183 static const char sigTypeCommandValue[] = "command";
00184 static const char sigTypeDisabledValue[] = "disabled";
00185 static const char sigTextKey[] = "Inline Signature";
00186 static const char sigFileKey[] = "Signature File";
00187 static const char sigCommandKey[] = "Signature Command";
00188 static const char sigTypeInlinedHtmlKey[] = "Inlined Html";
00189 
00190 void Signature::readConfig( const KConfigGroup &config )
00191 {
00192   QString sigType = config.readEntry( sigTypeKey );
00193   if ( sigType == sigTypeInlineValue ) {
00194     mType = Inlined;
00195     mInlinedHtml = config.readEntry( sigTypeInlinedHtmlKey, false );
00196   } else if ( sigType == sigTypeFileValue ) {
00197     mType = FromFile;
00198     mUrl = config.readPathEntry( sigFileKey, QString() );
00199   } else if ( sigType == sigTypeCommandValue ) {
00200     mType = FromCommand;
00201     mUrl = config.readPathEntry( sigCommandKey, QString() );
00202   } else {
00203     mType = Disabled;
00204   }
00205   mText = config.readEntry( sigTextKey );
00206 }
00207 
00208 void Signature::writeConfig( KConfigGroup &config ) const
00209 {
00210   switch ( mType ) {
00211     case Inlined:
00212       config.writeEntry( sigTypeKey, sigTypeInlineValue );
00213       config.writeEntry( sigTypeInlinedHtmlKey, mInlinedHtml );
00214       break;
00215     case FromFile:
00216       config.writeEntry( sigTypeKey, sigTypeFileValue );
00217       config.writePathEntry( sigFileKey, mUrl );
00218       break;
00219     case FromCommand:
00220       config.writeEntry( sigTypeKey, sigTypeCommandValue );
00221       config.writePathEntry( sigCommandKey, mUrl );
00222       break;
00223     case Disabled:
00224       config.writeEntry( sigTypeKey, sigTypeDisabledValue );
00225     default:
00226       break;
00227   }
00228   config.writeEntry( sigTextKey, mText );
00229 }
00230 
00231 // --------------------- Operators -------------------//
00232 
00233 QDataStream &KPIMIdentities::operator<<
00234 ( QDataStream &stream, const KPIMIdentities::Signature &sig )
00235 {
00236   return stream << static_cast<quint8>( sig.mType ) << sig.mUrl << sig.mText;
00237 }
00238 
00239 QDataStream &KPIMIdentities::operator>>
00240 ( QDataStream &stream, KPIMIdentities::Signature &sig )
00241 {
00242   quint8 s;
00243   stream >> s  >> sig.mUrl >> sig.mText;
00244   sig.mType = static_cast<Signature::Type>( s );
00245   return stream;
00246 }
00247 
00248 bool Signature::operator== ( const Signature &other ) const
00249 {
00250   if ( mType != other.mType ) {
00251     return false;
00252   }
00253 
00254   switch ( mType ) {
00255   case Inlined:
00256     return mText == other.mText;
00257   case FromFile:
00258   case FromCommand:
00259     return mUrl == other.mUrl;
00260   default:
00261   case Disabled:
00262     return true;
00263   }
00264 }
00265 
00266 // --------------- Getters -----------------------//
00267 
00268 QString Signature::text() const
00269 {
00270   return mText;
00271 }
00272 
00273 QString Signature::url() const
00274 {
00275   return mUrl;
00276 }
00277 
00278 Signature::Type Signature::type() const
00279 {
00280   return mType;
00281 }
00282 
00283 // --------------- Setters -----------------------//
00284 
00285 void Signature::setText( const QString &text )
00286 {
00287   mText = text;
00288 }
00289 
00290 void Signature::setType( Type type )
00291 {
00292   mType = type;
00293 }

kpimidentities

Skip menu "kpimidentities"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal