/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 * Paul ROUGET
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/*
** AutoCompleteSearch component sample
** Works with: <textbox type="autocomplete" autocompletesearch="Weeksdays"/>
*/

const nsIAutoCompleteSearch = Components.interfaces.nsIAutoCompleteSearch;
const nsISupports = Components.interfaces.nsISupports;

const CLASS_ID = Components.ID("{90437162-c051-4358-ae91-ab6af56cb34c}");
const CLASS_NAME = "Autocomplete Week's days";
const CONTRACT_ID = "@mozilla.org/autocomplete/search;1?name=Weeksdays";

function AutoCompleteSearch() {
  dump("autoSearch loaded.\n");
};

AutoCompleteSearch.prototype =
{
  startSearch: function(searchString, searchParam, previousResult, listener)
  {
          var week = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"];
          var candidats = new Array();
          var i;
          for each(i in week) {
            if (i.indexOf(searchString) == 0) {
              candidats.push(i);
            }
          }
          var result = {
            QueryInterface: function(aIID) {
              if (aIID.equals(nsIAutoCompleteResult) || aIID.equals(nsISupports))
                return this;
              throw Components.results.NS_NOINTERFACE;
            },
            results: null,
            defaultIndex: 0,
            errorDescription: null,
            matchCount: 0,
            searchResult: Components.interfaces.nsIAutoCompleteResult.RESULT_SUCCESS,
            searchString: searchString,
            getCommentAt: function(index) {},
            getStyleAt: function(index) {},
            getValueAt: function(index) {return this.results[index]},
            removeValueAt: function(rowIndex, removeFromDb) {}
         }
         result.results = candidats;
         result.matchCount = candidats.length;
         listener.onSearchResult(this, result);
  },
  stopSearch: function() { },

  QueryInterface: function(aIID)
  {
    if (!aIID.equals(nsIAutoCompleteSearch) &&    
        !aIID.equals(nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

var AutoCompleteSearchFactory =
{
  createInstance: function (aOuter, aIID)
  {
    if (aOuter != null) throw Components.results.NS_ERROR_NO_AGGREGATION;
    return (new AutoCompleteSearch()).QueryInterface(aIID);
  }
};

var AutoCompleteSearchModule =
{
  _firstTime: true,
  registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  {
    if (this._firstTime)
    {
      this._firstTime = false;
      throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
    };
    aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
    aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  },

  unregisterSelf: function(aCompMgr, aLocation, aType)
  {
    aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
    aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  },

  getClassObject: function(aCompMgr, aCID, aIID)
  {
    if (!aIID.equals(Components.interfaces.nsIFactory))
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

    if (aCID.equals(CLASS_ID))
      return AutoCompleteSearchFactory;

    throw Components.results.NS_ERROR_NO_INTERFACE;
  },

  canUnload: function(aCompMgr) { return true; }
};

function NSGetModule(aCompMgr, aFileSpec) { return AutoCompleteSearchModule; }


