~ubuntu-branches/ubuntu/trusty/fennec/trusty

« back to all changes in this revision

Viewing changes to mobile/components/FormAutoComplete.js

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2011-01-26 20:31:40 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110126203140-zcg54f8ost2vmrxr
Tags: 4.0~b3-0ubuntu1
* New upstream release v4.0 B3 (FENNEC_4_0b3_RELEASE)

* Update build-depends for xulrunner-2.0
  - update debian/control
* Update mozclient to point to the mobile-browser repo
  - update debian/mozclient/fennec.conf
* Build with "--with-system-libxul"
  - update debian/rules
* Add launcher script, based on the one used in Firefox but with the
  unnecessary bits stripped out
  - add debian/fennec.sh
  - update debian/rules
* Refresh patches for new version
  - update debian/patches/bump_gecko_versions_in_application.ini.patch
  - update debian/patches/ubuntu_codes_google.patch
  - update debian/patches/installer.patch
* Drop unneeded patches
  - remove debian/patches/nspr_flags_by_pkg_config_hack.patch
  - remove debian/patches/xul191_l10n.patch
  - update debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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
 
12
 * License.
 
13
 *
 
14
 * The Original Code is Form Autocomplete Plus.
 
15
 *
 
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.
 
19
 *
 
20
 * Contributor(s):
 
21
 *  Mark Finkle <mfinkle@mozilla.com>
 
22
 *  Dan Mills <thunder@mozilla.com>
 
23
 *  Justin Dolske <dolske@mozilla.com>
 
24
 *  Michael Hanson <mhanson@mozilla.com>
 
25
 *
 
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.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
const Cc = Components.classes;
 
41
const Ci = Components.interfaces;
 
42
const Cu = Components.utils;
 
43
 
 
44
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
45
Cu.import("resource://gre/modules/Services.jsm");
 
46
 
 
47
function LOG() {
 
48
  return; // comment out for verbose debugging
 
49
  let msg = Array.join(arguments, " ");
 
50
  dump(msg + "\n");
 
51
  Cu.reportError(msg);
 
52
}
 
53
 
 
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);
 
58
});
 
59
 
 
60
XPCOMUtils.defineLazyGetter(this, "Contacts", function() {
 
61
  Cu.import("resource:///modules/contacts.jsm");
 
62
  return Contacts;
 
63
});
 
64
 
 
65
function FormAutoComplete() {
 
66
  LOG("new FAC");
 
67
}
 
68
 
 
69
FormAutoComplete.prototype = {
 
70
  classDescription: "Form AutoComplete Plus",
 
71
  classID: Components.ID("{cccd414c-3ec2-4cc5-9dc4-36c87cc3c4fe}"),
 
72
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormAutoComplete]),
 
73
 
 
74
  // Specify the html5 types that we want and some values to guess
 
75
  contactTypes: {
 
76
    email: /^(?:.*(?:e-?mail|recipients?).*|(send_)?to(_b?cc)?)$/i,
 
77
    tel: /^(?:tel(?:ephone)?|.*phone.*)$/i
 
78
  },
 
79
 
 
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)
 
85
        return type;
 
86
    }
 
87
 
 
88
    // Grab properties to check for contact inputs
 
89
    let props = [aName];
 
90
    if (aField) {
 
91
      let specialProps = [aField["className"], aField["id"], aField["rel"]];
 
92
      props = props.concat(props.filter(function(aValue) {
 
93
        return aValue;
 
94
      }));
 
95
    }
 
96
 
 
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))
 
100
        return type;
 
101
    }
 
102
    return null;
 
103
  },
 
104
 
 
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... ?
 
109
      try {
 
110
        LOG("findContact", "Contact " + contact.fullName);
 
111
 
 
112
        let suggestions;
 
113
        switch (aType) {
 
114
          case "email":
 
115
            suggestions = contact.emails;
 
116
            break;
 
117
          case "tel":
 
118
            suggestions = contact.phoneNumbers;
 
119
            break;
 
120
          default:
 
121
            LOG("unknown type!", aType);
 
122
            return;
 
123
        }
 
124
 
 
125
        for each (let suggestion in suggestions) {
 
126
          if (aDupCheck[suggestion])
 
127
            continue;
 
128
          aDupCheck[suggestion] = true;
 
129
 
 
130
          let data = contact.fullName + " <" + suggestion + ">";
 
131
          aResult.appendMatch(suggestion, data, null, "contact");
 
132
        }
 
133
      }
 
134
      catch(ex) {
 
135
        LOG("findContact error", ex);
 
136
      }
 
137
    });
 
138
  },
 
139
 
 
140
  autoCompleteSearch: function autoCompleteSearch(aName, aQuery, aField, aPrev) {
 
141
    if (!Services.prefs.getBoolPref("browser.formfill.enable"))
 
142
      return;
 
143
 
 
144
    LOG("autocomplete search", Array.slice(arguments));
 
145
    let result = Cc["@mozilla.org/autocomplete/simple-result;1"].createInstance(Ci.nsIAutoCompleteSimpleResult);
 
146
    result.setSearchString(aQuery);
 
147
 
 
148
    // Don't allow duplicates get merged into the final results
 
149
    let dupCheck = {};
 
150
 
 
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));
 
157
      }
 
158
    }
 
159
 
 
160
    // Do searches for certain input fields
 
161
    let type = this.checkQueryType(aName, aField);
 
162
    if (type != null)
 
163
      this.findContact(aQuery, type, result, dupCheck);
 
164
 
 
165
    let resultCode = result.matchCount ? "RESULT_SUCCESS" : "RESULT_NOMATCH";
 
166
    result.setSearchResult(Ci.nsIAutoCompleteResult[resultCode]);
 
167
    return result;
 
168
  }
 
169
};
 
170
 
 
171
let components = [FormAutoComplete];
 
172
const NSGetFactory = XPCOMUtils.generateNSGetFactory(components);