File indexing completed on 2024-05-12 05:22:09

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jan Grulich <grulja@gmail.com>
0003     SPDX-FileCopyrightText: 2012-2018 Daniel Vrátil <dvratil@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "mainwindow.h"
0009 
0010 #include "contacts/contact.h"
0011 #include "contacts/contactfetchjob.h"
0012 #include "core/account.h"
0013 #include "core/authjob.h"
0014 
0015 #include <QListWidgetItem>
0016 
0017 MainWindow::MainWindow(QWidget *parent)
0018     : QMainWindow(parent)
0019 {
0020     /* Initialize GUI */
0021     ui.setupUi(this);
0022     ui.errorLabel->setVisible(false);
0023     connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
0024     connect(ui.contactListButton, &QAbstractButton::clicked, this, &MainWindow::fetchContactList);
0025     connect(ui.contactList, &QListWidget::itemSelectionChanged, this, &MainWindow::contactSelected);
0026 }
0027 
0028 void MainWindow::authenticate()
0029 {
0030     auto account = KGAPI2::AccountPtr::create();
0031     account->setScopes({KGAPI2::Account::contactsScopeUrl()});
0032 
0033     /* Create AuthJob to retrieve OAuth tokens for the account */
0034     auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
0035     connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() {
0036         /* Always remember to delete the jobs, otherwise your application will
0037          * leak memory. */
0038         authJob->deleteLater();
0039         if (authJob->error() != KGAPI2::NoError) {
0040             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
0041             ui.errorLabel->setVisible(true);
0042             return;
0043         }
0044 
0045         m_account = authJob->account();
0046 
0047         ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
0048         ui.contactListButton->setEnabled(true);
0049         ui.authButton->setEnabled(false);
0050     });
0051 }
0052 
0053 void MainWindow::fetchContactList()
0054 {
0055     if (m_account.isNull()) {
0056         ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
0057         ui.errorLabel->setVisible(true);
0058         ui.authButton->setVisible(true);
0059         return;
0060     }
0061 
0062     auto *fetchJob = new KGAPI2::ContactFetchJob(m_account, this);
0063     connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
0064         fetchJob->deleteLater();
0065 
0066         if (fetchJob->error() != KGAPI2::NoError) {
0067             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
0068             ui.errorLabel->setVisible(true);
0069             ui.contactListButton->setEnabled(true);
0070             return;
0071         }
0072 
0073         /* Get all items the job has retrieved */
0074         const auto objects = fetchJob->items();
0075         for (const auto &object : objects) {
0076             const auto contact = object.dynamicCast<KGAPI2::Contact>();
0077 
0078             /* Convert the contact to QListWidget item */
0079             auto *item = new QListWidgetItem(ui.contactList);
0080             item->setText(contact->name());
0081             item->setData(Qt::UserRole, contact->uid());
0082             ui.contactList->addItem(item);
0083         }
0084 
0085         ui.contactListButton->setEnabled(true);
0086     });
0087 
0088     ui.contactListButton->setEnabled(false);
0089 }
0090 
0091 void MainWindow::contactSelected()
0092 {
0093     if (ui.contactList->selectedItems().count() == 0) {
0094         ui.contactInfo->clear();
0095         return;
0096     }
0097 
0098     const auto id = ui.contactList->selectedItems().at(0)->data(Qt::UserRole).toString();
0099 
0100     auto *fetchJob = new KGAPI2::ContactFetchJob(id, m_account);
0101     connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
0102         fetchJob->deleteLater();
0103 
0104         if (fetchJob->error() != KGAPI2::NoError) {
0105             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
0106             ui.errorLabel->setVisible(true);
0107             ui.contactListButton->setEnabled(true);
0108             return;
0109         }
0110 
0111         /* Get all items we have received from Google (should be just one) */
0112         const auto objects = fetchJob->items();
0113         if (objects.count() != 1) {
0114             ui.errorLabel->setText(QStringLiteral("Error: Server sent unexpected amount of contacts"));
0115             ui.errorLabel->setVisible(true);
0116             return;
0117         }
0118 
0119         const auto contact = objects.first().dynamicCast<KGAPI2::Contact>();
0120         QString text = QStringLiteral("Name: %1").arg(contact->name());
0121 
0122         if (!contact->phoneNumbers().isEmpty()) {
0123             text += QLatin1Char('\n') % QStringLiteral("Phone: %1").arg(contact->phoneNumbers().at(0).number());
0124         }
0125 
0126         if (!contact->emails().isEmpty()) {
0127             text += QLatin1Char('\n') % QStringLiteral("Email: %1").arg(contact->emails().at(0));
0128         }
0129 
0130         ui.contactInfo->setText(text);
0131     });
0132 }
0133 
0134 #include "moc_mainwindow.cpp"