You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
9.1 KiB
284 lines
9.1 KiB
15 years ago
|
/*
|
||
13 years ago
|
This file is part of tdepim.
|
||
15 years ago
|
|
||
|
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 ) {
|
||
15 years ago
|
TQString path( adaptor->defaultNewItemName( this ) );
|
||
15 years ago
|
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;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEIO::TransferJob *GroupwareUploadItem::createRawUploadJob(
|
||
15 years ago
|
GroupwareDataAdaptor *adaptor, const KURL &/*baseurl*/ )
|
||
|
{
|
||
|
Q_ASSERT( adaptor );
|
||
|
if ( !adaptor ) return 0;
|
||
15 years ago
|
const TQString dta = data();
|
||
15 years ago
|
//kdDebug(7000) << "Uploading: " << data << endl;
|
||
|
KURL upUrl( url() );
|
||
|
if ( adaptor )
|
||
|
adaptor->adaptUploadUrl( upUrl );
|
||
|
kdDebug(7000) << "Uploading to: " << upUrl.prettyURL() << endl;
|
||
12 years ago
|
TDEIO::TransferJob *job = TDEIO::storedPut( dta.utf8(), upUrl, -1, true,
|
||
15 years ago
|
false, false );
|
||
|
job->addMetaData( "PropagateHttpHeader", "true" );
|
||
|
if ( adaptor ) {
|
||
|
job->addMetaData( "customHTTPHeader", "Content-Type: " + adaptor->mimeType() );
|
||
|
}
|
||
|
return job;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEIO::TransferJob *GroupwareUploadItem::createUploadNewJob(
|
||
15 years ago
|
GroupwareDataAdaptor *adaptor, const KURL &baseurl )
|
||
|
{
|
||
|
kdDebug()<<"GroupwareUploadItem::createUploadNewJob, baseurl=" << baseurl.url() << endl;
|
||
|
setUrl( adaptNewItemUrl( adaptor, baseurl ) );
|
||
12 years ago
|
TDEIO::TransferJob *job = createRawUploadJob( adaptor, baseurl );
|
||
15 years ago
|
if ( job ) {
|
||
|
kdDebug() << "Adding If-None-Match " << endl;
|
||
15 years ago
|
TQString header;
|
||
13 years ago
|
if ( job->outgoingMetaData().contains("customHTTPHeader") ) {
|
||
15 years ago
|
header = job->outgoingMetaData()["customHTTPHeader"];
|
||
|
header += "\r\n";
|
||
|
}
|
||
|
header += "If-None-Match: *";
|
||
|
job->addMetaData( "customHTTPHeader", header );
|
||
|
}
|
||
|
return job;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEIO::TransferJob *GroupwareUploadItem::createUploadJob(
|
||
15 years ago
|
GroupwareDataAdaptor *adaptor, const KURL &baseurl )
|
||
|
{
|
||
|
kdDebug()<<"GroupwareUploadItem::createUploadJob" << endl;
|
||
12 years ago
|
TDEIO::TransferJob *job = createRawUploadJob( adaptor, baseurl );
|
||
15 years ago
|
if ( job && adaptor ) {
|
||
|
kdDebug()<<"Adding If-Match header: " << adaptor->idMapper()->fingerprint( uid() ) << endl;
|
||
15 years ago
|
TQString header;
|
||
13 years ago
|
if ( job->outgoingMetaData().contains("customHTTPHeader") ) {
|
||
15 years ago
|
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()
|
||
15 years ago
|
: TQObject(), mFolderLister( 0 ), mIdMapper( 0 )
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEIO::TransferJob *GroupwareDataAdaptor::createUploadJob( const KURL &url,
|
||
15 years ago
|
GroupwareUploadItem *item )
|
||
|
{
|
||
|
if ( item ) {
|
||
12 years ago
|
TDEIO::TransferJob *job = item->createUploadJob( this, url );
|
||
15 years ago
|
setUidForJob( job, item->uid() );
|
||
|
return job;
|
||
|
} else return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEIO::TransferJob *GroupwareDataAdaptor::createUploadNewJob( const KURL &url,
|
||
15 years ago
|
GroupwareUploadItem *item )
|
||
|
{
|
||
|
kdDebug()<<"GroupwareDataAdaptor::createUploadNewJob, url=" << url.url() << endl;
|
||
|
if ( item ) {
|
||
12 years ago
|
TDEIO::TransferJob *job = item->createUploadNewJob( this, url );
|
||
15 years ago
|
setUidForJob( job, item->uid() );
|
||
|
return job;
|
||
|
} else return 0;
|
||
|
}
|
||
|
|
||
|
void GroupwareDataAdaptor::processDownloadListItem( const KURL &entry,
|
||
15 years ago
|
const TQString &newFingerprint, KPIM::FolderLister::ContentType type )
|
||
15 years ago
|
{
|
||
|
bool download = false;
|
||
15 years ago
|
const TQString &location = entry.path();
|
||
15 years ago
|
|
||
|
emit itemOnServer( entry );
|
||
|
// if not locally present, download
|
||
15 years ago
|
const TQString &localId = idMapper()->localId( location );
|
||
15 years ago
|
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
|
||
15 years ago
|
const TQString &oldFingerprint = idMapper()->fingerprint( localId );
|
||
15 years ago
|
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 );
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
bool GroupwareDataAdaptor::interpretRemoveJob( TDEIO::Job *job, const TQString &/*jobData*/ )
|
||
15 years ago
|
{
|
||
|
if ( !job ) return false;
|
||
12 years ago
|
TDEIO::DeleteJob *deljob = dynamic_cast<TDEIO::DeleteJob*>(job);
|
||
15 years ago
|
bool error = job->error();
|
||
15 years ago
|
const TQString err = job->errorString();
|
||
15 years ago
|
|
||
|
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 {
|
||
14 years ago
|
// FIXME: Don't use TQString() here
|
||
|
emit itemDeleted( TQString(), *it );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
12 years ago
|
bool GroupwareDataAdaptor::interpretUploadJob( TDEIO::Job *job, const TQString &/*jobData*/ )
|
||
15 years ago
|
{
|
||
|
kdDebug(7000) << "GroupwareDataAdaptor::interpretUploadJob " << endl;
|
||
12 years ago
|
TDEIO::TransferJob *trfjob = dynamic_cast<TDEIO::TransferJob*>(job);
|
||
15 years ago
|
bool error = job->error();
|
||
15 years ago
|
const TQString err = job->errorString();
|
||
15 years ago
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
bool GroupwareDataAdaptor::interpretUploadNewJob( TDEIO::Job *job, const TQString &/*jobData*/ )
|
||
15 years ago
|
{
|
||
|
// TODO: How does the incidence mapper know the old/new ids???
|
||
|
kdDebug(7000) << "GroupwareDataAdaptor::interpretUploadNewJob " << endl;
|
||
12 years ago
|
TDEIO::TransferJob *trfjob = dynamic_cast<TDEIO::TransferJob*>(job);
|
||
15 years ago
|
bool error = job->error();
|
||
15 years ago
|
const TQString err = job->errorString();
|
||
15 years ago
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString GroupwareDataAdaptor::uidFromJob( TDEIO::Job *job ) const
|
||
15 years ago
|
{
|
||
|
kdDebug()<<"GroupwareDataAdaptor::uidFromJob( "<<job<<")"<<endl;
|
||
13 years ago
|
if ( mJobUIDMap.contains( job ) ) {
|
||
15 years ago
|
kdDebug()<<" Contained: "<< mJobUIDMap[job] << endl;
|
||
|
return mJobUIDMap[ job ];
|
||
|
} else {
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void GroupwareDataAdaptor::setUidForJob( TDEIO::Job *job, const TQString &uid )
|
||
15 years ago
|
{
|
||
|
if ( uid.isEmpty() ) {
|
||
|
mJobUIDMap.remove( job );
|
||
|
} else {
|
||
|
mJobUIDMap[ job ] = uid;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
#include "groupwaredataadaptor.moc"
|