26 #include <QCloseEvent>
28 #include <KMessageBox>
29 #include <KPushButton>
32 #include <TelepathyQt/AccountManager>
33 #include <TelepathyQt/Account>
34 #include <TelepathyQt/Connection>
35 #include <TelepathyQt/ContactManager>
36 #include <TelepathyQt/PendingContacts>
37 #include <TelepathyQt/Filter>
38 #include <TelepathyQt/AccountSet>
42 class KTP_NO_EXPORT SubscribableAccountFilter :
public Tp::AccountFilter
45 SubscribableAccountFilter()
55 bool matches(
const Tp::AccountPtr &account)
const
58 if (! account->connection()) {
63 if (! account->connection()->contactManager()->canRequestPresenceSubscription()){
74 ui(new Ui::AddContactDialog),
75 acceptInProgress(false)
79 bool acceptInProgress;
82 AddContactDialog::AddContactDialog(
const Tp::AccountManagerPtr &accountManager,
QWidget *parent) :
86 setWindowTitle(i18n(
"Add new contact"));
87 setWindowIcon(QIcon::fromTheme(QLatin1String(
"list-add-user")));
90 d->ui->setupUi(widget);
91 setMainWidget(widget);
93 Tp::AccountFilterPtr filter = Tp::AccountFilterPtr(
new KTp::SubscribableAccountFilter());
94 Tp::AccountSetPtr accountSet = accountManager->filterAccounts(filter);
96 d->ui->accountCombo->setAccountSet(accountSet);
98 d->ui->screenNameLineEdit->setFocus();
101 AddContactDialog::~AddContactDialog()
107 void AddContactDialog::accept()
109 Tp::AccountPtr account = d->ui->accountCombo->currentAccount();
111 if (account.isNull()) {
112 KMessageBox::sorry(
this, i18n(
"No account selected."));
113 }
else if (account->connection().isNull()) {
114 KMessageBox::sorry(
this, i18n(
"The requested account has been disconnected "
115 "and so the contact could not be added."));
116 }
else if (d->ui->screenNameLineEdit->text().isEmpty()) {
117 KMessageBox::sorry(
this, i18n(
"You did not specify the name of the contact to add."));
119 QStringList identifiers = QStringList() << d->ui->screenNameLineEdit->text();
120 kDebug() <<
"Requesting contacts for identifiers:" << identifiers;
122 Tp::PendingContacts *pendingContacts = account->connection()->contactManager()->contactsForIdentifiers(identifiers);
123 connect(pendingContacts, SIGNAL(finished(Tp::PendingOperation*)),
124 this, SLOT(_k_onContactsForIdentifiersFinished(Tp::PendingOperation*)));
130 void AddContactDialog::closeEvent(QCloseEvent *e)
133 if (!d->acceptInProgress) {
134 KDialog::closeEvent(e);
138 void AddContactDialog::_k_onContactsForIdentifiersFinished(Tp::PendingOperation *op)
141 kWarning() <<
"Failed to retrieve a contact for the given identifier"
142 << op->errorName() << op->errorMessage();
143 KMessageBox::sorry(
this, i18n(
"Failed to create new contact."));
144 setInProgress(
false);
146 kDebug() <<
"Requesting presence subscription";
148 Tp::PendingContacts *pc = qobject_cast<Tp::PendingContacts*>(op);
149 connect(pc->manager()->requestPresenceSubscription(pc->contacts()),
150 SIGNAL(finished(Tp::PendingOperation*)),
151 SLOT(_k_onRequestPresenceSubscriptionFinished(Tp::PendingOperation*)));
155 void AddContactDialog::_k_onRequestPresenceSubscriptionFinished(Tp::PendingOperation *op)
158 kWarning() <<
"Failed to request presence subscription"
159 << op->errorName() << op->errorMessage();
160 KMessageBox::sorry(
this, i18n(
"Failed to request presence subscription "
161 "from the requested contact."));
162 setInProgress(
false);
168 void AddContactDialog::setInProgress(
bool inProgress)
170 d->acceptInProgress = inProgress;
171 mainWidget()->setEnabled(!inProgress);
172 button(KDialog::Ok)->setEnabled(!inProgress);
173 button(KDialog::Cancel)->setEnabled(!inProgress);
178 #include "add-contact-dialog.moc"