kitchensync Library API Documentation

helper.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002,2003 Holger Freyther <freyther@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <config.h>
00023 
00024 #include <kstandarddirs.h>
00025 #include <kdebug.h>
00026 
00027 #include "helper.h"
00028 
00029 using namespace OpieHelper;
00030 
00031 Base::Base( CategoryEdit* edit,
00032             KSync::KonnectorUIDHelper* helper,
00033             const QString &tz,
00034             bool metaSyncing, Device* dev )
00035 {
00036     m_metaSyncing = metaSyncing;
00037     m_edit = edit;
00038     m_helper = helper;
00039     m_tz = tz;
00040     m_device = dev;
00041 }
00042 Base::~Base()
00043 {
00044 
00045 }
00046 QDateTime Base::fromUTC( time_t time )
00047 {
00048    struct tm *lt;
00049 
00050    /* getenv can be NULL */
00051    char* ptrTz = getenv( "TZ");
00052    QString real_TZ = ptrTz ? QString::fromLocal8Bit( ptrTz ) : QString::null;
00053 
00054    if (!m_tz.isEmpty() )
00055        setenv( "TZ", m_tz.local8Bit(), true );
00056 
00057    kdDebug(5229) << "TimeZone was " << real_TZ << " TimeZone now is " << m_tz << endl;
00058 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00059     _tzset();
00060 #else
00061     tzset();
00062 #endif
00063     lt = localtime( &time );
00064     QDateTime dt;
00065     dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) );
00066     dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) );
00067 
00068     if (!m_tz.isEmpty() ) {
00069         unsetenv("TZ");
00070         if (!real_TZ.isEmpty() )
00071             setenv("TZ",  real_TZ.local8Bit(), true );
00072     }
00073     kdDebug(5229) << "DateTime is " << dt.toString() << endl;
00074     // done
00075     return dt;
00076 }
00077 time_t Base::toUTC( const QDateTime& dt )
00078 {
00079     time_t tmp;
00080     struct tm *lt;
00081 
00082     /* getenv can be NULL */
00083     char* ptrTz = getenv( "TZ");
00084     QString real_TZ = ptrTz ? QString::fromLocal8Bit( getenv("TZ") ) : QString::null;
00085 
00086     if ( !m_tz.isEmpty() )
00087         setenv( "TZ", m_tz.local8Bit(), true );
00088 
00089 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00090     _tzset();
00091 #else
00092     tzset();
00093 #endif
00094 
00095     // get a tm structure from the system to get the correct tz_name
00096     tmp = time( 0 );
00097     lt = localtime( &tmp );
00098 
00099     lt->tm_sec = dt.time().second();
00100     lt->tm_min = dt.time().minute();
00101     lt->tm_hour = dt.time().hour();
00102     lt->tm_mday = dt.date().day();
00103     lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12
00104     lt->tm_year = dt.date().year() - 1900; // year - 1900
00105     //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway
00106     //lt->tm_yday = dt.date().dayOfYear(); ignored anyway
00107     lt->tm_wday = -1;
00108     lt->tm_yday = -1;
00109     // tm_isdst negative -> mktime will find out about DST
00110     lt->tm_isdst = -1;
00111     // keep tm_zone and tm_gmtoff
00112     tmp = mktime( lt );
00113 
00114     if (!m_tz.isEmpty() ) {
00115         unsetenv("TZ");
00116         if (!real_TZ.isEmpty() )
00117             setenv("TZ",  real_TZ.local8Bit(), true );
00118     }
00119     return tmp;
00120 }
00121 bool Base::isMetaSyncingEnabled()const
00122 {
00123     return m_metaSyncing;
00124 }
00125 void Base::setMetaSyncingEnabled(bool meta )
00126 {
00127     m_metaSyncing = meta;
00128 }
00129 KTempFile* Base::file() {
00130     KTempFile* fi = new KTempFile( locateLocal("tmp",  "opie-konnector"),  "new");
00131     return fi;
00132 }
00133 QString Base::categoriesToNumber( const QStringList &list, const QString &app )
00134 {
00135     kdDebug(5226) << "categoriesToNumber " << list.join(";") << endl;
00136  startover:
00137     QStringList dummy;
00138     QValueList<OpieCategories>::ConstIterator catIt;
00139     QValueList<OpieCategories> categories = m_edit->categories();
00140     bool found = false;
00141     for ( QStringList::ConstIterator listIt = list.begin(); listIt != list.end(); ++listIt ) {
00142         /* skip empty category name */
00143         if ( (*listIt).isEmpty() ) continue;
00144 
00145         found  = false;
00146         for ( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
00147         /*
00148          * We currently do not take app into account
00149          * if name matches and the id isn't already in dummy we'll add it
00150          */
00151             if ( (*catIt).name() == (*listIt) && !dummy.contains(( *catIt).id() )  ) { // the same name
00152             kdDebug(5226) << "Found " << (*listIt) << endl;
00153                 found= true;
00154                 dummy << (*catIt).id();
00155             }
00156         }
00157         /* if not found and the category is not empty
00158          *
00159          * generate a new category and start over again
00160          * ugly goto to reiterate
00161          */
00162 
00163         if ( !found && !(*listIt).isEmpty() ){
00164             kdDebug(5226) << "Not Found category " << (*listIt) << endl;
00165             m_edit->addCategory( app, (*listIt) );  // generate a new category
00166             goto startover;
00167     }
00168     }
00169 
00170     return dummy.join(";");
00171 }
00172 QString Base::konnectorId( const QString &appName,  const QString &uid )
00173 {
00174     QString id;
00175     QString id2;
00176     // Konnector-.length() ==  10
00177     if ( uid.startsWith( "Konnector-" ) ) { // not converted
00178         id2 =  uid.mid( 10 );
00179     }else if ( m_helper) {
00180         id =  m_helper->konnectorId( appName,  uid );
00181         //                        konnector kde
00182         if (id.isEmpty() ) { // generate new id
00183             id2 = QString::number( newId() );
00184             id = QString::fromLatin1("Konnector-") + id2;
00185         }else if ( id.startsWith( "Konnector-" ) ) { // not converted
00186             id2 =  id.mid( 10 );
00187         }
00188         m_kde2opie.append( Kontainer( id,     uid ) );
00189     }
00190     return id2;
00191 }
00192 /*
00193  * IntelliSync(tm) is completely broken in regards to assigning UID's
00194  * it's always assigning the 0. So for us to work properly we need to rely
00195  * on uids!
00196  * We'll see if it equals '0' and then prolly assign a new uid
00197  */
00198 QString Base::kdeId( const QString &appName,  const QString &_uid )
00199 {
00200     QString uid = _uid;
00201     if (_uid.stripWhiteSpace() == QString::fromLatin1("0") ) {
00202         kdDebug() << "broken uid found!!! reassigning" << endl;
00203         uid = QString::number( newId() );
00204     }
00205 
00206     QString ret;
00207     if ( !m_helper )
00208         ret = QString::fromLatin1("Konnector-")  + uid;
00209 
00210     else // only if meta
00211         ret = m_helper->kdeId( appName, "Konnector-"+uid,  "Konnector-"+uid);
00212 
00213     return ret;
00214 }
00215 // code copyrighted by tt FIXME
00216 // GPL from Qtopia
00217 int Base::newId()
00218 {
00219     static QMap<int,  bool> ids;
00220     int id = -1 * (int) ::time(NULL );
00221     while ( ids.contains( id ) ){
00222         id += -1;
00223         if ( id > 0 )
00224             id = -1;
00225     }
00226     ids.insert( id, true );
00227     return id;
00228 }
00229 const Device* Base::device() {
00230     return m_device;
00231 }
00232 
00233 // FROM TT QStyleSheet and StringUtil it's GPLed
00234 // we also need to escape '\"' for our xml files
00235 QString OpieHelper::escape( const QString& plain ) {
00236     QString rich;
00237 
00238     for ( int i = 0; i < int(plain.length()); ++i ) {
00239     if ( plain[i] == '<' )
00240         rich +="&lt;";
00241     else if ( plain[i] == '>' )
00242         rich +="&gt;";
00243     else if ( plain[i] == '&' )
00244         rich +="&amp;";
00245         else if ( plain[i] == '\"' )
00246             rich += "&quot;";
00247     else
00248         rich += plain[i];
00249     }
00250     return rich;
00251 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu May 3 20:20:50 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003