00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00075
return dt;
00076 }
00077 time_t Base::toUTC(
const QDateTime& dt )
00078 {
00079 time_t tmp;
00080
struct tm *lt;
00081
00082
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
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;
00104 lt->tm_year = dt.date().year() - 1900;
00105
00106
00107 lt->tm_wday = -1;
00108 lt->tm_yday = -1;
00109
00110 lt->tm_isdst = -1;
00111
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
00143
if ( (*listIt).isEmpty() )
continue;
00144
00145 found =
false;
00146
for ( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
00147
00148
00149
00150
00151
if ( (*catIt).name() == (*listIt) && !dummy.contains(( *catIt).id() ) ) {
00152 kdDebug(5226) <<
"Found " << (*listIt) << endl;
00153 found=
true;
00154 dummy << (*catIt).id();
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
if ( !found && !(*listIt).isEmpty() ){
00164 kdDebug(5226) <<
"Not Found category " << (*listIt) << endl;
00165 m_edit->addCategory( app, (*listIt) );
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
00177
if ( uid.startsWith(
"Konnector-" ) ) {
00178 id2 = uid.mid( 10 );
00179 }
else if ( m_helper) {
00180
id = m_helper->konnectorId( appName, uid );
00181
00182
if (
id.isEmpty() ) {
00183 id2 = QString::number( newId() );
00184
id = QString::fromLatin1(
"Konnector-") + id2;
00185 }
else if (
id.startsWith(
"Konnector-" ) ) {
00186 id2 =
id.mid( 10 );
00187 }
00188 m_kde2opie.append( Kontainer(
id, uid ) );
00189 }
00190
return id2;
00191 }
00192
00193
00194
00195
00196
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
00211 ret = m_helper->kdeId( appName,
"Konnector-"+uid,
"Konnector-"+uid);
00212
00213
return ret;
00214 }
00215
00216
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
00234
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 +=
"<";
00241
else if ( plain[i] ==
'>' )
00242 rich +=
">";
00243
else if ( plain[i] ==
'&' )
00244 rich +=
"&";
00245
else if ( plain[i] ==
'\"' )
00246 rich +=
""";
00247
else
00248 rich += plain[i];
00249 }
00250
return rich;
00251 }