Changeset View
Changeset View
Standalone View
Standalone View
src/personmanager.cpp
Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Line(s) | 131 | { | |||
---|---|---|---|---|---|
137 | QStringList metacontacts; | 137 | QStringList metacontacts; | ||
138 | QStringList contacts; | 138 | QStringList contacts; | ||
139 | 139 | | |||
140 | bool rc = true; | 140 | bool rc = true; | ||
141 | 141 | | |||
142 | QList<QDBusMessage> pendingMessages; | 142 | QList<QDBusMessage> pendingMessages; | ||
143 | 143 | | |||
144 | // separate the passed ids to metacontacts and simple contacts | 144 | // separate the passed ids to metacontacts and simple contacts | ||
145 | Q_FOREACH (const QString &id, ids) { | 145 | for (const QString &id : ids) { | ||
146 | if (id.startsWith(QLatin1String("kpeople://"))) { | 146 | if (id.startsWith(QLatin1String("kpeople://"))) { | ||
147 | metacontacts << id; | 147 | metacontacts << id; | ||
148 | } else { | 148 | } else { | ||
149 | contacts << id; | 149 | contacts << id; | ||
150 | } | 150 | } | ||
151 | } | 151 | } | ||
152 | 152 | | |||
153 | // create new personUriString | 153 | // create new personUriString | ||
Show All 16 Lines | |||||
170 | 170 | | |||
171 | // start a db transaction (ends automatically on destruction) | 171 | // start a db transaction (ends automatically on destruction) | ||
172 | Transaction t(m_db); | 172 | Transaction t(m_db); | ||
173 | 173 | | |||
174 | // processed passed metacontacts | 174 | // processed passed metacontacts | ||
175 | if (metacontacts.count() > 1) { | 175 | if (metacontacts.count() > 1) { | ||
176 | // collect all the contacts from other persons | 176 | // collect all the contacts from other persons | ||
177 | QStringList personContacts; | 177 | QStringList personContacts; | ||
178 | Q_FOREACH (const QString &id, metacontacts) { | 178 | for (const QString &id : qAsConst(metacontacts)) { | ||
179 | if (id != personUriString) { | 179 | if (id != personUriString) { | ||
180 | personContacts << contactsForPersonUri(id); | 180 | personContacts << contactsForPersonUri(id); | ||
181 | } | 181 | } | ||
182 | } | 182 | } | ||
183 | 183 | | |||
184 | // iterate over all of the contacts and change their personID to the new personUriString | 184 | // iterate over all of the contacts and change their personID to the new personUriString | ||
185 | Q_FOREACH (const QString &id, personContacts) { | 185 | for (const QString &id : qAsConst(personContacts)) { | ||
186 | QSqlQuery updateQuery(m_db); | 186 | QSqlQuery updateQuery(m_db); | ||
187 | updateQuery.prepare(QStringLiteral("UPDATE persons SET personID = ? WHERE contactID = ?")); | 187 | updateQuery.prepare(QStringLiteral("UPDATE persons SET personID = ? WHERE contactID = ?")); | ||
188 | updateQuery.bindValue(0, personUriString.mid(strlen("kpeople://"))); | 188 | updateQuery.bindValue(0, personUriString.mid(strlen("kpeople://"))); | ||
189 | updateQuery.bindValue(1, id); | 189 | updateQuery.bindValue(1, id); | ||
190 | if (!updateQuery.exec()) { | 190 | if (!updateQuery.exec()) { | ||
191 | rc = false; | 191 | rc = false; | ||
192 | } | 192 | } | ||
193 | 193 | | |||
Show All 11 Lines | |||||
205 | message.setArguments(QVariantList() << id << personUriString); | 205 | message.setArguments(QVariantList() << id << personUriString); | ||
206 | pendingMessages << message; | 206 | pendingMessages << message; | ||
207 | } | 207 | } | ||
208 | } | 208 | } | ||
209 | 209 | | |||
210 | // process passed contacts | 210 | // process passed contacts | ||
211 | if (!contacts.isEmpty()) { | 211 | if (!contacts.isEmpty()) { | ||
212 | 212 | | |||
213 | Q_FOREACH (const QString &id, contacts) { | 213 | for (const QString &id : qAsConst(contacts)) { | ||
214 | QSqlQuery insertQuery(m_db); | 214 | QSqlQuery insertQuery(m_db); | ||
215 | insertQuery.prepare(QStringLiteral("INSERT INTO persons VALUES (?, ?)")); | 215 | insertQuery.prepare(QStringLiteral("INSERT INTO persons VALUES (?, ?)")); | ||
216 | insertQuery.bindValue(0, id); | 216 | insertQuery.bindValue(0, id); | ||
217 | insertQuery.bindValue(1, personUriString.mid(strlen("kpeople://"))); //strip kpeople:// | 217 | insertQuery.bindValue(1, personUriString.mid(strlen("kpeople://"))); //strip kpeople:// | ||
218 | if (!insertQuery.exec()) { | 218 | if (!insertQuery.exec()) { | ||
219 | rc = false; | 219 | rc = false; | ||
220 | } | 220 | } | ||
221 | 221 | | |||
222 | //FUTURE OPTIMIZATION - this would be best as one signal, but arguments become complex | 222 | //FUTURE OPTIMIZATION - this would be best as one signal, but arguments become complex | ||
223 | QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KPeople"), | 223 | QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KPeople"), | ||
224 | QStringLiteral("org.kde.KPeople"), | 224 | QStringLiteral("org.kde.KPeople"), | ||
225 | QStringLiteral("ContactAddedToPerson")); | 225 | QStringLiteral("ContactAddedToPerson")); | ||
226 | 226 | | |||
227 | message.setArguments(QVariantList() << id << personUriString); | 227 | message.setArguments(QVariantList() << id << personUriString); | ||
228 | pendingMessages << message; | 228 | pendingMessages << message; | ||
229 | } | 229 | } | ||
230 | } | 230 | } | ||
231 | 231 | | |||
232 | //if success send all messages to other clients | 232 | //if success send all messages to other clients | ||
233 | //otherwise roll back our database changes and return an empty string | 233 | //otherwise roll back our database changes and return an empty string | ||
234 | if (rc) { | 234 | if (rc) { | ||
235 | Q_FOREACH (const QDBusMessage &message, pendingMessages) { | 235 | for (const QDBusMessage &message : qAsConst(pendingMessages)) { | ||
236 | QDBusConnection::sessionBus().send(message); | 236 | QDBusConnection::sessionBus().send(message); | ||
237 | } | 237 | } | ||
238 | } else { | 238 | } else { | ||
239 | t.cancel(); | 239 | t.cancel(); | ||
240 | personUriString.clear(); | 240 | personUriString.clear(); | ||
241 | } | 241 | } | ||
242 | 242 | | |||
243 | return personUriString; | 243 | return personUriString; | ||
244 | } | 244 | } | ||
245 | 245 | | |||
246 | bool PersonManager::unmergeContact(const QString &id) | 246 | bool PersonManager::unmergeContact(const QString &id) | ||
247 | { | 247 | { | ||
248 | //remove rows from DB | 248 | //remove rows from DB | ||
249 | if (id.startsWith(QLatin1String("kpeople://"))) { | 249 | if (id.startsWith(QLatin1String("kpeople://"))) { | ||
250 | QSqlQuery query(m_db); | 250 | QSqlQuery query(m_db); | ||
251 | 251 | | |||
252 | const QStringList contactUris = contactsForPersonUri(id); | 252 | const QStringList contactUris = contactsForPersonUri(id); | ||
253 | query.prepare(QStringLiteral("DELETE FROM persons WHERE personId = ?")); | 253 | query.prepare(QStringLiteral("DELETE FROM persons WHERE personId = ?")); | ||
254 | query.bindValue(0, id.mid(strlen("kpeople://"))); | 254 | query.bindValue(0, id.mid(strlen("kpeople://"))); | ||
255 | query.exec(); | 255 | query.exec(); | ||
256 | 256 | | |||
257 | Q_FOREACH (const QString &contactUri, contactUris) { | 257 | for (const QString &contactUri : contactUris) { | ||
258 | //FUTURE OPTIMIZATION - this would be best as one signal, but arguments become complex | 258 | //FUTURE OPTIMIZATION - this would be best as one signal, but arguments become complex | ||
259 | QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KPeople"), | 259 | QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KPeople"), | ||
260 | QStringLiteral("org.kde.KPeople"), | 260 | QStringLiteral("org.kde.KPeople"), | ||
261 | QStringLiteral("ContactRemovedFromPerson")); | 261 | QStringLiteral("ContactRemovedFromPerson")); | ||
262 | 262 | | |||
263 | message.setArguments(QVariantList() << contactUri); | 263 | message.setArguments(QVariantList() << contactUri); | ||
264 | QDBusConnection::sessionBus().send(message); | 264 | QDBusConnection::sessionBus().send(message); | ||
265 | } | 265 | } | ||
Show All 28 Lines |