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.
226 lines
6.3 KiB
226 lines
6.3 KiB
15 years ago
|
/*
|
||
|
This file is part of KDE.
|
||
|
|
||
|
Copyright (C) 2007 Trolltech ASA. All rights reserved.
|
||
|
|
||
|
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.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqapplication.h>
|
||
|
#include <tqeventloop.h>
|
||
15 years ago
|
|
||
|
#include <kapplication.h>
|
||
|
#include <kcmdlineargs.h>
|
||
|
#include <kdebug.h>
|
||
13 years ago
|
#include <tdeversion.h>
|
||
12 years ago
|
#include <tdeio/global.h>
|
||
15 years ago
|
#include <klocale.h>
|
||
|
|
||
13 years ago
|
#include <tdepimmacros.h>
|
||
15 years ago
|
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include "scalix.h"
|
||
|
|
||
|
extern "C" {
|
||
|
KDE_EXPORT int kdemain( int argc, char **argv );
|
||
|
}
|
||
|
|
||
|
static const KCmdLineOptions options[] =
|
||
|
{
|
||
|
{ "+protocol", I18N_NOOP( "Protocol name" ), 0 },
|
||
|
{ "+pool", I18N_NOOP( "Socket name" ), 0 },
|
||
|
{ "+app", I18N_NOOP( "Socket name" ), 0 },
|
||
|
KCmdLineLastOption
|
||
|
};
|
||
|
|
||
|
int kdemain( int argc, char **argv )
|
||
|
{
|
||
|
putenv( strdup( "SESSION_MANAGER=" ) );
|
||
12 years ago
|
TDEApplication::disableAutoDcopRegistration();
|
||
15 years ago
|
|
||
12 years ago
|
TDECmdLineArgs::init( argc, argv, "kio_scalix", 0, 0, 0, 0 );
|
||
|
TDECmdLineArgs::addCmdLineOptions( options );
|
||
12 years ago
|
TDEApplication app( false, false );
|
||
15 years ago
|
|
||
12 years ago
|
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
|
||
15 years ago
|
Scalix slave( args->arg( 0 ), args->arg( 1 ), args->arg( 2 ) );
|
||
|
slave.dispatchLoop();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
Scalix::Scalix( const TQCString &protocol, const TQCString &pool, const TQCString &app )
|
||
15 years ago
|
: SlaveBase( protocol, pool, app )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void Scalix::get( const KURL &url )
|
||
|
{
|
||
|
mimeType( "text/plain" );
|
||
|
|
||
15 years ago
|
TQString path = url.path();
|
||
15 years ago
|
|
||
13 years ago
|
if ( path.contains( "/freebusy/" ) ) {
|
||
15 years ago
|
retrieveFreeBusy( url );
|
||
|
} else {
|
||
12 years ago
|
error( TDEIO::ERR_SLAVE_DEFINED, i18n( "Unknown path. Known path is '/freebusy/'" ) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void Scalix::put( const KURL& url, int, bool, bool )
|
||
|
{
|
||
15 years ago
|
TQString path = url.path();
|
||
15 years ago
|
|
||
13 years ago
|
if ( path.contains( "/freebusy/" ) ) {
|
||
15 years ago
|
publishFreeBusy( url );
|
||
|
} else {
|
||
12 years ago
|
error( TDEIO::ERR_SLAVE_DEFINED, i18n( "Unknown path. Known path is '/freebusy/'" ) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void Scalix::retrieveFreeBusy( const KURL &url )
|
||
|
{
|
||
|
/**
|
||
|
* The url is of the following form:
|
||
|
* scalix://user:password@host/freebusy/user@domain.ifb
|
||
|
*/
|
||
|
|
||
|
// Extract user@domain (e.g. everything between '/freebusy/' and '.ifb')
|
||
15 years ago
|
const TQString requestUser = url.path().mid( 10, url.path().length() - 14 );
|
||
15 years ago
|
|
||
15 years ago
|
TQByteArray packedArgs;
|
||
|
TQDataStream stream( packedArgs, IO_WriteOnly );
|
||
15 years ago
|
|
||
13 years ago
|
const TQString argument = TQString( "BEGIN:VFREEBUSY\nATTENDEE:MAILTO:%1\nEND:VFREEBUSY" ).arg( requestUser );
|
||
|
const TQString command = TQString( "X-GET-ICAL-FREEBUSY {%1}" ).arg( argument.length() );
|
||
15 years ago
|
|
||
|
stream << (int) 'X' << 'E' << command << argument;
|
||
|
|
||
13 years ago
|
TQString imapUrl = TQString( "imap://%1@%3/" ).arg( url.pass().isEmpty() ?
|
||
15 years ago
|
url.user() : url.user() + ":" + url.pass() )
|
||
13 years ago
|
.arg( url.host() );
|
||
15 years ago
|
|
||
15 years ago
|
mFreeBusyData = TQString();
|
||
15 years ago
|
|
||
12 years ago
|
TDEIO::SimpleJob *job = TDEIO::special( imapUrl, packedArgs, false );
|
||
|
connect( job, TQT_SIGNAL( infoMessage( TDEIO::Job*, const TQString& ) ),
|
||
|
this, TQT_SLOT( slotInfoMessage( TDEIO::Job*, const TQString& ) ) );
|
||
|
connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ),
|
||
|
this, TQT_SLOT( slotRetrieveResult( TDEIO::Job* ) ) );
|
||
15 years ago
|
|
||
14 years ago
|
tqApp->eventLoop()->enterLoop();
|
||
15 years ago
|
}
|
||
|
|
||
|
void Scalix::publishFreeBusy( const KURL &url )
|
||
|
{
|
||
|
/**
|
||
|
* The url is of the following form:
|
||
|
* scalix://user:password@host/freebusy/path/to/calendar/user@domain
|
||
|
*/
|
||
15 years ago
|
TQString requestUser, calendar;
|
||
|
TQString path = url.path();
|
||
15 years ago
|
|
||
|
// extract user name
|
||
13 years ago
|
int lastSlash = path.findRev( '/' );
|
||
15 years ago
|
if ( lastSlash != -1 )
|
||
|
requestUser = path.mid( lastSlash + 1 );
|
||
|
|
||
|
// extract calendar name
|
||
13 years ago
|
int secondSlash = path.find( '/', 1 );
|
||
15 years ago
|
if ( secondSlash != -1 )
|
||
|
calendar = path.mid( secondSlash + 1, lastSlash - secondSlash - 1 );
|
||
|
|
||
|
if ( requestUser.isEmpty() || calendar.isEmpty() ) {
|
||
12 years ago
|
error( TDEIO::ERR_SLAVE_DEFINED, i18n( "No user or calendar given!" ) );
|
||
15 years ago
|
return;
|
||
|
};
|
||
|
|
||
|
// read freebusy information
|
||
15 years ago
|
TQByteArray data;
|
||
15 years ago
|
while ( true ) {
|
||
|
dataReq();
|
||
|
|
||
15 years ago
|
TQByteArray buffer;
|
||
15 years ago
|
const int newSize = readData(buffer);
|
||
|
if ( newSize < 0 ) {
|
||
|
// read error: network in unknown state so disconnect
|
||
12 years ago
|
error( TDEIO::ERR_COULD_NOT_READ, i18n("KIO data supply error.") );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if ( newSize == 0 )
|
||
|
break;
|
||
|
|
||
|
unsigned int oldSize = data.size();
|
||
|
data.resize( oldSize + buffer.size() );
|
||
|
memcpy( data.data() + oldSize, buffer.data(), buffer.size() );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQByteArray packedArgs;
|
||
|
TQDataStream stream( packedArgs, IO_WriteOnly );
|
||
15 years ago
|
|
||
15 years ago
|
const TQString argument = TQString::fromUtf8( data );
|
||
13 years ago
|
const TQString command = TQString( "X-PUT-ICAL-FREEBUSY Calendar {%1}" ).arg( argument.length() );
|
||
15 years ago
|
|
||
|
stream << (int) 'X' << 'E' << command << argument;
|
||
|
|
||
13 years ago
|
TQString imapUrl = TQString( "imap://%1@%3/" ).arg( url.pass().isEmpty() ?
|
||
15 years ago
|
url.user() : url.user() + ":" + url.pass() )
|
||
13 years ago
|
.arg( url.host() );
|
||
15 years ago
|
|
||
12 years ago
|
TDEIO::SimpleJob *job = TDEIO::special( imapUrl, packedArgs, false );
|
||
|
connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ),
|
||
|
this, TQT_SLOT( slotPublishResult( TDEIO::Job* ) ) );
|
||
15 years ago
|
|
||
14 years ago
|
tqApp->eventLoop()->enterLoop();
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void Scalix::slotInfoMessage( TDEIO::Job *job, const TQString &data )
|
||
15 years ago
|
{
|
||
|
if ( job->error() ) {
|
||
|
// error is handled in slotResult
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mFreeBusyData = data;
|
||
|
}
|
||
|
|
||
|
|
||
12 years ago
|
void Scalix::slotRetrieveResult( TDEIO::Job *job )
|
||
15 years ago
|
{
|
||
|
if ( job->error() ) {
|
||
12 years ago
|
error( TDEIO::ERR_SLAVE_DEFINED, job->errorString() );
|
||
15 years ago
|
} else {
|
||
|
data( mFreeBusyData.utf8() );
|
||
|
finished();
|
||
|
}
|
||
|
|
||
14 years ago
|
tqApp->eventLoop()->exitLoop();
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void Scalix::slotPublishResult( TDEIO::Job *job )
|
||
15 years ago
|
{
|
||
|
if ( job->error() ) {
|
||
12 years ago
|
error( TDEIO::ERR_SLAVE_DEFINED, job->errorString() );
|
||
15 years ago
|
} else {
|
||
|
finished();
|
||
|
}
|
||
|
|
||
14 years ago
|
tqApp->eventLoop()->exitLoop();
|
||
15 years ago
|
}
|
||
|
|
||
|
#include "scalix.moc"
|