00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qlayout.h>
00026 #include <qtextbrowser.h>
00027 #include <qtextcodec.h>
00028 #include <qfileinfo.h>
00029 #include <qlabel.h>
00030
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 #include <kdebug.h>
00034 #include <kiconloader.h>
00035 #include <kmessagebox.h>
00036
00037 #include <libkcal/calendar.h>
00038
00039 #include "koglobals.h"
00040 #include "koprefs.h"
00041 #include "koeventviewerdialog.h"
00042
00043 #include "kowhatsnextview.h"
00044
00045 using namespace KOrg;
00046
00047 void WhatsNextTextBrowser::setSource(const QString& n)
00048 {
00049 kdDebug(5850) << "WhatsNextTextBrowser::setSource(): " << n << endl;
00050
00051 if (n.startsWith("event:")) {
00052 emit showIncidence(n);
00053 return;
00054 } else if (n.startsWith("todo:")) {
00055 emit showIncidence(n);
00056 return;
00057 } else {
00058 QTextBrowser::setSource(n);
00059 }
00060 }
00061
00062 KOWhatsNextView::KOWhatsNextView(Calendar *calendar, QWidget *parent,
00063 const char *name)
00064 : KOrg::BaseView(calendar, parent, name)
00065 {
00066
00067
00068
00069
00070
00071 mView = new WhatsNextTextBrowser(this);
00072 connect(mView,SIGNAL(showIncidence(const QString &)),SLOT(showIncidence(const QString &)));
00073
00074 QBoxLayout *topLayout = new QVBoxLayout(this);
00075
00076 topLayout->addWidget(mView);
00077 }
00078
00079 KOWhatsNextView::~KOWhatsNextView()
00080 {
00081 }
00082
00083 int KOWhatsNextView::currentDateCount()
00084 {
00085 return mStartDate.daysTo( mEndDate );
00086 }
00087
00088 void KOWhatsNextView::updateView()
00089 {
00090 KIconLoader kil("kdepim");
00091 QString *ipath = new QString();
00092 kil.loadIcon("kdepim",KIcon::NoGroup,32,KIcon::DefaultState,ipath);
00093
00094 mText = "<table width=\"100%\">\n";
00095 mText += "<tr bgcolor=\"#3679AD\"><td><h1>";
00096 mText += "<img src=\"";
00097 mText += *ipath;
00098 mText += "\">";
00099 mText += "<font color=\"white\"> ";
00100 mText += i18n("What's Next?") + "</font></h1>";
00101 mText += "</td></tr>\n<tr><td>";
00102
00103 mText += "<h2>";
00104 if ( mStartDate.daysTo( mEndDate ) < 1 ) {
00105 mText += KGlobal::locale()->formatDate( mStartDate );
00106 } else {
00107 mText += i18n("Date from - to", "%1 - %2")
00108 .arg( KGlobal::locale()->formatDate( mStartDate ) )
00109 .arg( KGlobal::locale()->formatDate( mEndDate ) );
00110 }
00111 mText+="</h2>\n";
00112
00113 Event::List events;
00114 for ( QDate date = mStartDate; date <= mEndDate; date = date.addDays( 1 ) )
00115 events += calendar()->events(date, EventSortStartDate, SortDirectionAscending);
00116
00117 if (events.count() > 0) {
00118 mText += "<p></p>";
00119 kil.loadIcon("appointment",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00120 mText += "<h2><img src=\"";
00121 mText += *ipath;
00122 mText += "\">";
00123 mText += i18n("Events:") + "</h2>\n";
00124 mText += "<table>\n";
00125 Event::List::ConstIterator it;
00126 for( it = events.begin(); it != events.end(); ++it ) {
00127 Event *ev = *it;
00128 if ( !ev->doesRecur() ){
00129 appendEvent(ev);
00130 } else {
00131
00132
00133
00134 Recurrence *recur = ev->recurrence();
00135 int duration = ev->dtStart().secsTo( ev->dtEnd() );
00136 QDateTime start = recur->getPreviousDateTime(
00137 QDateTime( mStartDate, QTime() ) );
00138 QDateTime end = start.addSecs( duration );
00139 if ( end.date() >= mStartDate ) {
00140 appendEvent( ev, start, end );
00141 }
00142 start = recur->getNextDateTime( start );
00143 while ( start.isValid() && start.date() <= mEndDate ) {
00144 appendEvent( ev, start );
00145 start = recur->getNextDateTime( start );
00146 }
00147 }
00148 }
00149 mText += "</table>\n";
00150 }
00151
00152 mTodos.clear();
00153 Todo::List todos = calendar()->todos( TodoSortDueDate, SortDirectionAscending );
00154 if ( todos.count() > 0 ) {
00155 kil.loadIcon("todo",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00156 mText += "<h2><img src=\"";
00157 mText += *ipath;
00158 mText += "\">";
00159 mText += i18n("To-do:") + "</h2>\n";
00160 mText += "<ul>\n";
00161 Todo::List::ConstIterator it;
00162 for( it = todos.begin(); it != todos.end(); ++it ) {
00163 Todo *todo = *it;
00164 if ( !todo->isCompleted() && todo->hasDueDate() && todo->dtDue().date() <= mEndDate )
00165 appendTodo(todo);
00166 }
00167 bool gotone = false;
00168 int priority = 1;
00169 while (!gotone && priority<=9 ) {
00170 for( it = todos.begin(); it != todos.end(); ++it ) {
00171 Todo *todo = *it;
00172 if (!todo->isCompleted() && (todo->priority() == priority) ) {
00173 appendTodo(todo);
00174 gotone = true;
00175 }
00176 }
00177 priority++;
00178 kdDebug(5850) << "adding the todos..." << endl;
00179 }
00180 mText += "</ul>\n";
00181 }
00182
00183 QStringList myEmails( KOPrefs::instance()->allEmails() );
00184 int replies = 0;
00185 events = calendar()->events( QDate::currentDate(), QDate(2975,12,6) );
00186 Event::List::ConstIterator it2;
00187 for( it2 = events.begin(); it2 != events.end(); ++it2 ) {
00188 Event *ev = *it2;
00189 Attendee *me = ev->attendeeByMails( myEmails );
00190 if (me!=0) {
00191 if (me->status()==Attendee::NeedsAction && me->RSVP()) {
00192 if (replies == 0) {
00193 mText += "<p></p>";
00194 kil.loadIcon("reply",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00195 mText += "<h2><img src=\"";
00196 mText += *ipath;
00197 mText += "\">";
00198 mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
00199 mText += "<table>\n";
00200 }
00201 replies++;
00202 appendEvent( ev );
00203 }
00204 }
00205 }
00206 todos = calendar()->todos();
00207 Todo::List::ConstIterator it3;
00208 for( it3 = todos.begin(); it3 != todos.end(); ++it3 ) {
00209 Todo *to = *it3;
00210 Attendee *me = to->attendeeByMails( myEmails );
00211 if (me!=0) {
00212 if (me->status()==Attendee::NeedsAction && me->RSVP()) {
00213 if (replies == 0) {
00214 mText += "<p></p>";
00215 kil.loadIcon("reply",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00216 mText += "<h2><img src=\"";
00217 mText += *ipath;
00218 mText += "\">";
00219 mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
00220 mText += "<table>\n";
00221 }
00222 replies++;
00223 appendEvent(to);
00224 }
00225 }
00226 kdDebug () << "check for todo-replies..." << endl;
00227 }
00228 if (replies > 0 ) mText += "</table>\n";
00229
00230
00231 mText += "</td></tr>\n</table>\n";
00232
00233 kdDebug(5850) << "KOWhatsNextView::updateView: text: " << mText << endl;
00234
00235 delete ipath;
00236
00237 mView->setText(mText);
00238 }
00239
00240 void KOWhatsNextView::showDates( const QDate &start, const QDate &end )
00241 {
00242 mStartDate = start;
00243 mEndDate = end;
00244 updateView();
00245 }
00246
00247 void KOWhatsNextView::showIncidences( const Incidence::List & )
00248 {
00249 }
00250
00251 void KOWhatsNextView::changeIncidenceDisplay(Incidence *, int action)
00252 {
00253 switch(action) {
00254 case KOGlobals::INCIDENCEADDED:
00255 case KOGlobals::INCIDENCEEDITED:
00256 case KOGlobals::INCIDENCEDELETED:
00257 updateView();
00258 break;
00259 default:
00260 kdDebug(5850) << "KOWhatsNextView::changeIncidenceDisplay(): Illegal action " << action << endl;
00261 }
00262 }
00263
00264 void KOWhatsNextView::appendEvent( Incidence *ev, const QDateTime &start,
00265 const QDateTime &end )
00266 {
00267 kdDebug(5850) << "KOWhatsNextView::appendEvent(): " << ev->uid() << endl;
00268
00269 mText += "<tr><td><b>";
00270
00271 if (ev->type()=="Event") {
00272 Event *event = static_cast<Event *>(ev);
00273 QDateTime starttime( start );
00274 if ( !starttime.isValid() )
00275 starttime = event->dtStart();
00276 QDateTime endtime( end );
00277 if ( !endtime.isValid() )
00278 endtime = starttime.addSecs(
00279 event->dtStart().secsTo( event->dtEnd() ) );
00280
00281 if ( starttime.date().daysTo( endtime.date() ) >= 1 ) {
00282 mText += i18n("date from - to", "%1 - %2")
00283 .arg( KGlobal::locale()->formatDateTime( starttime ) )
00284 .arg( KGlobal::locale()->formatDateTime( endtime ) );
00285 } else {
00286
00287 mText += i18n("date, from - to", "%1, %2 - %3")
00288 .arg( KGlobal::locale()->formatDate( starttime.date(), true ) )
00289 .arg( KGlobal::locale()->formatTime( starttime.time() ) )
00290 .arg( KGlobal::locale()->formatTime( endtime.time() ) );
00291 }
00292 }
00293
00294 mText += "</b></td><td><a ";
00295 if (ev->type()=="Event") mText += "href=\"event:";
00296 if (ev->type()=="Todo") mText += "href=\"todo:";
00297 mText += ev->uid() + "\">";
00298 mText += ev->summary();
00299 mText += "</a></td></tr>\n";
00300 }
00301
00302 void KOWhatsNextView::appendTodo( Incidence *ev )
00303 {
00304 if ( mTodos.find( ev ) != mTodos.end() ) return;
00305
00306 mTodos.append( ev );
00307
00308 mText += "<li><a href=\"todo:" + ev->uid() + "\">";
00309 mText += ev->summary();
00310 mText += "</a>";
00311
00312 if ( ev->type()=="Todo" ) {
00313 Todo *todo = static_cast<Todo*>(ev);
00314 if ( todo->hasDueDate() ) {
00315 mText += i18n(" (Due: %1)")
00316 .arg( (todo->doesFloat())?(todo->dtDueDateStr()):(todo->dtDueStr()) );
00317 }
00318 }
00319 mText += "</li>\n";
00320 }
00321
00322 void KOWhatsNextView::showIncidence( const QString &uid )
00323 {
00324 kdDebug(5850) << "KOWhatsNextView::showIncidence(): " << uid << endl;
00325 Incidence *incidence = 0;
00326
00327 if ( uid.startsWith( "event://" ) ) {
00328 incidence = calendar()->incidence( uid.mid( 8 ) );
00329 } else if ( uid.startsWith( "todo://" ) ) {
00330 incidence = calendar()->incidence( uid.mid( 7 ) );
00331 }
00332 if ( incidence ) emit showIncidenceSignal( incidence );
00333 }
00334
00335 #include "kowhatsnextview.moc"