1
0
mirror of https://github.com/Kopano-dev/kopano-ol-extension.git synced 2023-10-10 13:37:40 +02:00

[KOE-184] Fixed fetching of full GAB in shared folders dialog

This commit is contained in:
Patrick Simpson 2018-12-19 14:12:11 +02:00
parent 93b68913b5
commit 5c722432da
4 changed files with 104 additions and 72 deletions

View File

@ -19,7 +19,6 @@ namespace Acacia.Controls
public interface KDataSourceRaw public interface KDataSourceRaw
{ {
System.Collections.IEnumerable Items { get; }
System.Collections.IEnumerable FilteredItems { get; } System.Collections.IEnumerable FilteredItems { get; }
KDataFilter Filter { get; set; } KDataFilter Filter { get; set; }
string GetItemText(object item); string GetItemText(object item);
@ -28,39 +27,30 @@ namespace Acacia.Controls
abstract public class KDataSource<T> : KDataSourceRaw abstract public class KDataSource<T> : KDataSourceRaw
{ {
/// <summary> private KDataFilter _filter;
/// Returns all the items
/// </summary> public KDataFilter Filter
abstract public IEnumerable<T> Items {
get { return _filter; }
set
{
_filter = value;
UpdateFilter();
}
}
public bool HasFilter
{
get { return _filter?.FilterText != null; }
}
abstract protected void UpdateFilter();
abstract public IEnumerable<T> FilteredItems
{ {
get; get;
} }
public IEnumerable<T> FilteredItems
{
get
{
if (string.IsNullOrWhiteSpace(Filter?.FilterText))
return Items;
return ApplyFilter();
}
}
private IEnumerable<T> ApplyFilter()
{
foreach (T item in Items)
{
if (MatchesFilter(item))
yield return item;
}
}
virtual protected bool MatchesFilter(T item)
{
return GetItemText(item).StartsWith(Filter.FilterText);
}
abstract protected string GetItemText(T item); abstract protected string GetItemText(T item);
public string GetItemText(object item) public string GetItemText(object item)
@ -68,18 +58,11 @@ namespace Acacia.Controls
return GetItemText((T)item); return GetItemText((T)item);
} }
public KDataFilter Filter
{
get;
set;
}
virtual public object NotFoundItem virtual public object NotFoundItem
{ {
get { return null; } get { return null; }
} }
IEnumerable KDataSourceRaw.Items { get{return Items;}}
IEnumerable KDataSourceRaw.FilteredItems { get { return FilteredItems; } } IEnumerable KDataSourceRaw.FilteredItems { get { return FilteredItems; } }
} }
} }

View File

