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                                             CalendarResourceManager *manager )
00106 {
00107   Q_ASSERT( manager );
00108   QString myfamily = family;
00109   if ( family.isEmpty() ) {
00110     myfamily = "calendar";
00111   }
00112 
00113   CalendarResourceManager::ActiveIterator it;
00114 
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 bool CalHelper::hasWritableEventsFolders( const QString &family,
00144                                           CalendarResourceManager *manager )
00145 {
00146   Q_ASSERT( manager );
00147   QString myfamily = family;
00148   if ( family.isEmpty() ) {
00149     myfamily = "calendar";
00150   }
00151 
00152   CalendarResourceManager::ActiveIterator it;
00153 
00154   for ( it=manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00155     if ( (*it)->readOnly() ) {
00156       continue;
00157     }
00158 
00159     const QStringList subResources = (*it)->subresources();
00160     if ( subResources.isEmpty() ) {
00161       return true;
00162     }
00163 
00164     QStringList::ConstIterator subIt;
00165     for ( subIt=subResources.begin(); subIt != subResources.end(); ++subIt ) {
00166       if ( !(*it)->subresourceActive( (*subIt) ) ) {
00167         continue;
00168       }
00169       if ( (*it)->type() == "imap" || (*it)->type() == "kolab" ) {
00170         if ( (*it)->subresourceType( ( *subIt ) ) == "todo" ||
00171              (*it)->subresourceType( ( *subIt ) ) == "journal" ||
00172              !(*it)->subresourceWritable( *subIt ) ) {
00173           continue;
00174         }
00175       }
00176       return true;
00177     }
00178   }
00179   return false;
00180 }
00181 
00182 ResourceCalendar *CalHelper::incResourceCalendar( Calendar *calendar, Incidence *incidence )
00183 {
00184   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00185   if ( !cal || !incidence ) {
00186     return 0;
00187   }
00188 
00189   return cal->resource( incidence );
00190 }
00191 
00192 QPair<ResourceCalendar *, QString> CalHelper::incSubResourceCalendar( Calendar *calendar,
00193                                                                       Incidence *incidence )
00194 {
00195   QPair<ResourceCalendar *, QString> p( 0, QString() );
00196 
00197   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
00198   if ( !cal || !incidence ) {
00199     return p;
00200   }
00201 
00202   ResourceCalendar *res = cal->resource( incidence );
00203 
00204   QString subRes;
00205   if ( res && res->canHaveSubresources() ) {
00206     subRes = res->subresourceIdentifier( incidence );
00207   }
00208   p = qMakePair( res, subRes );
00209   return p;
00210 }
00211 
00212 bool CalHelper::incOrganizerOwnsCalendar( Calendar *calendar, Incidence *incidence )
00213 {
00214   if ( !calendar || !incidence ) {
00215     return false;
00216   }
00217 
00218   QPair<ResourceCalendar *, QString> p =  incSubResourceCalendar( calendar, incidence );
00219   ResourceCalendar *res = p.first;
00220   QString subRes = p.second;
00221 
00222   if ( !res ) {
00223     return false;
00224   }
00225 
00226   QString orgEmail;
00227   QString orgName;
00228   KPIM::getNameAndMail( incidence->organizer().email(), orgName, orgEmail );
00229   if ( KPIM::isValidEmailAddress( orgEmail ) != KPIM::AddressOk ) {
00230     return false;
00231   }
00232 
00233   // first determine if I am the organizer.
00234   bool iam = false;
00235   KEMailSettings settings;
00236   QStringList profiles = settings.profiles();
00237   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
00238     settings.setProfile( *it );
00239     if ( settings.getSetting( KEMailSettings::EmailAddress ) == orgEmail ) {
00240       iam = true;
00241       break;
00242     }
00243   }
00244 
00245   // if I am the organizer and the incidence is in my calendar
00246   if ( iam && isMyCalendarIncidence( calendar, incidence ) ) {
00247     // then we have a winner.
00248     return true;
00249   }
00250 
00251   // The organizer is not me.
00252 
00253   if ( ( res->type() == "imap" || res->type() == "kolab" ) && !subRes.isEmpty() ) {
00254     // KOLAB SPECIFIC:
00255     // Check if the organizer owns this calendar by looking at the
00256     // username part of the email encoded in the subresource name,
00257     // which is of the form "/.../.user.directory/.<name>.directory/..."
00258     const int atChar = orgEmail.findRev( '@' );
00259     const QString name = orgEmail.left( atChar );
00260     QString kolabFolder = "/.user.directory/." + name + ".directory/";
00261     if ( subRes.contains( kolabFolder ) ) {
00262       return true;
00263     }
00264     // if that fails, maybe the first name of the organizer email name will work
00265     const int dotChar = name.find( '.' );
00266     if ( dotChar > 0 ) {
00267       const QString firstName = name.left( dotChar );
00268       kolabFolder = "/.user.directory/." + firstName + ".directory/";
00269       if ( subRes.contains( kolabFolder ) ) {
00270         return true;
00271       }
00272     }
00273   }
00274 
00275   // TODO: support other resource types
00276 
00277   return false;
00278 }
KDE Home | KDE Accessibility Home | Description of Access Keys