util.h
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 TQt library by Trolltech AS, Norway (or with modified versions 00029 ** of TQt that use the same license as TQt), 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 ** TQt. 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 <tqobject.h> 00044 #include <tqcstring.h> 00045 00046 #include <kio/netaccess.h> 00047 #include <kmessagebox.h> 00048 #include <klocale.h> 00049 00050 class DwString; 00051 class KURL; 00052 class TQWidget; 00053 00054 namespace KMail 00055 { 00060 namespace Util { 00069 size_t crlf2lf( char* str, const size_t strLen ); 00070 00071 00077 TQCString lf2crlf( const TQCString & src ); 00083 TQByteArray lf2crlf( const TQByteArray & src ); 00084 00088 TQCString CString( const DwString& str ); 00089 00093 TQByteArray ByteArray( const DwString& str ); 00094 00098 DwString dwString( const TQCString& str ); 00099 00103 DwString dwString( const TQByteArray& str ); 00104 00108 void setFromTQCString( TQByteArray& arr, const TQCString& cstr ); 00109 00110 inline void setFromTQCString( TQByteArray& arr, const TQCString& cstr ) 00111 { 00112 if ( cstr.size() ) 00113 arr.duplicate( cstr.data(), cstr.size()-1 ); 00114 else 00115 arr.resize(0); 00116 } 00117 00123 TQByteArray byteArrayFromTQCStringNoDetach( TQCString& cstr ); 00124 inline TQByteArray byteArrayFromTQCStringNoDetach( TQCString& cstr ) 00125 { 00126 TQByteArray arr = cstr; 00127 if ( arr.size() ) 00128 arr.resize( arr.size() - 1 ); 00129 return arr; 00130 } 00131 00135 void restoreTQCString( TQCString& str ); 00136 inline void restoreTQCString( TQCString& str ) 00137 { 00138 if ( str.data() ) 00139 str.resize( str.size() + 1 ); 00140 } 00141 00145 void setFromByteArray( TQCString& cstr, const TQByteArray& arr ); 00146 00147 inline void setFromByteArray( TQCString& result, const TQByteArray& 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( TQByteArray& that, const TQByteArray& str ); 00159 00163 void append( TQByteArray& that, const char* str ); 00164 00168 void append( TQByteArray& that, const TQCString& str ); 00169 00170 void insert( TQByteArray& that, uint index, const char* s ); 00171 00179 class LaterDeleter 00180 { 00181 public: 00182 LaterDeleter( TQObject *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 TQObject *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, TQWidget* 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