summaryrefslogtreecommitdiffabout
path: root/libkcal/kincidenceformatter.cpp
blob: bce68b0c60f56407b19c16e630fef10a5a87ae75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "kincidenceformatter.h"
#include <kstaticdeleter.h>
#include <kglobal.h>
#include <klocale.h>
#ifdef DEKTOP_VERSION
#include <kabc/stdaddressbook.h>
//Added by qt3to4:
#include <Q3PtrList>
#define size count
#endif

KIncidenceFormatter* KIncidenceFormatter::mInstance = 0;
static KStaticDeleter<KIncidenceFormatter> insd;

QString KIncidenceFormatter::getFormattedText( Incidence * inc, bool details, bool created , bool modified  )
{
// #ifndef QT_NO_INPUTDIALOG
//   return QInputDialog::getItem( caption, label, items, current, editable );
// #else
//   return QString::null;
// #endif
    mDetails = details;
    mCreated = created ;
    mModified = modified;
    mText = "";
    if ( inc->typeID() == eventID )
        setEvent((Event *) inc );
    else if ( inc->typeID() == todoID )
        setTodo((Todo *) inc );
    return mText;
}

KIncidenceFormatter* KIncidenceFormatter::instance()
{
    if (!mInstance) {
      mInstance = insd.setObject(new KIncidenceFormatter());
  }
  return mInstance;
}
KIncidenceFormatter::~KIncidenceFormatter()
{
 if (mInstance == this)
      mInstance = insd.setObject(0); 
 //qDebug("KIncidenceFormatter::~KIncidenceFormatter ");
}
KIncidenceFormatter::KIncidenceFormatter()
{
    mColorMode = 0;
}
void KIncidenceFormatter::setEvent(Event *event)
{
    int mode = 0;
    mCurrentIncidence = event;
    bool shortDate = true;
    if ( mode == 0 ) {
        addTag("h3",deTag(event->summary()));
        formatReadOnly(event);
    }
    else {
        if ( mColorMode == 1 ) {
            mText +="<font color=\"#00A000\">";
        }
        if ( mColorMode == 2 ) {
            mText +="<font color=\"#C00000\">";
        }
        // mText +="<font color=\"#F00000\">" + i18n("O-due!") + "</font>";
        if ( mode == 1 ) {
            addTag("h2",i18n( "Local: " ) +deTag(event->summary()));
        } else {
            addTag("h2",i18n( "Remote: " ) +deTag(event->summary()));
        } 
        formatReadOnly(event);
        addTag("h3",i18n( "Last modified: " ) + KGlobal::locale()->formatDateTime(event->lastModified(),shortDate, true ) );
        if ( mColorMode )
            mText += "</font>";
    } 
    if (event->cancelled ()) {
        mText +="<font color=\"#B00000\">";
        addTag("i",i18n("This event has been cancelled!"));
        mText.append("<br>");
        mText += "</font>";
    }
    if (event->doesFloat()) {
        if (event->isMultiDay()) {
            mText.append(i18n("<p><b>From:</b> %1 </p><p><b>To:</b> %2</p>")
                         .arg(event->dtStartDateStr(shortDate))
                         .arg(event->dtEndDateStr(shortDate)));  
            mText.append(i18n("<p><b>Duration:</b> %1 days</p>")
                         .arg(event->dtStart().daysTo(event->dtEnd())+1));
        } else {
            mText.append(i18n("<p><b>On:</b> %1</p>").arg(event->dtStartDateStr( shortDate )));
        }
    } else {
        if (event->isMultiDay()) {
            mText.append(i18n("<p><b>From:</b> %1</p> ")
                         .arg(event->dtStartStr( shortDate)));
            mText.append(i18n("<p><b>To:</b> %1</p>")
                         .arg(event->dtEndStr(shortDate)));
        } else {
            mText.append(i18n("<p><b>From:</b> %1 <b>To:</b> %2</p>")
                         .arg(event->dtStartTimeStr())
                         .arg(event->dtEndTimeStr()));
            mText.append(i18n("<p><b>On:</b> %1</p> ")
                         .arg(event->dtStartDateStr( shortDate )));
        }
    }
    if (!event->location().isEmpty()) {
        addTag("b",i18n("Location: "));
        mText.append(deTag(event->location())+"<br>");
    }

    if (event->doesRecur()) {

        QString recurText = event->recurrence()->recurrenceText();
        addTag("p","<em>" + i18n("This is a %1 recurring event.").arg(recurText ) + "</em>");

 bool ok;
        QDate start = QDate::currentDate();
        QDateTime next;
        next = event->getNextOccurence( QDateTime::currentDateTime() , &ok );
        if ( ok ) {
            addTag("p",i18n("<b>Next recurrence is on:</b>")  );
            addTag("p", KGlobal::locale()->formatDate( next.date(), shortDate ));

        } else {
            bool last;
            QDate nextd;
            nextd = event->recurrence()->getPreviousDate( QDate::currentDate() , &last );
            if ( last ) {
                addTag("p",i18n("<b>Last recurrence was on:</b>")  );
                addTag("p", KGlobal::locale()->formatDate( nextd, shortDate ));
            }
        }
    }


    if (event->isAlarmEnabled()) {
        Alarm *alarm =event->alarms().first() ;
        QDateTime t = alarm->time();
	if (event->doesRecur()) {
	  bool ok = false;
	  int offset = 0;
	  QDateTime next = event->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
	  if ( ok ) {
	    t = next;
	  }
	}
        QString s =i18n("( %1 before )").arg( alarm->offsetText() );
        addTag("p",i18n("<b>Alarm on:  </b>")  + s  + ": "+KGlobal::locale()->formatDateTime( t, shortDate ));
        //addTag("p", KGlobal::locale()->formatDateTime( t, shortDate ));
        //addTag("p",s);
    }



    addTag("p",i18n("<b>Access: </b>") +event->secrecyStr() );
    // mText.append(event->secrecyStr()+"<br>");
    formatCategories(event);

    formatAttendees(event);

    if ( mCreated ) {
#ifdef DESKTOP_VERSION
        addTag("p",i18n("<b>Created: ")  +" </b>"+KGlobal::locale()->formatDateTime( event->created(), shortDate ));
#else
        addTag("p",i18n("<b>Created: ")  +" </b>");
        addTag("p", KGlobal::locale()->formatDateTime( event->created(), shortDate ));
#endif
        
    }
    if ( mModified ) {
#ifdef DESKTOP_VERSION
        addTag("p",i18n("<b>Last modified: ")  +" </b>"+KGlobal::locale()->formatDateTime( event->lastModified(), shortDate ));
#else
        addTag("p",i18n("<b>Last modified: ")  +" </b>");
        addTag("p", KGlobal::locale()->formatDateTime( event->lastModified(), shortDate ));
#endif
        
    }
    if ( mDetails  ) {
        if (!event->description().isEmpty()) {
            addTag("p",i18n("<b>Details: </b>"));
            addTag("p",deTag(event->description()));
        }
    }
 
}

