kpimidentities
signature.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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();
00073 }
00074
00075 QString Signature::textFromCommand( bool *ok ) const
00076 {
00077 assert( mType == FromCommand );
00078
00079
00080 if ( mUrl.isEmpty() ) {
00081 if ( ok ) {
00082 *ok = true;
00083 }
00084 return QString();
00085 }
00086
00087
00088 KProcess proc;
00089 proc.setOutputChannelMode( KProcess::SeparateChannels );
00090 proc.setShellCommand( mUrl );
00091 int rc = proc.execute();
00092
00093
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
00105 if ( ok ) {
00106 *ok = true;
00107 }
00108
00109
00110 QByteArray output = proc.readAllStandardOutput();
00111
00112
00113 return QString::fromLocal8Bit( output.data(), output.size() );
00114 }
00115
00116 QString Signature::textFromFile( bool *ok ) const
00117 {
00118 assert( mType == FromFile );
00119
00120
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
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;
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
00156 return signature;
00157 } else {
00158
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
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
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
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
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 }