File: mainwindow.cpp

package info (click to toggle)
libkgapi5 22.12.3-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,452 kB
  • sloc: cpp: 29,921; ansic: 979; xml: 142; makefile: 18; sh: 1
file content (132 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2012 Jan Grulich <grulja@gmail.com>
    SPDX-FileCopyrightText: 2012-2018 Daniel Vrátil <dvratil@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "mainwindow.h"

#include "contacts/contact.h"
#include "contacts/contactfetchjob.h"
#include "core/account.h"
#include "core/authjob.h"

#include <QListWidgetItem>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* Initialize GUI */
    ui.setupUi(this);
    ui.errorLabel->setVisible(false);
    connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
    connect(ui.contactListButton, &QAbstractButton::clicked, this, &MainWindow::fetchContactList);
    connect(ui.contactList, &QListWidget::itemSelectionChanged, this, &MainWindow::contactSelected);
}

void MainWindow::authenticate()
{
    auto account = KGAPI2::AccountPtr::create();
    account->setScopes({KGAPI2::Account::contactsScopeUrl()});

    /* Create AuthJob to retrieve OAuth tokens for the account */
    auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
    connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() {
        /* Always remember to delete the jobs, otherwise your application will
         * leak memory. */
        authJob->deleteLater();
        if (authJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
            ui.errorLabel->setVisible(true);
            return;
        }

        m_account = authJob->account();

        ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
        ui.contactListButton->setEnabled(true);
        ui.authButton->setEnabled(false);
    });
}

void MainWindow::fetchContactList()
{
    if (m_account.isNull()) {
        ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
        ui.errorLabel->setVisible(true);
        ui.authButton->setVisible(true);
        return;
    }

    auto *fetchJob = new KGAPI2::ContactFetchJob(m_account, this);
    connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
        fetchJob->deleteLater();

        if (fetchJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.contactListButton->setEnabled(true);
            return;
        }

        /* Get all items the job has retrieved */
        const auto objects = fetchJob->items();
        for (const auto &object : objects) {
            const auto contact = object.dynamicCast<KGAPI2::Contact>();

            /* Convert the contact to QListWidget item */
            auto *item = new QListWidgetItem(ui.contactList);
            item->setText(contact->name());
            item->setData(Qt::UserRole, contact->uid());
            ui.contactList->addItem(item);
        }

        ui.contactListButton->setEnabled(true);
    });

    ui.contactListButton->setEnabled(false);
}

void MainWindow::contactSelected()
{
    if (ui.contactList->selectedItems().count() == 0) {
        ui.contactInfo->clear();
        return;
    }

    const auto id = ui.contactList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto *fetchJob = new KGAPI2::ContactFetchJob(id, m_account);
    connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
        fetchJob->deleteLater();

        if (fetchJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.contactListButton->setEnabled(true);
            return;
        }

        /* Get all items we have received from Google (should be just one) */
        const auto objects = fetchJob->items();
        if (objects.count() != 1) {
            ui.errorLabel->setText(QStringLiteral("Error: Server sent unexpected amount of contacts"));
            ui.errorLabel->setVisible(true);
            return;
        }

        const auto 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));
        }

        ui.contactInfo->setText(text);
    });
}