void KIncidenceFormatter::setTodo(Todo *event )
{
    int mode = 0;
    mCurrentIncidence = event;
    bool shortDate = true;
    if (mode == 0 ) {
        addTag("h3",deTag(event->summary()));
        formatReadOnly(event);
    }
    else { 
        if ( mColorMode == 1 ) {
            mText +="<font color=\"#00A000\">";
        }
        if ( mColorMode == 2 ) {
            mText +="<font color=\"#B00000\">";
        }
        if ( mode == 1 ) {
            addTag("h2",i18n( "Local: " ) +deTag(event->summary()));
        } else {
            addTag("h2",i18n( "Remote: " ) +deTag(event->summary()));
        }
        formatReadOnly(event);
        addTag("h3",i18n( "Last modified: " ) + KGlobal::locale()->formatDateTime(event->lastModified(),shortDate, true ) );
        if ( mColorMode )
            mText += "</font>";
    } 
    if ( event->percentComplete() == 100 && event->hasCompletedDate() ) {
        mText +="<font color=\"#B00000\">";
        addTag("i",  i18n("<p><i>Completed on %1</i></p>").arg( event->completedStr(shortDate) ) );
        mText += "</font>";
    } else {
        mText.append(i18n("<p><i>%1 % completed</i></p>")
                     .arg(event->percentComplete()));
    }
    if (event->cancelled ()) {
        mText +="<font color=\"#B00000\">";
        addTag("i",i18n("This todo has been cancelled!"));
        mText.append("<br>");
        mText += "</font>";
    }
   
   
    if (event->doesRecur()) {

        QString recurText = event->recurrence()->recurrenceText();
        addTag("p","<em>" + i18n("This is a %1 recurring todo.").arg(recurText ) + "</em>");
    } 
        
    if (event->hasStartDate()) {
        mText.append(i18n("<p><b>Start on:</b> %1</p>").arg(event->dtStartStr(shortDate)));
    }
        

    if (event->hasDueDate()) {
        mText.append(i18n("<p><b>Due on:</b> %1</p>").arg(event->dtDueStr(shortDate)));
    }

    if (!event->location().isEmpty()) {
        addTag("b",i18n("Location: "));
        mText.append(deTag(event->location())+"<br>");
    }

    mText.append(i18n("<p><b>Priority:</b> %2</p>")
                 .arg(QString::number(event->priority())));
  
   if (event->isAlarmEnabled()) {
        Alarm *alarm =event->alarms().first() ;
        QDateTime t = alarm->time();
	if (event->doesRecur()) {
	  bool ok = false;
	  int offset = 0;
	  QDateTime next = event->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
	  if ( ok ) {
	    t = next;
	  }
	}
        QString s =i18n("( %1 before )").arg( alarm->offsetText() );
        addTag("p",i18n("<b>Alarm on: ") + s  +" </b>");
        addTag("p", KGlobal::locale()->formatDateTime( t, shortDate ));
        //addTag("p",s);
    }

    addTag("p",i18n("<b>Access: </b>") +event->secrecyStr() );
    formatCategories(event);
   
    formatAttendees(event);
    if ( mCreated ) {
#ifdef DESKTOP_VERSION
        addTag("p",i18n("<b>Created: ")  +" </b>"+KGlobal::locale()->formatDateTime( event->created(), shortDate ));
#else
        addTag("p",i18n("<b>Created: ")  +" </b>");
        addTag("p", KGlobal::locale()->formatDateTime( event->created(), shortDate ));
#endif
        
    }
    if ( mModified ) {
#ifdef DESKTOP_VERSION
        addTag("p",i18n("<b>Last modified: ")  +" </b>"+KGlobal::locale()->formatDateTime( event->lastModified(), shortDate ));
#else
        addTag("p",i18n("<b>Last modified: ")  +" </b>");
        addTag("p", KGlobal::locale()->formatDateTime( event->lastModified(), shortDate ));
#endif
        
    }
    if ( mDetails  ) {
        if (!event->description().isEmpty()) {
            addTag("p",i18n("<b>Details: </b>"));
            addTag("p",deTag(event->description()));
        }
    }
}

