calendarimporter/js/plugin.calendarimporter.js

320 lines
10 KiB
JavaScript
Raw Normal View History

/**
2013-03-30 14:55:18 +01:00
* plugin.calendarimporter.js zarafa calender to ics im/exporter
*
2013-03-30 14:55:18 +01:00
* Author: Christoph Haas <christoph.h@sprinternet.at>
2016-06-20 15:04:53 +02:00
* Copyright (C) 2012-2016 Christoph Haas
2013-03-30 14:55:18 +01:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
Ext.namespace("Zarafa.plugins.calendarimporter"); // Assign the right namespace
2012-11-11 19:18:23 +01:00
Zarafa.plugins.calendarimporter.ImportPlugin = Ext.extend(Zarafa.core.Plugin, { // create new import plugin
2016-06-20 15:48:24 +02:00
/**
* @constructor
* @param {Object} config Configuration object
*
*/
2012-11-11 19:18:23 +01:00
constructor: function (config) {
config = config || {};
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
Zarafa.plugins.calendarimporter.ImportPlugin.superclass.constructor.call(this, config);
2012-11-11 19:18:23 +01:00
},
2016-06-20 15:48:24 +02:00
2012-11-11 19:18:23 +01:00
/**
* initialises insertion point for plugin
* @protected
*/
2016-06-20 15:48:24 +02:00
initPlugin: function () {
2013-03-30 14:55:18 +01:00
Zarafa.plugins.calendarimporter.ImportPlugin.superclass.initPlugin.apply(this, arguments);
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
/* our panel */
Zarafa.core.data.SharedComponentType.addProperty('plugins.calendarimporter.dialogs.importevents');
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
/* directly import received icals */
this.registerInsertionPoint('common.contextmenu.attachment.actions', this.createAttachmentImportButton);
2016-06-20 15:48:24 +02:00
/* add settings widget */
this.registerInsertionPoint('context.settings.category.calendar', this.createSettingsWidget);
2016-06-20 15:48:24 +02:00
/* export a calendar entry via rightclick */
this.registerInsertionPoint('context.calendar.contextmenu.actions', this.createItemExportInsertionPoint, this);
2013-08-25 15:47:49 +02:00
/* ical sync stuff */
2016-06-20 15:48:24 +02:00
if (container.getSettingsModel().get("zarafa/v1/plugins/calendarimporter/enable_sync") === true) {
2013-08-25 15:47:49 +02:00
/* edit panel */
Zarafa.core.data.SharedComponentType.addProperty('plugins.calendarimporter.settings.dialogs.calsyncedit');
2016-06-20 15:48:24 +02:00
2013-08-25 15:47:49 +02:00
/* enable the settings widget */
this.registerInsertionPoint('context.settings.category.calendar', this.createSettingsCalSyncWidget);
2013-08-25 15:47:49 +02:00
}
2012-11-11 19:18:23 +01:00
},
2016-06-20 15:48:24 +02:00
/**
* This method hooks to the contact context menu and allows users to export users to vcf.
*
* @param include
* @param btn
* @returns {Object}
*/
createItemExportInsertionPoint: function (include, btn) {
return {
text : dgettext('plugin_files', 'Export Event'),
handler: this.exportToICS.createDelegate(this, [btn]),
scope : this,
iconCls: 'icon_calendarimporter_export'
};
2016-06-20 15:48:24 +02:00
},
/**
* Generates a request to download the selected records as vCard.
* @param {Ext.Button} btn
*/
exportToICS: function (btn) {
if (btn.records.length == 0) {
return; // skip if no records where given!
2012-11-11 19:18:23 +01:00
}
2016-06-20 15:48:24 +02:00
var recordIds = [];
for (var i = 0; i < btn.records.length; i++) {
recordIds.push(btn.records[i].get("entryid"));
}
2016-10-31 23:01:52 +01:00
var responseHandler = new Zarafa.plugins.calendarimporter.data.ResponseHandler({
2016-06-20 15:48:24 +02:00
successCallback: Zarafa.plugins.calendarimporter.data.Actions.downloadICS,
scope : this
});
// request attachment preperation
container.getRequest().singleRequest(
'calendarmodule',
'export',
{
storeid: btn.records[0].get("store_entryid"),
records: recordIds
},
responseHandler
);
2012-11-11 19:18:23 +01:00
},
2016-06-20 15:48:24 +02:00
2013-08-25 15:47:49 +02:00
/**
2016-06-20 15:48:24 +02:00
* Creates the button
*
* @return {Object} Configuration object for a {@link Ext.Button button}
*
*/
2013-08-25 15:47:49 +02:00
createSettingsWidget: function () {
return [{
2016-06-20 15:48:24 +02:00
xtype: 'calendarimporter.settingswidget'
}];
},
2016-06-20 15:48:24 +02:00
/**
2016-06-20 15:48:24 +02:00
* Creates the button
*
* @return {Object} Configuration object for a {@link Ext.Button button}
*
*/
createSettingsCalSyncWidget: function () {
2013-08-25 15:47:49 +02:00
return [{
2016-06-20 15:48:24 +02:00
xtype: 'calendarimporter.settingscalsyncwidget'
2013-08-25 15:47:49 +02:00
}];
},
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
/**
* Insert import button in all attachment suggestions
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
* @return {Object} Configuration object for a {@link Ext.Button button}
*/
2016-06-20 15:48:24 +02:00
createAttachmentImportButton: function (include, btn) {
2013-03-30 14:55:18 +01:00
return {
2016-06-20 15:48:24 +02:00
text : _('Import to Calendar'),
handler : this.getAttachmentFileName.createDelegate(this, [btn]),
scope : this,
iconCls : 'icon_calendarimporter_button',
beforeShow: function (item, record) {
2013-03-30 14:55:18 +01:00
var extension = record.data.name.split('.').pop().toLowerCase();
2016-06-20 15:48:24 +02:00
if (record.data.filetype == "text/calendar" || extension == "ics" || extension == "ifb" || extension == "ical" || extension == "ifbf") {
item.setVisible(true);
2013-03-30 14:55:18 +01:00
} else {
2016-06-20 15:48:24 +02:00
item.setVisible(false);
2013-03-30 14:55:18 +01:00
}
}
};
},
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
/**
* Callback for getAttachmentFileName
*/
2016-06-20 15:48:24 +02:00
gotAttachmentFileName: function (response) {
if (response.status == true) {
2016-10-31 23:01:52 +01:00
this.scope.openImportDialog(response.tmpname);
2013-03-30 14:55:18 +01:00
} else {
Zarafa.common.dialogs.MessageBox.show({
2016-06-20 15:48:24 +02:00
title : _('Error'),
msg : _(response["message"]),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
2013-03-30 14:55:18 +01:00
});
}
},
/**
* Clickhandler for the button
*/
getAttachmentFileName: function (btn, callback) {
Zarafa.common.dialogs.MessageBox.show({
2016-06-20 15:48:24 +02:00
title : 'Please wait',
msg : 'Loading attachment...',
2013-03-30 14:55:18 +01:00
progressText: 'Initializing...',
2016-06-20 15:48:24 +02:00
width : 300,
progress : true,
closable : false
2013-03-30 14:55:18 +01:00
});
// progress bar... ;)
2016-06-20 15:48:24 +02:00
var f = function (v) {
return function () {
if (v == 100) {
2013-03-30 14:55:18 +01:00
Zarafa.common.dialogs.MessageBox.hide();
2016-06-20 15:48:24 +02:00
} else {
Zarafa.common.dialogs.MessageBox.updateProgress(v / 100, Math.round(v) + '% loaded');
2013-03-30 14:55:18 +01:00
}
2016-06-20 15:48:24 +02:00
};
2013-03-30 14:55:18 +01:00
};
2016-06-20 15:48:24 +02:00
for (var i = 1; i < 101; i++) {
setTimeout(f(i), 20 * i);
2013-03-30 14:55:18 +01:00
}
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
/* store the attachment to a temporary folder and prepare it for uploading */
var attachmentRecord = btn.records;
var attachmentStore = attachmentRecord.store;
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
var store = attachmentStore.getParentRecord().get('store_entryid');
var entryid = attachmentStore.getAttachmentParentRecordEntryId();
var attachNum = new Array(1);
2016-06-20 15:48:24 +02:00
if (attachmentRecord.get('attach_num') != -1) {
2013-03-30 14:55:18 +01:00
attachNum[0] = attachmentRecord.get('attach_num');
2016-06-20 15:48:24 +02:00
} else {
2013-03-30 14:55:18 +01:00
attachNum[0] = attachmentRecord.get('tmpname');
2016-06-20 15:48:24 +02:00
}
2013-03-30 14:55:18 +01:00
var dialog_attachments = attachmentStore.getId();
var filename = attachmentRecord.data.name;
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
var responseHandler = new Zarafa.plugins.calendarimporter.data.ResponseHandler({
2016-06-20 15:48:24 +02:00
successCallback: this.gotAttachmentFileName,
scope : this
2013-03-30 14:55:18 +01:00
});
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
// request attachment preperation
container.getRequest().singleRequest(
'calendarmodule',
2016-06-20 15:48:24 +02:00
'importattachment',
2013-03-30 14:55:18 +01:00
{
2016-06-20 15:48:24 +02:00
entryid : entryid,
store : store,
attachNum : attachNum,
2013-03-30 14:55:18 +01:00
dialog_attachments: dialog_attachments,
2016-06-20 15:48:24 +02:00
filename : filename
2013-03-30 14:55:18 +01:00
},
responseHandler
);
},
2016-06-20 15:48:24 +02:00
2012-11-11 19:18:23 +01:00
/**
2016-06-20 15:48:24 +02:00
* Open the import dialog.
* @param {String} filename
2012-11-11 19:18:23 +01:00
*/
2016-06-20 15:48:24 +02:00
openImportDialog: function (filename) {
var componentType = Zarafa.core.data.SharedComponentType['plugins.calendarimporter.dialogs.importevents'];
var config = {
filename: filename,
modal : true
};
Zarafa.core.data.UIFactory.openLayerComponent(componentType, undefined, config);
2012-11-11 19:18:23 +01:00
},
2016-06-20 15:48:24 +02:00
2012-11-11 19:18:23 +01:00
/**
* Bid for the type of shared component
* and the given record.
* This will bid on calendar.dialogs.importevents
* @param {Zarafa.core.data.SharedComponentType} type Type of component a context can bid for.
* @param {Ext.data.Record} record Optionally passed record.
* @return {Number} The bid for the shared component
*/
2016-06-20 15:48:24 +02:00
bidSharedComponent: function (type, record) {
2012-11-11 19:18:23 +01:00
var bid = -1;
2016-06-20 15:48:24 +02:00
switch (type) {
2012-11-11 19:18:23 +01:00
case Zarafa.core.data.SharedComponentType['plugins.calendarimporter.dialogs.importevents']:
bid = 2;
break;
2013-08-25 15:47:49 +02:00
case Zarafa.core.data.SharedComponentType['plugins.calendarimporter.settings.dialogs.calsyncedit']:
bid = 2;
break;
2016-06-20 15:48:24 +02:00
case Zarafa.core.data.SharedComponentType['common.contextmenu']:
if (record instanceof Zarafa.core.data.MAPIRecord) {
if (record.get('object_type') == Zarafa.core.mapi.ObjectType.MAPI_FOLDER && record.get('container_class') == "IPF.Appointment") {
bid = 2;
}
}
break;
2012-11-11 19:18:23 +01:00
}
return bid;
},
/**
* Will return the reference to the shared component.
* Based on the type of component requested a component is returned.
* @param {Zarafa.core.data.SharedComponentType} type Type of component a context can bid for.
* @param {Ext.data.Record} record Optionally passed record.
* @return {Ext.Component} Component
*/
2016-06-20 15:48:24 +02:00
getSharedComponent: function (type, record) {
2012-11-11 19:18:23 +01:00
var component;
2016-06-20 15:48:24 +02:00
switch (type) {
2012-11-11 19:18:23 +01:00
case Zarafa.core.data.SharedComponentType['plugins.calendarimporter.dialogs.importevents']:
component = Zarafa.plugins.calendarimporter.dialogs.ImportContentPanel;
break;
2013-08-25 15:47:49 +02:00
case Zarafa.core.data.SharedComponentType['plugins.calendarimporter.settings.dialogs.calsyncedit']:
component = Zarafa.plugins.calendarimporter.settings.dialogs.CalSyncEditContentPanel;
break;
2016-06-20 15:48:24 +02:00
case Zarafa.core.data.SharedComponentType['common.contextmenu']:
component = Zarafa.plugins.calendarimporter.ui.ContextMenu;
break;
2012-11-11 19:18:23 +01:00
}
return component;
}
});
2013-08-25 15:47:49 +02:00
/*############################################################################################################################*
2012-11-11 19:18:23 +01:00
* STARTUP
*############################################################################################################################*/
2016-06-20 15:48:24 +02:00
Zarafa.onReady(function () {
container.registerPlugin(new Zarafa.core.PluginMetaData({
2016-06-20 15:48:24 +02:00
name : 'calendarimporter',
displayName : _('Calendarimporter Plugin'),
about : Zarafa.plugins.calendarimporter.ABOUT,
pluginConstructor: Zarafa.plugins.calendarimporter.ImportPlugin
}));
2012-11-11 19:18:23 +01:00
});