Skip to content

Commit

Permalink
Refactor own identity into its own class to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
dragotin committed Oct 15, 2024
1 parent 1180472 commit 82f1638
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 348 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ set(kraft_SRCS
reportitemlist.cpp
reportitem.cpp
docidentgenerator.cpp
myidentity.cpp
3rdparty/qrcodegen.cpp
)

Expand Down
171 changes: 171 additions & 0 deletions src/myidentity.cpp
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();
}
81 changes: 81 additions & 0 deletions src/myidentity.h
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
78 changes: 6 additions & 72 deletions src/portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "ui_dbtoxml.h"
#include "dbtoxmlconverter.h"
#include "xmldocindex.h"
#include "myidentity.h"

// Litte class diagram to describe the main view of Kraft:
//
Expand Down Expand Up @@ -482,57 +483,8 @@ void Portal::slotStartupChecks()
}

// Fetch my address
const QString myUid = KraftSettings::self()->userUid();
bool useManual = false;

if( ! myUid.isEmpty() ) {
slotStatusMsg( i18n( "Fetching user address data" ) );
KContacts::Addressee contact;
// qDebug () << "Got My UID: " << myUid;
connect( mAddressProvider, SIGNAL( lookupResult(QString,KContacts::Addressee)),
this, SLOT( slotReceivedMyAddress(QString, KContacts::Addressee)) );

AddressProvider::LookupState state = mAddressProvider->lookupAddressee( myUid );
switch( state ) {
case AddressProvider::LookupFromCache:
contact = mAddressProvider->getAddresseeFromCache(myUid);
slotReceivedMyAddress(myUid, contact);
break;
case AddressProvider::LookupNotFound:
case AddressProvider::ItemError:
case AddressProvider::BackendError:
// Try to read from stored vcard.
useManual = true;
break;
case AddressProvider::LookupOngoing:
case AddressProvider::LookupStarted:
// Not much to do, just wait
break;
}
} else {
// in case there is no uid in the settings file, try to use the manual address.
useManual = true;
}

if( useManual ) {
// check if the vcard can be read
QString file = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation );
file += "/myidentity.vcd";
QFile f(file);
if( f.exists() ) {
if( f.open( QIODevice::ReadOnly )) {
const QByteArray data = f.readAll();
VCardConverter converter;
Addressee::List list = converter.parseVCards( data );

if( list.count() > 0 ) {
KContacts::Addressee c = list.at(0);
c.insertCustom(CUSTOM_ADDRESS_MARKER, "manual");
slotReceivedMyAddress(QString(), c);
}
}
}
}
connect(&_myIdentity, &MyIdentity::myIdentityLoaded, this, &Portal::slotReceivedMyAddress);
_myIdentity.load();

connect( &_reportGenerator, &ReportGenerator::docAvailable,
this, &Portal::slotDocConverted);
Expand All @@ -543,9 +495,6 @@ void Portal::slotStartupChecks()

void Portal::slotReceivedMyAddress( const QString& uid, const KContacts::Addressee& contact )
{
disconnect( mAddressProvider, SIGNAL(lookupResult(QString,KContacts::Addressee)),
this, SLOT(slotReceivedMyAddress(QString, KContacts::Addressee)));

if( contact.isEmpty() ) {
if( !uid.isEmpty() ) {
// FIXME: Read the stored Address and compare the uid
Expand All @@ -555,12 +504,7 @@ void Portal::slotReceivedMyAddress( const QString& uid, const KContacts::Address
return;
}

myContact = contact;

// qDebug () << "Received my address: " << contact.realName() << "(" << uid << ")";
_reportGenerator.setMyContact( myContact );

QString name = myContact.formattedName();
QString name = contact.formattedName();
if( !name.isEmpty() ) {
name = i18n("Welcome to Kraft, %1", name);
statusBar()->showMessage(name, 30*1000);
Expand Down Expand Up @@ -1396,20 +1340,10 @@ QString Portal::textWrap( const QString& t, int width, int maxLines )
void Portal::preferences()
{
_prefsDialog = new PrefsDialog(this);
connect( _prefsDialog, SIGNAL(finished(int)), SLOT(slotPrefsDialogFinished(int)) );
connect( _prefsDialog, SIGNAL(newOwnIdentity(const QString&, KContacts::Addressee)),
SLOT(slotReceivedMyAddress(QString,KContacts::Addressee)));
_prefsDialog->setMyIdentity( myContact, mAddressProvider->backendUp() );

_prefsDialog->open();
}

void Portal::slotPrefsDialogFinished( int result )
{
if( result == QDialog::Accepted) {
_prefsDialog->setMyIdentity(&_myIdentity);

}
_prefsDialog->deleteLater();
_prefsDialog->open();
}

void Portal::slotHandbook()
Expand Down
Loading

0 comments on commit 82f1638

Please sign in to comment.