// **************************************************************************************
// Class CQuickFindCallback : 
// **************************************************************************************

function CQuickFindCallback ( ) { this.wrappedJSObject = this; }

CQuickFindCallback.prototype = {

  value          : 0,

  listeners      : new Array( ),

  addlistener    : function ( listener )
                   {
                     if(this.listeners.length == 0)
                     {
                       var Server = Components.classes["@idm.fr/QuickFindServer;1"].
                                    getService(Components.interfaces.nsIQFServer);
                       Server.Create( );
                     }
                   
                     for(var n=0;
                         n<this.listeners.length && 
                         this.listeners[n] != listener; 
                         n++)
                     ;
                     this.listeners[n] = listener;
                   },
  removelistener : function ( listener )
                   {
                     for(var n=0;
                         n<this.listeners.length && 
                         this.listeners[n] != listener;
                         n++)
                     ;
                     this.listeners.splice(n,1);
                     if(this.listeners.length == 0)
                     {
                       var Server = Components.classes["@idm.fr/QuickFindServer;1"].
                                    getService(Components.interfaces.nsIQFServer);
                       Server.Destroy( );
                     }                     
                   },
  broadcast :      function ( sData )
                   {
                     // this.prompt("going to broadcast, "+this.listeners.length+" listeners are active.");
                     for(var n=0;n<this.listeners.length; n++)
                       if(this.listeners[n].callback)
                         this.listeners[n].callback( sData );
                   },
  debug :          function ( )
                   {
                     var res = "Hello World, from CQuickFindCallback, #" + (this.value++) + ", Listeners = " + this.listeners.length;
                     this.broadcast(res);
                   },
/*
  prompt:          function( psText )
                   {
                       var xiPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].createInstance()
                                                       .QueryInterface(Components.interfaces.nsIPromptService);
                       xiPromptService.alert( this.window, "dummy title", psText );
                   },
*/
  QueryInterface : function ( iid )
                   {
                     if(!iid.equals(Components.interfaces.nsISupports) &&
                        !iid.equals(Components.interfaces.nsIQFCallback) )
                       throw Components.results.NS_ERRROR_NO_INTERFACE;
                     return this;
                   }

}; // END CQuickFindCallback.prototype

var Module = {
  // Register this component
  registerSelf: function ( compMgr, fileSpec, location, type )
  {
    compMgr = compMgr.QueryInterface( Components.interfaces.nsIComponentManagerObsolete );
    
    compMgr.registerComponentWithType( this.cid,
                                       "IDM QuickFind callback object",
                                       this.contractId,
                                       fileSpec,
                                       location,
                                       true,
                                       true,
                                       type );
  },
  // getClassObject: Return this component's factory object.
  getClassObject: function (compMgr, cid, iid)
  {
    if(!cid.equals(this.cid))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    if(!iid.equals(Components.interfaces.nsIFactory))
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
      
    return this.factory;
  },
  cid : Components.ID("{7BF1C4F9-9B6A-41A6-992D-3595B2EEE3DE}"),
  contractId : "@idm.fr/QuickFindCallback;1",
  factory :
  {
    // The Callback Object is a singleton
    singleton : null,

    createInstance: function( outer, iid)
    {
      if(outer != null)
        throw Components.results.NS_ERROR_NO_AGGREGATION;
      if(!this.singleton)
        this.singleton = new CQuickFindCallback( );
      return this.singleton.QueryInterface(iid);
    }
  },
  
  canUnload: function ( compMgr )
  {
    return true;
  }

}; // END Module

function NSGetModule( compMgr, fileSpec ) { return Module; }
