-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor own identity into its own class to simplify code
- Loading branch information
Showing
10 changed files
with
467 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/*************************************************************************** | ||
myidentity.h | ||
------------------- | ||
begin : Oct. 2024 | ||
copyright : (C) 2024 by Klaas Freitag | ||
email : [email protected] | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "myidentity.h" | ||
#include "kraftsettings.h" | ||
#include "addressprovider.h" | ||
|
||
#include <KLocalizedString> | ||
#include <QFile> | ||
|
||
#include <kcontacts/resourcelocatorurl.h> | ||
#include <kcontacts/vcardconverter.h> | ||
|
||
KContacts::Addressee MyIdentity::_myContact = KContacts::Addressee(); | ||
|
||
MyIdentity::MyIdentity(QObject *parent) | ||
: QObject{parent}, | ||
_addressProvider{nullptr} | ||
{ | ||
|
||
} | ||
|
||
KContacts::Addressee MyIdentity::UIToAddressee(Ui::manualOwnIdentity ui) | ||
{ | ||
KContacts::Addressee add; | ||
add.setFormattedName(ui.leName->text()); | ||
add.setOrganization(ui.leOrganization->text()); | ||
KContacts::Address workAddress; | ||
|
||
workAddress.setStreet(ui.leStreet->text()); | ||
workAddress.setPostalCode(ui.lePostcode->text()); | ||
workAddress.setLocality(ui.leCity->text()); | ||
workAddress.setType(KContacts::Address::Work); | ||
add.insertAddress(workAddress); | ||
|
||
add.insertPhoneNumber(KContacts::PhoneNumber(ui.lePhone->text(), KContacts::PhoneNumber::Work)); | ||
add.insertPhoneNumber(KContacts::PhoneNumber(ui.leFax->text(), KContacts::PhoneNumber::Fax)); | ||
add.insertPhoneNumber(KContacts::PhoneNumber(ui.leMobile->text(), KContacts::PhoneNumber::Cell)); | ||
KContacts::ResourceLocatorUrl resUrl; | ||
resUrl.setUrl(QUrl(ui.leWebsite->text())); | ||
add.setUrl(resUrl); | ||
|
||
KContacts::Email email; | ||
email.setEmail(ui.leEmail->text()); | ||
email.setPreferred(true); | ||
email.setType(KContacts::Email::TypeFlag::Work); | ||
add.addEmail(email); | ||
|
||
return add; | ||
} | ||
|
||
QString MyIdentity::identityFile() | ||
{ | ||
QString file = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ); | ||
file += "/myidentity.vcd"; | ||
|
||
return file; | ||
} | ||
|
||
void MyIdentity::load() | ||
{ | ||
// Fetch my address | ||
const QString myUid = KraftSettings::self()->userUid(); | ||
_addressProvider = new AddressProvider(this); | ||
connect(_addressProvider, &AddressProvider::lookupResult, | ||
this, &MyIdentity::slotAddresseeFound); | ||
|
||
_myContact = KContacts::Addressee(); | ||
|
||
KContacts::Addressee contact; | ||
if( ! myUid.isEmpty() ) { | ||
_source = Source::Backend; | ||
// qDebug () << "Got My UID: " << myUid; | ||
AddressProvider::LookupState state = _addressProvider->lookupAddressee( myUid ); | ||
switch( state ) { | ||
case AddressProvider::LookupFromCache: | ||
contact = _addressProvider->getAddresseeFromCache(myUid); | ||
break; | ||
case AddressProvider::LookupNotFound: | ||
case AddressProvider::ItemError: | ||
case AddressProvider::BackendError: | ||
// Try to read from stored vcard. | ||
break; | ||
case AddressProvider::LookupOngoing: | ||
case AddressProvider::LookupStarted: | ||
// Not much to do, just wait for the signal to come in | ||
break; | ||
} | ||
} else { | ||
// check if the vcard can be read | ||
_source = Source::Manual; | ||
const QString file = identityFile(); | ||
QFile f(file); | ||
if( f.exists() ) { | ||
if( f.open( QIODevice::ReadOnly )) { | ||
const QByteArray data = f.readAll(); | ||
KContacts::VCardConverter converter; | ||
KContacts::Addressee::List list = converter.parseVCards( data ); | ||
|
||
if( list.count() > 0 ) { | ||
contact = list.at(0); | ||
contact.insertCustom(CUSTOM_ADDRESS_MARKER, "manual"); | ||
} | ||
} | ||
} | ||
slotAddresseeFound(myUid, contact); | ||
} | ||
} | ||
|
||
void MyIdentity::slotAddresseeFound(const QString& uid, const KContacts::Addressee& contact) | ||
{ | ||
_myContact = contact; | ||
emit myIdentityLoaded(uid, contact); | ||
} | ||
|
||
KContacts::Addressee MyIdentity::contact() const | ||
{ | ||
return _myContact; | ||
} | ||
|
||
MyIdentity::Source MyIdentity::source() const | ||
{ | ||
return _source; | ||
} | ||
|
||
void MyIdentity::save(const QString& uuid, const KContacts::Addressee& contact) | ||
{ | ||
const QString file{identityFile()}; | ||
|
||
const QString myUid = KraftSettings::self()->userUid(); | ||
if (!uuid.isEmpty() && myUid == uuid) { | ||
// nothing has changed | ||
return; | ||
} | ||
|
||
if (uuid.isEmpty()) { // save the manual address | ||
KContacts::VCardConverter vcc; | ||
const QByteArray vcard = vcc.createVCard(contact); | ||
|
||
QFile f ( file ); | ||
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
f.write(vcard); | ||
f.close(); | ||
qDebug() << "Saved own identity to " << file; | ||
} | ||
} else { | ||
QFile::remove(file); // remove a maybe existing file | ||
} | ||
|
||
// emit the signal for consumers of the address | ||
slotAddresseeFound(uuid, contact); | ||
|
||
// update the settings - clear the user name as it is deprecated anyway | ||
KraftSettings::self()->setUserName(QString()); | ||
KraftSettings::self()->setUserUid(uuid); | ||
KraftSettings::self()->save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*************************************************************************** | ||
myidentity.h | ||
------------------- | ||
begin : Oct. 2024 | ||
copyright : (C) 2024 by Klaas Freitag | ||
email : [email protected] | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef MYIDENTITY_H | ||
#define MYIDENTITY_H | ||
|
||
#include <QObject> | ||
|
||
#include <KContacts/Addressee> | ||
|
||
#include "ui_identity.h" | ||
|
||
class AddressProvider; | ||
|
||
/** | ||
* @brief The MyIdentity class | ||
* | ||
* The identity can be stored in two different ways: | ||
* 1. There is just a UUID in the settings file stored under userUid(), | ||
* which contains the id under which the own identify can be found in | ||
* the addressbook through the backend. | ||
* 2. If the id is non existant or empty, the identity is read from a file | ||
* stored in a specific path. It is written by the prefsdialog. | ||
*/ | ||
|
||
class MyIdentity : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit MyIdentity(QObject *parent = nullptr); | ||
|
||
enum class Source { | ||
Unknown, | ||
Manual, | ||
Backend | ||
}; | ||
|
||
static KContacts::Addressee UIToAddressee(Ui::manualOwnIdentity ui); | ||
|
||
void load(); | ||
|
||
// One of the parameters need to be empty when calling this method | ||
void save(const QString& uuid, const KContacts::Addressee& contact = KContacts::Addressee()); | ||
|
||
QString identityFile(); | ||
|
||
// returns the addressee that was found on the last attemt to look up the own identity. | ||
// If there was no call to load before, the returned addressee is obviously empty. | ||
KContacts::Addressee contact() const; | ||
|
||
MyIdentity::Source source() const; | ||
|
||
signals: | ||
|
||
// final signal after the contact could be loaded | ||
void myIdentityLoaded(const QString& uuid, const KContacts::Addressee& contact); | ||
|
||
private slots: | ||
void slotAddresseeFound(const QString& uid, const KContacts::Addressee &contact); | ||
|
||
private: | ||
AddressProvider *_addressProvider; | ||
static KContacts::Addressee _myContact; | ||
Source _source; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.