2009-09-30 14:02:08 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
const DBus = imports.dbus;
|
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;
|
2011-01-28 21:35:46 +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');
|
2011-01-28 21:35:46 +00:00
|
|
|
const Mainloop = imports.mainloop;
|
|
|
|
const Shell = imports.gi.Shell;
|
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-28 21:35:46 +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-28 21:35:46 +00:00
|
|
|
function _sameYear(dateA, dateB) {
|
|
|
|
return (dateA.getYear() == dateB.getYear());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: maybe needs config - right now we assume that Saturday and
|
|
|
|
* Sunday are non-work days (not true in e.g. Israel, it's Sunday and
|
|
|
|
* Monday there)
|
|
|
|
*/
|
|
|
|
function _isWorkDay(date) {
|
|
|
|
return date.getDay() != 0 && date.getDay() != 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getBeginningOfDay(date) {
|
|
|
|
let ret = new Date(date.getTime());
|
|
|
|
ret.setHours(0);
|
|
|
|
ret.setMinutes(0);
|
|
|
|
ret.setSeconds(0);
|
|
|
|
ret.setMilliseconds(0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getEndOfDay(date) {
|
|
|
|
let ret = new Date(date.getTime());
|
|
|
|
ret.setHours(23);
|
|
|
|
ret.setMinutes(59);
|
|
|
|
ret.setSeconds(59);
|
|
|
|
ret.setMilliseconds(999);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _formatEventTime(event, clockFormat) {
|
|
|
|
let ret;
|
|
|
|
if (event.allDay) {
|
2011-02-03 11:12:57 +00:00
|
|
|
/* Translators: Shown in calendar event list for all day events
|
|
|
|
* Keep it short, best if you can use less then 10 characters
|
|
|
|
*/
|
|
|
|
ret = C_("event list time", "All Day");
|
2011-01-28 21:35:46 +00:00
|
|
|
} else {
|
|
|
|
switch (clockFormat) {
|
|
|
|
case '24h':
|
2011-02-03 11:12:57 +00:00
|
|
|
/* Translators: Shown in calendar event list, if 24h format */
|
|
|
|
ret = event.date.toLocaleFormat(C_("event list time", "%H:%M"));
|
2011-01-28 21:35:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* explicit fall-through */
|
|
|
|
case '12h':
|
2011-02-03 11:12:57 +00:00
|
|
|
/* Transators: Shown in calendar event list, if 12h format */
|
|
|
|
ret = event.date.toLocaleFormat(C_("event list time", "%l:%M %p"));
|
2011-01-28 21:35:46 +00:00
|
|
|
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-28 21:35:46 +00:00
|
|
|
function _getCalendarDayAbbreviation(dayNumber) {
|
|
|
|
let abbreviations = [
|
|
|
|
/* Translators: Calendar grid abbreviation for Sunday.
|
|
|
|
*
|
2011-02-03 11:12:57 +00:00
|
|
|
* NOTE: These grid abbreviations are always shown together
|
|
|
|
* and in order, e.g. "S M T W T F S".
|
2011-01-28 21:35:46 +00:00
|
|
|
*/
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid sunday", "S"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Monday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid monday", "M"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Tuesday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid tuesday", "T"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Wednesday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid wednesday", "W"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Thursday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid thursday", "T"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Friday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid friday", "F"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Calendar grid abbreviation for Saturday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("grid saturday", "S")
|
2011-01-28 21:35:46 +00:00
|
|
|
];
|
|
|
|
return abbreviations[dayNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getEventDayAbbreviation(dayNumber) {
|
|
|
|
let abbreviations = [
|
|
|
|
/* Translators: Event list abbreviation for Sunday.
|
|
|
|
*
|
2011-02-03 11:12:57 +00:00
|
|
|
* NOTE: These list abbreviations are normally not shown together
|
2011-01-28 21:35:46 +00:00
|
|
|
* so they need to be unique (e.g. Tuesday and Thursday cannot
|
|
|
|
* both be 'T').
|
|
|
|
*/
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list sunday", "Su"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Monday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list monday", "M"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Tuesday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list tuesday", "T"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Wednesday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list wednesday", "W"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Thursday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list thursday", "Th"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Friday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list friday", "F"),
|
2011-01-28 21:35:46 +00:00
|
|
|
/* Translators: Event list abbreviation for Saturday */
|
2011-02-03 11:12:57 +00:00
|
|
|
C_("list saturday", "S")
|
2011-01-28 21:35:46 +00:00
|
|
|
];
|
|
|
|
return abbreviations[dayNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abstraction for an appointment/event in a calendar
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
function CalendarEvent(date, end, summary, allDay) {
|
|
|
|
this._init(date, end, summary, allDay);
|
2011-01-28 21:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CalendarEvent.prototype = {
|
2011-02-25 22:42:25 +00:00
|
|
|
_init: function(date, end, summary, allDay) {
|
2011-01-28 21:35:46 +00:00
|
|
|
this.date = date;
|
2011-02-25 22:42:25 +00:00
|
|
|
this.end = end;
|
2011-01-28 21:35:46 +00:00
|
|
|
this.summary = summary;
|
|
|
|
this.allDay = allDay;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Interface for appointments/events - e.g. the contents of a calendar
|
|
|
|
//
|
|
|
|
|
|
|
|
// First, an implementation with no events
|
|
|
|
function EmptyEventSource() {
|
2009-09-30 14:02:08 +00:00
|
|
|
this._init();
|
2010-03-15 13:50:05 +00:00
|
|
|
}
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
EmptyEventSource.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
requestRange: function(begin, end) {
|
|
|
|
},
|
|
|
|
|
|
|
|
getEvents: function(begin, end) {
|
|
|
|
let result = [];
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasEvents: function(day) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(EmptyEventSource.prototype);
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
const CalendarServerIface = {
|
|
|
|
name: 'org.gnome.Shell.CalendarServer',
|
|
|
|
methods: [{ name: 'GetEvents',
|
|
|
|
inSignature: 'xxb',
|
|
|
|
outSignature: 'a(sssbxxa{sv})' }],
|
|
|
|
signals: [{ name: 'Changed',
|
|
|
|
inSignature: '' }]
|
|
|
|
};
|
|
|
|
|
|
|
|
const CalendarServer = function () {
|
2011-01-28 21:35:46 +00:00
|
|
|
this._init();
|
2011-02-25 22:42:25 +00:00
|
|
|
};
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
CalendarServer.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
DBus.session.proxifyObject(this, 'org.gnome.Shell.CalendarServer', '/org/gnome/Shell/CalendarServer');
|
|
|
|
}
|
|
|
|
};
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
DBus.proxifyPrototype(CalendarServer.prototype, CalendarServerIface);
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
// an implementation that reads data from a session bus service
|
|
|
|
function DBusEventSource(owner) {
|
|
|
|
this._init(owner);
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
function _datesEqual(a, b) {
|
|
|
|
if (a < b)
|
|
|
|
return false;
|
|
|
|
else if (a > b)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
function _dateIntervalsOverlap(a0, a1, b0, b1)
|
|
|
|
{
|
|
|
|
if (a1 <= b0)
|
|
|
|
return false;
|
|
|
|
else if (b1 <= a0)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
DBusEventSource.prototype = {
|
|
|
|
_init: function(owner) {
|
|
|
|
this._resetCache();
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
this._dbusProxy = new CalendarServer(owner);
|
|
|
|
this._dbusProxy.connect('Changed', Lang.bind(this, this._onChanged));
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
DBus.session.watch_name('org.gnome.Shell.CalendarServer',
|
|
|
|
false, // do not launch a name-owner if none exists
|
|
|
|
Lang.bind(this, this._onNameAppeared),
|
|
|
|
Lang.bind(this, this._onNameVanished));
|
|
|
|
},
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_resetCache: function() {
|
|
|
|
this._events = [];
|
|
|
|
this._lastRequestBegin = null;
|
|
|
|
this._lastRequestEnd = null;
|
|
|
|
},
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_onNameAppeared: function(owner) {
|
|
|
|
this._resetCache();
|
|
|
|
this._loadEvents(true);
|
|
|
|
},
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_onNameVanished: function(oldOwner) {
|
|
|
|
this._resetCache();
|
|
|
|
this.emit('changed');
|
|
|
|
},
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_onChanged: function() {
|
|
|
|
this._loadEvents(false);
|
|
|
|
},
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_onEventsReceived: function(appointments) {
|
|
|
|
let newEvents = [];
|
|
|
|
if (appointments != null) {
|
|
|
|
for (let n = 0; n < appointments.length; n++) {
|
|
|
|
let a = appointments[n];
|
|
|
|
let date = new Date(a[4] * 1000);
|
|
|
|
let end = new Date(a[5] * 1000);
|
|
|
|
let summary = a[1];
|
|
|
|
let allDay = a[3];
|
|
|
|
let event = new CalendarEvent(date, end, summary, allDay);
|
|
|
|
newEvents.push(event);
|
|
|
|
}
|
|
|
|
newEvents.sort(function(event1, event2) {
|
|
|
|
return event1.date.getTime() - event2.date.getTime();
|
|
|
|
});
|
2011-01-28 21:35:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
this._events = newEvents;
|
|
|
|
this.emit('changed');
|
2011-01-28 21:35:46 +00:00
|
|
|
},
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_loadEvents: function(forceReload) {
|
|
|
|
if (this._curRequestBegin && this._curRequestEnd){
|
|
|
|
let callFlags = 0;
|
|
|
|
if (forceReload)
|
|
|
|
callFlags |= DBus.CALL_FLAG_START;
|
|
|
|
this._dbusProxy.GetEventsRemote(this._curRequestBegin.getTime() / 1000,
|
|
|
|
this._curRequestEnd.getTime() / 1000,
|
|
|
|
forceReload,
|
|
|
|
Lang.bind(this, this._onEventsReceived),
|
|
|
|
callFlags);
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
},
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
requestRange: function(begin, end, forceReload) {
|
|
|
|
if (forceReload || !(_datesEqual(begin, this._lastRequestBegin) && _datesEqual(end, this._lastRequestEnd))) {
|
|
|
|
this._lastRequestBegin = begin;
|
|
|
|
this._lastRequestEnd = end;
|
|
|
|
this._curRequestBegin = begin;
|
|
|
|
this._curRequestEnd = end;
|
|
|
|
this._loadEvents(forceReload);
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getEvents: function(begin, end) {
|
|
|
|
let result = [];
|
2011-02-25 22:42:25 +00:00
|
|
|
for(let n = 0; n < this._events.length; n++) {
|
|
|
|
let event = this._events[n];
|
|
|
|
if (_dateIntervalsOverlap (event.date, event.end, begin, end)) {
|
2011-01-28 21:35:46 +00:00
|
|
|
result.push(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasEvents: function(day) {
|
|
|
|
let dayBegin = _getBeginningOfDay(day);
|
|
|
|
let dayEnd = _getEndOfDay(day);
|
|
|
|
|
|
|
|
let events = this.getEvents(dayBegin, dayEnd);
|
|
|
|
|
|
|
|
if (events.length == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2011-02-25 22:42:25 +00:00
|
|
|
Signals.addSignalMethods(DBusEventSource.prototype);
|
2011-01-28 21:35:46 +00:00
|
|
|
|
|
|
|
// Calendar:
|
|
|
|
// @eventSource: is an object implementing the EventSource API, e.g. the
|
|
|
|
// requestRange(), getEvents(), hasEvents() methods and the ::changed signal.
|
|
|
|
function Calendar(eventSource) {
|
|
|
|
this._init(eventSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
Calendar.prototype = {
|
|
|
|
_init: function(eventSource) {
|
2011-08-22 18:21:51 +00:00
|
|
|
if (eventSource) {
|
|
|
|
this._eventSource = eventSource;
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-08-22 18:21:51 +00:00
|
|
|
this._eventSource.connect('changed', Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
this._update(false);
|
|
|
|
}));
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
|
2011-06-24 00:40:36 +00:00
|
|
|
this._weekStart = Shell.util_get_week_start();
|
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
|
|
|
|
2009-09-30 14:02:08 +00:00
|
|
|
// Find the ordering for month/year in the calendar heading
|
2011-01-28 21:35:46 +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-28 21:35:46 +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 ();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sets the calendar to show a specific date
|
2011-02-25 22:42:25 +00:00
|
|
|
setDate: function(date, forceReload) {
|
2011-01-28 21:35:46 +00:00
|
|
|
if (!_sameDay(date, this._selectedDate)) {
|
|
|
|
this._selectedDate = date;
|
2011-02-25 22:42:25 +00:00
|
|
|
this._update(forceReload);
|
2011-01-28 21:35:46 +00:00
|
|
|
this.emit('selected-date-changed', new Date(this._selectedDate));
|
2011-02-25 22:42:25 +00:00
|
|
|
} else {
|
|
|
|
if (forceReload)
|
|
|
|
this._update(forceReload);
|
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
|
|
|
|
2011-01-28 21:35:46 +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);
|
2011-01-28 21:35:46 +00:00
|
|
|
back.connect('clicked', Lang.bind(this, this._onPrevMonthButtonClicked));
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
this._monthLabel = new St.Label({style_class: 'calendar-month-label'});
|
|
|
|
this._topBox.add(this._monthLabel, { expand: true, x_fill: false, x_align: St.Align.MIDDLE });
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2011-01-28 21:35:46 +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);
|
2011-01-28 21:35:46 +00:00
|
|
|
forward.connect('clicked', Lang.bind(this, this._onNextMonthButtonClicked));
|
2009-09-30 14:02:08 +00:00
|
|
|
|
2011-01-28 21:35:46 +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-28 21:35:46 +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++) {
|
2011-01-28 21:35:46 +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
|
|
|
|
let customDayAbbrev = _getCalendarDayAbbreviation(iter.getDay());
|
|
|
|
let label = new St.Label({ style_class: 'calendar-day-base calendar-day-heading',
|
|
|
|
text: customDayAbbrev });
|
|
|
|
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,
|
2011-01-28 21:35:46 +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:
|
2011-01-28 21:35:46 +00:00
|
|
|
this._onPrevMonthButtonClicked();
|
2009-10-01 19:18:20 +00:00
|
|
|
break;
|
|
|
|
case Clutter.ScrollDirection.DOWN:
|
|
|
|
case Clutter.ScrollDirection.RIGHT:
|
2011-01-28 21:35:46 +00:00
|
|
|
this._onNextMonthButtonClicked();
|
2009-10-01 19:18:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
_onPrevMonthButtonClicked: function() {
|
|
|
|
let newDate = new Date(this._selectedDate);
|
2011-01-31 20:00:16 +00:00
|
|
|
let oldMonth = newDate.getMonth();
|
|
|
|
if (oldMonth == 0) {
|
2011-01-28 21:35:46 +00:00
|
|
|
newDate.setMonth(11);
|
|
|
|
newDate.setFullYear(newDate.getFullYear() - 1);
|
2011-01-31 20:00:16 +00:00
|
|
|
if (newDate.getMonth() != 11) {
|
|
|
|
let day = 32 - new Date(newDate.getFullYear() - 1, 11, 32).getDate();
|
|
|
|
newDate = new Date(newDate.getFullYear() - 1, 11, day);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newDate.setMonth(oldMonth - 1);
|
|
|
|
if (newDate.getMonth() != oldMonth - 1) {
|
|
|
|
let day = 32 - new Date(newDate.getFullYear(), oldMonth - 1, 32).getDate();
|
|
|
|
newDate = new Date(newDate.getFullYear(), oldMonth - 1, day);
|
|
|
|
}
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
2011-01-31 20:00:16 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
this.setDate(newDate, false);
|
2009-09-30 14:02:08 +00:00
|
|
|
},
|
|
|
|
|
2011-01-31 20:00:16 +00:00
|
|
|
_onNextMonthButtonClicked: function() {
|
2011-01-28 21:35:46 +00:00
|
|
|
let newDate = new Date(this._selectedDate);
|
2011-01-31 20:00:16 +00:00
|
|
|
let oldMonth = newDate.getMonth();
|
|
|
|
if (oldMonth == 11) {
|
2011-01-28 21:35:46 +00:00
|
|
|
newDate.setMonth(0);
|
|
|
|
newDate.setFullYear(newDate.getFullYear() + 1);
|
2011-01-31 20:00:16 +00:00
|
|
|
if (newDate.getMonth() != 0) {
|
|
|
|
let day = 32 - new Date(newDate.getFullYear() + 1, 0, 32).getDate();
|
|
|
|
newDate = new Date(newDate.getFullYear() + 1, 0, day);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newDate.setMonth(oldMonth + 1);
|
|
|
|
if (newDate.getMonth() != oldMonth + 1) {
|
|
|
|
let day = 32 - new Date(newDate.getFullYear(), oldMonth + 1, 32).getDate();
|
|
|
|
newDate = new Date(newDate.getFullYear(), oldMonth + 1, day);
|
|
|
|
}
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
2011-01-31 20:00:16 +00:00
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
this.setDate(newDate, false);
|
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();
|
2011-02-25 22:42:25 +00:00
|
|
|
this._update(false);
|
2010-05-27 07:31:46 +00:00
|
|
|
},
|
|
|
|
|
2011-02-25 22:42:25 +00:00
|
|
|
_update: function(forceReload) {
|
2011-01-28 21:35:46 +00:00
|
|
|
let now = new Date();
|
|
|
|
|
|
|
|
if (_sameYear(this._selectedDate, now))
|
|
|
|
this._monthLabel.text = this._selectedDate.toLocaleFormat(this._headerFormatWithoutYear);
|
|
|
|
else
|
|
|
|
this._monthLabel.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-28 21:35:46 +00:00
|
|
|
let beginDate = new Date(this._selectedDate);
|
|
|
|
beginDate.setDate(1);
|
|
|
|
beginDate.setSeconds(0);
|
|
|
|
beginDate.setHours(12);
|
|
|
|
let daysToWeekStart = (7 + beginDate.getDay() - this._weekStart) % 7;
|
|
|
|
beginDate.setTime(beginDate.getTime() - daysToWeekStart * MSECS_IN_DAY);
|
|
|
|
|
|
|
|
let iter = new Date(beginDate);
|
2009-09-30 14:02:08 +00:00
|
|
|
let row = 2;
|
|
|
|
while (true) {
|
2011-01-28 21:35:46 +00:00
|
|
|
let button = new St.Button({ label: iter.getDate().toString() });
|
|
|
|
|
2011-08-22 18:21:51 +00:00
|
|
|
if (!this._eventSource)
|
|
|
|
button.reactive = false;
|
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
let iterStr = iter.toUTCString();
|
|
|
|
button.connect('clicked', Lang.bind(this, function() {
|
|
|
|
let newlySelectedDate = new Date(iterStr);
|
2011-02-25 22:42:25 +00:00
|
|
|
this.setDate(newlySelectedDate, false);
|
2011-01-28 21:35:46 +00:00
|
|
|
}));
|
|
|
|
|
2011-08-22 18:21:51 +00:00
|
|
|
let hasEvents = this._eventSource && this._eventSource.hasEvents(iter);
|
2011-01-28 21:35:46 +00:00
|
|
|
let styleClass = 'calendar-day-base calendar-day';
|
|
|
|
if (_isWorkDay(iter))
|
|
|
|
styleClass += ' calendar-work-day'
|
2009-09-30 14:02:08 +00:00
|
|
|
else
|
2011-01-28 21:35:46 +00:00
|
|
|
styleClass += ' calendar-nonwork-day'
|
|
|
|
|
|
|
|
// Hack used in lieu of border-collapse - see gnome-shell.css
|
|
|
|
if (row == 2)
|
|
|
|
styleClass = 'calendar-day-top ' + styleClass;
|
2011-01-31 20:53:56 +00:00
|
|
|
if (iter.getDay() == this._weekStart)
|
2011-01-28 21:35:46 +00:00
|
|
|
styleClass = 'calendar-day-left ' + styleClass;
|
|
|
|
|
|
|
|
if (_sameDay(now, iter))
|
|
|
|
styleClass += ' calendar-today';
|
|
|
|
else if (iter.getMonth() != this._selectedDate.getMonth())
|
|
|
|
styleClass += ' calendar-other-month-day';
|
|
|
|
|
|
|
|
if (_sameDay(this._selectedDate, iter))
|
|
|
|
button.add_style_pseudo_class('active');
|
|
|
|
|
|
|
|
if (hasEvents)
|
|
|
|
styleClass += ' calendar-day-with-events'
|
|
|
|
|
|
|
|
button.style_class = styleClass;
|
2010-05-27 07:31:46 +00:00
|
|
|
|
|
|
|
let offsetCols = this._useWeekdate ? 1 : 0;
|
2011-01-28 21:35:46 +00:00
|
|
|
this.actor.add(button,
|
|
|
|
{ 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(),
|
2011-01-28 21:35:46 +00:00
|
|
|
style_class: 'calendar-day-base calendar-week-number'});
|
2010-05-27 07:31:46 +00:00
|
|
|
this.actor.add(label,
|
2011-01-28 21:35:46 +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-28 21:35:46 +00:00
|
|
|
if (iter.getMonth() > this._selectedDate.getMonth() || iter.getYear() > this._selectedDate.getYear())
|
2009-09-30 14:02:08 +00:00
|
|
|
break;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
2011-01-28 21:35:46 +00:00
|
|
|
// Signal to the event source that we are interested in events
|
|
|
|
// only from this date range
|
2011-08-22 18:21:51 +00:00
|
|
|
if (this._eventSource)
|
|
|
|
this._eventSource.requestRange(beginDate, iter, forceReload);
|
2011-01-28 21:35:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Signals.addSignalMethods(Calendar.prototype);
|
|
|
|
|
|
|
|
function EventsList(eventSource) {
|
|
|
|
this._init(eventSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventsList.prototype = {
|
|
|
|
_init: function(eventSource) {
|
|
|
|
this.actor = new St.BoxLayout({ vertical: true, style_class: 'events-header-vbox'});
|
|
|
|
this._date = new Date();
|
|
|
|
this._eventSource = eventSource;
|
|
|
|
this._eventSource.connect('changed', Lang.bind(this, this._update));
|
|
|
|
this._desktopSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
|
|
|
|
this._desktopSettings.connect('changed', Lang.bind(this, this._update));
|
2011-06-24 00:40:36 +00:00
|
|
|
this._weekStart = Shell.util_get_week_start();
|
2011-01-29 02:09:47 +00:00
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
|
|
|
_addEvent: function(dayNameBox, timeBox, eventTitleBox, includeDayName, day, time, desc) {
|
|
|
|
if (includeDayName) {
|
|
|
|
dayNameBox.add(new St.Label( { style_class: 'events-day-dayname',
|
|
|
|
text: day } ),
|
|
|
|
{ x_fill: true } );
|
|
|
|
}
|
|
|
|
timeBox.add(new St.Label( { style_class: 'events-day-time',
|
|
|
|
text: time} ),
|
|
|
|
{ x_fill: true } );
|
|
|
|
eventTitleBox.add(new St.Label( { style_class: 'events-day-task',
|
|
|
|
text: desc} ));
|
|
|
|
},
|
|
|
|
|
|
|
|
_addPeriod: function(header, begin, end, includeDayName, showNothingScheduled) {
|
2011-08-22 18:21:51 +00:00
|
|
|
if (!this._eventSource)
|
|
|
|
return;
|
|
|
|
|
2011-01-28 21:35:46 +00:00
|
|
|
let events = this._eventSource.getEvents(begin, end);
|
|
|
|
|
|
|
|
let clockFormat = this._desktopSettings.get_string(CLOCK_FORMAT_KEY);;
|
|
|
|
|
|
|
|
if (events.length == 0 && !showNothingScheduled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let vbox = new St.BoxLayout( {vertical: true} );
|
|
|
|
this.actor.add(vbox);
|
|
|
|
|
|
|
|
vbox.add(new St.Label({ style_class: 'events-day-header', text: header }));
|
|
|
|
let box = new St.BoxLayout({style_class: 'events-header-hbox'});
|
|
|
|
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' });
|
|
|
|
box.add(dayNameBox, {x_fill: false});
|
|
|
|
box.add(timeBox, {x_fill: false});
|
|
|
|
box.add(eventTitleBox, {expand: true});
|
|
|
|
vbox.add(box);
|
|
|
|
|
|
|
|
for (let n = 0; n < events.length; n++) {
|
|
|
|
let event = events[n];
|
|
|
|
let dayString = _getEventDayAbbreviation(event.date.getDay());
|
|
|
|
let timeString = _formatEventTime(event, clockFormat);
|
|
|
|
let summaryString = event.summary;
|
|
|
|
this._addEvent(dayNameBox, timeBox, eventTitleBox, includeDayName, dayString, timeString, summaryString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events.length == 0 && showNothingScheduled) {
|
|
|
|
let now = new Date();
|
|
|
|
/* Translators: Text to show if there are no events */
|
2011-02-25 22:42:25 +00:00
|
|
|
let nothingEvent = new CalendarEvent(now, now, _("Nothing Scheduled"), true);
|
2011-01-28 21:35:46 +00:00
|
|
|
let timeString = _formatEventTime(nothingEvent, clockFormat);
|
|
|
|
this._addEvent(dayNameBox, timeBox, eventTitleBox, false, "", timeString, nothingEvent.summary);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_showOtherDay: function(day) {
|
|
|
|
this.actor.destroy_children();
|
|
|
|
|
|
|
|
let dayBegin = _getBeginningOfDay(day);
|
|
|
|
let dayEnd = _getEndOfDay(day);
|
|
|
|
|
|
|
|
let dayString;
|
|
|
|
let now = new Date();
|
|
|
|
if (_sameYear(day, now))
|
2011-02-03 11:12:57 +00:00
|
|
|
/* Translators: Shown on calendar heading when selected day occurs on current year */
|
|
|
|
dayString = day.toLocaleFormat(C_("calendar heading", "%A, %B %d"));
|
2011-01-28 21:35:46 +00:00
|
|
|
else
|
2011-02-03 11:12:57 +00:00
|
|
|
/* Translators: Shown on calendar heading when selected day occurs on different year */
|
|
|
|
dayString = day.toLocaleFormat(C_("calendar heading", "%A, %B %d, %Y"));
|
2011-01-28 21:35:46 +00:00
|
|
|
this._addPeriod(dayString, dayBegin, dayEnd, false, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_showToday: function() {
|
|
|
|
this.actor.destroy_children();
|
|
|
|
|
|
|
|
let now = new Date();
|
|
|
|
let dayBegin = _getBeginningOfDay(now);
|
|
|
|
let dayEnd = _getEndOfDay(now);
|
|
|
|
this._addPeriod(_("Today"), dayBegin, dayEnd, false, true);
|
|
|
|
|
|
|
|
let tomorrowBegin = new Date(dayBegin.getTime() + 86400 * 1000);
|
|
|
|
let tomorrowEnd = new Date(dayEnd.getTime() + 86400 * 1000);
|
|
|
|
this._addPeriod(_("Tomorrow"), tomorrowBegin, tomorrowEnd, false, true);
|
|
|
|
|
2011-01-29 02:09:47 +00:00
|
|
|
if (dayEnd.getDay() <= 4 + this._weekStart) {
|
|
|
|
/* If now is within the first 5 days we show "This week" and
|
|
|
|
* include events up until and including Saturday/Sunday
|
|
|
|
* (depending on whether a week starts on Sunday/Monday).
|
2011-01-28 21:35:46 +00:00
|
|
|
*/
|
|
|
|
let thisWeekBegin = new Date(dayBegin.getTime() + 2 * 86400 * 1000);
|
2011-01-29 02:09:47 +00:00
|
|
|
let thisWeekEnd = new Date(dayEnd.getTime() + (6 + this._weekStart - dayEnd.getDay()) * 86400 * 1000);
|
2011-01-28 21:35:46 +00:00
|
|
|
this._addPeriod(_("This week"), thisWeekBegin, thisWeekEnd, true, false);
|
|
|
|
} else {
|
2011-01-29 02:09:47 +00:00
|
|
|
/* otherwise it's one of the two last days of the week ... show
|
|
|
|
* "Next week" and include events up until and including *next*
|
|
|
|
* Saturday/Sunday
|
2011-01-28 21:35:46 +00:00
|
|
|
*/
|
|
|
|
let nextWeekBegin = new Date(dayBegin.getTime() + 2 * 86400 * 1000);
|
2011-01-29 02:09:47 +00:00
|
|
|
let nextWeekEnd = new Date(dayEnd.getTime() + (13 + this._weekStart - dayEnd.getDay()) * 86400 * 1000);
|
2011-01-28 21:35:46 +00:00
|
|
|
this._addPeriod(_("Next week"), nextWeekBegin, nextWeekEnd, true, false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2009-09-30 14:02:08 +00:00
|
|
|
}
|
|
|
|
};
|