korganizer Library API Documentation

koeventviewer.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "koeventviewer.h"
00026 
00027 #include "urihandler.h"
00028 
00029 #include <libkcal/event.h>
00030 #include <libkcal/todo.h>
00031 #include <libkcal/journal.h>
00032 #include <libkdepim/email.h>
00033 
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 #include <kapplication.h>
00037 #include <kdebug.h>
00038 #ifndef KORG_NOKABC
00039 #include <kabc/stdaddressbook.h>
00040 #endif
00041 
00042 KOEventViewer::KOEventViewer( QWidget *parent, const char *name )
00043   : QTextBrowser( parent, name )
00044 {
00045 }
00046 
00047 KOEventViewer::~KOEventViewer()
00048 {
00049 }
00050 
00051 void KOEventViewer::setSource( const QString &n )
00052 {
00053   UriHandler::process( n );
00054 }
00055 
00056 void KOEventViewer::addTag( const QString & tag, const QString & text )
00057 {
00058   int numLineBreaks = text.contains( "\n" );
00059   QString str = "<" + tag + ">";
00060   QString tmpText = text;
00061   QString tmpStr = str;
00062   if( numLineBreaks >= 0 ) {
00063     if ( numLineBreaks > 0) {
00064       int pos = 0;
00065       QString tmp;
00066       for( int i = 0; i <= numLineBreaks; i++ ) {
00067         pos = tmpText.find( "\n" );
00068         tmp = tmpText.left( pos );
00069         tmpText = tmpText.right( tmpText.length() - pos - 1 );
00070         tmpStr += tmp + "<br>";
00071       }
00072     } else {
00073       tmpStr += tmpText;
00074     }
00075     tmpStr += "</" + tag + ">";
00076     mText.append( tmpStr );
00077   } else {
00078     str += text + "</" + tag + ">";
00079     mText.append( str );
00080   }
00081 }
00082 
00083 void KOEventViewer::appendEvent( Event *event )
00084 {
00085   addTag( "h1", event->summary() );
00086 
00087   if ( !event->location().isEmpty() ) {
00088     addTag( "b", i18n("Location: ") );
00089     mText.append( event->location() + "<br>" );
00090   }
00091 
00092   // for recurring events, display the datetimes for the next occurrence from now on. 
00093   // fixes the case when the view is dialog is called for an alarm.
00094 
00095   Event tmp;
00096 
00097   if ( event->doesRecur() )
00098   {
00099      tmp.setDtStart( event->recurrence()->getNextDateTime( event->doesFloat() ? 
00100        QDate::currentDate().addDays(-1) : QDateTime::currentDateTime()) );
00101      const int diff = event->dtStart().secsTo( tmp.dtStart() );
00102      tmp.setDtEnd( event->dtEnd().addSecs(diff) );
00103   }
00104   else
00105   {
00106      tmp.setDtStart( event->dtStart() );
00107      tmp.setDtEnd( event->dtEnd() );
00108   }  
00109 
00110   if ( event->doesFloat() ) {
00111     if ( event->isMultiDay() ) {
00112       mText.append( i18n("<b>From:</b> %1 <b>To:</b> %2")
00113                     .arg( tmp.dtStartDateStr() )
00114                     .arg( tmp.dtEndDateStr() ) );
00115     } else {
00116       mText.append( i18n("<b>On:</b> %1").arg( tmp.dtStartDateStr() ) );
00117     }
00118   } else {
00119     if ( event->isMultiDay() ) {
00120       mText.append( i18n("<b>From:</b> %1 <b>To:</b> %2")
00121                     .arg( tmp.dtStartStr() )
00122                     .arg( tmp.dtEndStr() ) );
00123     } else {
00124       mText.append( i18n("<b>On:</b> %1 <b>From:</b> %2 <b>To:</b> %3")
00125                     .arg( tmp.dtStartDateStr() )
00126                     .arg( tmp.dtStartTimeStr() )
00127                     .arg( tmp.dtEndTimeStr() ) );
00128     }
00129   }
00130 
00131   if ( !event->description().isEmpty() ) addTag( "p", event->description() );
00132 
00133   formatCategories( event );
00134 
00135   if ( event->doesRecur() ) {
00136       addTag( "p", "<em>" +
00137         i18n("This is a recurring event.") + "</em>" );
00138   }
00139 
00140   formatReadOnly( event );
00141   formatAttendees( event );
00142   formatAttachments( event );
00143 
00144   setText( mText );
00145 }
00146 
00147 void KOEventViewer::appendTodo( Todo *todo )
00148 {
00149   addTag( "h1", todo->summary() );
00150 
00151   if ( !todo->location().isEmpty() ) {
00152     addTag( "b", i18n("Location:") );
00153     mText.append( todo->location() + "<br>" );
00154   }
00155   if ( todo->hasDueDate() ) {
00156     mText.append( i18n("<b>Due on:</b> %1").arg( todo->dtDueStr() ) );
00157   }
00158 
00159   if ( !todo->description().isEmpty() ) addTag( "p", todo->description() );
00160 
00161   formatCategories( todo );
00162 
00163   mText.append( i18n("<p><b>Priority:</b> %2</p>")
00164                 .arg( QString::number( todo->priority() ) ) );
00165 
00166   mText.append( i18n("<p><i>%1 % completed</i></p>")
00167                      .arg( todo->percentComplete() ) );
00168 
00169   if ( todo->doesRecur() ) {
00170     addTag( "p", "<em>" + i18n("This is a recurring todo.") +"</em>");
00171   }
00172   formatReadOnly( todo );
00173   formatAttendees( todo );
00174   formatAttachments( todo );
00175 
00176   setText( mText );
00177 }
00178 
00179 void KOEventViewer::appendJournal( Journal *journal )
00180 {
00181   addTag( "h1", i18n("Journal for %1").arg( journal->dtStartDateStr( false ) ) );
00182   addTag( "p", journal->description() );
00183   setText( mText );
00184 }
00185 
00186 void KOEventViewer::formatCategories( Incidence *event )
00187 {
00188   if ( !event->categoriesStr().isEmpty() ) {
00189     if ( event->categories().count() == 1 ) {
00190       addTag( "h2", i18n("Category") );
00191     } else {
00192       addTag( "h2", i18n("Categories") );
00193     }
00194     addTag( "p", event->categoriesStr() );
00195   }
00196 }
00197 
00198 void KOEventViewer::linkPerson( const QString& email, QString name,
00199                                 QString uid, const QString& iconPath )
00200 {
00201 #ifndef KORG_NOKABC
00202   // Make the search, if there is an email address to search on,
00203   // and either name or uid is missing
00204   if ( !email.isEmpty() && ( name.isEmpty() || uid.isEmpty() ) ) {
00205     KABC::AddressBook *add_book = KABC::StdAddressBook::self();
00206     KABC::Addressee::List addressList = add_book->findByEmail( email );
00207     KABC::Addressee o = addressList.first();
00208     if ( !o.isEmpty() && addressList.size() < 2 ) {
00209       if ( name.isEmpty() )
00210         // No name set, so use the one from the addressbook
00211         name = o.formattedName();
00212       uid = o.uid();
00213     } else
00214       // Email not found in the addressbook. Don't make a link
00215       uid = "";
00216   }
00217 #else
00218   // No addressbook - don't try to contact it then
00219   uid = "";
00220 #endif
00221   kdDebug(5850) << "formatAttendees: uid = " << uid << endl;
00222 
00223   // Show the attendee
00224   mText += "<li>";
00225   if ( !uid.isEmpty() ) {
00226     // There is a UID, so make a link to the addressbook
00227     if ( name.isEmpty() )
00228       // Use the email address for text
00229       addLink( "uid:" + uid, email );
00230     else
00231       addLink( "uid:" + uid, name );
00232   } else {
00233     // No UID, just show some text
00234     mText += ( name.isEmpty() ? email : name );
00235   }
00236   mText += '\n';
00237 
00238   // Make the mailto link
00239   if ( !email.isEmpty() && !iconPath.isNull() ) {
00240     KCal::Person person( name, email );
00241     KURL mailto;
00242     mailto.setProtocol( "mailto" );
00243     mailto.setPath( person.fullName() );
00244     addLink( mailto.url(), "<img src=\"" + iconPath + "\">" );
00245   }
00246   mText += "</li>\n";
00247 }
00248 
00249 void KOEventViewer::formatAttendees( Incidence *event )
00250 {
00251   Attendee::List attendees = event->attendees();
00252   if ( attendees.count() ) {
00253     KIconLoader iconLoader;
00254     const QString iconPath = iconLoader.iconPath( "mail_generic",
00255                                                   KIcon::Small );
00256 
00257     // Add organizer link
00258     addTag( "h3", i18n("Organizer") );
00259     mText.append( "<ul>" );
00260     linkPerson( event->organizer().email(), event->organizer().name(), "", iconPath );
00261     mText += "</ul>";
00262 
00263     // Add attendees links
00264     addTag( "h3", i18n("Attendees") );
00265     mText.append( "<ul>" );
00266     Attendee::List::ConstIterator it;
00267     for( it = attendees.begin(); it != attendees.end(); ++it ) {
00268       Attendee *a = *it;
00269       linkPerson( a->email(), a->name(), a->uid(), iconPath );
00270     }
00271     mText.append( "</ul>" );
00272   }
00273 }
00274 
00275 void KOEventViewer::formatReadOnly( Incidence *i )
00276 {
00277   if ( i->isReadOnly() ) {
00278     addTag( "p", "<em>(" + i18n("read-only") + ")</em>" );
00279   }
00280 }
00281 
00282 void KOEventViewer::formatAttachments( Incidence *i )
00283 {
00284   Attachment::List as = i->attachments();
00285   if ( as.count() > 0 ) {
00286     mText += "<ul>";
00287     Attachment::List::ConstIterator it;
00288     for( it = as.begin(); it != as.end(); ++it ) {
00289       if ( (*it)->isUri() ) {
00290         mText += "<li>";
00291         addLink( (*it)->uri(), (*it)->uri() );
00292         mText += "</li>";
00293       }
00294     }
00295     mText += "</ul>";
00296   }
00297 }
00298 
00299 void KOEventViewer::setTodo( Todo *event )
00300 {
00301   clearEvents();
00302   appendTodo( event );
00303 }
00304 
00305 void KOEventViewer::setEvent( Event *event )
00306 {
00307   clearEvents();
00308   appendEvent( event );
00309 }
00310 
00311 void KOEventViewer::setJournal( Journal *journal )
00312 {
00313   clearEvents();
00314   appendJournal( journal );
00315 }
00316 
00317 void KOEventViewer::clearEvents( bool now )
00318 {
00319   mText = "";
00320   if ( now ) setText( mText );
00321 }
00322 
00323 void KOEventViewer::addText( const QString &text )
00324 {
00325   mText.append( text );
00326   setText( mText );
00327 }
00328 
00329 void KOEventViewer::addLink( const QString &ref, const QString &text,
00330                              bool newline )
00331 {
00332   mText += "<a href=\"" + ref + "\">" + text + "</a>";
00333   if ( newline ) mText += "\n";
00334 }
00335 
00336 #include "koeventviewer.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:56:25 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003