summaryrefslogtreecommitdiffstats
path: root/libkpimexchange/core/exchangedelete.cpp
blob: 094b3b5ccd24c91d98ba31457ed1f7cf66879948 (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
/*
    This file is part of libkpimexchange
    Copyright (c) 2002 Jan-Pascal van Best <[email protected]>

    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 <tqstring.h>
#include <tqregexp.h>

#include <kurl.h>
#include <kdebug.h>
#include <krfcdate.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>

#include <tdeio/slave.h>
#include <tdeio/scheduler.h>
#include <tdeio/slavebase.h>
#include <tdeio/davjob.h>
#include <tdeio/http.h>

#include "exchangeclient.h"
#include "exchangeprogress.h"
#include "exchangedelete.h"
#include "exchangeaccount.h"
#include "utils.h"

using namespace KPIM;

// Delete:
// - Find URL for uid
// - Delete URL
// - Can there be multipe URLs, for instance when dealing with 
// recurrent appointments? Maybe, so we just look for Master or Single
// instancetypes

ExchangeDelete::ExchangeDelete( KCal::Event* event, ExchangeAccount* account, TQWidget* window ) :
  mWindow( window )
{
  kdDebug() << "Created ExchangeDelete" << endl;

  mAccount = account;

  findUidSingleMaster( event->uid() );
}

ExchangeDelete::~ExchangeDelete()
{
  kdDebug() << "ExchangeDelete destructor" << endl;
}

void ExchangeDelete::findUidSingleMaster( TQString const& uid )
{
  TQString query = 
        "SELECT \"DAV:href\", \"urn:schemas:calendar:uid\"\r\n"
        "FROM Scope('shallow traversal of \"\"')\r\n"
        "WHERE \"urn:schemas:calendar:uid\" = '" + uid + "'\r\n"
	" AND (\"urn:schemas:calendar:instancetype\" = 0\r\n"
	"      OR \"urn:schemas:calendar:instancetype\" = 1)\r\n";

  TDEIO::DavJob* job = TDEIO::davSearch( mAccount->calendarURL(), "DAV:", "sql", query, false );
  job->setWindow( mWindow );
  connect(job, TQT_SIGNAL(result( TDEIO::Job * )), this, TQT_SLOT(slotFindUidResult(TDEIO::Job *)));
}

void ExchangeDelete::slotFindUidResult( TDEIO::Job * job )
{
  if ( job->error() ) {
    job->showErrorDialog( 0L );
    emit finished( this, ExchangeClient::CommunicationError, "IO Error: " + TQString::number(job->error()) + ":" + job->errorString() );
    return;
  }
  TQDomDocument& response = static_cast<TDEIO::DavJob *>( job )->response();

  TQDomElement item = response.documentElement().firstChild().toElement();
  TQDomElement hrefElement = item.namedItem( "href" ).toElement();
  if ( item.isNull() || hrefElement.isNull() ) {
    // Not found
    emit finished( this, ExchangeClient::DeleteUnknownEventError, "UID of event to be deleted not found on server\n"+response.toString() );
    return;
  }
  // Found the appointment's URL
  TQString href = hrefElement.text();
  KURL url(href);

  startDelete( toDAV( url ) );  
}  

void ExchangeDelete::startDelete( const KURL& url )
{
  TDEIO::SimpleJob* job = TDEIO::file_delete( url, false ); // no GUI
  job->setWindow( mWindow );
  connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ), this, TQT_SLOT( slotDeleteResult( TDEIO::Job * ) ) );
}

void ExchangeDelete::slotDeleteResult( TDEIO::Job* job )
{
  kdDebug() << "Finished Delete" << endl;
  if ( job->error() ) {
    job->showErrorDialog( 0L );
    emit finished( this, ExchangeClient::CommunicationError, "IO Error: " + TQString::number(job->error()) + ":" + job->errorString() );
    return;
  }
  emit finished( this, ExchangeClient::ResultOK, TQString() );
}

#include "exchangedelete.moc"