libkcal

incidenceformatter.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006     Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "incidenceformatter.h"
00025 
00026 #include <libkcal/attachment.h>
00027 #include <libkcal/event.h>
00028 #include <libkcal/todo.h>
00029 #include <libkcal/journal.h>
00030 #include <libkcal/calendar.h>
00031 #include <libkcal/calendarlocal.h>
00032 #include <libkcal/icalformat.h>
00033 #include <libkcal/freebusy.h>
00034 #include <libkcal/calendarresources.h>
00035 
00036 #include <libemailfunctions/email.h>
00037 
00038 #include <ktnef/ktnefparser.h>
00039 #include <ktnef/ktnefmessage.h>
00040 #include <ktnef/ktnefdefs.h>
00041 #include <kabc/phonenumber.h>
00042 #include <kabc/vcardconverter.h>
00043 #include <kabc/stdaddressbook.h>
00044 
00045 #include <kapplication.h>
00046 #include <kemailsettings.h>
00047 
00048 #include <klocale.h>
00049 #include <kglobal.h>
00050 #include <kiconloader.h>
00051 #include <kcalendarsystem.h>
00052 #include <kmimetype.h>
00053 
00054 #include <qbuffer.h>
00055 #include <qstylesheet.h>
00056 #include <qdatetime.h>
00057 #include <qregexp.h>
00058 
00059 #include <time.h>
00060 
00061 using namespace KCal;
00062 
00063 /*******************
00064  *  General helpers
00065  *******************/
00066 
00067 static QString htmlAddLink( const QString &ref, const QString &text,
00068                              bool newline = true )
00069 {
00070   QString tmpStr( "<a href=\"" + ref + "\">" + text + "</a>" );
00071   if ( newline ) tmpStr += "\n";
00072   return tmpStr;
00073 }
00074 
00075 static QString htmlAddTag( const QString & tag, const QString & text )
00076 {
00077   int numLineBreaks = text.contains( "\n" );
00078   QString str = "<" + tag + ">";
00079   QString tmpText = text;
00080   QString tmpStr = str;
00081   if( numLineBreaks >= 0 ) {
00082     if ( numLineBreaks > 0) {
00083       int pos = 0;
00084       QString tmp;
00085       for( int i = 0; i <= numLineBreaks; i++ ) {
00086         pos = tmpText.find( "\n" );
00087         tmp = tmpText.left( pos );
00088         tmpText = tmpText.right( tmpText.length() - pos - 1 );
00089         tmpStr += tmp + "<br>";
00090       }
00091     } else {
00092       tmpStr += tmpText;
00093     }
00094   }
00095   tmpStr += "</" + tag + ">";
00096   return tmpStr;
00097 }
00098 
00099 static bool iamAttendee( Attendee *attendee )
00100 {
00101   // Check if I'm this attendee
00102 
00103   bool iam = false;
00104   KEMailSettings settings;
00105   QStringList profiles = settings.profiles();
00106   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
00107     settings.setProfile( *it );
00108     if ( settings.getSetting( KEMailSettings::EmailAddress ) == attendee->email() ) {
00109       iam = true;
00110       break;
00111     }
00112   }
00113   return iam;
00114 }
00115 
00116 static bool iamOrganizer( Incidence *incidence )
00117 {
00118   // Check if I'm the organizer for this incidence
00119 
00120   if ( !incidence ) {
00121     return false;
00122   }
00123 
00124   bool iam = false;
00125   KEMailSettings settings;
00126   QStringList profiles = settings.profiles();
00127   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
00128     settings.setProfile( *it );
00129     if ( settings.getSetting( KEMailSettings::EmailAddress ) == incidence->organizer().email() ) {
00130       iam = true;
00131       break;
00132     }
00133   }
00134   return iam;
00135 }
00136 
00137 static bool senderIsOrganizer( Incidence *incidence, const QString &sender )
00138 {
00139   // Check if the specified sender is the organizer
00140 
00141   if ( !incidence || sender.isEmpty() ) {
00142     return true;
00143   }
00144 
00145   bool isorg = true;
00146   QString senderName, senderEmail;
00147   if ( KPIM::getNameAndMail( sender, senderName, senderEmail ) ) {
00148     // for this heuristic, we say the sender is the organizer if either the name or the email match.
00149     if ( incidence->organizer().email() != senderEmail &&
00150          incidence->organizer().name() != senderName ) {
00151       isorg = false;
00152     }
00153   }
00154   return isorg;
00155 }
00156 
00157 static QString firstAttendeeName( Incidence *incidence, const QString &defName )
00158 {
00159   QString name;
00160   if ( !incidence ) {
00161     return name;
00162   }
00163 
00164   Attendee::List attendees = incidence->attendees();
00165   if( attendees.count() > 0 ) {
00166     Attendee *attendee = *attendees.begin();
00167     name = attendee->name();
00168     if ( name.isEmpty() ) {
00169       name = attendee->email();
00170     }
00171     if ( name.isEmpty() ) {
00172       name = defName;
00173     }
00174   }
00175   return name;
00176 }
00177 
00178 /*******************************************************************
00179  *  Helper functions for the extensive display (display viewer)
00180  *******************************************************************/
00181 
00182 static QString displayViewLinkPerson( const QString& email, QString name, QString uid )
00183 {
00184   // Make the search, if there is an email address to search on,
00185   // and either name or uid is missing
00186   if ( !email.isEmpty() && ( name.isEmpty() || uid.isEmpty() ) ) {
00187     KABC::AddressBook *add_book = KABC::StdAddressBook::self( true );
00188     KABC::Addressee::List addressList = add_book->findByEmail( email );
00189     if ( !addressList.isEmpty() ) {
00190       KABC::Addressee o = addressList.first();
00191       if ( !o.isEmpty() && addressList.size() < 2 ) {
00192         if ( name.isEmpty() ) {
00193           // No name set, so use the one from the addressbook
00194           name = o.formattedName();
00195         }
00196         uid = o.uid();
00197       } else {
00198         // Email not found in the addressbook. Don't make a link
00199         uid = QString::null;
00200       }
00201     }
00202   }
00203 
00204   // Show the attendee
00205   QString tmpString;
00206   if ( !uid.isEmpty() ) {
00207     // There is a UID, so make a link to the addressbook
00208     if ( name.isEmpty() ) {
00209       // Use the email address for text
00210       tmpString += htmlAddLink( "uid:" + uid, email );
00211     } else {
00212       tmpString += htmlAddLink( "uid:" + uid, name );
00213     }
00214   } else {
00215     // No UID, just show some text
00216     tmpString += ( name.isEmpty() ? email : name );
00217   }
00218 
00219   // Make the mailto link
00220   if ( !email.isEmpty() ) {
00221     KURL mailto;
00222     mailto.setProtocol( "mailto" );
00223     mailto.setPath( email );
00224     const QString iconPath =
00225       KGlobal::iconLoader()->iconPath( "mail_new", KIcon::Small );
00226     tmpString += "&nbsp;" +
00227                  htmlAddLink( mailto.url(),
00228                               "<img valign=\"top\" src=\"" + iconPath + "\">" );
00229   }
00230 
00231   return tmpString;
00232 }
00233 
00234 static QString displayViewFormatAttendeeRoleList( Incidence *incidence, Attendee::Role role )
00235 {
00236   QString tmpStr;
00237   Attendee::List::ConstIterator it;
00238   Attendee::List attendees = incidence->attendees();
00239 
00240   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00241     Attendee *a = *it;
00242     if ( a->role() != role ) {
00243       // skip this role
00244       continue;
00245     }
00246     if ( a->email() == incidence->organizer().email() ) {
00247       // skip attendee that is also the organizer
00248       continue;
00249     }
00250     tmpStr += displayViewLinkPerson( a->email(), a->name(), a->uid() );
00251     if ( !a->delegator().isEmpty() ) {
00252       tmpStr += i18n(" (delegated by %1)" ).arg( a->delegator() );
00253     }
00254     if ( !a->delegate().isEmpty() ) {
00255       tmpStr += i18n(" (delegated to %1)" ).arg( a->delegate() );
00256     }
00257     tmpStr += "<br>";
00258   }
00259   if ( tmpStr.endsWith( "<br>" ) ) {
00260     tmpStr.truncate( tmpStr.length() - 4 );
00261   }
00262   return tmpStr;
00263 }
00264 
00265 static QString displayViewFormatAttendees( Incidence *incidence )
00266 {
00267   QString tmpStr, str;
00268 
00269   // Add organizer link
00270   int attendeeCount = incidence->attendees().count();
00271   if ( attendeeCount > 1 ||
00272        ( attendeeCount == 1 &&
00273          incidence->organizer().email() != incidence->attendees().first()->email() ) ) {
00274     tmpStr += "<tr>";
00275     tmpStr += "<td><b>" + i18n( "Organizer:" ) + "</b></td>";
00276     tmpStr += "<td>" +
00277               displayViewLinkPerson( incidence->organizer().email(),
00278                                      incidence->organizer().name(),
00279                                      QString::null ) +
00280               "</td>";
00281     tmpStr += "</tr>";
00282   }
00283 
00284   // Add "chair"
00285   str = displayViewFormatAttendeeRoleList( incidence, Attendee::Chair );
00286   if ( !str.isEmpty() ) {
00287     tmpStr += "<tr>";
00288     tmpStr += "<td><b>" + i18n( "Chair:" ) + "</b></td>";
00289     tmpStr += "<td>" + str + "</td>";
00290     tmpStr += "</tr>";
00291   }
00292 
00293   // Add required participants
00294   str = displayViewFormatAttendeeRoleList( incidence, Attendee::ReqParticipant );
00295   if ( !str.isEmpty() ) {
00296     tmpStr += "<tr>";
00297     tmpStr += "<td><b>" + i18n( "Required Participants:" ) + "</b></td>";
00298     tmpStr += "<td>" + str + "</td>";
00299     tmpStr += "</tr>";
00300   }
00301 
00302   // Add optional participants
00303   str = displayViewFormatAttendeeRoleList( incidence, Attendee::OptParticipant );
00304   if ( !str.isEmpty() ) {
00305     tmpStr += "<tr>";
00306     tmpStr += "<td><b>" + i18n( "Optional Participants:" ) + "</b></td>";
00307     tmpStr += "<td>" + str + "</td>";
00308     tmpStr += "</tr>";
00309   }
00310 
00311   // Add observers
00312   str = displayViewFormatAttendeeRoleList( incidence, Attendee::NonParticipant );
00313   if ( !str.isEmpty() ) {
00314     tmpStr += "<tr>";
00315     tmpStr += "<td><b>" + i18n( "Observers:" ) + "</b></td>";
00316     tmpStr += "<td>" + str + "</td>";
00317     tmpStr += "</tr>";
00318   }
00319 
00320   return tmpStr;
00321 }
00322 
00323 static QString displayViewFormatAttachments( Incidence *incidence )
00324 {
00325   QString tmpStr;
00326   Attachment::List as = incidence->attachments();
00327   Attachment::List::ConstIterator it;
00328   uint count = 0;
00329   for( it = as.begin(); it != as.end(); ++it ) {
00330     count++;
00331     if ( (*it)->isUri() ) {
00332       QString name;
00333       if ( (*it)->uri().startsWith( "kmail:" ) ) {
00334         name = i18n( "Show mail" );
00335       } else {
00336         name = (*it)->label();
00337       }
00338       tmpStr += htmlAddLink( (*it)->uri(), name );
00339     } else {
00340       tmpStr += (*it)->label();
00341     }
00342     if ( count < as.count() ) {
00343       tmpStr += "<br>";
00344     }
00345   }
00346   return tmpStr;
00347 }
00348 
00349 static QString displayViewFormatCategories( Incidence *incidence )
00350 {
00351   // We do not use Incidence::categoriesStr() since it does not have whitespace
00352   return incidence->categories().join( ", " );
00353 }
00354 
00355 static QString displayViewFormatCreationDate( Incidence *incidence )
00356 {
00357   return i18n( "Creation date: %1" ).
00358     arg( IncidenceFormatter::dateTimeToString( incidence->created(), false, true ) );
00359 }
00360 
00361 static QString displayViewFormatBirthday( Event *event )
00362 {
00363   if ( !event ) {
00364     return  QString::null;
00365   }
00366   if ( event->customProperty("KABC","BIRTHDAY") != "YES" ) {
00367     return QString::null;
00368   }
00369 
00370   QString uid = event->customProperty("KABC","UID-1");
00371   QString name = event->customProperty("KABC","NAME-1");
00372   QString email= event->customProperty("KABC","EMAIL-1");
00373 
00374   QString tmpStr = displayViewLinkPerson( email, name, uid );
00375 
00376   if ( event->customProperty( "KABC", "ANNIVERSARY") == "YES" ) {
00377     uid = event->customProperty("KABC","UID-2");
00378     name = event->customProperty("KABC","NAME-2");
00379     email= event->customProperty("KABC","EMAIL-2");
00380     tmpStr += "<br>";
00381     tmpStr += displayViewLinkPerson( email, name, uid );
00382   }
00383 
00384   return tmpStr;
00385 }
00386 
00387 static QString displayViewFormatHeader( Incidence *incidence )
00388 {
00389   QString tmpStr = "<table><tr>";
00390 
00391   // show icons
00392   {
00393     tmpStr += "<td>";
00394 
00395     if ( incidence->type() == "Event" ) {
00396       QString iconPath;
00397       if ( incidence->customProperty( "KABC", "BIRTHDAY" ) == "YES" ) {
00398         if ( incidence->customProperty( "KABC", "ANNIVERSARY" ) == "YES" ) {
00399           iconPath =
00400             KGlobal::iconLoader()->iconPath( "calendaranniversary", KIcon::Small );
00401         } else {
00402           iconPath = KGlobal::iconLoader()->iconPath( "calendarbirthday", KIcon::Small );
00403         }
00404       } else {
00405         iconPath = KGlobal::iconLoader()->iconPath( "appointment", KIcon::Small );
00406       }
00407       tmpStr += "<img valign=\"top\" src=\"" + iconPath + "\">";
00408     }
00409     if ( incidence->type() == "Todo" ) {
00410       tmpStr += "<img valign=\"top\" src=\"" +
00411                 KGlobal::iconLoader()->iconPath( "todo", KIcon::Small ) +
00412                 "\">";
00413     }
00414     if ( incidence->type() == "Journal" ) {
00415       tmpStr += "<img valign=\"top\" src=\"" +
00416                 KGlobal::iconLoader()->iconPath( "journal", KIcon::Small ) +
00417                 "\">";
00418     }
00419     if ( incidence->isAlarmEnabled() ) {
00420       tmpStr += "<img valign=\"top\" src=\"" +
00421                 KGlobal::iconLoader()->iconPath( "bell", KIcon::Small ) +
00422                 "\">";
00423     }
00424     if ( incidence->doesRecur() ) {
00425       tmpStr += "<img valign=\"top\" src=\"" +
00426                 KGlobal::iconLoader()->iconPath( "recur", KIcon::Small ) +
00427                 "\">";
00428     }
00429     if ( incidence->isReadOnly() ) {
00430       tmpStr += "<img valign=\"top\" src=\"" +
00431                 KGlobal::iconLoader()->iconPath( "readonlyevent", KIcon::Small ) +
00432                 "\">";
00433     }
00434 
00435     tmpStr += "</td>";
00436   }
00437 
00438   tmpStr += "<td>";
00439   tmpStr += "<b><u>" + incidence->summary() + "</u></b>";
00440   tmpStr += "</td>";
00441 
00442   tmpStr += "</tr></table>";
00443 
00444   return tmpStr;
00445 }
00446 
00447 static QString displayViewFormatEvent( Calendar *calendar, Event *event,
00448                                        const QDate &date )
00449 {
00450   if ( !event ) {
00451     return QString::null;
00452   }
00453 
00454   QString tmpStr = displayViewFormatHeader( event );
00455 
00456   tmpStr += "<table>";
00457   tmpStr += "<col width=\"25%\"/>";
00458   tmpStr += "<col width=\"75%\"/>";
00459 
00460   if ( calendar ) {
00461     QString calStr = IncidenceFormatter::resourceString( calendar, event );
00462     if ( !calStr.isEmpty() ) {
00463       tmpStr += "<tr>";
00464       tmpStr += "<td><b>" + i18n( "Calendar:" ) + "</b></td>";
00465       tmpStr += "<td>" + calStr + "</td>";
00466       tmpStr += "</tr>";
00467     }
00468   }
00469 
00470   if ( !event->location().isEmpty() ) {
00471     tmpStr += "<tr>";
00472     tmpStr += "<td><b>" + i18n( "Location:" ) + "</b></td>";
00473     tmpStr += "<td>" + event->location() + "</td>";
00474     tmpStr += "</tr>";
00475   }
00476 
00477   QDateTime startDt = event->dtStart();
00478   QDateTime endDt = event->dtEnd();
00479   if ( event->doesRecur() ) {
00480     if ( date.isValid() ) {
00481       QDateTime dt( date, QTime( 0, 0, 0 ) );
00482       int diffDays = startDt.daysTo( dt );
00483       dt = dt.addSecs( -1 );
00484       startDt.setDate( event->recurrence()->getNextDateTime( dt ).date() );
00485       if ( event->hasEndDate() ) {
00486         endDt = endDt.addDays( diffDays );
00487         if ( startDt > endDt ) {
00488           startDt.setDate( event->recurrence()->getPreviousDateTime( dt ).date() );
00489           endDt = startDt.addDays( event->dtStart().daysTo( event->dtEnd() ) );
00490         }
00491       }
00492     }
00493   }
00494 
00495   tmpStr += "<tr>";
00496   if ( event->doesFloat() ) {
00497     if ( event->isMultiDay() ) {
00498       tmpStr += "<td><b>" + i18n( "Date:" ) + "</b></td>";
00499       tmpStr += "<td>" +
00500                 i18n("<beginDate> - <endDate>","%1 - %2").
00501                 arg( IncidenceFormatter::dateToString( startDt, false ) ).
00502                 arg( IncidenceFormatter::dateToString( endDt, false ) ) +
00503                 "</td>";
00504     } else {
00505       tmpStr += "<td><b>" + i18n( "Date:" ) + "</b></td>";
00506       tmpStr += "<td>" +
00507                 i18n("date as string","%1").
00508                 arg( IncidenceFormatter::dateToString( startDt, false ) ) +
00509                 "</td>";
00510     }
00511   } else {
00512     if ( event->isMultiDay() ) {
00513       tmpStr += "<td><b>" + i18n( "Date:" ) + "</b></td>";
00514       tmpStr += "<td>" +
00515                 i18n("<beginDate> - <endDate>","%1 - %2").
00516                 arg( IncidenceFormatter::dateToString( startDt, false ) ).
00517                 arg( IncidenceFormatter::dateToString( endDt, false ) ) +
00518                 "</td>";
00519     } else {
00520       tmpStr += "<td><b>" + i18n( "Date:" ) + "</b></td>";
00521       tmpStr += "<td>" +
00522                 i18n("date as string","%1").
00523                 arg( IncidenceFormatter::dateToString( startDt, false ) ) +
00524                 "</td>";
00525 
00526       tmpStr += "</tr><tr>";
00527       tmpStr += "<td><b>" + i18n( "Time:" ) + "</b></td>";
00528       if ( event->hasEndDate() && startDt != endDt ) {
00529         tmpStr += "<td>" +
00530                   i18n("<beginTime> - <endTime>","%1 - %2").
00531                   arg( IncidenceFormatter::timeToString( startDt, true ) ).
00532                   arg( IncidenceFormatter::timeToString( endDt, true ) ) +
00533                   "</td>";
00534       } else {
00535         tmpStr += "<td>" +
00536                   IncidenceFormatter::timeToString( startDt, true ) +
00537                   "</td>";
00538       }
00539     }
00540   }
00541   tmpStr += "</tr>";
00542 
00543   QString durStr = IncidenceFormatter::durationString( event );
00544   if ( !durStr.isEmpty() ) {
00545     tmpStr += "<tr>";
00546     tmpStr += "<td><b>" + i18n( "Duration:" ) + "</b></td>";
00547     tmpStr += "<td>" + durStr + "</td>";
00548     tmpStr += "</tr>";
00549   }
00550 
00551   if ( event->doesRecur() ) {
00552     tmpStr += "<tr>";
00553     tmpStr += "<td><b>" + i18n( "Recurrence:" ) + "</b></td>";
00554     tmpStr += "<td>" +
00555               IncidenceFormatter::recurrenceString( event ) +
00556               "</td>";
00557     tmpStr += "</tr>";
00558   }
00559 
00560   if ( event->customProperty("KABC","BIRTHDAY")== "YES" ) {
00561     tmpStr += "<tr>";
00562     if ( event->customProperty( "KABC", "ANNIVERSARY" ) == "YES" ) {
00563       tmpStr += "<td><b>" + i18n( "Anniversary:" ) + "</b></td>";
00564     } else {
00565       tmpStr += "<td><b>" + i18n( "Birthday:" ) + "</b></td>";
00566     }
00567     tmpStr += "<td>" + displayViewFormatBirthday( event ) + "</td>";
00568     tmpStr += "</tr>";
00569     tmpStr += "</table>";
00570     return tmpStr;
00571   }
00572 
00573   if ( !event->description().isEmpty() ) {
00574     tmpStr += "<tr>";
00575     tmpStr += "<td><b>" + i18n( "Description:" ) + "</b></td>";
00576     tmpStr += "<td>" + event->description() + "</td>";
00577     tmpStr += "</tr>";
00578   }
00579 
00580   // TODO: print comments?
00581 
00582   int reminderCount = event->alarms().count();
00583   if ( reminderCount > 0 && event->isAlarmEnabled() ) {
00584     tmpStr += "<tr>";
00585     tmpStr += "<td><b>" +
00586               i18n( "Reminder:", "%n Reminders:", reminderCount ) +
00587               "</b></td>";
00588     tmpStr += "<td>" + IncidenceFormatter::reminderStringList( event ).join( "<br>" ) + "</td>";
00589     tmpStr += "</tr>";
00590   }
00591 
00592   tmpStr += displayViewFormatAttendees( event );
00593 
00594   int categoryCount = event->categories().count();
00595   if ( categoryCount > 0 ) {
00596     tmpStr += "<tr>";
00597     tmpStr += "<td><b>" +
00598               i18n( "Category:", "%n Categories:", categoryCount ) +
00599               "</b></td>";
00600     tmpStr += "<td>" + displayViewFormatCategories( event ) + "</td>";
00601     tmpStr += "</tr>";
00602   }
00603 
00604   int attachmentCount = event->attachments().count();
00605   if ( attachmentCount > 0 ) {
00606     tmpStr += "<tr>";
00607     tmpStr += "<td><b>" +
00608               i18n( "Attachment:", "%n Attachments:", attachmentCount ) +
00609               "</b></td>";
00610     tmpStr += "<td>" + displayViewFormatAttachments( event ) + "</td>";
00611     tmpStr += "</tr>";
00612   }
00613   tmpStr += "</table>";
00614 
00615   tmpStr += "<em>" + displayViewFormatCreationDate( event ) + "</em>";
00616 
00617   return tmpStr;
00618 }
00619 
00620 static QString displayViewFormatTodo( Calendar *calendar, Todo *todo,
00621                                       const QDate &date )
00622 {
00623   if ( !todo ) {
00624     return QString::null;
00625   }
00626 
00627   QString tmpStr = displayViewFormatHeader( todo );
00628 
00629   tmpStr += "<table>";
00630   tmpStr += "<col width=\"25%\"/>";
00631   tmpStr += "<col width=\"75%\"/>";
00632 
00633   if ( calendar ) {
00634     QString calStr = IncidenceFormatter::resourceString( calendar, todo );
00635     if ( !calStr.isEmpty() ) {
00636       tmpStr += "<tr>";
00637       tmpStr += "<td><b>" + i18n( "Calendar:" ) + "</b></td>";
00638       tmpStr += "<td>" + calStr + "</td>";
00639       tmpStr += "</tr>";
00640     }
00641   }
00642 
00643   if ( !todo->location().isEmpty() ) {
00644     tmpStr += "<tr>";
00645     tmpStr += "<td><b>" + i18n( "Location:" ) + "</b></td>";
00646     tmpStr += "<td>" + todo->location() + "</td>";
00647     tmpStr += "</tr>";
00648   }
00649 
00650   if ( todo->hasStartDate() && todo->dtStart().isValid() ) {
00651     QDateTime startDt = todo->dtStart();
00652     if ( todo->doesRecur() ) {
00653       if ( date.isValid() ) {
00654         startDt.setDate( date );
00655       }
00656     }
00657     tmpStr += "<tr>";
00658     tmpStr += "<td><b>" + i18n( "Start:" ) + "</b></td>";
00659     tmpStr += "<td>" +
00660               IncidenceFormatter::dateTimeToString( startDt,
00661                                                     todo->doesFloat(), false ) +
00662               "</td>";
00663     tmpStr += "</tr>";
00664   }
00665 
00666   if ( todo->hasDueDate() && todo->dtDue().isValid() ) {
00667     QDateTime dueDt = todo->dtDue();
00668     if ( todo->doesRecur() ) {
00669       if ( date.isValid() ) {
00670         QDateTime dt( date, QTime( 0, 0, 0 ) );
00671         dt = dt.addSecs( -1 );
00672         dueDt.setDate( todo->recurrence()->getNextDateTime( dt ).date() );
00673       }
00674     }
00675     tmpStr += "<tr>";
00676     tmpStr += "<td><b>" + i18n( "Due:" ) + "</b></td>";
00677     tmpStr += "<td>" +
00678               IncidenceFormatter::dateTimeToString( dueDt,
00679                                                     todo->doesFloat(), false ) +
00680               "</td>";
00681     tmpStr += "</tr>";
00682   }
00683 
00684   QString durStr = IncidenceFormatter::durationString( todo );
00685   if ( !durStr.isEmpty() ) {
00686     tmpStr += "<tr>";
00687     tmpStr += "<td><b>" + i18n( "Duration:" ) + "</b></td>";
00688     tmpStr += "<td>" + durStr + "</td>";
00689     tmpStr += "</tr>";
00690   }
00691 
00692   if ( todo->doesRecur() ) {
00693     tmpStr += "<tr>";
00694     tmpStr += "<td><b>" + i18n( "Recurrence:" ) + "</b></td>";
00695     tmpStr += "<td>" +
00696               IncidenceFormatter::recurrenceString( todo ) +
00697               "</td>";
00698     tmpStr += "</tr>";
00699   }
00700 
00701   if ( !todo->description().isEmpty() ) {
00702     tmpStr += "<tr>";
00703     tmpStr += "<td><b>" + i18n( "Description:" ) + "</b></td>";
00704     tmpStr += "<td>" + todo->description() + "</td>";
00705     tmpStr += "</tr>";
00706   }
00707 
00708   // TODO: print comments?
00709 
00710   int reminderCount = todo->alarms().count();
00711   if ( reminderCount > 0 && todo->isAlarmEnabled() ) {
00712     tmpStr += "<tr>";
00713     tmpStr += "<td><b>" +
00714               i18n( "Reminder:", "%n Reminders:", reminderCount ) +
00715               "</b></td>";
00716     tmpStr += "<td>" + IncidenceFormatter::reminderStringList( todo ).join( "<br>" ) + "</td>";
00717     tmpStr += "</tr>";
00718   }
00719 
00720   tmpStr += displayViewFormatAttendees( todo );
00721 
00722   int categoryCount = todo->categories().count();
00723   if ( categoryCount > 0 ) {
00724     tmpStr += "<tr>";
00725     tmpStr += "<td><b>" +
00726               i18n( "Category:", "%n Categories:", categoryCount ) +
00727               "</b></td>";
00728     tmpStr += "<td>" + displayViewFormatCategories( todo ) + "</td>";
00729     tmpStr += "</tr>";
00730   }
00731 
00732   if ( todo->priority() > 0 ) {
00733     tmpStr += "<tr>";
00734     tmpStr += "<td><b>" + i18n( "Priority:" ) + "</b></td>";
00735     tmpStr += "<td>";
00736     tmpStr += QString::number( todo->priority() );
00737     tmpStr += "</td>";
00738     tmpStr += "</tr>";
00739   }
00740 
00741   tmpStr += "<tr>";
00742   if ( todo->isCompleted() ) {
00743     tmpStr += "<td><b>" + i18n( "Completed:" ) + "</b></td>";
00744     tmpStr += "<td>";
00745     tmpStr += todo->completedStr();
00746   } else {
00747     tmpStr += "<td><b>" + i18n( "Percent Done:" ) + "</b></td>";
00748     tmpStr += "<td>";
00749     tmpStr += i18n( "%1%" ).arg( todo->percentComplete() );
00750   }
00751   tmpStr += "</td>";
00752   tmpStr += "</tr>";
00753 
00754   int attachmentCount = todo->attachments().count();
00755   if ( attachmentCount > 0 ) {
00756     tmpStr += "<tr>";
00757     tmpStr += "<td><b>" +
00758               i18n( "Attachment:", "Attachments:", attachmentCount ) +
00759               "</b></td>";
00760     tmpStr += "<td>" + displayViewFormatAttachments( todo ) + "</td>";
00761     tmpStr += "</tr>";
00762   }
00763 
00764   tmpStr += "</table>";
00765 
00766   tmpStr += "<em>" + displayViewFormatCreationDate( todo ) + "</em>";
00767 
00768   return tmpStr;
00769 }
00770 
00771 static QString displayViewFormatJournal( Calendar *calendar, Journal *journal )
00772 {
00773   if ( !journal ) {
00774     return QString::null;
00775   }
00776 
00777   QString tmpStr = displayViewFormatHeader( journal );
00778 
00779   tmpStr += "<table>";
00780   tmpStr += "<col width=\"25%\"/>";
00781   tmpStr += "<col width=\"75%\"/>";
00782 
00783   if ( calendar ) {
00784     QString calStr = IncidenceFormatter::resourceString( calendar, journal );
00785     if ( !calStr.isEmpty() ) {
00786       tmpStr += "<tr>";
00787       tmpStr += "<td><b>" + i18n( "Calendar:" ) + "</b></td>";
00788       tmpStr += "<td>" + calStr + "</td>";
00789       tmpStr += "</tr>";
00790     }
00791   }
00792 
00793   tmpStr += "<tr>";
00794   tmpStr += "<td><b>" + i18n( "Date:" ) + "</b></td>";
00795   tmpStr += "<td>" +
00796             IncidenceFormatter::dateToString( journal->dtStart(), false ) +
00797             "</td>";
00798   tmpStr += "</tr>";
00799 
00800   if ( !journal->description().isEmpty() ) {
00801     tmpStr += "<tr>";
00802     tmpStr += "<td><b>" + i18n( "Description:" ) + "</b></td>";
00803     tmpStr += "<td>" + journal->description() + "</td>";
00804     tmpStr += "</tr>";
00805   }
00806 
00807   int categoryCount = journal->categories().count();
00808   if ( categoryCount > 0 ) {
00809     tmpStr += "<tr>";
00810     tmpStr += "<td><b>" +
00811               i18n( "Category:", "%n Categories:", categoryCount ) +
00812               "</b></td>";
00813     tmpStr += "<td>" + displayViewFormatCategories( journal ) + "</td>";
00814     tmpStr += "</tr>";
00815   }
00816   tmpStr += "</table>";
00817 
00818   tmpStr += "<em>" + displayViewFormatCreationDate( journal ) + "</em>";
00819 
00820   return tmpStr;
00821 }
00822 
00823 static QString displayViewFormatFreeBusy( Calendar * /*calendar*/, FreeBusy *fb )
00824 {
00825   if ( !fb ) {
00826     return QString::null;
00827   }
00828 
00829   QString tmpStr = htmlAddTag( "h2",
00830                                htmlAddTag( "b",
00831                                            i18n("Free/Busy information for %1").
00832                                            arg( fb->organizer().fullName() ) ) );
00833 
00834   tmpStr += htmlAddTag( "h4", i18n("Busy times in date range %1 - %2:").
00835                         arg( IncidenceFormatter::dateToString( fb->dtStart(), true ) ).
00836                         arg( IncidenceFormatter::dateToString( fb->dtEnd(), true ) ) );
00837 
00838   QValueList<Period> periods = fb->busyPeriods();
00839 
00840   QString text = htmlAddTag( "em", htmlAddTag( "b", i18n("Busy:") ) );
00841   QValueList<Period>::iterator it;
00842   for ( it = periods.begin(); it != periods.end(); ++it ) {
00843     Period per = *it;
00844     if ( per.hasDuration() ) {
00845       int dur = per.duration().asSeconds();
00846       QString cont;
00847       if ( dur >= 3600 ) {
00848         cont += i18n("1 hour ", "%n hours ", dur / 3600 );
00849         dur %= 3600;
00850       }
00851       if ( dur >= 60 ) {
00852         cont += i18n("1 minute ", "%n minutes ", dur / 60);
00853         dur %= 60;
00854       }
00855       if ( dur > 0 ) {
00856         cont += i18n("1 second", "%n seconds", dur);
00857       }
00858       text += i18n("startDate for duration", "%1 for %2").
00859               arg( IncidenceFormatter::dateTimeToString( per.start(), false, true ) ).
00860               arg( cont );
00861       text += "<br>";
00862     } else {
00863       if ( per.start().date() == per.end().date() ) {
00864         text += i18n("date, fromTime - toTime ", "%1, %2 - %3").
00865                 arg( IncidenceFormatter::dateToString( per.start().date(), true ) ).
00866                 arg( IncidenceFormatter::timeToString( per.start(), true ) ).
00867                 arg( IncidenceFormatter::timeToString( per.end(), true ) );
00868       } else {
00869         text += i18n("fromDateTime - toDateTime", "%1 - %2").
00870                 arg( IncidenceFormatter::dateTimeToString( per.start(), false, true ) ).
00871                 arg( IncidenceFormatter::dateTimeToString( per.end(), false, true ) );
00872       }
00873       text += "<br>";
00874     }
00875   }
00876   tmpStr += htmlAddTag( "p", text );
00877   return tmpStr;
00878 }
00879 
00880 class IncidenceFormatter::EventViewerVisitor : public IncidenceBase::Visitor
00881 {
00882   public:
00883     EventViewerVisitor()
00884       : mCalendar( 0 ), mResult( "" ) {}
00885 
00886     bool act( Calendar *calendar, IncidenceBase *incidence, const QDate &date )
00887     {
00888       mCalendar = calendar;
00889       mDate = date;
00890       mResult = "";
00891       return incidence->accept( *this );
00892     }
00893     QString result() const { return mResult; }
00894 
00895   protected:
00896     bool visit( Event *event )
00897     {
00898       mResult = displayViewFormatEvent( mCalendar, event, mDate );
00899       return !mResult.isEmpty();
00900     }
00901     bool visit( Todo *todo )
00902     {
00903       mResult = displayViewFormatTodo( mCalendar, todo, mDate );
00904       return !mResult.isEmpty();
00905     }
00906     bool visit( Journal *journal )
00907     {
00908       mResult = displayViewFormatJournal( mCalendar, journal );
00909       return !mResult.isEmpty();
00910     }
00911     bool visit( FreeBusy *fb )
00912     {
00913       mResult = displayViewFormatFreeBusy( mCalendar, fb );
00914       return !mResult.isEmpty();
00915     }
00916 
00917   protected:
00918     Calendar *mCalendar;
00919     QDate mDate;
00920     QString mResult;
00921 };
00922 
00923 QString IncidenceFormatter::extensiveDisplayString( IncidenceBase *incidence )
00924 {
00925   return extensiveDisplayStr( 0, incidence, QDate() );
00926 }
00927 
00928 QString IncidenceFormatter::extensiveDisplayStr( Calendar *calendar,
00929                                                  IncidenceBase *incidence,
00930                                                  const QDate &date )
00931 {
00932   if ( !incidence ) {
00933     return QString::null;
00934   }
00935 
00936   EventViewerVisitor v;
00937   if ( v.act( calendar, incidence, date ) ) {
00938     return v.result();
00939   } else {
00940     return QString::null;
00941   }
00942 }
00943 
00944 /***********************************************************************
00945  *  Helper functions for the body part formatter of kmail (Invitations)
00946  ***********************************************************************/
00947 
00948 static QString string2HTML( const QString& str )
00949 {
00950   return QStyleSheet::convertFromPlainText(str, QStyleSheetItem::WhiteSpaceNormal);
00951 }
00952 
00953 static QString cleanHtml( const QString &html )
00954 {
00955   QRegExp rx( "<body[^>]*>(.*)</body>" );
00956   rx.setCaseSensitive( false );
00957   rx.search( html );
00958   QString body = rx.cap( 1 );
00959 
00960   return QStyleSheet::escape( body.remove( QRegExp( "<[^>]*>" ) ).stripWhiteSpace() );
00961 }
00962 
00963 static QString eventStartTimeStr( Event *event )
00964 {
00965   QString tmp;
00966   if ( !event->doesFloat() ) {
00967     tmp = i18n( "%1: Start Date, %2: Start Time", "%1 %2" ).
00968           arg( IncidenceFormatter::dateToString( event->dtStart(), true ),
00969                IncidenceFormatter::timeToString( event->dtStart(), true ) );
00970   } else {
00971     tmp = i18n( "%1: Start Date", "%1 (all day)" ).
00972           arg( IncidenceFormatter::dateToString( event->dtStart(), true ) );
00973   }
00974   return tmp;
00975 }
00976 
00977 static QString eventEndTimeStr( Event *event )
00978 {
00979   QString tmp;
00980   if ( event->hasEndDate() && event->dtEnd().isValid() ) {
00981     if ( !event->doesFloat() ) {
00982       tmp = i18n( "%1: End Date, %2: End Time", "%1 %2" ).
00983             arg( IncidenceFormatter::dateToString( event->dtEnd(), true ),
00984                  IncidenceFormatter::timeToString( event->dtEnd(), true ) );
00985     } else {
00986       tmp = i18n( "%1: End Date", "%1 (all day)" ).
00987             arg( IncidenceFormatter::dateToString( event->dtEnd(), true ) );
00988     }
00989   }
00990   return tmp;
00991 }
00992 
00993 static QString invitationRow( const QString &cell1, const QString &cell2 )
00994 {
00995   return "<tr><td>" + cell1 + "</td><td>" + cell2 + "</td></tr>\n";
00996 }
00997 
00998 static Attendee *findDelegatedFromMyAttendee( Incidence *incidence )
00999 {
01000   // Return the first attendee that was delegated-from me
01001 
01002   Attendee *attendee = 0;
01003   if ( !incidence ) {
01004     return attendee;
01005   }
01006 
01007   KEMailSettings settings;
01008   QStringList profiles = settings.profiles();
01009   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
01010     settings.setProfile( *it );
01011 
01012     QString delegatorName, delegatorEmail;
01013     Attendee::List attendees = incidence->attendees();
01014     Attendee::List::ConstIterator it2;
01015     for ( it2 = attendees.begin(); it2 != attendees.end(); ++it2 ) {
01016       Attendee *a = *it2;
01017       KPIM::getNameAndMail( a->delegator(), delegatorName, delegatorEmail );
01018       if ( settings.getSetting( KEMailSettings::EmailAddress ) == delegatorEmail ) {
01019         attendee = a;
01020         break;
01021       }
01022     }
01023   }
01024   return attendee;
01025 }
01026 
01027 static Attendee *findMyAttendee( Incidence *incidence )
01028 {
01029   // Return the attendee for the incidence that is probably me
01030 
01031   Attendee *attendee = 0;
01032   if ( !incidence ) {
01033     return attendee;
01034   }
01035 
01036   KEMailSettings settings;
01037   QStringList profiles = settings.profiles();
01038   for( QStringList::Iterator it=profiles.begin(); it!=profiles.end(); ++it ) {
01039     settings.setProfile( *it );
01040 
01041     Attendee::List attendees = incidence->attendees();
01042     Attendee::List::ConstIterator it2;
01043     for ( it2 = attendees.begin(); it2 != attendees.end(); ++it2 ) {
01044       Attendee *a = *it2;
01045       if ( settings.getSetting( KEMailSettings::EmailAddress ) == a->email() ) {
01046         attendee = a;
01047         break;
01048       }
01049     }
01050   }
01051   return attendee;
01052 }
01053 
01054 static Attendee *findAttendee( Incidence *incidence, const QString &email )
01055 {
01056   // Search for an attendee by email address
01057 
01058   Attendee *attendee = 0;
01059   if ( !incidence ) {
01060     return attendee;
01061   }
01062 
01063   Attendee::List attendees = incidence->attendees();
01064   Attendee::List::ConstIterator it;
01065   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
01066     Attendee *a = *it;
01067     if ( email == a->email() ) {
01068       attendee = a;
01069       break;
01070     }
01071   }
01072   return attendee;
01073 }
01074 
01075 static bool rsvpRequested( Incidence *incidence )
01076 {
01077   if ( !incidence ) {
01078     return false;
01079   }
01080 
01081   //use a heuristic to determine if a response is requested.
01082 
01083   bool rsvp = true; // better send superfluously than not at all
01084   Attendee::List attendees = incidence->attendees();
01085   Attendee::List::ConstIterator it;
01086   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
01087     if ( it == attendees.begin() ) {
01088       rsvp = (*it)->RSVP(); // use what the first one has
01089     } else {
01090       if ( (*it)->RSVP() != rsvp ) {
01091         rsvp = true; // they differ, default
01092         break;
01093       }
01094     }
01095   }
01096   return rsvp;
01097 }
01098 
01099 static QString rsvpRequestedStr( bool rsvpRequested, const QString &role )
01100 {
01101   if ( rsvpRequested ) {
01102     if ( role.isEmpty() ) {
01103       return i18n( "Your response is requested" );
01104     } else {
01105       return i18n( "Your response as <b>%1</b> is requested" ).arg( role );
01106     }
01107   } else {
01108     if ( role.isEmpty() ) {
01109       return i18n( "No response is necessary" );
01110     } else {
01111       return i18n( "No response as <b>%1</b> is necessary" ).arg( role );
01112     }
01113   }
01114 }
01115 
01116 static QString myStatusStr( Incidence *incidence )
01117 {
01118   QString ret;
01119   Attendee *a = findMyAttendee( incidence );
01120   if ( a &&
01121        a->status() != Attendee::NeedsAction && a->status() != Attendee::Delegated ) {
01122     ret = i18n( "(<b>Note</b>: the Organizer preset your response to <b>%1</b>)" ).
01123           arg( Attendee::statusName( a->status() ) );
01124   }
01125   return ret;
01126 }
01127 
01128 static QString invitationPerson( const QString& email, QString name, QString uid )
01129 {
01130   // Make the search, if there is an email address to search on,
01131   // and either name or uid is missing
01132   if ( !email.isEmpty() && ( name.isEmpty() || uid.isEmpty() ) ) {
01133     KABC::AddressBook *add_book = KABC::StdAddressBook::self( true );
01134     KABC::Addressee::List addressList = add_book->findByEmail( email );
01135     if ( !addressList.isEmpty() ) {
01136       KABC::Addressee o = addressList.first();
01137       if ( !o.isEmpty() && addressList.size() < 2 ) {
01138         if ( name.isEmpty() ) {
01139           // No name set, so use the one from the addressbook
01140           name = o.formattedName();
01141         }
01142         uid = o.uid();
01143       } else {
01144         // Email not found in the addressbook. Don't make a link
01145         uid = QString::null;
01146       }
01147     }
01148   }
01149 
01150   // Show the attendee
01151   QString tmpString;
01152   if ( !uid.isEmpty() ) {
01153     // There is a UID, so make a link to the addressbook
01154     if ( name.isEmpty() ) {
01155       // Use the email address for text
01156       tmpString += htmlAddLink( "uid:" + uid, email );
01157     } else {
01158       tmpString += htmlAddLink( "uid:" + uid, name );
01159     }
01160   } else {
01161     // No UID, just show some text
01162     tmpString += ( name.isEmpty() ? email : name );
01163   }
01164   tmpString += '\n';
01165 
01166   // Make the mailto link
01167   if ( !email.isEmpty() ) {
01168     KCal::Person person( name, email );
01169     KURL mailto;
01170     mailto.setProtocol( "mailto" );
01171     mailto.setPath( person.fullName() );
01172     const QString iconPath =
01173       KGlobal::iconLoader()->iconPath( "mail_new", KIcon::Small );
01174     tmpString += "&nbsp;" +
01175                  htmlAddLink( mailto.url(), "<img src=\"" + iconPath + "\">" )
01176 ;
01177   }
01178   tmpString += "\n";
01179 
01180   return tmpString;
01181 }
01182 
01183 static QString invitationsDetailsIncidence( Incidence *incidence, bool noHtmlMode )
01184 {
01185   // if description and comment -> use both
01186   // if description, but no comment -> use the desc as the comment (and no desc)
01187   // if comment, but no description -> use the comment and no description
01188 
01189   QString html;
01190   QString descr;
01191   QStringList comments;
01192 
01193   if ( incidence->comments().isEmpty() ) {
01194     if ( !incidence->description().isEmpty() ) {
01195       // use description as comments
01196       if ( !QStyleSheet::mightBeRichText( incidence->description() ) ) {
01197         comments << string2HTML( incidence->description() );
01198       } else {
01199         comments << incidence->description();
01200         if ( noHtmlMode ) {
01201           comments[0] = cleanHtml( comments[0] );
01202         }
01203         comments[0] = htmlAddTag( "p", comments[0] );
01204       }
01205     }
01206     //else desc and comments are empty
01207   } else {
01208     // non-empty comments
01209     QStringList cl = incidence->comments();
01210     uint i = 0;
01211     for( QStringList::Iterator it=cl.begin(); it!=cl.end(); ++it ) {
01212       if ( !QStyleSheet::mightBeRichText( *it ) ) {
01213         comments.append( string2HTML( *it ) );
01214       } else {
01215         if ( noHtmlMode ) {
01216           comments.append( cleanHtml( "<body>" + (*it) + "</body>" ) );
01217         } else {
01218           comments.append( *it );
01219         }
01220       }
01221       i++;
01222     }
01223     if ( !incidence->description().isEmpty() ) {
01224       // use description too
01225       if ( !QStyleSheet::mightBeRichText( incidence->description() ) ) {
01226         descr = string2HTML( incidence->description() );
01227       } else {
01228         descr = incidence->description();
01229         if ( noHtmlMode ) {
01230           descr = cleanHtml( descr );
01231         }
01232         descr = htmlAddTag( "p", descr );
01233       }
01234     }
01235   }
01236 
01237   if( !descr.isEmpty() ) {
01238     html += "<p>";
01239     html += "<table border=\"0\" style=\"margin-top:4px;\">";
01240     html += "<tr><td><center>" +
01241             htmlAddTag( "u", i18n( "Description:" ) ) +
01242             "</center></td></tr>";
01243     html += "<tr><td>" + descr + "</td></tr>";
01244     html += "</table>";
01245   }
01246 
01247   if ( !comments.isEmpty() ) {
01248     html += "<p>";
01249     html += "<table border=\"0\" style=\"margin-top:4px;\">";
01250     html += "<tr><td><center>" +
01251             htmlAddTag( "u", i18n( "Comments:" ) ) +
01252             "</center></td></tr>";
01253     html += "<tr><td>";
01254     if ( comments.count() > 1 ) {
01255       html += "<ul>";
01256       for ( uint i=0; i < comments.count(); ++i ) {
01257         html += "<li>" + comments[i] + "</li>";
01258       }
01259       html += "</ul>";
01260     } else {
01261       html += comments[0];
01262     }
01263     html += "</td></tr>";
01264     html += "</table>";
01265   }
01266   return html;
01267 }
01268 
01269 static QString invitationDetailsEvent( Event* event, bool noHtmlMode )
01270 {
01271   // Invitation details are formatted into an HTML table
01272   if ( !event ) {
01273     return QString::null;
01274   }
01275 
01276   QString sSummary = i18n( "Summary unspecified" );
01277   if ( !event->summary().isEmpty() ) {
01278     if ( !QStyleSheet::mightBeRichText( event->summary() ) ) {
01279       sSummary = QStyleSheet::escape( event->summary() );
01280     } else {
01281       sSummary = event->summary();
01282       if ( noHtmlMode ) {
01283         sSummary = cleanHtml( sSummary );
01284       }
01285     }
01286   }
01287 
01288   QString sLocation = i18n( "Location unspecified" );
01289   if ( !event->location().isEmpty() ) {
01290     if ( !QStyleSheet::mightBeRichText( event->location() ) ) {
01291       sLocation = QStyleSheet::escape( event->location() );
01292     } else {
01293       sLocation = event->location();
01294       if ( noHtmlMode ) {
01295         sLocation = cleanHtml( sLocation );
01296       }
01297     }
01298   }
01299 
01300   QString dir = ( QApplication::reverseLayout() ? "rtl" : "ltr" );
01301   QString html = QString("<div dir=\"%1\">\n").arg(dir);
01302   html += "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">\n";
01303 
01304   // Invitation summary & location rows
01305   html += invitationRow( i18n( "What:" ), sSummary );
01306   html += invitationRow( i18n( "Where:" ), sLocation );
01307 
01308   // If a 1 day event
01309   if ( event->dtStart().date() == event->dtEnd().date() ) {
01310     html += invitationRow( i18n( "Date:" ),
01311                            IncidenceFormatter::dateToString( event->dtStart(), false ) );
01312     if ( !event->doesFloat() ) {
01313       html += invitationRow( i18n( "Time:" ),
01314                              IncidenceFormatter::timeToString( event->dtStart(), true ) +
01315                              " - " +
01316                              IncidenceFormatter::timeToString( event->dtEnd(), true ) );
01317     }
01318   } else {
01319     html += invitationRow( i18n( "Starting date of an event", "From:" ),
01320                            IncidenceFormatter::dateToString( event->dtStart(), false ) );
01321     if ( !event->doesFloat() ) {
01322       html += invitationRow( i18n( "Starting time of an event", "At:" ),
01323                              IncidenceFormatter::timeToString( event->dtStart(), true ) );
01324     }
01325     if ( event->hasEndDate() ) {
01326       html += invitationRow( i18n( "Ending date of an event", "To:" ),
01327                              IncidenceFormatter::dateToString( event->dtEnd(), false ) );
01328       if ( !event->doesFloat() ) {
01329         html += invitationRow( i18n( "Starting time of an event", "At:" ),
01330                                IncidenceFormatter::timeToString( event->dtEnd(), true ) );
01331       }
01332     } else {
01333       html += invitationRow( i18n( "Ending date of an event", "To:" ),
01334                              i18n( "no end date specified" ) );
01335     }
01336   }
01337 
01338   // Invitation Duration Row
01339   QString durStr = IncidenceFormatter::durationString( event );
01340   if ( !durStr.isEmpty() ) {
01341     html += invitationRow( i18n( "Duration:" ), durStr );
01342   }
01343 
01344   if ( event->doesRecur() )
01345     html += invitationRow( i18n( "Recurrence:" ), IncidenceFormatter::recurrenceString( event ) );
01346 
01347   html += "</table>\n";
01348   html += invitationsDetailsIncidence( event, noHtmlMode );
01349   html += "</div>\n";
01350 
01351   return html;
01352 }
01353 
01354 static QString invitationDetailsTodo( Todo *todo, bool noHtmlMode )
01355 {
01356   // Task details are formatted into an HTML table
01357   if ( !todo ) {
01358     return QString::null;
01359   }
01360 
01361   QString sSummary = i18n( "Summary unspecified" );
01362   if ( !todo->summary().isEmpty() ) {
01363     if ( !QStyleSheet::mightBeRichText( todo->summary() ) ) {
01364       sSummary = QStyleSheet::escape( todo->summary() );
01365     } else {
01366       sSummary = todo->summary();
01367       if ( noHtmlMode ) {
01368         sSummary = cleanHtml( sSummary );
01369       }
01370     }
01371   }
01372 
01373   QString sLocation = i18n( "Location unspecified" );
01374   if ( !todo->location().isEmpty() ) {
01375     if ( !QStyleSheet::mightBeRichText( todo->location() ) ) {
01376       sLocation = QStyleSheet::escape( todo->location() );
01377     } else {
01378       sLocation = todo->location();
01379       if ( noHtmlMode ) {
01380         sLocation = cleanHtml( sLocation );
01381       }
01382     }
01383   }
01384 
01385   QString dir = ( QApplication::reverseLayout() ? "rtl" : "ltr" );
01386   QString html = QString("<div dir=\"%1\">\n").arg(dir);
01387   html += "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">\n";
01388 
01389   // Invitation summary & location rows
01390   html += invitationRow( i18n( "What:" ), sSummary );
01391   html += invitationRow( i18n( "Where:" ), sLocation );
01392 
01393   if ( todo->hasStartDate() && todo->dtStart().isValid() ) {
01394     html += invitationRow( i18n( "Start Date:" ),
01395                            IncidenceFormatter::dateToString( todo->dtStart(), false ) );
01396   }
01397   if ( todo->hasDueDate() && todo->dtDue().isValid() ) {
01398     html += invitationRow( i18n( "Due Date:" ),
01399                            IncidenceFormatter::dateToString( todo->dtDue(), false ) );
01400     if ( !todo->doesFloat() ) {
01401       html += invitationRow( i18n( "Due Time:" ),
01402                              KGlobal::locale()->formatTime( todo->dtDue().time() ) );
01403     }
01404 
01405   } else {
01406     html += invitationRow( i18n( "Due Date:" ),
01407                            i18n( "Due Date: None", "None" ) );
01408   }
01409 
01410   html += "</table></div>\n";
01411   html += invitationsDetailsIncidence( todo, noHtmlMode );
01412 
01413   return html;
01414 }
01415 
01416 static QString invitationDetailsJournal( Journal *journal, bool noHtmlMode )
01417 {
01418   if ( !journal ) {
01419     return QString::null;
01420   }
01421 
01422   QString sSummary = i18n( "Summary unspecified" );
01423   QString sDescr = i18n( "Description unspecified" );
01424   if ( ! journal->summary().isEmpty() ) {
01425     sSummary = journal->summary();
01426     if ( noHtmlMode ) {
01427       sSummary = cleanHtml( sSummary );
01428     }
01429   }
01430   if ( ! journal->description().isEmpty() ) {
01431     sDescr = journal->description();
01432     if ( noHtmlMode ) {
01433       sDescr = cleanHtml( sDescr );
01434     }
01435   }
01436   QString html( "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">\n" );
01437   html += invitationRow( i18n( "Summary:" ), sSummary );
01438   html += invitationRow( i18n( "Date:" ),
01439                          IncidenceFormatter::dateToString( journal->dtStart(), false ) );
01440   html += invitationRow( i18n( "Description:" ), sDescr );
01441   html += "</table>\n";
01442   html += invitationsDetailsIncidence( journal, noHtmlMode );
01443 
01444   return html;
01445 }
01446 
01447 static QString invitationDetailsFreeBusy( FreeBusy *fb, bool /*noHtmlMode*/ )
01448 {
01449   if ( !fb )
01450     return QString::null;
01451   QString html( "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">\n" );
01452 
01453   html += invitationRow( i18n("Person:"), fb->organizer().fullName() );
01454   html += invitationRow( i18n("Start date:"),
01455                          IncidenceFormatter::dateToString( fb->dtStart(), true ) );
01456   html += invitationRow( i18n("End date:"),
01457                          KGlobal::locale()->formatDate( fb->dtEnd().date(), true ) );
01458   html += "<tr><td colspan=2><hr></td></tr>\n";
01459   html += "<tr><td colspan=2>Busy periods given in this free/busy object:</td></tr>\n";
01460 
01461   QValueList<Period> periods = fb->busyPeriods();
01462 
01463   QValueList<Period>::iterator it;
01464   for ( it = periods.begin(); it != periods.end(); ++it ) {
01465     Period per = *it;
01466     if ( per.hasDuration() ) {
01467       int dur = per.duration().asSeconds();
01468       QString cont;
01469       if ( dur >= 3600 ) {
01470         cont += i18n("1 hour ", "%n hours ", dur / 3600);
01471         dur %= 3600;
01472       }
01473       if ( dur >= 60 ) {
01474         cont += i18n("1 minute", "%n minutes ", dur / 60);
01475         dur %= 60;
01476       }
01477       if ( dur > 0 ) {
01478         cont += i18n("1 second", "%n seconds", dur);
01479       }
01480       html += invitationRow( QString::null, i18n("startDate for duration", "%1 for %2")
01481           .arg( KGlobal::locale()->formatDateTime( per.start(), false ) )
01482           .arg(cont) );
01483     } else {
01484       QString cont;
01485       if ( per.start().date() == per.end().date() ) {
01486         cont = i18n("date, fromTime - toTime ", "%1, %2 - %3")
01487             .arg( KGlobal::locale()->formatDate( per.start().date() ) )
01488             .arg( KGlobal::locale()->formatTime( per.start().time() ) )
01489             .arg( KGlobal::locale()->formatTime( per.end().time() ) );
01490       } else {
01491         cont = i18n("fromDateTime - toDateTime", "%1 - %2")
01492           .arg( KGlobal::locale()->formatDateTime( per.start(), false ) )
01493           .arg( KGlobal::locale()->formatDateTime( per.end(), false ) );
01494       }
01495 
01496       html += invitationRow( QString::null, cont );
01497     }
01498   }
01499 
01500   html += "</table>\n";
01501   return html;
01502 }
01503 
01504 static bool replyMeansCounter( Incidence */*incidence*/ )
01505 {
01506   return false;
01521 }
01522 
01523 static QString invitationHeaderEvent( Event *event, Incidence *existingIncidence,
01524                                       ScheduleMessage *msg, const QString &sender )
01525 {
01526   if ( !msg || !event )
01527     return QString::null;
01528 
01529   switch ( msg->method() ) {
01530   case Scheduler::Publish:
01531     return i18n( "This invitation has been published" );
01532   case Scheduler::Request:
01533     if ( existingIncidence && event->revision() > 0 ) {
01534       return i18n( "This invitation has been updated by the organizer %1" ).
01535         arg( event->organizer().fullName() );
01536     }
01537     if ( iamOrganizer( event ) ) {
01538       return i18n( "I created this invitation" );
01539     } else {
01540       if ( senderIsOrganizer( event, sender ) ) {
01541         if ( !event->organizer().fullName().isEmpty() ) {
01542           return i18n( "You received an invitation from %1" ).
01543             arg( event->organizer().fullName() );
01544         } else {
01545           return i18n( "You received an invitation" );
01546         }
01547       } else {
01548         if ( !event->organizer().fullName().isEmpty() ) {
01549           return i18n( "You received an invitation from %1 as a representative of %2" ).
01550             arg( sender, event->organizer().fullName() );
01551         } else {
01552           return i18n( "You received an invitation from %1 as the organizer's representative" ).
01553             arg( sender );
01554         }
01555       }
01556     }
01557   case Scheduler::Refresh:
01558     return i18n( "This invitation was refreshed" );
01559   case Scheduler::Cancel:
01560     return i18n( "This invitation has been canceled" );
01561   case Scheduler::Add:
01562     return i18n( "Addition to the invitation" );
01563   case Scheduler::Reply:
01564   {
01565     if ( replyMeansCounter( event ) ) {
01566       return i18n( "%1 makes this counter proposal" ).
01567         arg( firstAttendeeName( event, i18n( "Sender" ) ) );
01568     }
01569 
01570     Attendee::List attendees = event->attendees();
01571     if( attendees.count() == 0 ) {
01572       kdDebug(5850) << "No attendees in the iCal reply!" << endl;
01573       return QString::null;
01574     }
01575     if( attendees.count() != 1 ) {
01576       kdDebug(5850) << "Warning: attendeecount in the reply should be 1 "
01577                     << "but is " << attendees.count() << endl;
01578     }
01579     QString attendeeName = firstAttendeeName( event, i18n( "Sender" ) );
01580 
01581     QString delegatorName, dummy;
01582     Attendee* attendee = *attendees.begin();
01583     KPIM::getNameAndMail( attendee->delegator(), delegatorName, dummy );
01584     if ( delegatorName.isEmpty() ) {
01585       delegatorName = attendee->delegator();
01586     }
01587 
01588     switch( attendee->status() ) {
01589     case Attendee::NeedsAction:
01590       return i18n( "%1 indicates this invitation still needs some action" ).arg( attendeeName );
01591     case Attendee::Accepted:
01592       if ( event->revision() > 0 ) {
01593         if ( !sender.isEmpty() ) {
01594           return i18n( "This invitation has been updated by attendee %1" ).arg( sender );
01595         } else {
01596           return i18n( "This invitation has been updated by an attendee" );
01597         }
01598       } else {
01599         if ( delegatorName.isEmpty() ) {
01600           return i18n( "%1 accepts this invitation" ).arg( attendeeName );
01601         } else {
01602           return i18n( "%1 accepts this invitation on behalf of %2" ).
01603             arg( attendeeName ).arg( delegatorName );
01604         }
01605       }
01606     case Attendee::Tentative:
01607       if ( delegatorName.isEmpty() ) {
01608         return i18n( "%1 tentatively accepts this invitation" ).
01609           arg( attendeeName );
01610       } else {
01611         return i18n( "%1 tentatively accepts this invitation on behalf of %2" ).
01612           arg( attendeeName ).arg( delegatorName );
01613       }
01614     case Attendee::Declined:
01615       if ( delegatorName.isEmpty() ) {
01616         return i18n( "%1 declines this invitation" ).arg( attendeeName );
01617       } else {
01618         return i18n( "%1 declines this invitation on behalf of %2" ).
01619           arg( attendeeName ).arg( delegatorName );
01620       }
01621     case Attendee::Delegated: {
01622       QString delegate, dummy;
01623       KPIM::getNameAndMail( attendee->delegate(), delegate, dummy );
01624       if ( delegate.isEmpty() ) {
01625         delegate = attendee->delegate();
01626       }
01627       if ( !delegate.isEmpty() ) {
01628         return i18n( "%1 has delegated this invitation to %2" ).
01629           arg( attendeeName ) .arg( delegate );
01630       } else {
01631         return i18n( "%1 has delegated this invitation" ).arg( attendeeName );
01632       }
01633     }
01634     case Attendee::Completed:
01635       return i18n( "This invitation is now completed" );
01636     case Attendee::InProcess:
01637       return i18n( "%1 is still processing the invitation" ).
01638         arg( attendeeName );
01639     default:
01640       return i18n( "Unknown response to this invitation" );
01641     }
01642     break;
01643   }
01644 
01645   case Scheduler::Counter:
01646     return i18n( "%1 makes this counter proposal" ).
01647       arg( firstAttendeeName( event, i18n( "Sender" ) ) );
01648 
01649   case Scheduler::Declinecounter:
01650     return i18n( "%1 declines the counter proposal" ).
01651       arg( firstAttendeeName( event, i18n( "Sender" ) ) );
01652 
01653   case Scheduler::NoMethod:
01654     return i18n("Error: iMIP message with unknown method: '%1'").
01655       arg( msg->method() );
01656   }
01657   return QString::null;
01658 }
01659 
01660 static QString invitationHeaderTodo( Todo *todo, Incidence *existingIncidence,
01661                                      ScheduleMessage *msg, const QString &sender )
01662 {
01663   if ( !msg || !todo ) {
01664     return QString::null;
01665   }
01666 
01667   switch ( msg->method() ) {
01668   case Scheduler::Publish:
01669     return i18n("This task has been published");
01670   case Scheduler::Request:
01671     if ( existingIncidence && todo->revision() > 0 ) {
01672       return i18n( "This task has been updated by the organizer %1" ).
01673         arg( todo->organizer().fullName() );
01674     } else {
01675       if ( iamOrganizer( todo ) ) {
01676         return i18n( "I created this task" );
01677       } else {
01678         if ( senderIsOrganizer( todo, sender ) ) {
01679           if ( !todo->organizer().fullName().isEmpty() ) {
01680             return i18n( "You have been assigned this task by %1" ).
01681               arg( todo->organizer().fullName() );
01682           } else {
01683             return i18n( "You have been assigned this task" );
01684           }
01685         } else {
01686           if ( !todo->organizer().fullName().isEmpty() ) {
01687             return i18n( "You have been assigned this task by %1 as a representative of %2" ).
01688               arg( sender, todo->organizer().fullName() );
01689           } else {
01690             return i18n( "You have been assigned this task by %1 as the organizer's representative" ).
01691               arg( sender );
01692           }
01693         }
01694       }
01695     }
01696   case Scheduler::Refresh:
01697     return i18n( "This task was refreshed" );
01698   case Scheduler::Cancel:
01699     return i18n( "This task was canceled" );
01700   case Scheduler::Add:
01701     return i18n( "Addition to the task" );
01702   case Scheduler::Reply:
01703   {
01704     if ( replyMeansCounter( todo ) ) {
01705       return i18n( "%1 makes this counter proposal" ).
01706         arg( firstAttendeeName( todo, i18n( "Sender" ) ) );
01707     }
01708 
01709     Attendee::List attendees = todo->attendees();
01710     if( attendees.count() == 0 ) {
01711       kdDebug(5850) << "No attendees in the iCal reply!" << endl;
01712       return QString::null;
01713     }
01714     if( attendees.count() != 1 ) {
01715       kdDebug(5850) << "Warning: attendeecount in the reply should be 1 "
01716                     << "but is " << attendees.count() << endl;
01717     }
01718     QString attendeeName = firstAttendeeName( todo, i18n( "Sender" ) );
01719 
01720     QString delegatorName, dummy;
01721     Attendee* attendee = *attendees.begin();
01722     KPIM::getNameAndMail( attendee->delegator(), delegatorName, dummy );
01723     if ( delegatorName.isEmpty() ) {
01724       delegatorName = attendee->delegator();
01725     }
01726 
01727     switch( attendee->status() ) {
01728     case Attendee::NeedsAction:
01729       return i18n( "%1 indicates this task assignment still needs some action" ).arg( attendeeName );
01730     case Attendee::Accepted:
01731       if ( todo->revision() > 0 ) {
01732         if ( !sender.isEmpty() ) {
01733           if ( todo->isCompleted() ) {
01734             return i18n( "This task has been completed by assignee %1" ).arg( sender );
01735           } else {
01736             return i18n( "This task has been updated by assignee %1" ).arg( sender );
01737           }
01738         } else {
01739           if ( todo->isCompleted() ) {
01740             return i18n( "This task has been completed by an assignee" );
01741           } else {
01742             return i18n( "This task has been updated by an assignee" );
01743           }
01744         }
01745       } else {
01746         if ( delegatorName.isEmpty() ) {
01747           return i18n( "%1 accepts this task" ).arg( attendeeName );
01748         } else {
01749           return i18n( "%1 accepts this task on behalf of %2" ).
01750             arg( attendeeName ).arg( delegatorName );
01751         }
01752       }
01753     case Attendee::Tentative:
01754       if ( delegatorName.isEmpty() ) {
01755         return i18n( "%1 tentatively accepts this task" ).
01756           arg( attendeeName );
01757       } else {
01758         return i18n( "%1 tentatively accepts this task on behalf of %2" ).
01759           arg( attendeeName ).arg( delegatorName );
01760       }
01761     case Attendee::Declined:
01762       if ( delegatorName.isEmpty() ) {
01763         return i18n( "%1 declines this task" ).arg( attendeeName );
01764       } else {
01765         return i18n( "%1 declines this task on behalf of %2" ).
01766           arg( attendeeName ).arg( delegatorName );
01767       }
01768     case Attendee::Delegated: {
01769       QString delegate, dummy;
01770       KPIM::getNameAndMail( attendee->delegate(), delegate, dummy );
01771       if ( delegate.isEmpty() ) {
01772         delegate = attendee->delegate();
01773       }
01774       if ( !delegate.isEmpty() ) {
01775         return i18n( "%1 has delegated this request for the task to %2" ).
01776           arg( attendeeName ).arg( delegate );
01777       } else {
01778         return i18n( "%1 has delegated this request for the task" ).
01779           arg( attendeeName );
01780       }
01781     }
01782     case Attendee::Completed:
01783       return i18n( "The request for this task is now completed" );
01784     case Attendee::InProcess:
01785       return i18n( "%1 is still processing the task" ).
01786         arg( attendeeName );
01787     default:
01788       return i18n( "Unknown response to this task" );
01789     }
01790     break;
01791   }
01792 
01793   case Scheduler::Counter:
01794     return i18n( "%1 makes this counter proposal" ).
01795       arg( firstAttendeeName( todo, i18n( "Sender" ) ) );
01796 
01797   case Scheduler::Declinecounter:
01798     return i18n( "%1 declines the counter proposal" ).
01799       arg( firstAttendeeName( todo, i18n( "Sender" ) ) );
01800 
01801   case Scheduler::NoMethod:
01802     return i18n( "Error: iMIP message with unknown method: '%1'" ).
01803       arg( msg->method() );
01804   }
01805   return QString::null;
01806 }
01807 
01808 static QString invitationHeaderJournal( Journal *journal, ScheduleMessage *msg )
01809 {
01810   if ( !msg || !journal ) {
01811     return QString::null;
01812   }
01813 
01814   switch ( msg->method() ) {
01815   case Scheduler::Publish:
01816     return i18n("This journal has been published");
01817   case Scheduler::Request:
01818     return i18n( "You have been assigned this journal" );
01819   case Scheduler::Refresh:
01820     return i18n( "This journal was refreshed" );
01821   case Scheduler::Cancel:
01822     return i18n( "This journal was canceled" );
01823   case Scheduler::Add:
01824     return i18n( "Addition to the journal" );
01825   case Scheduler::Reply:
01826   {
01827     if ( replyMeansCounter( journal ) ) {
01828       return i18n( "Sender makes this counter proposal" );
01829     }
01830 
01831     Attendee::List attendees = journal->attendees();
01832     if( attendees.count() == 0 ) {
01833       kdDebug(5850) << "No attendees in the iCal reply!" << endl;
01834       return QString::null;
01835     }
01836     if( attendees.count() != 1 ) {
01837       kdDebug(5850) << "Warning: attendeecount in the reply should be 1 "
01838                     << "but is " << attendees.count() << endl;
01839     }
01840     Attendee* attendee = *attendees.begin();
01841 
01842     switch( attendee->status() ) {
01843     case Attendee::NeedsAction:
01844       return i18n( "Sender indicates this journal assignment still needs some action" );
01845     case Attendee::Accepted:
01846       return i18n( "Sender accepts this journal" );
01847     case Attendee::Tentative:
01848       return i18n( "Sender tentatively accepts this journal" );
01849     case Attendee::Declined:
01850       return i18n( "Sender declines this journal" );
01851     case Attendee::Delegated:
01852       return i18n( "Sender has delegated this request for the journal" );
01853     case Attendee::Completed:
01854       return i18n( "The request for this journal is now completed" );
01855     case Attendee::InProcess:
01856       return i18n( "Sender is still processing the invitation" );
01857     default:
01858       return i18n( "Unknown response to this journal" );
01859     }
01860     break;
01861   }
01862   case Scheduler::Counter:
01863     return i18n( "Sender makes this counter proposal" );
01864   case Scheduler::Declinecounter:
01865     return i18n( "Sender declines the counter proposal" );
01866   case Scheduler::NoMethod:
01867     return i18n("Error: iMIP message with unknown method: '%1'").
01868       arg( msg->method() );
01869   }
01870   return QString::null;
01871 }
01872 
01873 static QString invitationHeaderFreeBusy( FreeBusy *fb, ScheduleMessage *msg )
01874 {
01875   if ( !msg || !fb ) {
01876     return QString::null;
01877   }
01878 
01879   switch ( msg->method() ) {
01880   case Scheduler::Publish:
01881     return i18n("This free/busy list has been published");
01882   case Scheduler::Request:
01883     return i18n( "The free/busy list has been requested" );
01884   case Scheduler::Refresh:
01885     return i18n( "This free/busy list was refreshed" );
01886   case Scheduler::Cancel:
01887     return i18n( "This free/busy list was canceled" );
01888   case Scheduler::Add:
01889     return i18n( "Addition to the free/busy list" );
01890   case Scheduler::NoMethod:
01891   default:
01892     return i18n("Error: Free/Busy iMIP message with unknown method: '%1'").
01893       arg( msg->method() );
01894   }
01895 }
01896 
01897 static QString invitationAttendees( Incidence *incidence )
01898 {
01899   QString tmpStr;
01900   if ( !incidence ) {
01901     return tmpStr;
01902   }
01903 
01904   tmpStr += htmlAddTag( "u", i18n( "Attendee List" ) );
01905   tmpStr += "<br/>";
01906 
01907   int count=0;
01908   Attendee::List attendees = incidence->attendees();
01909   if ( !attendees.isEmpty() ) {
01910 
01911     Attendee::List::ConstIterator it;
01912     for( it = attendees.begin(); it != attendees.end(); ++it ) {
01913       Attendee *a = *it;
01914       if ( !iamAttendee( a ) ) {
01915         count++;
01916         if ( count == 1 ) {
01917           tmpStr += "<table border=\"1\" cellpadding=\"1\" cellspacing=\"0\" columns=\"2\">";
01918         }
01919         tmpStr += "<tr>";
01920         tmpStr += "<td>";
01921         tmpStr += invitationPerson( a->email(), a->name(), QString::null );
01922         if ( !a->delegator().isEmpty() ) {
01923           tmpStr += i18n(" (delegated by %1)" ).arg( a->delegator() );
01924         }
01925         if ( !a->delegate().isEmpty() ) {
01926           tmpStr += i18n(" (delegated to %1)" ).arg( a->delegate() );
01927         }
01928         tmpStr += "</td>";
01929         tmpStr += "<td>" + a->statusStr() + "</td>";
01930         tmpStr += "</tr>";
01931       }
01932     }
01933   }
01934   if ( count ) {
01935     tmpStr += "</table>";
01936   } else {
01937     tmpStr += "<i>" + i18n( "No attendee", "None" ) + "</i>";
01938   }
01939 
01940   return tmpStr;
01941 }
01942 
01943 static QString invitationAttachments( InvitationFormatterHelper *helper, Incidence *incidence )
01944 {
01945   QString tmpStr;
01946   if ( !incidence ) {
01947     return tmpStr;
01948   }
01949 
01950   Attachment::List attachments = incidence->attachments();
01951   if ( !attachments.isEmpty() ) {
01952     tmpStr += i18n( "Attached Documents:" ) + "<ol>";
01953 
01954     Attachment::List::ConstIterator it;
01955     for( it = attachments.begin(); it != attachments.end(); ++it ) {
01956       Attachment *a = *it;
01957       tmpStr += "<li>";
01958       // Attachment icon
01959       KMimeType::Ptr mimeType = KMimeType::mimeType( a->mimeType() );
01960       const QString iconStr = mimeType ? mimeType->icon( a->uri(), false ) : QString( "application-octet-stream" );
01961       const QString iconPath = KGlobal::iconLoader()->iconPath( iconStr, KIcon::Small );
01962       if ( !iconPath.isEmpty() ) {
01963         tmpStr += "<img valign=\"top\" src=\"" + iconPath + "\">";
01964       }
01965       tmpStr += helper->makeLink( "ATTACH:" + a->label(), a->label() );
01966       tmpStr += "</li>";
01967     }
01968     tmpStr += "</ol>";
01969   }
01970 
01971   return tmpStr;
01972 }
01973 
01974 class IncidenceFormatter::ScheduleMessageVisitor
01975   : public IncidenceBase::Visitor
01976 {
01977   public:
01978     ScheduleMessageVisitor() : mExistingIncidence( 0 ), mMessage( 0 ) { mResult = ""; }
01979     bool act( IncidenceBase *incidence, Incidence *existingIncidence, ScheduleMessage *msg,
01980               const QString &sender )
01981     {
01982       mExistingIncidence = existingIncidence;
01983       mMessage = msg;
01984       mSender = sender;
01985       return incidence->accept( *this );
01986     }
01987     QString result() const { return mResult; }
01988 
01989   protected:
01990     QString mResult;
01991     Incidence *mExistingIncidence;
01992     ScheduleMessage *mMessage;
01993     QString mSender;
01994 };
01995 
01996 class IncidenceFormatter::InvitationHeaderVisitor
01997   : public IncidenceFormatter::ScheduleMessageVisitor
01998 {
01999   protected:
02000     bool visit( Event *event )
02001     {
02002       mResult = invitationHeaderEvent( event, mExistingIncidence, mMessage, mSender );
02003       return !mResult.isEmpty();
02004     }
02005     bool visit( Todo *todo )
02006     {
02007       mResult = invitationHeaderTodo( todo, mExistingIncidence, mMessage, mSender );
02008       return !mResult.isEmpty();
02009     }
02010     bool visit( Journal *journal )
02011     {
02012       mResult = invitationHeaderJournal( journal, mMessage );
02013       return !mResult.isEmpty();
02014     }
02015     bool visit( FreeBusy *fb )
02016     {
02017       mResult = invitationHeaderFreeBusy( fb, mMessage );
02018       return !mResult.isEmpty();
02019     }
02020 };
02021 
02022 class IncidenceFormatter::InvitationBodyVisitor
02023   : public IncidenceFormatter::ScheduleMessageVisitor
02024 {
02025   public:
02026     InvitationBodyVisitor( bool noHtmlMode )
02027       : ScheduleMessageVisitor(), mNoHtmlMode( noHtmlMode ) {}
02028 
02029   protected:
02030     bool visit( Event *event )
02031     {
02032       mResult = invitationDetailsEvent( event, mNoHtmlMode );
02033       return !mResult.isEmpty();
02034     }
02035     bool visit( Todo *todo )
02036     {
02037       mResult = invitationDetailsTodo( todo, mNoHtmlMode );
02038       return !mResult.isEmpty();
02039     }
02040     bool visit( Journal *journal )
02041     {
02042       mResult = invitationDetailsJournal( journal, mNoHtmlMode );
02043       return !mResult.isEmpty();
02044     }
02045     bool visit( FreeBusy *fb )
02046     {
02047       mResult = invitationDetailsFreeBusy( fb, mNoHtmlMode );
02048       return !mResult.isEmpty();
02049     }
02050 
02051   private:
02052     bool mNoHtmlMode;
02053 };
02054 
02055 class IncidenceFormatter::IncidenceCompareVisitor
02056   : public IncidenceBase::Visitor
02057 {
02058   public:
02059     IncidenceCompareVisitor() : mExistingIncidence(0) {}
02060     bool act( IncidenceBase *incidence, Incidence* existingIncidence )
02061     {
02062       Incidence *inc = dynamic_cast<Incidence*>( incidence );
02063       if ( !inc || !existingIncidence || inc->revision() <= existingIncidence->revision() )
02064         return false;
02065       mExistingIncidence = existingIncidence;
02066       return incidence->accept( *this );
02067     }
02068 
02069     QString result() const
02070     {
02071       if ( mChanges.isEmpty() ) {
02072         return QString::null;
02073       }
02074       QString html = "<div align=\"left\"><ul><li>";
02075       html += mChanges.join( "</li><li>" );
02076       html += "</li><ul></div>";
02077       return html;
02078     }
02079 
02080   protected:
02081     bool visit( Event *event )
02082     {
02083       compareEvents( event, dynamic_cast<Event*>( mExistingIncidence ) );
02084       compareIncidences( event, mExistingIncidence );
02085       return !mChanges.isEmpty();
02086     }
02087     bool visit( Todo *todo )
02088     {
02089       compareTodos( todo, dynamic_cast<Todo*>( mExistingIncidence ) );
02090       compareIncidences( todo, mExistingIncidence );
02091       return !mChanges.isEmpty();
02092     }
02093     bool visit( Journal *journal )
02094     {
02095       compareIncidences( journal, mExistingIncidence );
02096       return !mChanges.isEmpty();
02097     }
02098     bool visit( FreeBusy *fb )
02099     {
02100       Q_UNUSED( fb );
02101       return !mChanges.isEmpty();
02102     }
02103 
02104   private:
02105     void compareEvents( Event *newEvent, Event *oldEvent )
02106     {
02107       if ( !oldEvent || !newEvent )
02108         return;
02109       if ( oldEvent->dtStart() != newEvent->dtStart() || oldEvent->doesFloat() != newEvent->doesFloat() )
02110         mChanges += i18n( "The invitation starting time has been changed from %1 to %2" )
02111                     .arg( eventStartTimeStr( oldEvent ) ).arg( eventStartTimeStr( newEvent ) );
02112       if ( oldEvent->dtEnd() != newEvent->dtEnd() || oldEvent->doesFloat() != newEvent->doesFloat() )
02113         mChanges += i18n( "The invitation ending time has been changed from %1 to %2" )
02114                     .arg( eventEndTimeStr( oldEvent ) ).arg( eventEndTimeStr( newEvent ) );
02115     }
02116 
02117     void compareTodos( Todo *newTodo, Todo *oldTodo )
02118     {
02119       if ( !oldTodo || !newTodo ) {
02120         return;
02121       }
02122 
02123       if ( !oldTodo->isCompleted() && newTodo->isCompleted() ) {
02124         mChanges += i18n( "The task has been completed" );
02125       }
02126       if ( oldTodo->isCompleted() && !newTodo->isCompleted() ) {
02127         mChanges += i18n( "The task is no longer completed" );
02128       }
02129       if ( !oldTodo->isCompleted() && !newTodo->isCompleted() &&
02130            oldTodo->percentComplete() != newTodo->percentComplete() ) {
02131         mChanges += i18n( "The task completed percentage has changed from %1 to %2" ).
02132                     arg( oldTodo->percentComplete(), newTodo->percentComplete() );
02133       }
02134 
02135       if ( !oldTodo->hasStartDate() && newTodo->hasStartDate() ) {
02136         mChanges += i18n( "A task starting time has been added" );
02137       }
02138       if ( oldTodo->hasStartDate() && !newTodo->hasStartDate() ) {
02139         mChanges += i18n( "The task starting time has been removed" );
02140       }
02141       if ( oldTodo->hasStartDate() && newTodo->hasStartDate() &&
02142            oldTodo->dtStart() != newTodo->dtStart() ) {
02143         mChanges += i18n( "The task starting time has been changed from %1 to %2" ).
02144                     arg( dateTimeToString( oldTodo->dtStart(), oldTodo->doesFloat(), false ),
02145                          dateTimeToString( newTodo->dtStart(), newTodo->doesFloat(), false ) );
02146       }
02147 
02148       if ( !oldTodo->hasDueDate() && newTodo->hasDueDate() ) {
02149         mChanges += i18n( "A task due time has been added" );
02150       }
02151       if ( oldTodo->hasDueDate() && !newTodo->hasDueDate() ) {
02152         mChanges += i18n( "The task due time has been removed" );
02153       }
02154       if ( oldTodo->hasDueDate() && newTodo->hasDueDate() &&
02155            oldTodo->dtDue() != newTodo->dtDue() ) {
02156         mChanges += i18n( "The task due time has been changed from %1 to %2" ).
02157                     arg( dateTimeToString( oldTodo->dtDue(), oldTodo->doesFloat(), false ),
02158                          dateTimeToString( newTodo->dtDue(), newTodo->doesFloat(), false ) );
02159       }
02160     }
02161 
02162     void compareIncidences( Incidence *newInc, Incidence *oldInc )
02163     {
02164       if ( !oldInc || !newInc )
02165         return;
02166       if ( oldInc->summary() != newInc->summary() )
02167         mChanges += i18n( "The summary has been changed to: \"%1\"" ).arg( newInc->summary() );
02168       if ( oldInc->location() != newInc->location() )
02169         mChanges += i18n( "The location has been changed to: \"%1\"" ).arg( newInc->location() );
02170       if ( oldInc->description() != newInc->description() )
02171         mChanges += i18n( "The description has been changed to: \"%1\"" ).arg( newInc->description() );
02172       Attendee::List oldAttendees = oldInc->attendees();
02173       Attendee::List newAttendees = newInc->attendees();
02174       for ( Attendee::List::ConstIterator it = newAttendees.constBegin(); it != newAttendees.constEnd(); ++it ) {
02175         Attendee *oldAtt = oldInc->attendeeByMail( (*it)->email() );
02176         if ( !oldAtt ) {
02177           mChanges += i18n( "Attendee %1 has been added" ).arg( (*it)->fullName() );
02178         } else {
02179           if ( oldAtt->status() != (*it)->status() )
02180             mChanges += i18n( "The status of attendee %1 has been changed to: %2" ).arg( (*it)->fullName() )
02181                         .arg( (*it)->statusStr() );
02182         }
02183       }
02184       for ( Attendee::List::ConstIterator it = oldAttendees.constBegin(); it != oldAttendees.constEnd(); ++it ) {
02185         Attendee *newAtt = newInc->attendeeByMail( (*it)->email() );
02186         if ( !newAtt )
02187           mChanges += i18n( "Attendee %1 has been removed" ).arg( (*it)->fullName() );
02188       }
02189     }
02190 
02191   private:
02192     Incidence* mExistingIncidence;
02193     QStringList mChanges;
02194 };
02195 
02196 
02197 QString InvitationFormatterHelper::makeLink( const QString &id, const QString &text )
02198 {
02199   if ( !id.startsWith( "ATTACH:" ) ) {
02200     QString res = QString( "<a href=\"%1\"><b>%2</b></a>" ).
02201                   arg( generateLinkURL( id ), text );
02202     return res;
02203   } else {
02204     // draw the attachment links in non-bold face
02205     QString res = QString( "<a href=\"%1\">%2</a>" ).
02206                   arg( generateLinkURL( id ), text );
02207     return res;
02208   }
02209 }
02210 
02211 // Check if the given incidence is likely one that we own instead one from
02212 // a shared calendar (Kolab-specific)
02213 static bool incidenceOwnedByMe( Calendar *calendar, Incidence *incidence )
02214 {
02215   CalendarResources *cal = dynamic_cast<CalendarResources*>( calendar );
02216   if ( !cal || !incidence ) {
02217     return true;
02218   }
02219   ResourceCalendar *res = cal->resource( incidence );
02220   if ( !res ) {
02221     return true;
02222   }
02223   const QString subRes = res->subresourceIdentifier( incidence );
02224   if ( !subRes.contains( "/.INBOX.directory/" ) ) {
02225     return false;
02226   }
02227   return true;
02228 }
02229 
02230 // The spacer for the invitation buttons
02231 static QString spacer = "<td> &nbsp; </td>";
02232 // The open & close table cell tags for the invitation buttons
02233 static QString tdOpen = "<td>";
02234 static QString tdClose = "</td>" + spacer;
02235 
02236 static QString responseButtons( Incidence *inc, bool rsvpReq, bool rsvpRec,
02237                                 InvitationFormatterHelper *helper )
02238 {
02239   QString html;
02240   if ( !helper ) {
02241     return html;
02242   }
02243 
02244   if ( !rsvpReq && ( inc && inc->revision() == 0 ) ) {
02245     // Record only
02246     html += tdOpen;
02247     html += helper->makeLink( "record", i18n( "[Record]" ) );
02248     html += tdClose;
02249 
02250     // Move to trash
02251     html += tdOpen;
02252     html += helper->makeLink( "delete", i18n( "[Move to Trash]" ) );
02253     html += tdClose;
02254 
02255   } else {
02256 
02257     // Accept
02258     html += tdOpen;
02259     html += helper->makeLink( "accept", i18n( "[Accept]" ) );
02260     html += tdClose;
02261 
02262     // Tentative
02263     html += tdOpen;
02264     html += helper->makeLink( "accept_conditionally",
02265                               i18n( "Accept conditionally", "[Accept cond.]" ) );
02266     html += tdClose;
02267 
02268     // Counter proposal
02269     html += tdOpen;
02270     html += helper->makeLink( "counter", i18n( "[Counter proposal]" ) );
02271     html += tdClose;
02272 
02273     // Decline
02274     html += tdOpen;
02275     html += helper->makeLink( "decline", i18n( "[Decline]" ) );
02276     html += tdClose;
02277   }
02278 
02279   if ( !rsvpRec || ( inc && inc->revision() > 0 ) ) {
02280     // Delegate
02281     html += tdOpen;
02282     html += helper->makeLink( "delegate", i18n( "[Delegate]" ) );
02283     html += tdClose;
02284 
02285     // Forward
02286     html += tdOpen;
02287     html += helper->makeLink( "forward", i18n( "[Forward]" ) );
02288     html += tdClose;
02289 
02290     // Check calendar
02291     if ( inc && inc->type() == "Event" ) {
02292       html += tdOpen;
02293       html += helper->makeLink( "check_calendar", i18n("[Check my calendar]" ) );
02294       html += tdClose;
02295     }
02296   }
02297   return html;
02298 }
02299 
02300 static QString counterButtons( Incidence *incidence,
02301                                InvitationFormatterHelper *helper )
02302 {
02303   QString html;
02304   if ( !helper ) {
02305     return html;
02306   }
02307 
02308   // Accept proposal
02309   html += tdOpen;
02310   html += helper->makeLink( "accept_counter", i18n("[Accept]") );
02311   html += tdClose;
02312 
02313   // Decline proposal
02314   html += tdOpen;
02315   html += helper->makeLink( "decline_counter", i18n("[Decline]") );
02316   html += tdClose;
02317 
02318   // Check calendar
02319   if ( incidence && incidence->type() == "Event" ) {
02320     html += tdOpen;
02321     html += helper->makeLink( "check_calendar", i18n("[Check my calendar]" ) );
02322     html += tdClose;
02323   }
02324   return html;
02325 }
02326 
02327 QString IncidenceFormatter::formatICalInvitationHelper( QString invitation,
02328                                                         Calendar *mCalendar,
02329                                                         InvitationFormatterHelper *helper,
02330                                                         bool noHtmlMode,
02331                                                         const QString &sender )
02332 {
02333   if ( invitation.isEmpty() ) {
02334     return QString::null;
02335   }
02336 
02337   ICalFormat format;
02338   // parseScheduleMessage takes the tz from the calendar, no need to set it manually here for the format!
02339   ScheduleMessage *msg = format.parseScheduleMessage( mCalendar, invitation );
02340 
02341   if( !msg ) {
02342     kdDebug( 5850 ) << "Failed to parse the scheduling message" << endl;
02343     Q_ASSERT( format.exception() );
02344     kdDebug( 5850 ) << format.exception()->message() << endl;
02345     return QString::null;
02346   }
02347 
02348   IncidenceBase *incBase = msg->event();
02349 
02350   // Determine if this incidence is in my calendar (and owned by me)
02351   Incidence *existingIncidence = 0;
02352   if ( incBase && helper->calendar() ) {
02353     existingIncidence = helper->calendar()->incidence( incBase->uid() );
02354     if ( !incidenceOwnedByMe( helper->calendar(), existingIncidence ) ) {
02355       existingIncidence = 0;
02356     }
02357     if ( !existingIncidence ) {
02358       const Incidence::List list = helper->calendar()->incidences();
02359       for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
02360         if ( (*it)->schedulingID() == incBase->uid() &&
02361              incidenceOwnedByMe( helper->calendar(), *it ) ) {
02362           existingIncidence = *it;
02363           break;
02364         }
02365       }
02366     }
02367   }
02368 
02369   // First make the text of the message
02370   QString html;
02371 
02372   QString tableStyle = QString::fromLatin1(
02373     "style=\"border: solid 1px; margin: 0em;\"" );
02374   QString tableHead = QString::fromLatin1(
02375     "<div align=\"center\">"
02376     "<table width=\"80%\" cellpadding=\"1\" cellspacing=\"0\" %1>"
02377     "<tr><td>").arg(tableStyle);
02378 
02379   html += tableHead;
02380   InvitationHeaderVisitor headerVisitor;
02381   // The InvitationHeaderVisitor returns false if the incidence is somehow invalid, or not handled
02382   if ( !headerVisitor.act( incBase, existingIncidence, msg, sender ) )
02383     return QString::null;
02384   html += "<b>" + headerVisitor.result() + "</b>";
02385 
02386   InvitationBodyVisitor bodyVisitor( noHtmlMode );
02387   if ( !bodyVisitor.act( incBase, existingIncidence, msg, sender ) )
02388     return QString::null;
02389   html += bodyVisitor.result();
02390 
02391   if ( msg->method() == Scheduler::Request ) {
02392     IncidenceCompareVisitor compareVisitor;
02393     if ( compareVisitor.act( incBase, existingIncidence ) ) {
02394       html += "<p align=\"left\">";
02395       html += i18n( "The following changes have been made by the organizer:" );
02396       html += "</p>";
02397       html += compareVisitor.result();
02398     }
02399   }
02400   if ( msg->method() == Scheduler::Reply ) {
02401     IncidenceCompareVisitor compareVisitor;
02402     if ( compareVisitor.act( incBase, existingIncidence ) ) {
02403       html += "<p align=\"left\">";
02404       if ( !sender.isEmpty() ) {
02405         html += i18n( "The following changes have been made by %1:" ).arg( sender );
02406       } else {
02407         html += i18n( "The following changes have been made by an attendee:" );
02408       }
02409       html += "</p>";
02410       html += compareVisitor.result();
02411     }
02412   }
02413 
02414   Incidence *inc = dynamic_cast<Incidence*>( incBase );
02415 
02416   // determine if I am the organizer for this invitation
02417   bool myInc = iamOrganizer( inc );
02418 
02419   // determine if the invitation response has already been recorded
02420   bool rsvpRec = false;
02421   Attendee *ea = 0;
02422   if ( !myInc ) {
02423     Incidence *rsvpIncidence = existingIncidence;
02424     if ( !rsvpIncidence && inc && inc->revision() > 0 ) {
02425       rsvpIncidence = inc;
02426     }
02427     if ( rsvpIncidence ) {
02428       ea = findMyAttendee( rsvpIncidence );
02429     }
02430     if ( ea &&
02431          ( ea->status() == Attendee::Accepted ||
02432            ea->status() == Attendee::Declined ||
02433            ea->status() == Attendee::Tentative ) ) {
02434       rsvpRec = true;
02435     }
02436   }
02437 
02438   // determine invitation role
02439   QString role;
02440   bool isDelegated = false;
02441   Attendee *a = findMyAttendee( inc );
02442   if ( !a && inc ) {
02443     if ( !inc->attendees().isEmpty() ) {
02444       a = inc->attendees().first();
02445     }
02446   }
02447   if ( a ) {
02448     isDelegated = ( a->status() == Attendee::Delegated );
02449     role = Attendee::roleName( a->role() );
02450   }
02451 
02452   // Print if RSVP needed, not-needed, or response already recorded
02453   bool rsvpReq = rsvpRequested( inc );
02454   if ( !myInc && a ) {
02455     html += "<br/>";
02456     html += "<i><u>";
02457     if ( rsvpRec && inc ) {
02458       if ( inc->revision() == 0 ) {
02459         html += i18n( "Your <b>%1</b> response has already been recorded" ).
02460                 arg( ea->statusStr() );
02461       } else {
02462         html += i18n( "Your status for this invitation is <b>%1</b>" ).
02463                 arg( ea->statusStr() );
02464       }
02465       rsvpReq = false;
02466     } else if ( msg->method() == Scheduler::Cancel ) {
02467       html += i18n( "This invitation was declined" );
02468     } else if ( msg->method() == Scheduler::Add ) {
02469       html += i18n( "This invitation was accepted" );
02470     } else {
02471       if ( !isDelegated ) {
02472         html += rsvpRequestedStr( rsvpReq, role );
02473       } else {
02474         html += i18n( "Awaiting delegation response" );
02475       }
02476     }
02477     html += "</u></i>";
02478   }
02479 
02480   // Print if the organizer gave you a preset status
02481   if ( !myInc ) {
02482     if ( inc && inc->revision() == 0 ) {
02483       QString statStr = myStatusStr( inc );
02484       if ( !statStr.isEmpty() ) {
02485         html += "<br/>";
02486         html += "<i>";
02487         html += statStr;
02488         html += "</i>";
02489       }
02490     }
02491   }
02492 
02493   // Add groupware links
02494 
02495   html += "<br><table border=\"0\" cellspacing=\"0\"><tr><td>&nbsp;</td></tr>";
02496 
02497   switch ( msg->method() ) {
02498     case Scheduler::Publish:
02499     case Scheduler::Request:
02500     case Scheduler::Refresh:
02501     case Scheduler::Add:
02502     {
02503       if ( inc && inc->revision() > 0 && ( existingIncidence || !helper->calendar() ) ) {
02504         html += "<tr>";
02505         if ( inc->type() == "Todo" ) {
02506           html += "<td colspan=\"9\">";
02507           html += helper->makeLink( "reply", i18n( "[Record invitation in my task list]" ) );
02508         } else {
02509           html += "<td colspan=\"13\">";
02510           html += helper->makeLink( "reply", i18n( "[Record invitation in my calendar]" ) );
02511         }
02512         html += "</td></tr>";
02513       }
02514 
02515       if ( !myInc && a ) {
02516         html += "<tr>" + responseButtons( inc, rsvpReq, rsvpRec, helper ) + "</tr>";
02517       }
02518       break;
02519     }
02520 
02521     case Scheduler::Cancel:
02522       // Remove invitation
02523       html += "<tr>";
02524       if ( inc->type() == "Todo" ) {
02525         html += "<td colspan=\"9\">";
02526         html += helper->makeLink( "cancel", i18n( "[Remove invitation from my task list]" ) );
02527       } else {
02528         html += "<td colspan=\"13\">";
02529         html += helper->makeLink( "cancel", i18n( "[Remove invitation from my calendar]" ) );
02530       }
02531       html += "</td></tr>";
02532       break;
02533 
02534     case Scheduler::Reply:
02535     {
02536       // Record invitation response
02537       Attendee *a = 0;
02538       Attendee *ea = 0;
02539       if ( inc ) {
02540         // First, determine if this reply is really a counter in disguise.
02541         if ( replyMeansCounter( inc ) ) {
02542           html += "<tr>" + counterButtons( inc, helper ) + "</tr>";
02543           break;
02544         }
02545 
02546         // Next, maybe this is a declined reply that was delegated from me?
02547         // find first attendee who is delegated-from me
02548         // look a their PARTSTAT response, if the response is declined,
02549         // then we need to start over which means putting all the action
02550         // buttons and NOT putting on the [Record response..] button
02551         a = findDelegatedFromMyAttendee( inc );
02552         if ( a ) {
02553           if ( a->status() != Attendee::Accepted ||
02554                a->status() != Attendee::Tentative ) {
02555             html += "<tr>" + responseButtons( inc, rsvpReq, rsvpRec, helper ) + "</tr>";
02556             break;
02557           }
02558         }
02559 
02560         // Finally, simply allow a Record of the reply
02561         if ( !inc->attendees().isEmpty() ) {
02562           a = inc->attendees().first();
02563         }
02564         if ( a ) {
02565           ea = findAttendee( existingIncidence, a->email() );
02566         }
02567       }
02568       if ( ea && ( ea->status() != Attendee::NeedsAction ) && ( ea->status() == a->status() ) ) {
02569         if ( inc && inc->revision() > 0 ) {
02570           html += "<br><u><i>";
02571           html += i18n( "The response has been recorded [%1]" ).arg( ea->statusStr() );
02572           html += "</i></u>";
02573         }
02574       } else {
02575         if ( inc ) {
02576           html += "<tr><td>";
02577           if ( inc->type() == "Todo" ) {
02578             html += helper->makeLink( "reply", i18n( "[Record response in my task list]" ) );
02579           } else {
02580             html += helper->makeLink( "reply", i18n( "[Record response in my calendar]" ) );
02581           }
02582           html += "</td></tr>";
02583         }
02584       }
02585       break;
02586     }
02587 
02588     case Scheduler::Counter:
02589       // Counter proposal
02590       html += "<tr>" + counterButtons( inc, helper ) + "</tr>";
02591       break;
02592 
02593     case Scheduler::Declinecounter:
02594     case Scheduler::NoMethod:
02595       break;
02596   }
02597 
02598   // close the groupware table
02599   html += "</td></tr></table>";
02600 
02601   // Add the attendee list if I am the organizer
02602   if ( myInc && helper->calendar() ) {
02603     html += invitationAttendees( helper->calendar()->incidence( inc->uid() ) );
02604   }
02605 
02606   // close the top-level table
02607   html += "</td></tr></table><br></div>";
02608 
02609   // Add the attachment list
02610   html += invitationAttachments( helper, inc );
02611 
02612   return html;
02613 }
02614 
02615 QString IncidenceFormatter::formatICalInvitation( QString invitation,
02616                                                   Calendar *mCalendar,
02617                                                   InvitationFormatterHelper *helper )
02618 {
02619   return formatICalInvitationHelper( invitation, mCalendar, helper, false, QString() );
02620 }
02621 
02622 QString IncidenceFormatter::formatICalInvitationNoHtml( QString invitation,
02623                                                         Calendar *mCalendar,
02624                                                         InvitationFormatterHelper *helper )
02625 {
02626   return formatICalInvitationHelper( invitation, mCalendar, helper, true, QString() );
02627 }
02628 
02629 QString IncidenceFormatter::formatICalInvitationNoHtml( QString invitation,
02630                                                         Calendar *mCalendar,
02631                                                         InvitationFormatterHelper *helper,
02632                                                         const QString &sender )
02633 {
02634   return formatICalInvitationHelper( invitation, mCalendar, helper, true, sender );
02635 }
02636 
02637 
02638 /*******************************************************************
02639  *  Helper functions for the msTNEF -> VPart converter
02640  *******************************************************************/
02641 
02642 
02643 //-----------------------------------------------------------------------------
02644 
02645 static QString stringProp( KTNEFMessage* tnefMsg, const Q_UINT32& key,
02646                            const QString& fallback = QString::null)
02647 {
02648   return tnefMsg->findProp( key < 0x10000 ? key & 0xFFFF : key >> 16,
02649                             fallback );
02650 }
02651 
02652 static QString sNamedProp( KTNEFMessage* tnefMsg, const QString& name,
02653                            const QString& fallback = QString::null )
02654 {
02655   return tnefMsg->findNamedProp( name, fallback );
02656 }
02657 
02658 struct save_tz { char* old_tz; char* tz_env_str; };
02659 
02660 /* temporarily go to a different timezone */
02661 static struct save_tz set_tz( const char* _tc )
02662 {
02663   const char *tc = _tc?_tc:"UTC";
02664 
02665   struct save_tz rv;
02666 
02667   rv.old_tz = 0;
02668   rv.tz_env_str = 0;
02669 
02670   //kdDebug(5006) << "set_tz(), timezone before = " << timezone << endl;
02671 
02672   char* tz_env = 0;
02673   if( getenv( "TZ" ) ) {
02674     tz_env = strdup( getenv( "TZ" ) );
02675     rv.old_tz = tz_env;
02676   }
02677   char* tmp_env = (char*)malloc( strlen( tc ) + 4 );
02678   strcpy( tmp_env, "TZ=" );
02679   strcpy( tmp_env+3, tc );
02680   putenv( tmp_env );
02681 
02682   rv.tz_env_str = tmp_env;
02683 
02684   /* tmp_env is not free'ed -- it is part of the environment */
02685 
02686   tzset();
02687   //kdDebug(5006) << "set_tz(), timezone after = " << timezone << endl;
02688 
02689   return rv;
02690 }
02691 
02692 /* restore previous timezone */
02693 static void unset_tz( struct save_tz old_tz )
02694 {
02695   if( old_tz.old_tz ) {
02696     char* tmp_env = (char*)malloc( strlen( old_tz.old_tz ) + 4 );
02697     strcpy( tmp_env, "TZ=" );
02698     strcpy( tmp_env+3, old_tz.old_tz );
02699     putenv( tmp_env );
02700     /* tmp_env is not free'ed -- it is part of the environment */
02701     free( old_tz.old_tz );
02702   } else {
02703     /* clear TZ from env */
02704     putenv( strdup("TZ") );
02705   }
02706   tzset();
02707 
02708   /* is this OK? */
02709   if( old_tz.tz_env_str ) free( old_tz.tz_env_str );
02710 }
02711 
02712 static QDateTime utc2Local( const QDateTime& utcdt )
02713 {
02714   struct tm tmL;
02715 
02716   save_tz tmp_tz = set_tz("UTC");
02717   time_t utc = utcdt.toTime_t();
02718   unset_tz( tmp_tz );
02719 
02720   localtime_r( &utc, &tmL );
02721   return QDateTime( QDate( tmL.tm_year+1900, tmL.tm_mon+1, tmL.tm_mday ),
02722                     QTime( tmL.tm_hour, tmL.tm_min, tmL.tm_sec ) );
02723 }
02724 
02725 
02726 static QDateTime pureISOToLocalQDateTime( const QString& dtStr,
02727                                           bool bDateOnly = false )
02728 {
02729   QDate tmpDate;
02730   QTime tmpTime;
02731   int year, month, day, hour, minute, second;
02732 
02733   if( bDateOnly ) {
02734     year = dtStr.left( 4 ).toInt();
02735     month = dtStr.mid( 4, 2 ).toInt();
02736     day = dtStr.mid( 6, 2 ).toInt();
02737     hour = 0;
02738     minute = 0;
02739     second = 0;
02740   } else {
02741     year = dtStr.left( 4 ).toInt();
02742     month = dtStr.mid( 4, 2 ).toInt();
02743     day = dtStr.mid( 6, 2 ).toInt();
02744     hour = dtStr.mid( 9, 2 ).toInt();
02745     minute = dtStr.mid( 11, 2 ).toInt();
02746     second = dtStr.mid( 13, 2 ).toInt();
02747   }
02748   tmpDate.setYMD( year, month, day );
02749   tmpTime.setHMS( hour, minute, second );
02750 
02751   if( tmpDate.isValid() && tmpTime.isValid() ) {
02752     QDateTime dT = QDateTime( tmpDate, tmpTime );
02753 
02754     if( !bDateOnly ) {
02755       // correct for GMT ( == Zulu time == UTC )
02756       if (dtStr.at(dtStr.length()-1) == 'Z') {
02757         //dT = dT.addSecs( 60 * KRFCDate::localUTCOffset() );
02758         //localUTCOffset( dT ) );
02759         dT = utc2Local( dT );
02760       }
02761     }
02762     return dT;
02763   } else
02764     return QDateTime();
02765 }
02766 
02767 
02768 
02769 QString IncidenceFormatter::msTNEFToVPart( const QByteArray& tnef )
02770 {
02771   bool bOk = false;
02772 
02773   KTNEFParser parser;
02774   QBuffer buf( tnef );
02775   CalendarLocal cal ( QString::fromLatin1( "UTC" ) );
02776   KABC::Addressee addressee;
02777   KABC::VCardConverter cardConv;
02778   ICalFormat calFormat;
02779   Event* event = new Event();
02780 
02781   if( parser.openDevice( &buf ) ) {
02782     KTNEFMessage* tnefMsg = parser.message();
02783     //QMap<int,KTNEFProperty*> props = parser.message()->properties();
02784 
02785     // Everything depends from property PR_MESSAGE_CLASS
02786     // (this is added by KTNEFParser):
02787     QString msgClass = tnefMsg->findProp( 0x001A, QString::null, true )
02788       .upper();
02789     if( !msgClass.isEmpty() ) {
02790       // Match the old class names that might be used by Outlook for
02791       // compatibility with Microsoft Mail for Windows for Workgroups 3.1.
02792       bool bCompatClassAppointment = false;
02793       bool bCompatMethodRequest = false;
02794       bool bCompatMethodCancled = false;
02795       bool bCompatMethodAccepted = false;
02796       bool bCompatMethodAcceptedCond = false;
02797       bool bCompatMethodDeclined = false;
02798       if( msgClass.startsWith( "IPM.MICROSOFT SCHEDULE." ) ) {
02799         bCompatClassAppointment = true;
02800         if( msgClass.endsWith( ".MTGREQ" ) )
02801           bCompatMethodRequest = true;
02802         if( msgClass.endsWith( ".MTGCNCL" ) )
02803           bCompatMethodCancled = true;
02804         if( msgClass.endsWith( ".MTGRESPP" ) )
02805           bCompatMethodAccepted = true;
02806         if( msgClass.endsWith( ".MTGRESPA" ) )
02807           bCompatMethodAcceptedCond = true;
02808         if( msgClass.endsWith( ".MTGRESPN" ) )
02809           bCompatMethodDeclined = true;
02810       }
02811       bool bCompatClassNote = ( msgClass == "IPM.MICROSOFT MAIL.NOTE" );
02812 
02813       if( bCompatClassAppointment || "IPM.APPOINTMENT" == msgClass ) {
02814         // Compose a vCal
02815         bool bIsReply = false;
02816         QString prodID = "-//Microsoft Corporation//Outlook ";
02817         prodID += tnefMsg->findNamedProp( "0x8554", "9.0" );
02818         prodID += "MIMEDIR/EN\n";
02819         prodID += "VERSION:2.0\n";
02820         calFormat.setApplication( "Outlook", prodID );
02821 
02822         Scheduler::Method method;
02823         if( bCompatMethodRequest )
02824           method = Scheduler::Request;
02825         else if( bCompatMethodCancled )
02826           method = Scheduler::Cancel;
02827         else if( bCompatMethodAccepted || bCompatMethodAcceptedCond ||
02828                  bCompatMethodDeclined ) {
02829           method = Scheduler::Reply;
02830           bIsReply = true;
02831         } else {
02832           // pending(khz): verify whether "0x0c17" is the right tag ???
02833           //
02834           // at the moment we think there are REQUESTS and UPDATES
02835           //
02836           // but WHAT ABOUT REPLIES ???
02837           //
02838           //
02839 
02840           if( tnefMsg->findProp(0x0c17) == "1" )
02841             bIsReply = true;
02842           method = Scheduler::Request;
02843         }
02844 
02846         ScheduleMessage schedMsg(event, method, ScheduleMessage::Unknown );
02847 
02848         QString sSenderSearchKeyEmail( tnefMsg->findProp( 0x0C1D ) );
02849 
02850         if( !sSenderSearchKeyEmail.isEmpty() ) {
02851           int colon = sSenderSearchKeyEmail.find( ':' );
02852           // May be e.g. "SMTP:KHZ@KDE.ORG"
02853           if( sSenderSearchKeyEmail.find( ':' ) == -1 )
02854             sSenderSearchKeyEmail.remove( 0, colon+1 );
02855         }
02856 
02857         QString s( tnefMsg->findProp( 0x0e04 ) );
02858         QStringList attendees = QStringList::split( ';', s );
02859         if( attendees.count() ) {
02860           for( QStringList::Iterator it = attendees.begin();
02861                it != attendees.end(); ++it ) {
02862             // Skip all entries that have no '@' since these are
02863             // no mail addresses
02864             if( (*it).find('@') == -1 ) {
02865               s = (*it).stripWhiteSpace();
02866 
02867               Attendee *attendee = new Attendee( s, s, true );
02868               if( bIsReply ) {
02869                 if( bCompatMethodAccepted )
02870                   attendee->setStatus( Attendee::Accepted );
02871                 if( bCompatMethodDeclined )
02872                   attendee->setStatus( Attendee::Declined );
02873                 if( bCompatMethodAcceptedCond )
02874                   attendee->setStatus(Attendee::Tentative);
02875               } else {
02876                 attendee->setStatus( Attendee::NeedsAction );
02877                 attendee->setRole( Attendee::ReqParticipant );
02878               }
02879               event->addAttendee(attendee);
02880             }
02881           }
02882         } else {
02883           // Oops, no attendees?
02884           // This must be old style, let us use the PR_SENDER_SEARCH_KEY.
02885           s = sSenderSearchKeyEmail;
02886           if( !s.isEmpty() ) {
02887             Attendee *attendee = new Attendee( QString::null, QString::null,
02888                                                true );
02889             if( bIsReply ) {
02890               if( bCompatMethodAccepted )
02891                 attendee->setStatus( Attendee::Accepted );
02892               if( bCompatMethodAcceptedCond )
02893                 attendee->setStatus( Attendee::Declined );
02894               if( bCompatMethodDeclined )
02895                 attendee->setStatus( Attendee::Tentative );
02896             } else {
02897               attendee->setStatus(Attendee::NeedsAction);
02898               attendee->setRole(Attendee::ReqParticipant);
02899             }
02900             event->addAttendee(attendee);
02901           }
02902         }
02903         s = tnefMsg->findProp( 0x0c1f ); // look for organizer property
02904         if( s.isEmpty() && !bIsReply )
02905           s = sSenderSearchKeyEmail;
02906         // TODO: Use the common name?
02907         if( !s.isEmpty() )
02908           event->setOrganizer( s );
02909 
02910         s = tnefMsg->findProp( 0x8516 ).replace( QChar( '-' ), QString::null )
02911           .replace( QChar( ':' ), QString::null );
02912         event->setDtStart( QDateTime::fromString( s ) ); // ## Format??
02913 
02914         s = tnefMsg->findProp( 0x8517 ).replace( QChar( '-' ), QString::null )
02915           .replace( QChar( ':' ), QString::null );
02916         event->setDtEnd( QDateTime::fromString( s ) );
02917 
02918         s = tnefMsg->findProp( 0x8208 );
02919         event->setLocation( s );
02920 
02921         // is it OK to set this to OPAQUE always ??
02922         //vPart += "TRANSP:OPAQUE\n"; ###FIXME, portme!
02923         //vPart += "SEQUENCE:0\n";
02924 
02925         // is "0x0023" OK  -  or should we look for "0x0003" ??
02926         s = tnefMsg->findProp( 0x0023 );
02927         event->setUid( s );
02928 
02929         // PENDING(khz): is this value in local timezone? Must it be
02930         // adjusted? Most likely this is a bug in the server or in
02931         // Outlook - we ignore it for now.
02932         s = tnefMsg->findProp( 0x8202 ).replace( QChar( '-' ), QString::null )
02933           .replace( QChar( ':' ), QString::null );
02934         // ### libkcal always uses currentDateTime()
02935         // event->setDtStamp(QDateTime::fromString(s));
02936 
02937         s = tnefMsg->findNamedProp( "Keywords" );
02938         event->setCategories( s );
02939 
02940         s = tnefMsg->findProp( 0x1000 );
02941         event->setDescription( s );
02942 
02943         s = tnefMsg->findProp( 0x0070 );
02944         event->setSummary( s );
02945 
02946         s = tnefMsg->findProp( 0x0026 );
02947         event->setPriority( s.toInt() );
02948 
02949         // is reminder flag set ?
02950         if(!tnefMsg->findProp(0x8503).isEmpty()) {
02951           Alarm *alarm = new Alarm(event);
02952           QDateTime highNoonTime =
02953             pureISOToLocalQDateTime( tnefMsg->findProp( 0x8502 )
02954                                      .replace( QChar( '-' ), "" )
02955                                      .replace( QChar( ':' ), "" ) );
02956           QDateTime wakeMeUpTime =
02957             pureISOToLocalQDateTime( tnefMsg->findProp( 0x8560, "" )
02958                                      .replace( QChar( '-' ), "" )
02959                                      .replace( QChar( ':' ), "" ) );
02960           alarm->setTime(wakeMeUpTime);
02961 
02962           if( highNoonTime.isValid() && wakeMeUpTime.isValid() )
02963             alarm->setStartOffset( Duration( highNoonTime, wakeMeUpTime ) );
02964           else
02965             // default: wake them up 15 minutes before the appointment
02966             alarm->setStartOffset( Duration( 15*60 ) );
02967           alarm->setDisplayAlarm( i18n( "Reminder" ) );
02968 
02969           // Sorry: the different action types are not known (yet)
02970           //        so we always set 'DISPLAY' (no sounds, no images...)
02971           event->addAlarm( alarm );
02972         }
02973         cal.addEvent( event );
02974         bOk = true;
02975         // we finished composing a vCal
02976       } else if( bCompatClassNote || "IPM.CONTACT" == msgClass ) {
02977         addressee.setUid( stringProp( tnefMsg, attMSGID ) );
02978         addressee.setFormattedName( stringProp( tnefMsg, MAPI_TAG_PR_DISPLAY_NAME ) );
02979         addressee.insertEmail( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_EMAIL1EMAILADDRESS ), true );
02980         addressee.insertEmail( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_EMAIL2EMAILADDRESS ), false );
02981         addressee.insertEmail( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_EMAIL3EMAILADDRESS ), false );
02982         addressee.insertCustom( "KADDRESSBOOK", "X-IMAddress", sNamedProp( tnefMsg, MAPI_TAG_CONTACT_IMADDRESS ) );
02983         addressee.insertCustom( "KADDRESSBOOK", "X-SpousesName", stringProp( tnefMsg, MAPI_TAG_PR_SPOUSE_NAME ) );
02984         addressee.insertCustom( "KADDRESSBOOK", "X-ManagersName", stringProp( tnefMsg, MAPI_TAG_PR_MANAGER_NAME ) );
02985         addressee.insertCustom( "KADDRESSBOOK", "X-AssistantsName", stringProp( tnefMsg, MAPI_TAG_PR_ASSISTANT ) );
02986         addressee.insertCustom( "KADDRESSBOOK", "X-Department", stringProp( tnefMsg, MAPI_TAG_PR_DEPARTMENT_NAME ) );
02987         addressee.insertCustom( "KADDRESSBOOK", "X-Office", stringProp( tnefMsg, MAPI_TAG_PR_OFFICE_LOCATION ) );
02988         addressee.insertCustom( "KADDRESSBOOK", "X-Profession", stringProp( tnefMsg, MAPI_TAG_PR_PROFESSION ) );
02989 
02990         QString s = tnefMsg->findProp( MAPI_TAG_PR_WEDDING_ANNIVERSARY )
02991           .replace( QChar( '-' ), QString::null )
02992           .replace( QChar( ':' ), QString::null );
02993         if( !s.isEmpty() )
02994           addressee.insertCustom( "KADDRESSBOOK", "X-Anniversary", s );
02995 
02996         addressee.setUrl( KURL( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_WEBPAGE )  ) );
02997 
02998         // collect parts of Name entry
02999         addressee.setFamilyName( stringProp( tnefMsg, MAPI_TAG_PR_SURNAME ) );
03000         addressee.setGivenName( stringProp( tnefMsg, MAPI_TAG_PR_GIVEN_NAME ) );
03001         addressee.setAdditionalName( stringProp( tnefMsg, MAPI_TAG_PR_MIDDLE_NAME ) );
03002         addressee.setPrefix( stringProp( tnefMsg, MAPI_TAG_PR_DISPLAY_NAME_PREFIX ) );
03003         addressee.setSuffix( stringProp( tnefMsg, MAPI_TAG_PR_GENERATION ) );
03004 
03005         addressee.setNickName( stringProp( tnefMsg, MAPI_TAG_PR_NICKNAME ) );
03006         addressee.setRole( stringProp( tnefMsg, MAPI_TAG_PR_TITLE ) );
03007         addressee.setOrganization( stringProp( tnefMsg, MAPI_TAG_PR_COMPANY_NAME ) );
03008         /*
03009         the MAPI property ID of this (multiline) )field is unknown:
03010         vPart += stringProp(tnefMsg, "\n","NOTE", ... , "" );
03011         */
03012 
03013         KABC::Address adr;
03014         adr.setPostOfficeBox( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_PO_BOX ) );
03015         adr.setStreet( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_STREET ) );
03016         adr.setLocality( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_CITY ) );
03017         adr.setRegion( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_STATE_OR_PROVINCE ) );
03018         adr.setPostalCode( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_POSTAL_CODE ) );
03019         adr.setCountry( stringProp( tnefMsg, MAPI_TAG_PR_HOME_ADDRESS_COUNTRY ) );
03020         adr.setType(KABC::Address::Home);
03021         addressee.insertAddress(adr);
03022 
03023         adr.setPostOfficeBox( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSPOBOX ) );
03024         adr.setStreet( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSSTREET ) );
03025         adr.setLocality( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSCITY ) );
03026         adr.setRegion( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSSTATE ) );
03027         adr.setPostalCode( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSPOSTALCODE ) );
03028         adr.setCountry( sNamedProp( tnefMsg, MAPI_TAG_CONTACT_BUSINESSADDRESSCOUNTRY ) );
03029         adr.setType( KABC::Address::Work );
03030         addressee.insertAddress( adr );
03031 
03032         adr.setPostOfficeBox( stringProp( tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_PO_BOX ) );
03033         adr.setStreet( stringProp(tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_STREET ) );
03034         adr.setLocality( stringProp(tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_CITY ) );
03035         adr.setRegion( stringProp(tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_STATE_OR_PROVINCE ) );
03036         adr.setPostalCode( stringProp(tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_POSTAL_CODE ) );
03037         adr.setCountry( stringProp(tnefMsg, MAPI_TAG_PR_OTHER_ADDRESS_COUNTRY ) );
03038         adr.setType( KABC::Address::Dom );
03039         addressee.insertAddress(adr);
03040 
03041         // problem: the 'other' address was stored by KOrganizer in
03042         //          a line looking like the following one:
03043         // vPart += "\nADR;TYPE=dom;TYPE=intl;TYPE=parcel;TYPE=postal;TYPE=work;TYPE=home:other_pobox;;other_str1\nother_str2;other_loc;other_region;other_pocode;other_country
03044 
03045         QString nr;
03046         nr = stringProp( tnefMsg, MAPI_TAG_PR_HOME_TELEPHONE_NUMBER );
03047         addressee.insertPhoneNumber( KABC::PhoneNumber( nr, KABC::PhoneNumber::Home ) );
03048         nr = stringProp( tnefMsg, MAPI_TAG_PR_BUSINESS_TELEPHONE_NUMBER );
03049         addressee.insertPhoneNumber( KABC::PhoneNumber( nr, KABC::PhoneNumber::Work ) );
03050         nr = stringProp( tnefMsg, MAPI_TAG_PR_MOBILE_TELEPHONE_NUMBER );
03051         addressee.insertPhoneNumber( KABC::PhoneNumber( nr, KABC::PhoneNumber::Cell ) );
03052         nr = stringProp( tnefMsg, MAPI_TAG_PR_HOME_FAX_NUMBER );
03053         addressee.insertPhoneNumber( KABC::PhoneNumber( nr, KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
03054         nr = stringProp( tnefMsg, MAPI_TAG_PR_BUSINESS_FAX_NUMBER );
03055         addressee.insertPhoneNumber( KABC::PhoneNumber( nr, KABC::PhoneNumber::Fax | KABC::PhoneNumber::Work ) );
03056 
03057         s = tnefMsg->findProp( MAPI_TAG_PR_BIRTHDAY )
03058           .replace( QChar( '-' ), QString::null )
03059           .replace( QChar( ':' ), QString::null );
03060         if( !s.isEmpty() )
03061           addressee.setBirthday( QDateTime::fromString( s ) );
03062 
03063         bOk = ( !addressee.isEmpty() );
03064       } else if( "IPM.NOTE" == msgClass ) {
03065 
03066       } // else if ... and so on ...
03067     }
03068   }
03069 
03070   // Compose return string
03071   QString iCal = calFormat.toString( &cal );
03072   if( !iCal.isEmpty() )
03073     // This was an iCal
03074     return iCal;
03075 
03076   // Not an iCal - try a vCard
03077   KABC::VCardConverter converter;
03078   return converter.createVCard( addressee );
03079 }
03080 
03081 
03082 QString IncidenceFormatter::formatTNEFInvitation( const QByteArray& tnef,
03083         Calendar *mCalendar, InvitationFormatterHelper *helper )
03084 {
03085   QString vPart = IncidenceFormatter::msTNEFToVPart( tnef );
03086   QString iCal = IncidenceFormatter::formatICalInvitation( vPart, mCalendar, helper );
03087   if( !iCal.isEmpty() )
03088     return iCal;
03089   return vPart;
03090 }
03091 
03092 
03093 
03094 
03095 /*******************************************************************
03096  *  Helper functions for the Incidence tooltips
03097  *******************************************************************/
03098 
03099 class IncidenceFormatter::ToolTipVisitor : public IncidenceBase::Visitor
03100 {
03101   public:
03102     ToolTipVisitor()
03103       : mCalendar( 0 ), mRichText( true ), mResult( "" ) {}
03104 
03105     bool act( Calendar *calendar, IncidenceBase *incidence,
03106               const QDate &date=QDate(), bool richText=true )
03107     {
03108       mCalendar = calendar;
03109       mDate = date;
03110       mRichText = richText;
03111       mResult = "";
03112       return incidence ? incidence->accept( *this ) : false;
03113     }
03114     QString result() const { return mResult; }
03115 
03116   protected:
03117     bool visit( Event *event );
03118     bool visit( Todo *todo );
03119     bool visit( Journal *journal );
03120     bool visit( FreeBusy *fb );
03121 
03122     QString dateRangeText( Event *event, const QDate &date );
03123     QString dateRangeText( Todo *todo, const QDate &date );
03124     QString dateRangeText( Journal *journal );
03125     QString dateRangeText( FreeBusy *fb );
03126 
03127     QString generateToolTip( Incidence* incidence, QString dtRangeText );
03128 
03129   protected:
03130     Calendar *mCalendar;
03131     QDate mDate;
03132     bool mRichText;
03133     QString mResult;
03134 };
03135 
03136 QString IncidenceFormatter::ToolTipVisitor::dateRangeText( Event *event, const QDate &date )
03137 {
03138   QString ret;
03139   QString tmp;
03140 
03141   QDateTime startDt = event->dtStart();
03142   QDateTime endDt = event->dtEnd();
03143   if ( event->doesRecur() ) {
03144     if ( date.isValid() ) {
03145       QDateTime dt( date, QTime( 0, 0, 0 ) );
03146       int diffDays = startDt.daysTo( dt );
03147       dt = dt.addSecs( -1 );
03148       startDt.setDate( event->recurrence()->getNextDateTime( dt ).date() );
03149       if ( event->hasEndDate() ) {
03150         endDt = endDt.addDays( diffDays );
03151         if ( startDt > endDt ) {
03152           startDt.setDate( event->recurrence()->getPreviousDateTime( dt ).date() );
03153           endDt = startDt.addDays( event->dtStart().daysTo( event->dtEnd() ) );
03154         }
03155       }
03156     }
03157   }
03158   if ( event->isMultiDay() ) {
03159 
03160     tmp = "<br>" + i18n("Event start", "<i>From:</i>&nbsp;%1");
03161     if (event->doesFloat())
03162       ret += tmp.arg( IncidenceFormatter::dateToString( startDt, false ).replace(" ", "&nbsp;") );
03163     else
03164       ret += tmp.arg( IncidenceFormatter::dateToString( startDt ).replace(" ", "&nbsp;") );
03165 
03166     tmp = "<br>" + i18n("Event end","<i>To:</i>&nbsp;%1");
03167     if (event->doesFloat())
03168       ret += tmp.arg( IncidenceFormatter::dateToString( endDt, false ).replace(" ", "&nbsp;") );
03169     else
03170       ret += tmp.arg( IncidenceFormatter::dateToString( endDt ).replace(" ", "&nbsp;") );
03171 
03172   } else {
03173 
03174     ret += "<br>"+i18n("<i>Date:</i>&nbsp;%1").
03175            arg( IncidenceFormatter::dateToString( startDt, false ).replace(" ", "&nbsp;") );
03176     if ( !event->doesFloat() ) {
03177       const QString dtStartTime =
03178         IncidenceFormatter::timeToString( startDt, true ).replace( " ", "&nbsp;" );
03179       const QString dtEndTime =
03180         IncidenceFormatter::timeToString( endDt, true ).replace( " ", "&nbsp;" );
03181       if ( dtStartTime == dtEndTime ) { // to prevent 'Time: 17:00 - 17:00'
03182         tmp = "<br>" + i18n("time for event, &nbsp; to prevent ugly line breaks",
03183         "<i>Time:</i>&nbsp;%1").
03184         arg( dtStartTime );
03185       } else {
03186         tmp = "<br>" + i18n("time range for event, &nbsp; to prevent ugly line breaks",
03187         "<i>Time:</i>&nbsp;%1&nbsp;-&nbsp;%2").
03188         arg( dtStartTime, dtEndTime );
03189       }
03190       ret += tmp;
03191     }
03192 
03193   }
03194   return ret;
03195 }
03196 
03197 QString IncidenceFormatter::ToolTipVisitor::dateRangeText( Todo *todo, const QDate &date )
03198 {
03199   QString ret;
03200   bool floats( todo->doesFloat() );
03201 
03202   if ( todo->hasStartDate() && todo->dtStart().isValid() ) {
03203     QDateTime startDt = todo->dtStart();
03204     if ( todo->doesRecur() ) {
03205       if ( date.isValid() ) {
03206         startDt.setDate( date );
03207       }
03208     }
03209     ret += "<br>" +
03210            i18n("<i>Start:</i>&nbsp;%1").
03211            arg( IncidenceFormatter::dateTimeToString( startDt, floats, false ).
03212                 replace( " ", "&nbsp;" ) );
03213   }
03214 
03215   if ( todo->hasDueDate() && todo->dtDue().isValid() ) {
03216     QDateTime dueDt = todo->dtDue();
03217     if ( todo->doesRecur() ) {
03218       if ( date.isValid() ) {
03219         QDateTime dt( date, QTime( 0, 0, 0 ) );
03220         dt = dt.addSecs( -1 );
03221         dueDt.setDate( todo->recurrence()->getNextDateTime( dt ).date() );
03222       }
03223     }
03224     ret += "<br>" +
03225            i18n("<i>Due:</i>&nbsp;%1").
03226            arg( IncidenceFormatter::dateTimeToString( dueDt, floats, false ).
03227                 replace( " ", "&nbsp;" ) );
03228   }
03229 
03230   // Print priority and completed info here, for lack of a better place
03231 
03232   if ( todo->priority() > 0 ) {
03233     ret += "<br>";
03234     ret += "<i>" + i18n( "Priority:" ) + "</i>" + "&nbsp;";
03235     ret += QString::number( todo->priority() );
03236   }
03237 
03238   ret += "<br>";
03239   if ( todo->isCompleted() ) {
03240     ret += "<i>" + i18n( "Completed:" ) + "</i>" + "&nbsp;";
03241     ret += todo->completedStr().replace( " ", "&nbsp;" );
03242   } else {
03243     ret += "<i>" + i18n( "Percent Done:" ) + "</i>" + "&nbsp;";
03244     ret += i18n( "%1%" ).arg( todo->percentComplete() );
03245   }
03246 
03247   return ret;
03248 }
03249 
03250 QString IncidenceFormatter::ToolTipVisitor::dateRangeText( Journal*journal )
03251 {
03252   QString ret;
03253   if (journal->dtStart().isValid() ) {
03254     ret += "<br>" +
03255            i18n("<i>Date:</i>&nbsp;%1").
03256            arg( IncidenceFormatter::dateToString( journal->dtStart(), false ) );
03257   }
03258   return ret;
03259 }
03260 
03261 QString IncidenceFormatter::ToolTipVisitor::dateRangeText( FreeBusy *fb )
03262 {
03263   QString tmp( "<br>" + i18n("<i>Period start:</i>&nbsp;%1") );
03264   QString ret = tmp.arg( KGlobal::locale()->formatDateTime( fb->dtStart() ) );
03265   tmp = "<br>" + i18n("<i>Period start:</i>&nbsp;%1");
03266   ret += tmp.arg( KGlobal::locale()->formatDateTime( fb->dtEnd() ) );
03267   return ret;
03268 }
03269 
03270 
03271 
03272 bool IncidenceFormatter::ToolTipVisitor::visit( Event *event )
03273 {
03274   mResult = generateToolTip( event, dateRangeText( event, mDate ) );
03275   return !mResult.isEmpty();
03276 }
03277 
03278 bool IncidenceFormatter::ToolTipVisitor::visit( Todo *todo )
03279 {
03280   mResult = generateToolTip( todo, dateRangeText( todo, mDate ) );
03281   return !mResult.isEmpty();
03282 }
03283 
03284 bool IncidenceFormatter::ToolTipVisitor::visit( Journal *journal )
03285 {
03286   mResult = generateToolTip( journal, dateRangeText( journal ) );
03287   return !mResult.isEmpty();
03288 }
03289 
03290 bool IncidenceFormatter::ToolTipVisitor::visit( FreeBusy *fb )
03291 {
03292   mResult = "<qt><b>" + i18n("Free/Busy information for %1")
03293         .arg(fb->organizer().fullName()) + "</b>";
03294   mResult += dateRangeText( fb );
03295   mResult += "</qt>";
03296   return !mResult.isEmpty();
03297 }
03298 
03299 static QString tooltipPerson( const QString& email, QString name )
03300 {
03301   // Make the search, if there is an email address to search on,
03302   // and name is missing
03303   if ( name.isEmpty() && !email.isEmpty() ) {
03304     KABC::AddressBook *add_book = KABC::StdAddressBook::self( true );
03305     KABC::Addressee::List addressList = add_book->findByEmail( email );
03306     if ( !addressList.isEmpty() ) {
03307       KABC::Addressee o = addressList.first();
03308       if ( !o.isEmpty() && addressList.size() < 2 ) {
03309         // use the name from the addressbook
03310         name = o.formattedName();
03311       }
03312     }
03313   }
03314 
03315   // Show the attendee
03316   QString tmpString = ( name.isEmpty() ? email : name );
03317 
03318   return tmpString;
03319 }
03320 
03321 static QString etc = i18n( "elipsis", "..." );
03322 static QString tooltipFormatAttendeeRoleList( Incidence *incidence, Attendee::Role role )
03323 {
03324   int maxNumAtts = 8; // maximum number of people to print per attendee role
03325   QString sep = i18n( "separator for lists of people names", ", " );
03326   int sepLen = sep.length();
03327 
03328   int i = 0;
03329   QString tmpStr;
03330   Attendee::List::ConstIterator it;
03331   Attendee::List attendees = incidence->attendees();
03332 
03333   for( it = attendees.begin(); it != attendees.end(); ++it ) {
03334     Attendee *a = *it;
03335     if ( a->role() != role ) {
03336       // skip not this role
03337       continue;
03338     }
03339     if ( a->email() == incidence->organizer().email() ) {
03340       // skip attendee that is also the organizer
03341       continue;
03342     }
03343     if ( i == maxNumAtts ) {
03344       tmpStr += etc;
03345       break;
03346     }
03347     tmpStr += tooltipPerson( a->email(), a->name() );
03348     if ( !a->delegator().isEmpty() ) {
03349       tmpStr += i18n(" (delegated by %1)" ).arg( a->delegator() );
03350     }
03351     if ( !a->delegate().isEmpty() ) {
03352       tmpStr += i18n(" (delegated to %1)" ).arg( a->delegate() );
03353     }
03354     tmpStr += sep;
03355     i++;
03356   }
03357   if ( tmpStr.endsWith( sep ) ) {
03358     tmpStr.truncate( tmpStr.length() - sepLen );
03359   }
03360   return tmpStr;
03361 }
03362 
03363 static QString tooltipFormatAttendees( Incidence *incidence )
03364 {
03365   QString tmpStr, str;
03366 
03367   // Add organizer link
03368   int attendeeCount = incidence->attendees().count();
03369   if ( attendeeCount > 1 ||
03370        ( attendeeCount == 1 &&
03371          incidence->organizer().email() != incidence->attendees().first()->email() ) ) {
03372     tmpStr += "<i>" + i18n( "Organizer:" ) + "</i>" + "&nbsp;";
03373     tmpStr += tooltipPerson( incidence->organizer().email(),
03374                              incidence->organizer().name() );
03375   }
03376 
03377   // Add "chair"
03378   str = tooltipFormatAttendeeRoleList( incidence, Attendee::Chair );
03379   if ( !str.isEmpty() ) {
03380     tmpStr += "<br><i>" + i18n( "Chair:" ) + "</i>" + "&nbsp;";
03381     tmpStr += str;
03382   }
03383 
03384   // Add required participants
03385   str = tooltipFormatAttendeeRoleList( incidence, Attendee::ReqParticipant );
03386   if ( !str.isEmpty() ) {
03387     tmpStr += "<br><i>" + i18n( "Required Participants:" ) + "</i>" + "&nbsp;";
03388     tmpStr += str;
03389   }
03390 
03391   // Add optional participants
03392   str = tooltipFormatAttendeeRoleList( incidence, Attendee::OptParticipant );
03393   if ( !str.isEmpty() ) {
03394     tmpStr += "<br><i>" + i18n( "Optional Participants:" ) + "</i>" + "&nbsp;";
03395     tmpStr += str;
03396   }
03397 
03398   // Add observers
03399   str = tooltipFormatAttendeeRoleList( incidence, Attendee::NonParticipant );
03400   if ( !str.isEmpty() ) {
03401     tmpStr += "<br><i>" + i18n( "Observers:" ) + "</i>" + "&nbsp;";
03402     tmpStr += str;
03403   }
03404 
03405   return tmpStr;
03406 }
03407 
03408 QString IncidenceFormatter::ToolTipVisitor::generateToolTip( Incidence* incidence, QString dtRangeText )
03409 {
03410   uint maxDescLen = 120; // maximum description chars to print (before elipsis)
03411 
03412   if ( !incidence ) {
03413     return QString::null;
03414   }
03415 
03416   QString tmp = "<qt>";
03417 
03418   // header
03419   tmp += "<b>" + incidence->summary().replace( "\n", "<br>" ) + "</b>";
03420   //NOTE: using <hr> seems to confuse Qt3 tooltips in some cases so use "-----"
03421   tmp += "<br>----------<br>";
03422 
03423   if ( mCalendar ) {
03424     QString calStr = IncidenceFormatter::resourceString( mCalendar, incidence );
03425     if ( !calStr.isEmpty() ) {
03426       tmp += "<i>" + i18n( "Calendar:" ) + "</i>" + "&nbsp;";
03427       tmp += calStr;
03428     }
03429   }
03430 
03431   tmp += dtRangeText;
03432 
03433   if ( !incidence->location().isEmpty() ) {
03434     tmp += "<br>";
03435     tmp += "<i>" + i18n( "Location:" ) + "</i>" + "&nbsp;";
03436     tmp += incidence->location().replace( "\n", "<br>" );
03437   }
03438 
03439   QString durStr = IncidenceFormatter::durationString( incidence );
03440   if ( !durStr.isEmpty() ) {
03441     tmp += "<br>";
03442     tmp += "<i>" + i18n( "Duration:" ) + "</i>" + "&nbsp;";
03443     tmp += durStr;
03444   }
03445 
03446   if ( incidence->doesRecur() ) {
03447     tmp += "<br>";
03448     tmp += "<i>" + i18n( "Recurrence:" ) + "</i>" + "&nbsp;";
03449     tmp += IncidenceFormatter::recurrenceString( incidence );
03450   }
03451 
03452   if ( !incidence->description().isEmpty() ) {
03453     QString desc( incidence->description() );
03454     if ( desc.length() > maxDescLen ) {
03455       desc = desc.left( maxDescLen ) + etc;
03456     }
03457     tmp += "<br>----------<br>";
03458     tmp += "<i>" + i18n( "Description:" ) + "</i>" + "<br>";
03459     tmp += desc.replace( "\n", "<br>" );
03460     tmp += "<br>----------";
03461   }
03462 
03463   int reminderCount = incidence->alarms().count();
03464   if ( reminderCount > 0 && incidence->isAlarmEnabled() ) {
03465     tmp += "<br>";
03466     tmp += "<i>" + i18n( "Reminder:", "%n Reminders:", reminderCount ) + "</i>" + "&nbsp;";
03467     tmp += IncidenceFormatter::reminderStringList( incidence ).join( ", " );
03468   }
03469 
03470   tmp += "<br>";
03471   tmp += tooltipFormatAttendees( incidence );
03472 
03473   int categoryCount = incidence->categories().count();
03474   if ( categoryCount > 0 ) {
03475     tmp += "<br>";
03476     tmp += "<i>" + i18n( "Category:", "%n Categories:", categoryCount ) + "</i>" + "&nbsp;";
03477     tmp += incidence->categories().join( ", " );
03478   }
03479 
03480   tmp += "</qt>";
03481   return tmp;
03482 }
03483 
03484 QString IncidenceFormatter::toolTipString( IncidenceBase *incidence, bool richText )
03485 {
03486   return toolTipStr( 0, incidence, QDate(), richText );
03487 }
03488 
03489 QString IncidenceFormatter::toolTipStr( Calendar *calendar,
03490                                         IncidenceBase *incidence,
03491                                         const QDate &date,
03492                                         bool richText )
03493 {
03494   ToolTipVisitor v;
03495   if ( v.act( calendar, incidence, date, richText ) ) {
03496     return v.result();
03497   } else {
03498     return QString::null;
03499   }
03500 }
03501 
03502 /*******************************************************************
03503  *  Helper functions for the Incidence tooltips
03504  *******************************************************************/
03505 
03506 class IncidenceFormatter::MailBodyVisitor : public IncidenceBase::Visitor
03507 {
03508   public:
03509     MailBodyVisitor() : mResult( "" ) {}
03510 
03511     bool act( IncidenceBase *incidence )
03512     {
03513       mResult = "";
03514       return incidence ? incidence->accept( *this ) : false;
03515     }
03516     QString result() const { return mResult; }
03517 
03518   protected:
03519     bool visit( Event *event );
03520     bool visit( Todo *todo );
03521     bool visit( Journal *journal );
03522     bool visit( FreeBusy * ) { mResult = i18n("This is a Free Busy Object"); return !mResult.isEmpty(); }
03523   protected:
03524     QString mResult;
03525 };
03526 
03527 
03528 static QString mailBodyIncidence( Incidence *incidence )
03529 {
03530   QString body;
03531   if ( !incidence->summary().isEmpty() ) {
03532     body += i18n("Summary: %1\n").arg( incidence->summary() );
03533   }
03534   if ( !incidence->organizer().isEmpty() ) {
03535     body += i18n("Organizer: %1\n").arg( incidence->organizer().fullName() );
03536   }
03537   if ( !incidence->location().isEmpty() ) {
03538     body += i18n("Location: %1\n").arg( incidence->location() );
03539   }
03540   return body;
03541 }
03542 
03543 bool IncidenceFormatter::MailBodyVisitor::visit( Event *event )
03544 {
03545   QString recurrence[]= {i18n("no recurrence", "None"),
03546     i18n("Minutely"), i18n("Hourly"), i18n("Daily"),
03547     i18n("Weekly"), i18n("Monthly Same Day"), i18n("Monthly Same Position"),
03548     i18n("Yearly"), i18n("Yearly"), i18n("Yearly")};
03549 
03550   mResult = mailBodyIncidence( event );
03551   mResult += i18n("Start Date: %1\n").
03552              arg( IncidenceFormatter::dateToString( event->dtStart(), true ) );
03553   if ( !event->doesFloat() ) {
03554     mResult += i18n("Start Time: %1\n").
03555                arg( IncidenceFormatter::timeToString( event->dtStart(), true ) );
03556   }
03557   if ( event->dtStart() != event->dtEnd() ) {
03558     mResult += i18n("End Date: %1\n").
03559                arg( IncidenceFormatter::dateToString( event->dtEnd(), true ) );
03560   }
03561   if ( !event->doesFloat() ) {
03562     mResult += i18n("End Time: %1\n").
03563                arg( IncidenceFormatter::timeToString( event->dtEnd(), true ) );
03564   }
03565   if ( event->doesRecur() ) {
03566     Recurrence *recur = event->recurrence();
03567     // TODO: Merge these two to one of the form "Recurs every 3 days"
03568     mResult += i18n("Recurs: %1\n")
03569              .arg( recurrence[ recur->recurrenceType() ] );
03570     mResult += i18n("Frequency: %1\n")
03571              .arg( event->recurrence()->frequency() );
03572 
03573     if ( recur->duration() > 0 ) {
03574       mResult += i18n ("Repeats once", "Repeats %n times", recur->duration());
03575       mResult += '\n';
03576     } else {
03577       if ( recur->duration() != -1 ) {
03578 // TODO_Recurrence: What to do with floating
03579         QString endstr;
03580         if ( event->doesFloat() ) {
03581           endstr = KGlobal::locale()->formatDate( recur->endDate() );
03582         } else {
03583           endstr = KGlobal::locale()->formatDateTime( recur->endDateTime() );
03584         }
03585         mResult += i18n("Repeat until: %1\n").arg( endstr );
03586       } else {
03587         mResult += i18n("Repeats forever\n");
03588       }
03589     }
03590   }
03591   QString details = event->description();
03592   if ( !details.isEmpty() ) {
03593     mResult += i18n("Details:\n%1\n").arg( details );
03594   }
03595   return !mResult.isEmpty();
03596 }
03597 
03598 bool IncidenceFormatter::MailBodyVisitor::visit( Todo *todo )
03599 {
03600   mResult = mailBodyIncidence( todo );
03601 
03602   if ( todo->hasStartDate() ) {
03603     mResult += i18n("Start Date: %1\n").
03604                arg( IncidenceFormatter::dateToString( todo->dtStart( false ), true ) );
03605     if ( !todo->doesFloat() ) {
03606       mResult += i18n("Start Time: %1\n").
03607                  arg( IncidenceFormatter::timeToString( todo->dtStart( false ),true ) );
03608     }
03609   }
03610   if ( todo->hasDueDate() ) {
03611     mResult += i18n("Due Date: %1\n").
03612                arg( IncidenceFormatter::dateToString( todo->dtDue(), true ) );
03613     if ( !todo->doesFloat() ) {
03614       mResult += i18n("Due Time: %1\n").
03615                  arg( IncidenceFormatter::timeToString( todo->dtDue(), true ) );
03616     }
03617   }
03618   QString details = todo->description();
03619   if ( !details.isEmpty() ) {
03620     mResult += i18n("Details:\n%1\n").arg( details );
03621   }
03622   return !mResult.isEmpty();
03623 }
03624 
03625 bool IncidenceFormatter::MailBodyVisitor::visit( Journal *journal )
03626 {
03627   mResult = mailBodyIncidence( journal );
03628   mResult += i18n("Date: %1\n").
03629              arg( IncidenceFormatter::dateToString( journal->dtStart(), true ) );
03630   if ( !journal->doesFloat() ) {
03631     mResult += i18n("Time: %1\n").
03632                arg( IncidenceFormatter::timeToString( journal->dtStart(), true ) );
03633   }
03634   if ( !journal->description().isEmpty() )
03635     mResult += i18n("Text of the journal:\n%1\n").arg( journal->description() );
03636   return !mResult.isEmpty();
03637 }
03638 
03639 
03640 
03641 QString IncidenceFormatter::mailBodyString( IncidenceBase *incidence )
03642 {
03643   if ( !incidence )
03644     return QString::null;
03645 
03646   MailBodyVisitor v;
03647   if ( v.act( incidence ) ) {
03648     return v.result();
03649   }
03650   return QString::null;
03651 }
03652 
03653 static QString recurEnd( Incidence *incidence )
03654 {
03655   QString endstr;
03656   if ( incidence->doesFloat() ) {
03657     endstr = KGlobal::locale()->formatDate( incidence->recurrence()->endDate() );
03658   } else {
03659     endstr = KGlobal::locale()->formatDateTime( incidence->recurrence()->endDateTime() );
03660   }
03661   return endstr;
03662 }
03663 
03664 /************************************
03665  *  More static formatting functions
03666  ************************************/
03667 QString IncidenceFormatter::recurrenceString( Incidence *incidence )
03668 {
03669   if ( !incidence->doesRecur() ) {
03670     return i18n( "No recurrence" );
03671   }
03672   QStringList dayList;
03673   dayList.append( i18n( "31st Last" ) );
03674   dayList.append( i18n( "30th Last" ) );
03675   dayList.append( i18n( "29th Last" ) );
03676   dayList.append( i18n( "28th Last" ) );
03677   dayList.append( i18n( "27th Last" ) );
03678   dayList.append( i18n( "26th Last" ) );
03679   dayList.append( i18n( "25th Last" ) );
03680   dayList.append( i18n( "24th Last" ) );
03681   dayList.append( i18n( "23rd Last" ) );
03682   dayList.append( i18n( "22nd Last" ) );
03683   dayList.append( i18n( "21st Last" ) );
03684   dayList.append( i18n( "20th Last" ) );
03685   dayList.append( i18n( "19th Last" ) );
03686   dayList.append( i18n( "18th Last" ) );
03687   dayList.append( i18n( "17th Last" ) );
03688   dayList.append( i18n( "16th Last" ) );
03689   dayList.append( i18n( "15th Last" ) );
03690   dayList.append( i18n( "14th Last" ) );
03691   dayList.append( i18n( "13th Last" ) );
03692   dayList.append( i18n( "12th Last" ) );
03693   dayList.append( i18n( "11th Last" ) );
03694   dayList.append( i18n( "10th Last" ) );
03695   dayList.append( i18n( "9th Last" ) );
03696   dayList.append( i18n( "8th Last" ) );
03697   dayList.append( i18n( "7th Last" ) );
03698   dayList.append( i18n( "6th Last" ) );
03699   dayList.append( i18n( "5th Last" ) );
03700   dayList.append( i18n( "4th Last" ) );
03701   dayList.append( i18n( "3rd Last" ) );
03702   dayList.append( i18n( "2nd Last" ) );
03703   dayList.append( i18n( "last day of the month", "Last" ) );
03704   dayList.append( i18n( "unknown day of the month", "unknown" ) ); //#31 - zero offset from UI
03705   dayList.append( i18n( "1st" ) );
03706   dayList.append( i18n( "2nd" ) );
03707   dayList.append( i18n( "3rd" ) );
03708   dayList.append( i18n( "4th" ) );
03709   dayList.append( i18n( "5th" ) );
03710   dayList.append( i18n( "6th" ) );
03711   dayList.append( i18n( "7th" ) );
03712   dayList.append( i18n( "8th" ) );
03713   dayList.append( i18n( "9th" ) );
03714   dayList.append( i18n( "10th" ) );
03715   dayList.append( i18n( "11th" ) );
03716   dayList.append( i18n( "12th" ) );
03717   dayList.append( i18n( "13th" ) );
03718   dayList.append( i18n( "14th" ) );
03719   dayList.append( i18n( "15th" ) );
03720   dayList.append( i18n( "16th" ) );
03721   dayList.append( i18n( "17th" ) );
03722   dayList.append( i18n( "18th" ) );
03723   dayList.append( i18n( "19th" ) );
03724   dayList.append( i18n( "20th" ) );
03725   dayList.append( i18n( "21st" ) );
03726   dayList.append( i18n( "22nd" ) );
03727   dayList.append( i18n( "23rd" ) );
03728   dayList.append( i18n( "24th" ) );
03729   dayList.append( i18n( "25th" ) );
03730   dayList.append( i18n( "26th" ) );
03731   dayList.append( i18n( "27th" ) );
03732   dayList.append( i18n( "28th" ) );
03733   dayList.append( i18n( "29th" ) );
03734   dayList.append( i18n( "30th" ) );
03735   dayList.append( i18n( "31st" ) );
03736   int weekStart = KGlobal::locale()->weekStartDay();
03737   QString dayNames;
03738   QString recurStr, txt;
03739   const KCalendarSystem *calSys = KGlobal::locale()->calendar();
03740   Recurrence *recur = incidence->recurrence();
03741   switch ( recur->recurrenceType() ) {
03742   case Recurrence::rNone:
03743     return i18n( "No recurrence" );
03744 
03745   case Recurrence::rMinutely:
03746     recurStr = i18n( "Recurs every minute", "Recurs every %n minutes", recur->frequency() );
03747     if ( recur->duration() != -1 ) {
03748       txt = i18n( "%1 until %2" ).arg( recurStr ).arg( recurEnd( incidence ) );
03749       if ( recur->duration() >  0 ) {
03750         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03751       }
03752       return txt;
03753     }
03754     return recurStr;
03755 
03756   case Recurrence::rHourly:
03757     recurStr = i18n( "Recurs hourly", "Recurs every %n hours", recur->frequency() );
03758     if ( recur->duration() != -1 ) {
03759       txt = i18n( "%1 until %2" ).arg( recurStr ).arg( recurEnd( incidence ) );
03760       if ( recur->duration() >  0 ) {
03761         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03762       }
03763       return txt;
03764     }
03765     return recurStr;
03766 
03767   case Recurrence::rDaily:
03768     recurStr = i18n( "Recurs daily", "Recurs every %n days", recur->frequency() );
03769     if ( recur->duration() != -1 ) {
03770 
03771       txt = i18n( "%1 until %2" ).arg( recurStr ).arg( recurEnd( incidence ) );
03772       if ( recur->duration() >  0 ) {
03773         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03774       }
03775       return txt;
03776     }
03777     return recurStr;
03778 
03779   case Recurrence::rWeekly:
03780   {
03781     recurStr = i18n( "Recurs weekly", "Recurs every %n weeks", recur->frequency() );
03782 
03783     bool addSpace = false;
03784     for ( int i = 0; i < 7; ++i ) {
03785       if ( recur->days().testBit( ( i + weekStart + 6 ) % 7 ) ) {
03786         if ( addSpace ) {
03787           dayNames.append( i18n( "separator for list of days", ", " ) );
03788         }
03789         dayNames.append( calSys->weekDayName( ( ( i + weekStart + 6 ) % 7 ) + 1, true ) );
03790         addSpace = true;
03791       }
03792     }
03793     if ( dayNames.isEmpty() ) {
03794       dayNames = i18n( "Recurs weekly on no days", "no days" );
03795     }
03796     if ( recur->duration() != -1 ) {
03797       txt = i18n( "%1 on %2 until %3" ).
03798             arg( recurStr ).arg( dayNames ).arg( recurEnd( incidence ) );
03799       if ( recur->duration() >  0 ) {
03800         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03801       }
03802       return txt;
03803     }
03804     txt = i18n( "%1 on %2" ).arg( recurStr ).arg( dayNames );
03805     return txt;
03806   }
03807 
03808   case Recurrence::rMonthlyPos:
03809   {
03810     recurStr = i18n( "Recurs monthly", "Recurs every %n months", recur->frequency() );
03811 
03812     KCal::RecurrenceRule::WDayPos rule = recur->monthPositions()[0];
03813     if ( recur->duration() != -1 ) {
03814       txt = i18n( "%1 on the %2 %3 until %4" ).
03815             arg( recurStr ).
03816             arg( dayList[rule.pos() + 31] ).
03817             arg( calSys->weekDayName( rule.day(), false ) ).
03818             arg( recurEnd( incidence ) );
03819       if ( recur->duration() >  0 ) {
03820         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03821       }
03822       return txt;
03823     }
03824     txt = i18n( "%1 on the %2 %3" ).
03825           arg( recurStr ).
03826           arg( dayList[rule.pos() + 31] ).
03827           arg( calSys->weekDayName( rule.day(), false ) );
03828     return txt;
03829   }
03830 
03831   case Recurrence::rMonthlyDay:
03832   {
03833     recurStr = i18n( "Recurs monthly", "Recurs every %n months", recur->frequency() );
03834 
03835     int days = recur->monthDays()[0];
03836     if ( recur->duration() != -1 ) {
03837       txt = i18n( "%1 on the %2 day until %3" ).
03838             arg( recurStr ).
03839             arg( dayList[days + 31] ).
03840             arg( recurEnd( incidence ) );
03841       if ( recur->duration() >  0 ) {
03842         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03843       }
03844       return txt;
03845     }
03846     txt = i18n( "%1 on the %2 day" ).arg( recurStr ).arg( dayList[days + 31] );
03847     return txt;
03848   }
03849 
03850   case Recurrence::rYearlyMonth:
03851   {
03852     recurStr = i18n( "Recurs yearly", "Recurs every %n years", recur->frequency() );
03853 
03854     if ( recur->duration() != -1 ) {
03855       if ( !recur->yearDates().isEmpty() ) {
03856         txt = i18n( "%1 on %2 %3 until %4" ).
03857               arg( recurStr ).
03858               arg( calSys->monthName( recur->yearMonths()[0], recur->startDate().year() ) ).
03859               arg( dayList[ recur->yearDates()[0] + 31 ] ).
03860               arg( recurEnd( incidence ) );
03861         if ( recur->duration() >  0 ) {
03862           txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03863         }
03864         return txt;
03865       }
03866     }
03867     if ( !recur->yearDates().isEmpty() ) {
03868       txt = i18n( "%1 on %2 %3" ).
03869             arg( recurStr ).
03870             arg( calSys->monthName( recur->yearMonths()[0], recur->startDate().year() ) ).
03871             arg( dayList[ recur->yearDates()[0] + 31 ] );
03872       return txt;
03873     } else {
03874       if ( !recur->yearMonths().isEmpty() ) {
03875         txt = i18n( "Recurs yearly on %1 %2" ).
03876               arg( calSys->monthName( recur->yearMonths()[0],
03877                                       recur->startDate().year() ) ).
03878               arg( dayList[ recur->startDate().day() + 31 ] );
03879       } else {
03880         txt = i18n( "Recurs yearly on %1 %2" ).
03881               arg( calSys->monthName( recur->startDate().month(),
03882                                       recur->startDate().year() ) ).
03883               arg( dayList[ recur->startDate().day() + 31 ] );
03884       }
03885       return txt;
03886     }
03887   }
03888 
03889   case Recurrence::rYearlyDay:
03890   {
03891     recurStr = i18n( "Recurs yearly", "Recurs every %n years", recur->frequency() );
03892 
03893     if ( recur->duration() != -1 ) {
03894       txt = i18n( "%1 on day %2 until %3" ).
03895             arg( recurStr ).
03896             arg( recur->yearDays()[0] ).
03897             arg( recurEnd( incidence ) );
03898       if ( recur->duration() >  0 ) {
03899         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03900       }
03901       return txt;
03902     }
03903     txt = i18n( "%1 on day %2" ).arg( recurStr ).arg( recur->yearDays()[0] );
03904     return txt;
03905   }
03906 
03907   case Recurrence::rYearlyPos:
03908   {
03909     recurStr = i18n( "Every year", "Every %n years", recur->frequency() );
03910 
03911     KCal::RecurrenceRule::WDayPos rule = recur->yearPositions()[0];
03912     if ( recur->duration() != -1 ) {
03913       txt = i18n( "%1 on the %2 %3 of %4 until %5" ).
03914             arg( recurStr ).
03915             arg( dayList[rule.pos() + 31] ).
03916             arg( calSys->weekDayName( rule.day(), false ) ).
03917             arg( calSys->monthName( recur->yearMonths()[0], recur->startDate().year() ) ).
03918             arg( recurEnd( incidence ) );
03919       if ( recur->duration() >  0 ) {
03920         txt += i18n( " (%1 occurrences)" ).arg( recur->duration() );
03921       }
03922       return txt;
03923     }
03924     txt = i18n( "%1 on the %2 %3 of %4" ).
03925           arg( recurStr ).
03926           arg( dayList[rule.pos() + 31] ).
03927           arg( calSys->weekDayName( rule.day(), false ) ).
03928           arg( calSys->monthName( recur->yearMonths()[0], recur->startDate().year() ) );
03929     return txt;
03930   }
03931 
03932   default:
03933     return i18n( "Incidence recurs" );
03934   }
03935 }
03936 
03937 QString IncidenceFormatter::timeToString( const QDateTime &date, bool shortfmt )
03938 {
03939   return KGlobal::locale()->formatTime( date.time(), !shortfmt );
03940 }
03941 
03942 QString IncidenceFormatter::dateToString( const QDateTime &date, bool shortfmt )
03943 {
03944   return
03945     KGlobal::locale()->formatDate( date.date(), shortfmt );
03946 }
03947 
03948 QString IncidenceFormatter::dateTimeToString( const QDateTime &date,
03949                                               bool allDay, bool shortfmt )
03950 {
03951   if ( allDay ) {
03952     return dateToString( date, shortfmt );
03953   }
03954 
03955   return  KGlobal::locale()->formatDateTime( date, shortfmt );
03956 }
03957 
03958 QString IncidenceFormatter::resourceString( Calendar *calendar, Incidence *incidence )
03959 {
03960   if ( !calendar || !incidence ) {
03961     return QString::null;
03962   }
03963 
03964   CalendarResources *calendarResource = dynamic_cast<CalendarResources*>( calendar );
03965   if ( !calendarResource ) {
03966     return QString::null;
03967   }
03968 
03969   ResourceCalendar *resourceCalendar = calendarResource->resource( incidence );
03970   if ( resourceCalendar ) {
03971     if ( !resourceCalendar->subresources().isEmpty() ) {
03972       QString subRes = resourceCalendar->subresourceIdentifier( incidence );
03973       if ( subRes.isEmpty() ) {
03974         return resourceCalendar->resourceName();
03975       } else {
03976         return resourceCalendar->labelForSubresource( subRes );
03977       }
03978     }
03979     return resourceCalendar->resourceName();
03980   }
03981 
03982   return QString::null;
03983 }
03984 
03985 static QString secs2Duration( int secs )
03986 {
03987   QString tmp;
03988   int days = secs / 86400;
03989   if ( days > 0 ) {
03990     tmp += i18n( "1 day", "%n days", days );
03991     tmp += ' ';
03992     secs -= ( days * 86400 );
03993   }
03994   int hours = secs / 3600;
03995   if ( hours > 0 ) {
03996     tmp += i18n( "1 hour", "%n hours", hours );
03997     tmp += ' ';
03998     secs -= ( hours * 3600 );
03999   }
04000   int mins = secs / 60;
04001   if ( mins > 0 ) {
04002     tmp += i18n( "1 minute", "%n minutes",  mins );
04003   }
04004   return tmp;
04005 }
04006 
04007 QString IncidenceFormatter::durationString( Incidence *incidence )
04008 {
04009   QString tmp;
04010   if ( incidence->type() == "Event" ) {
04011     Event *event = static_cast<Event *>( incidence );
04012     if ( event->hasEndDate() ) {
04013       if ( !event->doesFloat() ) {
04014         tmp = secs2Duration( event->dtStart().secsTo( event->dtEnd() ) );
04015       } else {
04016         tmp = i18n( "1 day", "%n days",
04017                     event->dtStart().date().daysTo( event->dtEnd().date() ) + 1 );
04018       }
04019     } else {
04020       tmp = i18n( "forever" );
04021     }
04022   } else if ( incidence->type() == "Todo" ) {
04023     Todo *todo = static_cast<Todo *>( incidence );
04024     if ( todo->hasDueDate() ) {
04025       if ( todo->hasStartDate() ) {
04026         if ( !todo->doesFloat() ) {
04027           tmp = secs2Duration( todo->dtStart().secsTo( todo->dtDue() ) );
04028         } else {
04029           tmp = i18n( "1 day", "%n days",
04030                       todo->dtStart().date().daysTo( todo->dtDue().date() ) + 1 );
04031         }
04032       }
04033     }
04034   }
04035   return tmp;
04036 }
04037 
04038 QStringList IncidenceFormatter::reminderStringList( Incidence *incidence, bool shortfmt )
04039 {
04040   //TODO: implement shortfmt=false
04041   Q_UNUSED( shortfmt );
04042 
04043   QStringList reminderStringList;
04044 
04045   if ( incidence ) {
04046     Alarm::List alarms = incidence->alarms();
04047     Alarm::List::ConstIterator it;
04048     for ( it = alarms.begin(); it != alarms.end(); ++it ) {
04049       Alarm *alarm = *it;
04050       int offset = 0;
04051       QString remStr, atStr, offsetStr;
04052       if ( alarm->hasTime() ) {
04053         offset = 0;
04054         if ( alarm->time().isValid() ) {
04055           atStr = KGlobal::locale()->formatDateTime( alarm->time() );
04056         }
04057       } else if ( alarm->hasStartOffset() ) {
04058         offset = alarm->startOffset().asSeconds();
04059         if ( offset < 0 ) {
04060           offset = -offset;
04061           offsetStr = i18n( "N days/hours/minutes before the start datetime",
04062                             "%1 before the start" );
04063         } else if ( offset > 0 ) {
04064           offsetStr = i18n( "N days/hours/minutes after the start datetime",
04065                             "%1 after the start" );
04066         } else { //offset is 0
04067           if ( incidence->dtStart().isValid() ) {
04068             atStr = KGlobal::locale()->formatDateTime( incidence->dtStart() );
04069           }
04070         }
04071       } else if ( alarm->hasEndOffset() ) {
04072         offset = alarm->endOffset().asSeconds();
04073         if ( offset < 0 ) {
04074           offset = -offset;
04075           if ( incidence->type() == "Todo" ) {
04076             offsetStr = i18n( "N days/hours/minutes before the due datetime",
04077                               "%1 before the to-do is due" );
04078           } else {
04079             offsetStr = i18n( "N days/hours/minutes before the end datetime",
04080                               "%1 before the end" );
04081           }
04082         } else if ( offset > 0 ) {
04083           if ( incidence->type() == "Todo" ) {
04084             offsetStr = i18n( "N days/hours/minutes after the due datetime",
04085                               "%1 after the to-do is due" );
04086           } else {
04087             offsetStr = i18n( "N days/hours/minutes after the end datetime",
04088                               "%1 after the end" );
04089           }
04090         } else { //offset is 0
04091           if ( incidence->type() == "Todo" ) {
04092             Todo *t = static_cast<Todo *>( incidence );
04093             if ( t->dtDue().isValid() ) {
04094               atStr = KGlobal::locale()->formatDateTime( t->dtDue() );
04095             }
04096           } else {
04097             Event *e = static_cast<Event *>( incidence );
04098             if ( e->dtEnd().isValid() ) {
04099               atStr = KGlobal::locale()->formatDateTime( e->dtEnd() );
04100             }
04101           }
04102         }
04103       }
04104       if ( offset == 0 ) {
04105         if ( !atStr.isEmpty() ) {
04106           remStr = i18n( "reminder occurs at datetime", "at %1" ).arg( atStr );
04107         }
04108       } else {
04109         remStr = offsetStr.arg( secs2Duration( offset ) );
04110       }
04111 
04112       if ( alarm->repeatCount() > 0 ) {
04113         QString countStr = i18n( "repeats once", "repeats %n times", alarm->repeatCount() );
04114         QString intervalStr = i18n( "interval is N days/hours/minutes", "interval is %1" ).
04115                               arg( secs2Duration( alarm->snoozeTime().asSeconds() ) );
04116         QString repeatStr = i18n( "(repeat string, interval string)", "(%1, %2)" ).
04117                             arg( countStr, intervalStr );
04118         remStr = remStr + ' ' + repeatStr;
04119 
04120       }
04121       reminderStringList << remStr;
04122     }
04123   }
04124 
04125   return reminderStringList;
04126 }
KDE Home | KDE Accessibility Home | Description of Access Keys