korganizer

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