summaryrefslogtreecommitdiffstats
path: root/kresources/slox/sloxaccounts.cpp
blob: a32df5fa28068d4e756399a54739e7e046be3a94 (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
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
/*
    This file is part of kdepim.

    Copyright (c) 2004 Cornelius Schumacher <[email protected]>
    Copyright (c) 2005 Volker Krause <[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 "sloxaccounts.h"
#include "sloxbase.h"
#include "webdavhandler.h"

#include <libkcal/freebusyurlstore.h>

#include <kstaticdeleter.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <kio/job.h>
#include <kio/davjob.h>
#include <kstringhandler.h>
#include <kconfig.h>

#include <tqfile.h>
#include <tqdom.h>
#include <tqstring.h>

SloxAccounts::SloxAccounts( SloxBase *res, const KURL &baseUrl )
  : mBaseUrl( baseUrl ), mRes( res )
{
  kdDebug() << "SloxAccounts(): " << baseUrl << endl;

  mDownloadJob = 0;

  TQString server = mBaseUrl.host();

  TQStringList l = TQStringList::split( '.', server );

  if ( l.count() < 2 ) mDomain = server;
  else mDomain = l[ l.count() - 2 ] + "." + l[ l.count() - 1 ];

  readAccounts();
}

SloxAccounts::~SloxAccounts()
{
  kdDebug() << "~SloxAccounts()" << endl;

  if ( mDownloadJob ) mDownloadJob->kill();
}

void SloxAccounts::insertUser( const TQString &id, const KABC::Addressee &a )
{
  kdDebug() << "SloxAccount::insertUser() " << id << endl;

  mUsers.replace( id, a );

  TQString email = a.preferredEmail();

  TQString url = "http://" + mBaseUrl.host() + "/servlet/webdav.freebusy?username=";
  url += id + "&server=" + mDomain;

  KCal::FreeBusyUrlStore::self()->writeUrl( email, url );
}

KABC::Addressee SloxAccounts::lookupUser( const TQString &id )
{
  TQMap<TQString, KABC::Addressee>::ConstIterator it;
  it = mUsers.find( id );
  if ( it == mUsers.end() ) {
    requestAccounts();
    return KABC::Addressee();
  } else {
    return *it;
  }
}

TQString SloxAccounts::lookupId( const TQString &email )
{
  kdDebug() << "SloxAccounts::lookupId() " << email << endl;

  TQMap<TQString, KABC::Addressee>::ConstIterator it;
  for( it = mUsers.begin(); it != mUsers.end(); ++it ) {
    kdDebug() << "PREF: " << (*it).preferredEmail() << endl;
    kdDebug() << "KEY: " << it.key() << endl;
    if ( (*it).preferredEmail() == email ) return it.key();
  }
  requestAccounts();

  int pos = email.find( '@' );
  if ( pos < 0 ) return email;
  else return email.left( pos );
}

void SloxAccounts::requestAccounts()
{
  kdDebug() << "SloxAccounts::requestAccounts()" << endl;

  if ( mDownloadJob ) {
    kdDebug() << "SloxAccount::requestAccounts(): Download still in progress"
              << endl;
    return;
  }

  if ( mRes->resType() == "slox" ) {
    KURL url = mBaseUrl;
    url.addPath( "/servlet/webdav.groupuser" );
    url.setQuery( "?user=*&group=*&groupres=*&res=*&details=t" );

    kdDebug() << "SloxAccounts::requestAccounts() URL: " << url << endl;

    mDownloadJob = KIO::file_copy( url, cacheFile(), -1, true, false, false );
  } else if ( mRes->resType() == "ox" ) {
    KURL url = mBaseUrl;
    url.setPath( "/servlet/webdav.groupuser/" );

    TQDomDocument doc;
    TQDomElement root = WebdavHandler::addDavElement( doc, doc, "propfind" );
    TQDomElement prop = WebdavHandler::addDavElement( doc, root, "prop" );
    WebdavHandler::addSloxElement( mRes, doc, prop, "user", "*" );
    WebdavHandler::addSloxElement( mRes, doc, prop, "group", "*" );
    WebdavHandler::addSloxElement( mRes, doc, prop, "resource", "*" );
    WebdavHandler::addSloxElement( mRes, doc, prop, "resourcegroup", "*" );

    kdDebug() << k_funcinfo << doc.toString( 2 ) << endl;

    mDownloadJob = KIO::davPropFind( url, doc, "0", false );
  }

  connect( mDownloadJob, TQT_SIGNAL( result( KIO::Job * ) ),
           TQT_SLOT( slotResult( KIO::Job * ) ) );
}

void SloxAccounts::slotResult( KIO::Job *job )
{
  kdDebug() << "SloxAccounts::slotResult()" << endl;

  if ( job->error() ) {
    job->showErrorDialog( 0 );
  } else {
    if ( mRes->resType() == "ox" ) {
      TQFile f( cacheFile() );
      if ( !f.open( IO_WriteOnly ) ) {
        kdWarning() << "Unable to open '" << cacheFile() << "'" << endl;
        return;
      }
      TQTextStream stream ( &f );
      stream << static_cast<KIO::DavJob*>( mDownloadJob )->response();
      f.close();
    }
    readAccounts();
  }

  mDownloadJob = 0;
}

TQString SloxAccounts::cacheFile() const
{
  TQString host = mBaseUrl.host();

  TQString file = locateLocal( "cache", "slox/accounts_" + host );

  kdDebug() << "SloxAccounts::cacheFile(): " << file << endl;

  return file;
}

void SloxAccounts::readAccounts()
{
  kdDebug() << "SloxAccounts::readAccounts()" << endl;

  TQFile f( cacheFile() );
  if ( !f.open( IO_ReadOnly ) ) {
    kdDebug() << "Unable to open '" << cacheFile() << "'" << endl;
    requestAccounts();
    return;
  }

  TQDomDocument doc;
  doc.setContent( &f );

//  kdDebug() << "SLOX ACCOUNTS: " << doc.toString( 2 ) << endl;

  TQDomElement docElement = doc.documentElement();

  mUsers.clear();

  TQDomNodeList nodes = doc.elementsByTagName( mRes->resType() == "ox" ? "ox:user" : "user" );
  for( uint i = 0; i < nodes.count(); ++i ) {
    TQDomElement element = nodes.item(i).toElement();
    TQString id;
    KABC::Addressee a;
    TQDomNode n;
    for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
      TQDomElement e = n.toElement();
      TQString tag = e.tagName();
      // remove XML namespace
      tag = tag.right( tag.length() - ( tag.find( ':' ) + 1 ) );
      TQString value = e.text();
      if ( tag == "uid" ) id = value;
      else if ( tag == "mail" ) a.insertEmail( value, true );
      else if ( tag == "forename" ) a.setGivenName( value );
      else if ( tag == "surename" ) a.setFamilyName( value );
    }
//     kdDebug() << "MAIL: " << a.preferredEmail() << endl;
    insertUser( id, a );
  }
}

#include "sloxaccounts.moc"