File indexing completed on 2024-11-24 04:50:41
0001 // SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@gmail.com> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "addresseewrapper.h" 0005 #include "merkuro_contact_debug.h" 0006 #include <KContacts/VCardConverter> 0007 #include <QGuiApplication> 0008 0009 AddresseeWrapper::AddresseeWrapper(QObject *parent) 0010 : QObject(parent) 0011 , Akonadi::ItemMonitor() 0012 , m_addressesModel(new AddressModel(this)) 0013 , m_emailModel(new EmailModel(this)) 0014 , m_imppModel(new ImppModel(this)) 0015 , m_phoneModel(new PhoneModel(this)) 0016 { 0017 Akonadi::ItemFetchScope scope; 0018 scope.fetchFullPayload(); 0019 scope.fetchAllAttributes(); 0020 scope.setFetchRelations(true); 0021 scope.setAncestorRetrieval(Akonadi::ItemFetchScope::Parent); 0022 setFetchScope(scope); 0023 0024 connect(m_emailModel, &EmailModel::changed, this, [this](const KContacts::Email::List &emails) { 0025 m_addressee.setEmailList(emails); 0026 }); 0027 0028 connect(m_phoneModel, &PhoneModel::changed, this, [this](const KContacts::PhoneNumber::List &phoneNumbers) { 0029 m_addressee.setPhoneNumbers(phoneNumbers); 0030 }); 0031 0032 connect(m_imppModel, &ImppModel::changed, this, [this](const KContacts::Impp::List &impps) { 0033 m_addressee.setImppList(impps); 0034 }); 0035 } 0036 0037 AddresseeWrapper::~AddresseeWrapper() = default; 0038 0039 void AddresseeWrapper::notifyDataChanged() 0040 { 0041 Q_EMIT collectionChanged(); 0042 Q_EMIT formattedNameChanged(); 0043 Q_EMIT additionalNameChanged(); 0044 Q_EMIT familyNameChanged(); 0045 Q_EMIT givenNameChanged(); 0046 Q_EMIT prefixChanged(); 0047 Q_EMIT suffixChanged(); 0048 Q_EMIT birthdayChanged(); 0049 Q_EMIT photoChanged(); 0050 Q_EMIT phoneNumbersChanged(); 0051 Q_EMIT preferredEmailChanged(); 0052 Q_EMIT uidChanged(); 0053 Q_EMIT noteChanged(); 0054 Q_EMIT nickNameChanged(); 0055 Q_EMIT blogFeedChanged(); 0056 Q_EMIT anniversaryChanged(); 0057 Q_EMIT spousesNameChanged(); 0058 Q_EMIT organizationChanged(); 0059 Q_EMIT professionChanged(); 0060 Q_EMIT titleChanged(); 0061 Q_EMIT departmentChanged(); 0062 Q_EMIT officeChanged(); 0063 Q_EMIT managersNameChanged(); 0064 Q_EMIT assistantsNameChanged(); 0065 } 0066 0067 Akonadi::Item AddresseeWrapper::addresseeItem() const 0068 { 0069 return item(); 0070 } 0071 0072 AddressModel *AddresseeWrapper::addressesModel() const 0073 { 0074 return m_addressesModel; 0075 } 0076 0077 ImppModel *AddresseeWrapper::imppModel() const 0078 { 0079 return m_imppModel; 0080 } 0081 0082 void AddresseeWrapper::setAddresseeItem(const Akonadi::Item &addresseeItem) 0083 { 0084 Akonadi::ItemMonitor::setItem(addresseeItem); 0085 if (addresseeItem.hasPayload<KContacts::Addressee>()) { 0086 setAddressee(addresseeItem.payload<KContacts::Addressee>()); 0087 Q_EMIT addresseeItemChanged(); 0088 Q_EMIT collectionChanged(); 0089 } else { 0090 // Payload not found, try to fetch it 0091 auto job = new Akonadi::ItemFetchJob(addresseeItem); 0092 job->fetchScope().fetchFullPayload(); 0093 connect(job, &Akonadi::ItemFetchJob::result, this, [this](KJob *job) { 0094 auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job); 0095 auto item = fetchJob->items().at(0); 0096 if (item.hasPayload<KContacts::Addressee>()) { 0097 setAddressee(item.payload<KContacts::Addressee>()); 0098 Q_EMIT addresseeItemChanged(); 0099 Q_EMIT collectionChanged(); 0100 } else { 0101 qCWarning(MERKURO_LOG) << "This is not an addressee item."; 0102 } 0103 }); 0104 } 0105 } 0106 0107 void AddresseeWrapper::itemChanged(const Akonadi::Item &item) 0108 { 0109 setAddressee(item.payload<KContacts::Addressee>()); 0110 } 0111 0112 KContacts::Addressee AddresseeWrapper::addressee() const 0113 { 0114 return m_addressee; 0115 } 0116 0117 void AddresseeWrapper::setAddressee(const KContacts::Addressee &addressee) 0118 { 0119 m_addressee = addressee; 0120 m_addressesModel->setAddresses(addressee.addresses()); 0121 m_emailModel->loadContact(addressee); 0122 m_phoneModel->loadContact(addressee); 0123 m_imppModel->loadContact(addressee); 0124 notifyDataChanged(); 0125 } 0126 0127 QString AddresseeWrapper::uid() const 0128 { 0129 return m_addressee.uid(); 0130 } 0131 0132 Akonadi::Collection AddresseeWrapper::collection() const 0133 { 0134 return m_collection.isValid() ? m_collection : item().parentCollection(); 0135 } 0136 0137 qint64 AddresseeWrapper::collectionId() const 0138 { 0139 return collection().id(); 0140 } 0141 0142 void AddresseeWrapper::setCollection(Akonadi::Collection collection) 0143 { 0144 m_collection = collection; 0145 Q_EMIT collectionChanged(); 0146 } 0147 0148 QString AddresseeWrapper::formattedName() const 0149 { 0150 return m_addressee.formattedName(); 0151 } 0152 0153 void AddresseeWrapper::setFormattedName(const QString &name) 0154 { 0155 if (name == m_addressee.formattedName()) { 0156 return; 0157 } 0158 m_addressee.setNameFromString(name); 0159 Q_EMIT formattedNameChanged(); 0160 Q_EMIT givenNameChanged(); 0161 Q_EMIT familyNameChanged(); 0162 Q_EMIT suffixChanged(); 0163 Q_EMIT prefixChanged(); 0164 Q_EMIT additionalNameChanged(); 0165 } 0166 0167 QDateTime AddresseeWrapper::birthday() const 0168 { 0169 return m_addressee.birthday(); 0170 } 0171 0172 void AddresseeWrapper::setBirthday(const QDateTime &birthday) 0173 { 0174 if (birthday == m_addressee.birthday()) { 0175 return; 0176 } 0177 m_addressee.setBirthday(birthday); 0178 Q_EMIT birthdayChanged(); 0179 } 0180 0181 KContacts::PhoneNumber::List AddresseeWrapper::phoneNumbers() const 0182 { 0183 return m_addressee.phoneNumbers(); 0184 } 0185 0186 KContacts::Picture AddresseeWrapper::photo() const 0187 { 0188 return m_addressee.photo(); 0189 } 0190 0191 QString AddresseeWrapper::preferredEmail() const 0192 { 0193 return m_addressee.preferredEmail(); 0194 } 0195 0196 EmailModel *AddresseeWrapper::emailModel() const 0197 { 0198 return m_emailModel; 0199 } 0200 0201 PhoneModel *AddresseeWrapper::phoneModel() const 0202 { 0203 return m_phoneModel; 0204 } 0205 0206 QString AddresseeWrapper::qrCodeData() const 0207 { 0208 KContacts::VCardConverter converter; 0209 KContacts::Addressee addr(m_addressee); 0210 addr.setPhoto(KContacts::Picture()); 0211 addr.setLogo(KContacts::Picture()); 0212 return QString::fromUtf8(converter.createVCard(addr)); 0213 } 0214 0215 void AddresseeWrapper::updatePhoto(const KContacts::Picture &loadedPhoto) 0216 { 0217 m_addressee.setPhoto(loadedPhoto); 0218 Q_EMIT photoChanged(); 0219 } 0220 0221 KContacts::Picture AddresseeWrapper::preparePhoto(const QUrl &path) const 0222 { 0223 const QImage image(path.toLocalFile()); 0224 // Scale the photo down to make sure contacts are not taking too 0225 // long time to load their photos 0226 constexpr auto unscaledWidth = 200; 0227 const auto scaleFactor = dynamic_cast<QGuiApplication *>(QCoreApplication::instance())->devicePixelRatio(); 0228 const auto avatarSize = int(unscaledWidth * scaleFactor); 0229 const QSize size(avatarSize, avatarSize); 0230 0231 return KContacts::Picture(image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation)); 0232 } 0233 0234 QString AddresseeWrapper::note() const 0235 { 0236 return m_addressee.note(); 0237 } 0238 0239 QDateTime AddresseeWrapper::anniversary() const 0240 { 0241 return QDateTime(m_addressee.anniversary(), QTime()); 0242 } 0243 0244 QString AddresseeWrapper::spousesName() const 0245 { 0246 return m_addressee.spousesName(); 0247 } 0248 0249 QString AddresseeWrapper::organization() const 0250 { 0251 return m_addressee.organization(); 0252 } 0253 0254 QString AddresseeWrapper::profession() const 0255 { 0256 return m_addressee.profession(); 0257 } 0258 0259 QString AddresseeWrapper::title() const 0260 { 0261 return m_addressee.title(); 0262 } 0263 0264 QString AddresseeWrapper::department() const 0265 { 0266 return m_addressee.department(); 0267 } 0268 0269 QString AddresseeWrapper::office() const 0270 { 0271 return m_addressee.office(); 0272 } 0273 0274 QString AddresseeWrapper::managersName() const 0275 { 0276 return m_addressee.managersName(); 0277 } 0278 0279 QString AddresseeWrapper::assistantsName() const 0280 { 0281 return m_addressee.assistantsName(); 0282 } 0283 0284 void AddresseeWrapper::setNote(const QString ¬e) 0285 { 0286 if (note == m_addressee.note()) { 0287 return; 0288 } 0289 m_addressee.setNote(note); 0290 Q_EMIT noteChanged(); 0291 } 0292 0293 void AddresseeWrapper::setAnniversary(const QDateTime &anniversary) 0294 { 0295 if (anniversary.date() == m_addressee.anniversary()) { 0296 return; 0297 } 0298 m_addressee.setAnniversary(anniversary.date()); 0299 Q_EMIT anniversaryChanged(); 0300 } 0301 0302 void AddresseeWrapper::setSpousesName(const QString &spousesName) 0303 { 0304 if (spousesName == m_addressee.spousesName()) { 0305 return; 0306 } 0307 m_addressee.setSpousesName(spousesName); 0308 Q_EMIT spousesNameChanged(); 0309 } 0310 0311 void AddresseeWrapper::setOrganization(const QString &organization) 0312 { 0313 if (organization == m_addressee.organization()) { 0314 return; 0315 } 0316 m_addressee.setOrganization(organization); 0317 Q_EMIT organizationChanged(); 0318 } 0319 0320 void AddresseeWrapper::setProfession(const QString &profession) 0321 { 0322 if (profession == m_addressee.profession()) { 0323 return; 0324 } 0325 m_addressee.setProfession(profession); 0326 Q_EMIT professionChanged(); 0327 } 0328 0329 void AddresseeWrapper::setTitle(const QString &title) 0330 { 0331 if (title == m_addressee.title()) { 0332 return; 0333 } 0334 m_addressee.setTitle(title); 0335 Q_EMIT titleChanged(); 0336 } 0337 0338 void AddresseeWrapper::setDepartment(const QString &department) 0339 { 0340 if (department == m_addressee.department()) { 0341 return; 0342 } 0343 m_addressee.setDepartment(department); 0344 Q_EMIT departmentChanged(); 0345 } 0346 0347 void AddresseeWrapper::setOffice(const QString &office) 0348 { 0349 if (office == m_addressee.office()) { 0350 return; 0351 } 0352 m_addressee.setOffice(office); 0353 Q_EMIT officeChanged(); 0354 } 0355 0356 void AddresseeWrapper::setManagersName(const QString &managersName) 0357 { 0358 if (managersName == m_addressee.managersName()) { 0359 return; 0360 } 0361 m_addressee.setManagersName(managersName); 0362 Q_EMIT managersNameChanged(); 0363 } 0364 0365 void AddresseeWrapper::setAssistantsName(const QString &assistantsName) 0366 { 0367 if (assistantsName == m_addressee.assistantsName()) { 0368 return; 0369 } 0370 m_addressee.setAssistantsName(assistantsName); 0371 Q_EMIT assistantsNameChanged(); 0372 } 0373 0374 QString AddresseeWrapper::nickName() const 0375 { 0376 return m_addressee.nickName(); 0377 } 0378 0379 void AddresseeWrapper::setNickName(const QString &nickName) 0380 { 0381 if (nickName == m_addressee.nickName()) { 0382 return; 0383 } 0384 m_addressee.setNickName(nickName); 0385 Q_EMIT nickNameChanged(); 0386 } 0387 0388 QUrl AddresseeWrapper::blogFeed() const 0389 { 0390 return m_addressee.blogFeed(); 0391 } 0392 0393 void AddresseeWrapper::setBlogFeed(const QUrl &blogFeed) 0394 { 0395 if (blogFeed == m_addressee.blogFeed()) { 0396 return; 0397 } 0398 m_addressee.setBlogFeed(blogFeed); 0399 Q_EMIT blogFeedChanged(); 0400 } 0401 0402 AddresseeWrapper::DisplayType AddresseeWrapper::displayType() const 0403 { 0404 return m_displayType; 0405 } 0406 0407 void AddresseeWrapper::setDisplayType(AddresseeWrapper::DisplayType displayType) 0408 { 0409 if (m_displayType == displayType) { 0410 return; 0411 } 0412 m_displayType = displayType; 0413 Q_EMIT displayTypeChanged(); 0414 } 0415 0416 QString AddresseeWrapper::additionalName() const 0417 { 0418 return m_addressee.additionalName(); 0419 } 0420 0421 void AddresseeWrapper::setAdditionalName(const QString &name) 0422 { 0423 if (name == m_addressee.additionalName()) { 0424 return; 0425 } 0426 m_addressee.setAdditionalName(name); 0427 setFormattedName(m_addressee.assembledName()); 0428 Q_EMIT additionalNameChanged(); 0429 } 0430 0431 QString AddresseeWrapper::givenName() const 0432 { 0433 return m_addressee.givenName(); 0434 } 0435 0436 void AddresseeWrapper::setGivenName(const QString &name) 0437 { 0438 if (name == m_addressee.givenName()) { 0439 return; 0440 } 0441 m_addressee.setGivenName(name); 0442 setFormattedName(m_addressee.assembledName()); 0443 Q_EMIT givenNameChanged(); 0444 } 0445 0446 QString AddresseeWrapper::familyName() const 0447 { 0448 return m_addressee.familyName(); 0449 } 0450 0451 void AddresseeWrapper::setFamilyName(const QString &name) 0452 { 0453 if (name == m_addressee.familyName()) { 0454 return; 0455 } 0456 m_addressee.setFamilyName(name); 0457 setFormattedName(m_addressee.assembledName()); 0458 Q_EMIT familyNameChanged(); 0459 } 0460 0461 QString AddresseeWrapper::prefix() const 0462 { 0463 return m_addressee.prefix(); 0464 } 0465 0466 void AddresseeWrapper::setPrefix(const QString &name) 0467 { 0468 if (name == m_addressee.prefix()) { 0469 return; 0470 } 0471 m_addressee.setPrefix(name); 0472 setFormattedName(m_addressee.assembledName()); 0473 Q_EMIT prefixChanged(); 0474 } 0475 0476 QString AddresseeWrapper::suffix() const 0477 { 0478 return m_addressee.suffix(); 0479 } 0480 0481 void AddresseeWrapper::setSuffix(const QString &name) 0482 { 0483 if (name == m_addressee.suffix()) { 0484 return; 0485 } 0486 m_addressee.setSuffix(name); 0487 setFormattedName(m_addressee.assembledName()); 0488 Q_EMIT suffixChanged(); 0489 } 0490 0491 #include "moc_addresseewrapper.cpp"