Calendar: clean up code by always having an event source

Instead of sometimes having an event source and sometimes not, use
the empty event source when the session mode says the calendar is
disabled. This way, the code can assume an event source object and
avoid checks.

https://bugzilla.gnome.org/show_bug.cgi?id=641383
This commit is contained in:
Giovanni Campagna 2013-03-04 17:04:28 +01:00
parent 443fe813c3
commit beb0fdf4b8
2 changed files with 23 additions and 29 deletions

View File

@ -169,6 +169,10 @@ const EmptyEventSource = new Lang.Class({
_init: function() {
this.isLoading = false;
this.isDummy = true;
},
destroy: function() {
},
requestRange: function(begin, end) {
@ -231,6 +235,7 @@ const DBusEventSource = new Lang.Class({
_init: function() {
this._resetCache();
this.isLoading = false;
this.isDummy = false;
this._initialized = false;
this._dbusProxy = new CalendarServer();
@ -256,6 +261,10 @@ const DBusEventSource = new Lang.Class({
}));
},
destroy: function() {
this._dbusProxy.run_dispose();
},
_resetCache: function() {
this._events = [];
this._lastRequestBegin = null;
@ -393,19 +402,11 @@ const Calendar = new Lang.Class({
// @eventSource: is an object implementing the EventSource API, e.g. the
// requestRange(), getEvents(), hasEvents() methods and the ::changed signal.
setEventSource: function(eventSource) {
if (this._eventSource) {
this._eventSource.disconnect(this._eventSourceChangedId);
this._eventSource = null;
}
this._eventSource = eventSource;
if (this._eventSource) {
this._eventSourceChangedId = this._eventSource.connect('changed', Lang.bind(this, function() {
this._update(false);
}));
this._update(true);
}
this._eventSource.connect('changed', Lang.bind(this, function() {
this._update(false);
}));
this._update(true);
},
// Sets the calendar to show a specific date
@ -579,7 +580,7 @@ const Calendar = new Lang.Class({
let button = new St.Button({ label: iter.getDate().toString() });
let rtl = button.get_text_direction() == Clutter.TextDirection.RTL;
if (!this._eventSource)
if (this._eventSource.isDummy)
button.reactive = false;
let iterStr = iter.toUTCString();
@ -588,7 +589,7 @@ const Calendar = new Lang.Class({
this.setDate(newlySelectedDate, false);
}));
let hasEvents = this._eventSource && this._eventSource.hasEvents(iter);
let hasEvents = this._eventSource.hasEvents(iter);
let styleClass = 'calendar-day-base calendar-day';
if (_isWorkDay(iter))
@ -636,8 +637,7 @@ const Calendar = new Lang.Class({
}
// Signal to the event source that we are interested in events
// only from this date range
if (this._eventSource)
this._eventSource.requestRange(beginDate, iter, forceReload);
this._eventSource.requestRange(beginDate, iter, forceReload);
}
});
@ -655,16 +655,8 @@ const EventsList = new Lang.Class({
},
setEventSource: function(eventSource) {
if (this._eventSource) {
this._eventSource.disconnect(this._eventSourceChangedId);
this._eventSource = null;
}
this._eventSource = eventSource;
if (this._eventSource) {
this._eventSourceChangedId = this._eventSource.connect('changed', Lang.bind(this, this._update));
}
this._eventSource.connect('changed', Lang.bind(this, this._update));
},
_addEvent: function(dayNameBox, timeBox, eventTitleBox, includeDayName, day, time, desc) {
@ -681,9 +673,6 @@ const EventsList = new Lang.Class({
},
_addPeriod: function(header, begin, end, includeDayName, showNothingScheduled) {
if (!this._eventSource)
return;
let events = this._eventSource.getEvents(begin, end);
let clockFormat = this._desktopSettings.get_string(CLOCK_FORMAT_KEY);;

View File

@ -177,8 +177,13 @@ const DateMenuButton = new Lang.Class({
},
_setEventSource: function(eventSource) {
if (this._eventSource)
this._eventSource.destroy();
this._calendar.setEventSource(eventSource);
this._eventList.setEventSource(eventSource);
this._eventSource = eventSource;
},
_sessionUpdated: function() {
@ -187,7 +192,7 @@ const DateMenuButton = new Lang.Class({
if (showEvents) {
eventSource = new Calendar.DBusEventSource();
} else {
eventSource = null;
eventSource = new Calendar.EmptyEventSource();
}
this._setEventSource(eventSource);
this._setEventsVisibility(showEvents);