1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is Form Autocomplete Plus.
16
* The Initial Developer of the Original Code is Mozilla Foundation.
17
* Portions created by the Initial Developer are Copyright (C) 2010
18
* the Initial Developer. All Rights Reserved.
21
* Mark Finkle <mfinkle@mozilla.com>
22
* Dan Mills <thunder@mozilla.com>
23
* Justin Dolske <dolske@mozilla.com>
24
* Michael Hanson <mhanson@mozilla.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
const Cc = Components.classes;
41
const Ci = Components.interfaces;
42
const Cu = Components.utils;
44
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
45
Cu.import("resource://gre/modules/Services.jsm");
48
return; // comment out for verbose debugging
49
let msg = Array.join(arguments, " ");
54
// Lazily get the base Form AutoComplete Search
55
XPCOMUtils.defineLazyGetter(this, "FAC", function() {
56
return Components.classesByID["{c11c21b2-71c9-4f87-a0f8-5e13f50495fd}"]
57
.getService(Ci.nsIFormAutoComplete);
60
XPCOMUtils.defineLazyGetter(this, "Contacts", function() {
61
Cu.import("resource:///modules/contacts.jsm");
65
function FormAutoComplete() {
69
FormAutoComplete.prototype = {
70
classDescription: "Form AutoComplete Plus",
71
classID: Components.ID("{cccd414c-3ec2-4cc5-9dc4-36c87cc3c4fe}"),
72
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormAutoComplete]),
74
// Specify the html5 types that we want and some values to guess
76
email: /^(?:.*(?:e-?mail|recipients?).*|(send_)?to(_b?cc)?)$/i,
77
tel: /^(?:tel(?:ephone)?|.*phone.*)$/i
80
checkQueryType: function checkQueryType(aName, aField) {
81
// If we have an input field with the desired html5 type, take it!
82
if (aField && "type" in aField) {
83
let type = aField.type;
84
if (this.contactTypes[type] != null)
88
// Grab properties to check for contact inputs
91
let specialProps = [aField["className"], aField["id"], aField["rel"]];
92
props = props.concat(props.filter(function(aValue) {
97
// Check the gathered properties for contact-like values
98
for (let [type, regex] in Iterator(this.contactTypes)) {
99
if (props.some(function(prop) prop.search(regex) != -1))
105
findContact: function findContact(aQuery, aType, aResult, aDupCheck) {
106
// Match the name and show the email for now..
107
Contacts.find({ fullName: aQuery }).forEach(function(contact) {
108
// Might not have an email for some reason... ?
110
LOG("findContact", "Contact " + contact.fullName);
115
suggestions = contact.emails;
118
suggestions = contact.phoneNumbers;
121
LOG("unknown type!", aType);
125
for each (let suggestion in suggestions) {
126
if (aDupCheck[suggestion])
128
aDupCheck[suggestion] = true;
130
let data = contact.fullName + " <" + suggestion + ">";
131
aResult.appendMatch(suggestion, data, null, "contact");
135
LOG("findContact error", ex);
140
autoCompleteSearch: function autoCompleteSearch(aName, aQuery, aField, aPrev) {
141
if (!Services.prefs.getBoolPref("browser.formfill.enable"))
144
LOG("autocomplete search", Array.slice(arguments));
145
let result = Cc["@mozilla.org/autocomplete/simple-result;1"].createInstance(Ci.nsIAutoCompleteSimpleResult);
146
result.setSearchString(aQuery);
148
// Don't allow duplicates get merged into the final results
151
// Use the base form autocomplete for non-contact searches
152
let normal = FAC.autoCompleteSearch(aName, aQuery, aField, aPrev);
153
if (normal.matchCount > 0) {
154
for (let i = 0; i < normal.matchCount; i++) {
155
dupCheck[normal.getValueAt(i)] = true;
156
result.appendMatch(normal.getValueAt(i), normal.getCommentAt(i), normal.getImageAt(i), normal.getStyleAt(i));
160
// Do searches for certain input fields
161
let type = this.checkQueryType(aName, aField);
163
this.findContact(aQuery, type, result, dupCheck);
165
let resultCode = result.matchCount ? "RESULT_SUCCESS" : "RESULT_NOMATCH";
166
result.setSearchResult(Ci.nsIAutoCompleteResult[resultCode]);
171
let components = [FormAutoComplete];
172
const NSGetFactory = XPCOMUtils.generateNSGetFactory(components);