libkcal

calhelper.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the kcal library.
00003 
00004   Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019   Boston, MA 02110-1301, USA.
00020 */
00032 #include "calhelper.h"
00033 #include "calendarresources.h"
00034 
00035 #include <libemailfunctions/email.h>
00036 
00037 #include <kemailsettings.h>
00038 
00039 using namespace KCal;
00040 
00041 bool CalHelper::isMyKolabIncidence( Calendar *calendar, Incidence *incidence )
00042 {
00043   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00044   if ( !cal || !incidence ) {
00045     return true;
00046   }
00047 
00048   CalendarResourceManager *manager = cal->resourceManager();
00049   CalendarResourceManager::Iterator it;
00050   for ( it = manager->begin(); it != manager->end(); ++it ) {
00051     QString subRes = (*it)->subresourceIdentifier( incidence );
00052     if ( !subRes.isEmpty() && !subRes.contains( "/.INBOX.directory/" ) ) {
00053       return false;
00054     }
00055   }
00056   return true;
00057 }
00058 
00059 bool CalHelper::isMyCalendarIncidence( Calendar *calendar, Incidence *incidence )
00060 {
00061   return isMyKolabIncidence( calendar, incidence );
00062 }
00063 
00064 Incidence *CalHelper::findMyCalendarIncidenceByUid( Calendar *calendar, const QString &uid )
00065 {
00066   // Determine if this incidence is in my calendar (and owned by me)
00067   Incidence *existingIncidence = 0;
00068   if ( calendar ) {
00069     existingIncidence = calendar->incidence( uid );
00070     if ( !isMyCalendarIncidence( calendar, existingIncidence ) ) {
00071       existingIncidence = 0;
00072     }
00073     if ( !existingIncidence ) {
00074       const Incidence::List list = calendar->incidences();
00075       for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00076         if ( (*it)->schedulingID() == uid && isMyCalendarIncidence( calendar, *it ) ) {
00077           existingIncidence = *it;
00078           break;
00079         }
00080       }
00081     }
00082   }
00083   return existingIncidence;
00084 }
00085 
00086 bool CalHelper::usingGroupware( Calendar *calendar )
00087 {
00088   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00089   if ( !cal ) {
00090     return true;
00091   }
00092 
00093   CalendarResourceManager *manager = cal->resourceManager();
00094   CalendarResourceManager::Iterator it;
00095   for ( it = manager->begin(); it != manager->end(); ++it ) {
00096     QString res = (*it)->type();
00097     if ( res == "imap" ) {
00098       return true;
00099     }
00100   }
00101   return false;
00102 }
00103 
00104 bool CalHelper::hasMyWritableEventsFolders( const QString &family )
00105 {
00106   QString myfamily = family;
00107   if ( family.isEmpty() ) {
00108     myfamily = "calendar";
00109   }
00110 
00111   CalendarResourceManager manager( myfamily );
00112   manager.readConfig();
00113 
00114   CalendarResourceManager::ActiveIterator it;
00115   for ( it=manager.activeBegin(); it != manager.activeEnd(); ++it ) {
00116     if ( (*it)->readOnly() ) {
00117       continue;
00118     }
00119 
00120     const QStringList subResources = (*it)->subresources();
00121     if ( subResources.isEmpty() ) {
00122       return true;
00123     }
00124 
00125     QStringList::ConstIterator subIt;
00126     for ( subIt=subResources.begin(); subIt != subResources.end(); ++subIt ) {
00127       if ( !(*it)->subresourceActive( (*subIt) ) ) {
00128         continue;
00129       }
00130       if ( (*it)->type() == "imap" || (*it)->type() == "kolab" ) {
00131         if ( (*it)->subresourceType( ( *subIt ) ) == "todo" ||
00132              (*it)->subresourceType( ( *subIt ) ) == "journal" ||
00133              !(*subIt).contains( "/.INBOX.directory/" ) ) {
00134           continue;
00135         }
00136       }
00137       return true;
00138     }
00139   }
00140   return false;
00141 }
00142 
00143 ResourceCalendar *CalHelper::incResourceCalendar( Calendar *calendar, Incidence *incidence )
00144 {
00145   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00146   if ( !cal || !incidence ) {
00147     return 0;
00148   }
00149 
00150   return cal->resource( incidence );
00151 }
00152 
00153 QPair<ResourceCalendar *, QString> CalHelper::incSubResourceCalendar( Calendar *calendar,
00154                                                                       Incidence *incidence )
00155 {
00156   QPair<ResourceCalendar *, QString> p( 0, QString() );
00157 
00158   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00159   if ( !cal || !incidence ) {
00160     return p;
00161   }
00162 
00163   ResourceCalendar *res = cal->resource( incidence );
00164 
00165   QString subRes;
00166   if ( res && res->canHaveSubresources() ) {
00167     subRes = res->subresourceIdentifier( incidence );
00168   }
00169   p = qMakePair( res, subRes );
00170   return p;
00171 }
00172 
00173 bool CalHelper::incOrganizerOwnsCalendar( Calendar *calendar, Incidence *incidence )
00174 {
00175   if ( !calendar || !incidence ) {
00176     return false;
00177   }
00178 
00179   QPair<ResourceCalendar *, QString> p =  incSubResourceCalendar( calendar, incidence );
00180   ResourceCalendar *res = p.first;
00181   QString subRes = p.second;
00182 
00183   if ( !res ) {
00184     return false;
00185   }
00186 
00187   QString orgEmail;
00188   QString orgName;
00189   KPIM::getNameAndMail( incidence->organizer().email(), orgName, orgEmail );
00190   if ( KPIM::isValidEmailAddress( orgEmail ) != KPIM::AddressOk ) {
00191     return false;
00192   }
00193 
00194   // first determine if I am the organizer.
00195   bool iam = false;
00196   KEMailSettings settings;
00197   QStringList profiles = settings.profiles();
00198   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
00199     settings.setProfile( *it );
00200     if ( settings.getSetting( KEMailSettings::EmailAddress ) == orgEmail ) {
00201       iam = true;
00202       break;
00203     }
00204   }
00205 
00206   // if I am the organizer and the incidence is in my calendar
00207   if ( iam && isMyCalendarIncidence( calendar, incidence ) ) {
00208     // then we have a winner.
00209     return true;
00210   }
00211 
00212   // The organizer is not me.
00213 
00214   if ( ( res->type() == "imap" || res->type() == "kolab" ) && !subRes.isEmpty() ) {
00215     // KOLAB SPECIFIC:
00216     // Check if the organizer owns this calendar by looking at the
00217     // username part of the email encoded in the subresource name,
00218     // which is of the form "/.../.user.directory/.<name>.directory/..."
00219     const int atChar = orgEmail.findRev( '@' );
00220     const QString name = orgEmail.left( atChar );
00221     QString kolabFolder = "/.user.directory/." + name + ".directory/";
00222     if ( subRes.contains( kolabFolder ) ) {
00223       return true;
00224     }
00225     // if that fails, maybe the first name of the organizer email name will work
00226     const int dotChar = name.find( '.' );
00227     if ( dotChar > 0 ) {
00228       const QString firstName = name.left( dotChar );
00229       kolabFolder = "/.user.directory/." + firstName + ".directory/";
00230       if ( subRes.contains( kolabFolder ) ) {
00231         return true;
00232       }
00233     }
00234   }
00235 
00236   // TODO: support other resource types
00237 
00238   return false;
00239 }
KDE Home | KDE Accessibility Home | Description of Access Keys