void KIncidenceFormatter::setJournal(Journal*  )
{

}

void KIncidenceFormatter::formatCategories(Incidence *event)
{
 if (!event->categoriesStr().isEmpty()) {
     addTag("p",i18n("<b>Categories: </b>")+event->categoriesStrWithSpace() );
     //mText.append(event->categoriesStr());
 }
}
void KIncidenceFormatter::addTag(const QString & tag,const QString & text)
{
  int number=text.count("\n");
    QString str = "<" + tag + ">";
    QString tmpText=text;
    QString tmpStr=str;
    if(number !=-1) 
        {
            if (number > 0) {
                int pos=0;
                QString tmp;
                for(int i=0;i<=number;i++) {
                    pos=tmpText.find("\n");
                    tmp=tmpText.left(pos);
                    tmpText=tmpText.right(tmpText.length()-pos-1);
                    tmpStr+=tmp+"<br>";
                }
            }
            else tmpStr += tmpText;
            tmpStr+="</" + tag + ">";
            mText.append(tmpStr);
        }
    else
        {
            str += text + "</" + tag + ">";
            mText.append(str);
        }
}

void KIncidenceFormatter::formatAttendees(Incidence *event)
{
 Q3PtrList<Attendee> attendees = event->attendees();
    if (attendees.count()) {
        QString iconPath = KGlobal::iconLoader()->iconPath("mailappt",KIcon::Small);
        QString NOiconPath = KGlobal::iconLoader()->iconPath("nomailappt",KIcon::Small);
        addTag("h3",i18n("Organizer"));
        mText.append("<ul><li>");
#if 0
        //ndef KORG_NOKABC
       
        KABC::AddressBook *add_book = KABC::StdAddressBook::self();
        KABC::Addressee::List addressList;
        addressList = add_book->findByEmail(event->organizer());
        KABC::Addressee o = addressList.first();
        if (!o.isEmpty() && addressList.size()<2) {
            mText += "<a href=\"uid:" + o.uid() + "\">";
            mText += o.formattedName();
            mText += "</a>\n";
        } else {
            mText.append(event->organizer());
        }
#else
        mText.append(event->organizer());
#endif
        if (!iconPath.isEmpty()) {
            mText += " <a href=\"mailto:" + event->organizer() + "\">";
            mText += "<IMG src=\"" + iconPath + "\">";
            mText += "</a>\n";
        }
        mText.append("</li></ul>");

        addTag("h3",i18n("Attendees"));
        Attendee *a;
        mText.append("<ul>");
        for(a=attendees.first();a;a=attendees.next()) {
#if 0
//ndef KORG_NOKABC
            if (a->name().isEmpty()) {
                addressList = add_book->findByEmail(a->email());
                KABC::Addressee o = addressList.first();
                if (!o.isEmpty() && addressList.size()<2) {
                    mText += "<a href=\"uid:" + o.uid() + "\">";
                    mText += o.formattedName();
                    mText += "</a>\n";
                } else {
                    mText += "<li>";
                    mText.append(a->email());
                    mText += "\n";
                }
            } else {
                mText += "<li><a href=\"uid:" + a->uid() + "\">";
                if (!a->name().isEmpty()) mText += a->name();
                else mText += a->email();
                mText += "</a>\n";
            }
#else
            //qDebug("nokabc ");
            mText += "<li><a href=\"uid:" + a->uid() + "\">";
            if (!a->name().isEmpty()) mText += a->name();
            else mText += a->email();
            mText += "</a>\n";
#endif

            if (!a->email().isEmpty()) {
                if (!iconPath.isEmpty()) {
                    mText += "<a href=\"mailto:" + a->name() +" "+ "<" + a->email() + ">" + "\">"; 
                    if ( a->RSVP() )
                        mText += "<IMG src=\"" + iconPath + "\">";
                    else
                        mText += "<IMG src=\"" + NOiconPath + "\">";
                    mText += "</a>\n";
                }
            }
            if (a->status() != Attendee::NeedsAction )
                mText +="[" + a->statusStr() + "] ";
            if (a->role() == Attendee::Chair )
                mText +="(" + a->roleStr().left(1) + ".)";
        }
        mText.append("</li></ul>");
    }
}

void KIncidenceFormatter::formatReadOnly(Incidence *event)
{
 if (event->isReadOnly()) {
        addTag("p","<em>(" + i18n("read-only") + ")</em>");
    }
}
QString KIncidenceFormatter::deTag(QString text)
{
#if QT_VERSION >= 0x030000
    text.replace( '<' , "&lt;" );
    text.replace( '>' , "&gt;" );
#else
    if ( text.find ('<') >= 0 ) {
        text.replace( QRegExp("<") , "&lt;" );
    }
    if ( text.find ('>') >= 0 ) {
        text.replace( QRegExp(">") , "&gt;" );
    }
#endif
    return text;
}