2009-09-30 14:02:08 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2009-10-01 19:18:20 +00:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2010-05-05 21:05:42 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
2009-09-30 14:02:08 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
2010-10-20 18:30:35 +00:00
|
|
|
const Signals = imports.signals;
|
2010-05-27 07:31:46 +00:00
|
|
|
const Pango = imports.gi.Pango;
|
2010-11-29 21:41:05 +00:00
|
|
|
const Gettext_gtk30 = imports.gettext.domain('gtk30');
|
2010-10-13 21:36:52 +00:00
|
|
|
const Gettext = imports.gettext.domain('gnome-shell');
|
|
|
|
const _ = Gettext.gettext;
|
2010-12-20 18:51:44 +00:00
|
|
|
const Mainloop = imports.mainloop;
|
2009-09-30 14:02:08 +00:00
|
|
|
|
|
|
|
const MSECS_IN_DAY = 24 * 60 * 60 * 1000;
|
2010-05-27 07:31:46 +00:00
|
|
|
const WEEKDATE_HEADER_WIDTH_DIGITS = 3;
|
2010-05-05 21:05:42 +00:00
|
|
|
const SHOW_WEEKDATE_KEY = 'show-weekdate';
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
// in org.gnome.desktop.interface
|
|
|
|
const CLOCK_FORMAT_KEY = 'clock-format';
|
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
function _sameDay(dateA, dateB) {
|
|
|
|
return (dateA.getDate() == dateB.getDate() &&
|
|
|
|
dateA.getMonth() == dateB.getMonth() &&
|
|
|
|
dateA.getYear() == dateB.getYear());
|
|
|
|
}
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
function _sameYear(dateA, dateB) {
|
|
|
|
return (dateA.getYear() == dateB.getYear());
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:36:52 +00:00
|
|
|
/* TODO: maybe needs config - right now we assume that Saturday and
|
2011-01-20 18:08:58 +00:00
|
|
|
* Sunday are non-work days (not true in e.g. Israel, it's Sunday and
|
2010-10-13 21:36:52 +00:00
|
|
|
* Monday there)
|
|
|
|
*/
|
|
|
|
function _isWorkDay(date) {
|
|
|
|
return date.getDay() != 0 && date.getDay() != 6;
|
|
|
|
}
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
function _getBeginningOfDay(date) {
|
|
|
|
let ret = new Date(date.getTime());
|
|
|
|
ret.setHours(0);
|
|
|
|
ret.setMinutes(0);
|
|
|
|
ret.setMilliseconds(0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getEndOfDay(date) {
|
|
|
|
let ret = new Date(date.getTime());
|
|
|
|
ret.setHours(23);
|
|
|
|
ret.setMinutes(59);
|
|
|
|
ret.setMilliseconds(999);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _formatTaskTime(task, clockFormat) {
|
|
|
|
let ret;
|
|
|
|
if (task.allDay) {
|
|
|
|
/* Translators: Shown in calendar event list for all day events */
|
|
|
|
ret = _("All Day");
|
|
|
|
} else {
|
|
|
|
switch (clockFormat) {
|
|
|
|
case '24h':
|
|
|
|
ret = task.date.toLocaleFormat('%H:%M');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* explicit fall-through */
|
|
|
|
case '12h':
|
|
|
|
ret = task.date.toLocaleFormat('%l:%M %p');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
function _getCalendarWeekForDate(date) {
|
2010-05-30 21:17:40 +00:00
|
|
|
// Based on the algorithms found here:
|
|
|
|
// http://en.wikipedia.org/wiki/Talk:ISO_week_date
|
|
|
|
let midnightDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
|
|
// Need to get Monday to be 1 ... Sunday to be 7
|
|
|
|
let dayOfWeek = 1 + ((midnightDate.getDay() + 6) % 7);
|
|
|
|
let nearestThursday = new Date(midnightDate.getFullYear(), midnightDate.getMonth(),
|
|
|
|
midnightDate.getDate() + (4 - dayOfWeek));
|
|
|
|
|
|
|
|
let jan1st = new Date(nearestThursday.getFullYear(), 0, 1);
|
|
|
|
let diffDate = nearestThursday - jan1st;
|
|
|
|
let dayNumber = Math.floor(Math.abs(diffDate) / MSECS_IN_DAY);
|
|
|
|
let weekNumber = Math.floor(dayNumber / 7) + 1;
|
|
|
|
|
|
|
|
return weekNumber;
|
2010-05-27 07:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _getDigitWidth(actor){
|
|
|
|
let context = actor.get_pango_context();
|
|
|
|
let themeNode = actor.get_theme_node();
|
|
|
|
let font = themeNode.get_font();
|
|
|
|
let metrics = context.get_metrics(font, context.get_language());
|
|
|
|
let width = metrics.get_approximate_digit_width();
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2011-01-19 17:44:40 +00:00
|
|
|
function _getCalendarDayAbbreviation(dayNumber) {
|
|
|
|
let abbreviations = [
|
|
|
|
/* Translators: Calendar grid abbreviation for Sunday.
|
|
|
|
*
|
|
|
|
* NOTE: These abbreviations are always shown together and in
|
|
|
|
* order, e.g. "S M T W T F S".
|
2010-10-13 21:36:52 +00:00
|
|
|
*/
|
2011-01-19 17:44:40 +00:00
|
|
|
_("S"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Monday */
|
|
|
|
_("M"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Tuesday */
|
|
|
|
_("T"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Wednesday */
|
|
|
|
_("W"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Thursday */
|
|
|
|
_("T"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Friday */
|
|
|
|
_("F"),
|
|
|
|
/* Translators: Calendar grid abbreviation for Saturday */
|
|
|
|
_("S")
|
|
|
|
];
|
|
|
|
return abbreviations[dayNumber];
|
2010-10-13 21:36:52 +00:00
|
|
|
}
|
|
|
|
|
2011-01-19 17:44:40 +00:00
|
|
|
function _getEventDayAbbreviation(dayNumber) {
|
|
|
|
let abbreviations = [
|
|
|
|
/* Translators: Event list abbreviation for Sunday.
|
|
|
|
*
|
|
|
|
* NOTE: These abbreviations are normally not shown together
|
|
|
|
* so they need to be unique (e.g. Tuesday and Thursday cannot
|
|
|
|
* both be 'T').
|
|
|
|
*/
|
|
|
|
_("Su"),
|
|
|
|
/* Translators: Event list abbreviation for Monday */
|
|
|
|
_("M"),
|
|
|
|
/* Translators: Event list abbreviation for Tuesday */
|
|
|
|
_("T"),
|
|
|
|
/* Translators: Event list abbreviation for Wednesday */
|
|
|
|
_("W"),
|
|
|
|
/* Translators: Event list abbreviation for Thursday */
|
|
|
|
_("Th"),
|
|
|
|
/* Translators: Event list abbreviation for Friday */
|
|
|
|
_("F"),
|
|
|
|
/* Translators: Event list abbreviation for Saturday */
|
|
|
|
_("S")
|
|
|
|
];
|
|
|
|
return abbreviations[dayNumber];
|
2010-11-30 19:31:32 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 18:51:44 +00:00
|
|
|
// Abstraction for an appointment/task in a calendar
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
function CalendarTask(date, summary, allDay) {
|
|
|
|
this._init(date, summary, allDay);
|
2010-12-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CalendarTask.prototype = {
|
2011-01-25 14:55:41 +00:00
|
|
|
_init: function(date, summary, allDay) {
|
2010-12-20 18:51:44 +00:00
|
|
|
this.date = date;
|
|
|
|
this.summary = summary;
|
2011-01-25 14:55:41 +00:00
|
|
|
this.allDay = allDay;
|
2010-12-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Interface for appointments/tasks - e.g. the contents of a calendar
|
|
|
|
//
|
|
|
|
// TODO: write e.g. EvolutionEventSource
|
|
|
|
|
|
|
|
// First, an implementation with no events
|
|
|
|
function EmptyEventSource() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
EmptyEventSource.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
getTasks: function(begin, end) {
|
|
|
|
let result = [];
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2010-12-20 19:36:29 +00:00
|
|
|
hasTasks: function(day) {
|
2010-12-20 18:51:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(EmptyEventSource.prototype);
|
|
|
|
|
|
|
|
// Second, an implementation with fake events
|
|
|
|
function FakeEventSource() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeEventSource.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
this._fakeTasks = [];
|
2010-12-20 18:51:44 +00:00
|
|
|
|
|
|
|
// Generate fake events
|
|
|
|
//
|
2011-01-25 14:55:41 +00:00
|
|
|
let midnightToday = _getBeginningOfDay(new Date());
|
2010-12-20 18:51:44 +00:00
|
|
|
let summary = '';
|
|
|
|
|
|
|
|
// '10-oclock pow-wow' is an event occuring IN THE PAST every four days at 10am
|
2011-01-25 14:55:41 +00:00
|
|
|
for (let n = 0; n < 10; n++) {
|
|
|
|
let t = new Date(midnightToday.getTime() - n * 4 * 86400 * 1000);
|
2010-12-20 18:51:44 +00:00
|
|
|
t.setHours(10);
|
|
|
|
summary = '10-oclock pow-wow (n=' + n + ')';
|
2011-01-25 14:55:41 +00:00
|
|
|
this._fakeTasks.push(new CalendarTask(t, summary, false));
|
2010-12-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// '11-oclock thing' is an event occuring every three days at 11am
|
2011-01-25 14:55:41 +00:00
|
|
|
for (let n = 0; n < 10; n++) {
|
|
|
|
let t = new Date(midnightToday.getTime() + n * 3 * 86400 * 1000);
|
2010-12-20 18:51:44 +00:00
|
|
|
t.setHours(11);
|
|
|
|
summary = '11-oclock thing (n=' + n + ')';
|
2011-01-25 14:55:41 +00:00
|
|
|
this._fakeTasks.push(new CalendarTask(t, summary, false));
|
2010-12-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'Weekly Meeting' is an event occuring every seven days at 1:45pm (two days displaced)
|
2011-01-25 14:55:41 +00:00
|
|
|
for (let n = 0; n < 5; n++) {
|
|
|
|
let t = new Date(midnightToday.getTime() + (n * 7 + 2) * 86400 * 1000);
|
2010-12-20 18:51:44 +00:00
|
|
|
t.setHours(13);
|
|
|
|
t.setMinutes(45);
|
|
|
|
summary = 'Weekly Meeting (n=' + n + ')';
|
2011-01-25 14:55:41 +00:00
|
|
|
this._fakeTasks.push(new CalendarTask(t, summary, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'Fun All Day' is an all-day event occuring every fortnight (three days displayed)
|
|
|
|
for (let n = 0; n < 10; n++) {
|
|
|
|
let t = new Date(midnightToday.getTime() + (n * 14 + 3) * 86400 * 1000);
|
|
|
|
summary = 'Fun All Day (n=' + n + ')';
|
|
|
|
this._fakeTasks.push(new CalendarTask(t, summary, true));
|
2010-12-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'Get Married' is an event that actually reflects reality (Dec 4, 2010) :-)
|
2011-01-25 14:55:41 +00:00
|
|
|
this._fakeTasks.push(new CalendarTask(new Date(2010, 11, 4, 16, 0), 'Get Married', false));
|
2010-12-20 18:51:44 +00:00
|
|
|
|
|
|
|
// ditto for 'NE Patriots vs NY Jets'
|
2011-01-25 14:55:41 +00:00
|
|
|
this._fakeTasks.push(new CalendarTask(new Date(2010, 11, 6, 20, 30), 'NE Patriots vs NY Jets', false));
|
2010-12-20 18:51:44 +00:00
|
|
|
|
2010-12-20 19:36:29 +00:00
|
|
|
// An event for tomorrow @6:30pm that is added/removed every five
|
|
|
|
// seconds (to check that the ::changed signal works)
|
2011-01-25 14:55:41 +00:00
|
|
|
let transientEventDate = new Date(midnightToday.getTime() + 86400 * 1000);
|
2011-01-19 18:31:29 +00:00
|
|
|
transientEventDate.setHours(18);
|
|
|
|
transientEventDate.setMinutes(30);
|
|
|
|
transientEventDate.setSeconds(0);
|
2010-12-20 18:51:44 +00:00
|
|
|
Mainloop.timeout_add(5000, Lang.bind(this, this._updateTransientEvent));
|
|
|
|
this._includeTransientEvent = false;
|
2011-01-25 14:55:41 +00:00
|
|
|
this._transientEvent = new CalendarTask(transientEventDate, 'A Transient Event', false);
|
2010-12-20 18:51:44 +00:00
|
|
|
this._transientEventCounter = 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateTransientEvent: function() {
|
|
|
|
this._includeTransientEvent = !this._includeTransientEvent;
|
|
|
|
this._transientEventCounter = this._transientEventCounter + 1;
|
|
|
|
this._transientEvent.summary = 'A Transient Event (' + this._transientEventCounter + ')';
|
|
|
|
this.emit('changed');
|
|
|
|
Mainloop.timeout_add(5000, Lang.bind(this, this._updateTransientEvent));
|
|
|
|
},
|
|
|
|
|
|
|
|
getTasks: function(begin, end) {
|
|
|
|
let result = [];
|
|
|
|
//log('begin:' + begin);
|
|
|
|
//log('end: ' + end);
|
2011-01-19 18:31:29 +00:00
|
|
|
for(let n = 0; n < this._fakeTasks.length; n++) {
|
|
|
|
let task = this._fakeTasks[n];
|
2010-12-20 18:51:44 +00:00
|
|
|
if (task.date >= begin && task.date <= end) {
|
|
|
|
result.push(task);
|
|
|
|
}
|
|
|
|
//log('when:' + task.date + ' summary:' + task.summary);
|
|
|
|
}
|
|
|
|
if (this._includeTransientEvent && this._transientEvent.date >= begin && this._transientEvent.date <= end)
|
|
|
|
result.push(this._transientEvent);
|
2011-01-25 14:55:41 +00:00
|
|
|
result.sort(function(task1, task2) {
|
|
|
|
return task1.date.getTime() - task2.date.getTime();
|
|
|
|
});
|
2010-12-20 18:51:44 +00:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2010-12-20 19:36:29 +00:00
|
|
|
hasTasks: function(day) {
|
2011-01-25 14:55:41 +00:00
|
|
|
let dayBegin = _getBeginningOfDay(day);
|
|
|
|
let dayEnd = _getEndOfDay(day);
|
2010-12-20 18:51:44 +00:00
|
|
|
|
|
|
|
let tasks = this.getTasks(dayBegin, dayEnd);
|
|
|
|
|
|
|
|
if (tasks.length == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(FakeEventSource.prototype);
|
|
|
|
|
2011-01-19 17:44:40 +00:00
|
|
|
// Calendar:
|
|
|
|
// @eventSource: is an object implementing the EventSource API, e.g. the
|
2010-12-20 19:36:29 +00:00
|
|
|
// getTasks(), hasTasks() methods and the ::changed signal.
|
2011-01-19 18:55:04 +00:00
|
|
|
function Calendar(eventSource) {
|
|
|
|
this._init(eventSource);
|
2010-03-15 13:50:05 +00:00
|
|
|
}
|
2009-09-30 14:02:08 +00:00
|
|
|
|
|
|
|
Calendar.prototype = {
|
2011-01-19 18:55:04 +00:00
|
|
|
_init: function(eventSource) {
|
2011-01-19 18:31:29 +00:00
|
|
|
this._eventSource = eventSource;
|
2010-12-20 18:51:44 +00:00
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
this._eventSource.connect('changed', Lang.bind(this, this._update));
|
2010-11-30 19:31:32 +00:00
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
// FIXME: This is actually the fallback method for GTK+ for the week start;
|
|
|
|
// GTK+ by preference uses nl_langinfo (NL_TIME_FIRST_WEEKDAY). We probably
|
|
|
|
// should add a C function so we can do the full handling.
|
|
|
|
this._weekStart = NaN;
|
2010-05-27 07:31:46 +00:00
|
|
|
this._weekdate = NaN;
|
|
|
|
this._digitWidth = NaN;
|
2010-05-05 21:05:42 +00:00
|
|
|
this._settings = new Gio.Settings({ schema: 'org.gnome.shell.calendar' });
|
2010-05-27 07:31:46 +00:00
|
|
|
|
2010-05-05 21:05:42 +00:00
|
|
|
this._settings.connect('changed::' + SHOW_WEEKDATE_KEY, Lang.bind(this, this._onSettingsChange));
|
|
|
|
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);
|
2010-05-27 07:31:46 +00:00
|
|
|
|
2010-11-29 21:41:05 +00:00
|
|
|
let weekStartString = Gettext_gtk30.gettext('calendar:week_start:0');
|
2010-05-13 19:46:04 +00:00
|
|
|
if (weekStartString.indexOf('calendar:week_start:') == 0) {
|
2009-09-30 14:02:08 +00:00
|
|
|
this._weekStart = parseInt(weekStartString.substring(20));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(this._weekStart) || this._weekStart < 0 || this._weekStart > 6) {
|
2010-05-13 19:46:04 +00:00
|
|
|
log('Translation of "calendar:week_start:0" in GTK+ is not correct');
|
2009-12-25 08:34:40 +00:00
|
|
|
this._weekStart = 0;
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the ordering for month/year in the calendar heading
|
2011-01-25 14:55:41 +00:00
|
|
|
this._headerFormatWithoutYear = '%B';
|
2010-11-29 21:41:05 +00:00
|
|
|
switch (Gettext_gtk30.gettext('calendar:MY')) {
|
2010-05-13 19:46:04 +00:00
|
|
|
case 'calendar:MY':
|
|
|
|
this._headerFormat = '%B %Y';
|
2009-09-30 14:02:08 +00:00
|
|
|
break;
|
2010-05-13 19:46:04 +00:00
|
|
|
case 'calendar:YM':
|
|
|
|
this._headerFormat = '%Y %B';
|
2009-09-30 14:02:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-05-13 19:46:04 +00:00
|
|
|
log('Translation of "calendar:MY" in GTK+ is not correct');
|
|
|
|
this._headerFormat = '%B %Y';
|
2009-09-30 14:02:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start off with the current date
|
2011-01-19 18:31:29 +00:00
|
|
|
this.selectedDate = new Date();
|
2009-09-30 14:02:08 +00:00
|
|
|
|
|
|
|
this.actor = new St.Table({ homogeneous: false,
|
2010-05-13 19:46:04 +00:00
|
|
|
style_class: 'calendar',
|
2009-10-01 19:18:20 +00:00
|
|
|
reactive: true });
|
|
|
|
|
|
|
|
this.actor.connect('scroll-event',
|
|
|
|
Lang.bind(this, this._onScroll));
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
this._buildHeader ();
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sets the calendar to show a specific date
|
|
|
|
setDate: function(date) {
|
2011-01-19 18:31:29 +00:00
|
|
|
if (!_sameDay(date, this.selectedDate)) {
|
|
|
|
this.selectedDate = date;
|
2010-05-27 07:31:46 +00:00
|
|
|
this._update();
|
2011-01-19 18:31:29 +00:00
|
|
|
this.emit('selected-date-changed', new Date(this.selectedDate));
|
2010-05-27 07:31:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildHeader: function() {
|
|
|
|
let offsetCols = this._useWeekdate ? 1 : 0;
|
|
|
|
this.actor.destroy_children();
|
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
// Top line of the calendar '<| September 2009 |>'
|
|
|
|
this._topBox = new St.BoxLayout();
|
|
|
|
this.actor.add(this._topBox,
|
2010-05-27 07:31:46 +00:00
|
|
|
{ row: 0, col: 0, col_span: offsetCols + 7 });
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
this.actor.connect('style-changed', Lang.bind(this, this._onStyleChange));
|
2009-12-25 08:34:40 +00:00
|
|
|
|
2010-12-15 20:37:28 +00:00
|
|
|
let back = new St.Button({ style_class: 'calendar-change-month-back' });
|
2009-09-30 14:02:08 +00:00
|
|
|
this._topBox.add(back);
|
2010-12-20 18:51:44 +00:00
|
|
|
back.connect('clicked', Lang.bind(this, this._onPrevMonthButtonClicked));
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2010-10-13 21:36:52 +00:00
|
|
|
this._dateLabel = new St.Label({style_class: 'calendar-change-month'});
|
2009-09-30 14:02:08 +00:00
|
|
|
this._topBox.add(this._dateLabel, { expand: true, x_fill: false, x_align: St.Align.MIDDLE });
|
|
|
|
|
2010-12-15 20:37:28 +00:00
|
|
|
let forward = new St.Button({ style_class: 'calendar-change-month-forward' });
|
2009-09-30 14:02:08 +00:00
|
|
|
this._topBox.add(forward);
|
2010-12-20 18:51:44 +00:00
|
|
|
forward.connect('clicked', Lang.bind(this, this._onNextMonthButtonClicked));
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2010-10-13 21:36:52 +00:00
|
|
|
// Add weekday labels...
|
|
|
|
//
|
2009-09-30 14:02:08 +00:00
|
|
|
// We need to figure out the abbreviated localized names for the days of the week;
|
|
|
|
// we do this by just getting the next 7 days starting from right now and then putting
|
|
|
|
// them in the right cell in the table. It doesn't matter if we add them in order
|
2011-01-19 18:31:29 +00:00
|
|
|
let iter = new Date(this.selectedDate);
|
2009-09-30 14:02:08 +00:00
|
|
|
iter.setSeconds(0); // Leap second protection. Hah!
|
2009-10-01 20:14:50 +00:00
|
|
|
iter.setHours(12);
|
2009-09-30 14:02:08 +00:00
|
|
|
for (let i = 0; i < 7; i++) {
|
2010-10-13 21:36:52 +00:00
|
|
|
// Could use iter.toLocaleFormat('%a') but that normally gives three characters
|
|
|
|
// and we want, ideally, a single character for e.g. S M T W T F S
|
2011-01-19 18:31:29 +00:00
|
|
|
let customDayAbbrev = _getCalendarDayAbbreviation(iter.getDay());
|
|
|
|
let label = new St.Label({ text: customDayAbbrev });
|
2010-10-13 21:36:52 +00:00
|
|
|
label.style_class = 'calendar-day-base calendar-day-heading';
|
|
|
|
this.actor.add(label,
|
2009-09-30 14:02:08 +00:00
|
|
|
{ row: 1,
|
2010-05-27 07:31:46 +00:00
|
|
|
col: offsetCols + (7 + iter.getDay() - this._weekStart) % 7,
|
2010-10-13 21:36:52 +00:00
|
|
|
x_fill: false, x_align: St.Align.MIDDLE });
|
2009-09-30 14:02:08 +00:00
|
|
|
iter.setTime(iter.getTime() + MSECS_IN_DAY);
|
|
|
|
}
|
|
|
|
|
2009-10-01 20:14:50 +00:00
|
|
|
// All the children after this are days, and get removed when we update the calendar
|
|
|
|
this._firstDayIndex = this.actor.get_children().length;
|
2010-05-27 07:31:46 +00:00
|
|
|
},
|
2009-10-01 20:14:50 +00:00
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
_onStyleChange: function(actor, event) {
|
|
|
|
// width of a digit in pango units
|
|
|
|
this._digitWidth = _getDigitWidth(this.actor) / Pango.SCALE;
|
|
|
|
this._setWeekdateHeaderWidth();
|
2009-09-30 14:02:08 +00:00
|
|
|
},
|
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
_setWeekdateHeaderWidth: function() {
|
|
|
|
if (this.digitWidth != NaN && this._useWeekdate && this._weekdateHeader) {
|
|
|
|
this._weekdateHeader.set_width (this._digitWidth * WEEKDATE_HEADER_WIDTH_DIGITS);
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-10-01 19:18:20 +00:00
|
|
|
_onScroll : function(actor, event) {
|
|
|
|
switch (event.get_scroll_direction()) {
|
|
|
|
case Clutter.ScrollDirection.UP:
|
|
|
|
case Clutter.ScrollDirection.LEFT:
|
2010-12-20 20:29:14 +00:00
|
|
|
this._onPrevMonthButtonClicked();
|
2009-10-01 19:18:20 +00:00
|
|
|
break;
|
|
|
|
case Clutter.ScrollDirection.DOWN:
|
|
|
|
case Clutter.ScrollDirection.RIGHT:
|
2010-12-20 20:29:14 +00:00
|
|
|
this._onNextMonthButtonClicked();
|
2009-10-01 19:18:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-20 18:51:44 +00:00
|
|
|
_onPrevMonthButtonClicked: function() {
|
2011-01-19 18:31:29 +00:00
|
|
|
let newDate = new Date(this.selectedDate);
|
|
|
|
if (newDate.getMonth() == 0) {
|
|
|
|
newDate.setMonth(11);
|
|
|
|
newDate.setFullYear(newDate.getFullYear() - 1);
|
2009-09-30 14:02:08 +00:00
|
|
|
} else {
|
2011-01-19 18:31:29 +00:00
|
|
|
newDate.setMonth(newDate.getMonth() - 1);
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
2011-01-19 18:31:29 +00:00
|
|
|
this.setDate(newDate);
|
2009-09-30 14:02:08 +00:00
|
|
|
},
|
|
|
|
|
2010-12-20 18:51:44 +00:00
|
|
|
_onNextMonthButtonClicked: function() {
|
2011-01-19 18:31:29 +00:00
|
|
|
let newDate = new Date(this.selectedDate);
|
|
|
|
if (newDate.getMonth() == 11) {
|
|
|
|
newDate.setMonth(0);
|
|
|
|
newDate.setFullYear(newDate.getFullYear() + 1);
|
2009-09-30 14:02:08 +00:00
|
|
|
} else {
|
2011-01-19 18:31:29 +00:00
|
|
|
newDate.setMonth(newDate.getMonth() + 1);
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
2011-01-19 18:31:29 +00:00
|
|
|
this.setDate(newDate);
|
2009-09-30 14:02:08 +00:00
|
|
|
},
|
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
_onSettingsChange: function() {
|
2010-05-05 21:05:42 +00:00
|
|
|
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);
|
2010-05-27 07:31:46 +00:00
|
|
|
this._buildHeader();
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
_update: function() {
|
2011-01-25 14:55:41 +00:00
|
|
|
let now = new Date();
|
|
|
|
|
|
|
|
if (_sameYear(this.selectedDate, now))
|
|
|
|
this._dateLabel.text = this.selectedDate.toLocaleFormat(this._headerFormatWithoutYear);
|
|
|
|
else
|
|
|
|
this._dateLabel.text = this.selectedDate.toLocaleFormat(this._headerFormat);
|
2009-09-30 14:02:08 +00:00
|
|
|
|
|
|
|
// Remove everything but the topBox and the weekday labels
|
|
|
|
let children = this.actor.get_children();
|
2009-10-01 20:14:50 +00:00
|
|
|
for (let i = this._firstDayIndex; i < children.length; i++)
|
2009-09-30 14:02:08 +00:00
|
|
|
children[i].destroy();
|
|
|
|
|
|
|
|
// Start at the beginning of the week before the start of the month
|
2011-01-19 18:31:29 +00:00
|
|
|
let iter = new Date(this.selectedDate);
|
2009-09-30 14:02:08 +00:00
|
|
|
iter.setDate(1);
|
|
|
|
iter.setSeconds(0);
|
2009-10-01 20:14:50 +00:00
|
|
|
iter.setHours(12);
|
2010-08-01 10:47:16 +00:00
|
|
|
let daysToWeekStart = (7 + iter.getDay() - this._weekStart) % 7;
|
|
|
|
iter.setTime(iter.getTime() - daysToWeekStart * MSECS_IN_DAY);
|
2009-09-30 14:02:08 +00:00
|
|
|
|
|
|
|
let row = 2;
|
|
|
|
while (true) {
|
2010-10-20 18:30:35 +00:00
|
|
|
let button = new St.Button({ label: iter.getDate().toString() });
|
|
|
|
|
|
|
|
let iterStr = iter.toUTCString();
|
|
|
|
button.connect('clicked', Lang.bind(this, function() {
|
2011-01-19 18:31:29 +00:00
|
|
|
let newly_selectedDate = new Date(iterStr);
|
|
|
|
this.setDate(newly_selectedDate);
|
2010-10-20 18:30:35 +00:00
|
|
|
}));
|
2010-10-13 21:36:52 +00:00
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
let styleClass;
|
|
|
|
let hasTasks;
|
|
|
|
hasTasks = this._eventSource.hasTasks(iter);
|
|
|
|
styleClass = 'calendar-day-base calendar-day';
|
2010-10-13 21:36:52 +00:00
|
|
|
if (_isWorkDay(iter))
|
2011-01-19 18:31:29 +00:00
|
|
|
styleClass += ' calendar-work-day'
|
2010-10-13 21:36:52 +00:00
|
|
|
else
|
2011-01-19 18:31:29 +00:00
|
|
|
styleClass += ' calendar-nonwork-day'
|
2010-10-13 21:36:52 +00:00
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
if (_sameDay(now, iter))
|
2011-01-19 18:31:29 +00:00
|
|
|
styleClass += ' calendar-today';
|
|
|
|
else if (iter.getMonth() != this.selectedDate.getMonth())
|
|
|
|
styleClass += ' calendar-other-month-day';
|
2010-10-13 21:36:52 +00:00
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
if (_sameDay(this.selectedDate, iter))
|
2010-12-20 18:51:44 +00:00
|
|
|
button.add_style_pseudo_class('active');
|
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
if (hasTasks)
|
|
|
|
styleClass += ' calendar-day-with-events'
|
2010-11-30 19:31:32 +00:00
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
button.style_class = styleClass;
|
2010-05-27 07:31:46 +00:00
|
|
|
|
|
|
|
let offsetCols = this._useWeekdate ? 1 : 0;
|
2010-10-20 18:30:35 +00:00
|
|
|
this.actor.add(button,
|
2010-10-13 21:36:52 +00:00
|
|
|
{ row: row, col: offsetCols + (7 + iter.getDay() - this._weekStart) % 7 });
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2010-05-27 07:31:46 +00:00
|
|
|
if (this._useWeekdate && iter.getDay() == 4) {
|
|
|
|
let label = new St.Label({ text: _getCalendarWeekForDate(iter).toString(),
|
2010-10-13 21:36:52 +00:00
|
|
|
style_class: 'calendar-day-base calendar-week-number'});
|
2010-05-27 07:31:46 +00:00
|
|
|
this.actor.add(label,
|
2010-10-13 21:36:52 +00:00
|
|
|
{ row: row, col: 0, y_align: St.Align.MIDDLE });
|
2010-05-27 07:31:46 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
iter.setTime(iter.getTime() + MSECS_IN_DAY);
|
|
|
|
if (iter.getDay() == this._weekStart) {
|
|
|
|
// We stop on the first "first day of the week" after the month we are displaying
|
2011-01-19 18:31:29 +00:00
|
|
|
if (iter.getMonth() > this.selectedDate.getMonth() || iter.getYear() > this.selectedDate.getYear())
|
2009-09-30 14:02:08 +00:00
|
|
|
break;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 18:30:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-20 18:51:44 +00:00
|
|
|
Signals.addSignalMethods(Calendar.prototype);
|
2010-11-30 19:31:32 +00:00
|
|
|
|
2011-01-19 18:31:29 +00:00
|
|
|
function EventsList(eventSource) {
|
|
|
|
this._init(eventSource);
|
2010-10-20 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventsList.prototype = {
|
2011-01-19 18:31:29 +00:00
|
|
|
_init: function(eventSource) {
|
2010-12-15 22:32:35 +00:00
|
|
|
this.actor = new St.BoxLayout({ vertical: true, style_class: 'events-header-vbox'});
|
2011-01-19 18:55:04 +00:00
|
|
|
this._date = new Date();
|
2011-01-19 18:31:29 +00:00
|
|
|
this._eventSource = eventSource;
|
2011-01-19 18:55:04 +00:00
|
|
|
this._eventSource.connect('changed', Lang.bind(this, this._update));
|
2011-01-25 14:55:41 +00:00
|
|
|
this._desktopSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
|
|
|
|
this._desktopSettings.connect('changed', Lang.bind(this, this._update));
|
2011-01-19 18:55:04 +00:00
|
|
|
this._update();
|
2010-11-30 15:50:20 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 19:31:32 +00:00
|
|
|
_addEvent: function(dayNameBox, timeBox, eventTitleBox, includeDayName, day, time, desc) {
|
|
|
|
if (includeDayName) {
|
2011-01-19 17:44:40 +00:00
|
|
|
dayNameBox.add(new St.Label( { style_class: 'events-day-dayname',
|
|
|
|
text: day } ),
|
|
|
|
{ x_fill: false } );
|
2010-11-30 19:31:32 +00:00
|
|
|
}
|
2011-01-19 17:44:40 +00:00
|
|
|
timeBox.add(new St.Label( { style_class: 'events-day-time',
|
|
|
|
text: time} ),
|
|
|
|
{ x_fill: false } );
|
|
|
|
eventTitleBox.add(new St.Label( { style_class: 'events-day-task',
|
|
|
|
text: desc} ));
|
2010-10-20 18:30:35 +00:00
|
|
|
},
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
_addPeriod: function(header, begin, end, includeDayName, showNothingScheduled) {
|
2011-01-19 18:31:29 +00:00
|
|
|
let tasks = this._eventSource.getTasks(begin, end);
|
2010-11-30 19:31:32 +00:00
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
let clockFormat = this._desktopSettings.get_string(CLOCK_FORMAT_KEY);;
|
|
|
|
|
|
|
|
if (tasks.length == 0 && !showNothingScheduled)
|
2010-11-30 19:31:32 +00:00
|
|
|
return;
|
|
|
|
|
2010-12-15 22:32:35 +00:00
|
|
|
let vbox = new St.BoxLayout( {vertical: true} );
|
|
|
|
this.actor.add(vbox);
|
|
|
|
|
|
|
|
vbox.add(new St.Label({ style_class: 'events-day-header', text: header }));
|
2010-11-30 19:31:32 +00:00
|
|
|
let box = new St.BoxLayout({style_class: 'events-header-hbox'});
|
2010-10-20 18:30:35 +00:00
|
|
|
let dayNameBox = new St.BoxLayout({ vertical: true, style_class: 'events-day-name-box' });
|
|
|
|
let timeBox = new St.BoxLayout({ vertical: true, style_class: 'events-time-box' });
|
|
|
|
let eventTitleBox = new St.BoxLayout({ vertical: true, style_class: 'events-event-box' });
|
2010-11-30 19:31:32 +00:00
|
|
|
box.add(dayNameBox, {x_fill: false});
|
|
|
|
box.add(timeBox, {x_fill: false});
|
|
|
|
box.add(eventTitleBox, {expand: true});
|
2010-12-15 22:32:35 +00:00
|
|
|
vbox.add(box);
|
2010-10-20 18:30:35 +00:00
|
|
|
|
2010-11-30 19:31:32 +00:00
|
|
|
for (let n = 0; n < tasks.length; n++) {
|
|
|
|
let task = tasks[n];
|
2010-12-20 20:29:14 +00:00
|
|
|
let dayString = _getEventDayAbbreviation(task.date.getDay());
|
2011-01-25 14:55:41 +00:00
|
|
|
let timeString = _formatTaskTime(task, clockFormat);
|
2010-11-30 19:31:32 +00:00
|
|
|
let summaryString = task.summary;
|
|
|
|
this._addEvent(dayNameBox, timeBox, eventTitleBox, includeDayName, dayString, timeString, summaryString);
|
2010-11-30 15:50:20 +00:00
|
|
|
}
|
2011-01-25 14:55:41 +00:00
|
|
|
|
|
|
|
if (tasks.length == 0 && showNothingScheduled) {
|
|
|
|
let now = new Date();
|
|
|
|
/* Translators: Text to show if there are no events */
|
|
|
|
let nothingTask = new CalendarTask(now, _("Nothing Scheduled"), true);
|
|
|
|
let timeString = _formatTaskTime(nothingTask, clockFormat);
|
|
|
|
this._addEvent(dayNameBox, timeBox, eventTitleBox, false, "", timeString, nothingTask.summary);
|
|
|
|
}
|
2010-11-30 19:31:32 +00:00
|
|
|
},
|
2010-10-20 18:30:35 +00:00
|
|
|
|
2011-01-19 18:55:04 +00:00
|
|
|
_showOtherDay: function(day) {
|
2010-10-20 18:30:35 +00:00
|
|
|
this.actor.destroy_children();
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
let dayBegin = _getBeginningOfDay(day);
|
|
|
|
let dayEnd = _getEndOfDay(day);
|
|
|
|
|
|
|
|
let dayString;
|
|
|
|
let now = new Date();
|
|
|
|
if (_sameYear(day, now))
|
|
|
|
dayString = day.toLocaleFormat('%A, %B %d');
|
|
|
|
else
|
|
|
|
dayString = day.toLocaleFormat('%A, %B %d, %Y');
|
|
|
|
this._addPeriod(dayString, dayBegin, dayEnd, false, true);
|
2010-10-20 18:30:35 +00:00
|
|
|
},
|
|
|
|
|
2011-01-19 18:55:04 +00:00
|
|
|
_showToday: function() {
|
2010-10-20 18:30:35 +00:00
|
|
|
this.actor.destroy_children();
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
let now = new Date();
|
|
|
|
let dayBegin = _getBeginningOfDay(now);
|
|
|
|
let dayEnd = _getEndOfDay(now);
|
|
|
|
this._addPeriod(_("Today"), dayBegin, dayEnd, false, true);
|
2010-10-20 18:30:35 +00:00
|
|
|
|
|
|
|
dayBegin.setDate(dayBegin.getDate() + 1);
|
|
|
|
dayEnd.setDate(dayEnd.getDate() + 1);
|
2011-01-25 14:55:41 +00:00
|
|
|
this._addPeriod(_("Tomorrow"), dayBegin, dayEnd, false, true);
|
2010-10-20 18:30:35 +00:00
|
|
|
|
|
|
|
if (dayEnd.getDay() == 6 || dayEnd.getDay() == 0) {
|
|
|
|
dayBegin.setDate(dayEnd.getDate() + 1);
|
|
|
|
dayEnd.setDate(dayBegin.getDate() + 6 - dayBegin.getDay());
|
|
|
|
|
2011-01-25 14:55:41 +00:00
|
|
|
this._addPeriod(_("Next week"), dayBegin, dayEnd, true, true);
|
2010-10-20 18:30:35 +00:00
|
|
|
return;
|
2011-01-25 14:55:41 +00:00
|
|
|
} else {
|
|
|
|
let d = 6 - dayEnd.getDay() - 1;
|
|
|
|
dayBegin.setDate(dayBegin.getDate() + 1);
|
|
|
|
dayEnd.setDate(dayEnd.getDate() + 1 + d);
|
|
|
|
this._addPeriod(_("This week"), dayBegin, dayEnd, true, true);
|
2010-10-20 18:30:35 +00:00
|
|
|
}
|
2011-01-19 18:55:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Sets the event list to show events from a specific date
|
|
|
|
setDate: function(date) {
|
|
|
|
if (!_sameDay(date, this._date)) {
|
|
|
|
this._date = date;
|
|
|
|
this._update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_update: function() {
|
|
|
|
let today = new Date();
|
|
|
|
if (_sameDay (this._date, today)) {
|
|
|
|
this._showToday();
|
|
|
|
} else {
|
|
|
|
this._showOtherDay(this._date);
|
|
|
|
}
|
2010-10-20 18:30:35 +00:00
|
|
|
}
|
|
|
|
};
|