Esempio n. 1
0
bool KAddrBookExternal::addAddressee(const KABC::Addressee &addr)
{
    KABC::AddressBook *addressBook = KABC::StdAddressBook::self(true);

#if KDE_IS_VERSION(3,4,89)
    // This ugly hack will be removed in 4.0
    while(!addressBook->loadingHasFinished())
    {
        QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput);

        // use sleep here to reduce cpu usage
        usleep(100);
    }
#endif

    // Select a resource
    QPtrList<KABC::Resource> kabcResources = addressBook->resources();

    QPtrList<KRES::Resource> kresResources;
    QPtrListIterator<KABC::Resource> resIt(kabcResources);
    KABC::Resource *kabcResource;
    while((kabcResource = resIt.current()) != 0)
    {
        ++resIt;
        if(!kabcResource->readOnly())
        {
            KRES::Resource *res = static_cast<KRES::Resource *>(kabcResource);
            if(res)
                kresResources.append(res);
        }
    }

    kabcResource = static_cast<KABC::Resource *>(KRES::SelectDialog::getResource(kresResources, 0));

    KABC::Ticket *ticket = addressBook->requestSaveTicket(kabcResource);
    bool saved = false;
    if(ticket)
    {
        KABC::Addressee addressee(addr);
        addressee.setResource(kabcResource);
        addressBook->insertAddressee(addressee);
        saved = addressBook->save(ticket);
        if(!saved)
            addressBook->releaseSaveTicket(ticket);
    }

    addressBook->emitAddressBookChanged();

    return saved;
}
CompletionOrderEditor::CompletionOrderEditor(KPIM::LdapSearch *ldapSearch,
        QWidget *parent, const char *name)
    : KDialogBase(parent, name, true, i18n("Edit Completion Order"), Ok | Cancel, Ok, true),
      mConfig("kpimcompletionorder"), mDirty(false)
{
    mItems.setAutoDelete(true);
    // The first step is to gather all the data, creating CompletionItem objects
    QValueList< LdapClient * > ldapClients = ldapSearch->clients();
    for(QValueList<LdapClient *>::const_iterator it = ldapClients.begin(); it != ldapClients.end(); ++it)
    {
        //kdDebug(5300) << "LDAP: host " << (*it)->host() << " weight " << (*it)->completionWeight() << endl;
        mItems.append(new LDAPCompletionItem(*it));
    }
    KABC::AddressBook *addressBook = KABC::StdAddressBook::self(true);
    QPtrList<KABC::Resource> resources = addressBook->resources();
    for(QPtrListIterator<KABC::Resource> resit(resources); *resit; ++resit)
    {
        //kdDebug(5300) << "KABC Resource: " << (*resit)->className() << endl;
        ResourceABC *res = dynamic_cast<ResourceABC *>(*resit);
        if(res)      // IMAP KABC resource
        {
            const QStringList subresources = res->subresources();
            for(QStringList::const_iterator it = subresources.begin(); it != subresources.end(); ++it)
            {
                mItems.append(new KABCImapSubResCompletionItem(res, *it));
            }
        }
        else     // non-IMAP KABC resource
        {
            mItems.append(new SimpleCompletionItem(this, (*resit)->resourceName(),
                                                   (*resit)->identifier()));
        }
    }

#ifndef KDEPIM_NEW_DISTRLISTS // new distr lists are normal contact, so no separate item if using them
    // Add an item for distribution lists
    mItems.append(new SimpleCompletionItem(this, i18n("Distribution Lists"), "DistributionLists"));
#endif

    // Now sort the items, then create the GUI
    mItems.sort();

    QHBox *page = makeHBoxMainWidget();
    mListView = new KListView(page);
    mListView->setSorting(-1);
    mListView->addColumn(QString::null);
    mListView->header()->hide();

    for(QPtrListIterator<CompletionItem> compit(mItems); *compit; ++compit)
    {
        new CompletionViewItem(mListView, *compit);
        kdDebug(5300) << "  " << (*compit)->label() << " " << (*compit)->completionWeight() << endl;
    }

    QVBox *upDownBox = new QVBox(page);
    mUpButton = new KPushButton(upDownBox, "mUpButton");
    mUpButton->setIconSet(BarIconSet("up", KIcon::SizeSmall));
    mUpButton->setEnabled(false);   // b/c no item is selected yet
    mUpButton->setFocusPolicy(StrongFocus);

    mDownButton = new KPushButton(upDownBox, "mDownButton");
    mDownButton->setIconSet(BarIconSet("down", KIcon::SizeSmall));
    mDownButton->setEnabled(false);   // b/c no item is selected yet
    mDownButton->setFocusPolicy(StrongFocus);

    QWidget *spacer = new QWidget(upDownBox);
    upDownBox->setStretchFactor(spacer, 100);

    connect(mListView, SIGNAL(selectionChanged(QListViewItem *)),
            SLOT(slotSelectionChanged(QListViewItem *)));
    connect(mUpButton, SIGNAL(clicked()), this, SLOT(slotMoveUp()));
    connect(mDownButton, SIGNAL(clicked()), this, SLOT(slotMoveDown()));
}
void TestDistrList::testConverter()
{
    kDebug() ;

    KABC::AddressBook *ab = KABC::StdAddressBook::self();
    QList<KABC::Resource*> resources = ab->resources();
    assert( resources.count() == 1 );

    DistributionListConverter converter( resources[ 0 ] );

    DistributionList pimList;
    KABC::DistributionList *kabcList = converter.convertToKABC( pimList );

    assert( kabcList != 0 );
    assert( kabcList->entries().isEmpty() && kabcList->emails().isEmpty() );
    assert( kabcList->identifier().isEmpty() && kabcList->name().isEmpty() );

    DistributionList pimList2 = converter.convertFromKABC( kabcList );
    assert( pimList2 == pimList );

    delete kabcList;

    pimList.setName( "pimlist-name" );
    pimList.setUid( "pimlist-id" );

    kabcList = converter.convertToKABC( pimList );
    assert( kabcList != 0 );
    assert( kabcList->entries().isEmpty() && kabcList->emails().isEmpty() );
    assert( kabcList->identifier() == "pimlist-id" &&
            kabcList->name() == "pimlist-name" );

    pimList2 = converter.convertFromKABC( kabcList );
    assert( pimList2 == pimList );

    delete kabcList;

    KABC::Addressee addr1 = ab->findByName( "addr1" ).first();
    assert( !addr1.isEmpty() );

    pimList.insertEntry( addr1 );

    kabcList = converter.convertToKABC( pimList );
    assert( kabcList != 0 );
    assert( kabcList->identifier() == "pimlist-id" &&
            kabcList->name() == "pimlist-name" );
    assert( kabcList->entries().count() == 1 && kabcList->emails().count() == 1 );
    assert( kabcList->entries()[ 0 ].addressee() == addr1 );

    pimList2 = converter.convertFromKABC( kabcList );
    assert( pimList2 == pimList );

    delete kabcList;

    KABC::Addressee addr2 = ab->findByName( "addr2" ).first();
    assert( !addr2.isEmpty() );

    pimList.insertEntry( addr2 );

    kabcList = converter.convertToKABC( pimList );
    assert( kabcList != 0 );
    assert( kabcList->identifier() == "pimlist-id" &&
            kabcList->name() == "pimlist-name" );
    assert( kabcList->entries().count() == 2 && kabcList->emails().count() == 2 );
    assert( kabcList->entries()[ 0 ].addressee() == addr1 );
    assert( kabcList->entries()[ 2 ].addressee() == addr2 );

    pimList2 = converter.convertFromKABC( kabcList );
    assert( pimList2 == pimList );

    delete kabcList;
}