summaryrefslogtreecommitdiffstats
path: root/kresources/lib/groupwaredataadaptor.cpp
blob: da012b1581be12288c76f840c5246e5f222baa26 (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
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
/*
    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 library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "groupwaredataadaptor.h"
#include <kdebug.h>
#include <kio/job.h>
#include <libemailfunctions/idmapper.h>

using namespace KPIM;

GroupwareUploadItem::GroupwareUploadItem( UploadType type ) : mItemType( KPIM::FolderLister::Unknown ), mType( type )
{
}

KURL GroupwareUploadItem::adaptNewItemUrl( GroupwareDataAdaptor *adaptor,
                                           const KURL &baseurl )
{
kdDebug()<<"GroupwareUploadItem::adaptNewItemUrl, baseurl=" << baseurl.url() << endl;
  if ( adaptor ) {
    TQString path( adaptor->defaultNewItemName( this ) );
kdDebug() << "path=" << path << endl;
    KURL u( baseurl );
    if ( path.isEmpty() ) return u;
    else {
      u.addPath( path );
kdDebug() << "Final Path for new item: " << u.url() << endl;
      return u;
    }
  } else return baseurl;
}

KIO::TransferJob *GroupwareUploadItem::createRawUploadJob(
                       GroupwareDataAdaptor *adaptor, const KURL &/*baseurl*/ )
{
  Q_ASSERT( adaptor );
  if ( !adaptor ) return 0;
  const TQString dta = data();
  //kdDebug(7000) << "Uploading: " << data << endl;
  KURL upUrl( url() );
  if ( adaptor )
    adaptor->adaptUploadUrl( upUrl );
  kdDebug(7000) << "Uploading to: " << upUrl.prettyURL() << endl;
  KIO::TransferJob *job = KIO::storedPut( dta.utf8(), upUrl, -1, true,
                                          false, false );
  job->addMetaData( "PropagateHttpHeader", "true" );
  if ( adaptor ) {
    job->addMetaData( "customHTTPHeader", "Content-Type: " + adaptor->mimeType() );
  }
  return job;
}

KIO::TransferJob *GroupwareUploadItem::createUploadNewJob(
      GroupwareDataAdaptor *adaptor, const KURL &baseurl )
{
kdDebug()<<"GroupwareUploadItem::createUploadNewJob, baseurl=" << baseurl.url() << endl;
  setUrl( adaptNewItemUrl( adaptor, baseurl ) );
  KIO::TransferJob *job = createRawUploadJob( adaptor, baseurl );
  if ( job ) {
    kdDebug() << "Adding If-None-Match " << endl;
    TQString header;
    if ( job->outgoingMetaData().contains("customHTTPHeader") ) {
      header = job->outgoingMetaData()["customHTTPHeader"];
      header += "\r\n";
    }
    header += "If-None-Match: *";
    job->addMetaData( "customHTTPHeader", header );
  }
  return job;
}

KIO::TransferJob *GroupwareUploadItem::createUploadJob(
                           GroupwareDataAdaptor *adaptor, const KURL &baseurl )
{
kdDebug()<<"GroupwareUploadItem::createUploadJob" << endl;
  KIO::TransferJob *job = createRawUploadJob( adaptor, baseurl );
  if ( job && adaptor ) {
  kdDebug()<<"Adding If-Match header: " << adaptor->idMapper()->fingerprint( uid() ) << endl;
    TQString header;
    if ( job->outgoingMetaData().contains("customHTTPHeader") ) {
      header = job->outgoingMetaData()["customHTTPHeader"];
      header += "\r\n";
    }
kdDebug()<<"old HEADER: " << header << endl;
    header += "If-Match: " + adaptor->idMapper()->fingerprint( uid() );
kdDebug()<<"new HEADER: " << header << endl;
    job->addMetaData( "customHTTPHeader", header );
  }
  return job;
}


GroupwareDataAdaptor::GroupwareDataAdaptor()
  : TQObject(), mFolderLister( 0 ), mIdMapper( 0 )
{
}

GroupwareDataAdaptor::~GroupwareDataAdaptor()
{
}

void GroupwareDataAdaptor::setUserPassword( KURL &url )
{
  kdDebug(5800) << "GroupwareDataAdaptor::setUserPassword, mUser="
                << mUser << endl;
  url.setUser( mUser );
  url.setPass( mPassword );
}

FolderLister::Entry::List GroupwareDataAdaptor::defaultFolders()
{
  return FolderLister::Entry::List();
}

KIO::TransferJob *GroupwareDataAdaptor::createUploadJob( const KURL &url,
                                                     GroupwareUploadItem *item )
{
  if ( item ) {
    KIO::TransferJob *job = item->createUploadJob( this, url );
    setUidForJob( job, item->uid() );
    return job;
  } else return 0;
}

