• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
  • Widgets
add-contact-dialog.cpp
Go to the documentation of this file.
1 /*
2  * Add contact dialog
3  *
4  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
5  * Copyright (C) 2012 George Kiagiadakis <kiagiadakis.george@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "add-contact-dialog.h"
23 #include "ui_add-contact-dialog.h"
24 
25 #include <QObject>
26 #include <QCloseEvent>
27 
28 #include <KMessageBox>
29 #include <KPushButton>
30 #include <KDebug>
31 
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>
39 
40 namespace KTp {
41 
42 class KTP_NO_EXPORT SubscribableAccountFilter : public Tp::AccountFilter
43 {
44 public:
45  SubscribableAccountFilter()
46  {
47  }
48 
49  bool isValid() const
50  {
51  //return whether the filter is valid which is always true as this filter is hardcoded
52  return true;
53  }
54 
55  bool matches(const Tp::AccountPtr &account) const
56  {
57  //if there's no connection we can't add contacts as we have no contactmanager
58  if (! account->connection()) {
59  return false;
60  }
61 
62  //only show items which can add a contact (i.e hide accounts like IRC which are online but there's no point listing)
63  if (! account->connection()->contactManager()->canRequestPresenceSubscription()){
64  return false;
65  }
66  return true;
67  }
68 };
69 
70 
71 struct KTP_NO_EXPORT AddContactDialog::Private
72 {
73  Private() :
74  ui(new Ui::AddContactDialog),
75  acceptInProgress(false)
76  {}
77 
78  Ui::AddContactDialog *ui;
79  bool acceptInProgress;
80 };
81 
82 AddContactDialog::AddContactDialog(const Tp::AccountManagerPtr &accountManager, QWidget *parent) :
83  KDialog(parent),
84  d(new Private)
85 {
86  setWindowTitle(i18n("Add new contact"));
87  setWindowIcon(QIcon::fromTheme(QLatin1String("list-add-user")));
88 
89  QWidget *widget = new QWidget(this);
90  d->ui->setupUi(widget);
91  setMainWidget(widget);
92 
93  Tp::AccountFilterPtr filter = Tp::AccountFilterPtr(new KTp::SubscribableAccountFilter());
94  Tp::AccountSetPtr accountSet = accountManager->filterAccounts(filter);
95 
96  d->ui->accountCombo->setAccountSet(accountSet);
97 
98  d->ui->screenNameLineEdit->setFocus();
99 }
100 
101 AddContactDialog::~AddContactDialog()
102 {
103  delete d->ui;
104  delete d;
105 }
106 
107 void AddContactDialog::accept()
108 {
109  Tp::AccountPtr account = d->ui->accountCombo->currentAccount();
110 
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."));
118  } else {
119  QStringList identifiers = QStringList() << d->ui->screenNameLineEdit->text();
120  kDebug() << "Requesting contacts for identifiers:" << identifiers;
121 
122  Tp::PendingContacts *pendingContacts = account->connection()->contactManager()->contactsForIdentifiers(identifiers);
123  connect(pendingContacts, SIGNAL(finished(Tp::PendingOperation*)),
124  this, SLOT(_k_onContactsForIdentifiersFinished(Tp::PendingOperation*)));
125 
126  setInProgress(true);
127  }
128 }
129 
130 void AddContactDialog::closeEvent(QCloseEvent *e)
131 {
132  // ignore close event if we are in the middle of an operation
133  if (!d->acceptInProgress) {
134  KDialog::closeEvent(e);
135  }
136 }
137 
138 void AddContactDialog::_k_onContactsForIdentifiersFinished(Tp::PendingOperation *op)
139 {
140  if (op->isError()) {
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);
145  } else {
146  kDebug() << "Requesting presence subscription";
147 
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*)));
152  }
153 }
154 
155 void AddContactDialog::_k_onRequestPresenceSubscriptionFinished(Tp::PendingOperation *op)
156 {
157  if (op->isError()) {
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);
163  } else {
164  QDialog::accept();
165  }
166 }
167 
168 void AddContactDialog::setInProgress(bool inProgress)
169 {
170  d->acceptInProgress = inProgress;
171  mainWidget()->setEnabled(!inProgress);
172  button(KDialog::Ok)->setEnabled(!inProgress);
173  button(KDialog::Cancel)->setEnabled(!inProgress);
174 }
175 
176 } //namespace KTp
177 
178 #include "add-contact-dialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal