summaryrefslogtreecommitdiffstats
path: root/tderesources/lib/webdavhandler.cpp
blob: fea7a0b1624a714c1217078a525d6ec0a9c68b82 (plain)
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
/*
    This file is part of tdepim.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2004 Till Adam <adam@kde.org>
    Copyright (c) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>

    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 <kdebug.h>

#include <tqdatetime.h>

extern "C" {
  #include <libical/ical.h>
}


#include "webdavhandler.h"

WebdavHandler::WebdavHandler()
{
}


KURL WebdavHandler::toDAV( const KURL& url )
{
  KURL result( url );

  if ( result.protocol() == "http" ) {
    if ( result.port() == 443 ) {
      // OpenGroupware.org returns 'http://server:443' instead of 'https://server'
      result.setProtocol( "webdavs" );
      result.setPort( 0 );
    } else
      result.setProtocol( "webdav" );
  } else if ( result.protocol() == "https" )
    result.setProtocol( "webdavs" );

  return result;
}



TQDomElement WebdavHandler::addElement( TQDomDocument &doc, TQDomNode &node,
                                       const TQString &tag, const TQString &value )
{
  TQDomElement el = doc.createElement( tag );
  node.appendChild( el );
  if ( !value.isNull() ) {
    TQDomText txt = doc.createTextNode( value );
    el.appendChild( txt );
  }
  return el;
}

TQDomElement WebdavHandler::addElementNS( TQDomDocument &doc, TQDomNode &node,
                                       const TQString &ns, const TQString &tag, const TQString &value )
{
  TQDomElement el = doc.createElementNS( ns, tag );
  node.appendChild( el );
  if ( !value.isNull() ) {
    TQDomText txt = doc.createTextNode( value );
    el.appendChild( txt );
  }
  return el;
}

TQDomElement WebdavHandler::addDavElement( TQDomDocument &doc, TQDomNode &node,
                                          const TQString &tag, const TQString &value )
{
  return addElementNS( doc, node, "DAV:", tag, value );
}

bool WebdavHandler::extractBool( const TQDomElement &node, const TQString &entry, bool &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() ) {
    value = (element.text() != "0");
    return true;
  }
  return false;
}

bool WebdavHandler::extractLong( const TQDomElement &node, const TQString &entry, long &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() ) {
    value = element.text().toLong();
    return true;
  }
  return false;
}

bool WebdavHandler::extractFloat( const TQDomElement &node, const TQString &entry, float &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() ) {
    value = element.text().toFloat();
    return true;
  }
  return false;
}

bool WebdavHandler::extractDateTime( const TQDomElement &node, const TQString &entry, TQDateTime &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() && !element.text().isEmpty() ) {
    value = TQDateTime::fromString( element.text(), Qt::ISODate );
    return true;
  }
  return false;
}

bool WebdavHandler::extractString( const TQDomElement &node, const TQString &entry, TQString &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() ) {
    value = element.text();
    return true;
  }
  return false;
}

bool WebdavHandler::extractStringList( const TQDomElement &node, const TQString &entry, TQStringList &value )
{
  TQDomElement element = node.namedItem( entry ).toElement();
  if ( !element.isNull() ) {
    value.clear();
    TQDomNodeList list = element.elementsByTagNameNS( "xml:", "v" );
    for( uint i=0; i < list.count(); i++ ) {
      TQDomElement item = list.item(i).toElement();
      value.append( item.text() );
    }
    return true;
  }
  return false;
}


const TQString WebdavHandler::getEtagFromHeaders( const TQString& headers )
{
  int start = headers.find( "etag:", 0, false );
  if ( start < 0 ) return TQString();
  start += 6;
  return headers.mid( start, headers.find( "\n", start ) - start );
}

//TODO: should not call libical functions directly -- better to make
//      a new libkcal abstraction method.
TQDateTime WebdavHandler::utcAsZone( const TQDateTime& utc, const TQString& timeZoneId )
{
  int daylight;
  TQDateTime epoch;
  epoch.setTime_t( 0 );
  time_t v = epoch.secsTo( utc );
  struct icaltimetype tt = 
      icaltime_from_timet_with_zone( v, 0 /*is_date*/,
         icaltimezone_get_builtin_timezone( "UTC" ) );
  int offset = icaltimezone_get_utc_offset(
    icaltimezone_get_builtin_timezone( timeZoneId.latin1() ),
    &tt, &daylight );
kdDebug() << "Calculated offset of: " << offset << " of timezone: " << timeZoneId << endl;
  return utc.addSecs( offset );
}

//TODO: should not call libical functions directly -- better to make
//      a new libkcal abstraction method.
TQDateTime WebdavHandler::zoneAsUtc( const TQDateTime& zone, const TQString& timeZoneId )
{
  int daylight;
  TQDateTime epoch;
  epoch.setTime_t( 0 );
  time_t v = epoch.secsTo( zone );
  struct icaltimetype tt = icaltime_from_timet( v, 0 ); // 0: is_date=false
  int offset = icaltimezone_get_utc_offset(
    icaltimezone_get_builtin_timezone( timeZoneId.latin1() ),
    &tt, &daylight );
  return zone.addSecs( - offset );
}