@ -33,7 +33,9 @@ namespace Acacia.Stubs
GreaterEqual, GreaterEqual,
Equal, Equal,
NotEqual, NotEqual,
Like Like,
StartsWith,
StartsWithCI
} }
public interface ISearchField public interface ISearchField
@ -61,6 +63,7 @@ namespace Acacia.Stubs
public interface ISearch<ItemType> : ISearchQuery, IDisposable public interface ISearch<ItemType> : ISearchQuery, IDisposable
where ItemType : IItem where ItemType : IItem
{ {
void Sort(string field, bool descending);
IEnumerable<ItemType> Search(int maxResults = int.MaxValue); IEnumerable<ItemType> Search(int maxResults = int.MaxValue);

View File

@ -121,6 +121,12 @@ namespace Acacia.Stubs.OutlookWrappers
case SearchOperation.Like: case SearchOperation.Like:
oper = "like"; oper = "like";
break; break;
case SearchOperation.StartsWith:
oper = "startswith";
break;
case SearchOperation.StartsWithCI:
oper = "ci_startswith";
break;
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -150,7 +156,9 @@ namespace Acacia.Stubs.OutlookWrappers
} }
} }
private readonly List<SearchTerm> terms = new List<SearchTerm>(); private readonly List<SearchTerm> _terms = new List<SearchTerm>();
private string _sortField;
private bool _sortDescending;
/// <summary> /// <summary>
/// Constructor. /// Constructor.
@ -160,17 +168,23 @@ namespace Acacia.Stubs.OutlookWrappers
{ {
} }
public void Sort(string field, bool descending)
{
_sortField = field;
_sortDescending = descending;
}
public ISearchOperator AddOperator(SearchOperator oper) public ISearchOperator AddOperator(SearchOperator oper)
{ {
SearchOperatorImpl so = new SearchOperatorImpl(oper); SearchOperatorImpl so = new SearchOperatorImpl(oper);
terms.Add(so); _terms.Add(so);
return so; return so;
} }
public ISearchField AddField(string name, bool isUserField = false) public ISearchField AddField(string name, bool isUserField = false)
{ {
SearchField field = new SearchField(name, isUserField); SearchField field = new SearchField(name, isUserField);
terms.Add(field); _terms.Add(field);
return field; return field;
} }
@ -178,6 +192,11 @@ namespace Acacia.Stubs.OutlookWrappers
{ {
string filter = MakeFilter(); string filter = MakeFilter();
if (_sortField != null)
{
_item.Sort(_sortField, _sortDescending);
}
int count = 0; int count = 0;
object value = _item.Find(filter); object value = _item.Find(filter);
while(value != null) while(value != null)
@ -222,7 +241,7 @@ namespace Acacia.Stubs.OutlookWrappers
string filter = "@SQL="; string filter = "@SQL=";
bool first = true; bool first = true;
foreach(SearchTerm term in terms) foreach(SearchTerm term in _terms)
{ {
if (first) if (first)
first = false; first = false;

View File

@ -43,29 +43,61 @@ namespace Acacia.UI
private class GABDataSource : KDataSource<GABUser> private class GABDataSource : KDataSource<GABUser>
{ {
private readonly GABHandler _gab; private readonly GABHandler _gab;
private readonly List<GABUser> _users; public int Limit { get; set; }
public GABDataSource(GABHandler gab) public GABDataSource(GABHandler gab)
{ {
this._gab = gab; this._gab = gab;
Limit = 10;
_users = new List<GABUser>();
foreach (IItem item in _gab.Contacts.Items.Sort("FullName", false))
{
IContactItem contact = item as IContactItem;
// The check for customer id is to avoid groups created as contacts
if (contact != null && contact.CustomerID != null)
{
_users.Add(new GABUser(contact));
}
}
} }
public override IEnumerable<GABUser> Items protected override void UpdateFilter()
{
}
public override IEnumerable<GABUser> FilteredItems
{ {
get get
{ {
return _users; ISearch<IContactItem> search = null;
try
{
IEnumerable<IItem> items;
if (HasFilter)
{
search = _gab.Contacts.Search<IContactItem>();
search.Sort("FullName", false);
ISearchOperator terms = search.AddOperator(SearchOperator.Or);
terms.AddField("urn:schemas:contacts:cn").SetOperation(SearchOperation.Like, Filter.FilterText + "%");
terms.AddField("urn:schemas:contacts:customerid").SetOperation(SearchOperation.Like, Filter.FilterText + "%");
terms.AddField("urn:schemas:contacts:email1").SetOperation(SearchOperation.Like, Filter.FilterText + "%");
items = search.Search(Limit);
}
else
{
items = _gab.Contacts.Items.Sort("FullName", false);
}
int index = 0;
foreach (IItem item in items)
{
IContactItem contact = item as IContactItem;
// The check for customer id is to avoid groups created as contacts
if (contact != null && contact.CustomerID != null)
{
++index;
if (index > Limit)
break;
yield return new GABUser(contact);
}
}
}
finally
{
if (search != null)
search.Dispose();
}
} }
} }
@ -85,15 +117,6 @@ namespace Acacia.UI
return item.UserName; return item.UserName;
} }
protected override bool MatchesFilter(GABUser item)
{
string s = Filter.FilterText.ToLower();
return
item.FullName?.ToLower().StartsWith(s) == true ||
item.UserName?.ToLower().StartsWith(s) == true ||
item.EmailAddress?.ToLower().StartsWith(s) == true;
}
public override object NotFoundItem public override object NotFoundItem
{ {
get get
@ -202,15 +225,19 @@ namespace Acacia.UI
string s = username.ToLower(); string s = username.ToLower();
if (DataSource != null) if (DataSource != null)
{ {
foreach(GABUser user in DataSource.Items) using (ISearch<IContactItem> search = _gab.Contacts.Search<IContactItem>())
{ {
if ( search.Sort("FullName", false);
user.FullName?.ToLower().Equals(s) == true || ISearchOperator terms = search.AddOperator(SearchOperator.Or);
user.UserName?.ToLower().Equals(s) == true || terms.AddField("urn:schemas:contacts:cn").SetOperation(SearchOperation.Like, username);
user.EmailAddress?.ToLower().Equals(s) == true terms.AddField("urn:schemas:contacts:customerid").SetOperation(SearchOperation.Like, username);
) terms.AddField("urn:schemas:contacts:email1").SetOperation(SearchOperation.Like, username);
using (IContactItem contact = search.SearchOne())
{ {
return user; if (contact != null)
{
return new GABUser(contact);
}
} }
} }
} }