Changeset View
Changeset View
Standalone View
Standalone View
smsapp/conversationlistmodel.cpp
Show First 20 Lines • Show All 132 Lines • ▼ Show 20 Line(s) | 103 | { | |||
---|---|---|---|---|---|
133 | item->setData(message.date(), DateRole); | 133 | item->setData(message.date(), DateRole); | ||
134 | 134 | | |||
135 | if (toadd) | 135 | if (toadd) | ||
136 | appendRow(item); | 136 | appendRow(item); | ||
137 | } | 137 | } | ||
138 | 138 | | |||
139 | KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address) | 139 | KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address) | ||
140 | { | 140 | { | ||
141 | const QString& canonicalAddress = canonicalizePhoneNumber(address); | ||||
141 | int rowIndex = 0; | 142 | int rowIndex = 0; | ||
142 | for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) { | 143 | for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) { | ||
143 | const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString(); | 144 | const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString(); | ||
144 | KPeople::PersonData* person = new KPeople::PersonData(uri); | 145 | KPeople::PersonData* person = new KPeople::PersonData(uri); | ||
145 | 146 | | |||
146 | const QString& email = person->email(); | 147 | const QString& email = person->email(); | ||
147 | const QString& phoneNumber = canonicalizePhoneNumber(person->contactCustomProperty("phoneNumber").toString()); | 148 | const QString& phoneNumber = canonicalizePhoneNumber(person->contactCustomProperty("phoneNumber").toString()); | ||
148 | 149 | | |||
149 | if (address == email || canonicalizePhoneNumber(address) == phoneNumber) { | 150 | // To decide if a phone number matches: | ||
151 | // 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored | ||||
152 | // 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string, | ||||
153 | // so if one phone number ends with the other, it is probably just a more-complete version of the same thing | ||||
154 | const QString& longerNumber = canonicalAddress.length() >= phoneNumber.length() ? canonicalAddress : phoneNumber; | ||||
155 | const QString& shorterNumber = canonicalAddress.length() < phoneNumber.length() ? canonicalAddress : phoneNumber; | ||||
156 | | ||||
157 | bool matchingPhoneNumber = longerNumber.endsWith(shorterNumber) && shorterNumber.length() * 2 >= longerNumber.length(); | ||||
158 | | ||||
159 | if (address == email || matchingPhoneNumber) { | ||||
150 | qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name(); | 160 | qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name(); | ||
151 | return person; | 161 | return person; | ||
152 | } | 162 | } | ||
153 | 163 | | |||
154 | delete person; | 164 | delete person; | ||
155 | } | 165 | } | ||
156 | 166 | | |||
157 | return nullptr; | 167 | return nullptr; | ||
158 | } | 168 | } | ||
159 | 169 | | |||
160 | QString ConversationListModel::canonicalizePhoneNumber(const QString& phoneNumber) | 170 | QString ConversationListModel::canonicalizePhoneNumber(const QString& phoneNumber) | ||
161 | { | 171 | { | ||
162 | QString toReturn(phoneNumber); | 172 | QString toReturn(phoneNumber); | ||
163 | toReturn = toReturn.remove(' '); | 173 | toReturn = toReturn.remove(' '); | ||
164 | toReturn = toReturn.remove('-'); | 174 | toReturn = toReturn.remove('-'); | ||
165 | toReturn = toReturn.remove('('); | 175 | toReturn = toReturn.remove('('); | ||
166 | toReturn = toReturn.remove(')'); | 176 | toReturn = toReturn.remove(')'); | ||
167 | toReturn = toReturn.remove('+'); | 177 | toReturn = toReturn.remove('+'); | ||
178 | toReturn = toReturn.remove(QRegularExpression("^0*")); // Strip leading zeroes | ||||
168 | return toReturn; | 179 | return toReturn; | ||
169 | } | 180 | } |