/* accountselector.cpp - An Accountselector Copyright (c) 2004 by Stefan Gehn Kopete (c) 2002-2004 by the Kopete developers ************************************************************************* * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * ************************************************************************* */ #include "accountselector.h" #include "kopeteaccount.h" #include "kopeteaccountmanager.h" #include #include #include #include #include class AccountListViewItem : public TDEListViewItem { private: Kopete::Account *mAccount; public: AccountListViewItem(TQListView *parent, Kopete::Account *acc) : TDEListViewItem(parent) { if (acc==0) return; /*kdDebug(14010) << k_funcinfo << "account name = " << acc->accountId() << endl;*/ mAccount = acc; setText(0, mAccount->accountId()); setPixmap(0, mAccount->accountIcon()); } Kopete::Account *account() { return mAccount; } }; // ---------------------------------------------------------------------------- class AccountSelectorPrivate { public: TDEListView *lv; Kopete::Protocol *proto; }; AccountSelector::AccountSelector(TQWidget *parent, const char *name) : TQWidget(parent, name) { //kdDebug(14010) << k_funcinfo << "for no special protocol" << endl; d = new AccountSelectorPrivate; d->proto = 0; initUI(); } AccountSelector::AccountSelector(Kopete::Protocol *proto, TQWidget *parent, const char *name) : TQWidget(parent, name) { //kdDebug(14010) << k_funcinfo << " for protocol " << proto->pluginId() << endl; d = new AccountSelectorPrivate; d->proto = proto; initUI(); } AccountSelector::~AccountSelector() { kdDebug(14010) << k_funcinfo << endl; delete d; } void AccountSelector::initUI() { kdDebug(14010) << k_funcinfo << endl; (new TQVBoxLayout(this))->setAutoAdd(true); d->lv = new TDEListView(this); d->lv->setFullWidth(true); d->lv->addColumn(TQString::fromLatin1("")); d->lv->header()->hide(); if(d->proto != 0) { kdDebug(14010) << k_funcinfo << "creating list for a certain protocol" << endl; TQDict accounts = Kopete::AccountManager::self()->accounts(d->proto); TQDictIterator it(accounts); for(; Kopete::Account *account = it.current(); ++it) { new AccountListViewItem(d->lv, account); } } else { kdDebug(14010) << k_funcinfo << "creating list of all accounts" << endl; TQPtrList accounts = Kopete::AccountManager::self()->accounts(); Kopete::Account *account = 0; for(account = accounts.first(); account; account = accounts.next()) { new AccountListViewItem(d->lv, account); } } connect(d->lv, TQ_SIGNAL(selectionChanged(TQListViewItem *)), this, TQ_SLOT(slotSelectionChanged(TQListViewItem *))); } void AccountSelector::setSelected(Kopete::Account *account) { if (account==0) return; TQListViewItemIterator it(d->lv); while (it.current()) { if(static_cast(it.current())->account() == account) { it.current()->setSelected(true); return; } } } bool AccountSelector::isSelected(Kopete::Account *account) { if (account==0) return false; TQListViewItemIterator it(d->lv); while (it.current()) { if(static_cast(it.current())->account() == account) return true; } return false; } Kopete::Account *AccountSelector::selectedItem() { //kdDebug(14010) << k_funcinfo << endl; if (d->lv->selectedItem() != 0) return static_cast(d->lv->selectedItem())->account(); return 0; } void AccountSelector::slotSelectionChanged(TQListViewItem *item) { //kdDebug(14010) << k_funcinfo << endl; if (item != 0) { Kopete::Account *account = static_cast(item)->account(); if (account != 0) { emit selectionChanged(account); return; } } emit selectionChanged(0); } #include "accountselector.moc"