Bug 452232 Support for ldap_2.<server>.autoComplete.filterTemplate preference r=Standard8 sr=Neil
--- a/mailnews/addrbook/public/nsIAbDirectoryQuery.idl
+++ b/mailnews/addrbook/public/nsIAbDirectoryQuery.idl
@@ -13,17 +13,17 @@ interface nsIAbDirectory;
/**
* The arguments for a query.
*
* Contains an expression for perform matches
* and an array of properties which should be
* returned if a match is found from the expression
*
*/
-[scriptable, uuid(d6fcbedd-d574-4357-abf4-81565d867749)]
+[scriptable, uuid(03af3018-2590-4f4c-a88c-1fff6595ef05)]
interface nsIAbDirectoryQueryArguments : nsISupports
{
/**
* Defines the boolean expression for
* the matching of cards
*
*/
attribute nsISupports expression;
@@ -35,16 +35,21 @@ interface nsIAbDirectoryQueryArguments :
*/
attribute boolean querySubDirectories;
/**
* A parameter which can be used to pass in data specific to a particular
* type of addressbook.
*/
attribute nsISupports typeSpecificArg;
+
+ /**
+ * A custom search filter which user wants to use in LDAP query.
+ */
+ attribute AUTF8String filter;
};
[scriptable, uuid(3A6E0C0C-1DD2-11B2-B23D-EA3A8CCB333C)]
interface nsIAbDirectoryQueryPropertyValue : nsISupports
{
/**
* The property which should be matched
--- a/mailnews/addrbook/src/nsAbDirectoryQuery.cpp
+++ b/mailnews/addrbook/src/nsAbDirectoryQuery.cpp
@@ -140,16 +140,27 @@ NS_IMETHODIMP nsAbDirectoryQueryArgument
}
NS_IMETHODIMP nsAbDirectoryQueryArguments::SetTypeSpecificArg(nsISupports* aArg)
{
mTypeSpecificArg = aArg;
return NS_OK;
}
+NS_IMETHODIMP nsAbDirectoryQueryArguments::GetFilter(nsACString & aFilter)
+{
+ aFilter.Assign(mFilter);
+ return NS_OK;
+}
+
+NS_IMETHODIMP nsAbDirectoryQueryArguments::SetFilter(const nsACString & aFilter)
+{
+ mFilter.Assign(aFilter);
+ return NS_OK;
+}
NS_IMPL_THREADSAFE_ISUPPORTS1(nsAbDirectoryQueryPropertyValue, nsIAbDirectoryQueryPropertyValue)
nsAbDirectoryQueryPropertyValue::nsAbDirectoryQueryPropertyValue()
{
}
nsAbDirectoryQueryPropertyValue::nsAbDirectoryQueryPropertyValue(const char* aName,
--- a/mailnews/addrbook/src/nsAbDirectoryQuery.h
+++ b/mailnews/addrbook/src/nsAbDirectoryQuery.h
@@ -36,16 +36,17 @@ public:
nsAbDirectoryQueryArguments();
virtual ~nsAbDirectoryQueryArguments();
protected:
nsCOMPtr<nsISupports> mExpression;
nsCOMPtr<nsISupports> mTypeSpecificArg;
bool mQuerySubDirectories;
+ nsCString mFilter;
};
class nsAbDirectoryQueryPropertyValue : public nsIAbDirectoryQueryPropertyValue
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIABDIRECTORYQUERYPROPERTYVALUE
--- a/mailnews/addrbook/src/nsAbLDAPAutoCompleteSearch.js
+++ b/mailnews/addrbook/src/nsAbLDAPAutoCompleteSearch.js
@@ -285,24 +285,31 @@ nsAbLDAPAutoCompleteSearch.prototype = {
this._result._commentColumn = queryObject.book.dirName;
this._listener = aListener;
var args =
Components.classes["@mozilla.org/addressbook/directory/query-arguments;1"]
.createInstance(Components.interfaces.nsIAbDirectoryQueryArguments);
- // These are hard-coded for now and the _checkEntry function should match
- // what these statements do.
- var searchStr = "(or(PrimaryEmail,bw,@V)(DisplayName,bw,@V))";
- searchStr = searchStr.replace(/@V/g, encodeURIComponent(aSearchString));
+ var filterTemplate = queryObject.book.getStringValue("autoComplete.filterTemplate", "");
+
+ // Use default value when preference is not set or it contains empty string
+ if (!filterTemplate)
+ filterTemplate = "(|(cn=%v1*%v2-*)(mail=%v1*%v2-*)(sn=%v1*%v2-*))";
- args.expression = abMgr.convertQueryStringToExpression(searchStr);
+ // Create filter from filter template and search string
+ var ldapSvc = Components.classes["@mozilla.org/network/ldap-service;1"]
+ .getService(Components.interfaces.nsILDAPService);
+ var filter = ldapSvc.createFilter(1024, filterTemplate, "", "", "", aSearchString);
+ if (!filter)
+ throw new Error("Filter string is empty, check if filterTemplate variable is valid in prefs.js.");
args.typeSpecificArg = queryObject.attributes;
args.querySubDirectories = true;
+ args.filter = filter;
// Start the actual search
queryObject.context =
queryObject.query.doQuery(queryObject.book, args, this,
queryObject.book.maxHits, 0);
},
stopSearch: function stopSearch() {
--- a/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp
+++ b/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp
@@ -415,28 +415,35 @@ NS_IMETHODIMP nsAbLDAPDirectoryQuery::Do
rv = url->SetAttributes(returnAttributes);
// Now do the error check
NS_ENSURE_SUCCESS(rv, rv);
// Also require the objectClass attribute, it is used by
// nsAbLDAPCard::SetMetaProperties
rv = url->AddAttribute(NS_LITERAL_CSTRING("objectClass"));
- // Get the filter
- nsCOMPtr<nsISupports> supportsExpression;
- rv = aArguments->GetExpression (getter_AddRefs (supportsExpression));
+ nsAutoCString filter;
+
+ // Get filter from arguments if set:
+ rv = aArguments->GetFilter(filter);
NS_ENSURE_SUCCESS(rv, rv);
- nsCOMPtr<nsIAbBooleanExpression> expression (do_QueryInterface (supportsExpression, &rv));
- nsAutoCString filter;
-
- // figure out how we map attribute names to addressbook fields for this
- // query
- rv = nsAbBoolExprToLDAPFilter::Convert(map, expression, filter);
- NS_ENSURE_SUCCESS(rv, rv);
+ if (filter.IsEmpty()) {
+ // Get the filter
+ nsCOMPtr<nsISupports> supportsExpression;
+ rv = aArguments->GetExpression(getter_AddRefs(supportsExpression));
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsIAbBooleanExpression> expression(do_QueryInterface(supportsExpression, &rv));
+
+ // figure out how we map attribute names to addressbook fields for this
+ // query
+ rv = nsAbBoolExprToLDAPFilter::Convert(map, expression, filter);
+ NS_ENSURE_SUCCESS(rv, rv);
+ }
/*
* Mozilla itself cannot arrive here with a blank filter
* as the nsAbLDAPDirectory::StartSearch() disallows it.
* But 3rd party LDAP query integration with Mozilla begins
* in this method.
*
* Default the filter string if blank, otherwise it gets