21 #include "freebusymanager.h"
22 #include "freebusymanager_p.h"
23 #include "freebusydownloadjob_p.h"
24 #include "mailscheduler_p.h"
25 #include "publishdialog.h"
26 #include "calendarsettings.h"
29 #include <akonadi/agentinstance.h>
30 #include <akonadi/agentmanager.h>
31 #include <akonadi/contact/contactsearchjob.h>
33 #include <kcalcore/event.h>
34 #include <kcalcore/freebusy.h>
35 #include <kcalcore/person.h>
38 #include <KMessageBox>
39 #include <KStandardDirs>
40 #include <KTemporaryFile>
43 #include <KIO/JobUiDelegate>
44 #include <KIO/NetAccess>
50 #include <QTextStream>
52 #include <QTimerEvent>
54 using namespace Akonadi;
55 using namespace KCalCore;
59 KUrl replaceVariablesUrl(
const KUrl &url,
const QString &email )
64 const int atPos = email.indexOf(
'@' );
66 emailName = email.left( atPos );
67 emailHost = email.mid( atPos + 1 );
70 QString saveStr = url.path();
71 saveStr.replace( QRegExp(
"%[Ee][Mm][Aa][Ii][Ll]%" ), email );
72 saveStr.replace( QRegExp(
"%[Nn][Aa][Mm][Ee]%" ), emailName );
73 saveStr.replace( QRegExp(
"%[Ss][Ee][Rr][Vv][Ee][Rr]%" ), emailHost );
76 retUrl.setPath( saveStr );
80 bool fbExists(
const KUrl &url )
87 KIO::Job *job = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );
89 if ( KIO::NetAccess::synchronousRun( job, 0, &data ) ) {
90 QString dataStr( data );
91 if ( dataStr.contains(
"BEGIN:VCALENDAR" ) ) {
101 : mRequestStatus( NotStarted ), mInterface( 0 )
104 QSharedPointer<QDBusInterface>(
105 new QDBusInterface(
"org.freedesktop.Akonadi.Resource." + provider,
107 "org.freedesktop.Akonadi.Resource.FreeBusyProvider" ) );
112 FreeBusyManagerPrivate::FreeBusyProvidersRequestsQueue::FreeBusyProvidersRequestsQueue(
113 const QString &start,
const QString &end )
114 : mHandlersCount( 0 ), mResultingFreeBusy( 0 )
116 KDateTime startDate, endDate;
118 if ( !start.isEmpty() ) {
120 startDate = KDateTime::fromString( start );
123 startDate = KDateTime( KDateTime::currentLocalDate() );
124 mStartTime = startDate.toString();
127 if ( !end.isEmpty() ) {
129 endDate = KDateTime::fromString( end );
132 endDate = KDateTime( KDateTime::currentLocalDate() ).addDays( 14 );
133 mEndTime = endDate.toString();
136 mResultingFreeBusy = KCalCore::FreeBusy::Ptr(
new KCalCore::FreeBusy( startDate, endDate ) );
139 FreeBusyManagerPrivate::FreeBusyProvidersRequestsQueue::FreeBusyProvidersRequestsQueue(
140 const KDateTime &start,
const KDateTime &end )
141 : mHandlersCount( 0 ), mResultingFreeBusy( 0 )
143 mStartTime = start.toString();
144 mEndTime = end.toString();
145 mResultingFreeBusy = KCalCore::FreeBusy::Ptr(
new KCalCore::FreeBusy( start, end ) );
150 FreeBusyManagerPrivate::FreeBusyManagerPrivate( FreeBusyManager *q )
154 mUploadingFreeBusy( false ),
156 mParentWidgetForRetrieval( 0 )
158 connect(
this, SIGNAL(freeBusyUrlRetrieved(QString,KUrl)),
159 SLOT(finishProcessRetrieveQueue(QString,KUrl)) );
162 QString FreeBusyManagerPrivate::freeBusyDir()
const
164 return KStandardDirs::locateLocal(
"data", QLatin1String(
"korganizer/freebusy" ) );
167 void FreeBusyManagerPrivate::checkFreeBusyUrl()
169 KUrl targetURL( CalendarSettings::self()->freeBusyPublishUrl() );
170 mBrokenUrl = targetURL.isEmpty() || !targetURL.isValid();
173 void FreeBusyManagerPrivate::fetchFreeBusyUrl(
const QString &email )
176 QString configFile = KStandardDirs::locateLocal(
"data",
177 QLatin1String(
"korganizer/freebusyurls" ) );
178 KConfig cfg( configFile );
179 KConfigGroup group = cfg.group( email );
180 QString url = group.readEntry( QLatin1String(
"url" ) );
181 if ( !url.isEmpty() ) {
182 kDebug() <<
"Found cached url:" << url;
183 KUrl cachedUrl( url );
184 if ( Akonadi::CalendarUtils::thatIsMe( email ) ) {
185 cachedUrl.setUser( CalendarSettings::self()->freeBusyRetrieveUser() );
186 cachedUrl.setPass( CalendarSettings::self()->freeBusyRetrievePassword() );
188 emit freeBusyUrlRetrieved( email, replaceVariablesUrl( cachedUrl, email ) );
194 job->setProperty(
"contactEmail", QVariant::fromValue( email ) );
195 connect( job, SIGNAL(result(KJob*)),
this, SLOT(contactSearchJobFinished(KJob*)) );
199 void FreeBusyManagerPrivate::contactSearchJobFinished( KJob *_job )
201 const QString email = _job->property(
"contactEmail" ).toString();
203 if ( _job->error() ) {
204 kError() <<
"Error while searching for contact: "
205 << _job->errorString() <<
", email = " << email;
206 emit freeBusyUrlRetrieved( email, KUrl() );
211 QString configFile = KStandardDirs::locateLocal(
"data",
212 QLatin1String(
"korganizer/freebusyurls" ) );
213 KConfig cfg( configFile );
214 KConfigGroup group = cfg.group( email );
215 QString url = group.readEntry( QLatin1String(
"url" ) );
218 const KABC::Addressee::List contacts = job->
contacts();
219 foreach (
const KABC::Addressee &contact, contacts ) {
220 pref = contact.preferredEmail();
221 if ( !pref.isEmpty() && pref != email ) {
222 group = cfg.group( pref );
223 url = group.readEntry (
"url" );
224 kDebug() <<
"Preferred email of" << email <<
"is" << pref;
225 if ( !url.isEmpty() ) {
226 kDebug() <<
"Taken url from preferred email:" << url;
227 emit freeBusyUrlRetrieved( email, replaceVariablesUrl( KUrl( url ), email ) );
233 if ( !CalendarSettings::self()->freeBusyRetrieveAuto() ) {
235 kDebug() <<
"No automatic retrieving";
236 emit freeBusyUrlRetrieved( email, KUrl() );
242 int emailpos = email.indexOf( QLatin1Char(
'@' ) );
243 if( emailpos == -1 ) {
244 kWarning() <<
"No '@' found in" << email;
245 emit freeBusyUrlRetrieved( email, KUrl() );
249 const QString emailHost = email.mid( emailpos + 1 );
252 if ( CalendarSettings::self()->freeBusyCheckHostname() ) {
255 const QString hostDomain = KUrl( CalendarSettings::self()->freeBusyRetrieveUrl() ).host();
256 if ( hostDomain != emailHost &&
257 !hostDomain.endsWith( QLatin1Char(
'.' ) + emailHost ) &&
258 !emailHost.endsWith( QLatin1Char(
'.' ) + hostDomain ) ) {
260 kDebug() <<
"Host '" << hostDomain <<
"' doesn't match email '" << email <<
'\'';
261 emit freeBusyUrlRetrieved( email, KUrl() );
266 if ( CalendarSettings::self()->freeBusyRetrieveUrl().contains( QRegExp(
"\\.[xiv]fb$" ) ) ) {
269 const KUrl sourceUrl( CalendarSettings::self()->freeBusyRetrieveUrl() );
270 KUrl fullpathURL = replaceVariablesUrl( sourceUrl, email );
273 fullpathURL.setUser( CalendarSettings::self()->freeBusyRetrieveUser() );
274 fullpathURL.setPass( CalendarSettings::self()->freeBusyRetrievePassword() );
278 kDebug() <<
"Found url. email=" << email <<
"; url=" << fullpathURL;
279 emit freeBusyUrlRetrieved( email, fullpathURL );
284 const QStringList extensions = QStringList() <<
"xfb" <<
"ifb" <<
"vfb";
285 QStringList::ConstIterator ext;
286 for ( ext = extensions.constBegin(); ext != extensions.constEnd(); ++ext ) {
288 const KUrl sourceUrl = CalendarSettings::self()->freeBusyRetrieveUrl();
289 KUrl dirURL = replaceVariablesUrl( sourceUrl, email );
290 if ( CalendarSettings::self()->freeBusyFullDomainRetrieval() ) {
291 dirURL.addPath( email +
'.' + (*ext) );
294 const QString emailName = email.left( emailpos );
295 dirURL.addPath( emailName +
'.' + (*ext ) );
297 dirURL.setUser( CalendarSettings::self()->freeBusyRetrieveUser() );
298 dirURL.setPass( CalendarSettings::self()->freeBusyRetrievePassword() );
299 if ( fbExists( dirURL ) ) {
301 KConfigGroup group = cfg.group( email );
302 group.writeEntry(
"url", dirURL.prettyUrl() );
303 kDebug() <<
"Found url email=" << email <<
"; url=" << dirURL;
304 emit freeBusyUrlRetrieved( email, dirURL );
309 kDebug() <<
"Returning invalid url";
310 emit freeBusyUrlRetrieved( email, KUrl() );
313 QString FreeBusyManagerPrivate::freeBusyToIcal(
const KCalCore::FreeBusy::Ptr &freebusy )
315 return mFormat.createScheduleMessage( freebusy, KCalCore::iTIPPublish );
318 KCalCore::FreeBusy::Ptr FreeBusyManagerPrivate::iCalToFreeBusy(
const QByteArray &freeBusyData )
320 const QString freeBusyVCal( QString::fromUtf8( freeBusyData ) );
321 KCalCore::FreeBusy::Ptr fb = mFormat.parseFreeBusy( freeBusyVCal );
324 kDebug() <<
"Error parsing free/busy";
325 kDebug() << freeBusyVCal;
331 KCalCore::FreeBusy::Ptr FreeBusyManagerPrivate::ownerFreeBusy()
333 KDateTime start = KDateTime::currentUtcDateTime();
334 KDateTime end = start.addDays( CalendarSettings::self()->freeBusyPublishDays() );
336 KCalCore::Event::List events = mCalendar ? mCalendar->rawEvents( start.date(), end.date() ) : KCalCore::Event::List();
337 KCalCore::FreeBusy::Ptr freebusy (
new KCalCore::FreeBusy( events, start, end ) );
338 freebusy->setOrganizer( KCalCore::Person::Ptr(
339 new KCalCore::Person( Akonadi::CalendarUtils::fullName(),
340 Akonadi::CalendarUtils::email() ) ) );
344 QString FreeBusyManagerPrivate::ownerFreeBusyAsString()
346 return freeBusyToIcal( ownerFreeBusy() );
349 void FreeBusyManagerPrivate::processFreeBusyDownloadResult( KJob *_job )
351 Q_Q( FreeBusyManager );
353 FreeBusyDownloadJob *job = qobject_cast<FreeBusyDownloadJob *>( _job );
355 if ( job->error() ) {
356 kError() <<
"Error downloading freebusy" << _job->errorString();
358 mParentWidgetForRetrieval,
359 i18n(
"Failed to download free/busy data from: %1\nReason: %2",
360 job->url().prettyUrl(), job->errorText() ),
361 i18n(
"Free/busy retrieval error" ) );
366 mFreeBusyUrlEmailMap.take( job->url() );
368 KCalCore::FreeBusy::Ptr fb = iCalToFreeBusy( job->rawFreeBusyData() );
370 Q_ASSERT( mFreeBusyUrlEmailMap.contains( job->url() ) );
371 const QString email = mFreeBusyUrlEmailMap.take( job->url() );
374 KCalCore::Person::Ptr p = fb->organizer();
375 p->setEmail( email );
376 q->saveFreeBusy( fb, p );
377 kDebug() <<
"Freebusy retrieved for " << email;
378 emit q->freeBusyRetrieved( fb, email );
380 kError() <<
"Error downloading freebusy, invalid fb.";
382 mParentWidgetForRetrieval,
383 i18n(
"Failed to parse free/busy information that was retrieved from: %1",
384 job->url().prettyUrl() ),
385 i18n(
"Free/busy retrieval error" ) );
391 processRetrieveQueue();
394 void FreeBusyManagerPrivate::processFreeBusyUploadResult( KJob *_job )
396 KIO::FileCopyJob *job =
static_cast<KIO::FileCopyJob *
>( _job );
397 if ( job->error() ) {
400 i18n(
"<qt><p>The software could not upload your free/busy list to "
401 "the URL '%1'. There might be a problem with the access "
402 "rights, or you specified an incorrect URL. The system said: "
404 "<p>Please check the URL or contact your system administrator."
405 "</p></qt>", job->destUrl().prettyUrl(),
406 job->errorString() ) );
409 KUrl src = job->srcUrl();
410 Q_ASSERT( src.isLocalFile() );
411 if ( src.isLocalFile() ) {
412 QFile::remove( src.toLocalFile() );
414 mUploadingFreeBusy =
false;
417 void FreeBusyManagerPrivate::processRetrieveQueue()
419 if ( mRetrieveQueue.isEmpty() ) {
423 QString email = mRetrieveQueue.takeFirst();
426 QStringList providers = getFreeBusyProviders();
427 kDebug() <<
"Got the following FreeBusy providers: " << providers;
431 if ( !providers.isEmpty() ) {
432 queryFreeBusyProviders( providers, email );
434 fetchFreeBusyUrl( email );
440 void FreeBusyManagerPrivate::finishProcessRetrieveQueue(
const QString &email,
441 const KUrl &freeBusyUrlForEmail )
443 Q_Q( FreeBusyManager );
445 if ( !freeBusyUrlForEmail.isValid() ) {
446 kDebug() <<
"Invalid FreeBusy URL" << freeBusyUrlForEmail.prettyUrl() << email;
450 if ( mFreeBusyUrlEmailMap.contains( freeBusyUrlForEmail ) ) {
451 kDebug() <<
"Download already in progress for " << freeBusyUrlForEmail;
455 mFreeBusyUrlEmailMap.insert( freeBusyUrlForEmail, email );
457 FreeBusyDownloadJob *job =
new FreeBusyDownloadJob( freeBusyUrlForEmail, mParentWidgetForRetrieval );
458 q->connect( job, SIGNAL(result(KJob*)), SLOT(processFreeBusyDownloadResult(KJob*)) );
462 void FreeBusyManagerPrivate::uploadFreeBusy()
464 Q_Q( FreeBusyManager );
467 if ( !CalendarSettings::self()->freeBusyPublishAuto() ||
468 CalendarSettings::self()->freeBusyPublishUrl().isEmpty() ) {
472 if( mTimerID != 0 ) {
477 int now =
static_cast<int>( QDateTime::currentDateTime().toTime_t() );
478 int eta =
static_cast<int>( mNextUploadTime.toTime_t() ) - now;
480 if ( !mUploadingFreeBusy ) {
482 if ( mNextUploadTime.isNull() ||
483 QDateTime::currentDateTime() > mNextUploadTime ) {
485 q->publishFreeBusy();
492 q->publishFreeBusy();
498 kDebug() <<
"This shouldn't happen! eta <= 0";
504 mTimerID = q->startTimer( eta * 1000 );
506 if ( mTimerID == 0 ) {
508 q->publishFreeBusy();
512 QStringList FreeBusyManagerPrivate::getFreeBusyProviders()
const
514 QStringList providers;
517 if ( agent.
type().
capabilities().contains( QLatin1String(
"FreeBusyProvider" ) ) ) {
524 void FreeBusyManagerPrivate::queryFreeBusyProviders(
const QStringList &providers,
525 const QString &email )
527 if ( !mProvidersRequestsByEmail.contains( email ) ) {
528 mProvidersRequestsByEmail[email] = FreeBusyProvidersRequestsQueue();
531 foreach (
const QString &provider, providers ) {
532 FreeBusyProviderRequest request( provider );
534 connect( request.mInterface.data(), SIGNAL(handlesFreeBusy(QString,
bool)),
535 this, SLOT(onHandlesFreeBusy(QString,
bool)) );
537 request.mInterface->call(
"canHandleFreeBusy", email );
538 request.mRequestStatus = FreeBusyProviderRequest::HandlingRequested;
539 mProvidersRequestsByEmail[email].mRequests << request;
543 void FreeBusyManagerPrivate::queryFreeBusyProviders(
const QStringList &providers,
544 const QString &email,
545 const KDateTime &start,
546 const KDateTime &end )
548 if ( !mProvidersRequestsByEmail.contains( email ) ) {
549 mProvidersRequestsByEmail[email] = FreeBusyProvidersRequestsQueue( start, end );
552 queryFreeBusyProviders( providers, email );
555 void FreeBusyManagerPrivate::onHandlesFreeBusy(
const QString &email,
bool handles )
557 if ( !mProvidersRequestsByEmail.contains( email ) ) {
561 QDBusInterface *iface =
dynamic_cast<QDBusInterface*
>( sender() );
566 FreeBusyProvidersRequestsQueue *queue = &mProvidersRequestsByEmail[email];
567 QString respondingService = iface->service();
568 kDebug() << respondingService <<
"responded to our FreeBusy request:" << handles;
569 int requestIndex = -1;
571 for (
int i = 0; i < queue->mRequests.size(); ++i ) {
572 if ( queue->mRequests.at( i ).mInterface->service() == respondingService ) {
577 if ( requestIndex == -1 ) {
581 disconnect( iface, SIGNAL(handlesFreeBusy(QString,
bool)),
582 this, SLOT(onHandlesFreeBusy(QString,
bool)) );
585 queue->mRequests.removeAt( requestIndex );
588 if ( queue->mRequests.isEmpty() && queue->mHandlersCount == 0 ) {
589 mProvidersRequestsByEmail.remove( email );
590 fetchFreeBusyUrl( email );
593 ++queue->mHandlersCount;
594 connect( iface, SIGNAL(freeBusyRetrieved(QString,QString,
bool,QString)),
595 this, SLOT(onFreeBusyRetrieved(QString,QString,
bool,QString)) );
596 iface->call(
"retrieveFreeBusy", email, queue->mStartTime, queue->mEndTime );
597 queue->mRequests[requestIndex].mRequestStatus = FreeBusyProviderRequest::FreeBusyRequested;
601 void FreeBusyManagerPrivate::processMailSchedulerResult( Akonadi::Scheduler::Result result,
602 const QString &errorMsg )
604 if ( result == Scheduler::ResultSuccess ) {
605 KMessageBox::information(
606 mParentWidgetForMailling,
607 i18n(
"The free/busy information was successfully sent." ),
608 i18n(
"Sending Free/Busy" ),
609 "FreeBusyPublishSuccess" );
611 KMessageBox::error( mParentWidgetForMailling,
612 i18n(
"Unable to publish the free/busy data: %1", errorMsg ) );
615 sender()->deleteLater();
618 void FreeBusyManagerPrivate::onFreeBusyRetrieved(
const QString &email,
619 const QString &freeBusy,
621 const QString &errorText )
623 Q_Q( FreeBusyManager );
624 Q_UNUSED( errorText );
626 if ( !mProvidersRequestsByEmail.contains( email ) ) {
630 QDBusInterface *iface =
dynamic_cast<QDBusInterface*
>( sender() );
635 FreeBusyProvidersRequestsQueue *queue = &mProvidersRequestsByEmail[email];
636 QString respondingService = iface->service();
637 int requestIndex = -1;
639 for (
int i = 0; i < queue->mRequests.size(); ++i ) {
640 if ( queue->mRequests.at( i ).mInterface->service() == respondingService ) {
645 if ( requestIndex == -1 ) {
649 disconnect( iface, SIGNAL(freeBusyRetrieved(QString,QString,
bool,QString)),
650 this, SLOT(onFreeBusyRetrieved(QString,QString,
bool,QString)) );
652 queue->mRequests.removeAt( requestIndex );
655 KCalCore::FreeBusy::Ptr fb = iCalToFreeBusy( freeBusy.toUtf8() );
657 --queue->mHandlersCount;
659 queue->mResultingFreeBusy->merge( fb );
663 if ( queue->mRequests.isEmpty() ) {
664 if ( queue->mHandlersCount == 0 ) {
665 fetchFreeBusyUrl( email );
667 emit q->freeBusyRetrieved( queue->mResultingFreeBusy, email );
669 mProvidersRequestsByEmail.remove( email );
677 struct FreeBusyManagerStatic
679 FreeBusyManager instance;
684 K_GLOBAL_STATIC( FreeBusyManagerStatic, sManagerInstance )
686 FreeBusyManager::FreeBusyManager() : d_ptr( new FreeBusyManagerPrivate( this ) )
688 setObjectName( QLatin1String(
"FreeBusyManager" ) );
689 connect( CalendarSettings::self(), SIGNAL(configChanged()), SLOT(checkFreeBusyUrl()) );
692 FreeBusyManager::~FreeBusyManager()
697 FreeBusyManager *FreeBusyManager::self()
699 return &sManagerInstance->instance;
702 void FreeBusyManager::setCalendar(
const Akonadi::ETMCalendar::Ptr &c )
704 Q_D( FreeBusyManager );
706 if ( d->mCalendar ) {
707 disconnect( d->mCalendar.data(), SIGNAL(calendarChanged()) );
711 if ( d->mCalendar ) {
712 d->mFormat.setTimeSpec( d->mCalendar->timeSpec() );
715 connect( d->mCalendar.data(), SIGNAL(calendarChanged()), SLOT(uploadFreeBusy()) );
718 QTimer::singleShot( 0,
this, SLOT(uploadFreeBusy()) );
725 void FreeBusyManager::publishFreeBusy( QWidget *parentWidget )
727 Q_D( FreeBusyManager );
729 if ( d->mUploadingFreeBusy ) {
735 if ( !d->mCalendar ) {
739 KUrl targetURL( CalendarSettings::self()->freeBusyPublishUrl() );
740 if ( targetURL.isEmpty() ) {
743 i18n(
"<qt><p>No URL configured for uploading your free/busy list. "
744 "Please set it in KOrganizer's configuration dialog, on the "
745 "\"Free/Busy\" page.</p>"
746 "<p>Contact your system administrator for the exact URL and the "
747 "account details.</p></qt>" ),
748 i18n(
"No Free/Busy Upload URL" ) );
752 if ( d->mBrokenUrl ) {
756 if ( !targetURL.isValid() ) {
759 i18n(
"<qt>The target URL '%1' provided is invalid.</qt>", targetURL.prettyUrl() ),
760 i18n(
"Invalid URL" ) );
761 d->mBrokenUrl =
true;
764 targetURL.setUser( CalendarSettings::self()->freeBusyPublishUser() );
765 targetURL.setPass( CalendarSettings::self()->freeBusyPublishPassword() );
767 d->mUploadingFreeBusy =
true;
770 if ( d->mTimerID != 0 ) {
771 killTimer( d->mTimerID );
776 d->mNextUploadTime = QDateTime::currentDateTime();
777 if ( CalendarSettings::self()->freeBusyPublishDelay() > 0 ) {
779 d->mNextUploadTime.addSecs( CalendarSettings::self()->freeBusyPublishDelay() * 60 );
782 QString messageText = d->ownerFreeBusyAsString();
786 messageText = messageText.replace( QRegExp( QLatin1String(
"ORGANIZER\\s*:MAILTO:" ) ),
787 QLatin1String(
"ORGANIZER:" ) );
790 KTemporaryFile tempFile;
791 tempFile.setAutoRemove(
false );
792 if ( tempFile.open() ) {
793 QTextStream textStream ( &tempFile );
794 textStream << messageText;
798 QString defaultEmail = KOCore()::self()->email();
799 QString emailHost = defaultEmail.mid( defaultEmail.indexOf(
'@' ) + 1 );
803 if( CalendarSettings::self()->publishKolab() ) {
806 if ( CalendarSettings::self()->publishKolabServer() == QLatin1String(
"%SERVER%" ) ||
807 CalendarSettings::self()->publishKolabServer().isEmpty() ) {
810 server = CalendarSettings::self()->publishKolabServer();
813 targetURL.setProtocol(
"webdavs" );
814 targetURL.setHost( server );
816 QString fbname = CalendarSettings::self()->publishUserName();
817 int at = fbname.indexOf(
'@' );
818 if ( at > 1 && fbname.length() > (uint)at ) {
819 fbname = fbname.left(at);
821 targetURL.setPath(
"/freebusy/" + fbname +
".ifb" );
822 targetURL.setUser( CalendarSettings::self()->publishUserName() );
823 targetURL.setPass( CalendarSettings::self()->publishPassword() );
826 targetURL = CalendarSettings::self()->+publishAnyURL().replace(
"%SERVER%", emailHost );
827 targetURL.setUser( CalendarSettings::self()->publishUserName() );
828 targetURL.setPass( CalendarSettings::self()->publishPassword() );
833 src.setPath( tempFile.fileName() );
835 kDebug() << targetURL;
837 KIO::Job *job = KIO::file_copy( src, targetURL, -1, KIO::Overwrite | KIO::HideProgressInfo );
839 job->ui()->setWindow( parentWidget );
841 connect( job, SIGNAL(result(KJob*)), SLOT(slotUploadFreeBusyResult(KJob*)) );
845 void FreeBusyManager::mailFreeBusy(
int daysToPublish, QWidget *parentWidget )
847 Q_D( FreeBusyManager );
849 if ( !d->mCalendar ) {
853 KDateTime start = KDateTime::currentUtcDateTime().toTimeSpec( d->mCalendar->timeSpec() );
854 KDateTime end = start.addDays( daysToPublish );
856 KCalCore::Event::List events = d->mCalendar->rawEvents( start.date(), end.date() );
858 FreeBusy::Ptr freebusy(
new FreeBusy( events, start, end ) );
859 freebusy->setOrganizer( Person::Ptr(
860 new Person( Akonadi::CalendarUtils::fullName(),
861 Akonadi::CalendarUtils::email() ) ) );
863 QPointer<PublishDialog> publishdlg =
new PublishDialog();
864 if ( publishdlg->exec() == QDialog::Accepted ) {
866 MailScheduler *scheduler =
new MailScheduler();
867 connect( scheduler, SIGNAL(transactionFinished(Akonadi::Scheduler::Result,QString))
868 , d, SLOT(processMailSchedulerResult(Akonadi::Scheduler::Result,QString)) );
869 d->mParentWidgetForMailling = parentWidget;
871 scheduler->publish( freebusy, publishdlg->addresses() );
876 bool FreeBusyManager::retrieveFreeBusy(
const QString &email,
bool forceDownload,
877 QWidget *parentWidget )
879 Q_D( FreeBusyManager );
882 if ( email.isEmpty() ) {
883 kDebug() <<
"Email is empty";
887 d->mParentWidgetForRetrieval = parentWidget;
889 if ( Akonadi::CalendarUtils::thatIsMe( email ) ) {
891 kDebug() <<
"freebusy of owner, not downloading";
892 emit freeBusyRetrieved( d->ownerFreeBusy(), email );
897 KCalCore::FreeBusy::Ptr fb = loadFreeBusy( email );
899 kDebug() <<
"Found a cached copy for " << email;
900 emit freeBusyRetrieved( fb, email );
905 if ( !CalendarSettings::self()->freeBusyRetrieveAuto() && !forceDownload ) {
906 kDebug() <<
"Not downloading freebusy";
910 d->mRetrieveQueue.append( email );
912 if ( d->mRetrieveQueue.count() > 1 ) {
914 kWarning() <<
"Returning true without emit, is this correct?";
920 QMetaObject::invokeMethod( d,
"processRetrieveQueue", Qt::QueuedConnection );
924 void FreeBusyManager::cancelRetrieval()
926 Q_D( FreeBusyManager );
927 d->mRetrieveQueue.clear();
930 KCalCore::FreeBusy::Ptr FreeBusyManager::loadFreeBusy(
const QString &email )
932 Q_D( FreeBusyManager );
933 const QString fbd = d->freeBusyDir();
935 QFile f( fbd + QLatin1Char(
'/' ) + email + QLatin1String(
".ifb" ) );
937 kDebug() << f.fileName() <<
"doesn't exist.";
938 return KCalCore::FreeBusy::Ptr();
941 if ( !f.open( QIODevice::ReadOnly ) ) {
942 kDebug() <<
"Unable to open file" << f.fileName();
943 return KCalCore::FreeBusy::Ptr();
946 QTextStream ts( &f );
947 QString str = ts.readAll();
949 return d->iCalToFreeBusy( str.toUtf8() );
952 bool FreeBusyManager::saveFreeBusy(
const KCalCore::FreeBusy::Ptr &freebusy,
953 const KCalCore::Person::Ptr &person )
955 Q_D( FreeBusyManager );
957 kDebug() << person->fullName();
959 QString fbd = d->freeBusyDir();
961 QDir freeBusyDirectory( fbd );
962 if ( !freeBusyDirectory.exists() ) {
963 kDebug() <<
"Directory" << fbd <<
" does not exist!";
964 kDebug() <<
"Creating directory:" << fbd;
966 if( !freeBusyDirectory.mkpath( fbd ) ) {
967 kDebug() <<
"Could not create directory:" << fbd;
972 QString filename( fbd );
973 filename += QLatin1Char(
'/' );
974 filename += person->email();
975 filename += QLatin1String(
".ifb" );
978 kDebug() <<
"filename:" << filename;
980 freebusy->clearAttendees();
981 freebusy->setOrganizer( person );
983 QString messageText = d->mFormat.createScheduleMessage( freebusy, KCalCore::iTIPPublish );
985 if ( !f.open( QIODevice::ReadWrite ) ) {
986 kDebug() <<
"acceptFreeBusy: Can't open:" << filename <<
"for writing";
996 void FreeBusyManager::timerEvent( QTimerEvent * )
1001 #include "freebusymanager.moc"
1002 #include "freebusymanager_p.moc"