KIO::TransferJob *GroupwareDataAdaptor::createUploadNewJob( const KURL &url,
                                                     GroupwareUploadItem *item )
{
kdDebug()<<"GroupwareDataAdaptor::createUploadNewJob, url=" << url.url() << endl;
  if ( item ) {
    KIO::TransferJob *job = item->createUploadNewJob( this, url );
    setUidForJob( job, item->uid() );
    return job;
  } else return 0;
}

void GroupwareDataAdaptor::processDownloadListItem( const KURL &entry,
        const TQString &newFingerprint, KPIM::FolderLister::ContentType type )
{
  bool download = false;
  const TQString &location = entry.path();

  emit itemOnServer( entry );
  // if not locally present, download
  const TQString &localId = idMapper()->localId( location );
  kdDebug(5800) << "Looking up remote: " << location
                << " found: " << localId << endl;
  if ( localId.isEmpty() || !localItemExists( localId ) ) {
    //kdDebug(7000) << "Not locally present, download: " << location << endl;
    download = true;
  } else {
    kdDebug(5800) << "Locally present " << endl;
    // locally present, let's check if it's newer than what we have
    const TQString &oldFingerprint = idMapper()->fingerprint( localId );
      if ( oldFingerprint != newFingerprint ) {
      kdDebug(5800) << "Fingerprint changed old: " << oldFingerprint <<
        " new: " << newFingerprint << endl;
      // something changed on the server, check if we also changed it locally
      if ( localItemHasChanged( localId ) ) {
        // TODO conflict resolution
        kdDebug(5800) << "TODO conflict resolution" << endl;
        download = true;
      } else {
        download = true;
      }
    } else {
      kdDebug(5800) << "Fingerprint not changed, don't download this " << endl;
    }
  }
  if ( download ) {
    emit itemToDownload( entry, type );
  }
}

bool GroupwareDataAdaptor::interpretRemoveJob( KIO::Job *job, const TQString &/*jobData*/ )
{
  if ( !job ) return false;
  KIO::DeleteJob *deljob = dynamic_cast<KIO::DeleteJob*>(job);
  bool error = job->error();
  const TQString err = job->errorString();

  if ( deljob ) {
    KURL::List urls( deljob->urls() );
    for ( KURL::List::Iterator it = urls.begin(); it != urls.end(); ++it ) {
      if ( error ) {
        emit itemDeletionError( *it, err );
      } else {
        // FIXME: Don't use TQString() here
        emit itemDeleted( TQString(), *it );
      }
    }
    return true;
  } else {
    return false;
  }
}


bool GroupwareDataAdaptor::interpretUploadJob( KIO::Job *job, const TQString &/*jobData*/ )
{
  kdDebug(7000) << "GroupwareDataAdaptor::interpretUploadJob " << endl;
  KIO::TransferJob *trfjob = dynamic_cast<KIO::TransferJob*>(job);
  bool error = job->error();
  const TQString err = job->errorString();

  if ( trfjob ) {
    KURL url( trfjob->url() );
    if ( error ) {
      emit itemUploadError( url, err );
    } else {
      // We don't know the local id here (and we don't want to extract it from
      // the idMapper, that's the task of the receiver
      emit itemUploaded( uidFromJob( job ), url );
    }
    return true;
  } else {
    return false;
  }
}

bool GroupwareDataAdaptor::interpretUploadNewJob( KIO::Job *job, const TQString &/*jobData*/ )
{
// TODO: How does the incidence mapper know the old/new ids???
  kdDebug(7000) << "GroupwareDataAdaptor::interpretUploadNewJob " << endl;
  KIO::TransferJob *trfjob = dynamic_cast<KIO::TransferJob*>(job);
  bool error = job->error();
  const TQString err = job->errorString();

  if ( trfjob ) {
    KURL url( trfjob->url() );
    if ( error ) {
      emit itemUploadNewError( idMapper()->localId( url.path() ), err );
    } else {
      // We don't know the local id here (and we don't want to extract it from
      // the idMapper, that's the task of the receiver
      emit itemUploadedNew( uidFromJob( job ), url );
    }
    return true;
  } else {
    return false;
  }
}

TQString GroupwareDataAdaptor::uidFromJob( KIO::Job *job ) const
{
kdDebug()<<"GroupwareDataAdaptor::uidFromJob( "<<job<<")"<<endl;
  if ( mJobUIDMap.contains( job ) ) {
    kdDebug()<<"  Contained: "<< mJobUIDMap[job] << endl;
    return mJobUIDMap[ job ];
  } else {
    return TQString();
  }
}

void GroupwareDataAdaptor::setUidForJob( KIO::Job *job, const TQString &uid )
{
  if ( uid.isEmpty() ) {
    mJobUIDMap.remove( job );
  } else {
    mJobUIDMap[ job ] = uid;
  }
}


#include "groupwaredataadaptor.moc"