Example #1
0
QString KAddrBookExternal::expandDistributionList(const QString &listName)
{
    if(listName.isEmpty())
        return QString::null;

    const QString lowerListName = listName.lower();
    KABC::AddressBook *addressBook = KABC::StdAddressBook::self(true);
#ifdef KDEPIM_NEW_DISTRLISTS
    KPIM::DistributionList distrList = KPIM::DistributionList::findByName(addressBook, lowerListName, false);
    if(!distrList.isEmpty())
    {
        return distrList.emails(addressBook).join(", ");
    }
#else
    KABC::DistributionListManager manager(addressBook);
    manager.load();
    const QStringList listNames = manager.listNames();

    for(QStringList::ConstIterator it = listNames.begin();
            it != listNames.end(); ++it)
    {
        if((*it).lower() == lowerListName)
        {
            const QStringList addressList = manager.list(*it)->emails();
            return addressList.join(", ");
        }
    }
#endif
    return QString::null;
}
void KOEditorDetails::fillIncidence( Incidence *incidence )
{
    incidence->clearAttendees();
    QVector<Q3ListViewItem*> toBeDeleted;
    Q3ListViewItem *item;
    AttendeeListItem *a;
    for ( item = mListView->firstChild(); item; item = item->nextSibling() ) {
        a = (AttendeeListItem *)item;
        Attendee *attendee = a->data();
        Q_ASSERT( attendee );
        /* Check if the attendee is a distribution list and expand it */
        if ( attendee->email().isEmpty() ) {
            KPIM::DistributionList list =
                KPIM::DistributionList::findByName( KABC::StdAddressBook::self(), attendee->name() );
            if ( !list.isEmpty() ) {
                toBeDeleted.push_back( item ); // remove it once we are done expanding
                KPIM::DistributionList::Entry::List entries = list.entries( KABC::StdAddressBook::self() );
                KPIM::DistributionList::Entry::List::Iterator it( entries.begin() );
                while ( it != entries.end() ) {
                    KPIM::DistributionList::Entry &e = ( *it );
                    ++it;
                    // this calls insertAttendee, which appends
                    insertAttendeeFromAddressee( e.addressee, attendee );
                    // TODO: duplicate check, in case it was already added manually
                }
            }
        } else {
            bool skip = false;
            if ( attendee->email().endsWith( QLatin1String( "example.net" ) ) ) {
                if ( KMessageBox::warningYesNo(
                            this,
                            i18nc( "@info",
                                   "%1 does not look like a valid email address. "
                                   "Are you sure you want to invite this participant?",
                                   attendee->email() ),
                            i18nc( "@title", "Invalid Email Address" ) ) != KMessageBox::Yes ) {
                    skip = true;
                }
            }
            if ( !skip ) {
                incidence->addAttendee( new Attendee( *attendee ) );
            }
        }
    }

    KOAttendeeEditor::fillIncidence( incidence );

    // cleanup
    qDeleteAll( toBeDeleted );
    toBeDeleted.clear();
}
Example #3
0
void KOEditorFreeBusy::writeEvent(KCal::Event * event)
{
  event->clearAttendees();
  QValueVector<FreeBusyItem*> toBeDeleted;
  for ( FreeBusyItem *item = static_cast<FreeBusyItem *>( mGanttView->firstChild() ); item;
        item = static_cast<FreeBusyItem*>( item->nextSibling() ) )
  {
    Attendee *attendee = item->attendee();
    Q_ASSERT( attendee );
    /* Check if the attendee is a distribution list and expand it */
    if ( attendee->email().isEmpty() ) {
      KPIM::DistributionList list =
        KPIM::DistributionList::findByName( KABC::StdAddressBook::self(), attendee->name() );
      if ( !list.isEmpty() ) {
        toBeDeleted.push_back( item ); // remove it once we are done expanding
        KPIM::DistributionList::Entry::List entries = list.entries( KABC::StdAddressBook::self() );
        KPIM::DistributionList::Entry::List::Iterator it( entries.begin() );
        while ( it != entries.end() ) {
          KPIM::DistributionList::Entry &e = ( *it );
          ++it;
          // this calls insertAttendee, which appends
          insertAttendeeFromAddressee( e.addressee, attendee );
          // TODO: duplicate check, in case it was already added manually
        }
      }
    } else {
      bool skip = false;
      if ( attendee->email().endsWith( "example.net" ) ) {
        if ( KMessageBox::warningYesNo( this, i18n("%1 does not look like a valid email address. "
                "Are you sure you want to invite this participant?").arg( attendee->email() ),
              i18n("Invalid email address") ) != KMessageBox::Yes ) {
          skip = true;
        }
      }
      if ( !skip ) {
        event->addAttendee( new Attendee( *attendee ) );
      }
    }
  }

  KOAttendeeEditor::writeEvent( event );

  // cleanup
  QValueVector<FreeBusyItem*>::iterator it;
  for( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
    delete *it;
  }
}
Example #4
0
KABC::Addressee Contact::fromXml(const QString &xml)
{
    QDomDocument document;

    QString errorMsg;
    int errorLine, errorColumn;
    if(!document.setContent(xml, true, &errorMsg, &errorLine, &errorColumn))
    {
        qDebug("Error parsing XML in Scalix::Contact::fromXml: %s (%d,%d)", errorMsg.latin1(), errorLine, errorColumn);
        return KABC::Addressee();
    }

    QDomElement contactElement = document.documentElement();
    if(contactElement.tagName() != "contact")
    {
        if(contactElement.tagName() == "distlist")
        {
            const QDomNodeList names = contactElement.elementsByTagName("display_name");
            const QString listName = (names.count() == 1 ? names.item(0).toElement().text() : "Scalix Dummy List");

            /**
             * As we can't provide distribution list functionality we store the entry
             * here and return it on save.
             */
            KPIM::DistributionList list;
            list.setName(listName);

            if(!s_distListMap)
                sd.setObject(s_distListMap, new QMap<QString, QString>());

            s_distListMap->insert(list.uid(), xml);

            return list;
        }
        else
        {
            qDebug("Error interpreting XML in Scalix::Contact::fromXml: no 'contact' or 'distlist' tag found");
            return KABC::Addressee();
        }
    }

    QString emails[ 3 ];
    KABC::Address homeAddress(KABC::Address::Home);
    KABC::Address workAddress(KABC::Address::Work);
    KABC::Address otherAddress(KABC::Address::Dom);

    KABC::Addressee addr;
    setCustom("comes_from_scalix", "true", addr);

    QDomNode node = contactElement.firstChild();
    while(!node.isNull())
    {
        QDomElement element = node.toElement();
        if(!element.isNull())
        {
            if(element.tagName() == "direct_ref")
                addr.setUid(element.text());
            else if(element.tagName() == "sensitivity")
                setCustom("sensitivity", element.text(), addr);
            else if(element.tagName() == "is_recurring")
                setCustom("is_recurring", element.text(), addr);
            else if(element.tagName() == "reminder_set")
                setCustom("reminder_set", element.text(), addr);
            else if(element.tagName() == "send_rich_info")
                setCustom("send_rich_info", element.text(), addr);
            else if(element.tagName() == "last_modification_time")
                addr.setRevision(QDateTime::fromString(element.text(), Qt::ISODate));

            // name
            else if(element.tagName() == "display_name_prefix")
                addr.setPrefix(element.text());
            else if(element.tagName() == "first_name")
                addr.setGivenName(element.text());
            else if(element.tagName() == "middle_name")
                addr.setAdditionalName(element.text());
            else if(element.tagName() == "last_name")
                addr.setFamilyName(element.text());
            else if(element.tagName() == "suffix")
                addr.setSuffix(element.text());
            else if(element.tagName() == "file_as")
                addr.setFormattedName(element.text());
            else if(element.tagName() == "nickname")
                addr.setNickName(element.text());

            // job
            else if(element.tagName() == "web_page_address")
                addr.setUrl(element.text());
            else if(element.tagName() == "company_name")
                addr.setOrganization(element.text());
            else if(element.tagName() == "job_title")
                addr.setTitle(element.text());

            // emails
            else if(element.tagName().startsWith("email"))
            {
                if(element.tagName() == "email1_address")
                    emails[ 0 ] = element.text();
                else if(element.tagName() == "email2_address")
                    emails[ 1 ] = element.text();
                else if(element.tagName() == "email3_address")
                    emails[ 2 ] = element.text();
                else
                    setCustom(element.tagName(), element.text(), addr);
            }

            // phone numbers
            else if(element.tagName() == "home_phone_number")
                addr.insertPhoneNumber(KABC::PhoneNumber(element.text(), KABC::PhoneNumber::Home));
            else if(element.tagName() == "work_phone_number")
                addr.insertPhoneNumber(KABC::PhoneNumber(element.text(), KABC::PhoneNumber::Work));
            else if(element.tagName() == "work_fax_number")
                addr.insertPhoneNumber(KABC::PhoneNumber(element.text(), KABC::PhoneNumber::Work | KABC::PhoneNumber::Fax));
            else if(element.tagName() == "mobile_phone_number")
                addr.insertPhoneNumber(KABC::PhoneNumber(element.text(), KABC::PhoneNumber::Cell));

            // address (work)
            else if(element.tagName() == "work_address_street")
                workAddress.setStreet(element.text());
            else if(element.tagName() == "work_address_zip")
                workAddress.setPostalCode(element.text());
            else if(element.tagName() == "work_address_city")
                workAddress.setLocality(element.text());
            else if(element.tagName() == "work_address_state")
                workAddress.setRegion(element.text());
            else if(element.tagName() == "work_address_country")
                workAddress.setCountry(element.text());

            // address (home)
            else if(element.tagName() == "home_address_street")
                homeAddress.setStreet(element.text());
            else if(element.tagName() == "home_address_zip")
                homeAddress.setPostalCode(element.text());
            else if(element.tagName() == "home_address_city")
                homeAddress.setLocality(element.text());
            else if(element.tagName() == "home_address_state")
                homeAddress.setRegion(element.text());
            else if(element.tagName() == "home_address_country")
                homeAddress.setCountry(element.text());

            // address (other)
            else if(element.tagName() == "other_address_street")
                otherAddress.setStreet(element.text());
            else if(element.tagName() == "other_address_zip")
                otherAddress.setPostalCode(element.text());
            else if(element.tagName() == "other_address_city")
                otherAddress.setLocality(element.text());
            else if(element.tagName() == "other_address_state")
                otherAddress.setRegion(element.text());
            else if(element.tagName() == "other_address_country")
                otherAddress.setCountry(element.text());

            else if(element.tagName() == "selected_mailing_address")
                switch(element.text().toInt())
                {
                    case 1:
                        homeAddress.setType(homeAddress.type() | KABC::Address::Pref);
                        break;
                    case 2:
                        workAddress.setType(workAddress.type() | KABC::Address::Pref);
                        break;
                    case 3:
                        otherAddress.setType(otherAddress.type() | KABC::Address::Pref);
                        break;
                    default:
                        Q_ASSERT(!"Unknown selected_mailing_address enum");
                        break;
                }

            // misc
            else if(element.tagName() == "im_address")
                addr.insertCustom("KADDRESSBOOK", "X-IMAddress", element.text());
            else if(element.tagName() == "manager")
                addr.insertCustom("KADDRESSBOOK", "X-ManagersName", element.text());
            else if(element.tagName() == "department")
                addr.insertCustom("KADDRESSBOOK", "X-Department", element.text());
            else if(element.tagName() == "assistant")
                addr.insertCustom("KADDRESSBOOK", "X-AssistantsName", element.text());
            else if(element.tagName() == "profession")
                addr.insertCustom("KADDRESSBOOK", "X-Profession", element.text());
            else if(element.tagName() == "office_location")
                addr.insertCustom("KADDRESSBOOK", "X-Office", element.text());
            else if(element.tagName() == "spouse")
                addr.insertCustom("KADDRESSBOOK", "X-SpousesName", element.text());

            else if(element.tagName() == "bday")
                addr.setBirthday(QDateTime::fromString(element.text(), Qt::ISODate));
            else if(element.tagName() == "anniversary")
                addr.insertCustom("KADDRESSBOOK", "X-Anniversary", element.text());
            else
                setCustom(element.tagName(), element.text(), addr);
        }

        node = node.nextSibling();
    }

    for(int i = 0; i < 3; ++i)
        if(!emails[ i ].isEmpty())
            addr.insertEmail(emails[ i ]);

    if(!homeAddress.isEmpty())
        addr.insertAddress(homeAddress);
    if(!workAddress.isEmpty())
        addr.insertAddress(workAddress);
    if(!otherAddress.isEmpty())
        addr.insertAddress(otherAddress);

    return addr;
}
Example #5
0
void DistributionListDialog::slotUser1()
{
  bool isEmpty = true;

  KABC::AddressBook *ab = KABC::StdAddressBook::self( true );

  QListViewItem *i = mRecipientsList->firstChild();
  while( i ) {
    DistributionListItem *item = static_cast<DistributionListItem *>( i );
    if ( item->isOn() ) {
      isEmpty = false;
      break;
    }
    i = i->nextSibling();
  }

  if ( isEmpty ) {
    KMessageBox::information( this,
                              i18n("There are no recipients in your list. "
                                   "First select some recipients, "
                                   "then try again.") );
    return;
  }

#ifndef KDEPIM_NEW_DISTRLISTS
  KABC::DistributionListManager manager( ab );
  manager.load();
#endif

  QString name = mTitleEdit->text();

  if ( name.isEmpty() ) {
    bool ok = false;
    name = KInputDialog::getText( i18n("New Distribution List"),
      i18n("Please enter name:"), QString::null, &ok, this );
    if ( !ok || name.isEmpty() )
      return;
  }

#ifdef KDEPIM_NEW_DISTRLISTS
  if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
#else
  if ( manager.list( name ) ) {
#endif
    KMessageBox::information( this,
      i18n( "<qt>Distribution list with the given name <b>%1</b> "
        "already exists. Please select a different name.</qt>" ).arg( name ) );
    return;
  }

#ifdef KDEPIM_NEW_DISTRLISTS
  KPIM::DistributionList dlist;
  dlist.setName( name );

  i = mRecipientsList->firstChild();
  while( i ) {
    DistributionListItem *item = static_cast<DistributionListItem *>( i );
    if ( item->isOn() ) {
      kdDebug() << "  " << item->addressee().fullEmail() << endl;
      if ( item->isTransient() ) {
        ab->insertAddressee( item->addressee() );
      }
      if ( item->email() == item->addressee().preferredEmail() ) {
        dlist.insertEntry( item->addressee() );
      } else {
        dlist.insertEntry( item->addressee(), item->email() );
      }
    }
    i = i->nextSibling();
  }

  ab->insertAddressee( dlist );
#else
  KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name );
  i = mRecipientsList->firstChild();
  while( i ) {
    DistributionListItem *item = static_cast<DistributionListItem *>( i );
    if ( item->isOn() ) {
      kdDebug() << "  " << item->addressee().fullEmail() << endl;
      if ( item->isTransient() ) {
        ab->insertAddressee( item->addressee() );
      }
      if ( item->email() == item->addressee().preferredEmail() ) {
        dlist->insertEntry( item->addressee() );
      } else {
        dlist->insertEntry( item->addressee(), item->email() );
      }
    }
    i = i->nextSibling();
  }
