summaryrefslogtreecommitdiffstats
path: root/kioslaves/opengroupware/opengroupware.cpp
blob: 6e798c64e33c3f31fc0e3b0a7886287e04513935 (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
/*
    This file is part of KDE.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>

    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 "opengroupware.h"
#include "webdavhandler.h"

#include <kdebug.h>
#include <kurl.h>
#include <kio/job.h>
#include <kio/davjob.h>
#include <klocale.h>

#include <libtdepim/kabcresourcecached.h>

#include <libkcal/freebusy.h>
#include <libkcal/icalformat.h>
#include <libkcal/scheduler.h>
#include <libkcal/calendarlocal.h>

#include <kabc/vcardconverter.h>

#include <kinstance.h>
#include <kdebug.h>
#include <klocale.h>
#include <ktempfile.h>

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

#include <kdepimmacros.h>

namespace KABC {

class ResourceMemory : public ResourceCached
{
  public:
    ResourceMemory() : ResourceCached( 0 ) {}
    
    Ticket *requestSaveTicket() { return 0; }
    bool load() { return true; }
    bool save( Ticket * ) { return true; }
    void releaseSaveTicket( Ticket * ) {}
};

}


extern "C" {
KDE_EXPORT int kdemain( int argc, char **argv );
}

int kdemain( int argc, char **argv )
{
  KInstance instance( "kio_OpenGroupware" );
  
  kdDebug(7000) << "Starting kio_OpenGroupware(pid:  " << getpid() << ")" << endl;
  
  if (argc != 4) {
    fprintf( stderr, "Usage: kio_OpenGroupware protocol domain-socket1 domain-socket2\n");
    exit( -1 );
  }
  
  OpenGroupware slave( argv[1], argv[2], argv[3] );
  slave.dispatchLoop();
  
  return 0;
}

OpenGroupware::OpenGroupware( const TQCString &protocol, const TQCString &pool,
  const TQCString &app )
  : SlaveBase( protocol, pool, app )
{
}

void OpenGroupware::get( const KURL &url )
{
  kdDebug(7000) << "OpenGroupware::get()" << endl;
  kdDebug(7000) << " URL: " << url.url() << endl;
  #if 1
  kdDebug(7000) << " Path: " << url.path() << endl;
  kdDebug(7000) << " Query: " << url.query() << endl;
  kdDebug(7000) << " Protocol: " << url.protocol() << endl;
  kdDebug(7000) << " Filename: " << url.filename() << endl;
  #endif

  mimeType( "text/plain" );

  TQString path = url.path();
  debugMessage( "Path: " + path );

  if ( path.startsWith( "/freebusy/" ) ) {
    getFreeBusy( url );
  } else if ( path.startsWith( "/calendar/" ) ) {
    getCalendar( url );
  } else if ( path.startsWith( "/addressbook/" ) ) {
    getAddressbook( url );
  } else {
    TQString error = i18n("Unknown path. Known paths are '/freebusy/', "
      "'/calendar/' and '/addressbook/'.");
    errorMessage( error );
  }
  
  kdDebug(7000) << "OpenGroupwareCgiProtocol::get() done" << endl;
}

void OpenGroupware::getFreeBusy( const KURL &url )
{
  TQString file = url.filename();
  if ( file.right( 4 ) != ".ifb" ) {
    TQString error = i18n("Illegal filename. File has to have '.ifb' suffix.");
    errorMessage( error );
  } else {
    TQString email = file.left( file.length() - 4 );
    debugMessage( "Email: " + email );

    TQString user = url.user();
    TQString pass = url.pass();

    debugMessage( "URL: "  );
    debugMessage( "User: " + user );
    debugMessage( "Password: " + pass );

    KCal::FreeBusy *fb = new KCal::FreeBusy;

    if ( user.isEmpty() || pass.isEmpty() ) {
      errorMessage( i18n("Need username and password.") );
    } else {
      // FIXME get from server

      // FIXME: Read range from configuration or URL parameters.
      TQDate start = TQDate::tqcurrentDate().addDays( -3 );
      TQDate end = TQDate::tqcurrentDate().addDays( 60 );

      fb->setDtStart( start );
      fb->setDtEnd( end );

      kdDebug() << "Login" << endl;

    }

#if 0
    TQDateTime s = TQDateTime( TQDate( 2004, 9, 27 ), TQTime( 10, 0 ) );
    TQDateTime e = TQDateTime( TQDate( 2004, 9, 27 ), TQTime( 11, 0 ) );

    fb->addPeriod( s, e );
#endif

    KCal::ICalFormat format;

    TQString ical = format.createScheduleMessage( fb, KCal::Scheduler::Publish );

    data( ical.utf8() );

    finished();
  }
}


void OpenGroupware::getCalendar( const KURL &_url )
{

  KURL url( _url ); // we'll be changing it
  TQString user = url.user();
  TQString pass = url.pass();

  TQDomDocument props = WebdavHandler::createAllPropsRequest();

  debugMessage( "URL: "  );
  debugMessage( "User: " + user );
  debugMessage( "Password: " + pass );

  url.setProtocol( "webdav" );
  url.setPath ( "/zidestore/dav/till/" );

  kdDebug(7000) << "getCalendar: " << url.prettyURL() << endl;

  // FIXME do progress handling
  mListEventsJob = KIO::davPropFind( url, props, "0", false );
  connect( mListEventsJob, TQT_SIGNAL( result( KIO::Job * ) ),
           TQT_SLOT( slotGetCalendarListingResult( KIO::Job * ) ) );
}

void OpenGroupware::getAddressbook( const KURL &url )
{
  
}

void OpenGroupware::errorMessage( const TQString &msg )
{
  error( KIO::ERR_SLAVE_DEFINED, msg );
}

void OpenGroupware::debugMessage( const TQString &msg )
{
#if 0
  data( ( msg + "\n" ).utf8() );
#else
  Q_UNUSED( msg );
#endif
}


void OpenGroupware::slotGetCalendarListingResult( KIO::Job *job )
{
  
  kdDebug(7000) << k_funcinfo << endl;

  if (  job->error() ) {
    job->showErrorDialog(  0 );
  } else {
    kdDebug() << "ResourceSlox::slotResult() success" << endl;

    TQDomDocument doc = mListEventsJob->response();

  }
  KCal::ICalFormat format;
  KCal::CalendarLocal calendar;

  TQString ical = format.toString( &calendar );

  data( ical.utf8() );

  finished();
}


void OpenGroupware::slotGetCalendarResult( KIO::Job *job )
{
  Q_UNUSED( job );
}
#include "opengroupware.moc"