kmail Library API Documentation

kmgroupwarefuncs.cpp

00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "kmgroupwarefuncs.h"
00006 
00007 #include <stdlib.h>
00008 #include <time.h>
00009 
00010 
00011 /* temporarily go to a different timezone */
00012 struct save_tz set_tz( const char* _tc )
00013 {
00014   const char *tc = _tc?_tc:"UTC";
00015 
00016   struct save_tz rv;
00017 
00018   rv.old_tz = 0;
00019   rv.tz_env_str = 0;
00020 
00021   //kdDebug(5006) << "set_tz(), timezone before = " << timezone << endl;
00022 
00023   char* tz_env = 0;
00024   if( getenv( "TZ" ) ) {
00025     tz_env = strdup( getenv( "TZ" ) );
00026     rv.old_tz = tz_env;
00027   }
00028   char* tmp_env = (char*)malloc( strlen( tc ) + 4 );
00029   strcpy( tmp_env, "TZ=" );
00030   strcpy( tmp_env+3, tc );
00031   putenv( tmp_env );
00032 
00033   rv.tz_env_str = tmp_env;
00034 
00035   /* tmp_env is not free'ed -- it is part of the environment */
00036 
00037   tzset();
00038   //kdDebug(5006) << "set_tz(), timezone after = " << timezone << endl;
00039 
00040   return rv;
00041 }
00042 
00043 /* restore previous timezone */
00044 void unset_tz( struct save_tz old_tz )
00045 {
00046   if( old_tz.old_tz ) {
00047     char* tmp_env = (char*)malloc( strlen( old_tz.old_tz ) + 4 );
00048     strcpy( tmp_env, "TZ=" );
00049     strcpy( tmp_env+3, old_tz.old_tz );
00050     putenv( tmp_env );
00051     /* tmp_env is not free'ed -- it is part of the environment */
00052     free( old_tz.old_tz );
00053   } else {
00054     /* clear TZ from env */
00055     putenv( strdup("TZ") );
00056   }
00057   tzset();
00058 
00059   /* is this OK? */
00060   if( old_tz.tz_env_str ) free( old_tz.tz_env_str );
00061 }
00062 
00063 QDateTime utc2Local( const QDateTime& utcdt )
00064 {
00065   struct tm tmL;
00066 
00067   save_tz tmp_tz = set_tz("UTC");
00068   time_t utc = utcdt.toTime_t();
00069   unset_tz( tmp_tz );
00070 
00071   localtime_r( &utc, &tmL );
00072   return QDateTime( QDate( tmL.tm_year+1900, tmL.tm_mon+1, tmL.tm_mday ),
00073                     QTime( tmL.tm_hour, tmL.tm_min, tmL.tm_sec ) );
00074 }
00075 
00076 QDateTime pureISOToLocalQDateTime( const QString& dtStr, bool bDateOnly )
00077 {
00078   QDate tmpDate;
00079   QTime tmpTime;
00080   int year, month, day, hour, minute, second;
00081 
00082   if( bDateOnly ){
00083     year   = dtStr.left(4).toInt();
00084     month  = dtStr.mid(4,2).toInt();
00085     day    = dtStr.mid(6,2).toInt();
00086     hour   = 0;
00087     minute = 0;
00088     second = 0;
00089   }else{
00090     year   = dtStr.left(4).toInt();
00091     month  = dtStr.mid(4,2).toInt();
00092     day    = dtStr.mid(6,2).toInt();
00093     hour   = dtStr.mid(9,2).toInt();
00094     minute = dtStr.mid(11,2).toInt();
00095     second = dtStr.mid(13,2).toInt();
00096   }
00097   tmpDate.setYMD(year, month, day);
00098   tmpTime.setHMS(hour, minute, second);
00099 
00100   if( tmpDate.isValid() && tmpTime.isValid() ){
00101     QDateTime dT = QDateTime(tmpDate, tmpTime);
00102 
00103     if( !bDateOnly ){
00104       // correct for GMT ( == Zulu time == UTC )
00105       if (dtStr.at(dtStr.length()-1) == 'Z'){
00106         //dT = dT.addSecs( 60 * KRFCDate::localUTCOffset() );//localUTCOffset( dT ) );
00107         dT = utc2Local( dT );
00108       }
00109     }
00110     return dT;
00111   }else
00112     return QDateTime();
00113 }
00114 
00115 
00116 QString ISOToLocalQDateTime( const QString& dtStr )
00117 {
00118   const QString sDateFlag("VALUE=DATE:");
00119 
00120   QString tmpStr = dtStr.upper();
00121   const bool bDateOnly = tmpStr.startsWith( sDateFlag );
00122   if( bDateOnly )
00123     tmpStr = tmpStr.remove( sDateFlag );
00124   QDateTime dT( pureISOToLocalQDateTime(tmpStr, bDateOnly) );
00125   if( dT.isValid() )
00126     tmpStr = dT.toString( Qt::ISODate ) + '@' + dT.toString( Qt::LocalDate );
00127   else
00128     tmpStr = "?@?";
00129   return tmpStr;
00130 }
00131 
00132 
00133 // This is a very light-weight and fast 'parser' to retrieve up
00134 // to 7 data entries from a vCal taking continuation lines
00135 // into account
00136 // This very primitive function may be removed once a link
00137 // to an iCal/vCal parsing library is established...
00138 QString nullQString;
00139 void vPartMicroParser( const QCString& str, QString& s1, QString& s2, QString& s3,
00140                QString& s4, QString& s5, QString& s6, QString& s7 )
00141 {
00142   QString line;
00143   uint len = str.length();
00144 
00145   // "b1 (or b2..b7, resp.) == true"  means  "seach for s1 (or s2..s7, resp.)"
00146   bool b1 = true, b2 = (&s2!=&nullQString), b3 = (&s3!=&nullQString);
00147   bool b4 = (&s4!=&nullQString), b5 = (&s5!=&nullQString), b6 = (&s6!=&nullQString);
00148   bool b7 = (&s7!=&nullQString);
00149 
00150   for( uint i=0; i<len && (b1 || b2 || b3 || b4 || b5 || b6 || b7 ); ++i){
00151     if( str[i] == '\r' || str[i] == '\n' ){
00152       if( str[i] == '\r' )
00153         ++i;
00154       if( i+1 < len && str[i+1] == ' ' ){
00155         // found a continuation line, skip it's leading blanc
00156         ++i;
00157       }else{
00158         // found a logical line end, process the line
00159         if( b1 && line.startsWith( s1 ) ){
00160           s1 = line.mid( s1.length() + 1 );
00161           b1 = false;
00162         }else if( b2 && line.startsWith( s2 ) ){
00163           s2 = line.mid( s2.length() + 1 );
00164           b2 = false;
00165         }else if( b3 && line.startsWith( s3 ) ){
00166           s3 = line.mid( s3.length() + 1 );
00167           b3 = false;
00168         }else if( b4 && line.startsWith( s4 ) ){
00169           s4 = line.mid( s4.length() + 1 );
00170           b4 = false;
00171         }else if( b5 && line.startsWith( s5 ) ){
00172           s5 = line.mid( s5.length() + 1 );
00173           b5 = false;
00174         }else if( b6 && line.startsWith( s6 ) ){
00175           s6 = line.mid( s6.length() + 1 );
00176           b6 = false;
00177         }else if( b7 && line.startsWith( s7 ) ){
00178           s7 = line.mid( s7.length() + 1 );
00179           b7 = false;
00180         }
00181         line = "";
00182       }
00183     }else{
00184       line += str[i];
00185     }
00186   }
00187   if( b1 )
00188     s1.truncate(0);
00189   if( b2 )
00190     s2.truncate(0);
00191   if( b3 )
00192     s3.truncate(0);
00193   if( b4 )
00194     s4.truncate(0);
00195   if( b5 )
00196     s5.truncate(0);
00197   if( b6 )
00198     s6.truncate(0);
00199   if( b7 )
00200     s7.truncate(0);
00201 }
00202 
00203 QString isoDateTimeToLocal(const QString& isoStr )
00204 {
00205   const QDateTime dt( QDateTime::fromString( isoStr, Qt::ISODate ) );
00206   if( dt.time() == QTime(0,0,0) )
00207     return dt.date().toString( Qt::LocalDate );
00208   return dt.toString( Qt::LocalDate );
00209 }
00210 
00211 void string2HTML( QString& str ) {
00212   str.replace( QChar('&'), "&amp;" );
00213   str.replace( QChar('<'), "&lt;" );
00214   str.replace( QChar('>'), "&gt;" );
00215   str.replace( QChar('\"'), "&quot;" );
00216   str.replace( "\\n", "<br>" );
00217   str.replace( "\\,", "," );
00218 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:28 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003