Skip to content

Commit

Permalink
Make the tool findcontact working again with new addressprovider.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragotin committed Mar 7, 2018
1 parent f6ac93b commit 3f18ba2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 106 deletions.
4 changes: 1 addition & 3 deletions src/addressprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#include "addressprovider_akonadi.h"

#include "klocalizedstring.h"

/* ==================================================================================== */

AddressProvider::AddressProvider( QObject *parent )
Expand Down Expand Up @@ -81,7 +79,7 @@ void AddressProvider::slotErrorMsg(const QString& uid, const QString& msg)
QString AddressProvider::errorMsg( const QString& uid )
{
if( !_d->backendUp() ) {
return i18n("Backend down");
return "Backend down";
}
if( _errMessages.contains(uid) ) {
return _errMessages[uid];
Expand Down
3 changes: 1 addition & 2 deletions src/addressprovider_akonadi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "addressprovider_akonadi.h"
#include <kcontacts/contactgroup.h>
#include <klocalizedstring.h>

#include <QDebug>

Expand Down Expand Up @@ -65,7 +64,7 @@ QString AddressProviderPrivate::backendName() const
#ifdef HAVE_AKONADI
return QLatin1String("Akonadi");
#else
return i18n("No backend");
return "No backend";
#endif
}

Expand Down
30 changes: 19 additions & 11 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@

# set(findcontact_NAME findcontact)
# set(FINDCONTACT_SRC findcontact.cpp)
set(findcontact_NAME findcontact)
set(FINDCONTACT_SRC findcontact.cpp ../src/addressprovider.cpp ../src/addressprovider_akonadi.cpp)

# set(AUTOMOC ON)
set(AUTOMOC ON)

# add_executable(${findcontact_NAME} ${FINDCONTACT_SRC})
#
# For now there is only the Akonadi based address backend, and thus
# the findcontact tool is only built if akonadi is there.
# If there are other backends, this must be FIXED.
if(KF5Akonadi_FOUND)
add_executable(${findcontact_NAME} ${FINDCONTACT_SRC})

# target_link_libraries( ${findcontact_NAME}
# Qt5::Core
# Qt5::Widgets
# ${KDEPIMLIBS_KABC_LIBS} ${KDEPIMLIBS_AKONADI_CONTACT_LIBS}
# )
target_link_libraries( ${findcontact_NAME}
Qt5::Core
Qt5::Widgets
KF5::Contacts
KF5::AkonadiCore
KF5::AkonadiContact
)

########### install files ###############
# install(TARGETS ${findcontact_NAME} ${INSTALL_TARGETS_DEFAULT_ARGS})
########### install files ###############
install(TARGETS ${findcontact_NAME} ${INSTALL_TARGETS_DEFAULT_ARGS})
endif()

install(FILES erml2pdf.py DESTINATION ${DATA_INSTALL_DIR}/kraft/tools )
142 changes: 52 additions & 90 deletions tools/findcontact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,26 @@
#include <QUrl>
#include <QFile>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>

#include <kdebug.h>
#include <kdeversion.h>
#include "addressprovider.h"

#include <akonadi/contact/contactsearchjob.h>
#include <AkonadiCore/session.h>
#include <Akonadi/Item>
#include <AkonadiCore/ItemFetchJob>
#include <AkonadiCore/ItemFetchScope>

#include <kabc/vcardconverter.h>
#include <kabc/vcard.h>
#include <kcontacts/vcardconverter.h>


class FindContact : public QObject
{
Q_OBJECT

public slots:
void searchResult(KJob* job ) {
if( !job ) {
return;
}

Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
signals:
void quitLoop();

if( searchJob->error() ) {
kDebug() << "Address search job failed: " << job->errorString();
}

const KABC::Addressee::List contacts = searchJob->contacts();
// we set a limit to 1, so there is only one result.
if( contacts.size() > 0 ) {
KABC::Addressee contact;
contact = contacts.at(0);
dumpContact(contact, _options._outputType);
}

job->deleteLater();
exit(1);
}

void gidJobFinished( KJob *job ) {
if (job->error()) {
qDebug() << "gid job error: " << job->errorString();
exit(1);
}

Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>(job);

const Akonadi::Item::List items = fetchJob->items();
foreach( Akonadi::Item item, items ) {
if( item.hasPayload<KABC::Addressee>() ) {
dumpContact( item.payload<KABC::Addressee>(), _options._outputType );
}
}
exit(0);
public slots:
void slotAddresseeFound( const QString&, const KContacts::Addressee& contact )
{
dumpContact(contact, _options._outputType);
emit quitLoop();
}

public:
Expand All @@ -92,7 +55,12 @@ public slots:

// Constructor, called to initialize object
FindContact() : QObject() {
using namespace Akonadi;
_addressProvider.reset( new AddressProvider(this) );

connect( _addressProvider.data(),
SIGNAL(lookupResult(QString,KContacts::Addressee)),
this,
SLOT(slotAddresseeFound(QString, KContacts::Addressee)));
}

void help()
Expand Down Expand Up @@ -148,62 +116,51 @@ public slots:

// method to start the search job. It is asynchronous and ends up in the
// slot searchResult()
void akonadiSearch( )
void search( )
{
#if KDE_IS_VERSION(4,12,0)
akonadiSearchGID();
#else
QString uid = _options.uid;
const QString uid = _options.uid;

if( uid.isEmpty() ) return;

_job = new Akonadi::ContactSearchJob( );
_job->setLimit( 1 );
_job->setQuery( Akonadi::ContactSearchJob::ContactUid , uid);

connect( _job, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) );

_job->start();
#endif
}

#if KDE_IS_VERSION(4,12,0)
void akonadiSearchGID() {
QString gid = _options.uid;

if( gid.isEmpty() ) return;

Akonadi::Item item;
item.setGid( gid );

Akonadi::ItemFetchJob *fetchJob = new Akonadi::ItemFetchJob(item, this);

connect( fetchJob, SIGNAL(result(KJob*)), SLOT(gidJobFinished(KJob*)) );
fetchJob->fetchScope().fetchFullPayload();
fetchJob->start();

AddressProvider::LookupState state = _addressProvider->lookupAddressee(uid);
if( state == AddressProvider::LookupFromCache ) {
const KContacts::Addressee addressee = _addressProvider->getAddresseeFromCache(uid);
// this cant actually happen because the cache can not be prefilled.
slotAddresseeFound( QString::null, addressee );
} else if( state == AddressProvider::LookupOngoing ) {
} else if( state == AddressProvider::LookupStarted ) {
// thats the supposed return type.
} else if( state == AddressProvider::LookupNotFound ||
state == AddressProvider::BackendError ||
state == AddressProvider::ItemError ) {
// errors
exit(1);
}
}
#endif


#define NL (QLatin1Char('\n'));
// print the output
void dumpContact( KABC::Addressee contact, OutputType dt) {
void dumpContact( KContacts::Addressee contact, OutputType dt) {
QString out;

if( contact.isEmpty() ) {
return;
}

if( dt == VCard ) {
KABC::VCardConverter convert;
QByteArray arr = convert.exportVCard(contact, KABC::VCardConverter::v3_0);
KContacts::VCardConverter convert;
QByteArray arr = convert.exportVCard(contact, KContacts::VCardConverter::v3_0);
out = QString::fromUtf8(arr);
} else if( dt == Pretty ) {
out += contact.realName() + NL;
KABC::Address address = contact.address(KABC::Address::Pref);
KContacts::Address address = contact.address(KContacts::Address::Pref);
if( address.isEmpty() )
address = contact.address(KABC::Address::Work );
address = contact.address(KContacts::Address::Work );
if( address.isEmpty() )
address = contact.address(KABC::Address::Home );
address = contact.address(KContacts::Address::Home );
if( address.isEmpty() )
address = contact.address(KABC::Address::Postal );
address = contact.address(KContacts::Address::Postal );

if(address.isEmpty()) {
// std::cout << "Warn: No address found!";
Expand All @@ -213,7 +170,7 @@ public slots:
}
out += QLatin1Char('\n');

foreach( KABC::PhoneNumber pnum, contact.phoneNumbers() ) {
foreach( KContacts::PhoneNumber pnum, contact.phoneNumbers() ) {
out += QString( "Phone %1: %2").arg(pnum.typeLabel()).arg(pnum.number()) + NL;
}

Expand Down Expand Up @@ -243,7 +200,7 @@ public slots:
}

private:
Akonadi::ContactSearchJob *_job;
QScopedPointer<AddressProvider> _addressProvider;
CmdOptions _options;
};

Expand All @@ -254,7 +211,12 @@ int main(int argc, char **argv) {

FindContact fc;
fc.parseOptions( app.arguments());
fc.akonadiSearch();
fc.search();

QEventLoop loop;
QObject::connect(&fc, SIGNAL(quitLoop()), &loop, SLOT(quit()), Qt::QueuedConnection);
loop.exec();

app.exec();
return 0;
}
Expand Down

0 comments on commit 3f18ba2

Please sign in to comment.