Skip to content

Commit

Permalink
Own Identity: Let the user enter the own identity manually
Browse files Browse the repository at this point in the history
... in case the backend is not working.

Useful on Safari and for AppImage builds.
  • Loading branch information
dragotin committed Jan 12, 2018
1 parent 8792794 commit db9796c
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/addressprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void AddressProvider::slotAddresseeFound( const QString& uid, const KContacts::A
_errMessages.remove(uid);
}
_addressCache[uid] = contact;

emit lookupResult(uid, contact);
_addressCache[uid].insertCustom(CUSTOM_ADDRESS_MARKER, "addressbook");
emit lookupResult(uid, _addressCache[uid]);
}

void AddressProvider::slotAddresseeNotFound( const QString& uid )
Expand Down
3 changes: 3 additions & 0 deletions src/addressprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include <kjob.h>

// use define CUSTOM_ADDRESS_MARKER to mark the origin of addresses with .insertCustom
#define CUSTOM_ADDRESS_MARKER "kraft", "identity_source"

class AddressProviderPrivate;


Expand Down
1 change: 1 addition & 0 deletions src/addressselectordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ AddressSelectorDialog::AddressSelectorDialog( QWidget *parent )
void AddressSelectorDialog::slotAddresseeSelected( const KContacts::Addressee& addressee )
{
_addressee = addressee;
_addressee.insertCustom(CUSTOM_ADDRESS_MARKER, "addressbook");
}

KContacts::Addressee AddressSelectorDialog::addressee()
Expand Down
29 changes: 17 additions & 12 deletions src/portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,15 @@ void Portal::slotStartupChecks()

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

