-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBookDatabase.cs
More file actions
74 lines (59 loc) · 2.83 KB
/
Copy pathAddressBookDatabase.cs
File metadata and controls
74 lines (59 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.Data.Sqlite;
namespace iPhoneBackupMessagesRenderer;
public class AddressBookDatabase : IDisposable
{
private readonly SqliteConnection _connection;
private readonly string? _myName;
// we cache negative results via null here too
private readonly Dictionary<string, string?> _cache = new();
public AddressBookDatabase(string addressBookDbFilePath, string? myName = null)
{
_myName = myName;
_connection = SqliteHelper.OpenDatabaseReadOnly(addressBookDbFilePath);
}
public void Dispose()
{
_connection.Dispose();
}
// searchString is probably a phone number
public string? GuessName(string searchString)
{
// special case things we shouldn't guess
if (string.IsNullOrWhiteSpace(searchString)) return searchString;
if (searchString.Equals("Me", StringComparison.CurrentCultureIgnoreCase)) return _myName ?? searchString;
// We sometimes get SMS messages from short-codes like 422 which screw up the text search.
// We only want to guess if it's a qualified phone number (e.g. starts with +64 or similar)
// or an Email address
if (!searchString.StartsWith("+") && !searchString.Contains("@")) return searchString;
if (_cache.TryGetValue(searchString, out var value)) return value;
var guessed = GuessNameInternal(searchString);
_cache[searchString] = guessed;
return guessed;
}
string? GuessNameInternal(string searchString)
{
var cmd = _connection.CreateCommand();
cmd.CommandText = """
SELECT c0First, c1Last, c2Middle, c6Organization
FROM ABPersonFullTextSearch_content
WHERE c16Phone LIKE '%' || $searchString || '%'
OR c17Email LIKE '%' || $searchString || '%'
""";
cmd.Parameters.AddWithValue("$searchString", searchString);
using var reader = cmd.ExecuteReader();
string? firstName = null, lastName = null, middleName = null, organization = null;
if (reader.Read()) // just read the first row, if there are multiple we have no way to rank them
{
var iter = new DbDataReaderIterator(reader);
firstName = iter.NextNullableString();
lastName = iter.NextNullableString();
middleName = iter.NextNullableString();
organization = iter.NextNullableString();
}
var guessedName = string.Join(" ",
new[] { firstName, middleName, lastName }.Where(x => !string.IsNullOrWhiteSpace(x)));
if (!string.IsNullOrWhiteSpace(guessedName)) return guessedName;
if (!string.IsNullOrWhiteSpace(organization)) return organization;
return null; // couldn't match on this input
}
}