contactimporter/js/plugin.contactimporter.js

309 lines
9.6 KiB
JavaScript
Raw Normal View History

/**
* plugin.contactimporter.js zarafa contactimporter
*
* Author: Christoph Haas <christoph.h@sprinternet.at>
2016-06-13 22:59:05 +02:00
* Copyright (C) 2012-2016 Christoph Haas
*
* 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-13 22:59:05 +02:00
Ext.namespace("Zarafa.plugins.contactimporter"); // Assign the right namespace
Zarafa.plugins.contactimporter.ImportPlugin = Ext.extend(Zarafa.core.Plugin, { // create new import plugin
2016-06-13 22:59:05 +02:00
/**
* @constructor
* @param {Object} config Configuration object
*
*/
constructor: function (config) {
config = config || {};
2016-06-13 22:59:05 +02:00
Zarafa.plugins.contactimporter.ImportPlugin.superclass.constructor.call(this, config);
},
2016-06-13 22:59:05 +02:00
/**
* initialises insertion point for plugin
* @protected
*/
2016-06-13 22:59:05 +02:00
initPlugin: function () {
Zarafa.plugins.contactimporter.ImportPlugin.superclass.initPlugin.apply(this, arguments);
2016-06-13 22:59:05 +02:00
/* our panel */
Zarafa.core.data.SharedComponentType.addProperty('plugins.contactimporter.dialogs.importcontacts');
2016-06-13 22:59:05 +02:00
/* directly import received vcfs */
2016-06-13 22:59:05 +02:00
this.registerInsertionPoint('common.contextmenu.attachment.actions', this.createAttachmentImportButton, this);
2016-06-13 10:18:47 +02:00
2016-06-13 15:51:19 +02:00
/* export a contact via rightclick */
this.registerInsertionPoint('context.contact.contextmenu.actions', this.createItemExportInsertionPoint, this);
},
2016-06-13 22:59:05 +02:00
2016-06-13 15:51:19 +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 {
2016-06-14 01:09:52 +02:00
text : dgettext('plugin_files', 'Export vCard'),
2016-06-13 15:51:19 +02:00
handler: this.exportToVCF.createDelegate(this, [btn]),
scope : this,
iconCls: 'icon_contactimporter_export'
};
},
2016-06-19 00:16:25 +02:00
/**
* Generates a request to download the selected records as vCard.
* @param {Ext.Button} btn
*/
2016-06-13 22:59:05 +02:00
exportToVCF: function (btn) {
if (btn.records.length == 0) {
2016-06-13 15:51:19 +02:00
return; // skip if no records where given!
}
var recordIds = [];
2016-06-13 22:59:05 +02:00
for (var i = 0; i < btn.records.length; i++) {
2016-06-13 15:51:19 +02:00
recordIds.push(btn.records[i].get("entryid"));
}
var responseHandler = new Zarafa.plugins.contactimporter.data.ResponseHandler({
successCallback: this.downloadVCF,
2016-06-13 22:59:05 +02:00
scope : this
2016-06-13 15:51:19 +02:00
});
// request attachment preperation
container.getRequest().singleRequest(
'contactmodule',
'export',
{
2016-06-13 22:59:05 +02:00
storeid: btn.records[0].get("store_entryid"),
2016-06-13 15:51:19 +02:00
records: recordIds
},
responseHandler
);
},
2016-06-19 00:16:25 +02:00
/**
* Callback for the export request.
* @param {Object} response
*/
2016-06-13 22:59:05 +02:00
downloadVCF: function (response) {
2016-06-19 00:16:25 +02:00
if (response.status == false) {
2016-06-14 00:21:04 +02:00
Zarafa.common.dialogs.MessageBox.show({
title : dgettext('plugin_files', 'Warning'),
msg : dgettext('plugin_files', response.message),
icon : Zarafa.common.dialogs.MessageBox.WARNING,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
} else {
var downloadFrame = Ext.getBody().createChild({
tag: 'iframe',
cls: 'x-hidden'
});
2016-06-13 15:51:19 +02:00
2016-06-14 00:21:04 +02:00
var url = document.URL;
var link = url.substring(0, url.lastIndexOf('/') + 1);
2016-06-13 15:51:19 +02:00
2016-06-14 00:21:04 +02:00
link += "index.php?sessionid=" + container.getUser().getSessionId() + "&load=custom&name=download_vcf";
link = Ext.urlAppend(link, "token=" + encodeURIComponent(response.download_token));
link = Ext.urlAppend(link, "filename=" + encodeURIComponent(response.filename));
2016-06-13 15:51:19 +02:00
2016-06-14 00:21:04 +02:00
downloadFrame.dom.contentWindow.location = link;
}
2016-06-13 15:51:19 +02:00
},
2016-06-13 22:59:05 +02:00
/**
* Insert import button in all attachment suggestions
2016-06-13 22:59:05 +02:00
* @return {Object} Configuration object for a {@link Ext.Button button}
*/
2016-06-13 22:59:05 +02:00
createAttachmentImportButton: function (include, btn) {
return {
2016-06-14 01:09:52 +02:00
text : _('Import to Contacts'),
2016-06-13 22:59:05 +02:00
handler : this.getAttachmentFileName.createDelegate(this, [btn]),
scope : this,
iconCls : 'icon_contactimporter_button',
beforeShow: function (item, record) {
var extension = record.data.name.split('.').pop().toLowerCase();
2016-06-13 22:59:05 +02:00
if (record.data.filetype == "text/vcard" || record.data.filetype == "text/x-vcard" || extension == "vcf" || extension == "vcard") {
2016-06-13 10:18:47 +02:00
item.setVisible(true);
2016-06-13 13:41:52 +02:00
} else {
item.setVisible(false);
}
}
};
},
2016-06-13 22:59:05 +02:00
/**
* Callback for getAttachmentFileName
2016-06-19 00:16:25 +02:00
* @param {Object} response
*/
2016-06-13 22:59:05 +02:00
gotAttachmentFileName: function (response) {
if (response.status == true) {
2016-06-13 10:18:47 +02:00
this.openImportDialog(response.tmpname);
} else {
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _(response["message"]),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
}
},
/**
* Clickhandler for the button
2016-06-19 00:16:25 +02:00
* @param {Ext.Button} btn
*/
2016-06-13 13:41:52 +02:00
getAttachmentFileName: function (btn) {
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : 'Please wait',
msg : 'Loading attachment...',
progressText: 'Initializing...',
2016-06-13 22:59:05 +02:00
width : 300,
progress : true,
closable : false
});
// progress bar... ;)
2016-06-13 22:59:05 +02:00
var f = function (v) {
return function () {
if (v == 100) {
Zarafa.common.dialogs.MessageBox.hide();
2016-06-13 22:59:05 +02:00
} else {
Zarafa.common.dialogs.MessageBox.updateProgress(v / 100, Math.round(v) + '% loaded');
}
2016-06-13 22:59:05 +02:00
};
};
2016-06-13 22:59:05 +02:00
for (var i = 1; i < 101; i++) {
setTimeout(f(i), 20 * i);
}
2016-06-13 22:59:05 +02:00
/* store the attachment to a temporary folder and prepare it for uploading */
var attachmentRecord = btn.records;
var attachmentStore = attachmentRecord.store;
2016-06-13 22:59:05 +02:00
var store = attachmentStore.getParentRecord().get('store_entryid');
var entryid = attachmentStore.getAttachmentParentRecordEntryId();
var attachNum = new Array(1);
2016-06-13 22:59:05 +02:00
if (attachmentRecord.get('attach_num') != -1) {
attachNum[0] = attachmentRecord.get('attach_num');
2016-06-13 22:59:05 +02:00
} else {
attachNum[0] = attachmentRecord.get('tmpname');
2016-06-13 22:59:05 +02:00
}
var dialog_attachments = attachmentStore.getId();
var filename = attachmentRecord.data.name;
2016-06-13 22:59:05 +02:00
var responseHandler = new Zarafa.plugins.contactimporter.data.ResponseHandler({
2016-06-13 13:41:52 +02:00
successCallback: this.gotAttachmentFileName.createDelegate(this),
2016-06-13 22:59:05 +02:00
scope : this
});
2016-06-13 22:59:05 +02:00
// request attachment preperation
container.getRequest().singleRequest(
'contactmodule',
'importattachment',
{
2016-06-13 22:59:05 +02:00
entryid : entryid,
store : store,
attachNum : attachNum,
dialog_attachments: dialog_attachments,
2016-06-13 22:59:05 +02:00
filename : filename
},
responseHandler
);
},
2016-06-13 22:59:05 +02:00
2016-06-13 10:18:47 +02:00
/**
* Open the import dialog.
* @param {String} filename
2016-06-13 22:59:05 +02:00
*/
openImportDialog: function (filename) {
2016-06-13 10:18:47 +02:00
var componentType = Zarafa.core.data.SharedComponentType['plugins.contactimporter.dialogs.importcontacts'];
var config = {
2016-06-14 00:57:00 +02:00
filename: filename,
2016-06-19 00:16:25 +02:00
modal : true
2016-06-13 10:18:47 +02:00
};
Zarafa.core.data.UIFactory.openLayerComponent(componentType, undefined, config);
},
/**
* Bid for the type of shared component
* and the given record.
* This will bid on calendar.dialogs.importcontacts
* @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-13 22:59:05 +02:00
bidSharedComponent: function (type, record) {
var bid = -1;
2016-06-13 22:59:05 +02:00
switch (type) {
case Zarafa.core.data.SharedComponentType['plugins.contactimporter.dialogs.importcontacts']:
2016-06-13 10:18:47 +02:00
bid = 1;
break;
2016-06-14 00:21:04 +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.Contact") {
2016-06-14 00:21:04 +02:00
bid = 2;
}
}
break;
}
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-13 22:59:05 +02:00
getSharedComponent: function (type, record) {
var component;
2016-06-13 22:59:05 +02:00
switch (type) {
case Zarafa.core.data.SharedComponentType['plugins.contactimporter.dialogs.importcontacts']:
component = Zarafa.plugins.contactimporter.dialogs.ImportContentPanel;
break;
2016-06-14 00:21:04 +02:00
case Zarafa.core.data.SharedComponentType['common.contextmenu']:
component = Zarafa.plugins.contactimporter.ui.ContextMenu;
break;
}
return component;
}
});
/*############################################################################################################################
* STARTUP
*############################################################################################################################*/
2016-06-13 22:59:05 +02:00
Zarafa.onReady(function () {
container.registerPlugin(new Zarafa.core.PluginMetaData({
2016-06-13 22:59:05 +02:00
name : 'contactimporter',
displayName : _('Contactimporter Plugin'),
about : Zarafa.plugins.contactimporter.ABOUT,
pluginConstructor: Zarafa.plugins.contactimporter.ImportPlugin
}));
});