예제 #1
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;
}
예제 #2
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();
}