Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kmail / util.cpp
bloba914c08adb91dd1ed70d4573745b4119fbd96560
1 /*******************************************************************************
2 **
3 ** Filename : util
4 ** Created on : 03 April, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
7 **
8 *******************************************************************************/
10 /*******************************************************************************
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
17 ** It is distributed in the hope that it will be useful, but
18 ** WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 ** In addition, as a special exception, the copyright holders give
27 ** permission to link the code of this program with any edition of
28 ** the Qt library by Trolltech AS, Norway (or with modified versions
29 ** of Qt that use the same license as Qt), and distribute linked
30 ** combinations including the two. You must obey the GNU General
31 ** Public License in all respects for all of the code used other than
32 ** Qt. If you modify this file, you may extend this exception to
33 ** your version of the file, but you are not obligated to do so. If
34 ** you do not wish to do so, delete this exception statement from
35 ** your version.
37 *******************************************************************************/
40 #include "util.h"
42 #include <stdlib.h>
43 #include <mimelib/string.h>
45 void KMail::Util::reconnectSignalSlotPair( QObject *src, const char *signal, QObject *dst, const char *slot )
47 QObject::disconnect( src, signal, dst, slot );
48 QObject::connect( src, signal, dst, slot );
52 size_t KMail::Util::crlf2lf( char* str, const size_t strLen )
54 if ( !str || strLen == 0 )
55 return 0;
57 const char* source = str;
58 const char* sourceEnd = source + strLen;
60 // search the first occurrence of "\r\n"
61 for ( ; source < sourceEnd - 1; ++source ) {
62 if ( *source == '\r' && *( source + 1 ) == '\n' )
63 break;
66 if ( source == sourceEnd - 1 ) {
67 // no "\r\n" found
68 return strLen;
71 // replace all occurrences of "\r\n" with "\n" (in place)
72 char* target = const_cast<char*>( source ); // target points to '\r'
73 ++source; // source points to '\n'
74 for ( ; source < sourceEnd; ++source ) {
75 if ( *source != '\r' || *( source + 1 ) != '\n' )
76 * target++ = *source;
78 *target = '\0'; // terminate result
79 return target - str;
82 QByteArray KMail::Util::lf2crlf( const QByteArray & src )
84 QByteArray result;
85 result.resize( 2*src.size() ); // maximal possible length
87 QByteArray::ConstIterator s = src.begin();
88 QByteArray::Iterator d = result.begin();
89 // we use cPrev to make sure we insert '\r' only there where it is missing
90 char cPrev = '?';
91 const char* end = src.end();
92 while ( s != end ) {
93 if ( ('\n' == *s) && ('\r' != cPrev) )
94 *d++ = '\r';
95 cPrev = *s;
96 *d++ = *s++;
98 result.truncate( d - result.begin() );
99 return result;
102 QByteArray KMail::Util::ByteArray( const DwString& str )
104 const int strLen = str.size();
105 QByteArray arr;
106 arr.resize( strLen );
107 memcpy( arr.data(), str.data(), strLen );
108 return arr;
111 DwString KMail::Util::dwString( const QByteArray& str )
113 if ( !str.data() ) // DwString doesn't like char*=0
114 return DwString();
115 return DwString( str.data(), str.size() );
118 bool KMail::Util::checkOverwrite( const KUrl &url, QWidget *w )
120 if ( KIO::NetAccess::exists( url, KIO::NetAccess::DestinationSide, w ) ) {
121 if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel(
123 i18n( "A file named \"%1\" already exists. "
124 "Are you sure you want to overwrite it?", url.prettyUrl() ),
125 i18n( "Overwrite File?" ),
126 KStandardGuiItem::overwrite() ) )
127 return false;
129 return true;
132 #ifdef Q_WS_MACX
133 #include <QDesktopServices>
134 #endif
136 bool KMail::Util::handleUrlOnMac( const KUrl& url )
138 #ifdef Q_WS_MACX
139 QDesktopServices::openUrl( url );
140 return true;
141 #else
142 Q_UNUSED( url );
143 return false;
144 #endif