korganizer

kowhatsnextview.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqlayout.h>
26 #include <tqtextbrowser.h>
27 #include <tqtextcodec.h>
28 #include <tqfileinfo.h>
29 #include <tqlabel.h>
30 
31 #include <tdeglobal.h>
32 #include <tdelocale.h>
33 #include <kdebug.h>
34 #include <kiconloader.h>
35 #include <tdemessagebox.h>
36 
37 #include <libkcal/calendar.h>
38 #include <libkcal/incidenceformatter.h>
39 
40 #include "koglobals.h"
41 #include "koprefs.h"
42 #include "koeventviewerdialog.h"
43 
44 #include "kowhatsnextview.h"
45 
46 using namespace KOrg;
47 
48 void WhatsNextTextBrowser::setSource(const TQString& n)
49 {
50  kdDebug(5850) << "WhatsNextTextBrowser::setSource(): " << n << endl;
51 
52  if (n.startsWith("event:")) {
53  emit showIncidence(n);
54  return;
55  } else if (n.startsWith("todo:")) {
56  emit showIncidence(n);
57  return;
58  } else {
59  TQTextBrowser::setSource(n);
60  }
61 }
62 
63 KOWhatsNextView::KOWhatsNextView(Calendar *calendar, TQWidget *parent,
64  const char *name)
65  : KOrg::BaseView(calendar, parent, name)
66 {
67 // TQLabel *dateLabel =
68 // new TQLabel(TDEGlobal::locale()->formatDate(TQDate::currentDate()),this);
69 // dateLabel->setMargin(2);
70 // dateLabel->setAlignment(AlignCenter);
71 
72  mView = new WhatsNextTextBrowser(this);
73  connect(mView,TQ_SIGNAL(showIncidence(const TQString &)),TQ_SLOT(showIncidence(const TQString &)));
74 
75  TQBoxLayout *topLayout = new TQVBoxLayout(this);
76 // topLayout->addWidget(dateLabel);
77  topLayout->addWidget(mView);
78 }
79 
80 KOWhatsNextView::~KOWhatsNextView()
81 {
82 }
83 
85 {
86  return mStartDate.daysTo( mEndDate );
87 }
88 
89 void KOWhatsNextView::updateView()
90 {
91  TDEIconLoader kil("tdepim");
92  TQString *ipath = new TQString();
93  kil.loadIcon("tdepim",TDEIcon::NoGroup,32,TDEIcon::DefaultState,ipath);
94 
95  mText = "<table width=\"100%\">\n";
96  mText += "<tr bgcolor=\"#3679AD\"><td><h1>";
97  mText += "<img src=\"";
98  mText += *ipath;
99  mText += "\">";
100  mText += "<font color=\"white\"> ";
101  mText += i18n("What's Next?") + "</font></h1>";
102  mText += "</td></tr>\n<tr><td>";
103 
104  mText += "<h2>";
105  if ( mStartDate.daysTo( mEndDate ) < 1 ) {
106  mText += TDEGlobal::locale()->formatDate( mStartDate );
107  } else {
108  mText += i18n("Date from - to", "%1 - %2")
109  .arg( TDEGlobal::locale()->formatDate( mStartDate ) )
110  .arg( TDEGlobal::locale()->formatDate( mEndDate ) );
111  }
112  mText+="</h2>\n";
113 
114  Event::List events;
115  for ( TQDate date = mStartDate; date <= mEndDate; date = date.addDays( 1 ) )
116  events += calendar()->events(date, EventSortStartDate, SortDirectionAscending);
117 
118  if (events.count() > 0) {
119  mText += "<p></p>";
120  kil.loadIcon("appointment",TDEIcon::NoGroup,22,TDEIcon::DefaultState,ipath);
121  mText += "<h2><img src=\"";
122  mText += *ipath;
123  mText += "\">";
124  mText += i18n("Events:") + "</h2>\n";
125  mText += "<table>\n";
126  Event::List::ConstIterator it;
127  for( it = events.begin(); it != events.end(); ++it ) {
128  Event *ev = *it;
129  if ( !ev->doesRecur() ){
130  appendEvent(ev);
131  } else {
132  // FIXME: This should actually be cleaned up. Libkcal should
133  // provide a method to return a list of all recurrences in a
134  // given time span.
135  Recurrence *recur = ev->recurrence();
136  int duration = ev->dtStart().secsTo( ev->dtEnd() );
137  TQDateTime start = recur->getPreviousDateTime(
138  TQDateTime( mStartDate, TQTime() ) );
139  TQDateTime end = start.addSecs( duration );
140  if ( end.date() >= mStartDate ) {
141  appendEvent( ev, start, end );
142  }
143  start = recur->getNextDateTime( start );
144  while ( start.isValid() && start.date() <= mEndDate ) {
145  appendEvent( ev, start );
146  start = recur->getNextDateTime( start );
147  }
148  }
149  }
150  mText += "</table>\n";
151  }
152 
153  mTodos.clear();
154  Todo::List todos = calendar()->todos( TodoSortDueDate, SortDirectionAscending );
155  if ( todos.count() > 0 ) {
156  kil.loadIcon("todo",TDEIcon::NoGroup,22,TDEIcon::DefaultState,ipath);
157  mText += "<h2><img src=\"";
158  mText += *ipath;
159  mText += "\">";
160  mText += i18n("To-do:") + "</h2>\n";
161  mText += "<ul>\n";
162  Todo::List::ConstIterator it;
163  for( it = todos.begin(); it != todos.end(); ++it ) {
164  Todo *todo = *it;
165  if ( !todo->isCompleted() && todo->hasDueDate() && todo->dtDue().date() <= mEndDate )
166  appendTodo(todo);
167  }
168  bool gotone = false;
169  int priority = 1;
170  while (!gotone && priority<=9 ) {
171  for( it = todos.begin(); it != todos.end(); ++it ) {
172  Todo *todo = *it;
173  if (!todo->isCompleted() && (todo->priority() == priority) ) {
174  appendTodo(todo);
175  gotone = true;
176  }
177  }
178  priority++;
179  kdDebug(5850) << "adding the todos..." << endl;
180  }
181  mText += "</ul>\n";
182  }
183 
184  TQStringList myEmails( KOPrefs::instance()->allEmails() );
185  int replies = 0;
186  events = calendar()->events( TQDate::currentDate(), TQDate(2975,12,6) );
187  Event::List::ConstIterator it2;
188  for( it2 = events.begin(); it2 != events.end(); ++it2 ) {
189  Event *ev = *it2;
190  Attendee *me = ev->attendeeByMails( myEmails );
191  if (me!=0) {
192  if (me->status()==Attendee::NeedsAction && me->RSVP()) {
193  if (replies == 0) {
194  mText += "<p></p>";
195  kil.loadIcon("reply",TDEIcon::NoGroup,22,TDEIcon::DefaultState,ipath);
196  mText += "<h2><img src=\"";
197  mText += *ipath;
198  mText += "\">";
199  mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
200  mText += "<table>\n";
201  }
202  replies++;
203  appendEvent( ev );
204  }
205  }
206  }
207  todos = calendar()->todos();
208  Todo::List::ConstIterator it3;
209  for( it3 = todos.begin(); it3 != todos.end(); ++it3 ) {
210  Todo *to = *it3;
211  Attendee *me = to->attendeeByMails( myEmails );
212  if (me!=0) {
213  if (me->status()==Attendee::NeedsAction && me->RSVP()) {
214  if (replies == 0) {
215  mText += "<p></p>";
216  kil.loadIcon("reply",TDEIcon::NoGroup,22,TDEIcon::DefaultState,ipath);
217  mText += "<h2><img src=\"";
218  mText += *ipath;
219  mText += "\">";
220  mText += i18n("Events and to-dos that need a reply:") + "</h2>\n";
221  mText += "<table>\n";
222  }
223  replies++;
224  appendEvent(to);
225  }
226  }
227  kdDebug () << "check for todo-replies..." << endl;
228  }
229  if (replies > 0 ) mText += "</table>\n";
230 
231 
232  mText += "</td></tr>\n</table>\n";
233 
234  kdDebug(5850) << "KOWhatsNextView::updateView: text: " << mText << endl;
235 
236  delete ipath;
237 
238  mView->setText(mText);
239 }
240 
241 void KOWhatsNextView::showDates( const TQDate &start, const TQDate &end )
242 {
243  mStartDate = start;
244  mEndDate = end;
245  updateView();
246 }
247 
248 void KOWhatsNextView::showIncidences( const Incidence::List &, const TQDate & )
249 {
250 }
251 
252 void KOWhatsNextView::changeIncidenceDisplay(Incidence *, int action)
253 {
254  switch(action) {
255  case KOGlobals::INCIDENCEADDED:
256  case KOGlobals::INCIDENCEEDITED:
257  case KOGlobals::INCIDENCEDELETED:
258  updateView();
259  break;
260  default:
261  kdDebug(5850) << "KOWhatsNextView::changeIncidenceDisplay(): Illegal action " << action << endl;
262  }
263 }
264 
265 void KOWhatsNextView::appendEvent( Incidence *ev, const TQDateTime &start,
266  const TQDateTime &end )
267 {
268  kdDebug(5850) << "KOWhatsNextView::appendEvent(): " << ev->uid() << endl;
269 
270  mText += "<tr><td><b>";
271 // if (!ev->doesFloat()) {
272  if (ev->type()=="Event") {
273  Event *event = static_cast<Event *>(ev);
274  TQDateTime starttime( start );
275  if ( !starttime.isValid() )
276  starttime = event->dtStart();
277  TQDateTime endtime( end );
278  if ( !endtime.isValid() )
279  endtime = starttime.addSecs(
280  event->dtStart().secsTo( event->dtEnd() ) );
281 
282  if ( starttime.date().daysTo( endtime.date() ) >= 1 ) {
283  mText += i18n("date from - to", "%1 - %2")
284  .arg( TDEGlobal::locale()->formatDateTime( starttime ) )
285  .arg( TDEGlobal::locale()->formatDateTime( endtime ) );
286  } else {
287  /*if (reply) */
288  mText += i18n("date, from - to", "%1, %2 - %3")
289  .arg( TDEGlobal::locale()->formatDate( starttime.date(), true ) )
290  .arg( TDEGlobal::locale()->formatTime( starttime.time() ) )
291  .arg( TDEGlobal::locale()->formatTime( endtime.time() ) );
292  }
293  }
294 // }
295  mText += "</b></td><td><a ";
296  if (ev->type()=="Event") mText += "href=\"event:";
297  if (ev->type()=="Todo") mText += "href=\"todo:";
298  mText += ev->uid() + "\">";
299  mText += ev->summary();
300  mText += "</a></td></tr>\n";
301 }
302 
303 void KOWhatsNextView::appendTodo( Incidence *ev )
304 {
305  if ( mTodos.find( ev ) != mTodos.end() ) return;
306 
307  mTodos.append( ev );
308 
309  mText += "<li><a href=\"todo:" + ev->uid() + "\">";
310  mText += ev->summary();
311  mText += "</a>";
312 
313  if ( ev->type()=="Todo" ) {
314  Todo *todo = static_cast<Todo*>(ev);
315  if ( todo->hasDueDate() ) {
316  mText += i18n( " (Due: %1)" ).
317  arg( IncidenceFormatter::dateTimeToString( todo->dtDue(), todo->doesFloat() ) );
318  }
319  }
320  mText += "</li>\n";
321 }
322 
323 void KOWhatsNextView::showIncidence( const TQString &uid )
324 {
325  kdDebug(5850) << "KOWhatsNextView::showIncidence(): " << uid << endl;
326  Incidence *incidence = 0;
327 
328  if ( uid.startsWith( "event://" ) ) {
329  incidence = calendar()->incidence( uid.mid( 8 ) );
330  } else if ( uid.startsWith( "todo://" ) ) {
331  incidence = calendar()->incidence( uid.mid( 7 ) );
332  }
333  if ( incidence ) {
334  emit showIncidenceSignal( incidence, TQDate() );
335  }
336 }
337 
338 #include "kowhatsnextview.moc"
bool RSVP() const
PartStat status() const
virtual Event::List events(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Incidence * incidence(const TQString &uid)
virtual Todo::List todos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
virtual TQDateTime dtEnd() const
bool doesFloat() const
TQString uid() const
Attendee * attendeeByMails(const TQStringList &, const TQString &email=TQString()) const
virtual TQDateTime dtStart() const
int priority() const
bool doesRecur() const
TQString summary() const
Recurrence * recurrence() const
TQDateTime getPreviousDateTime(const TQDateTime &afterDateTime) const
TQDateTime getNextDateTime(const TQDateTime &preDateTime) const
bool hasDueDate() const
bool isCompleted() const
TQDateTime dtDue(bool first=false) const
virtual int currentDateCount()
Return number of currently shown dates.
This class provides an interface for all views being displayed within the main calendar view.
Definition: baseview.h:60
void showIncidenceSignal(Incidence *, const TQDate &)
instructs the receiver to show the incidence in read-only mode.
virtual Calendar * calendar()
Return calendar object of this view.
Definition: baseview.h:89