#endif

  // FIXME: Ask the user which resource to save to instead of the default
  bool saveError = true;
  KABC::Ticket *ticket = ab->requestSaveTicket( 0 /*default resource */ );
  if ( ticket )
    if ( ab->save( ticket ) )
      saveError = false;
    else
      ab->releaseSaveTicket( ticket );

  if ( saveError )
    kdWarning(5006) << k_funcinfo << " Couldn't save new addresses in the distribution list just created to the address book" << endl;

#ifndef KDEPIM_NEW_DISTRLISTS
  manager.save();
#endif

  close();
}
Example #6
0
void SearchManager::search(const QString &pattern, const KABC::Field::List &fields, Type type)
{
    mPattern = pattern;
    mFields = fields;
    mType = type;

    KABC::Addressee::List allContacts;
    mContacts.clear();

#if KDE_VERSION >= 319
    KABC::AddresseeList list(mAddressBook->allAddressees());
    if(!fields.isEmpty())
        list.sortByField(fields.first());

    allContacts = list;
#else
    KABC::AddressBook::ConstIterator abIt(mAddressBook->begin());
    const KABC::AddressBook::ConstIterator abEndIt(mAddressBook->end());
    for(; abIt != abEndIt; ++abIt)
        allContacts.append(*abIt);
#endif

#ifdef KDEPIM_NEW_DISTRLISTS
    // Extract distribution lists from allContacts
    mDistributionLists.clear();
    KABC::Addressee::List::Iterator rmIt(allContacts.begin());
    const KABC::Addressee::List::Iterator rmEndIt(allContacts.end());
    while(rmIt != rmEndIt)
    {
        if(KPIM::DistributionList::isDistributionList(*rmIt))
        {
            mDistributionLists.append(static_cast<KPIM::DistributionList>(*rmIt));
            rmIt = allContacts.remove(rmIt);
        }
        else
            ++rmIt;
    }

    typedef KPIM::DistributionList::Entry Entry;
    if(!mSelectedDistributionList.isNull())
    {
        const KPIM::DistributionList dl = KPIM::DistributionList::findByName(mAddressBook, mSelectedDistributionList);
        if(!dl.isEmpty())
        {
            allContacts.clear();
            const Entry::List entries = dl.entries(mAddressBook);
            const Entry::List::ConstIterator end = entries.end();
            for(Entry::List::ConstIterator it = entries.begin(); it != end; ++it)
            {
                allContacts.append((*it).addressee);
            }
        }
    }

#endif

    if(mPattern.isEmpty())      // no pattern, return all
    {
        mContacts = allContacts;

        emit contactsUpdated();

        return;
    }

    const KABC::Field::List fieldList = !mFields.isEmpty() ? mFields : KABC::Field::allFields();

    KABC::Addressee::List::ConstIterator it(allContacts.begin());
    const KABC::Addressee::List::ConstIterator endIt(allContacts.end());
    for(; it != endIt; ++it)
    {
#ifdef KDEPIM_NEW_DISTRLISTS
        if(KPIM::DistributionList::isDistributionList(*it))
            continue;
#endif

        bool found = false;
        // search over all fields
        KABC::Field::List::ConstIterator fieldIt(fieldList.begin());
        const KABC::Field::List::ConstIterator fieldEndIt(fieldList.end());
        for(; fieldIt != fieldEndIt; ++fieldIt)
        {

            if(type == StartsWith && (*fieldIt)->value(*it).startsWith(pattern, false))
            {
                mContacts.append(*it);
                found = true;
                break;
            }
            else if(type == EndsWith && (*fieldIt)->value(*it).endsWith(pattern, false))
            {
                mContacts.append(*it);
                found = true;
                break;
            }
            else if(type == Contains && (*fieldIt)->value(*it).find(pattern, 0, false) != -1)
            {
                mContacts.append(*it);
                found = true;
                break;
            }
            else if(type == Equals && (*fieldIt)->value(*it).localeAwareCompare(pattern) == 0)
            {
                mContacts.append(*it);
                found = true;
                break;
            }
        }

        if(!found)
        {
            // search over custom fields
            const QStringList customs = (*it).customs();

            QStringList::ConstIterator customIt(customs.begin());
            const QStringList::ConstIterator customEndIt(customs.end());
            for(; customIt != customEndIt; ++customIt)
            {
                int pos = (*customIt).find(':');
                if(pos != -1)
                {
                    const QString value = (*customIt).mid(pos + 1);
                    if(type == StartsWith && value.startsWith(pattern, false))
                    {
                        mContacts.append(*it);
                        break;
                    }
                    else if(type == EndsWith && value.endsWith(pattern, false))
                    {
                        mContacts.append(*it);
                        break;
                    }
                    else if(type == Contains && value.find(pattern, 0, false) != -1)
                    {
                        mContacts.append(*it);
                        break;
                    }
                    else if(type == Equals && value.localeAwareCompare(pattern) == 0)
                    {
                        mContacts.append(*it);
                        break;
                    }
                }
            }
        }
    }

    emit contactsUpdated();
}