if( ! myUid.isEmpty() ) {
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 );
KContacts::Addressee contact;
switch( state ) {
case AddressProvider::LookupFromCache:
contact = mAddressProvider->getAddresseeFromCache(myUid);
Expand All @@ -329,27 +331,33 @@ void Portal::slotStartupChecks()
case AddressProvider::ItemError:
case AddressProvider::BackendError:
// Try to read from stored vcard.
slotReceivedMyAddress(myUid, contact);
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 )) {
QByteArray data = f.readAll();

const QByteArray data = f.readAll();
VCardConverter converter;
Addressee::List list = converter.parseVCards( data );

if( list.count() > 0 ) {
slotReceivedMyAddress(QString::null, list.at(0));
KContacts::Addressee c = list.at(0);
c.insertCustom(CUSTOM_ADDRESS_MARKER, "manual");
slotReceivedMyAddress(QString::null, c);
}
}
}
Expand All @@ -366,21 +374,18 @@ void Portal::slotReceivedMyAddress( const QString& uid, const KContacts::Address

if( contact.isEmpty() ) {
if( !uid.isEmpty() ) {
// FIXME: Read the stored Address and compare the uid
const QString err = mAddressProvider->errorMsg(uid);
qDebug () << "My-Contact is empty: " << err;
qDebug () << "My-Contact could not be found:" << err;
}
return;
}

myContact = contact;

if( !uid.isEmpty() ) {
KraftSettings::self()->setUserUid( contact.uid() );
KraftSettings::self()->writeConfig();
}

// qDebug () << "Received my address: " << contact.realName() << "(" << uid << ")";
ReportGenerator::self()->setMyContact( contact );
ReportGenerator::self()->setMyContact( myContact );

QString name = myContact.formattedName();
if( !name.isEmpty() ) {
name = i18n("Welcome to Kraft, %1").arg(name);
Expand Down
160 changes: 128 additions & 32 deletions src/prefsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
#include "texttemplate.h"
#include "htmlview.h"
#include "addressselectordialog.h"
#include "addressprovider.h"

#include "kcontacts/vcardconverter.h"

// ################################################################################

Expand Down Expand Up @@ -169,40 +171,53 @@ void PrefsDialog::whoIsMeTab()
topFrame->setIcon(QIcon::fromTheme( "user-identity" ) );

QVBoxLayout *vboxLay = new QVBoxLayout;
// vboxLay->setSpacing( spacingHint() );

QLabel *label;
label = new QLabel(i18n("Select the identity of the sending entity of documents. That's <b>your companies</b> address."));
QLabel *label = new QLabel(i18n("Select the identity of the sending entity of documents. That's <b>your companies</b> address."));
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
vboxLay->addWidget( label );

QVBoxLayout *butLay = new QVBoxLayout;
butLay->addStretch( 1 );
_tabWidget = new QTabWidget;
vboxLay->addWidget(_tabWidget);

// == Tab that displays the Addressbook widget
QWidget *w = new QWidget;
QVBoxLayout *t1Lay = new QVBoxLayout;
mIdentityView = new HtmlView;
QString home = QString::fromLatin1(qgetenv("KRAFT_HOME"));
QString idFile = QString("%1/reports/images/identity.png").arg(home);
QFileInfo fi(idFile);
mIdentityView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);

if(! fi.exists() ) {
QString idFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kraft/reports/images/identity.png" );
fi.setFile(idFile);
fi.refresh();

if( fi.exists() ) {
idFile = fi.path();
}
} else {
idFile = fi.path();
const QString idFile = DefaultProvider::self()->locateFile("reports/images/identity.png");
if( !idFile.isEmpty() ) {
QFileInfo fi(idFile);
mIdentityView->setBaseUrl(fi.path());
}
mIdentityView->setBaseUrl(idFile);

butLay->addWidget(mIdentityView);
t1Lay->addWidget(mIdentityView);
QHBoxLayout *butLay = new QHBoxLayout;
butLay->addStretch( 1 );
_pbChangeIdentity = new QPushButton(i18n("Select Identity..."));
connect( _pbChangeIdentity, SIGNAL(clicked()), SLOT(slotChangeIdentity()) );
butLay->addWidget(_pbChangeIdentity);

vboxLay->addLayout( butLay );
t1Lay->addLayout( butLay );

w->setLayout(t1Lay);
_tabWidget->insertTab(0, w, i18n("From AddressBook"));

// == Tab that displays the manual widget
QWidget *w1 = new QWidget;
ui.setupUi(w1);
_tabWidget->insertTab(1, w1, QIcon(), i18n("Manual Entry"));
ui.nameLabel->setText( KContacts::Addressee::formattedNameLabel() );
ui.orgLabel->setText( KContacts::Addressee::organizationLabel());
ui.streetLabel->setText(KContacts::Addressee::businessAddressStreetLabel());
ui.postCodeLabel->setText(KContacts::Addressee::businessAddressPostalCodeLabel());
ui.cityLabel->setText(KContacts::Addressee::businessAddressLocalityLabel());
ui.phoneLabel->setText(KContacts::Addressee::businessPhoneLabel());
ui.faxLabel->setText(KContacts::Addressee::businessFaxLabel());
ui.mobileLabel->setText(KContacts::Addressee::mobilePhoneLabel());
ui.emailLabel->setText(KContacts::Addressee::emailLabel());
ui.websiteLabel->setText(KContacts::Addressee::urlLabel());

_tabWidget->insertTab(1, w1, i18n("Manual Address"));

topWidget->setLayout( vboxLay );

Expand All @@ -213,10 +228,9 @@ void PrefsDialog::slotChangeIdentity()
AddressSelectorDialog dialog(this);

if( dialog.exec() ) {
Addressee identity = dialog.addressee();
if( ! identity.isEmpty() ) {
setMyIdentity(identity, true);
emit newOwnIdentity(identity.uid(), identity);
_newIdentity = dialog.addressee();
if( ! _newIdentity.isEmpty() ) {
setMyIdentity(_newIdentity, true);
}
}
}
Expand Down Expand Up @@ -361,6 +375,58 @@ void PrefsDialog::readConfig()
mCbDefaultTaxType->setCurrentIndex( KraftSettings::self()->defaultTaxType()-1 );
}

void PrefsDialog::writeIdentity()
{
/*
* Save either the manually added address, or the Addressbook-ID
* If the user fills in the manual form, the addressbook ID is removed.
* FIXME: The handling of the ownIdentity should be refactored to its
* own class.
*/

if(_tabWidget->currentIndex() == 1 /* manually entered */ ) {
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(PhoneNumber(ui.lePhone->text(), KContacts::PhoneNumber::Work));
add.insertPhoneNumber(PhoneNumber(ui.leFax->text(), KContacts::PhoneNumber::Fax));
add.insertPhoneNumber(PhoneNumber(ui.leMobile->text(), KContacts::PhoneNumber::Cell));
ResourceLocatorUrl resUrl;
resUrl.setUrl(QUrl(ui.leWebsite->text()));
add.setUrl(resUrl);
add.insertEmail(ui.leEmail->text(), true /* prefered */ );

VCardConverter vcc;
QByteArray vcard = vcc.createVCard(add);

QString file = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation );
file += "/myidentity.vcd";
QFile f ( file );
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
f.write(vcard);
f.close();
qDebug() << "Saved own identity to " << file;

KraftSettings::self()->setUserName( QString::null );
KraftSettings::self()->setUserUid( QString::null );
KraftSettings::self()->save();
}
} else { /* AddressBook */
KraftSettings::self()->setUserName( _newIdentity.name() );
KraftSettings::self()->setUserUid( _newIdentity.uid() );
KraftSettings::self()->save();
}
emit newOwnIdentity(_newIdentity.uid(), _newIdentity);
}

