1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
This file is part of kdepim.
Copyright (c) 2004 Cornelius Schumacher <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "webdavhandler.h"
#include "sloxbase.h"
#ifdef HAVE_VALUES_H
#include <values.h>
#else
#ifdef HAVE_SYS_LIMITS_H
#include <sys/limits.h>
#endif
#endif
#include <config.h>
#include <stdlib.h>
#include <libkcal/incidence.h>
#include <libtdepim/kpimprefs.h>
#include <kdebug.h>
#include <kconfig.h>
#include <tqfile.h>
SloxItem::SloxItem()
: status( Invalid )
{
}
WebdavHandler::WebdavHandler()
: mLogCount( 0 )
{
KConfig cfg( "sloxrc" );
cfg.setGroup( "General" );
mLogFile = cfg.readEntry( "LogFile" );
kdDebug() << "LOG FILE: " << mLogFile << endl;
}
void WebdavHandler::setUserId( const TQString &id )
{
mUserId = id;
}
TQString WebdavHandler::userId() const
{
return mUserId;
}
void WebdavHandler::log( const TQString &text )
{
if ( mLogFile.isEmpty() ) return;
TQString filename = mLogFile + "-" + TQString::number( mLogCount );
TQFile file( filename );
if ( !file.open( IO_WriteOnly ) ) {
kdWarning() << "Unable to open log file '" << filename << "'" << endl;
return;
}
TQCString textUtf8 = text.utf8();
file.writeBlock( textUtf8.data(), textUtf8.size() - 1 );
if ( ++mLogCount > 5 ) mLogCount = 0;
}
TQValueList<SloxItem> WebdavHandler::getSloxItems( SloxBase *res, const TQDomDocument &doc )
{
kdDebug() << "getSloxItems" << endl;
TQValueList<SloxItem> items;
TQDomElement docElement = doc.documentElement();
TQDomNode responseNode;
for( responseNode = docElement.firstChild(); !responseNode.isNull();
responseNode = responseNode.nextSibling() ) {
TQDomElement responseElement = responseNode.toElement();
if ( responseElement.tagName() == "response" ) {
SloxItem item;
TQDomNode propstat = responseElement.namedItem( "propstat" );
if ( propstat.isNull() ) {
kdError() << "Unable to find propstat tag." << endl;
continue;
}
TQDomNode prop = propstat.namedItem( "prop" );
if ( prop.isNull() ) {
kdError() << "Unable to find WebDAV property" << endl;
continue;
}
item.domNode = prop;
TQDomNode sloxIdNode = prop.namedItem( res->fieldName( SloxBase::ObjectId ) );
if ( sloxIdNode.isNull() ) {
kdError() << "Unable to find SLOX id." << endl;
continue;
}
TQDomElement sloxIdElement = sloxIdNode.toElement();
item.sloxId = sloxIdElement.text();
TQDomNode clientIdNode = prop.namedItem( res->fieldName( SloxBase::ClientId ) );
if ( !clientIdNode.isNull() ) {
TQDomElement clientIdElement = clientIdNode.toElement();
item.clientId = clientIdElement.text();
if ( item.clientId != item.sloxId )
item.status = SloxItem::New;
}
TQDomNode sloxtqStatus = prop.namedItem( res->fieldName( SloxBase::ObjectqStatus ) );
if ( !sloxtqStatus.isNull() ) {
TQDomElement sloxStatusElement = sloxtqStatus.toElement();
if ( sloxStatusElement.text() == "DELETE" ) {
item.status = SloxItem::Delete;
} else if ( sloxStatusElement.text() == "CREATE" ) {
item.status = SloxItem::Create;
}
}
TQDomNode status = propstat.namedItem( "status" );
if ( status.isNull() ) {
kdError() << "Unable to find WebDAV status" << endl;
continue;
}
item.response = status.toElement().text();
TQDomNode desc = propstat.namedItem( "responsedescription" );
if ( desc.isNull() ) {
kdError() << "Unable to find WebDAV responsedescription" << endl;
continue;
}
item.responseDescription = desc.toElement().text();
items.append( item );
}
}
return items;
}
TQString WebdavHandler::qDateTimeToSlox( const TQDateTime &dt )
{
uint ticks = -dt.secsTo( TQDateTime( TQDate( 1970, 1, 1 ), TQTime( 0, 0 ) ) );
return TQString::number( ticks ) + "000";
}
TQString WebdavHandler::qDateTimeToSlox( const TQDateTime &dt,
const TQString &timeZoneId )
{
TQDateTime utc = KPimPrefs::localTimeToUtc( dt, timeZoneId );
// secsTo and toTime_t etc also perform a timezone conversion using the system timezone,
// but we want to use the calendar timezone, so we have to convert ourself and spoof the tz to UTC before
// converting to ticks to prevent this
TQCString origTz = getenv("TZ");
setenv( "TZ", "UTC", 1 );
uint ticks = utc.toTime_t();
if ( origTz.isNull() )
unsetenv( "TZ" );
else
setenv( "TZ", origTz, 1 );
return TQString::number( ticks ) + "000";
}
TQDateTime WebdavHandler::sloxToTQDateTime( const TQString &str )
{
TQString s = str.mid( 0, str.length() - 3 );
bool preEpoch = s.startsWith("-");
if (preEpoch)
s = s.mid(1);
unsigned long ticks = s.toULong();
TQDateTime dt;
if (preEpoch) {
dt.setTime_t( 0, Qt::UTC );
if (ticks > INT_MAX) {
dt = dt.addSecs(-INT_MAX);
ticks -= INT_MAX;
}
dt = dt.addSecs(-((long) ticks));
}
else
{
dt.setTime_t( ticks, Qt::UTC );
}
return dt;
}
TQDateTime WebdavHandler::sloxToTQDateTime( const TQString &str,
const TQString &timeZoneId )
{
return KPimPrefs::utcToLocalTime( sloxToTQDateTime(str), timeZoneId );
}
TQDomElement WebdavHandler::addElement( TQDomDocument &doc, TQDomNode &node,
const TQString &tag )
{
TQDomElement el = doc.createElement( tag );
node.appendChild( el );
return el;
}
TQDomElement WebdavHandler::addDavElement( TQDomDocument &doc, TQDomNode &node,
const TQString &tag )
{
TQDomElement el = doc.createElementNS( "DAV:", "D:" + tag );
node.appendChild( el );
return el;
}
TQDomElement WebdavHandler::addSloxElement( SloxBase *res,
TQDomDocument &doc, TQDomNode &node,
const TQString &tag,
const TQString &text )
{
TQDomElement el;
if ( res->resType() == "ox" )
el = doc.createElementNS( "http://www.open-xchange.org", "ox:" + tag );
else
el = doc.createElementNS( "SLOX", "S:" + tag );
if ( !text.isEmpty() ) {
TQDomText textnode = doc.createTextNode( text );
el.appendChild( textnode );
}
node.appendChild( el );
return el;
}
void WebdavHandler::parseSloxAttribute( const TQDomElement &e )
{
// kdDebug() << "parseSloxAttribute" << endl;
TQString tag = e.tagName();
TQString text = TQString::fromUtf8( e.text().latin1() );
if ( text.isEmpty() ) return;
if ( tag == "owner" ) {
if ( text == mUserId ) mWritable = true;
} else if ( tag == "writerights" ) {
TQDomNode n;
for( n = e.firstChild(); !n.isNull(); n = n.nextSibling() ) {
TQDomElement e2 = n.toElement();
if ( e2.tagName() == "member" ) {
if ( e2.text() == mUserId ) mWritable = true;
}
// TODO: Process group write rights
}
}
}
void WebdavHandler::clearSloxAttributetqStatus()
{
if ( mRes->resType() == "ox" )
mWritable = true; // parseSloxAttribute() won't work for OX
else
mWritable = false;
}
void WebdavHandler::setSloxAttributes( KCal::Incidence *i )
{
i->setReadOnly( !mWritable );
}
void WebdavHandler::setSloxAttributes( KABC::Addressee & )
{
// FIXME: libkabc doesn't allow to set an individual addressee to read-only
}
|