diff --git a/resources/google-new/CMakeLists.txt b/resources/google-new/CMakeLists.txt index eaa8ed31c..92846cfeb 100644 --- a/resources/google-new/CMakeLists.txt +++ b/resources/google-new/CMakeLists.txt @@ -1,75 +1,76 @@ add_definitions(-DTRANSLATION_DOMAIN=\"akonadi_google_resource\") set(googleresource_SRCS googleresource.cpp - settingsdialog.cpp googleaccountmanager.cpp googlesettings.cpp googlesettingsdialog.cpp kgapiversionattribute.cpp defaultreminderattribute.cpp generichandler.cpp calendarhandler.cpp taskhandler.cpp contacthandler.cpp ${accounts_SRCS}) if (ECM_VERSION VERSION_LESS "5.68.0") ecm_qt_declare_logging_category(googleresource_SRCS HEADER googleresource_debug.h IDENTIFIER GOOGLE_LOG CATEGORY_NAME org.kde.pim.google) else() ecm_qt_declare_logging_category(googleresource_SRCS HEADER googleresource_debug.h IDENTIFIER GOOGLE_LOG CATEGORY_NAME org.kde.pim.google DESCRIPTION "resource google (kdepim-runtime)" EXPORT KDEPIMRUNTIME ) endif() +ki18n_wrap_ui(googleresource_SRCS googlesettingsdialog.ui) + kconfig_add_kcfg_files(googleresource_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/settingsbase.kcfgc) kcfg_generate_dbus_interface( ${CMAKE_CURRENT_SOURCE_DIR}/settingsbase.kcfg org.kde.Akonadi.Google.Settings ) qt5_add_dbus_adaptor(googleresource_SRCS ${CMAKE_CURRENT_BINARY_DIR}/org.kde.Akonadi.Google.Settings.xml ${CMAKE_CURRENT_SOURCE_DIR}/googlesettings.h GoogleSettings ) add_executable(akonadi_google_resource ${googleresource_SRCS}) if( APPLE ) set_target_properties(akonadi_google_resource PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/../../Info.plist.template ) set_target_properties(akonadi_google_resource PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "org.kde.Akonadi.google" ) set_target_properties(akonadi_google_resource PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "KDE Akonadi Google Resource" ) endif() target_link_libraries(akonadi_google_resource KF5::AkonadiCalendar KF5::AkonadiCore KF5::AkonadiAgentBase KF5::CalendarCore KF5::Contacts KF5::Wallet KF5::I18n KF5::WindowSystem KF5::Completion KF5::TextWidgets KPim::GAPICalendar KPim::GAPIContacts KPim::GAPICore KPim::GAPITasks ) install(TARGETS akonadi_google_resource ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install( FILES googleresource.desktop DESTINATION "${KDE_INSTALL_DATAROOTDIR}/akonadi/agents" ) diff --git a/resources/google-new/googleresource.cpp b/resources/google-new/googleresource.cpp index e1a4875a0..28670ab7f 100644 --- a/resources/google-new/googleresource.cpp +++ b/resources/google-new/googleresource.cpp @@ -1,601 +1,603 @@ /* Copyright (C) 2011-2013 Daniel Vrátil 2020 Igor Poboiko This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "googleresource.h" #include "googlesettings.h" +#include "googlesettingsdialog.h" +#include "googleresource_debug.h" +#include "settingsadaptor.h" #include "calendarhandler.h" #include "contacthandler.h" #include "taskhandler.h" + #include "defaultreminderattribute.h" -#include "googleresource_debug.h" #include "kgapiversionattribute.h" -#include "settingsadaptor.h" -#include "settingsdialog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ACCESS_TOKEN_PROPERTY "AccessToken" #define CALENDARS_PROPERTY "_KGAPI2CalendarPtr" #define ROOT_COLLECTION_REMOTEID QStringLiteral("RootCollection") Q_DECLARE_METATYPE(KGAPI2::Job *) using namespace KGAPI2; using namespace Akonadi; GoogleResource::GoogleResource(const QString &id) : ResourceBase(id) , AgentBase::ObserverV2() , m_isConfiguring(false) { AttributeFactory::registerAttribute< DefaultReminderAttribute >(); AttributeFactory::registerAttribute(); connect(this, &GoogleResource::abortRequested, this, [this](){ cancelTask(i18n("Aborted")); }); connect(this, &GoogleResource::reloadConfiguration, this, &GoogleResource::reloadConfig); setNeedsNetwork(true); changeRecorder()->itemFetchScope().fetchFullPayload(true); changeRecorder()->itemFetchScope().setAncestorRetrieval(ItemFetchScope::All); changeRecorder()->fetchCollection(true); changeRecorder()->collectionFetchScope().setAncestorRetrieval(CollectionFetchScope::All); m_accountMgr = new GoogleAccountManager(this); connect(m_accountMgr, &GoogleAccountManager::accountChanged, this, [this](const AccountPtr &account){ if (accountId() > 0) { return; } m_account = account; }); connect(m_accountMgr, &GoogleAccountManager::accountRemoved, this, [this](const QString &accountName){ if (accountId() > 0) { return; } if (m_account && m_account->accountName() != accountName) { return; } Q_EMIT status(NotConfigured, i18n("Configured account has been removed")); m_account.clear(); GoogleSettings::self()->setAccount(QString()); }); connect(m_accountMgr, &GoogleAccountManager::managerReady, this, [this](bool ready){ if (accountId() > 0) { return; } if (!ready) { Q_EMIT status(Broken, i18n("Can't access KWallet")); return; } const QString accountName = GoogleSettings::self()->account(); if (accountName.isEmpty()) { Q_EMIT status(NotConfigured); return; } m_account = m_accountMgr->findAccount(accountName); if (m_account.isNull()) { Q_EMIT status(NotConfigured, i18n("Configured account does not exist")); return; } Q_EMIT status(Idle, i18nc("@info:status", "Ready")); synchronize(); }); Q_EMIT status(NotConfigured, i18n("Waiting for KWallet...")); updateResourceName(); m_freeBusyHandler.reset(new CalendarHandler(this)); m_handlers << m_freeBusyHandler; m_handlers << GenericHandler::Ptr(new ContactHandler(this)); m_handlers << GenericHandler::Ptr(new TaskHandler(this)); for (auto handler : m_handlers) { connect(handler.data(), &GenericHandler::status, [this](int code, QString message){ Q_EMIT status(code, message); }); connect(handler.data(), &GenericHandler::percent, [this](int value){ Q_EMIT percent(value); }); connect(handler.data(), &GenericHandler::collectionsRetrieved, this, &GoogleResource::collectionsPartiallyRetrieved); } new SettingsAdaptor(GoogleSettings::self()); QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"), GoogleSettings::self(), QDBusConnection::ExportAdaptors); } GoogleResource::~GoogleResource() { } void GoogleResource::cleanup() { accountManager()->cleanup(GoogleSettings::self()->account()); ResourceBase::cleanup(); } AccountPtr GoogleResource::account() const { return m_account; } GoogleAccountManager *GoogleResource::accountManager() const { return m_accountMgr; } Akonadi::Collection GoogleResource::rootCollection() const { return m_rootCollection; } void GoogleResource::configure(WId windowId) { - if (!m_accountMgr->isReady() || m_isConfiguring) { +/* if (!m_accountMgr->isReady() || m_isConfiguring) { Q_EMIT configurationDialogAccepted(); return; - } + }*/ m_isConfiguring = true; - QScopedPointer settingsDialog(new SettingsDialog(accountManager(), windowId, this)); + QScopedPointer settingsDialog(new GoogleSettingsDialog(this, windowId)); settingsDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("im-google"))); if (settingsDialog->exec() == QDialog::Accepted) { updateResourceName(); Q_EMIT configurationDialogAccepted(); m_account = accountManager()->findAccount(GoogleSettings::self()->account()); if (m_account.isNull()) { Q_EMIT status(NotConfigured, i18n("Configured account does not exist")); m_isConfiguring = false; return; } Q_EMIT status(Idle, i18nc("@info:status", "Ready")); synchronize(); } else { updateResourceName(); Q_EMIT configurationDialogRejected(); } + m_isConfiguring = false; } QList GoogleResource::scopes() const { // TODO: determine it based on what user wants? const QList< QUrl > scopes = {Account::accountInfoScopeUrl(), Account::calendarScopeUrl(), Account::contactsScopeUrl(), Account::tasksScopeUrl()}; return scopes; } void GoogleResource::updateResourceName() { const QString accountName = GoogleSettings::self()->account(); setName(i18nc("%1 is account name (user@gmail.com)", "Google Groupware (%1)", accountName.isEmpty() ? i18n("not configured") : accountName)); } void GoogleResource::updateAccountToken(const AccountPtr &account, KGAPI2::Job *restartJob) { if (!GoogleSettings::self()->account().isEmpty()) { AuthJob *authJob = new AuthJob(account, GoogleSettings::self()->clientId(), GoogleSettings::self()->clientSecret(), this); authJob->setProperty(JOB_PROPERTY, QVariant::fromValue(restartJob)); connect(authJob, &AuthJob::finished, this, &GoogleResource::slotAuthJobFinished); } } void GoogleResource::reloadConfig() { const QString accountName = GoogleSettings::self()->account(); if (!accountName.isEmpty()) { m_account = m_accountMgr->findAccount(accountName); if (m_account.isNull()) { Q_EMIT status(NotConfigured, i18n("Configured account does not exist")); return; } } else { Q_EMIT status(NotConfigured); return; } Q_EMIT status(Idle, i18nc("@info:status", "Ready")); } bool GoogleResource::handleError(KGAPI2::Job *job, bool _cancelTask) { if ((job->error() == KGAPI2::NoError) || (job->error() == KGAPI2::OK)) { return true; } qCDebug(GOOGLE_LOG) << job << job->errorString(); if (job->error() == KGAPI2::Unauthorized) { const QList resourceScopes = scopes(); for (const QUrl &scope : resourceScopes) { if (!m_account->scopes().contains(scope)) { m_account->addScope(scope); } } updateAccountToken(m_account, job); return false; } if (_cancelTask) { cancelTask(job->errorString()); } return false; } bool GoogleResource::canPerformTask() { if (!m_account && accountId() == 0) { cancelTask(i18nc("@info:status", "Resource is not configured")); Q_EMIT status(NotConfigured, i18nc("@info:status", "Resource is not configured")); return false; } return true; } void GoogleResource::slotAuthJobFinished(KGAPI2::Job *job) { if (job->error() != KGAPI2::NoError) { cancelTask(i18n("Failed to refresh tokens")); return; } AuthJob *authJob = qobject_cast(job); m_account = authJob->account(); if (!m_accountMgr->storeAccount(m_account)) { qWarning() << "Failed to store account in KWallet"; } KGAPI2::Job *otherJob = job->property(JOB_PROPERTY).value(); if (otherJob) { otherJob->setAccount(m_account); otherJob->restart(); } job->deleteLater(); } void GoogleResource::slotGenericJobFinished(KGAPI2::Job *job) { if (!handleError(job)) { return; } qCDebug(GOOGLE_LOG) << "Job finished"; const Item item = job->property(ITEM_PROPERTY).value(); const Collection collection = job->property(COLLECTION_PROPERTY).value(); if (item.isValid()) { changeCommitted(item); } else if (collection.isValid()) { changeCommitted(collection); } else { taskDone(); } Q_EMIT status(Idle, i18nc("@info:status", "Ready")); } int GoogleResource::accountId() const { return 0; } QDateTime GoogleResource::lastCacheUpdate() const { if (m_freeBusyHandler) { return m_freeBusyHandler->lastCacheUpdate(); } return QDateTime(); } void GoogleResource::canHandleFreeBusy(const QString &email) const { if (m_freeBusyHandler) { m_freeBusyHandler->canHandleFreeBusy(email); } else { handlesFreeBusy(email, false); } } void GoogleResource::retrieveFreeBusy(const QString &email, const QDateTime &start, const QDateTime &end) { if (m_freeBusyHandler) { m_freeBusyHandler->retrieveFreeBusy(email, start, end); } else { freeBusyRetrieved(email, QString(), false, QString()); } } /* * Collection handling */ void GoogleResource::retrieveCollections() { qCDebug(GOOGLE_LOG) << "Retrieve Collections"; if (!canPerformTask()) { return; } CachePolicy cachePolicy; if (GoogleSettings::self()->enableIntervalCheck()) { cachePolicy.setInheritFromParent(false); cachePolicy.setIntervalCheckTime(GoogleSettings::self()->intervalCheckTime()); } // Setting up root collection m_rootCollection = Collection(); m_rootCollection.setContentMimeTypes({ Collection::mimeType(), Collection::virtualMimeType() }); m_rootCollection.setRemoteId(ROOT_COLLECTION_REMOTEID); m_rootCollection.setName(account()->accountName()); m_rootCollection.setParentCollection(Collection::root()); m_rootCollection.setRights(Collection::CanCreateCollection); m_rootCollection.setCachePolicy(cachePolicy); EntityDisplayAttribute *attr = m_rootCollection.attribute(Collection::AddIfMissing); attr->setDisplayName(account()->accountName()); attr->setIconName(QStringLiteral("im-google")); m_collections = { m_rootCollection }; m_jobs = 0; for (auto handler : m_handlers) { handler->retrieveCollections(); m_jobs++; } } void GoogleResource::collectionsPartiallyRetrieved(const Collection::List& collections) { m_jobs--; m_collections << collections; if (m_jobs == 0) { qCDebug(GOOGLE_LOG) << "Collections retrieved!"; collectionsRetrieved(m_collections); } } void GoogleResource::retrieveItems(const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (collection.contentMimeTypes().contains(handler->mimetype())) { handler->retrieveItems(collection); found = true; break; } } if (!found) { qCWarning(GOOGLE_LOG) << "Unknown collection" << collection.name(); itemsRetrieved({}); } } void GoogleResource::itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } if (collection.parentCollection() == Akonadi::Collection::root()) { cancelTask(i18n("The top-level collection cannot contain anything")); return; } bool found = false; for (auto handler : m_handlers) { if (collection.contentMimeTypes().contains(handler->mimetype()) && handler->canPerformTask(item)) { handler->itemAdded(item, collection); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::itemChanged(const Akonadi::Item &item, const QSet< QByteArray > &partIdentifiers) { Q_UNUSED(partIdentifiers); if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (handler->canPerformTask(item)) { handler->itemChanged(item, partIdentifiers); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::itemRemoved(const Akonadi::Item &item) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (item.mimeType() == handler->mimetype()) { handler->itemRemoved(item); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::itemMoved(const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) { if (!canPerformTask()) { return; } if (collectionDestination.parentCollection() == Akonadi::Collection::root()) { cancelTask(i18n("The top-level collection cannot contain anything")); return; } bool found = false; for (auto handler : m_handlers) { if (handler->canPerformTask(item)) { handler->itemMoved(item, collectionSource, collectionDestination); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::itemLinked(const Akonadi::Item &item, const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (handler->canPerformTask(item)) { handler->itemLinked(item, collection); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::itemUnlinked(const Akonadi::Item &item, const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (handler->canPerformTask(item)) { handler->itemUnlinked(item, collection); found = true; break; } } if (!found) { cancelTask(i18n("Invalid payload type")); } } void GoogleResource::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (collection.contentMimeTypes().contains(handler->mimetype())) { handler->retrieveItems(collection); found = true; break; } } if (!found) { cancelTask(i18n("Unknown collection mimetype")); } } void GoogleResource::collectionChanged(const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (collection.contentMimeTypes().contains(handler->mimetype())) { handler->retrieveItems(collection); found = true; break; } } if (!found) { cancelTask(i18n("Unknown collection mimetype")); } } void GoogleResource::collectionRemoved(const Akonadi::Collection &collection) { if (!canPerformTask()) { return; } bool found = false; for (auto handler : m_handlers) { if (collection.contentMimeTypes().contains(handler->mimetype())) { handler->retrieveItems(collection); found = true; break; } } if (!found) { cancelTask(i18n("Unknown collection mimetype")); } } AKONADI_RESOURCE_MAIN(GoogleResource) diff --git a/resources/google-new/googleresource.h b/resources/google-new/googleresource.h index b80387663..178fe227c 100644 --- a/resources/google-new/googleresource.h +++ b/resources/google-new/googleresource.h @@ -1,131 +1,132 @@ /* Copyright (C) 2011-2013 Daniel Vrátil This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef GOOGLERESOURCE_H #define GOOGLERESOURCE_H #include #include #include #include #include "googleaccountmanager.h" #include "generichandler.h" #include "calendarhandler.h" #include #include #define ITEM_PROPERTY "_AkonadiItem" #define COLLECTION_PROPERTY "_AkonadiCollection" #define JOB_PROPERTY "_KGAPI2Job" namespace KGAPI2 { class Job; } class GoogleSettings; class GoogleResource : public Akonadi::ResourceBase, public Akonadi::AgentBase::ObserverV2, public Akonadi::FreeBusyProviderBase { Q_OBJECT public: explicit GoogleResource(const QString &id); ~GoogleResource() override; QList scopes() const; void cleanup() override; public Q_SLOTS: void configure(WId windowId) override; void reloadConfig(); protected: int runConfigurationDialog(WId windowId); void updateResourceName(); // Freebusy QDateTime lastCacheUpdate() const override; void canHandleFreeBusy(const QString &email) const override; void retrieveFreeBusy(const QString &email, const QDateTime &start, const QDateTime &end) override; void updateAccountToken(const KGAPI2::AccountPtr &account, KGAPI2::Job *restartJob = nullptr); template bool canPerformTask(const Akonadi::Item &item, const QString &mimeType = QString()) { if (item.isValid() && !item.hasPayload()) { cancelTask(i18n("Invalid item payload.")); return false; } else if (item.isValid() && mimeType != item.mimeType()) { cancelTask(i18n("Invalid payload MIME type. Expected %1, found %2", mimeType, item.mimeType())); return false; } return canPerformTask(); } bool canPerformTask(); KGAPI2::AccountPtr account() const; /** * KAccounts support abstraction. * * Returns 0 when compiled without KAccounts or not configured for KAccounts */ int accountId() const; GoogleAccountManager *accountManager() const; Akonadi::Collection rootCollection() const; protected Q_SLOTS: void retrieveCollections() override; void retrieveItems(const Akonadi::Collection &collection) override; void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) override; void itemChanged(const Akonadi::Item &item, const QSet< QByteArray > &partIdentifiers) override; void itemRemoved(const Akonadi::Item &item) override; void itemMoved(const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) override; void itemLinked(const Akonadi::Item &item, const Akonadi::Collection &collection) override; void itemUnlinked(const Akonadi::Item &item, const Akonadi::Collection &collection) override; void collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) override; void collectionChanged(const Akonadi::Collection &collection) override; void collectionRemoved(const Akonadi::Collection &collection) override; bool handleError(KGAPI2::Job *job, bool cancelTask = true); void collectionsPartiallyRetrieved(const Akonadi::Collection::List &collections); virtual void slotAuthJobFinished(KGAPI2::Job *job); virtual void slotGenericJobFinished(KGAPI2::Job *job); private: bool m_isConfiguring = false; GoogleAccountManager *m_accountMgr = nullptr; KGAPI2::AccountPtr m_account; Akonadi::Collection m_rootCollection; Akonadi::Collection::List m_collections; QList m_handlers; CalendarHandler::Ptr m_freeBusyHandler; int m_jobs; + friend class GoogleSettingsDialog; friend class GenericHandler; friend class CalendarHandler; friend class ContactHandler; friend class TaskHandler; }; #endif // GOOGLERESOURCE_H diff --git a/resources/google-new/googlesettingsdialog.cpp b/resources/google-new/googlesettingsdialog.cpp index 17b473df8..4ec3444c6 100644 --- a/resources/google-new/googlesettingsdialog.cpp +++ b/resources/google-new/googlesettingsdialog.cpp @@ -1,265 +1,288 @@ /* Copyright (C) 2013 Daniel Vrátil + 2020 Igor Poboiko This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "googlesettingsdialog.h" -#include "googleaccountmanager.h" +#include "ui_googlesettingsdialog.h" #include "googlesettings.h" +#include "googleaccountmanager.h" #include "googleresource.h" +#include "googleresource_debug.h" -#include -#include -#include -#include +#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include -#include - -#include -#include -#include - -Q_DECLARE_METATYPE(KGAPI2::Job *) using namespace KGAPI2; -GoogleSettingsDialog::GoogleSettingsDialog(GoogleAccountManager *accountManager, WId wId, GoogleResource *parent) +GoogleSettingsDialog::GoogleSettingsDialog(GoogleResource *resource, WId wId) : QDialog() - , m_parentResource(parent) - , m_accountManager(accountManager) + , m_resource(resource) { - setAttribute(Qt::WA_NativeWindow, true); - KWindowSystem::setMainWindow(windowHandle(), wId); + if (wId) { + setAttribute(Qt::WA_NativeWindow, true); + KWindowSystem::setMainWindow(windowHandle(), wId); + } + QVBoxLayout *mainLayout = new QVBoxLayout(this); + + QWidget *mainWidget = new QWidget(this); + mainLayout->addWidget(mainWidget); + QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); - QVBoxLayout *topLayout = new QVBoxLayout(this); QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); okButton->setDefault(true); okButton->setShortcut(Qt::CTRL | Qt::Key_Return); + mainLayout->addWidget(buttonBox); + + m_ui = new Ui::GoogleSettingsDialog; + m_ui->setupUi(mainWidget); + + m_ui->refreshSpinBox->setSuffix(ki18np(" minute", " minutes")); + m_ui->enableRefresh->setChecked(GoogleSettings::self()->enableIntervalCheck()); + m_ui->refreshSpinBox->setEnabled(GoogleSettings::self()->enableIntervalCheck()); + if (GoogleSettings::self()->enableIntervalCheck()) { + m_ui->refreshSpinBox->setValue(GoogleSettings::self()->intervalCheckTime()); + } else { + m_ui->refreshSpinBox->setValue(30); + } + + m_ui->eventsLimitCombo->setMaximumDate(QDate::currentDate()); + m_ui->eventsLimitCombo->setMinimumDate(QDate::fromString(QStringLiteral("2000-01-01"), Qt::ISODate)); + m_ui->eventsLimitCombo->setOptions(KDateComboBox::EditDate | KDateComboBox::SelectDate + |KDateComboBox::DatePicker | KDateComboBox::WarnOnInvalid); + if (GoogleSettings::self()->eventsSince().isEmpty()) { + const QString ds = QStringLiteral("%1-01-01").arg(QString::number(QDate::currentDate().year() - 3)); + m_ui->eventsLimitCombo->setDate(QDate::fromString(ds, Qt::ISODate)); + } else { + m_ui->eventsLimitCombo->setDate(QDate::fromString(GoogleSettings::self()->eventsSince(), Qt::ISODate)); + } + connect(buttonBox, &QDialogButtonBox::accepted, this, &GoogleSettingsDialog::slotSaveSettings); connect(buttonBox, &QDialogButtonBox::rejected, this, &GoogleSettingsDialog::reject); - - QWidget *widget = new QWidget(this); - topLayout->addWidget(widget); - topLayout->addWidget(buttonBox); - QVBoxLayout *mainLayout = new QVBoxLayout(widget); - mainLayout->setContentsMargins(0, 0, 0, 0); - m_mainLayout = mainLayout; - - m_accGroupBox = new QGroupBox(i18n("Accounts"), this); - mainLayout->addWidget(m_accGroupBox); - QHBoxLayout *accLayout = new QHBoxLayout(m_accGroupBox); - - m_accComboBox = new QComboBox(m_accGroupBox); - accLayout->addWidget(m_accComboBox, 1); - connect(m_accComboBox, QOverload::of(&QComboBox::currentTextChanged), this, &GoogleSettingsDialog::currentAccountChanged); - - m_addAccButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-add-user")), i18n("&Add"), m_accGroupBox); - accLayout->addWidget(m_addAccButton); - connect(m_addAccButton, &QPushButton::clicked, this, &GoogleSettingsDialog::slotAddAccountClicked); - - m_removeAccButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-remove-user")), i18n("&Remove"), m_accGroupBox); - accLayout->addWidget(m_removeAccButton); - connect(m_removeAccButton, &QPushButton::clicked, this, &GoogleSettingsDialog::slotRemoveAccountClicked); - - QGroupBox *refreshBox = new QGroupBox(i18n("Refresh"), this); - mainLayout->addWidget(refreshBox); - QGridLayout *refreshLayout = new QGridLayout(refreshBox); - - m_enableRefresh = new QCheckBox(i18n("Enable interval refresh"), refreshBox); - m_enableRefresh->setChecked(GoogleSettings::self()->enableIntervalCheck()); - refreshLayout->addWidget(m_enableRefresh, 0, 0, 1, 2); - - QLabel *label = new QLabel(i18n("Refresh interval:")); - refreshLayout->addWidget(label, 1, 0); - m_refreshSpinBox = new KPluralHandlingSpinBox(this); - m_refreshSpinBox->setMaximum(720); - m_refreshSpinBox->setMinimum(10); - m_refreshSpinBox->setSingleStep(1); - m_refreshSpinBox->setValue(30); - m_refreshSpinBox->setDisplayIntegerBase(10); - m_refreshSpinBox->setSuffix(ki18np(" minute", " minutes")); - m_refreshSpinBox->setEnabled(GoogleSettings::self()->enableIntervalCheck()); - refreshLayout->addWidget(m_refreshSpinBox, 1, 1); - connect(m_enableRefresh, &QCheckBox::toggled, m_refreshSpinBox, &KPluralHandlingSpinBox::setEnabled); - - if (m_enableRefresh->isEnabled()) { - m_refreshSpinBox->setValue(GoogleSettings::self()->intervalCheckTime()); + connect(m_ui->reloadCalendarsBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotReloadCalendars); + connect(m_ui->reloadTaskListsBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotReloadTaskLists); + connect(m_ui->configureBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotConfigure); + if (!GoogleSettings::self()->account().isEmpty()) { + m_account = m_resource->accountManager()->findAccount(GoogleSettings::self()->account()); } - QMetaObject::invokeMethod(this, &GoogleSettingsDialog::reloadAccounts, Qt::QueuedConnection); + QMetaObject::invokeMethod(this, &GoogleSettingsDialog::accountChanged, Qt::QueuedConnection); } GoogleSettingsDialog::~GoogleSettingsDialog() { } -QVBoxLayout *GoogleSettingsDialog::mainLayout() const -{ - return m_mainLayout; -} - -GoogleAccountManager *GoogleSettingsDialog::accountManager() const -{ - return m_accountManager; -} - -KGAPI2::AccountPtr GoogleSettingsDialog::currentAccount() const -{ - return m_accountManager->findAccount(m_accComboBox->currentText()); -} - -void GoogleSettingsDialog::reloadAccounts() +bool GoogleSettingsDialog::handleError(Job *job) { - disconnect(m_accComboBox, QOverload::of(&QComboBox::currentTextChanged), this, &GoogleSettingsDialog::currentAccountChanged); + if ((job->error() == KGAPI2::NoError) || (job->error() == KGAPI2::OK)) { + return true; + } - m_accComboBox->clear(); + if (job->error() == KGAPI2::Unauthorized) { + qCDebug(GOOGLE_LOG) << job << job->errorString(); + const QList resourceScopes = m_resource->scopes(); + for (const QUrl &scope : resourceScopes) { + if (!m_account->scopes().contains(scope)) { + m_account->addScope(scope); + } + } - const AccountsList accounts = m_accountManager->listAccounts(); - for (const AccountPtr &account : accounts) { - m_accComboBox->addItem(account->accountName()); - } + AuthJob *authJob = new AuthJob(m_account, GoogleSettings::self()->clientId(), + GoogleSettings::self()->clientSecret(), this); + authJob->setProperty(JOB_PROPERTY, QVariant::fromValue(job)); + connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAuthJobFinished); - int index = m_accComboBox->findText(GoogleSettings::self()->account(), Qt::MatchExactly); - if (index > -1) { - m_accComboBox->setCurrentIndex(index); + return false; } - connect(m_accComboBox, QOverload::of(&QComboBox::currentTextChanged), this, &GoogleSettingsDialog::currentAccountChanged); + KMessageBox::sorry(this, job->errorString()); + return false; +} - m_removeAccButton->setEnabled(m_accComboBox->count() > 0); - Q_EMIT currentAccountChanged(m_accComboBox->currentText()); +void GoogleSettingsDialog::accountChanged() +{ + if (!m_account) { + m_ui->accountLabel->setText(i18n("not configured")); + m_ui->calendarsBox->setDisabled(true); + m_ui->calendarsList->clear(); + m_ui->taskListsBox->setDisabled(true); + m_ui->taskListsList->clear(); + return; + } + m_ui->accountLabel->setText(QStringLiteral("%1").arg(m_account->accountName())); + slotReloadCalendars(); + slotReloadTaskLists(); } -void GoogleSettingsDialog::slotAddAccountClicked() +void GoogleSettingsDialog::slotConfigure() { - AccountPtr account(new Account()); - // FIXME: We need a proper API for this - account->addScope(Account::contactsScopeUrl()); - account->addScope(Account::calendarScopeUrl()); - account->addScope(Account::tasksScopeUrl()); - account->addScope(Account::accountInfoEmailScopeUrl()); - account->addScope(Account::accountInfoScopeUrl()); - - AuthJob *authJob = new AuthJob(account, + m_account = AccountPtr(new Account()); + const QList resourceScopes = m_resource->scopes(); + for (const QUrl &scope : resourceScopes) { + if (!m_account->scopes().contains(scope)) { + m_account->addScope(scope); + } + } + AuthJob *authJob = new AuthJob(m_account, GoogleSettings::self()->clientId(), GoogleSettings::self()->clientSecret()); - connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAccountAuthenticated); + connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAuthJobFinished); } -void GoogleSettingsDialog::slotRemoveAccountClicked() +void GoogleSettingsDialog::slotAuthJobFinished(Job* job) { - const AccountPtr account = currentAccount(); - if (!account) { + auto authJob = qobject_cast(job); + m_account = authJob->account(); + if (authJob->error() != KGAPI2::NoError) { + KMessageBox::sorry(this, authJob->errorString()); return; } - - if (KMessageBox::warningYesNo( - this, - i18n("Do you really want to revoke access to account %1?" - "

This will revoke access to all resources using this account!

", - account->accountName()), - i18n("Revoke Access?"), - KStandardGuiItem::yes(), - KStandardGuiItem::no(), - QString(), - KMessageBox::Dangerous) != KMessageBox::Yes) { + if (!m_resource->accountManager()->storeAccount(m_account)) { + qCWarning(GOOGLE_LOG) << "Failed to add account to KWallet"; return; } + accountChanged(); - m_accountManager->removeAccount(account->accountName()); - reloadAccounts(); + auto otherJob = job->property(JOB_PROPERTY).value(); + if (otherJob) { + otherJob->setAccount(m_account); + otherJob->restart(); + } } -void GoogleSettingsDialog::slotAccountAuthenticated(Job *job) +void GoogleSettingsDialog::slotSaveSettings() { - AuthJob *authJob = qobject_cast(job); - const AccountPtr account = authJob->account(); - - if (authJob->error() != KGAPI2::NoError) { - KMessageBox::sorry(this, authJob->errorString()); + if (!m_account) { + GoogleSettings::self()->setAccount(QString()); + GoogleSettings::self()->setEnableIntervalCheck(m_ui->enableRefresh->isChecked()); + GoogleSettings::self()->setIntervalCheckTime(m_ui->refreshSpinBox->value()); + GoogleSettings::self()->setCalendars({}); + GoogleSettings::self()->setTaskLists({}); + GoogleSettings::self()->setEventsSince(QString()); return; } + GoogleSettings::self()->setAccount(m_account->accountName()); + GoogleSettings::self()->setEnableIntervalCheck(m_ui->enableRefresh->isChecked()); + GoogleSettings::self()->setIntervalCheckTime(m_ui->refreshSpinBox->value()); - if (!m_accountManager->storeAccount(account)) { - qWarning() << "Failed to add account to KWallet"; - } - - reloadAccounts(); -} + QStringList calendars; + for (int i = 0; i < m_ui->calendarsList->count(); i++) { + QListWidgetItem *item = m_ui->calendarsList->item(i); -bool GoogleSettingsDialog::handleError(Job *job) -{ - if ((job->error() == KGAPI2::NoError) || (job->error() == KGAPI2::OK)) { - return true; + if (item->checkState() == Qt::Checked) { + calendars.append(item->data(Qt::UserRole).toString()); + } } + GoogleSettings::self()->setCalendars(calendars); - if (job->error() == KGAPI2::Unauthorized) { - qDebug() << job << job->errorString(); - const AccountPtr account = currentAccount(); - const QList resourceScopes = m_parentResource->scopes(); - for (const QUrl &scope : resourceScopes) { - if (!account->scopes().contains(scope)) { - account->addScope(scope); - } - } + if (m_ui->eventsLimitCombo->isValid()) { + GoogleSettings::self()->setEventsSince(m_ui->eventsLimitCombo->date().toString(Qt::ISODate)); + } - AuthJob *authJob = new AuthJob(account, GoogleSettings::self()->clientId(), - GoogleSettings::self()->clientSecret(), this); - authJob->setProperty(JOB_PROPERTY, QVariant::fromValue(job)); - connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAuthJobFinished); + QStringList taskLists; + for (int i = 0; i < m_ui->taskListsList->count(); i++) { + QListWidgetItem *item = m_ui->taskListsList->item(i); - return false; + if (item->checkState() == Qt::Checked) { + taskLists.append(item->data(Qt::UserRole).toString()); + } } + GoogleSettings::self()->setTaskLists(taskLists); - KMessageBox::sorry(this, job->errorString()); - job->deleteLater(); - return false; + accept(); } -void GoogleSettingsDialog::slotAuthJobFinished(Job *job) +void GoogleSettingsDialog::slotReloadCalendars() { - qDebug(); + m_ui->calendarsBox->setDisabled(true); + m_ui->calendarsList->clear(); - if (job->error() != KGAPI2::NoError) { - KMessageBox::sorry(this, job->errorString()); + if (!m_account) { return; } - AuthJob *authJob = qobject_cast(job); - const AccountPtr account = authJob->account(); - if (!m_accountManager->storeAccount(account)) { - qWarning() << "Failed to store account in KWallet"; + auto fetchJob = new CalendarFetchJob(m_account, this); + connect(fetchJob, &CalendarFetchJob::finished, [this](Job* job){ + if (!handleError(job) || !m_account) { + m_ui->calendarsBox->setEnabled(false); + return; + } + + const ObjectsList objects = qobject_cast(job)->items(); + + QStringList activeCalendars; + if (m_account->accountName() == GoogleSettings::self()->account()) { + activeCalendars = GoogleSettings::self()->calendars(); + } + m_ui->calendarsList->clear(); + for (const ObjectPtr &object : objects) { + const CalendarPtr calendar = object.dynamicCast(); + + QListWidgetItem *item = new QListWidgetItem(calendar->title()); + item->setData(Qt::UserRole, calendar->uid()); + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); + item->setCheckState((activeCalendars.isEmpty() || activeCalendars.contains(calendar->uid())) ? Qt::Checked : Qt::Unchecked); + m_ui->calendarsList->addItem(item); + } + + m_ui->calendarsBox->setEnabled(true); + }); +} + +void GoogleSettingsDialog::slotReloadTaskLists() +{ + if (!m_account) { + return; } - KGAPI2::Job *otherJob = job->property(JOB_PROPERTY).value(); - otherJob->setAccount(account); - otherJob->restart(); + m_ui->taskListsBox->setDisabled(true); + m_ui->taskListsList->clear(); - job->deleteLater(); -} + auto job = new TaskListFetchJob(m_account, this); + connect(job, &TaskListFetchJob::finished, [this](KGAPI2::Job *job){ + if (!handleError(job) || !m_account) { + m_ui->taskListsBox->setDisabled(true); + return; + } -void GoogleSettingsDialog::slotSaveSettings() -{ - GoogleSettings::self()->setEnableIntervalCheck(m_enableRefresh->isChecked()); - GoogleSettings::self()->setIntervalCheckTime(m_refreshSpinBox->value()); + const ObjectsList objects = qobject_cast(job)->items(); - saveSettings(); - accept(); + QStringList activeTaskLists; + if (m_account->accountName() == GoogleSettings::self()->account()) { + activeTaskLists = GoogleSettings::self()->taskLists(); + } + m_ui->taskListsList->clear(); + for (const ObjectPtr &object : objects) { + const TaskListPtr taskList = object.dynamicCast(); + + QListWidgetItem *item = new QListWidgetItem(taskList->title()); + item->setData(Qt::UserRole, taskList->uid()); + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); + item->setCheckState((activeTaskLists.isEmpty() || activeTaskLists.contains(taskList->uid())) ? Qt::Checked : Qt::Unchecked); + m_ui->taskListsList->addItem(item); + } + + m_ui->taskListsBox->setEnabled(true); + + }); } diff --git a/resources/google-new/googlesettingsdialog.h b/resources/google-new/googlesettingsdialog.h index 9aa1f7dcd..133d859f4 100644 --- a/resources/google-new/googlesettingsdialog.h +++ b/resources/google-new/googlesettingsdialog.h @@ -1,82 +1,54 @@ /* Copyright (C) 2013 Daniel Vrátil + 2020 Igor Poboiko This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef GOOGLESETTINGSDIALOG_H #define GOOGLESETTINGSDIALOG_H #include - #include +namespace Ui { + class GoogleSettingsDialog; +} namespace KGAPI2 { -class Job; + class Job; } - class GoogleResource; -class GoogleSettings; -class GoogleAccountManager; - -class QGroupBox; -class QComboBox; -class QCheckBox; -class KPluralHandlingSpinBox; -class QPushButton; -class QVBoxLayout; -class GoogleSettingsDialog : public QDialog +class GoogleSettingsDialog : public QDialog { Q_OBJECT - public: - explicit GoogleSettingsDialog(GoogleAccountManager *accountManager, WId wId, GoogleResource *parent); - ~GoogleSettingsDialog() override; - - GoogleAccountManager *accountManager() const; - KGAPI2::AccountPtr currentAccount() const; - -public Q_SLOTS: - void reloadAccounts(); - -Q_SIGNALS: - void currentAccountChanged(const QString &accountName); - + explicit GoogleSettingsDialog(GoogleResource *resource, WId wId); + ~GoogleSettingsDialog(); protected: bool handleError(KGAPI2::Job *job); - virtual void saveSettings() = 0; - QVBoxLayout *mainLayout() const; - + void accountChanged(); +private: + GoogleResource *m_resource = nullptr; + Ui::GoogleSettingsDialog *m_ui = nullptr; + KGAPI2::AccountPtr m_account; private Q_SLOTS: - void slotSaveSettings(); - void slotAddAccountClicked(); - void slotRemoveAccountClicked(); + void slotConfigure(); void slotAuthJobFinished(KGAPI2::Job *job); - void slotAccountAuthenticated(KGAPI2::Job *job); - -private: - GoogleResource *m_parentResource = nullptr; - GoogleAccountManager *m_accountManager = nullptr; - - QGroupBox *m_accGroupBox = nullptr; - QPushButton *m_addAccButton = nullptr; - QPushButton *m_removeAccButton = nullptr; - QComboBox *m_accComboBox = nullptr; - QCheckBox *m_enableRefresh = nullptr; - KPluralHandlingSpinBox *m_refreshSpinBox = nullptr; - QVBoxLayout *m_mainLayout = nullptr; + void slotSaveSettings(); + void slotReloadCalendars(); + void slotReloadTaskLists(); }; #endif // GOOGLESETTINGSDIALOG_H diff --git a/resources/google-new/googlesettingsdialog.ui b/resources/google-new/googlesettingsdialog.ui new file mode 100644 index 000000000..265f59bdb --- /dev/null +++ b/resources/google-new/googlesettingsdialog.ui @@ -0,0 +1,185 @@ + + + GoogleSettingsDialog + + + + 0 + 0 + 584 + 680 + + + + Form + + + + + + + + Account: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + <b>not confgured</b> + + + + + + + Configure... + + + + + + + + + Refresh + + + false + + + + + + Refresh interval: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + refreshSpinBox + + + + + + + 10 + + + 720 + + + 30 + + + + + + + Enable interval refresh + + + + + + + + + + Calendars + + + + + + + + + Fetch only events since + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + eventsLimitCombo + + + + + + + + + + Reload + + + + .. + + + + + + + + + + Tasklists + + + + + + + + + Reload + + + + .. + + + + + + + + + + + KPluralHandlingSpinBox + QSpinBox +
kpluralhandlingspinbox.h
+
+ + KDateComboBox + QComboBox +
kdatecombobox.h
+
+
+ + + + enableRefresh + toggled(bool) + refreshSpinBox + setEnabled(bool) + + + 182 + 99 + + + 312 + 128 + + + + +
diff --git a/resources/google-new/settingsdialog.cpp b/resources/google-new/settingsdialog.cpp deleted file mode 100644 index f745c39bc..000000000 --- a/resources/google-new/settingsdialog.cpp +++ /dev/null @@ -1,230 +0,0 @@ -/* - Copyright (C) 2011-2013 Daniel Vrátil - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "settingsdialog.h" -#include "googlesettings.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -using namespace KGAPI2; - -SettingsDialog::SettingsDialog(GoogleAccountManager *accountManager, WId windowId, GoogleResource *parent) - : GoogleSettingsDialog(accountManager, windowId, parent) -{ - connect(this, &SettingsDialog::currentAccountChanged, this, &SettingsDialog::slotCurrentAccountChanged); - - m_calendarsBox = new QGroupBox(i18n("Calendars"), this); - mainLayout()->addWidget(m_calendarsBox); - - QVBoxLayout *vbox = new QVBoxLayout(m_calendarsBox); - - m_calendarsList = new QListWidget(m_calendarsBox); - vbox->addWidget(m_calendarsList, 1); - - m_reloadCalendarsBtn = new QPushButton(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Reload"), m_calendarsBox); - vbox->addWidget(m_reloadCalendarsBtn); - connect(m_reloadCalendarsBtn, &QPushButton::clicked, this, &SettingsDialog::slotReloadCalendars); - - QHBoxLayout *hbox = new QHBoxLayout; - vbox->addLayout(hbox); - - m_eventsLimitLabel = new QLabel(i18nc("Followed by a date picker widget", "&Fetch only new events since"), this); - hbox->addWidget(m_eventsLimitLabel); - - m_eventsLimitCombo = new KDateComboBox(this); - m_eventsLimitLabel->setBuddy(m_eventsLimitCombo); - m_eventsLimitCombo->setMaximumDate(QDate::currentDate()); - m_eventsLimitCombo->setMinimumDate(QDate::fromString(QStringLiteral("2000-01-01"), Qt::ISODate)); - m_eventsLimitCombo->setOptions(KDateComboBox::EditDate | KDateComboBox::SelectDate - |KDateComboBox::DatePicker | KDateComboBox::WarnOnInvalid); - if (GoogleSettings::self()->eventsSince().isEmpty()) { - const QString ds = QStringLiteral("%1-01-01").arg(QString::number(QDate::currentDate().year() - 3)); - m_eventsLimitCombo->setDate(QDate::fromString(ds, Qt::ISODate)); - } else { - m_eventsLimitCombo->setDate(QDate::fromString(GoogleSettings::self()->eventsSince(), Qt::ISODate)); - } - hbox->addWidget(m_eventsLimitCombo); - - m_taskListsBox = new QGroupBox(i18n("Tasklists"), this); - mainLayout()->addWidget(m_taskListsBox); - - vbox = new QVBoxLayout(m_taskListsBox); - - m_taskListsList = new QListWidget(m_taskListsBox); - vbox->addWidget(m_taskListsList, 1); - - m_reloadTaskListsBtn = new QPushButton(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Reload"), m_taskListsBox); - vbox->addWidget(m_reloadTaskListsBtn); - connect(m_reloadTaskListsBtn, &QPushButton::clicked, this, &SettingsDialog::slotReloadTaskLists); -} - -SettingsDialog::~SettingsDialog() -{ -} - -void SettingsDialog::saveSettings() -{ - const AccountPtr account = currentAccount(); - if (!currentAccount()) { - GoogleSettings::self()->setAccount(QString()); - GoogleSettings::self()->setCalendars(QStringList()); - GoogleSettings::self()->setTaskLists(QStringList()); - GoogleSettings::self()->setEventsSince(QString()); - GoogleSettings::self()->save(); - return; - } - - GoogleSettings::self()->setAccount(account->accountName()); - - QStringList calendars; - for (int i = 0; i < m_calendarsList->count(); i++) { - QListWidgetItem *item = m_calendarsList->item(i); - - if (item->checkState() == Qt::Checked) { - calendars.append(item->data(Qt::UserRole).toString()); - } - } - GoogleSettings::self()->setCalendars(calendars); - if (m_eventsLimitCombo->isValid()) { - GoogleSettings::self()->setEventsSince(m_eventsLimitCombo->date().toString(Qt::ISODate)); - } - - QStringList taskLists; - for (int i = 0; i < m_taskListsList->count(); i++) { - QListWidgetItem *item = m_taskListsList->item(i); - - if (item->checkState() == Qt::Checked) { - taskLists.append(item->data(Qt::UserRole).toString()); - } - } - GoogleSettings::self()->setTaskLists(taskLists); - - GoogleSettings::self()->save(); -} - -void SettingsDialog::slotCurrentAccountChanged(const QString &accountName) -{ - if (accountName.isEmpty()) { - m_taskListsBox->setDisabled(true); - m_taskListsList->clear(); - m_calendarsBox->setDisabled(true); - m_calendarsList->clear(); - return; - } - - slotReloadCalendars(); - slotReloadTaskLists(); -} - -void SettingsDialog::slotReloadCalendars() -{ - const AccountPtr account = currentAccount(); - if (!account) { - return; - } - - CalendarFetchJob *fetchJob = new CalendarFetchJob(account, this); - connect(fetchJob, &CalendarFetchJob::finished, this, &SettingsDialog::slotCalendarsRetrieved); - - m_calendarsBox->setDisabled(true); - m_calendarsList->clear(); -} - -void SettingsDialog::slotReloadTaskLists() -{ - const AccountPtr account = currentAccount(); - if (!account) { - return; - } - - TaskListFetchJob *fetchJob = new TaskListFetchJob(account, this); - connect(fetchJob, &TaskListFetchJob::finished, this, &SettingsDialog::slotTaskListsRetrieved); - - m_taskListsBox->setDisabled(true); - m_taskListsList->clear(); -} - -void SettingsDialog::slotCalendarsRetrieved(Job *job) -{ - if (!handleError(job) || !currentAccount()) { - m_calendarsBox->setEnabled(true); - return; - } - - FetchJob *fetchJob = qobject_cast(job); - const ObjectsList objects = fetchJob->items(); - - QStringList activeCalendars; - if (currentAccount()->accountName() == GoogleSettings::self()->account()) { - activeCalendars = GoogleSettings::self()->calendars(); - } - m_calendarsList->clear(); - for (const ObjectPtr &object : objects) { - CalendarPtr calendar = object.dynamicCast(); - - QListWidgetItem *item = new QListWidgetItem(calendar->title()); - item->setData(Qt::UserRole, calendar->uid()); - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); - item->setCheckState((activeCalendars.isEmpty() || activeCalendars.contains(calendar->uid())) ? Qt::Checked : Qt::Unchecked); - m_calendarsList->addItem(item); - } - - m_calendarsBox->setEnabled(true); -} - -void SettingsDialog::slotTaskListsRetrieved(Job *job) -{ - if (!handleError(job) || !currentAccount()) { - m_taskListsBox->setEnabled(true); - return; - } - - FetchJob *fetchJob = qobject_cast(job); - const ObjectsList objects = fetchJob->items(); - - QStringList activeTaskLists; - if (currentAccount()->accountName() == GoogleSettings::self()->account()) { - activeTaskLists = GoogleSettings::self()->taskLists(); - } - m_taskListsList->clear(); - for (const ObjectPtr &object : objects) { - TaskListPtr taskList = object.dynamicCast(); - - QListWidgetItem *item = new QListWidgetItem(taskList->title()); - item->setData(Qt::UserRole, taskList->uid()); - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); - item->setCheckState((activeTaskLists.isEmpty() || activeTaskLists.contains(taskList->uid())) ? Qt::Checked : Qt::Unchecked); - m_taskListsList->addItem(item); - } - - m_taskListsBox->setEnabled(true); -} diff --git a/resources/google-new/settingsdialog.h b/resources/google-new/settingsdialog.h deleted file mode 100644 index 4ab503563..000000000 --- a/resources/google-new/settingsdialog.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - Copyright (C) 2011-2013 Daniel Vrátil - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef GOOGLE_CALENDAR_SETTINGSDIALOG_H -#define GOOGLE_CALENDAR_SETTINGSDIALOG_H - -#include "googlesettingsdialog.h" - -class QListWidget; -class QLabel; -class KDateComboBox; - -class SettingsDialog : public GoogleSettingsDialog -{ - Q_OBJECT -public: - explicit SettingsDialog(GoogleAccountManager *accountManager, WId windowId, GoogleResource *parent); - ~SettingsDialog(); - -private Q_SLOTS: - void slotReloadCalendars(); - void slotReloadTaskLists(); - void slotCurrentAccountChanged(const QString &accountName); - - void slotTaskListsRetrieved(KGAPI2::Job *job); - void slotCalendarsRetrieved(KGAPI2::Job *job); - - void saveSettings() override; - -private: - QGroupBox *m_calendarsBox = nullptr; - QListWidget *m_calendarsList = nullptr; - QPushButton *m_reloadCalendarsBtn = nullptr; - QLabel *m_eventsLimitLabel = nullptr; - KDateComboBox *m_eventsLimitCombo = nullptr; - - QGroupBox *m_taskListsBox = nullptr; - QListWidget *m_taskListsList = nullptr; - QPushButton *m_reloadTaskListsBtn = nullptr; -}; - -#endif // SETTINGSDIALOG_H