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 Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
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 //  QLabel *dateLabel =
00067 //      new QLabel(KGlobal::locale()->formatDate(QDate::currentDate()),this);
00068 //  dateLabel->setMargin(2);
00069 //  dateLabel->setAlignment(AlignCenter);
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 //  topLayout->addWidget(dateLabel);
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("korganizer");
00091   QString *ipath = new QString();
00092   kil.loadIcon("korganizer",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         // FIXME: This should actually be cleaned up. Libkcal should
00132         // provide a method to return a list of all recurrences in a
00133         // given time span.
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   int replies = 0;
00184   events = calendar()->events( QDate::currentDate(), QDate(2975,12,6) );
00185   Event::List::ConstIterator it2;
00186   for( it2 = events.begin(); it2 != events.end(); ++it2 ) {
00187     Event *ev = *it2;
00188     Attendee *me = ev->attendeeByMails( KOPrefs::instance()->allEmails() );
00189     if (me!=0) {
00190       if (me->status()==Attendee::NeedsAction && me->RSVP()) {
00191         if (replies == 0) {
00192           mText += "<p></p>";
00193           kil.loadIcon("reply",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00194           mText += "<h2><img src=\"";
00195           mText += *ipath;
00196           mText += "\">";
00197           mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
00198           mText += "<table>\n";
00199         }
00200         replies++;
00201         appendEvent( ev );
00202       }
00203     }
00204   }
00205   todos = calendar()->todos();
00206   Todo::List::ConstIterator it3;
00207   for( it3 = todos.begin(); it3 != todos.end(); ++it3 ) {
00208     Todo *to = *it3;
00209     Attendee *me = to->attendeeByMails( KOPrefs::instance()->allEmails() );
00210     if (me!=0) {
00211       if (me->status()==Attendee::NeedsAction && me->RSVP()) {
00212         if (replies == 0) {
00213           mText += "<p></p>";
00214           kil.loadIcon("reply",KIcon::NoGroup,22,KIcon::DefaultState,ipath);
00215           mText += "<h2><img src=\"";
00216           mText += *ipath;
00217           mText += "\">";
00218           mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
00219           mText += "<table>\n";
00220         }
00221         replies++;
00222         appendEvent(to);
00223       }
00224     }
00225     kdDebug () << "check for todo-replies..." << endl;
00226   }
00227   if (replies > 0 ) mText += "</table>\n";
00228 
00229 
00230   mText += "</td></tr>\n</table>\n";
00231 
00232   kdDebug(5850) << "KOWhatsNextView::updateView: text: " << mText << endl;
00233 
00234   delete ipath;
00235 
00236   mView->setText(mText);
00237 }
00238 
00239 void KOWhatsNextView::showDates( const QDate &start, const QDate &end )
00240 {
00241   mStartDate = start;
00242   mEndDate = end;
00243   updateView();
00244 }
00245 
00246 void KOWhatsNextView::showIncidences( const Incidence::List & )
00247 {
00248 }
00249 
00250 void KOWhatsNextView::changeIncidenceDisplay(Incidence *, int action)
00251 {
00252   switch(action) {
00253     case KOGlobals::INCIDENCEADDED:
00254     case KOGlobals::INCIDENCEEDITED:
00255     case KOGlobals::INCIDENCEDELETED:
00256       updateView();
00257       break;
00258     default:
00259       kdDebug(5850) << "KOWhatsNextView::changeIncidenceDisplay(): Illegal action " << action << endl;
00260   }
00261 }
00262 
00263 void KOWhatsNextView::appendEvent( Incidence *ev, const QDateTime &start,
00264                                    const QDateTime &end )
00265 {
00266   kdDebug(5850) << "KOWhatsNextView::appendEvent(): " << ev->uid() << endl;
00267 
00268   mText += "<tr><td><b>";
00269 //  if (!ev->doesFloat()) {
00270     if (ev->type()=="Event") {
00271       Event *event = static_cast<Event *>(ev);
00272       QDateTime starttime( start );
00273       if ( !starttime.isValid() )
00274         starttime = event->dtStart();
00275       QDateTime endtime( end );
00276       if ( !endtime.isValid() )
00277         endtime = starttime.addSecs(
00278                   event->dtStart().secsTo( event->dtEnd() ) );
00279 
00280       if ( starttime.date().daysTo( endtime.date() ) >= 1 ) {
00281         mText += i18n("date from - to", "%1 - %2")
00282               .arg( KGlobal::locale()->formatDateTime( starttime ) )
00283               .arg( KGlobal::locale()->formatDateTime( endtime ) );
00284       } else {
00285         /*if (reply) */
00286         mText += i18n("date, from - to", "%1, %2 - %3")
00287             .arg( KGlobal::locale()->formatDate( starttime.date(), true ) )
00288             .arg( KGlobal::locale()->formatTime( starttime.time() ) )
00289             .arg( KGlobal::locale()->formatTime( endtime.time() ) );
00290       }
00291     }
00292 //  }
00293   mText += "</b></td><td><a ";
00294   if (ev->type()=="Event") mText += "href=\"event:";
00295   if (ev->type()=="Todo") mText += "href=\"todo:";
00296   mText += ev->uid() + "\">";
00297   mText += ev->summary();
00298   mText += "</a></td></tr>\n";
00299 }
00300 
00301 void KOWhatsNextView::appendTodo( Incidence *ev )
00302 {
00303   if ( mTodos.find( ev ) != mTodos.end() ) return;
00304 
00305   mTodos.append( ev );
00306 
00307   mText += "<li><a href=\"todo:" + ev->uid() + "\">";
00308   mText += ev->summary();
00309   mText += "</a>";
00310 
00311   if ( ev->type()=="Todo" ) {
00312     Todo *todo = static_cast<Todo*>(ev);
00313     if ( todo->hasDueDate() ) {
00314       mText += i18n("  (Due: %1)")
00315          .arg( (todo->doesFloat())?(todo->dtDueDateStr()):(todo->dtDueStr()) );
00316     }
00317   }
00318   mText += "</li>\n";
00319 }
00320 
00321 void KOWhatsNextView::showIncidence( const QString &uid )
00322 {
00323   kdDebug(5850) << "KOWhatsNextView::showIncidence(): " << uid << endl;
00324   Incidence *incidence = 0;
00325 
00326   if ( uid.startsWith( "event://" ) ) {
00327     incidence = calendar()->incidence( uid.mid( 8 ) );
00328   } else if ( uid.startsWith( "todo://" ) ) {
00329     incidence = calendar()->incidence( uid.mid( 7 ) );
00330   }
00331   if ( incidence ) emit showIncidenceSignal( incidence );
00332 }
00333 
00334 #include "kowhatsnextview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys