<commit_before>/*
  Simple Addressbook for KMail
  Copyright <NAME> <<EMAIL>>

  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.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "kaddrbookexternal.h"
#include "kaddressbookcore_interface.h"

#ifdef KDEPIM_NEW_DISTRLISTS
#include "distributionlist.h"
#else
#include <kabc/distributionlist.h>
#endif
#include <kabc/resource.h>
#include <kabc/stdaddressbook.h>
#include <kabc/vcardconverter.h>
#include <kabc/errorhandler.h>
#include <kresources/selectdialog.h>

#include <kdefakes.h> // usleep
#include <KDebug>
#include <KLocale>
#include <KMessageBox>
#include <KToolInvocation>

#include <QApplication>
#include <QEventLoop>
#include <QList>
#include <QRegExp>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>

#include <unistd.h>

using namespace KPIM;

//-----------------------------------------------------------------------------
void KAddrBookExternal::openEmail( const QString &email, const QString &addr, QWidget *) {
  KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
  KABC::Addressee::List addresseeList = addressBook->findByEmail(email);

  // If KAddressbook is running, talk to it, otherwise start it.
  QDBusInterface abinterface( "org.kde.KAddressbook", "/kaddressbook_PimApplication",
                              "org.kde.KUniqueApplication" );

  if ( abinterface.isValid() ) {
    //make sure kaddressbook is loaded, otherwise showContactEditor
    //won't work as desired, see bug #87233
    abinterface.call("newInstance", QByteArray(), QByteArray());
  } else {
    KToolInvocation::startServiceByDesktopName( "kaddressbook" );
  }

  OrgKdeKAddressbookCoreInterface interface( "org.kde.KAddressbook",
                                             "/KAddressbook",
                                             QDBusConnection::sessionBus() );
  if( !addresseeList.isEmpty() ) {
    interface.showContactEditor( addresseeList.first().uid() );
  } else {
    interface.addEmail( addr );
  }
}

//-----------------------------------------------------------------------------
void KAddrBookExternal::addEmail( const QString &addr, QWidget *parent )
{
  QString email;
  QString name;

  KABC::Addressee::parseEmailAddress( addr, name, email );
  KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
  ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );

  // force a reload of the address book file so that changes that were made
  // by other programs are loaded
  ab->asyncLoad();

  KABC::Addressee::List addressees = ab->findByEmail( email );

  if ( addressees.isEmpty() ) {
    KABC::Addressee a;
    a.setNameFromString( name );
    a.insertEmail( email, true );

    if ( KAddrBookExternal::addAddressee( a ) ) {
      QString text = i18n( "<qt>The email address <b>%1</b> was added to your "
                           "addressbook; you can add more information to this "
                           "entry by opening the addressbook.</qt>", addr );
      KMessageBox::information( parent, text, QString(), "addedtokabc" );
    }
  } else {
    QString text =
      i18n( "<qt>The email address <b>%1</b> is already in your addressbook.</qt>", addr );
    KMessageBox::information( parent, text, QString(), "alreadyInAddressBook" );
  }
  ab->setErrorHandler( 0 );
}

void KAddrBookExternal::openAddressBook( QWidget * )
{
  KToolInvocation::startServiceByDesktopName( "kaddressbook" );
}

void KAddrBookExternal::addNewAddressee( QWidget * )
{
  KToolInvocation::startServiceByDesktopName( "kaddressbook" );
  OrgKdeKAddressbookCoreInterface interface( "org.kde.KAddressbook",
                                             "/KAddressbook",
                                             QDBusConnection::sessionBus() );
  interface.newContact();
}

bool KAddrBookExternal::addVCard( const KABC::Addressee &addressee, QWidget *parent )
{
  KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
  bool inserted = false;

  ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );

  KABC::Addressee::List addressees =
    ab->findByEmail( addressee.preferredEmail() );
<commit_msg>backport SVN commit 884354 by tmcguire:<commit_after>/*
  Simple Addressbook for KMail
  Copyright <NAME> <<EMAIL>>

  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.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "kaddrbookexternal.h"
#include "kaddressbookcore_interface.h"

#ifdef KDEPIM_NEW_DISTRLISTS
#include "distributionlist.h"
#else
#include <kabc/distributionlist.h>
#endif
#include <kabc/resource.h>
#include <kabc/stdaddressbook.h>
#include <kabc/vcardconverter.h>
#include <kabc/errorhandler.h>
#include <kresources/selectdialog.h>

#include <kdefakes.h> // usleep
#include <KDebug>
#include <KLocale>
#include <KMessageBox>
#include <KToolInvocation>

#include <QApplication>
#include <QEventLoop>
#include <QList>
#include <QRegExp>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>

#include <unistd.h>

using namespace KPIM;

//-----------------------------------------------------------------------------
void KAddrBookExternal::openEmail( const QString &email, const QString &addr, QWidget *) {
  KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
  KABC::Addressee::List addresseeList = addressBook->findByEmail(email);

  // If KAddressbook is running, talk to it, otherwise start it.
  QDBusInterface abinterface( "org.kde.kaddressbook", "/kaddressbook_PimApplication",
                              "org.kde.KUniqueApplication" );

  if ( abinterface.isValid() ) {
    //make sure kaddressbook is loaded, otherwise showContactEditor
    //won't work as desired, see bug #87233
    abinterface.call("newInstance", QByteArray(), QByteArray());
  } else {
    KToolInvocation::startServiceByDesktopName( "kaddressbook" );
  }

  OrgKdeKAddressbookCoreInterface interface( "org.kde.kaddressbook",
                                             "/KAddressBook",
                                             QDBusConnection::sessionBus() );
  if( !addresseeList.isEmpty() ) {
    interface.showContactEditor( addresseeList.first().uid() );
  } else {
    interface.addEmail( addr );
  }
}

//-----------------------------------------------------------------------------
void KAddrBookExternal::addEmail( const QString &addr, QWidget *parent )
{
  QString email;
  QString name;

  KABC::Addressee::parseEmailAddress( addr, name, email );
  KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
  ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );

  // force a reload of the address book file so that changes that were made
  // by other programs are loaded
  ab->asyncLoad();

  KABC::Addressee::List addressees = ab->findByEmail( email );

  if ( addressees.isEmpty() ) {
    KABC::Addressee a;
    a.setNameFromString( name );
    a.insertEmail( email, true );

    if ( KAddrBookExternal::addAddressee( a ) ) {
      QString text = i18n( "<qt>The email address <b>%1</b> was added to your "
                           "addressbook; you can add more information to this "
                           "entry by opening the addressbook.</qt>", addr );
      KMessageBox::information( parent, text, QString(), "addedtokabc" );
    }
  } else {
    QString text =
      i18n( "<qt>The email address <b>%1</b> is already in your addressbook.</qt>", addr );
    KMessageBox::information( parent, text, QString(), "alreadyInAddressBook" );
  }
  ab->setErrorHandler( 0 );
}

void KAddrBookExternal::openAddressBook( QWidget * )
{
  KToolInvocation::startServiceByDesktopName( "kaddressbook" );
}

void KAddrBookExternal::addNewAddressee( QWidget * )
{
  KToolInvocation::startServiceByDesktopName( "kaddressbook" );
  OrgKdeKAddressbookCoreInterface interface( "org.kde.kaddressbook",
                                             "/KAddressBook",
                                             QDBusConnection::sessionBus() );
  interface.newContact();
}

bool KAddrBookExternal::addVCard( const KABC::Addressee &addressee, QWidget *parent )
{
  KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
  bool inserted = false;

  ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );

  KABC::Addressee::List addressees =
    ab->findByEmail( addressee.preferredEmail() );

{'fraction_non_alphanumeric': 0.13980193847450484, 'fraction_numerical': 0.005689001264222503, 'mean_word_length': 3.1095238095238096, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '644698', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2721, 'n_words': 773}