<?xml version="1.0" encoding="UTF-8"?>
<meta id="meta.quota" label="CONF_MESSAGE[User quota]" description="CONF_MESSAGE[Maintain the size of a given directory for each user]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="file:../core.ajaxplorer/ajxp_registry.xsd">
    <class_definition filename="plugins/meta.quota/class.QuotaComputer.php" classname="QuotaComputer"/>
    <server_settings>
        <param description="CONF_MESSAGE[Authorized quota. Use php.ini like values (20M, 2G), etc.]" label="CONF_MESSAGE[User Quota]" name="DEFAULT_QUOTA" type="string" inherit="true"/>
        <param description="CONF_MESSAGE[Store computed quota value in the user preferences, to avoid triggering computation each time it is requested. This may introduce some lag if the repository is shared by many users.]" label="CONF_MESSAGE[Cache value]" name="CACHE_QUOTA" type="boolean" default="false"/>
        <param description="Limit that will trigger an alert when approaching the end of the quota. Use a percent value" label="CONF_MESSAGE[Soft Limit (%)]"
               name="SOFT_QUOTA"
               type="integer"/>
        <param description="CONF_MESSAGE[If you want to define quotas for each user, define a custom field in the CUSTOM_DATA parameter of the conf plugin, and declare this field name here.]" label="CONF_MESSAGE[Custom Field (Deprecated)]" name="CUSTOM_DATA_FIELD" type="string"/>
        <global_param description="CONF_MESSAGE[Define if usage must be computed for this repository only, or cumulated on all the repositories of the user]" label="CONF_MESSAGE[Usage scope]" name="USAGE_SCOPE" type="select" choices="local|CONF_MESSAGE[Repository],global|CONF_MESSAGE[Cumulate repositories]" default="local"/>
    </server_settings>
    <client_settings>
        <resources>
            <img_library alias="meta_quota" path="plugins/meta.quota/img"/>
            <i18n namespace="meta.quota" path="plugins/meta.quota/i18n"/>
        </resources>
    </client_settings>
   	<registry_contributions>
   		<hooks>
   			<serverCallback hookName="node.change" methodName="recomputeQuotaUsage" defer="true"/>
   			<serverCallback hookName="node.before_change" methodName="precheckQuotaUsage"/>
   			<serverCallback hookName="node.before_path_change" methodName="precheckQuotaUsage"/>
   			<serverCallback hookName="node.before_create" methodName="precheckQuotaUsage"/>
            <serverCallback methodName="loadRepositoryInfo" hookName="repository.load_info"/>
   		</hooks>
        <actions>
            <action name="monitor_quota">
                <gui src="meta_quota/ICON_SIZE/progressbar.png" iconClass="icon-tasks" text="meta.quota.1" title="meta.quota.2" hasAccessKey="false">
                    <context dir="true" recycle="true" selection="false" actionBarGroup="utils" ajxpWidgets="UserWidget::logging_string"/>
                </gui>
                <rightsContext adminOnly="false" noUser="false" read="false" userLogged="true" write="false"/>
                <processing>
                    <clientCallback><![CDATA[
                        bufferCallback("ajxp_meta_quota_loader", 5, function(){
                            if(window.ajxp_meta_quota_loading) return;
                            var c = new Connexion();
                            c.setParameters(new Hash({get_action:'monitor_quota'}));
                            c.discrete = true;
                            c.onComplete = function(transport){
                                window.ajxp_meta_quota_loading = false;
                                var action = pydio.getController().getActionByName("monitor_quota");
                                if(!action) return;
                                var data = transport.responseJSON;
                                ajaxplorer.meta_quota_text = roundSize(data.USAGE, MessageHash["byte_unit_symbol"]) + "/" + roundSize(data.TOTAL, MessageHash["byte_unit_symbol"]);
                                action.options.text = ajaxplorer.meta_quota_text;
                                if($('ajxp_quota_panel_content')){
                                    $('ajxp_quota_panel_content').update(ajaxplorer.meta_quota_text);
                                }
                                action.refreshInstances();
                            };
                            window.ajxp_meta_quota_loading = true;
                            c.sendAsync();
                        });
                    ]]></clientCallback>
                    <clientListener name="contextChange"><![CDATA[
                        if(ajaxplorer && !ajaxplorer.meta_quota_pe_created){
                            var configs = ajaxplorer.getPluginConfigs("mq");
                            if(configs){
                                ajaxplorer.observe("server_message", function(event){
                                    var newValue = XPathSelectSingleNode(event, "/tree/metaquota");
                                    if(newValue){
                                        var action = pydio.getController().getActionByName("monitor_quota");
                                        if(!action) return;
                                        ajaxplorer.meta_quota_text = roundSize(newValue.getAttribute("usage"), MessageHash["byte_unit_symbol"]) + "/" + roundSize(newValue.getAttribute("total"), MessageHash["byte_unit_symbol"]);
                                        action.options.text = ajaxplorer.meta_quota_text;
                                        if($('ajxp_quota_panel_content')){
                                            $('ajxp_quota_panel_content').update(ajaxplorer.meta_quota_text);
                                        }
                                        action.refreshInstances();
                                    }
                                });
                                pydio.getController().fireAction('monitor_quota');
                            }else{
                                new PeriodicalExecuter(function(pe){
                                    var action = pydio.getController().getActionByName("monitor_quota");
                                    if(!action) {
                                        pe.stop();
                                        ajaxplorer.meta_quota_pe_created = false;
                                        return;
                                    }
                                    pydio.getController().fireAction('monitor_quota');
                                }, 20);
                            }
                            ajaxplorer.meta_quota_pe_created = true;
                        }
                        ]]></clientListener>
                    <serverCallback methodName="getCurrentQuota" restParams="/" developerComment="Get current quota usage information">
                        <output type="JSON" description="A Json structure containing usage and authorized quota"/>
                    </serverCallback>
                </processing>
            </action>
        </actions>
        <client_configs>
            <component_config className="InfoPanel">
                <infoPanelExtension mime="ajxp_root_node" attributes="" >
                    <messages>
                        <message key="quota_string" id="meta.quota.4"/>
                    </messages>
                    <html><![CDATA[
                        <div id="ajxp_quota_panel" data-infoPanelPosition="top">
                            <div class="panelHeader">#{quota_string}</div>
                            <div id="ajxp_quota_panel_content" style="padding:5px 0;font-size:20px;"></div>
                            <script>
                                if(pydio.getController().getActionByName("monitor_quota")){
                                    pydio.getController().fireAction('monitor_quota');
                                }
                            </script>
                        </div>
                    ]]></html>
                </infoPanelExtension>
            </component_config>
        </client_configs>
    </registry_contributions>
</meta>