void PrefsDialog::writeConfig()
{
KraftSettings::self()->setShowDocumentLocale( mCbDocLocale->isChecked() );
Expand All @@ -386,12 +452,28 @@ void PrefsDialog::accept()
mPrefsUnits->save();
writeTaxes();
writeConfig();
writeIdentity();
QDialog::accept();
}

#define IDENTITY_TAG(X) QLatin1String(X)
#define QL1(X) QLatin1String(X)

void PrefsDialog::fillManualIdentityForm(const KContacts::Addressee& addressee)
{
ui.leName->setText(addressee.formattedName());
ui.leStreet->setText(addressee.address(Address::Work).street());
ui.leCity->setText(addressee.address(Address::Work).locality());
ui.lePostcode->setText(addressee.address(Address::Work).postalCode());

ui.leEmail->setText(addressee.preferredEmail());
ui.leFax->setText(addressee.phoneNumber(PhoneNumber::Fax).number());
ui.leOrganization->setText(addressee.organization());
ui.lePhone->setText(addressee.phoneNumber(PhoneNumber::Work).number());
ui.leMobile->setText(addressee.phoneNumber(PhoneNumber::Cell).number());
ui.leWebsite->setText(addressee.url().url().toDisplayString());
}

void PrefsDialog::setMyIdentity( const KContacts::Addressee& addressee, bool backendUp )
{
// Note: This code is stolen from DocDigestDetailView::slotShowDocDetails
Expand All @@ -415,14 +497,28 @@ void PrefsDialog::setMyIdentity( const KContacts::Addressee& addressee, bool bac
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft Addressbook Integration down.</b></p>"
"<p>The address book backend is not up and running.</p>"
"<p>Please check your addressbook integration setup.</p>"));
}

if( addressee.isEmpty() ) {
addressBookInfo = i18n("The identity is not listed in an address book.");
tmpl.createDictionary(QL1("NO_IDENTITY"));
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft does not know your identity.</b></p>"
"<p>Please pick one from the address books by clicking on the Button below.</p>"
"<p>Not having an identity selected can make your documents look incomplete.</p>"));
} else {
if( addressee.isEmpty() ) {
addressBookInfo = i18n("The identity is not listed in an address book.");
tmpl.createDictionary(QL1("NO_IDENTITY"));
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft does not know your identity.</b></p>"
"<p>Please pick one from the address books by clicking on the Button below.</p>"
"<p>Not having an identity selected can make your documents look incomplete.</p>"));
const QString origin = addressee.custom( CUSTOM_ADDRESS_MARKER );
if( origin.isEmpty() || origin == "manual") {
// it is an manually added address.
fillManualIdentityForm(addressee);
_tabWidget->setTabIcon(1, QIcon::fromTheme("checkmark"));
_tabWidget->setTabIcon(0, QIcon());
_tabWidget->setCurrentIndex(1);
} else {
_tabWidget->setTabIcon(0, QIcon::fromTheme("checkmark"));
_tabWidget->setTabIcon(1, QIcon());
_tabWidget->setCurrentIndex(0);

// it is an address from the address book
addressBookInfo = i18n("Your identity can be found in the address books.");
tmpl.createDictionary(QL1("IDENTITY"));
tmpl.setValue( QL1("IDENTITY"), IDENTITY_TAG("IDENTITY_NAME"), addressee.realName() );
Expand Down
9 changes: 8 additions & 1 deletion src/prefsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <kpagedialog.h>
#include <QItemDelegate>

#include "ui_identity.h"

#include "doctypeedit.h"
#include "doctype.h"
#include "taxeditdialog.h"
Expand Down Expand Up @@ -80,7 +82,9 @@ protected slots:
void wagesTab();
void unitsTab();
void writeTaxes();
void writeIdentity();
void whoIsMeTab();
void fillManualIdentityForm(const KContacts::Addressee& addressee);

QComboBox *m_databaseDriver;
QLineEdit *m_leHost;
Expand All @@ -106,11 +110,14 @@ protected slots:
PrefsWages *mPrefsWages;
PrefsUnits *mPrefsUnits;

KContacts::Addressee _newIdentity;

QPushButton *mDelTax;
ImpTreeView *mTaxTreeView;
QSqlTableModel *mTaxModel;
HtmlView *mIdentityView;

QTabWidget *_tabWidget;
Ui::manualOwnIdentity ui;
};

class TaxItemDelegate : public QItemDelegate
Expand Down
2 changes: 2 additions & 0 deletions src/reportgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ void ReportGenerator::setMyContact( const KContacts::Addressee& contact )

void ReportGenerator::slotAddresseeSearchFinished( int )
{
qDebug() << "** Reached slotAddresseeSearchFinished!";

// now the addressee search through the address provider is finished.
// Rendering can be started.
QString tmplFile = findTemplate( mArchDoc->docType() );
Expand Down

0 comments on commit db9796c

Please sign in to comment.