Skip to content

Commit db9796c

Browse files
committed
Own Identity: Let the user enter the own identity manually
... in case the backend is not working. Useful on Safari and for AppImage builds.
1 parent 8792794 commit db9796c

7 files changed

+161
-47
lines changed

src/addressprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void AddressProvider::slotAddresseeFound( const QString& uid, const KContacts::A
5555
_errMessages.remove(uid);
5656
}
5757
_addressCache[uid] = contact;
58-
59-
emit lookupResult(uid, contact);
58+
_addressCache[uid].insertCustom(CUSTOM_ADDRESS_MARKER, "addressbook");
59+
emit lookupResult(uid, _addressCache[uid]);
6060
}
6161

6262
void AddressProvider::slotAddresseeNotFound( const QString& uid )

src/addressprovider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#include <kjob.h>
2727

28+
// use define CUSTOM_ADDRESS_MARKER to mark the origin of addresses with .insertCustom
29+
#define CUSTOM_ADDRESS_MARKER "kraft", "identity_source"
30+
2831
class AddressProviderPrivate;
2932

3033

src/addressselectordialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ AddressSelectorDialog::AddressSelectorDialog( QWidget *parent )
5151
void AddressSelectorDialog::slotAddresseeSelected( const KContacts::Addressee& addressee )
5252
{
5353
_addressee = addressee;
54+
_addressee.insertCustom(CUSTOM_ADDRESS_MARKER, "addressbook");
5455
}
5556

5657
KContacts::Addressee AddressSelectorDialog::addressee()

src/portal.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,15 @@ void Portal::slotStartupChecks()
313313

