1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*
Copyright (C) 2012 Jan Grulich <grulja@gmail.com>
Copyright (C) 2012 - 2018 Daniel Vrátil <dvratil@kde.org>
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.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_main.h"
#include <contacts/contact.h>
#include <contacts/contactfetchjob.h>
#include <authjob.h>
#include <account.h>
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget * parent):
QMainWindow(parent),
m_ui(new Ui::MainWindow)
{
/* Initialice GUI */
m_ui->setupUi(this);
m_ui->errorLabel->setVisible(false);
connect(m_ui->authButton, &QAbstractButton::clicked,
this, &MainWindow::authenticate);
connect(m_ui->contactListButton, &QAbstractButton::clicked,
this, &MainWindow::fetchContactList);
connect(m_ui->contactList, &QListWidget::itemSelectionChanged,
this, &MainWindow::contactSelected);
}
MainWindow::~MainWindow()
{
delete m_ui;
}
void MainWindow::authenticate()
{
KGAPI2::AccountPtr account(new KGAPI2::Account);
account->setScopes( QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() );
/* Create AuthJob to retrieve OAuth tokens for the account */
KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob(
account,
QStringLiteral("554041944266.apps.googleusercontent.com"),
QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
connect(authJob, &KGAPI2::Job::finished,
this, &MainWindow::slotAuthJobFinished);
}
void MainWindow::slotAuthJobFinished(KGAPI2::Job *job)
{
KGAPI2::AuthJob *authJob = qobject_cast<KGAPI2::AuthJob*>(job);
Q_ASSERT(authJob);
/* Always remember to delete the jobs, otherwise your application will
* leak memory. */
authJob->deleteLater();
if (authJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
m_ui->errorLabel->setVisible(true);
return;
}
m_account = authJob->account();
m_ui->authStatusLabel->setText(QStringLiteral("Authenticated"));
m_ui->contactListButton->setEnabled(true);
m_ui->authButton->setEnabled(false);
}
void MainWindow::fetchContactList()
{
if (m_account.isNull()) {
m_ui->errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
m_ui->errorLabel->setVisible(true);
m_ui->authButton->setVisible(true);
return;
}
KGAPI2::ContactFetchJob *fetchJob = new KGAPI2::ContactFetchJob(m_account, this);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotFetchJobFinished);
m_ui->contactListButton->setEnabled(false);
}
void MainWindow::slotFetchJobFinished(KGAPI2::Job *job)
{
KGAPI2::ContactFetchJob *fetchJob = qobject_cast<KGAPI2::ContactFetchJob*>(job);
Q_ASSERT(fetchJob);
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
m_ui->errorLabel->setVisible(true);
m_ui->contactListButton->setEnabled(true);
return;
}
/* Get all items the job has retrieved */
const KGAPI2::ObjectsList objects = fetchJob->items();
for (const KGAPI2::ObjectPtr &object : objects) {
const KGAPI2::ContactPtr contact = object.dynamicCast<KGAPI2::Contact>();
/* Convert the contact to QListWidget item */
QListWidgetItem *item = new QListWidgetItem(m_ui->contactList);
item->setText(contact->name());
item->setData(Qt::UserRole, contact->uid());
m_ui->contactList->addItem(item);
}
m_ui->contactListButton->setEnabled(true);
}
void MainWindow::contactSelected()
{
if (m_ui->contactList->selectedItems().count() == 0) {
m_ui->contactInfo->clear();
return;
}
const QString id = m_ui->contactList->selectedItems().at(0)->data(Qt::UserRole).toString();
KGAPI2::ContactFetchJob *fetchJob = new KGAPI2::ContactFetchJob(id, m_account);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotContactFetchJobFinished);
}
void MainWindow::slotContactFetchJobFinished(KGAPI2::Job *job)
{
KGAPI2::ContactFetchJob *fetchJob = qobject_cast<KGAPI2::ContactFetchJob*>(job);
Q_ASSERT(fetchJob);
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
m_ui->errorLabel->setVisible(true);
m_ui->contactListButton->setEnabled(true);
return;
}
/* Get all items we have received from Google (should be just one) */
KGAPI2::ObjectsList objects = fetchJob->items();
if (objects.count() != 1) {
m_ui->errorLabel->setText(QStringLiteral("Error: Server sent unexpected amount of contacts"));
m_ui->errorLabel->setVisible(true);
return;
}
KGAPI2::ContactPtr contact = objects.first().dynamicCast<KGAPI2::Contact>();
QString text = QStringLiteral("Name: %1").arg(contact->name());
if (!contact->phoneNumbers().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Phone: %1").arg(contact->phoneNumbers().at(0).number());
}
if (!contact->emails().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Email: %1").arg(contact->emails().at(0));
}
m_ui->contactInfo->setText(text);
}
|