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 if ( event->doesFloat() ) { 00092 if ( event->isMultiDay() ) { 00093 mText.append( i18n("<b>From:</b> %1 <b>To:</b> %2") 00094 .arg( event->dtStartDateStr() ) 00095 .arg( event->dtEndDateStr() ) ); 00096 } else { 00097 mText.append( i18n("<b>On:</b> %1").arg( event->dtStartDateStr() ) ); 00098 } 00099 } else { 00100 if ( event->isMultiDay() ) { 00101 mText.append( i18n("<b>From:</b> %1 <b>To:</b> %2") 00102 .arg( event->dtStartStr() ) 00103 .arg( event->dtEndStr() ) ); 00104 } else { 00105 mText.append( i18n("<b>On:</b> %1 <b>From:</b> %2 <b>To:</b> %3") 00106 .arg( event->dtStartDateStr() ) 00107 .arg( event->dtStartTimeStr() ) 00108 .arg( event->dtEndTimeStr() ) ); 00109 } 00110 } 00111 00112 if ( !event->description().isEmpty() ) addTag( "p", event->description() ); 00113 00114 formatCategories( event ); 00115 00116 if ( event->doesRecur() ) { 00117 QDateTime dt = event->recurrence()->getNextDateTime( 00118 QDateTime::currentDateTime() ); 00119 addTag( "p", "<em>" + 00120 i18n("This is a recurring event. The next occurrence will be on %1.").arg( 00121 KGlobal::locale()->formatDateTime( dt, true ) ) + "</em>" ); 00122 } 00123 00124 formatReadOnly( event ); 00125 formatAttendees( event ); 00126 formatAttachments( event ); 00127 00128 setText( mText ); 00129 } 00130 00131 void KOEventViewer::appendTodo( Todo *todo ) 00132 { 00133 addTag( "h1", todo->summary() ); 00134 00135 if ( !todo->location().isEmpty() ) { 00136 addTag( "b", i18n("Location:") ); 00137 mText.append( todo->location() + "<br>" ); 00138 } 00139 if ( todo->hasDueDate() ) { 00140 mText.append( i18n("<b>Due on:</b> %1").arg( todo->dtDueStr() ) ); 00141 } 00142 00143 if ( !todo->description().isEmpty() ) addTag( "p", todo->description() ); 00144 00145 formatCategories( todo ); 00146 00147 mText.append( i18n("<p><b>Priority:</b> %2</p>") 00148 .arg( QString::number( todo->priority() ) ) ); 00149 00150 mText.append( i18n("<p><i>%1 % completed</i></p>") 00151 .arg( todo->percentComplete() ) ); 00152 00153 if ( todo->doesRecur() ) { 00154 QDateTime dt = todo->recurrence()->getNextDateTime( 00155 QDateTime::currentDateTime() ); 00156 addTag( "p", "<em>" + 00157 i18n("This is a recurring todo. The next occurrence will be on %1.").arg( 00158 KGlobal::locale()->formatDateTime( dt, true ) ) + "</em>" ); 00159 } 00160 formatReadOnly( todo ); 00161 formatAttendees( todo ); 00162 formatAttachments( todo ); 00163 00164 setText( mText ); 00165 } 00166 00167 void KOEventViewer::appendJournal( Journal *journal ) 00168 { 00169 addTag( "h1", i18n("Journal for %1").arg( journal->dtStartDateStr( false ) ) ); 00170 addTag( "p", journal->description() ); 00171 setText( mText ); 00172 } 00173 00174 void KOEventViewer::formatCategories( Incidence *event ) 00175 { 00176 if ( !event->categoriesStr().isEmpty() ) { 00177 if ( event->categories().count() == 1 ) { 00178 addTag( "h2", i18n("Category") ); 00179 } else { 00180 addTag( "h2", i18n("Categories") ); 00181 } 00182 addTag( "p", event->categoriesStr() ); 00183 } 00184 } 00185 00186 void KOEventViewer::linkPerson( const QString& email, QString name, 00187 QString uid, const QString& iconPath ) 00188 { 00189 #ifndef KORG_NOKABC 00190 // Make the search, if there is an email address to search on, 00191 // and either name or uid is missing 00192 if ( !email.isEmpty() && ( name.isEmpty() || uid.isEmpty() ) ) { 00193 KABC::AddressBook *add_book = KABC::StdAddressBook::self(); 00194 KABC::Addressee::List addressList = add_book->findByEmail( email ); 00195 KABC::Addressee o = addressList.first(); 00196 if ( !o.isEmpty() && addressList.size() < 2 ) { 00197 if ( name.isEmpty() ) 00198 // No name set, so use the one from the addressbook 00199 name = o.formattedName(); 00200 uid = o.uid(); 00201 } else 00202 // Email not found in the addressbook. Don't make a link 00203 uid = ""; 00204 } 00205 #else 00206 // No addressbook - don't try to contact it then 00207 uid = ""; 00208 #endif 00209 kdDebug(5850) << "formatAttendees: uid = " << uid << endl; 00210 00211 // Show the attendee 00212 mText += "<li>"; 00213 if ( !uid.isEmpty() ) { 00214 // There is a UID, so make a link to the addressbook 00215 if ( name.isEmpty() ) 00216 // Use the email address for text 00217 addLink( "uid:" + uid, email ); 00218 else 00219 addLink( "uid:" + uid, name ); 00220 } else { 00221 // No UID, just show some text 00222 mText += ( name.isEmpty() ? email : name ); 00223 } 00224 mText += '\n'; 00225 00226 // Make the mailto link 00227 if ( !email.isEmpty() && !iconPath.isNull() ) { 00228 QString receiver; 00229 if ( name.isEmpty() ) 00230 // Only use the email address 00231 receiver = email; 00232 else 00233 // Full receiver 00234 receiver = name +" <" + email + ">"; 00235 addLink( "mailto:" + receiver, "<img src=\"" + iconPath + "\">" ); 00236 } 00237 mText += "</li>\n"; 00238 } 00239 00240 void KOEventViewer::formatAttendees( Incidence *event ) 00241 { 00242 Attendee::List attendees = event->attendees(); 00243 if ( attendees.count() ) { 00244 KIconLoader iconLoader; 00245 const QString iconPath = iconLoader.iconPath( "mail_generic", 00246 KIcon::Small ); 00247 00248 // Add organizer link 00249 addTag( "h3", i18n("Organizer") ); 00250 mText.append( "<ul>" ); 00251 QString name, email, organizer; 00252 organizer = event->organizer(); 00253 if ( organizer.length() > 1 ) { 00254 // If the organizer does not have a name, it looks like <foo@bar.org> 00255 // which can not be shown in a rich text widget. Filter those <> out 00256 if ( organizer[0] == '<' && organizer[organizer.length()-1] == '>' ) 00257 organizer = organizer.mid( 1, organizer.length() - 2 ); 00258 } 00259 if ( KPIM::getNameAndMail( organizer, name, email ) ) 00260 linkPerson( email, name, "", iconPath ); 00261 else 00262 // Doesn't seem to be a valid address. Just show whatever we have 00263 mText += "<li>" + organizer + "</li>\n"; 00264 mText += "</ul>"; 00265 00266 // Add attendees links 00267 addTag( "h3", i18n("Attendees") ); 00268 mText.append( "<ul>" ); 00269 Attendee::List::ConstIterator it; 00270 for( it = attendees.begin(); it != attendees.end(); ++it ) { 00271 Attendee *a = *it; 00272 linkPerson( a->email(), a->name(), a->uid(), iconPath ); 00273 } 00274 mText.append( "</ul>" ); 00275 } 00276 } 00277 00278 void KOEventViewer::formatReadOnly( Incidence *i ) 00279 { 00280 if ( i->isReadOnly() ) { 00281 addTag( "p", "<em>(" + i18n("read-only") + ")</em>" ); 00282 } 00283 } 00284 00285 void KOEventViewer::formatAttachments( Incidence *i ) 00286 { 00287 Attachment::List as = i->attachments(); 00288 if ( as.count() > 0 ) { 00289 mText += "<ul>"; 00290 Attachment::List::ConstIterator it; 00291 for( it = as.begin(); it != as.end(); ++it ) { 00292 if ( (*it)->isUri() ) { 00293 mText += "<li>"; 00294 addLink( (*it)->uri(), (*it)->uri() ); 00295 mText += "</li>"; 00296 } 00297 } 00298 mText += "</ul>"; 00299 } 00300 } 00301 00302 void KOEventViewer::setTodo( Todo *event ) 00303 { 00304 clearEvents(); 00305 appendTodo( event ); 00306 } 00307 00308 void KOEventViewer::setEvent( Event *event ) 00309 { 00310 clearEvents(); 00311 appendEvent( event ); 00312 } 00313 00314 void KOEventViewer::setJournal( Journal *journal ) 00315 { 00316 clearEvents(); 00317 appendJournal( journal ); 00318 } 00319 00320 void KOEventViewer::clearEvents( bool now ) 00321 { 00322 mText = ""; 00323 if ( now ) setText( mText ); 00324 } 00325 00326 void KOEventViewer::addText( const QString &text ) 00327 { 00328 mText.append( text ); 00329 setText( mText ); 00330 } 00331 00332 void KOEventViewer::addLink( const QString &ref, const QString &text, 00333 bool newline ) 00334 { 00335 mText += "<a href=\"" + ref + "\">" + text + "</a>"; 00336 if ( newline ) mText += "\n"; 00337 } 00338 00339 #include "koeventviewer.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:56 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003