314314
// Fetch my address
315315
const QString myUid = KraftSettings::self()->userUid();
316+
bool useManual = false;
317+
316318
if( ! myUid.isEmpty() ) {
319+
KContacts::Addressee contact;
317320
// qDebug () << "Got My UID: " << myUid;
318321
connect( mAddressProvider, SIGNAL( lookupResult(QString,KContacts::Addressee)),
319322
this, SLOT( slotReceivedMyAddress(QString, KContacts::Addressee)) );
320323

321324
AddressProvider::LookupState state = mAddressProvider->lookupAddressee( myUid );
322-
KContacts::Addressee contact;
323325
switch( state ) {
324326
case AddressProvider::LookupFromCache:
325327
contact = mAddressProvider->getAddresseeFromCache(myUid);
@@ -329,27 +331,33 @@ void Portal::slotStartupChecks()
329331
case AddressProvider::ItemError:
330332
case AddressProvider::BackendError:
331333
// Try to read from stored vcard.
332-
slotReceivedMyAddress(myUid, contact);
334+
useManual = true;
333335
break;
334336
case AddressProvider::LookupOngoing:
335337
case AddressProvider::LookupStarted:
336338
// Not much to do, just wait
337339
break;
338340
}
339341
} else {
342+
// in case there is no uid in the settings file, try to use the manual address.
343+
useManual = true;
344+
}
345+
346+
if( useManual ) {
340347
// check if the vcard can be read
341348
QString file = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation );
342349
file += "/myidentity.vcd";
343350
QFile f(file);
344351
if( f.exists() ) {
345352
if( f.open( QIODevice::ReadOnly )) {
346-
QByteArray data = f.readAll();
347-
353+
const QByteArray data = f.readAll();
348354
VCardConverter converter;
349355
Addressee::List list = converter.parseVCards( data );
350356

351357
if( list.count() > 0 ) {
352-
slotReceivedMyAddress(QString::null, list.at(0));
358+
KContacts::Addressee c = list.at(0);
359+
c.insertCustom(CUSTOM_ADDRESS_MARKER, "manual");
360+
slotReceivedMyAddress(QString::null, c);
353361
}
354362
}
355363
}
@@ -366,21 +374,18 @@ void Portal::slotReceivedMyAddress( const QString& uid, const KContacts::Address
366374

367375
if( contact.isEmpty() ) {
368376
if( !uid.isEmpty() ) {
377+
// FIXME: Read the stored Address and compare the uid
369378
const QString err = mAddressProvider->errorMsg(uid);
370-
qDebug () << "My-Contact is empty: " << err;
379+
qDebug () << "My-Contact could not be found:" << err;
371380
}
372381
return;
373382
}
374383

375384
myContact = contact;
376385

377-
if( !uid.isEmpty() ) {
378-
KraftSettings::self()->setUserUid( contact.uid() );
379-
KraftSettings::self()->writeConfig();
380-
}
381-
382386
// qDebug () << "Received my address: " << contact.realName() << "(" << uid << ")";
383-
ReportGenerator::self()->setMyContact( contact );
387+
ReportGenerator::self()->setMyContact( myContact );
388+
384389
QString name = myContact.formattedName();
385390
if( !name.isEmpty() ) {
386391
name = i18n("Welcome to Kraft, %1").arg(name);

src/prefsdialog.cpp

Lines changed: 128 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
#include "texttemplate.h"
5353
#include "htmlview.h"
5454
#include "addressselectordialog.h"
55+
#include "addressprovider.h"
5556

57+
#include "kcontacts/vcardconverter.h"
5658

5759
// ################################################################################
5860

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

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

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

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

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

187-
if(! fi.exists() ) {
188-
QString idFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kraft/reports/images/identity.png" );
189-
fi.setFile(idFile);
190-
fi.refresh();
191-
192-
if( fi.exists() ) {
193-
idFile = fi.path();
194-
}
195-
} else {
196-
idFile = fi.path();
188+
const QString idFile = DefaultProvider::self()->locateFile("reports/images/identity.png");
189+
if( !idFile.isEmpty() ) {
190+
QFileInfo fi(idFile);
191+
mIdentityView->setBaseUrl(fi.path());
197192
}
198-
mIdentityView->setBaseUrl(idFile);
199193

200-
butLay->addWidget(mIdentityView);
194+
t1Lay->addWidget(mIdentityView);
195+
QHBoxLayout *butLay = new QHBoxLayout;
196+
butLay->addStretch( 1 );
201197
_pbChangeIdentity = new QPushButton(i18n("Select Identity..."));
202198
connect( _pbChangeIdentity, SIGNAL(clicked()), SLOT(slotChangeIdentity()) );
203199
butLay->addWidget(_pbChangeIdentity);
204-
205-
vboxLay->addLayout( butLay );
200+
t1Lay->addLayout( butLay );
201+
202+
w->setLayout(t1Lay);
203+
_tabWidget->insertTab(0, w, i18n("From AddressBook"));
204+
205+
// == Tab that displays the manual widget
206+
QWidget *w1 = new QWidget;
207+
ui.setupUi(w1);
208+
_tabWidget->insertTab(1, w1, QIcon(), i18n("Manual Entry"));
209+
ui.nameLabel->setText( KContacts::Addressee::formattedNameLabel() );
210+
ui.orgLabel->setText( KContacts::Addressee::organizationLabel());
211+
ui.streetLabel->setText(KContacts::Addressee::businessAddressStreetLabel());
212+
ui.postCodeLabel->setText(KContacts::Addressee::businessAddressPostalCodeLabel());
213+
ui.cityLabel->setText(KContacts::Addressee::businessAddressLocalityLabel());
214+
ui.phoneLabel->setText(KContacts::Addressee::businessPhoneLabel());
215+
ui.faxLabel->setText(KContacts::Addressee::businessFaxLabel());
216+
ui.mobileLabel->setText(KContacts::Addressee::mobilePhoneLabel());
217+
ui.emailLabel->setText(KContacts::Addressee::emailLabel());
218+
ui.websiteLabel->setText(KContacts::Addressee::urlLabel());
219+
220+
_tabWidget->insertTab(1, w1, i18n("Manual Address"));
206221

207222
topWidget->setLayout( vboxLay );
208223

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

215230
if( dialog.exec() ) {
216-
Addressee identity = dialog.addressee();
217-
if( ! identity.isEmpty() ) {
218-
setMyIdentity(identity, true);
219-
emit newOwnIdentity(identity.uid(), identity);
231+
_newIdentity = dialog.addressee();
232+
if( ! _newIdentity.isEmpty() ) {
233+
setMyIdentity(_newIdentity, true);
220234
}
221235
}
222236
}
@@ -361,6 +375,58 @@ void PrefsDialog::readConfig()
361375
mCbDefaultTaxType->setCurrentIndex( KraftSettings::self()->defaultTaxType()-1 );
362376
}
363377

378+
void PrefsDialog::writeIdentity()
379+
{
380+
/*
381+
* Save either the manually added address, or the Addressbook-ID
382+
* If the user fills in the manual form, the addressbook ID is removed.
383+
* FIXME: The handling of the ownIdentity should be refactored to its
384+
* own class.
385+
*/
386+
387+
if(_tabWidget->currentIndex() == 1 /* manually entered */ ) {
388+
KContacts::Addressee add;
389+
add.setFormattedName(ui.leName->text());
390+
add.setOrganization(ui.leOrganization->text());
391+
KContacts::Address workAddress;
392+
393+
workAddress.setStreet(ui.leStreet->text());
394+
workAddress.setPostalCode(ui.lePostcode->text());
395+
workAddress.setLocality(ui.leCity->text());
396+
workAddress.setType(KContacts::Address::Work);
397+
add.insertAddress(workAddress);
398+
399+
add.insertPhoneNumber(PhoneNumber(ui.lePhone->text(), KContacts::PhoneNumber::Work));
400+
add.insertPhoneNumber(PhoneNumber(ui.leFax->text(), KContacts::PhoneNumber::Fax));
401+
add.insertPhoneNumber(PhoneNumber(ui.leMobile->text(), KContacts::PhoneNumber::Cell));
402+
ResourceLocatorUrl resUrl;
403+
resUrl.setUrl(QUrl(ui.leWebsite->text()));
404+
add.setUrl(resUrl);
405+
add.insertEmail(ui.leEmail->text(), true /* prefered */ );
406+
407+
VCardConverter vcc;
408+
QByteArray vcard = vcc.createVCard(add);
409+
410+
QString file = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation );
411+
file += "/myidentity.vcd";
412+
QFile f ( file );
413+
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
414+
f.write(vcard);
415+
f.close();
416+
qDebug() << "Saved own identity to " << file;
417+
418+
KraftSettings::self()->setUserName( QString::null );
419+
KraftSettings::self()->setUserUid( QString::null );
420+
KraftSettings::self()->save();
421+
}
422+
} else { /* AddressBook */
423+
KraftSettings::self()->setUserName( _newIdentity.name() );
424+
KraftSettings::self()->setUserUid( _newIdentity.uid() );
425+
KraftSettings::self()->save();
426+
}
427+
emit newOwnIdentity(_newIdentity.uid(), _newIdentity);
428+
}
429+
364430
void PrefsDialog::writeConfig()
365431
{
366432
KraftSettings::self()->setShowDocumentLocale( mCbDocLocale->isChecked() );
@@ -386,12 +452,28 @@ void PrefsDialog::accept()
386452
mPrefsUnits->save();
387453
writeTaxes();
388454
writeConfig();
455+
writeIdentity();
389456
QDialog::accept();
390457
}
391458

