• Skip to content
  • Skip to link menu
KDE 4.0 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 {}
00039 
00040 Signature::Signature( const QString &text )
00041   : mText( text ),
00042     mType( Inlined )
00043 {}
00044 
00045 Signature::Signature( const QString &url, bool isExecutable )
00046   : mUrl( url ),
00047     mType( isExecutable ? FromCommand : FromFile )
00048 {}
00049 
00050 QString Signature::rawText( bool *ok ) const
00051 {
00052   switch ( mType ) {
00053   case Disabled:
00054     if ( ok ) {
00055       *ok = true;
00056     }
00057     return QString();
00058   case Inlined:
00059     if ( ok ) {
00060       *ok = true;
00061     }
00062     return mText;
00063   case FromFile:
00064     return textFromFile( ok );
00065   case FromCommand:
00066     return textFromCommand( ok );
00067   };
00068   kFatal(5325) << "Signature::type() returned unknown value!";
00069   return QString(); // make compiler happy
00070 }
00071 
00072 QString Signature::textFromCommand( bool *ok ) const
00073 {
00074   assert( mType == FromCommand );
00075 
00076   // handle pathological cases:
00077   if ( mUrl.isEmpty() ) {
00078     if ( ok ) {
00079       *ok = true;
00080     }
00081     return QString();
00082   }
00083 
00084   // create a shell process:
00085   KProcess proc;
00086   proc.setOutputChannelMode( KProcess::SeparateChannels );
00087   proc.setShellCommand( mUrl );
00088   int rc = proc.execute();
00089 
00090   // handle errors, if any:
00091   if ( rc != 0 ) {
00092     if ( ok ) {
00093       *ok = false;
00094     }
00095     QString wmsg = i18n( "<qt>Failed to execute signature script<br><b>%1</b>:"
00096                          "<p>%2</p></qt>", mUrl, QString( proc.readAllStandardError() ) );
00097     KMessageBox::error( 0, wmsg );
00098     return QString();
00099   }
00100 
00101   // no errors:
00102   if ( ok ) {
00103     *ok = true;
00104   }
00105 
00106   // get output:
00107   QByteArray output = proc.readAllStandardOutput();
00108 
00109   // TODO: hmm, should we allow other encodings, too?
00110   return QString::fromLocal8Bit( output.data(), output.size() );
00111 }
00112 
00113 QString Signature::textFromFile( bool *ok ) const
00114 {
00115   assert( mType == FromFile );
00116 
00117   // TODO: Use KIO::NetAccess to download non-local files!
00118   if ( !KUrl( mUrl ).isLocalFile() &&
00119        !( QFileInfo( mUrl ).isRelative() &&
00120           QFileInfo( mUrl ).exists() ) ) {
00121     kDebug(5325) << "Signature::textFromFile:"
00122     << "non-local URLs are unsupported";
00123     if ( ok ) {
00124       *ok = false;
00125     }
00126     return QString();
00127   }
00128 
00129   if ( ok ) {
00130     *ok = true;
00131   }
00132 
00133   // TODO: hmm, should we allow other encodings, too?
00134   const QByteArray ba = KPIMUtils::kFileToByteArray( mUrl, false );
00135   return QString::fromLocal8Bit( ba.data(), ba.size() );
00136 }
00137 
00138 QString Signature::withSeparator( bool *ok ) const
00139 {
00140   bool internalOK = false;
00141   QString signature = rawText( &internalOK );
00142   if ( !internalOK ) {
00143     if ( ok ) {
00144       *ok = false;
00145     }
00146     return QString();
00147   }
00148   if ( ok ) {
00149     *ok = true;
00150   }
00151 
00152   if ( signature.isEmpty() ) {
00153     return signature; // don't add a separator in this case
00154   }
00155 
00156   if ( signature.startsWith( QString::fromLatin1( "-- \n" ) ) ) {
00157     // already have signature separator at start of sig:
00158     return signature;
00159   } else {
00160     // need to prepend one:
00161     return QString::fromLatin1( "-- \n" ) + signature;
00162   }
00163 }
00164 
00165 void Signature::setUrl( const QString &url, bool isExecutable )
00166 {
00167   mUrl = url;
00168   mType = isExecutable ? FromCommand : FromFile;
00169 }
00170 
00171 // config keys and values:
00172 static const char sigTypeKey[] = "Signature Type";
00173 static const char sigTypeInlineValue[] = "inline";
00174 static const char sigTypeFileValue[] = "file";
00175 static const char sigTypeCommandValue[] = "command";
00176 static const char sigTypeDisabledValue[] = "disabled";
00177 static const char sigTextKey[] = "Inline Signature";
00178 static const char sigFileKey[] = "Signature File";
00179 static const char sigCommandKey[] = "Signature Command";
00180 
00181 void Signature::readConfig( const KConfigGroup &config )
00182 {
00183   QString sigType = config.readEntry( sigTypeKey );
00184   if ( sigType == sigTypeInlineValue ) {
00185     mType = Inlined;
00186   } else if ( sigType == sigTypeFileValue ) {
00187     mType = FromFile;
00188     mUrl = config.readPathEntry( sigFileKey, QString() );
00189   } else if ( sigType == sigTypeCommandValue ) {
00190     mType = FromCommand;
00191     mUrl = config.readPathEntry( sigCommandKey, QString() );
00192   } else {
00193     mType = Disabled;
00194   }
00195   mText = config.readEntry( sigTextKey );
00196 }
00197 
00198 void Signature::writeConfig( KConfigGroup &config ) const
00199 {
00200   switch ( mType ) {
00201     case Inlined:
00202       config.writeEntry( sigTypeKey, sigTypeInlineValue );
00203       break;
00204     case FromFile:
00205       config.writeEntry( sigTypeKey, sigTypeFileValue );
00206       config.writePathEntry( sigFileKey, mUrl );
00207       break;
00208     case FromCommand:
00209       config.writeEntry( sigTypeKey, sigTypeCommandValue );
00210       config.writePathEntry( sigCommandKey, mUrl );
00211       break;
00212     case Disabled:
00213       config.writeEntry( sigTypeKey, sigTypeDisabledValue );
00214     default:
00215       break;
00216   }
00217   config.writeEntry( sigTextKey, mText );
00218 }
00219 
00220 // --------------------- Operators -------------------//
00221 
00222 QDataStream &KPIMIdentities::operator<<
00223 ( QDataStream &stream, const KPIMIdentities::Signature &sig )
00224 {
00225   return stream << static_cast<quint8>( sig.mType ) << sig.mUrl << sig.mText;
00226 }
00227 
00228 QDataStream &KPIMIdentities::operator>>
00229 ( QDataStream &stream, KPIMIdentities::Signature &sig )
00230 {
00231   quint8 s;
00232   stream >> s  >> sig.mUrl >> sig.mText;
00233   sig.mType = static_cast<Signature::Type>( s );
00234   return stream;
00235 }
00236 
00237 bool Signature::operator== ( const Signature &other ) const
00238 {
00239   if ( mType != other.mType ) {
00240     return false;
00241   }
00242 
00243   switch ( mType ) {
00244   case Inlined:
00245     return mText == other.mText;
00246   case FromFile:
00247   case FromCommand:
00248     return mUrl == other.mUrl;
00249   default:
00250   case Disabled:
00251     return true;
00252   }
00253 }
00254 
00255 // --------------- Getters -----------------------//
00256 
00257 QString Signature::text() const
00258 {
00259   return mText;
00260 }
00261 
00262 QString Signature::url() const
00263 {
00264   return mUrl;
00265 }
00266 
00267 Signature::Type Signature::type() const
00268 {
00269   return mType;
00270 }
00271 
00272 // --------------- Setters -----------------------//
00273 
00274 void Signature::setText( const QString &text )
00275 {
00276   mText = text;
00277 }
00278 
00279 void Signature::setType( Type type )
00280 {
00281   mType = type;
00282 }

kpimidentities

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
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