kmail

util.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 **
00003 ** Filename   : util
00004 ** Created on : 03 April, 2005
00005 ** Copyright  : (c) 2005 Till Adam
00006 ** Email      : <adam@kde.org>
00007 **
00008 *******************************************************************************/
00009 
00010 /*******************************************************************************
00011 **
00012 **   This program is free software; you can redistribute it and/or modify
00013 **   it under the terms of the GNU General Public License as published by
00014 **   the Free Software Foundation; either version 2 of the License, or
00015 **   (at your option) any later version.
00016 **
00017 **   It is distributed in the hope that it will be useful, but
00018 **   WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **   General Public License for more details.
00021 **
00022 **   You should have received a copy of the GNU General Public License
00023 **   along with this program; if not, write to the Free Software
00024 **   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00025 **
00026 **   In addition, as a special exception, the copyright holders give
00027 **   permission to link the code of this program with any edition of
00028 **   the Qt library by Trolltech AS, Norway (or with modified versions
00029 **   of Qt that use the same license as Qt), and distribute linked
00030 **   combinations including the two.  You must obey the GNU General
00031 **   Public License in all respects for all of the code used other than
00032 **   Qt.  If you modify this file, you may extend this exception to
00033 **   your version of the file, but you are not obligated to do so.  If
00034 **   you do not wish to do so, delete this exception statement from
00035 **   your version.
00036 **
00037 *******************************************************************************/
00038 #ifndef KMAILUTIL_H
00039 #define KMAILUTIL_H
00040 
00041 #include <stdlib.h>
00042 
00043 #include <qobject.h>
00044 #include <qcstring.h>
00045 
00046 #include <kio/netaccess.h>
00047 #include <kmessagebox.h>
00048 #include <klocale.h>
00049 
00050 class DwString;
00051 class KURL;
00052 class QWidget;
00053 
00054 namespace KMail
00055 {
00060 namespace Util {
00069     size_t crlf2lf( char* str, const size_t strLen );
00070 
00071 
00077     QCString lf2crlf( const QCString & src );
00083     QByteArray lf2crlf( const QByteArray & src );
00084 
00088     QCString CString( const DwString& str );
00089 
00093     QByteArray ByteArray( const DwString& str );
00094 
00098     DwString dwString( const QCString& str );
00099 
00103     DwString dwString( const QByteArray& str );
00104 
00108     void setFromQCString( QByteArray& arr, const QCString& cstr );
00109 
00110     inline void setFromQCString( QByteArray& arr, const QCString& cstr )
00111     {
00112       if ( cstr.size() )
00113         arr.duplicate( cstr.data(), cstr.size()-1 );
00114       else
00115         arr.resize(0);
00116     }
00117 
00123     QByteArray byteArrayFromQCStringNoDetach( QCString& cstr );
00124     inline QByteArray byteArrayFromQCStringNoDetach( QCString& cstr )
00125     {
00126       QByteArray arr = cstr;
00127       if ( arr.size() )
00128         arr.resize( arr.size() - 1 );
00129       return arr;
00130     }
00131 
00135     void restoreQCString( QCString& str );
00136     inline void restoreQCString( QCString& str )
00137     {
00138       if ( str.data() )
00139         str.resize( str.size() + 1 );
00140     }
00141 
00145     void setFromByteArray( QCString& cstr, const QByteArray& arr );
00146 
00147     inline void setFromByteArray( QCString& result, const QByteArray& arr )
00148     {
00149       const int len = arr.size();
00150       result.resize( len + 1 /* trailing NUL */ );
00151       memcpy(result.data(), arr.data(), len);
00152       result[len] = 0;
00153     }
00154 
00158     void append( QByteArray& that, const QByteArray& str );
00159 
00163     void append( QByteArray& that, const char* str );
00164 
00168     void append( QByteArray& that, const QCString& str );
00169 
00170     void insert( QByteArray& that, uint index, const char* s );
00171 
00179     class LaterDeleter
00180     {
00181       public:
00182       LaterDeleter( QObject *o)
00183         :m_object( o ), m_disabled( false )
00184       {
00185       }
00186       virtual ~LaterDeleter()
00187       {
00188         if ( !m_disabled ) {
00189           m_object->deleteLater();
00190         }
00191       }
00192       void setDisabled( bool v )
00193       {
00194         m_disabled = v;
00195       }
00196       protected:
00197       QObject *m_object;
00198       bool m_disabled;
00199     };
00200 
00201     // return true if we should proceed, false if we should abort
00202     inline bool checkOverwrite( const KURL& url, QWidget* w )
00203     {
00204         if ( KIO::NetAccess::exists( url, false /*dest*/, w ) ) {
00205             if ( KMessageBox::Cancel ==
00206                     KMessageBox::warningContinueCancel(
00207                         w,
00208                         i18n( "A file named \"%1\" already exists. "
00209                             "Are you sure you want to overwrite it?" ).arg( url.prettyURL() ),
00210                         i18n( "Overwrite File?" ),
00211                         i18n( "&Overwrite" ) ) )
00212                 return false;
00213         }
00214         return true;
00215     }
00216 
00217 
00218 
00219 }
00220 }
00221 
00222 #endif