kincidenceformatter.cpp
00001 #include "kincidenceformatter.h"
00002 #include <kstaticdeleter.h>
00003 #include <kglobal.h>
00004 #include <klocale.h>
00005 #include <kiconloader.h>
00006 #ifndef KORG_NOKABC
00007 #include <kabc/stdaddressbook.h>
00008 #define size count
00009 #endif
00010
00011 KIncidenceFormatter* KIncidenceFormatter::mInstance = 0;
00012 static KStaticDeleter<KIncidenceFormatter> insd;
00013
00014 QString KIncidenceFormatter::getFormattedText( Incidence * inc )
00015 {
00016
00017 mText = "";
00018 if ( inc->type() == "Event" )
00019 setEvent((Event *) inc );
00020 else if ( inc->type() == "Todo" )
00021 setTodo((Todo *) inc );
00022 else if ( inc->type() == "Journal" )
00023 mText = inc->description();
00024 return mText;
00025 }
00026
00027 KIncidenceFormatter* KIncidenceFormatter::instance()
00028 {
00029 if (!mInstance) {
00030 insd.setObject( mInstance, new KIncidenceFormatter());
00031 }
00032 return mInstance;
00033 }
00034 KIncidenceFormatter::~KIncidenceFormatter()
00035 {
00036
00037 }
00038 KIncidenceFormatter::KIncidenceFormatter()
00039 {
00040 mColorMode = 0;
00041 }
00042 void KIncidenceFormatter::setEvent(Event *event)
00043 {
00044 int mode = 0;
00045 mCurrentIncidence = event;
00046 bool shortDate = true;
00047 if ( mode == 0 ) {
00048 addTag("h3",event->summary());
00049 }
00050 else {
00051 if ( mColorMode == 1 ) {
00052 mText +="<font color=\"#00A000\">";
00053 }
00054 if ( mColorMode == 2 ) {
00055 mText +="<font color=\"#C00000\">";
00056 }
00057
00058 if ( mode == 1 ) {
00059 addTag("h2",i18n( "Local: " ) +event->summary());
00060 } else {
00061 addTag("h2",i18n( "Remote: " ) +event->summary());
00062 }
00063 addTag("h3",i18n( "Last modified: " ) + KGlobal::locale()->formatDateTime(event->lastModified(),shortDate, true ) );
00064 if ( mColorMode )
00065 mText += "</font>";
00066 }
00067 #if 0
00068 if (event->cancelled ()) {
00069 mText +="<font color=\"#B00000\">";
00070 addTag("i",i18n("This event has been cancelled!"));
00071 mText.append("<br>");
00072 mText += "</font>";
00073 }
00074 #endif
00075 if (!event->location().isEmpty()) {
00076 addTag("b",i18n("Location: "));
00077 mText.append(event->location()+"<br>");
00078 }
00079 if (event->doesFloat()) {
00080 if (event->isMultiDay()) {
00081 mText.append(i18n("<p><b>From:</b> %1 </p><p><b>To:</b> %2</p>")
00082 .arg(event->dtStartDateStr(shortDate))
00083 .arg(event->dtEndDateStr(shortDate)));
00084 } else {
00085 mText.append(i18n("<p><b>On:</b> %1</p>").arg(event->dtStartDateStr( shortDate )));
00086 }
00087 } else {
00088 if (event->isMultiDay()) {
00089 mText.append(i18n("<p><b>From:</b> %1</p> ")
00090 .arg(event->dtStartStr()));
00091 mText.append(i18n("<p><b>To:</b> %1</p>")
00092 .arg(event->dtEndStr()));
00093 } else {
00094 mText.append(i18n("<p><b>On:</b> %1</p> ")
00095 .arg(event->dtStartDateStr( shortDate )));
00096 mText.append(i18n("<p><b>From:</b> %1 <b>To:</b> %2</p>")
00097 .arg(event->dtStartTimeStr())
00098 .arg(event->dtEndTimeStr()));
00099 }
00100 }
00101
00102 if (event->recurrence()->doesRecur()) {
00103
00104 QString recurText = i18n("No");
00105 short recurs = event->recurrence()->doesRecur();
00106 if ( recurs == Recurrence::rMinutely )
00107 recurText = i18n("minutely");
00108 else if ( recurs == Recurrence::rHourly )
00109 recurText = i18n("hourly");
00110 else if ( recurs == Recurrence::rDaily )
00111 recurText = i18n("daily");
00112 else if ( recurs == Recurrence::rWeekly )
00113 recurText = i18n("weekly");
00114 else if ( recurs == Recurrence::rMonthlyPos )
00115 recurText = i18n("monthly");
00116 else if ( recurs == Recurrence::rMonthlyDay )
00117 recurText = i18n("day-monthly");
00118 else if ( recurs == Recurrence::rYearlyMonth )
00119 recurText = i18n("month-yearly");
00120 else if ( recurs == Recurrence::rYearlyDay )
00121 recurText = i18n("day-yearly");
00122 else if ( recurs == Recurrence::rYearlyPos )
00123 recurText = i18n("position-yearly");
00124 addTag("p","<em>" + i18n("This is a %1 recurring event.").arg(recurText ) + "</em>");
00125 bool last;
00126 QDate start = QDate::currentDate();
00127 QDate next;
00128 next = event->recurrence()->getPreviousDate( start , &last );
00129 if ( !last ) {
00130 next = event->recurrence()->getNextDate( start.addDays( - 1 ) );
00131 addTag("p",i18n("Next recurrence is on: ")+ KGlobal::locale()->formatDate( next, shortDate ) );
00132
00133 } else {
00134 addTag("p",i18n("<b>Last recurrence was on:</b>") );
00135 addTag("p", KGlobal::locale()->formatDate( next, shortDate ));
00136 }
00137 }
00138
00139
00140 if (event->isAlarmEnabled()) {
00141 Alarm *alarm =event->alarms().first() ;
00142 QDateTime t = alarm->time();
00143 int min = t.secsTo( event->dtStart() )/60;
00144 QString s =i18n("(%1 min before)").arg( min );
00145 addTag("p",i18n("<b>Alarm on: </b>") + s + ": "+KGlobal::locale()->formatDateTime( t, shortDate ));
00146
00147
00148 }
00149
00150 addTag("p",i18n("<b>Access: </b>") +event->secrecyStr() );
00151
00152 formatCategories(event);
00153 if (!event->description().isEmpty()) {
00154 addTag("p",i18n("<b>Details: </b>"));
00155 addTag("p",event->description());
00156 }
00157
00158
00159 formatReadOnly(event);
00160 formatAttendees(event);
00161
00162
00163 }
00164
00165 void KIncidenceFormatter::setTodo(Todo *event )
00166 {
00167 int mode = 0;
00168 mCurrentIncidence = event;
00169 bool shortDate = true;
00170 if (mode == 0 )
00171 addTag("h3",event->summary());
00172 else {
00173 if ( mColorMode == 1 ) {
00174 mText +="<font color=\"#00A000\">";
00175 }
00176 if ( mColorMode == 2 ) {
00177 mText +="<font color=\"#B00000\">";
00178 }
00179 if ( mode == 1 ) {
00180 addTag("h2",i18n( "Local: " ) +event->summary());
00181 } else {
00182 addTag("h2",i18n( "Remote: " ) +event->summary());
00183 }
00184 addTag("h3",i18n( "Last modified: " ) + KGlobal::locale()->formatDateTime(event->lastModified(),shortDate, true ) );
00185 if ( mColorMode )
00186 mText += "</font>";
00187 }
00188 #if 0
00189 if (event->cancelled ()) {
00190 mText +="<font color=\"#B00000\">";
00191 addTag("i",i18n("This todo has been cancelled!"));
00192 mText.append("<br>");
00193 mText += "</font>";
00194 }
00195 #endif
00196 if (!event->location().isEmpty()) {
00197 addTag("b",i18n("Location: "));
00198 mText.append(event->location()+"<br>");
00199 }
00200 if (event->hasDueDate()) {
00201 mText.append(i18n("<p><b>Due on:</b> %1</p>").arg(event->dtDueStr()));
00202 }
00203 mText.append(i18n("<p><b>Priority:</b> %2</p>")
00204 .arg(QString::number(event->priority())));
00205
00206 mText.append(i18n("<p><i>%1 % completed</i></p>")
00207 .arg(event->percentComplete()));
00208 addTag("p",i18n("<b>Access: </b>") +event->secrecyStr() );
00209 formatCategories(event);
00210 if (!event->description().isEmpty()) {
00211 addTag("p",i18n("<b>Details: </b>"));
00212 addTag("p",event->description());
00213 }
00214
00215
00216
00217 formatReadOnly(event);
00218 formatAttendees(event);
00219
00220 }
00221
00222 void KIncidenceFormatter::setJournal(Journal* )
00223 {
00224
00225 }
00226
00227 void KIncidenceFormatter::formatCategories(Incidence *event)
00228 {
00229 if (!event->categoriesStr().isEmpty()) {
00230 addTag("p",i18n("<b>Categories: </b>")+event->categoriesStr() );
00231
00232 }
00233 }
00234 void KIncidenceFormatter::addTag(const QString & tag,const QString & text)
00235 {
00236 int number=text.contains("\n");
00237 QString str = "<" + tag + ">";
00238 QString tmpText=text;
00239 QString tmpStr=str;
00240 if(number !=-1)
00241 {
00242 if (number > 0) {
00243 int pos=0;
00244 QString tmp;
00245 for(int i=0;i<=number;i++) {
00246 pos=tmpText.find("\n");
00247 tmp=tmpText.left(pos);
00248 tmpText=tmpText.right(tmpText.length()-pos-1);
00249 tmpStr+=tmp+"<br>";
00250 }
00251 }
00252 else tmpStr += tmpText;
00253 tmpStr+="</" + tag + ">";
00254 mText.append(tmpStr);
00255 }
00256 else
00257 {
00258 str += text + "</" + tag + ">";
00259 mText.append(str);
00260 }
00261 }
00262
00263 void KIncidenceFormatter::formatAttendees(Incidence *event)
00264 {
00265 Attendee::List attendees = event->attendees();
00266 if ( attendees.count() ) {
00267 KIconLoader* iconLoader = new KIconLoader();
00268 QString iconPath = iconLoader->iconPath( "mail_generic", KIcon::Small );
00269 addTag( "h3", i18n("Organizer") );
00270 mText.append( "<ul><li>" );
00271 #ifndef KORG_NOKABC
00272 KABC::AddressBook *add_book = KABC::StdAddressBook::self();
00273 KABC::Addressee::List addressList;
00274 addressList = add_book->findByEmail( event->organizer().email() );
00275 KABC::Addressee o = addressList.first();
00276 if ( !o.isEmpty() && addressList.size() < 2 ) {
00277 addLink( "uid" + o.uid(), o.formattedName() );
00278 } else {
00279 mText.append( event->organizer().fullName() );
00280 }
00281 #else
00282 mText.append( event->organizer().fullName() );
00283 #endif
00284 if ( !iconPath.isNull() ) {
00285 addLink( "mailto:" + event->organizer().email(),
00286 "<img src=\"" + iconPath + "\">" );
00287 }
00288 mText.append( "</li></ul>" );
00289
00290 addTag( "h3", i18n("Attendees") );
00291 mText.append( "<ul>" );
00292 Attendee::List::ConstIterator it;
00293 for( it = attendees.begin(); it != attendees.end(); ++it ) {
00294 Attendee *a = *it;
00295 #ifndef KORG_NOKABC
00296 if ( a->name().isEmpty() ) {
00297 addressList = add_book->findByEmail( a->email() );
00298 KABC::Addressee o = addressList.first();
00299 if ( !o.isEmpty() && addressList.size() < 2 ) {
00300 addLink( "uid" + o.uid(), o.formattedName() );
00301 } else {
00302 mText += "<li>";
00303 mText.append( a->email() );
00304 mText += "\n";
00305 }
00306 } else {
00307 mText += "<li><a href=\"uid:" + a->uid() + "\">";
00308 if ( !a->name().isEmpty() ) mText += a->name();
00309 else mText += a->email();
00310 mText += "</a>\n";
00311 }
00312 #else
00313 mText += "<li><a href=\"uid:" + a->uid() + "\">";
00314 if ( !a->name().isEmpty() ) mText += a->name();
00315 else mText += a->email();
00316 mText += "</a>\n";
00317 #endif
00318 kdDebug(5850) << "formatAttendees: uid = " << a->uid() << endl;
00319
00320 if ( !a->email().isEmpty() ) {
00321 if ( !iconPath.isNull() ) {
00322 mText += "<a href=\"mailto:" + a->name() +" "+ "<" + a->email() + ">" + "\">";
00323 mText += "<img src=\"" + iconPath + "\">";
00324 mText += "</a>\n";
00325 }
00326 }
00327 }
00328 mText.append( "</li></ul>" );
00329 }
00330 }
00331
00332 void KIncidenceFormatter::formatReadOnly(Incidence *event)
00333 {
00334 if (event->isReadOnly()) {
00335 addTag("p","<em>(" + i18n("read-only") + ")</em>");
00336 }
00337 }
00338 void KIncidenceFormatter::addLink( const QString &ref, const QString &text,
00339 bool newline )
00340 {
00341 mText += "<a href=\"" + ref + "\">" + text + "</a>";
00342 if ( newline ) mText += "\n";
00343 }
This file is part of the documentation for libkdepim Library Version 3.3.2.