392459
#define IDENTITY_TAG(X) QLatin1String(X)
393460
#define QL1(X) QLatin1String(X)
394461

462+
void PrefsDialog::fillManualIdentityForm(const KContacts::Addressee& addressee)
463+
{
464+
ui.leName->setText(addressee.formattedName());
465+
ui.leStreet->setText(addressee.address(Address::Work).street());
466+
ui.leCity->setText(addressee.address(Address::Work).locality());
467+
ui.lePostcode->setText(addressee.address(Address::Work).postalCode());
468+
469+
ui.leEmail->setText(addressee.preferredEmail());
470+
ui.leFax->setText(addressee.phoneNumber(PhoneNumber::Fax).number());
471+
ui.leOrganization->setText(addressee.organization());
472+
ui.lePhone->setText(addressee.phoneNumber(PhoneNumber::Work).number());
473+
ui.leMobile->setText(addressee.phoneNumber(PhoneNumber::Cell).number());
474+
ui.leWebsite->setText(addressee.url().url().toDisplayString());
475+
}
476+
395477
void PrefsDialog::setMyIdentity( const KContacts::Addressee& addressee, bool backendUp )
396478
{
397479
// Note: This code is stolen from DocDigestDetailView::slotShowDocDetails
@@ -415,14 +497,28 @@ void PrefsDialog::setMyIdentity( const KContacts::Addressee& addressee, bool bac
415497
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft Addressbook Integration down.</b></p>"
416498
"<p>The address book backend is not up and running.</p>"
417499
"<p>Please check your addressbook integration setup.</p>"));
500+
}
501+
502+
if( addressee.isEmpty() ) {
503+
addressBookInfo = i18n("The identity is not listed in an address book.");
504+
tmpl.createDictionary(QL1("NO_IDENTITY"));
505+
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft does not know your identity.</b></p>"
506+
"<p>Please pick one from the address books by clicking on the Button below.</p>"
507+
"<p>Not having an identity selected can make your documents look incomplete.</p>"));
418508
} else {
419-
if( addressee.isEmpty() ) {
420-
addressBookInfo = i18n("The identity is not listed in an address book.");
421-
tmpl.createDictionary(QL1("NO_IDENTITY"));
422-
tmpl.setValue(QL1("NO_IDENTITY_WRN"), i18n("<p><b>Kraft does not know your identity.</b></p>"
423-
"<p>Please pick one from the address books by clicking on the Button below.</p>"
424-
"<p>Not having an identity selected can make your documents look incomplete.</p>"));
509+
const QString origin = addressee.custom( CUSTOM_ADDRESS_MARKER );
510+
if( origin.isEmpty() || origin == "manual") {
511+
// it is an manually added address.
512+
fillManualIdentityForm(addressee);
513+
_tabWidget->setTabIcon(1, QIcon::fromTheme("checkmark"));
514+
_tabWidget->setTabIcon(0, QIcon());
515+
_tabWidget->setCurrentIndex(1);
425516
} else {
517+
_tabWidget->setTabIcon(0, QIcon::fromTheme("checkmark"));
518+
_tabWidget->setTabIcon(1, QIcon());
519+
_tabWidget->setCurrentIndex(0);
520+
521+
// it is an address from the address book
426522
addressBookInfo = i18n("Your identity can be found in the address books.");
427523
tmpl.createDictionary(QL1("IDENTITY"));
428524
tmpl.setValue( QL1("IDENTITY"), IDENTITY_TAG("IDENTITY_NAME"), addressee.realName() );

src/prefsdialog.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <kpagedialog.h>
2424
#include <QItemDelegate>
2525

26+
#include "ui_identity.h"
27+
2628
#include "doctypeedit.h"
2729
#include "doctype.h"
2830
#include "taxeditdialog.h"
@@ -80,7 +82,9 @@ protected slots:
8082
void wagesTab();
8183
void unitsTab();
8284
void writeTaxes();
85+
void writeIdentity();
8386
void whoIsMeTab();
87+
void fillManualIdentityForm(const KContacts::Addressee& addressee);
8488

8589
QComboBox *m_databaseDriver;
8690
QLineEdit *m_leHost;
@@ -106,11 +110,14 @@ protected slots:
106110
PrefsWages *mPrefsWages;
107111
PrefsUnits *mPrefsUnits;
108112

113+
KContacts::Addressee _newIdentity;
114+
109115
QPushButton *mDelTax;
110116
ImpTreeView *mTaxTreeView;
111117
QSqlTableModel *mTaxModel;
112118
HtmlView *mIdentityView;
113-
119+
QTabWidget *_tabWidget;
120+
Ui::manualOwnIdentity ui;
114121
};
115122

116123
class TaxItemDelegate : public QItemDelegate

src/reportgenerator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ void ReportGenerator::setMyContact( const KContacts::Addressee& contact )
189189

190190
void ReportGenerator::slotAddresseeSearchFinished( int )
191191
{
192+
qDebug() << "** Reached slotAddresseeSearchFinished!";
193+
192194
// now the addressee search through the address provider is finished.
193195
// Rendering can be started.
194196
QString tmplFile = findTemplate( mArchDoc->docType() );

0 commit comments

Comments
 (0)