<commit_before>/*
 * The main purpose of this is to allow to work on other windows besides compose, like
 * conversations compose extension.
 * 
 * 
 * Interface of any compose_window implementation:
 *   
 *   // type: can be "TO", "CC" or "BCC"
 *   // returns an array with the recipients
 *   recipients( type )
 *   
 *   // It registers the listeners for:
 *   //
 *   //    start and stop, when windows loses focus, etc.
 *   //    
 *   //    for dictionary change
 *   //    
 *   //    for recipients changed
 *   //
 *   setListeners( ad_object )
 *   
 *   // Shows a message to the user by some seconds.
 *   setLabel( message )
 **/

/**
 *  params are:
 *    ad: automatic_dictionary instance
 *    name: plugin name
 *    logo_url: url of the plugin logo
 *    notification_time: time to show notifications in ms
 *
 **/
AutomaticDictionary.ComposeWindow = (function( params ){
    this.ad = params.ad;
    this.params = params;
    this.shutdown_chain = [];
    this.logger = params.logger;
    this.window = this.ad.window;
});

AutomaticDictionary.ComposeWindow.canManageWindow = function(window){
    //We can manage the messengercompose window.
    return window.document.location == "chrome://messenger/content/messengercompose/messengercompose.xul";
};

AutomaticDictionary.extend(
    AutomaticDictionary.ComposeWindow.prototype,
    AutomaticDictionary.Lib.Shutdownable);

AutomaticDictionary.extend( AutomaticDictionary.ComposeWindow.prototype, {

    notificationbox_elem_id: "automatic_dictionary_notification",

    name: "ComposeWindow",
    logger: null,

    setListeners:function(){
        var window = this.ad.window;
        if( window && !window.automatic_dictionary_initialized ){
            var _this = this;

            this.setListener( window, 'unload', function(){
                _this.logger.debug("[event] window unload");
                _this.ad.stop();
                _this.ad.shutdown();
            });

            this.setListener( window, 'compose-window-reopen', function(){
                _this.logger.debug("[event] compose window reopen");
                _this.ad.start();
            }, false);
            //Observe when the dict changes
            this.setListener( window.document.getElementById("languageMenuList"),"command",
                function(event){
                    _this.logger.debug('[event] languageMenuList command');
                    _this.ad.languageChanged(event);
                },false);
            //capture language change event
            this.setListener( window.document, 'spellcheck-changed', function(evt){
                _this.logger.debug("spellcheck-changed event captured");
                _this.ad.languageChanged();
            }, false);
            //deactivate is the old window blur event
            this.setListener( window, "deactivate", function(evt){
                if(evt.target == window){
                    _this.logger.debug("[event] window deactivate");
                    _this.ad.stop();
                }
            } , false);
            //activate is the old window focus event
            this.setListener( window, "activate", function(){
                _this.logger.debug("[event] window activate");
                _this.ad.start();
                try{
                    _this.ad.deduceLanguage();
                }catch(e){
                    AutomaticDictionary.logException(e);
                }
            }, false );
            // Listen to subject input and find dictionary for current recipients.
            this.setListener( window.document.getElementById('msgSubject'), 'focus', function(evt){
                _this.logger.debug('subject focus');
                _this.ad.deduceLanguage();<commit_msg>Do not rush to read the dictionary after receiving some events<commit_after>/*
 * The main purpose of this is to allow to work on other windows besides compose, like
 * conversations compose extension.
 * 
 * 
 * Interface of any compose_window implementation:
 *   
 *   // type: can be "TO", "CC" or "BCC"
 *   // returns an array with the recipients
 *   recipients( type )
 *   
 *   // It registers the listeners for:
 *   //
 *   //    start and stop, when windows loses focus, etc.
 *   //    
 *   //    for dictionary change
 *   //    
 *   //    for recipients changed
 *   //
 *   setListeners( ad_object )
 *   
 *   // Shows a message to the user by some seconds.
 *   setLabel( message )
 **/

/**
 *  params are:
 *    ad: automatic_dictionary instance
 *    name: plugin name
 *    logo_url: url of the plugin logo
 *    notification_time: time to show notifications in ms
 *
 **/
AutomaticDictionary.ComposeWindow = (function( params ){
    this.ad = params.ad;
    this.params = params;
    this.shutdown_chain = [];
    this.logger = params.logger;
    this.window = this.ad.window;
});

AutomaticDictionary.ComposeWindow.canManageWindow = function(window){
    //We can manage the messengercompose window.
    return window.document.location == "chrome://messenger/content/messengercompose/messengercompose.xul";
};

AutomaticDictionary.extend(
    AutomaticDictionary.ComposeWindow.prototype,
    AutomaticDictionary.Lib.Shutdownable);

AutomaticDictionary.extend( AutomaticDictionary.ComposeWindow.prototype, {

    notificationbox_elem_id: "automatic_dictionary_notification",

    name: "ComposeWindow",
    logger: null,

    setListeners:function(){
        var window = this.ad.window;
        if( window && !window.automatic_dictionary_initialized ){
            var _this = this;
            var wait_and = function(fn){
                window.setTimeout(fn,800);
            }

            this.setListener( window, 'unload', function(){
                _this.logger.debug("[event] window unload");
                _this.ad.stop();
                _this.ad.shutdown();
            });

            this.setListener( window, 'compose-window-reopen', function(){
                _this.logger.debug("[event] compose window reopen");
                _this.ad.start();
            }, false);
            //Observe when the dict changes
            this.setListener( window.document.getElementById("languageMenuList"),"command",
                function(event){
                    _this.logger.debug('[event] languageMenuList command');
                    wait_and(function(){ _this.ad.languageChanged(event); });
                },false);
            //capture language change event
            this.setListener( window.document, 'spellcheck-changed', function(evt){
                _this.logger.debug("spellcheck-changed event captured");
                wait_and(function(){ _this.ad.languageChanged(); });
            }, false);
            //deactivate is the old window blur event
            this.setListener( window, "deactivate", function(evt){
                if(evt.target == window){
                    _this.logger.debug("[event] window deactivate");
                    _this.ad.stop();
                }
            } , false);
            //activate is the old window focus event
            this.setListener( window, "activate", function(){
                _this.logger.debug("[event] window activate");
                _this.ad.start();
                try{
                    _this.ad.deduceLanguage();
                }catch(e){
                    AutomaticDictionary.logException(e);
                }
            }, false );
            // Listen to subject input and find dictionary for current recipients.
            this.setListener( window.document.getElementById('msgSubject'), 'focus', function(evt){
                _this.logger.debug('subject focus');
                _this.ad.deduceLanguage();
{'fraction_non_alphanumeric': 0.11279657720731233, 'fraction_numerical': 0.00038895371450797355, 'mean_word_length': 2.0514240506329116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955250', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1963, 'n_words': 489}