Simplify code for 'This week' / 'Next week' logic

Also make the calendar work when there are many all-day events shown. See

 http://people.freedesktop.org/~david/many-all-day-events.png

for details.

Signed-off-by: David Zeuthen <davidz@redhat.com>
This commit is contained in:
David Zeuthen 2011-01-28 14:30:52 -05:00
parent 0b05280e03
commit e82cbf3cc8
3 changed files with 31 additions and 16 deletions

View File

@ -41,6 +41,7 @@ function _getBeginningOfDay(date) {
let ret = new Date(date.getTime()); let ret = new Date(date.getTime());
ret.setHours(0); ret.setHours(0);
ret.setMinutes(0); ret.setMinutes(0);
ret.setSeconds(0);
ret.setMilliseconds(0); ret.setMilliseconds(0);
return ret; return ret;
} }
@ -49,6 +50,7 @@ function _getEndOfDay(date) {
let ret = new Date(date.getTime()); let ret = new Date(date.getTime());
ret.setHours(23); ret.setHours(23);
ret.setMinutes(59); ret.setMinutes(59);
ret.setSeconds(59);
ret.setMilliseconds(999); ret.setMilliseconds(999);
return ret; return ret;
} }
@ -686,21 +688,24 @@ EventsList.prototype = {
let dayEnd = _getEndOfDay(now); let dayEnd = _getEndOfDay(now);
this._addPeriod(_("Today"), dayBegin, dayEnd, false, true); this._addPeriod(_("Today"), dayBegin, dayEnd, false, true);
dayBegin.setDate(dayBegin.getDate() + 1); let tomorrowBegin = new Date(dayBegin.getTime() + 86400 * 1000);
dayEnd.setDate(dayEnd.getDate() + 1); let tomorrowEnd = new Date(dayEnd.getTime() + 86400 * 1000);
this._addPeriod(_("Tomorrow"), dayBegin, dayEnd, false, true); this._addPeriod(_("Tomorrow"), tomorrowBegin, tomorrowEnd, false, true);
if (dayEnd.getDay() == 6 || dayEnd.getDay() == 0) { if (dayEnd.getDay() <= 4) {
dayBegin.setDate(dayEnd.getDate() + 1); /* if now is Sunday through Thursday show "This week" and include events up until
dayEnd.setDate(dayBegin.getDate() + 6 - dayBegin.getDay()); * and including Saturday
*/
this._addPeriod(_("Next week"), dayBegin, dayEnd, true, false); let thisWeekBegin = new Date(dayBegin.getTime() + 2 * 86400 * 1000);
return; let thisWeekEnd = new Date(dayEnd.getTime() + (6 - dayEnd.getDay()) * 86400 * 1000);
this._addPeriod(_("This week"), thisWeekBegin, thisWeekEnd, true, false);
} else { } else {
let d = 6 - dayEnd.getDay() - 1; /* otherwise it's a Friday or Saturday... show "Next week" and include events up
dayBegin.setDate(dayBegin.getDate() + 1); * until and including *next* Saturday
dayEnd.setDate(dayEnd.getDate() + 1 + d); */
this._addPeriod(_("This week"), dayBegin, dayEnd, true, false); let nextWeekBegin = new Date(dayBegin.getTime() + 2 * 86400 * 1000);
let nextWeekEnd = new Date(dayEnd.getTime() + (13 - dayEnd.getDay()) * 86400 * 1000);
this._addPeriod(_("Next week"), nextWeekBegin, nextWeekEnd, true, false);
} }
}, },

View File

@ -87,7 +87,7 @@ DateMenuButton.prototype = {
item = new PopupMenu.PopupSeparatorMenuItem(); item = new PopupMenu.PopupSeparatorMenuItem();
item.setColumnWidths(1); item.setColumnWidths(1);
vbox.add(item.actor); vbox.add(item.actor, {y_align: St.Align.END, expand: true, y_fill: false});
item = new PopupMenu.PopupMenuItem(_("Date and Time Settings")); item = new PopupMenu.PopupMenuItem(_("Date and Time Settings"));
item.connect('activate', Lang.bind(this, this._onPreferencesActivate)); item.connect('activate', Lang.bind(this, this._onPreferencesActivate));
vbox.add(item.actor); vbox.add(item.actor);

View File

@ -168,7 +168,7 @@ shell_evolution_event_source_get_events (ShellEvolutionEventSource *source,
GDateTime *next_date; GDateTime *next_date;
g_date_time_get_ymd (cur_date, &year, &mon, &day); g_date_time_get_ymd (cur_date, &year, &mon, &day);
/* g_print ("y=%04d m=%02d d=%02d: ", year, mon, day); */ /* g_print ("y=%04d m=%02d d=%02d\n", year, mon, day); */
/* Silently drop events not in range (see comment in /* Silently drop events not in range (see comment in
* shell_evolution_event_source_request_range() above) * shell_evolution_event_source_request_range() above)
@ -188,9 +188,19 @@ shell_evolution_event_source_get_events (ShellEvolutionEventSource *source,
{ {
CalendarAppointment *appointment = l->data; CalendarAppointment *appointment = l->data;
ShellEvolutionEvent *event; ShellEvolutionEvent *event;
gint64 start_time;
if (appointment->is_all_day)
{
start_time = g_date_time_to_unix (cur_date) * G_GINT64_CONSTANT (1000);
}
else
{
start_time = appointment->start_time * G_GINT64_CONSTANT (1000);
}
event = shell_evolution_event_new (appointment->summary, event = shell_evolution_event_new (appointment->summary,
appointment->is_all_day, appointment->is_all_day,
appointment->start_time * G_GINT64_CONSTANT (1000)); start_time);
result = g_list_prepend (result, event); result = g_list_prepend (result, event);
} }
g_slist_foreach (events, (GFunc) calendar_event_free, NULL); g_slist_foreach (events, (GFunc) calendar_event_free, NULL);