diff --git a/src/addressprovider.cpp b/src/addressprovider.cpp index b25be985..1b191c4e 100644 --- a/src/addressprovider.cpp +++ b/src/addressprovider.cpp @@ -23,8 +23,6 @@ #include "addressprovider_akonadi.h" -#include "klocalizedstring.h" - /* ==================================================================================== */ AddressProvider::AddressProvider( QObject *parent ) @@ -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]; diff --git a/src/addressprovider_akonadi.cpp b/src/addressprovider_akonadi.cpp index 16f8189b..483fce51 100644 --- a/src/addressprovider_akonadi.cpp +++ b/src/addressprovider_akonadi.cpp @@ -17,7 +17,6 @@ #include "addressprovider_akonadi.h" #include -#include #include @@ -65,7 +64,7 @@ QString AddressProviderPrivate::backendName() const #ifdef HAVE_AKONADI return QLatin1String("Akonadi"); #else - return i18n("No backend"); + return "No backend"; #endif } diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 7fa3278e..5dc26aea 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -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 ) diff --git a/tools/findcontact.cpp b/tools/findcontact.cpp index d71a7133..11cc2480 100644 --- a/tools/findcontact.cpp +++ b/tools/findcontact.cpp @@ -17,63 +17,26 @@ #include #include #include +#include +#include -#include -#include +#include "addressprovider.h" -#include -#include -#include -#include -#include - -#include -#include +#include class FindContact : public QObject { Q_OBJECT -public slots: - void searchResult(KJob* job ) { - if( !job ) { - return; - } - - Akonadi::ContactSearchJob *searchJob = qobject_cast( 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(job); - - const Akonadi::Item::List items = fetchJob->items(); - foreach( Akonadi::Item item, items ) { - if( item.hasPayload() ) { - dumpContact( item.payload(), _options._outputType ); - } - } - exit(0); +public slots: + void slotAddresseeFound( const QString&, const KContacts::Addressee& contact ) + { + dumpContact(contact, _options._outputType); + emit quitLoop(); } public: @@ -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() @@ -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!"; @@ -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; } @@ -243,7 +200,7 @@ public slots: } private: - Akonadi::ContactSearchJob *_job; + QScopedPointer _addressProvider; CmdOptions _options; }; @@ -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; }