Fix handling of Web calendars

- handle Web calendars that require authentication;
- properly save the "reload on login" option;
- reload Web calendars when clicking on the reload button on top of the
  events/tasks list;
- properly activate the checkbox of new calendars (as they are enabled
  by default).

Fixes #3326
This commit is contained in:
Francis Lachapelle
2016-05-13 15:50:24 -04:00
parent 199a5f5215
commit e396e29430
11 changed files with 188 additions and 28 deletions
@@ -231,20 +231,48 @@
urls: { webCalendarURL: url }
});
var calendar = new Calendar(calendarData);
Calendar.$add(calendar);
Calendar.$$resource.fetch(calendar.id, 'reload').then(function(data) {
// TODO: show a toast of the reload status
Calendar.$log.debug(JSON.stringify(data, undefined, 2));
Calendar.$add(calendar);
d.resolve();
}, function(response) {
if (response.status == 401) {
// Web calendar requires authentication
d.resolve(calendar);
}
else {
d.reject();
}
});
d.resolve();
}, function() {
d.reject();
});
}, d.reject);
}
return d.promise;
};
/**
* @function reloadWebCalendars
* @memberof Calendar
* @desc Reload all Web calendars
* @return a promise combining the results of all HTTP operations
*/
Calendar.reloadWebCalendars = function() {
var promises = [];
_.forEach(this.$webcalendars, function(calendar) {
var promise = Calendar.$$resource.fetch(calendar.id, 'reload');
promise.then(function(data) {
calendar.$error = false;
}, function(response) {
calendar.$error = l(response.statusText);
});
promises.push(promise);
});
return Calendar.$q.all(promises);
};
/**
* @function $deleteComponents
* @memberof Calendar
@@ -292,6 +320,7 @@
*/
Calendar.prototype.init = function(data) {
this.color = this.color || '#AAAAAA';
this.active = 1;
angular.extend(this, data);
if (this.id) {
this.$acl = new Calendar.$$Acl('Calendar/' + this.id);
@@ -438,6 +467,34 @@
});
};
/**
* @function setCredentials
* @memberof Calendar.prototype
* @desc Set the credentials for a Web calendar that requires authentication
* @returns a promise of the HTTP operation
*/
Calendar.prototype.setCredentials = function(username, password) {
var _this = this,
d = Calendar.$q.defer();
Calendar.$$resource.post(this.id, 'set-credentials', { username: username, password: password }).then(function() {
Calendar.$$resource.fetch(_this.id, 'reload').then(function(data) {
Calendar.$add(_this);
d.resolve();
}, function(response) {
if (response.status == 401) {
// Authentication failed
d.reject(l('Wrong username or password'));
}
else {
d.reject(response.statusText);
}
});
}, d.reject);
return d.promise;
};
/**
* @function export
* @memberof Calendar.prototype
@@ -309,7 +309,9 @@
}
function reload() {
$rootScope.$emit('calendars:list');
Calendar.reloadWebCalendars().finally(function() {
$rootScope.$emit('calendars:list');
});
}
function cancelSearch() {
@@ -127,8 +127,48 @@
function addWebCalendar() {
Dialog.prompt(l('Subscribe to a web calendar...'), l('URL of the Calendar'), {inputType: 'url'})
.then(function(url) {
Calendar.$addWebCalendar(url);
Calendar.$addWebCalendar(url).then(function(calendar) {
if (angular.isObject(calendar)) {
// Web calendar requires HTTP authentication
$mdDialog.show({
parent: angular.element(document.body),
clickOutsideToClose: true,
escapeToClose: true,
templateUrl: 'UIxWebCalendarAuthDialog',
controller: WebCalendarAuthDialogController,
controllerAs: '$WebCalendarAuthDialogController',
locals: {
url: url,
calendar: calendar
}
});
}
});
});
/**
* @ngInject
*/
WebCalendarAuthDialogController.$inject = ['scope', '$mdDialog', 'url', 'calendar'];
function WebCalendarAuthDialogController(scope, $mdDialog, url, calendar) {
var vm = this,
parts = url.split("/"),
hostname = parts[2];
vm.title = l("Please identify yourself to %{0}").formatted(hostname);
vm.authenticate = function(form) {
if (form.$valid || !form.$error.required) {
calendar.setCredentials(vm.username, vm.password).then(function(message) {
$mdDialog.hide();
}, function(reason) {
form.password.$setValidity('credentials', false);
});
}
};
vm.cancel = function() {
$mdDialog.cancel();
};
}
}
function confirmDelete(folder) {