PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/kdepim-4.3.4/kmail/util.cpp

#
C++ | 174 lines | 111 code | 23 blank | 40 comment | 26 complexity | 3b2a133ec8c6d1e69ace73f1e7b78235 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0
  1. /*******************************************************************************
  2. **
  3. ** Filename : util
  4. ** Created on : 03 April, 2005
  5. ** Copyright : (c) 2005 Till Adam
  6. ** Email : <adam@kde.org>
  7. **
  8. *******************************************************************************/
  9. /*******************************************************************************
  10. **
  11. ** This program is free software; you can redistribute it and/or modify
  12. ** it under the terms of the GNU General Public License as published by
  13. ** the Free Software Foundation; either version 2 of the License, or
  14. ** (at your option) any later version.
  15. **
  16. ** It is distributed in the hope that it will be useful, but
  17. ** WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. ** General Public License for more details.
  20. **
  21. ** You should have received a copy of the GNU General Public License
  22. ** along with this program; if not, write to the Free Software
  23. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. **
  25. ** In addition, as a special exception, the copyright holders give
  26. ** permission to link the code of this program with any edition of
  27. ** the Qt library by Trolltech AS, Norway (or with modified versions
  28. ** of Qt that use the same license as Qt), and distribute linked
  29. ** combinations including the two. You must obey the GNU General
  30. ** Public License in all respects for all of the code used other than
  31. ** Qt. If you modify this file, you may extend this exception to
  32. ** your version of the file, but you are not obligated to do so. If
  33. ** you do not wish to do so, delete this exception statement from
  34. ** your version.
  35. **
  36. *******************************************************************************/
  37. #include "util.h"
  38. #include <stdlib.h>
  39. #include <mimelib/string.h>
  40. #include <mailtransport/transportmanager.h>
  41. #include <mailtransport/transport.h>
  42. void KMail::Util::reconnectSignalSlotPair( QObject *src, const char *signal, QObject *dst, const char *slot )
  43. {
  44. QObject::disconnect( src, signal, dst, slot );
  45. QObject::connect( src, signal, dst, slot );
  46. }
  47. size_t KMail::Util::crlf2lf( char* str, const size_t strLen )
  48. {
  49. if ( !str || strLen == 0 )
  50. return 0;
  51. const char* source = str;
  52. const char* sourceEnd = source + strLen;
  53. // search the first occurrence of "\r\n"
  54. for ( ; source < sourceEnd - 1; ++source ) {
  55. if ( *source == '\r' && *( source + 1 ) == '\n' )
  56. break;
  57. }
  58. if ( source == sourceEnd - 1 ) {
  59. // no "\r\n" found
  60. return strLen;
  61. }
  62. // replace all occurrences of "\r\n" with "\n" (in place)
  63. char* target = const_cast<char*>( source ); // target points to '\r'
  64. ++source; // source points to '\n'
  65. for ( ; source < sourceEnd; ++source ) {
  66. if ( *source != '\r' || *( source + 1 ) != '\n' )
  67. * target++ = *source;
  68. }
  69. *target = '\0'; // terminate result
  70. return target - str;
  71. }
  72. QByteArray KMail::Util::lf2crlf( const QByteArray & src )
  73. {
  74. QByteArray result;
  75. result.resize( 2*src.size() ); // maximal possible length
  76. QByteArray::ConstIterator s = src.begin();
  77. QByteArray::Iterator d = result.begin();
  78. // we use cPrev to make sure we insert '\r' only there where it is missing
  79. char cPrev = '?';
  80. const char* end = src.end();
  81. while ( s != end ) {
  82. if ( ('\n' == *s) && ('\r' != cPrev) )
  83. *d++ = '\r';
  84. cPrev = *s;
  85. *d++ = *s++;
  86. }
  87. result.truncate( d - result.begin() );
  88. return result;
  89. }
  90. QByteArray KMail::Util::ByteArray( const DwString& str )
  91. {
  92. const int strLen = str.size();
  93. QByteArray arr;
  94. arr.resize( strLen );
  95. memcpy( arr.data(), str.data(), strLen );
  96. return arr;
  97. }
  98. DwString KMail::Util::dwString( const QByteArray& str )
  99. {
  100. if ( !str.data() ) // DwString doesn't like char*=0
  101. return DwString();
  102. return DwString( str.data(), str.size() );
  103. }
  104. bool KMail::Util::checkOverwrite( const KUrl &url, QWidget *w )
  105. {
  106. if ( KIO::NetAccess::exists( url, KIO::NetAccess::DestinationSide, w ) ) {
  107. if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel(
  108. w,
  109. i18n( "A file named \"%1\" already exists. "
  110. "Are you sure you want to overwrite it?", url.prettyUrl() ),
  111. i18n( "Overwrite File?" ),
  112. KStandardGuiItem::overwrite() ) )
  113. return false;
  114. }
  115. return true;
  116. }
  117. #ifdef Q_WS_MACX
  118. #include <QDesktopServices>
  119. #endif
  120. bool KMail::Util::handleUrlOnMac( const KUrl& url )
  121. {
  122. #ifdef Q_WS_MACX
  123. QDesktopServices::openUrl( url );
  124. return true;
  125. #else
  126. Q_UNUSED( url );
  127. return false;
  128. #endif
  129. }
  130. bool KMail::Util::checkTransport( QWidget *w )
  131. {
  132. if ( MailTransport::TransportManager::self()->transportNames().isEmpty() ) {
  133. KMessageBox::information( w,
  134. i18n("Please create an account for sending and try again.") );
  135. return false;
  136. }
  137. return true;
  138. }
  139. KMail::Util::RecursionPreventer::RecursionPreventer( int &counter )
  140. : mCounter( counter )
  141. {
  142. mCounter++;
  143. }
  144. KMail::Util::RecursionPreventer::~RecursionPreventer()
  145. {
  146. mCounter--;
  147. }
  148. bool KMail::Util::RecursionPreventer::isRecursive() const
  149. {
  150. return mCounter > 1;
  151. }