kitchensync

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., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <config.h>
00023 
00024 #include <kstandarddirs.h>
00025 
00026 #include "helper.h"
00027 
00028 using namespace OpieHelper;
00029 
00030 Base::Base( CategoryEdit* edit,
00031             KSync::KonnectorUIDHelper* helper,
00032             const QString &tz,
00033             Device* dev )
00034 {
00035     m_edit = edit;
00036     m_helper = helper;
00037     m_tz = tz;
00038     m_device = dev;
00039 }
00040 Base::~Base()
00041 {
00042 
00043 }
00044 QDateTime Base::fromUTC( time_t time )
00045 {
00046    struct tm *lt;
00047 
00048    /* getenv can be NULL */
00049    char* ptrTz = getenv( "TZ");
00050    QString real_TZ = ptrTz ? QString::fromLocal8Bit( ptrTz ) : QString::null;
00051 
00052    if (!m_tz.isEmpty() )
00053        setenv( "TZ", m_tz.local8Bit(), true );
00054 
00055 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00056     _tzset();
00057 #else
00058     tzset();
00059 #endif
00060     lt = localtime( &time );
00061     QDateTime dt;
00062     dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) );
00063     dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) );
00064 
00065     if (!m_tz.isEmpty() ) {
00066         unsetenv("TZ");
00067         if (!real_TZ.isEmpty() )
00068             setenv("TZ",  real_TZ.local8Bit(), true );
00069     }
00070     // done
00071     return dt;
00072 }
00073 time_t Base::toUTC( const QDateTime& dt )
00074 {
00075     time_t tmp;
00076     struct tm *lt;
00077 
00078     /* getenv can be NULL */
00079     char* ptrTz = getenv( "TZ");
00080     QString real_TZ = ptrTz ? QString::fromLocal8Bit( getenv("TZ") ) : QString::null;
00081 
00082     if ( !m_tz.isEmpty() )
00083         setenv( "TZ", m_tz.local8Bit(), true );
00084 
00085 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00086     _tzset();
00087 #else
00088     tzset();
00089 #endif
00090 
00091     // get a tm structure from the system to get the correct tz_name
00092     tmp = time( 0 );
00093     lt = localtime( &tmp );
00094 
00095     lt->tm_sec = dt.time().second();
00096     lt->tm_min = dt.time().minute();
00097     lt->tm_hour = dt.time().hour();
00098     lt->tm_mday = dt.date().day();
00099     lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12
00100     lt->tm_year = dt.date().year() - 1900; // year - 1900
00101     //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway
00102     //lt->tm_yday = dt.date().dayOfYear(); ignored anyway
00103     lt->tm_wday = -1;
00104     lt->tm_yday = -1;
00105     // tm_isdst negative -> mktime will find out about DST
00106     lt->tm_isdst = -1;
00107     // keep tm_zone and tm_gmtoff
00108     tmp = mktime( lt );
00109 
00110     if (!m_tz.isEmpty() ) {
00111         unsetenv("TZ");
00112         if (!real_TZ.isEmpty() )
00113             setenv("TZ",  real_TZ.local8Bit(), true );
00114     }
00115     return tmp;
00116 }
00117 
00118 
00119 KTempFile* Base::file() {
00120     KTempFile* fi = new KTempFile( locateLocal("tmp",  "opie-konnector"),  "new");
00121     return fi;
00122 }
00123 QString Base::categoriesToNumber( const QStringList &list, const QString &app )
00124 {
00125  startover:
00126     QStringList dummy;
00127     QValueList<OpieCategories>::ConstIterator catIt;
00128     QValueList<OpieCategories> categories = m_edit->categories();
00129     bool found = false;
00130     for ( QStringList::ConstIterator listIt = list.begin(); listIt != list.end(); ++listIt ) {
00131         /* skip empty category name */
00132         if ( (*listIt).isEmpty() ) continue;
00133 
00134         found  = false;
00135         for ( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
00136         /*
00137          * We currently do not take app into account
00138          * if name matches and the id isn't already in dummy we'll add it
00139          */
00140             if ( (*catIt).name() == (*listIt) && !dummy.contains(( *catIt).id() )  ) { // the same name
00141                 found= true;
00142                 dummy << (*catIt).id();
00143             }
00144         }
00145         /* if not found and the category is not empty
00146          *
00147          * generate a new category and start over again
00148          * ugly goto to reiterate
00149          */
00150 
00151         if ( !found && !(*listIt).isEmpty() ){
00152             m_edit->addCategory( app, (*listIt) );  // generate a new category
00153             goto startover;
00154     }
00155     }
00156 
00157     return dummy.join(";");
00158 }
00159 QString Base::konnectorId( const QString &appName,  const QString &uid )
00160 {
00161     QString id;
00162     QString id2;
00163     // Konnector-.length() ==  10
00164     if ( uid.startsWith( "Konnector-" ) ) { // not converted
00165         id2 =  uid.mid( 10 );
00166     }else if ( m_helper) {
00167         id =  m_helper->konnectorId( appName,  uid );
00168         //                        konnector kde
00169         if (id.isEmpty() ) { // generate new id
00170             id2 = QString::number( newId() );
00171             id = QString::fromLatin1("Konnector-") + id2;
00172         }else if ( id.startsWith( "Konnector-" ) ) { // not converted
00173             id2 =  id.mid( 10 );
00174         }
00175         m_kde2opie.append( Kontainer( id,     uid ) );
00176     }
00177     return id2;
00178 }
00179 /*
00180  * IntelliSync(tm) is completely broken in regards to assigning UID's
00181  * it's always assigning the 0. So for us to work properly we need to rely
00182  * on uids!
00183  * We'll see if it equals '0' and then prolly assign a new uid
00184  */
00185 QString Base::kdeId( const QString &appName,  const QString &_uid )
00186 {
00187     QString uid = _uid;
00188     if (_uid.stripWhiteSpace() == QString::fromLatin1("0") ) {
00189         uid = QString::number( newId() );
00190     }
00191 
00192     QString ret;
00193     if ( !m_helper )
00194         ret = QString::fromLatin1("Konnector-")  + uid;
00195 
00196     else // only if meta
00197         ret = m_helper->kdeId( appName, "Konnector-"+uid,  "Konnector-"+uid);
00198 
00199     return ret;
00200 }
00201 // code copyrighted by tt FIXME
00202 // GPL from Qtopia
00203 int Base::newId()
00204 {
00205     static QMap<int,  bool> ids;
00206     int id = -1 * (int) ::time(NULL );
00207     while ( ids.contains( id ) ){
00208         id += -1;
00209         if ( id > 0 )
00210             id = -1;
00211     }
00212     ids.insert( id, true );
00213     return id;
00214 }
00215 const Device* Base::device() {
00216     return m_device;
00217 }
00218 
00224 QString Base::appendText( const QString& append, const QString& def )
00225 {
00226   if ( append != def )
00227     return append;
00228 
00229   return QString::null;
00230 }
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 {
00237   QString rich;
00238 
00239   for ( int i = 0; i < int(plain.length()); ++i ) {
00240     if ( plain[i] == '<' )
00241       rich +="&lt;";
00242     else if ( plain[i] == '>' )
00243       rich +="&gt;";
00244     else if ( plain[i] == '&' )
00245       rich +="&amp;";
00246     else if ( plain[i] == '\"' )
00247       rich += "&quot;";
00248     else
00249       rich += plain[i];
00250   }
00251 
00252   return rich;
00253 }
KDE Home | KDE Accessibility Home | Description of Access Keys