fb0d78fb.patch 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. From fb0d78fb3bcfce5bd4ff7f51e4cd1405693e0275 Mon Sep 17 00:00:00 2001
  2. From: Carl Schwan <carl@carlschwan.eu>
  3. Date: Tue, 19 Sep 2023 17:04:13 +0200
  4. Subject: [PATCH] Revert "Revert "Use config plugin instead of out of process
  5. config dialog""
  6. This reverts commit f33496a346f49e95caaf7f664ef2f91e4b7f3ddb.
  7. (cherry picked from commit a17ef55dbe31e2020e96f66cd7021578ed8b5444)
  8. ---
  9. resources/google-groupware/CMakeLists.txt | 93 ++++--
  10. resources/google-groupware/googleconfig.cpp | 43 +++
  11. resources/google-groupware/googleconfig.json | 5 +
  12. resources/google-groupware/googleresource.cpp | 95 ++----
  13. resources/google-groupware/googleresource.h | 4 +-
  14. resources/google-groupware/googlescopes.cpp | 19 ++
  15. resources/google-groupware/googlescopes.h | 10 +
  16. resources/google-groupware/googlesettings.cpp | 22 +-
  17. resources/google-groupware/googlesettings.h | 11 +-
  18. .../google-groupware/googlesettingsdialog.cpp | 305 ------------------
  19. .../google-groupware/googlesettingswidget.cpp | 288 +++++++++++++++++
  20. ...ettingsdialog.h => googlesettingswidget.h} | 29 +-
  21. ...tingsdialog.ui => googlesettingswidget.ui} | 2 +-
  22. resources/google-groupware/settingsbase.kcfg | 2 +-
  23. 14 files changed, 505 insertions(+), 423 deletions(-)
  24. create mode 100644 resources/google-groupware/googleconfig.cpp
  25. create mode 100644 resources/google-groupware/googleconfig.json
  26. create mode 100644 resources/google-groupware/googlescopes.cpp
  27. create mode 100644 resources/google-groupware/googlescopes.h
  28. delete mode 100644 resources/google-groupware/googlesettingsdialog.cpp
  29. create mode 100644 resources/google-groupware/googlesettingswidget.cpp
  30. rename resources/google-groupware/{googlesettingsdialog.h => googlesettingswidget.h} (53%)
  31. rename resources/google-groupware/{googlesettingsdialog.ui => googlesettingswidget.ui} (99%)
  32. diff --git a/resources/google-groupware/CMakeLists.txt b/resources/google-groupware/CMakeLists.txt
  33. index babf58a953..2f2b46aded 100644
  34. --- a/resources/google-groupware/CMakeLists.txt
  35. +++ b/resources/google-groupware/CMakeLists.txt
  36. @@ -1,67 +1,75 @@
  37. add_definitions(-DTRANSLATION_DOMAIN=\"akonadi_google_resource\")
  38. -set(googleresource_SRCS
  39. - googleresource.cpp
  40. +set(googleresource_common_SRCS
  41. googlesettings.cpp
  42. - googlesettingsdialog.cpp
  43. - defaultreminderattribute.cpp
  44. - googleresourcestate.cpp
  45. - generichandler.cpp
  46. - calendarhandler.cpp
  47. - taskhandler.cpp
  48. - peopleconversionjob.cpp
  49. - personhandler.cpp
  50. - googleresource.h
  51. googlesettings.h
  52. - googlesettingsdialog.h
  53. - defaultreminderattribute.h
  54. - googleresourcestate.h
  55. - generichandler.h
  56. - calendarhandler.h
  57. - taskhandler.h
  58. - peopleconversionjob.h
  59. - personhandler.h
  60. - )
  61. + googlescopes.h
  62. + googlescopes.cpp
  63. +)
  64. -ecm_qt_declare_logging_category(googleresource_SRCS
  65. +ecm_qt_declare_logging_category(googleresource_common_SRCS
  66. HEADER googleresource_debug.h
  67. IDENTIFIER GOOGLE_LOG
  68. CATEGORY_NAME org.kde.pim.google
  69. DESCRIPTION "resource google (kdepim-runtime-kf6)"
  70. EXPORT KDEPIMRUNTIME)
  71. -ecm_qt_declare_logging_category(googleresource_SRCS
  72. +ecm_qt_declare_logging_category(googleresource_common_SRCS
  73. HEADER googlecalendar_debug.h
  74. IDENTIFIER GOOGLE_CALENDAR_LOG
  75. CATEGORY_NAME org.kde.pim.google.calendar
  76. DESCRIPTION "resource google calendar (kdepim-runtime-kf6)"
  77. EXPORT KDEPIMRUNTIME)
  78. -ecm_qt_declare_logging_category(googleresource_SRCS
  79. +ecm_qt_declare_logging_category(googleresource_common_SRCS
  80. HEADER googletasks_debug.h
  81. IDENTIFIER GOOGLE_TASKS_LOG
  82. CATEGORY_NAME org.kde.pim.google.tasks
  83. DESCRIPTION "resource google tasks (kdepim-runtime-kf6)"
  84. EXPORT KDEPIMRUNTIME)
  85. -ecm_qt_declare_logging_category(googleresource_SRCS
  86. +ecm_qt_declare_logging_category(googleresource_common_SRCS
  87. HEADER googlepeople_debug.h
  88. IDENTIFIER GOOGLE_PEOPLE_LOG
  89. CATEGORY_NAME org.kde.pim.google.people
  90. DESCRIPTION "resource google people (kdepim-runtime-kf6)"
  91. EXPORT KDEPIMRUNTIME)
  92. -ki18n-kf6_wrap_ui(googleresource_SRCS googlesettingsdialog.ui)
  93. -kconfig-kf6_add_kcfg_files(googleresource_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/settingsbase.kcfgc)
  94. +kconfig-kf6_add_kcfg_files(googleresource_common_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/settingsbase.kcfgc)
  95. kcfg_generate_dbus_interface(
  96. ${CMAKE_CURRENT_SOURCE_DIR}/settingsbase.kcfg
  97. org.kde.Akonadi.Google.Settings
  98. )
  99. -qt_add_dbus_adaptor(googleresource_SRCS
  100. +qt_add_dbus_adaptor(googleresource_common_SRCS
  101. ${CMAKE_CURRENT_BINARY_DIR}/org.kde.Akonadi.Google.Settings.xml
  102. ${CMAKE_CURRENT_SOURCE_DIR}/googlesettings.h GoogleSettings
  103. )
  104. +############################# Resource #################################
  105. +set(googleresource_SRCS
  106. + googleresource.cpp
  107. + defaultreminderattribute.cpp
  108. + googleresourcestate.cpp
  109. + generichandler.cpp
  110. + calendarhandler.cpp
  111. + taskhandler.cpp
  112. + peopleconversionjob.cpp
  113. + personhandler.cpp
  114. + googleresource.h
  115. + googlesettings.h
  116. + defaultreminderattribute.h
  117. + googleresourcestate.h
  118. + generichandler.h
  119. + calendarhandler.h
  120. + taskhandler.h
  121. + peopleconversionjob.h
  122. + personhandler.h
  123. + ${googleresource_common_SRCS}
  124. +)
  125. +
  126. +ki18n-kf6_wrap_ui(googleresource_SRCS googlesettingswidget.ui)
  127. +
  128. +
  129. add_executable(akonadi_google_resource ${googleresource_SRCS})
  130. if (COMPILE_WITH_UNITY_CMAKE_SUPPORT)
  131. set_target_properties(akonadi_google_resource PROPERTIES UNITY_BUILD ON)
  132. @@ -112,3 +120,34 @@ install(
  133. FILES akonadi_google_resource.notifyrc
  134. DESTINATION ${KDE_INSTALL_KNOTIFYRCDIR}
  135. )
  136. +
  137. +################################ Config plugin ###############################
  138. +
  139. +kcoreaddons-kf6_add_plugin(googleconfig
  140. + INSTALL_NAMESPACE "pim${QT_MAJOR_VERSION}/akonadi/config"
  141. +)
  142. +target_sources(googleconfig
  143. +PRIVATE
  144. + googleconfig.cpp
  145. + googlesettingswidget.cpp
  146. + googlesettingswidget.h
  147. + ${googleresource_common_SRCS}
  148. +)
  149. +target_link_libraries(googleconfig
  150. + KPim${KF_MAJOR_VERSION}::AkonadiCore
  151. + KPim${KF_MAJOR_VERSION}::AkonadiCalendar
  152. + KPim${KF_MAJOR_VERSION}::AkonadiCore
  153. + KPim${KF_MAJOR_VERSION}::AkonadiAgentBase
  154. + KF${KF_MAJOR_VERSION}::CalendarCore
  155. + KF${KF_MAJOR_VERSION}::Contacts
  156. + KF${KF_MAJOR_VERSION}::Wallet
  157. + KF${KF_MAJOR_VERSION}::I18n
  158. + KF${KF_MAJOR_VERSION}::WindowSystem
  159. + KF${KF_MAJOR_VERSION}::TextWidgets
  160. + KF${KF_MAJOR_VERSION}::Notifications
  161. + KPim${KF_MAJOR_VERSION}::GAPICalendar
  162. + KPim${KF_MAJOR_VERSION}::GAPIPeople
  163. + KPim${KF_MAJOR_VERSION}::GAPICore
  164. + KPim${KF_MAJOR_VERSION}::GAPITasks
  165. + qt${KF_MAJOR_VERSION}keychain
  166. +)
  167. diff --git a/resources/google-groupware/googleconfig.cpp b/resources/google-groupware/googleconfig.cpp
  168. new file mode 100644
  169. index 0000000000..2eb6ab2efa
  170. --- /dev/null
  171. +++ b/resources/google-groupware/googleconfig.cpp
  172. @@ -0,0 +1,43 @@
  173. +/*
  174. + SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
  175. +
  176. + SPDX-License-Identifier: LGPL-2.0-or-later
  177. +*/
  178. +
  179. +#include <Akonadi/AgentConfigurationBase>
  180. +
  181. +#include "googlesettings.h"
  182. +#include "googlesettingswidget.h"
  183. +
  184. +class GoogleConfig : public Akonadi::AgentConfigurationBase
  185. +{
  186. + Q_OBJECT
  187. +
  188. +public:
  189. + explicit GoogleConfig(const KSharedConfigPtr &config, QWidget *parent, const QVariantList &list)
  190. + : Akonadi::AgentConfigurationBase(config, parent, list)
  191. + , mSettings(config, GoogleSettings::Option::NoOption)
  192. + , mWidget(mSettings, identifier(), parent)
  193. + {
  194. + connect(&mWidget, &GoogleSettingsWidget::okEnabled, this, &Akonadi::AgentConfigurationBase::enableOkButton);
  195. + }
  196. +
  197. + void load() override
  198. + {
  199. + Akonadi::AgentConfigurationBase::load();
  200. + mWidget.loadSettings();
  201. + }
  202. +
  203. + Q_REQUIRED_RESULT bool save() const override
  204. + {
  205. + const_cast<GoogleConfig *>(this)->mWidget.saveSettings();
  206. + return Akonadi::AgentConfigurationBase::save();
  207. + }
  208. +
  209. + GoogleSettings mSettings;
  210. + GoogleSettingsWidget mWidget;
  211. +};
  212. +
  213. +AKONADI_AGENTCONFIG_FACTORY(GoogleConfigFactory, "googleconfig.json", GoogleConfig)
  214. +
  215. +#include "googleconfig.moc"
  216. diff --git a/resources/google-groupware/googleconfig.json b/resources/google-groupware/googleconfig.json
  217. new file mode 100644
  218. index 0000000000..69c76f583f
  219. --- /dev/null
  220. +++ b/resources/google-groupware/googleconfig.json
  221. @@ -0,0 +1,5 @@
  222. +{
  223. + "X-Akonadi-PluginType": "AgentConfig",
  224. + "X-Akonadi-Library": "googleconfig",
  225. + "X-Akonadi-AgentConfig-Type": "akonadi_google_resource"
  226. +}
  227. \ No newline at end of file
  228. diff --git a/resources/google-groupware/googleresource.cpp b/resources/google-groupware/googleresource.cpp
  229. index b40f093dde..47a7e0c8d5 100644
  230. --- a/resources/google-groupware/googleresource.cpp
  231. +++ b/resources/google-groupware/googleresource.cpp
  232. @@ -9,8 +9,9 @@
  233. #include "googleresource.h"
  234. #include "googleresource_debug.h"
  235. #include "googleresourcestate.h"
  236. +#include "googlescopes.h"
  237. #include "googlesettings.h"
  238. -#include "googlesettingsdialog.h"
  239. +#include "googlesettingswidget.h"
  240. #include "settingsadaptor.h"
  241. #include "personhandler.h"
  242. @@ -67,8 +68,11 @@ bool accountIsValid(const KGAPI2::AccountPtr &account)
  243. GoogleResource::GoogleResource(const QString &id)
  244. : ResourceBase(id)
  245. , AgentBase::ObserverV3()
  246. + , m_settings(KSharedConfig::openConfig())
  247. , m_iface(new GoogleResourceState(this))
  248. {
  249. + m_settings.setResourceId(identifier());
  250. +
  251. AttributeFactory::registerAttribute<DefaultReminderAttribute>();
  252. connect(this, &GoogleResource::reloadConfiguration, this, &GoogleResource::reloadConfig);
  253. @@ -83,9 +87,7 @@ GoogleResource::GoogleResource(const QString &id)
  254. Q_EMIT status(NotConfigured, i18n("Fetching password..."));
  255. - m_settings = new GoogleSettings();
  256. - m_settings->setWindowId(winIdForDialogs());
  257. - connect(m_settings, &GoogleSettings::accountReady, this, [this](bool ready) {
  258. + connect(&m_settings, &GoogleSettings::accountReady, this, [this](bool ready) {
  259. if (accountId() > 0) {
  260. return;
  261. }
  262. @@ -94,7 +96,7 @@ GoogleResource::GoogleResource(const QString &id)
  263. return;
  264. }
  265. - const auto account = m_settings->accountPtr();
  266. + const auto account = m_settings.accountPtr();
  267. if (account.isNull()) {
  268. Q_EMIT status(NotConfigured);
  269. return;
  270. @@ -107,18 +109,15 @@ GoogleResource::GoogleResource(const QString &id)
  271. synchronize();
  272. }
  273. });
  274. - m_settings->init();
  275. + m_settings.init();
  276. updateResourceName();
  277. - m_freeBusyHandler = std::make_unique<FreeBusyHandler>(m_iface, m_settings);
  278. + m_freeBusyHandler = std::make_unique<FreeBusyHandler>(m_iface, &m_settings);
  279. m_handlers.clear();
  280. - m_handlers.push_back(GenericHandler::Ptr(new CalendarHandler(m_iface, m_settings)));
  281. - m_handlers.push_back(GenericHandler::Ptr(new PersonHandler(m_iface, m_settings)));
  282. - m_handlers.push_back(GenericHandler::Ptr(new TaskHandler(m_iface, m_settings)));
  283. -
  284. - new SettingsAdaptor(m_settings);
  285. - QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"), m_settings, QDBusConnection::ExportAdaptors);
  286. + m_handlers.push_back(GenericHandler::Ptr(new CalendarHandler(m_iface, &m_settings)));
  287. + m_handlers.push_back(GenericHandler::Ptr(new PersonHandler(m_iface, &m_settings)));
  288. + m_handlers.push_back(GenericHandler::Ptr(new TaskHandler(m_iface, &m_settings)));
  289. }
  290. GoogleResource::~GoogleResource()
  291. @@ -128,7 +127,7 @@ GoogleResource::~GoogleResource()
  292. void GoogleResource::cleanup()
  293. {
  294. - m_settings->cleanup();
  295. + m_settings.cleanup();
  296. ResourceBase::cleanup();
  297. }
  298. @@ -137,65 +136,30 @@ void GoogleResource::emitReadyStatus()
  299. Q_EMIT status(Idle, i18nc("@info:status", "Ready"));
  300. }
  301. -void GoogleResource::configure(WId windowId)
  302. -{
  303. - if (!m_settings->isReady() || m_isConfiguring) {
  304. - Q_EMIT configurationDialogAccepted();
  305. - return;
  306. - }
  307. -
  308. - m_isConfiguring = true;
  309. -
  310. - QScopedPointer<GoogleSettingsDialog> settingsDialog(new GoogleSettingsDialog(this, m_settings, windowId));
  311. - settingsDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("im-google")));
  312. - if (settingsDialog->exec() == QDialog::Accepted) {
  313. - updateResourceName();
  314. -
  315. - Q_EMIT configurationDialogAccepted();
  316. -
  317. - if (m_settings->accountPtr().isNull()) {
  318. - Q_EMIT status(NotConfigured, i18n("Configured account does not exist"));
  319. - m_isConfiguring = false;
  320. - return;
  321. - }
  322. -
  323. - emitReadyStatus();
  324. - synchronize();
  325. - } else {
  326. - updateResourceName();
  327. -
  328. - Q_EMIT configurationDialogRejected();
  329. - }
  330. -
  331. - m_isConfiguring = false;
  332. -}
  333. -
  334. QList<QUrl> GoogleResource::scopes() const
  335. {
  336. // TODO: determine it based on what user wants?
  337. - const QList<QUrl> scopes = {
  338. - Account::accountInfoScopeUrl(),
  339. - Account::calendarScopeUrl(),
  340. - Account::peopleScopeUrl(),
  341. - Account::tasksScopeUrl(),
  342. - };
  343. - return scopes;
  344. + return googleScopes();
  345. }
  346. void GoogleResource::updateResourceName()
  347. {
  348. - const QString accountName = m_settings->account();
  349. + const QString accountName = m_settings.account();
  350. setName(i18nc("%1 is account name (user@gmail.com)", "Google Groupware (%1)", accountName.isEmpty() ? i18n("not configured") : accountName));
  351. }
  352. void GoogleResource::reloadConfig()
  353. {
  354. - const AccountPtr account = m_settings->accountPtr();
  355. + updateResourceName();
  356. +
  357. + const AccountPtr account = m_settings.accountPtr();
  358. if (account.isNull() || account->accountName().isEmpty()) {
  359. Q_EMIT status(NotConfigured, i18n("Configured account does not exist"));
  360. - } else {
  361. - emitReadyStatus();
  362. + return;
  363. }
  364. +
  365. + emitReadyStatus();
  366. + synchronize();
  367. }
  368. bool GoogleResource::handleError(KGAPI2::Job *job, bool _cancelTask)
  369. @@ -231,7 +195,7 @@ bool GoogleResource::handleError(KGAPI2::Job *job, bool _cancelTask)
  370. void GoogleResource::runAuthJob(const KGAPI2::AccountPtr &account, const QVariant &args)
  371. {
  372. - auto authJob = new AuthJob(account, m_settings->clientId(), m_settings->clientSecret(), this);
  373. + auto authJob = new AuthJob(account, m_settings.clientId(), m_settings.clientSecret(), this);
  374. authJob->setProperty(JOB_PROPERTY, args);
  375. connect(authJob, &AuthJob::finished, this, &GoogleResource::slotAuthJobFinished);
  376. }
  377. @@ -264,7 +228,7 @@ void GoogleResource::requestAuthenticationFromUser(const KGAPI2::AccountPtr &acc
  378. bool GoogleResource::canPerformTask()
  379. {
  380. - if (!m_settings->accountPtr() && accountId() == 0) {
  381. + if (!m_settings.accountPtr() && accountId() == 0) {
  382. cancelTask(i18nc("@info:status", "Resource is not configured"));
  383. Q_EMIT status(NotConfigured, i18nc("@info:status", "Resource is not configured"));
  384. return false;
  385. @@ -289,10 +253,11 @@ void GoogleResource::slotAuthJobFinished(KGAPI2::Job *job)
  386. auto authJob = qobject_cast<AuthJob *>(job);
  387. AccountPtr account = authJob->account();
  388. - auto writeJob = m_settings->storeAccount(account);
  389. + auto writeJob = m_settings.storeAccount(account);
  390. connect(writeJob, &WritePasswordJob::finished, this, [job, account, writeJob]() {
  391. if (writeJob->error()) {
  392. qCWarning(GOOGLE_LOG) << "Failed to store account's password in secret storage" << writeJob->errorString();
  393. + return;
  394. }
  395. auto otherJob = job->property(JOB_PROPERTY).value<KGAPI2::Job *>();
  396. @@ -375,22 +340,22 @@ void GoogleResource::retrieveCollections()
  397. setCollectionStreamingEnabled(true);
  398. CachePolicy cachePolicy;
  399. - if (m_settings->enableIntervalCheck()) {
  400. + if (m_settings.enableIntervalCheck()) {
  401. cachePolicy.setInheritFromParent(false);
  402. - cachePolicy.setIntervalCheckTime(m_settings->intervalCheckTime());
  403. + cachePolicy.setIntervalCheckTime(m_settings.intervalCheckTime());
  404. }
  405. // Setting up root collection
  406. m_rootCollection = Collection();
  407. m_rootCollection.setContentMimeTypes({Collection::mimeType(), Collection::virtualMimeType()});
  408. m_rootCollection.setRemoteId(ROOT_COLLECTION_REMOTEID);
  409. - m_rootCollection.setName(m_settings->accountPtr()->accountName());
  410. + m_rootCollection.setName(m_settings.accountPtr()->accountName());
  411. m_rootCollection.setParentCollection(Collection::root());
  412. m_rootCollection.setRights(Collection::CanCreateCollection);
  413. m_rootCollection.setCachePolicy(cachePolicy);
  414. auto attr = m_rootCollection.attribute<EntityDisplayAttribute>(Collection::AddIfMissing);
  415. - attr->setDisplayName(m_settings->accountPtr()->accountName());
  416. + attr->setDisplayName(m_settings.accountPtr()->accountName());
  417. attr->setIconName(QStringLiteral("im-google"));
  418. collectionsRetrieved({m_rootCollection});
  419. diff --git a/resources/google-groupware/googleresource.h b/resources/google-groupware/googleresource.h
  420. index 4609572756..75708afca4 100644
  421. --- a/resources/google-groupware/googleresource.h
  422. +++ b/resources/google-groupware/googleresource.h
  423. @@ -14,6 +14,7 @@
  424. #include "calendarhandler.h"
  425. #include "generichandler.h"
  426. +#include "googlesettings.h"
  427. #define JOB_PROPERTY "_KGAPI2Job"
  428. @@ -37,7 +38,6 @@ public:
  429. void cleanup() override;
  430. public Q_SLOTS:
  431. - void configure(WId windowId) override;
  432. void reloadConfig();
  433. protected:
  434. @@ -83,7 +83,7 @@ protected Q_SLOTS:
  435. private:
  436. bool m_isConfiguring = false;
  437. - GoogleSettings *m_settings = nullptr;
  438. + GoogleSettings m_settings;
  439. Akonadi::Collection m_rootCollection;
  440. GoogleResourceState *const m_iface;
  441. diff --git a/resources/google-groupware/googlescopes.cpp b/resources/google-groupware/googlescopes.cpp
  442. new file mode 100644
  443. index 0000000000..9e565f7e33
  444. --- /dev/null
  445. +++ b/resources/google-groupware/googlescopes.cpp
  446. @@ -0,0 +1,19 @@
  447. +// SPDX-FileCopyrightText: 2011 Daniel Vrátil <dvratil@redhat.com>
  448. +// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
  449. +// SPDX-License-Identifier: GPL-3.0-or-later
  450. +
  451. +#include "googlescopes.h"
  452. +#include <KGAPI/Account>
  453. +
  454. +using namespace KGAPI2;
  455. +
  456. +QList<QUrl> googleScopes()
  457. +{
  458. + // TODO: determine it based on what user wants?
  459. + return {
  460. + Account::accountInfoScopeUrl(),
  461. + Account::calendarScopeUrl(),
  462. + Account::peopleScopeUrl(),
  463. + Account::tasksScopeUrl(),
  464. + };
  465. +}
  466. diff --git a/resources/google-groupware/googlescopes.h b/resources/google-groupware/googlescopes.h
  467. new file mode 100644
  468. index 0000000000..33bf579a89
  469. --- /dev/null
  470. +++ b/resources/google-groupware/googlescopes.h
  471. @@ -0,0 +1,10 @@
  472. +/*
  473. + SPDX-FileCopyrightText: 2011 Daniel Vrátil <dvratil@redhat.com>
  474. + SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
  475. + SPDX-License-Identifier: GPL-3.0-or-later
  476. +*/
  477. +
  478. +#include <QList>
  479. +#include <QUrl>
  480. +
  481. +QList<QUrl> googleScopes();
  482. diff --git a/resources/google-groupware/googlesettings.cpp b/resources/google-groupware/googlesettings.cpp
  483. index 6a1785fcf9..aa94cad1ab 100644
  484. --- a/resources/google-groupware/googlesettings.cpp
  485. +++ b/resources/google-groupware/googlesettings.cpp
  486. @@ -8,6 +8,8 @@
  487. #include "googlesettings.h"
  488. #include "googleresource_debug.h"
  489. +#include "googlescopes.h"
  490. +#include "settingsadaptor.h"
  491. #include <KGAPI/Account>
  492. #include <KLocalizedString>
  493. @@ -27,8 +29,16 @@ using namespace KGAPI2;
  494. static const QString googleWalletFolder = QStringLiteral("Akonadi Google");
  495. -GoogleSettings::GoogleSettings()
  496. +GoogleSettings::GoogleSettings(const KSharedConfigPtr &config, Options options)
  497. + : SettingsBase(config)
  498. {
  499. + qDebug() << config;
  500. + if (options & Option::ExportToDBus) {
  501. + new SettingsAdaptor(this);
  502. + QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"),
  503. + this,
  504. + QDBusConnection::ExportAdaptors | QDBusConnection::ExportScriptableContents);
  505. + }
  506. }
  507. void GoogleSettings::init()
  508. @@ -82,15 +92,17 @@ WritePasswordJob *GoogleSettings::storeAccount(AccountPtr account)
  509. m_account = account;
  510. QStringList scopes;
  511. - const QList<QUrl> urlScopes = m_account->scopes();
  512. + const QList<QUrl> urlScopes = googleScopes();
  513. scopes.reserve(urlScopes.count());
  514. for (const QUrl &url : urlScopes) {
  515. scopes << url.toString();
  516. }
  517. - const QMap<QString, QString> map = {{QStringLiteral("accessToken"), m_account->accessToken()},
  518. - {QStringLiteral("refreshToken"), m_account->refreshToken()},
  519. - {QStringLiteral("scopes"), scopes.join(QLatin1Char(','))}};
  520. + const QMap<QString, QString> map = {
  521. + {QStringLiteral("accessToken"), m_account->accessToken()},
  522. + {QStringLiteral("refreshToken"), m_account->refreshToken()},
  523. + {QStringLiteral("scopes"), scopes.join(QLatin1Char(','))},
  524. + };
  525. // Legacy: store the map exactly like kwallet-kf6 is doing it
  526. QByteArray mapData;
  527. diff --git a/resources/google-groupware/googlesettings.h b/resources/google-groupware/googlesettings.h
  528. index f689dc7748..0ec4c3c34e 100644
  529. --- a/resources/google-groupware/googlesettings.h
  530. +++ b/resources/google-groupware/googlesettings.h
  531. @@ -33,7 +33,14 @@ class GoogleSettings : public SettingsBase
  532. Q_CLASSINFO("D-Bus Interface", "org.kde.Akonadi.Google.ExtendedSettings")
  533. public:
  534. - GoogleSettings();
  535. + enum class Option {
  536. + NoOption = 0,
  537. + ExportToDBus = 1,
  538. + };
  539. + Q_DECLARE_FLAGS(Options, Option)
  540. +
  541. + explicit GoogleSettings(const KSharedConfigPtr &config, Options options = Option::ExportToDBus);
  542. +
  543. void init();
  544. void setWindowId(WId id);
  545. void setResourceId(const QString &resourceIdentifier);
  546. @@ -50,9 +57,11 @@ public:
  547. bool isReady() const;
  548. QKeychain::WritePasswordJob *storeAccount(KGAPI2::AccountPtr account);
  549. void cleanup();
  550. +
  551. Q_SIGNALS:
  552. void accountReady(bool ready);
  553. void accountChanged();
  554. + void okEnabled(bool enabled);
  555. private:
  556. void slotWalletOpened(bool success);
  557. diff --git a/resources/google-groupware/googlesettingsdialog.cpp b/resources/google-groupware/googlesettingsdialog.cpp
  558. deleted file mode 100644
  559. index d418652d6f..0000000000
  560. --- a/resources/google-groupware/googlesettingsdialog.cpp
  561. +++ /dev/null
  562. @@ -1,305 +0,0 @@
  563. -/*
  564. - SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
  565. - SPDX-FileCopyrightText: 2020 Igor Poboiko <igor.poboiko@gmail.com>
  566. -
  567. - SPDX-License-Identifier: GPL-3.0-or-later
  568. -*/
  569. -
  570. -#include "googlesettingsdialog.h"
  571. -#include "googleresource.h"
  572. -#include "googleresource_debug.h"
  573. -#include "googlesettings.h"
  574. -#include "ui_googlesettingsdialog.h"
  575. -
  576. -#include <QDialogButtonBox>
  577. -
  578. -#include <KGAPI/Account>
  579. -#include <KGAPI/AuthJob>
  580. -#include <KGAPI/Calendar/Calendar>
  581. -#include <KGAPI/Calendar/CalendarFetchJob>
  582. -#include <KGAPI/Tasks/TaskList>
  583. -#include <KGAPI/Tasks/TaskListFetchJob>
  584. -#include <KMessageBox>
  585. -#include <kwindowsystem-kf6>
  586. -
  587. -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  588. -#include <qt5keychain/keychain.h>
  589. -#else
  590. -#include <qt6keychain/keychain.h>
  591. -#endif
  592. -
  593. -using namespace QKeychain;
  594. -using namespace KGAPI2;
  595. -
  596. -GoogleSettingsDialog::GoogleSettingsDialog(GoogleResource *resource, GoogleSettings *settings, WId wId)
  597. - : QDialog()
  598. - , m_resource(resource)
  599. - , m_settings(settings)
  600. - , m_ui(new Ui::GoogleSettingsDialog)
  601. -{
  602. - if (wId) {
  603. - setAttribute(Qt::WA_NativeWindow, true);
  604. - kwindowsystem-kf6::setMainWindow(windowHandle(), wId);
  605. - }
  606. - auto mainLayout = new QVBoxLayout(this);
  607. -
  608. - auto mainWidget = new QWidget(this);
  609. - mainLayout->addWidget(mainWidget);
  610. -
  611. - auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
  612. - QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
  613. - okButton->setDefault(true);
  614. - okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
  615. - mainLayout->addWidget(buttonBox);
  616. -
  617. - m_ui->setupUi(mainWidget);
  618. -
  619. - m_ui->refreshSpinBox->setSuffix(ki18n-kf6p(" minute", " minutes"));
  620. - m_ui->enableRefresh->setChecked(m_settings->enableIntervalCheck());
  621. - m_ui->refreshSpinBox->setEnabled(m_settings->enableIntervalCheck());
  622. - m_ui->refreshSpinBox->setValue(m_settings->intervalCheckTime());
  623. -
  624. - m_ui->eventsLimitCombo->setMaximumDate(QDate::currentDate());
  625. - m_ui->eventsLimitCombo->setMinimumDate(QDate::fromString(QStringLiteral("2000-01-01"), Qt::ISODate));
  626. - m_ui->eventsLimitCombo->setOptions(KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::WarnOnInvalid);
  627. - if (m_settings->eventsSince().isEmpty()) {
  628. - const QString ds = QStringLiteral("%1-01-01").arg(QString::number(QDate::currentDate().year() - 3));
  629. - m_ui->eventsLimitCombo->setDate(QDate::fromString(ds, Qt::ISODate));
  630. - } else {
  631. - m_ui->eventsLimitCombo->setDate(QDate::fromString(m_settings->eventsSince(), Qt::ISODate));
  632. - }
  633. -
  634. - connect(buttonBox, &QDialogButtonBox::accepted, this, &GoogleSettingsDialog::slotSaveSettings);
  635. - connect(buttonBox, &QDialogButtonBox::rejected, this, &GoogleSettingsDialog::reject);
  636. - connect(m_ui->reloadCalendarsBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotReloadCalendars);
  637. - connect(m_ui->reloadTaskListsBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotReloadTaskLists);
  638. - connect(m_ui->configureBtn, &QPushButton::clicked, this, &GoogleSettingsDialog::slotConfigure);
  639. - if (m_settings->isReady()) {
  640. - m_account = m_settings->accountPtr();
  641. - }
  642. - connect(m_settings, &GoogleSettings::accountReady, this, [this](bool ready) {
  643. - if (ready) {
  644. - m_account = m_settings->accountPtr();
  645. - accountChanged();
  646. - }
  647. - });
  648. - QMetaObject::invokeMethod(this, &GoogleSettingsDialog::accountChanged, Qt::QueuedConnection);
  649. -}
  650. -
  651. -GoogleSettingsDialog::~GoogleSettingsDialog()
  652. -{
  653. - delete m_ui;
  654. -}
  655. -
  656. -bool GoogleSettingsDialog::handleError(KGAPI2::Job *job)
  657. -{
  658. - if ((job->error() == KGAPI2::NoError) || (job->error() == KGAPI2::OK)) {
  659. - return true;
  660. - }
  661. -
  662. - if (job->error() == KGAPI2::Unauthorized) {
  663. - qCDebug(GOOGLE_LOG) << job << job->errorString();
  664. - const QList<QUrl> resourceScopes = m_resource->scopes();
  665. - for (const QUrl &scope : resourceScopes) {
  666. - if (!m_account->scopes().contains(scope)) {
  667. - m_account->addScope(scope);
  668. - }
  669. - }
  670. -
  671. - auto authJob = new AuthJob(m_account, m_settings->clientId(), m_settings->clientSecret(), this);
  672. - authJob->setProperty(JOB_PROPERTY, QVariant::fromValue(job));
  673. - connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAuthJobFinished);
  674. -
  675. - return false;
  676. - }
  677. -
  678. - KMessageBox::error(this, job->errorString());
  679. - return false;
  680. -}
  681. -
  682. -void GoogleSettingsDialog::accountChanged()
  683. -{
  684. - if (!m_account) {
  685. - m_ui->accountLabel->setText(i18n("<b>not configured</b>"));
  686. - m_ui->calendarsList->setDisabled(true);
  687. - m_ui->reloadCalendarsBtn->setDisabled(true);
  688. - m_ui->calendarsList->clear();
  689. - m_ui->taskListsList->setDisabled(true);
  690. - m_ui->reloadTaskListsBtn->setDisabled(true);
  691. - m_ui->taskListsList->clear();
  692. - return;
  693. - }
  694. - m_ui->accountLabel->setText(QStringLiteral("<b>%1</b>").arg(m_account->accountName()));
  695. - slotReloadCalendars();
  696. - slotReloadTaskLists();
  697. -}
  698. -
  699. -void GoogleSettingsDialog::slotConfigure()
  700. -{
  701. - const QString username = m_account && !m_account->accountName().isEmpty() ? m_account->accountName() : QString();
  702. - m_account = AccountPtr(new Account());
  703. - const QList<QUrl> resourceScopes = m_resource->scopes();
  704. - for (const QUrl &scope : resourceScopes) {
  705. - if (!m_account->scopes().contains(scope)) {
  706. - m_account->addScope(scope);
  707. - }
  708. - }
  709. - auto authJob = new AuthJob(m_account, m_settings->clientId(), m_settings->clientSecret());
  710. - authJob->setUsername(username);
  711. - connect(authJob, &AuthJob::finished, this, &GoogleSettingsDialog::slotAuthJobFinished);
  712. -}
  713. -
  714. -void GoogleSettingsDialog::slotAuthJobFinished(KGAPI2::Job *job)
  715. -{
  716. - auto authJob = qobject_cast<AuthJob *>(job);
  717. - m_account = authJob->account();
  718. - if (authJob->error() != KGAPI2::NoError) {
  719. - KMessageBox::error(this, authJob->errorString());
  720. - return;
  721. - }
  722. - accountChanged();
  723. -
  724. - auto otherJob = job->property(JOB_PROPERTY).value<KGAPI2::Job *>();
  725. - if (otherJob) {
  726. - otherJob->setAccount(m_account);
  727. - otherJob->restart();
  728. - }
  729. -}
  730. -
  731. -void GoogleSettingsDialog::slotSaveSettings()
  732. -{
  733. - auto reset = [this] {
  734. - m_settings->setAccount({});
  735. - m_settings->setEnableIntervalCheck(m_ui->enableRefresh->isChecked());
  736. - m_settings->setIntervalCheckTime(m_ui->refreshSpinBox->value());
  737. - m_settings->setCalendars({});
  738. - m_settings->setTaskLists({});
  739. - m_settings->setEventsSince({});
  740. - m_settings->save();
  741. - };
  742. -
  743. - if (!m_account) {
  744. - reset();
  745. - return;
  746. - }
  747. -
  748. - auto writeJob = m_settings->storeAccount(m_account);
  749. - connect(writeJob, &WritePasswordJob::finished, this, [this, reset, writeJob]() {
  750. - if (writeJob->error()) {
  751. - qCWarning(GOOGLE_LOG) << "Failed to store account's password in secret storage" << writeJob->errorString();
  752. - reset();
  753. - return;
  754. - }
  755. -
  756. - m_settings->setAccount(m_account->accountName());
  757. - m_settings->setEnableIntervalCheck(m_ui->enableRefresh->isChecked());
  758. - m_settings->setIntervalCheckTime(m_ui->refreshSpinBox->value());
  759. -
  760. - QStringList calendars;
  761. - for (int i = 0; i < m_ui->calendarsList->count(); i++) {
  762. - QListWidgetItem *item = m_ui->calendarsList->item(i);
  763. -
  764. - if (item->checkState() == Qt::Checked) {
  765. - calendars.append(item->data(Qt::UserRole).toString());
  766. - }
  767. - }
  768. - m_settings->setCalendars(calendars);
  769. -
  770. - if (m_ui->eventsLimitCombo->isValid()) {
  771. - m_settings->setEventsSince(m_ui->eventsLimitCombo->date().toString(Qt::ISODate));
  772. - }
  773. -
  774. - QStringList taskLists;
  775. - for (int i = 0; i < m_ui->taskListsList->count(); i++) {
  776. - QListWidgetItem *item = m_ui->taskListsList->item(i);
  777. -
  778. - if (item->checkState() == Qt::Checked) {
  779. - taskLists.append(item->data(Qt::UserRole).toString());
  780. - }
  781. - }
  782. - m_settings->setTaskLists(taskLists);
  783. - m_settings->save();
  784. -
  785. - accept();
  786. - });
  787. -}
  788. -
  789. -void GoogleSettingsDialog::slotReloadCalendars()
  790. -{
  791. - m_ui->calendarsList->setDisabled(true);
  792. - m_ui->reloadCalendarsBtn->setDisabled(true);
  793. - m_ui->calendarsList->clear();
  794. -
  795. - if (!m_account) {
  796. - return;
  797. - }
  798. -
  799. - auto fetchJob = new CalendarFetchJob(m_account, this);
  800. - connect(fetchJob, &CalendarFetchJob::finished, this, [this](KGAPI2::Job *job) {
  801. - if (!handleError(job) || !m_account) {
  802. - m_ui->calendarsList->setEnabled(false);
  803. - m_ui->reloadCalendarsBtn->setEnabled(false);
  804. - return;
  805. - }
  806. -
  807. - const ObjectsList objects = qobject_cast<FetchJob *>(job)->items();
  808. -
  809. - QStringList activeCalendars;
  810. - if (m_account->accountName() == m_settings->account()) {
  811. - activeCalendars = m_settings->calendars();
  812. - }
  813. - m_ui->calendarsList->clear();
  814. - for (const ObjectPtr &object : objects) {
  815. - const CalendarPtr calendar = object.dynamicCast<Calendar>();
  816. -
  817. - auto item = new QListWidgetItem(calendar->title());
  818. - item->setData(Qt::UserRole, calendar->uid());
  819. - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
  820. - item->setCheckState((activeCalendars.isEmpty() || activeCalendars.contains(calendar->uid())) ? Qt::Checked : Qt::Unchecked);
  821. - m_ui->calendarsList->addItem(item);
  822. - }
  823. -
  824. - m_ui->calendarsList->setEnabled(true);
  825. - m_ui->reloadCalendarsBtn->setEnabled(true);
  826. - });
  827. -}
  828. -
  829. -void GoogleSettingsDialog::slotReloadTaskLists()
  830. -{
  831. - if (!m_account) {
  832. - return;
  833. - }
  834. -
  835. - m_ui->taskListsList->setDisabled(true);
  836. - m_ui->reloadTaskListsBtn->setDisabled(true);
  837. - m_ui->taskListsList->clear();
  838. -
  839. - auto job = new TaskListFetchJob(m_account, this);
  840. - connect(job, &TaskListFetchJob::finished, this, [this](KGAPI2::Job *job) {
  841. - if (!handleError(job) || !m_account) {
  842. - m_ui->taskListsList->setDisabled(true);
  843. - m_ui->reloadTaskListsBtn->setDisabled(true);
  844. - return;
  845. - }
  846. -
  847. - const ObjectsList objects = qobject_cast<FetchJob *>(job)->items();
  848. -
  849. - QStringList activeTaskLists;
  850. - if (m_account->accountName() == m_settings->account()) {
  851. - activeTaskLists = m_settings->taskLists();
  852. - }
  853. - m_ui->taskListsList->clear();
  854. - for (const ObjectPtr &object : objects) {
  855. - const TaskListPtr taskList = object.dynamicCast<TaskList>();
  856. -
  857. - auto item = new QListWidgetItem(taskList->title());
  858. - item->setData(Qt::UserRole, taskList->uid());
  859. - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
  860. - item->setCheckState((activeTaskLists.isEmpty() || activeTaskLists.contains(taskList->uid())) ? Qt::Checked : Qt::Unchecked);
  861. - m_ui->taskListsList->addItem(item);
  862. - }
  863. -
  864. - m_ui->taskListsList->setEnabled(true);
  865. - m_ui->reloadTaskListsBtn->setEnabled(true);
  866. - });
  867. -}
  868. diff --git a/resources/google-groupware/googlesettingswidget.cpp b/resources/google-groupware/googlesettingswidget.cpp
  869. new file mode 100644
  870. index 0000000000..2543c4c6b8
  871. --- /dev/null
  872. +++ b/resources/google-groupware/googlesettingswidget.cpp
  873. @@ -0,0 +1,288 @@
  874. +/*
  875. + SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
  876. + SPDX-FileCopyrightText: 2020 Igor Poboiko <igor.poboiko@gmail.com>
  877. +
  878. + SPDX-License-Identifier: GPL-3.0-or-later
  879. +*/
  880. +
  881. +#include "googlesettingswidget.h"
  882. +#include "googleresource.h"
  883. +#include "googleresource_debug.h"
  884. +#include "googlescopes.h"
  885. +#include "googlesettings.h"
  886. +
  887. +#include <QDialogButtonBox>
  888. +
  889. +#include <KGAPI/Account>
  890. +#include <KGAPI/AuthJob>
  891. +#include <KGAPI/Calendar/Calendar>
  892. +#include <KGAPI/Calendar/CalendarFetchJob>
  893. +#include <KGAPI/Tasks/TaskList>
  894. +#include <KGAPI/Tasks/TaskListFetchJob>
  895. +#include <KMessageBox>
  896. +#include <kwindowsystem-kf6>
  897. +
  898. +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  899. +#include <qt5keychain/keychain.h>
  900. +#else
  901. +#include <qt6keychain/keychain.h>
  902. +#endif
  903. +
  904. +using namespace QKeychain;
  905. +using namespace KGAPI2;
  906. +
  907. +GoogleSettingsWidget::GoogleSettingsWidget(GoogleSettings &settings, const QString &identifier, QWidget *parent)
  908. + : QWidget(parent)
  909. + , m_settings(settings)
  910. + , m_identifier(identifier)
  911. +{
  912. + qDebug() << m_settings.account();
  913. + auto mainLayout = new QVBoxLayout(this);
  914. +
  915. + auto mainWidget = new QWidget(this);
  916. + mainLayout->addWidget(mainWidget);
  917. + setupUi(mainWidget);
  918. +
  919. + refreshSpinBox->setSuffix(ki18n-kf6p(" minute", " minutes"));
  920. + enableRefresh->setChecked(m_settings.enableIntervalCheck());
  921. + refreshSpinBox->setEnabled(m_settings.enableIntervalCheck());
  922. + refreshSpinBox->setValue(m_settings.intervalCheckTime());
  923. +
  924. + eventsLimitCombo->setMaximumDate(QDate::currentDate());
  925. + eventsLimitCombo->setMinimumDate(QDate::fromString(QStringLiteral("2000-01-01"), Qt::ISODate));
  926. + eventsLimitCombo->setOptions(KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::WarnOnInvalid);
  927. + if (m_settings.eventsSince().isEmpty()) {
  928. + const QString ds = QStringLiteral("%1-01-01").arg(QString::number(QDate::currentDate().year() - 3));
  929. + eventsLimitCombo->setDate(QDate::fromString(ds, Qt::ISODate));
  930. + } else {
  931. + eventsLimitCombo->setDate(QDate::fromString(m_settings.eventsSince(), Qt::ISODate));
  932. + }
  933. + connect(reloadCalendarsBtn, &QPushButton::clicked, this, &GoogleSettingsWidget::slotReloadCalendars);
  934. + connect(reloadTaskListsBtn, &QPushButton::clicked, this, &GoogleSettingsWidget::slotReloadTaskLists);
  935. + connect(configureBtn, &QPushButton::clicked, this, &GoogleSettingsWidget::loadSettings);
  936. + if (m_settings.isReady()) {
  937. + m_account = m_settings.accountPtr();
  938. + }
  939. + connect(&m_settings, &GoogleSettings::accountReady, this, [this](bool ready) {
  940. + if (ready) {
  941. + m_account = m_settings.accountPtr();
  942. + accountChanged();
  943. + }
  944. + });
  945. + QMetaObject::invokeMethod(this, &GoogleSettingsWidget::accountChanged, Qt::QueuedConnection);
  946. +}
  947. +
  948. +GoogleSettingsWidget::~GoogleSettingsWidget()
  949. +{
  950. +}
  951. +
  952. +bool GoogleSettingsWidget::handleError(KGAPI2::Job *job)
  953. +{
  954. + if ((job->error() == KGAPI2::NoError) || (job->error() == KGAPI2::OK)) {
  955. + return true;
  956. + }
  957. +
  958. + if (job->error() == KGAPI2::Unauthorized) {
  959. + qCDebug(GOOGLE_LOG) << job << job->errorString();
  960. + const QList<QUrl> resourceScopes = googleScopes();
  961. + for (const QUrl &scope : resourceScopes) {
  962. + if (!m_account->scopes().contains(scope)) {
  963. + m_account->addScope(scope);
  964. + }
  965. + }
  966. +
  967. + auto authJob = new AuthJob(m_account, m_settings.clientId(), m_settings.clientSecret(), this);
  968. + authJob->setProperty(JOB_PROPERTY, QVariant::fromValue(job));
  969. + connect(authJob, &AuthJob::finished, this, &GoogleSettingsWidget::slotAuthJobFinished);
  970. +
  971. + return false;
  972. + }
  973. +
  974. + KMessageBox::error(this, job->errorString());
  975. + return false;
  976. +}
  977. +
  978. +void GoogleSettingsWidget::accountChanged()
  979. +{
  980. + if (!m_account) {
  981. + accountLabel->setText(i18n("<b>not configured</b>"));
  982. + calendarsList->setDisabled(true);
  983. + reloadCalendarsBtn->setDisabled(true);
  984. + calendarsList->clear();
  985. + taskListsList->setDisabled(true);
  986. + reloadTaskListsBtn->setDisabled(true);
  987. + taskListsList->clear();
  988. + return;
  989. + }
  990. + accountLabel->setText(QStringLiteral("<b>%1</b>").arg(m_account->accountName()));
  991. + slotReloadCalendars();
  992. + slotReloadTaskLists();
  993. +}
  994. +
  995. +void GoogleSettingsWidget::loadSettings()
  996. +{
  997. + const QString username = m_account && !m_account->accountName().isEmpty() ? m_account->accountName() : QString();
  998. + m_account = AccountPtr(new Account());
  999. + const QList<QUrl> resourceScopes = googleScopes();
  1000. + for (const QUrl &scope : resourceScopes) {
  1001. + if (!m_account->scopes().contains(scope)) {
  1002. + m_account->addScope(scope);
  1003. + }
  1004. + }
  1005. + auto authJob = new AuthJob(m_account, m_settings.clientId(), m_settings.clientSecret());
  1006. + authJob->setUsername(username);
  1007. + connect(authJob, &AuthJob::finished, this, &GoogleSettingsWidget::slotAuthJobFinished);
  1008. +}
  1009. +
  1010. +void GoogleSettingsWidget::slotAuthJobFinished(KGAPI2::Job *job)
  1011. +{
  1012. + auto authJob = qobject_cast<AuthJob *>(job);
  1013. + m_account = authJob->account();
  1014. + if (authJob->error() != KGAPI2::NoError) {
  1015. + KMessageBox::error(this, authJob->errorString());
  1016. + return;
  1017. + }
  1018. + accountChanged();
  1019. +
  1020. + auto otherJob = job->property(JOB_PROPERTY).value<KGAPI2::Job *>();
  1021. + if (otherJob) {
  1022. + otherJob->setAccount(m_account);
  1023. + otherJob->restart();
  1024. + }
  1025. +}
  1026. +
  1027. +void GoogleSettingsWidget::saveSettings()
  1028. +{
  1029. + auto reset = [this] {
  1030. + m_settings.setAccount({});
  1031. + m_settings.setEnableIntervalCheck(enableRefresh->isChecked());
  1032. + m_settings.setIntervalCheckTime(refreshSpinBox->value());
  1033. + m_settings.setCalendars({});
  1034. + m_settings.setTaskLists({});
  1035. + m_settings.setEventsSince({});
  1036. + m_settings.save();
  1037. + };
  1038. +
  1039. + if (!m_account) {
  1040. + reset();
  1041. + return;
  1042. + }
  1043. +
  1044. + auto writeJob = m_settings.storeAccount(m_account);
  1045. + connect(writeJob, &WritePasswordJob::finished, this, [this, reset, writeJob]() {
  1046. + if (writeJob->error()) {
  1047. + qCWarning(GOOGLE_LOG) << "Failed to store account's password in secret storage" << writeJob->errorString();
  1048. + reset();
  1049. + return;
  1050. + }
  1051. +
  1052. + m_settings.setAccount(m_account->accountName());
  1053. + m_settings.setEnableIntervalCheck(enableRefresh->isChecked());
  1054. + m_settings.setIntervalCheckTime(refreshSpinBox->value());
  1055. +
  1056. + QStringList calendars;
  1057. + for (int i = 0; i < calendarsList->count(); i++) {
  1058. + QListWidgetItem *item = calendarsList->item(i);
  1059. +
  1060. + if (item->checkState() == Qt::Checked) {
  1061. + calendars.append(item->data(Qt::UserRole).toString());
  1062. + }
  1063. + }
  1064. + m_settings.setCalendars(calendars);
  1065. +
  1066. + if (eventsLimitCombo->isValid()) {
  1067. + m_settings.setEventsSince(eventsLimitCombo->date().toString(Qt::ISODate));
  1068. + }
  1069. +
  1070. + QStringList taskLists;
  1071. + for (int i = 0; i < taskListsList->count(); i++) {
  1072. + QListWidgetItem *item = taskListsList->item(i);
  1073. +
  1074. + if (item->checkState() == Qt::Checked) {
  1075. + taskLists.append(item->data(Qt::UserRole).toString());
  1076. + }
  1077. + }
  1078. + m_settings.setTaskLists(taskLists);
  1079. + m_settings.save();
  1080. + });
  1081. +}
  1082. +
  1083. +void GoogleSettingsWidget::slotReloadCalendars()
  1084. +{
  1085. + calendarsList->setDisabled(true);
  1086. + reloadCalendarsBtn->setDisabled(true);
  1087. + calendarsList->clear();
  1088. +
  1089. + if (!m_account) {
  1090. + return;
  1091. + }
  1092. +
  1093. + auto fetchJob = new CalendarFetchJob(m_account, this);
  1094. + connect(fetchJob, &CalendarFetchJob::finished, this, [this](KGAPI2::Job *job) {
  1095. + if (!handleError(job) || !m_account) {
  1096. + calendarsList->setEnabled(false);
  1097. + reloadCalendarsBtn->setEnabled(false);
  1098. + return;
  1099. + }
  1100. +
  1101. + const ObjectsList objects = qobject_cast<FetchJob *>(job)->items();
  1102. +
  1103. + QStringList activeCalendars;
  1104. + if (m_account->accountName() == m_settings.account()) {
  1105. + activeCalendars = m_settings.calendars();
  1106. + }
  1107. + calendarsList->clear();
  1108. + for (const ObjectPtr &object : objects) {
  1109. + const CalendarPtr calendar = object.dynamicCast<Calendar>();
  1110. +
  1111. + auto item = new QListWidgetItem(calendar->title());
  1112. + item->setData(Qt::UserRole, calendar->uid());
  1113. + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
  1114. + item->setCheckState((activeCalendars.isEmpty() || activeCalendars.contains(calendar->uid())) ? Qt::Checked : Qt::Unchecked);
  1115. + calendarsList->addItem(item);
  1116. + }
  1117. +
  1118. + calendarsList->setEnabled(true);
  1119. + reloadCalendarsBtn->setEnabled(true);
  1120. + });
  1121. +}
  1122. +
  1123. +void GoogleSettingsWidget::slotReloadTaskLists()
  1124. +{
  1125. + if (!m_account) {
  1126. + return;
  1127. + }
  1128. +
  1129. + taskListsList->setDisabled(true);
  1130. + reloadTaskListsBtn->setDisabled(true);
  1131. + taskListsList->clear();
  1132. +
  1133. + auto job = new TaskListFetchJob(m_account, this);
  1134. + connect(job, &TaskListFetchJob::finished, this, [this](KGAPI2::Job *job) {
  1135. + if (!handleError(job) || !m_account) {
  1136. + taskListsList->setDisabled(true);
  1137. + reloadTaskListsBtn->setDisabled(true);
  1138. + return;
  1139. + }
  1140. +
  1141. + const ObjectsList objects = qobject_cast<FetchJob *>(job)->items();
  1142. +
  1143. + QStringList activeTaskLists;
  1144. + if (m_account->accountName() == m_settings.account()) {
  1145. + activeTaskLists = m_settings.taskLists();
  1146. + }
  1147. + taskListsList->clear();
  1148. + for (const ObjectPtr &object : objects) {
  1149. + const TaskListPtr taskList = object.dynamicCast<TaskList>();
  1150. +
  1151. + auto item = new QListWidgetItem(taskList->title());
  1152. + item->setData(Qt::UserRole, taskList->uid());
  1153. + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
  1154. + item->setCheckState((activeTaskLists.isEmpty() || activeTaskLists.contains(taskList->uid())) ? Qt::Checked : Qt::Unchecked);
  1155. + taskListsList->addItem(item);
  1156. + }
  1157. +
  1158. + taskListsList->setEnabled(true);
  1159. + reloadTaskListsBtn->setEnabled(true);
  1160. + });
  1161. +}
  1162. diff --git a/resources/google-groupware/googlesettingsdialog.h b/resources/google-groupware/googlesettingswidget.h
  1163. similarity index 53%
  1164. rename from resources/google-groupware/googlesettingsdialog.h
  1165. rename to resources/google-groupware/googlesettingswidget.h
  1166. index a7d6ca0c92..4226a39d2d 100644
  1167. --- a/resources/google-groupware/googlesettingsdialog.h
  1168. +++ b/resources/google-groupware/googlesettingswidget.h
  1169. @@ -7,40 +7,37 @@
  1170. #pragma once
  1171. +#include "googlesettings.h"
  1172. +#include "ui_googlesettingswidget.h"
  1173. #include <KGAPI/Types>
  1174. -#include <QDialog>
  1175. -namespace Ui
  1176. -{
  1177. -class GoogleSettingsDialog;
  1178. -}
  1179. namespace KGAPI2
  1180. {
  1181. class Job;
  1182. }
  1183. -class GoogleResource;
  1184. -class GoogleSettings;
  1185. -
  1186. -class GoogleSettingsDialog : public QDialog
  1187. +class GoogleSettingsWidget : public QWidget, private Ui::GoogleSettingsWidget
  1188. {
  1189. Q_OBJECT
  1190. public:
  1191. - explicit GoogleSettingsDialog(GoogleResource *resource, GoogleSettings *settings, WId wId);
  1192. - ~GoogleSettingsDialog() override;
  1193. + explicit GoogleSettingsWidget(GoogleSettings &settings, const QString &identifier, QWidget *parent);
  1194. + ~GoogleSettingsWidget() override;
  1195. +
  1196. + void loadSettings();
  1197. + void saveSettings();
  1198. +
  1199. +Q_SIGNALS:
  1200. + void okEnabled(bool enabled);
  1201. protected:
  1202. bool handleError(KGAPI2::Job *job);
  1203. void accountChanged();
  1204. private:
  1205. - void slotConfigure();
  1206. void slotAuthJobFinished(KGAPI2::Job *job);
  1207. - void slotSaveSettings();
  1208. void slotReloadCalendars();
  1209. void slotReloadTaskLists();
  1210. - GoogleResource *const m_resource;
  1211. - GoogleSettings *const m_settings;
  1212. - Ui::GoogleSettingsDialog *const m_ui;
  1213. + GoogleSettings &m_settings;
  1214. KGAPI2::AccountPtr m_account;
  1215. + const QString m_identifier;
  1216. };
  1217. diff --git a/resources/google-groupware/googlesettingsdialog.ui b/resources/google-groupware/googlesettingswidget.ui
  1218. similarity index 99%
  1219. rename from resources/google-groupware/googlesettingsdialog.ui
  1220. rename to resources/google-groupware/googlesettingswidget.ui
  1221. index 9ff9bc93b4..75fdfd24f1 100644
  1222. --- a/resources/google-groupware/googlesettingsdialog.ui
  1223. +++ b/resources/google-groupware/googlesettingswidget.ui
  1224. @@ -1,6 +1,6 @@
  1225. <?xml version="1.0" encoding="UTF-8"?>
  1226. <ui version="4.0">
  1227. - <class>GoogleSettingsDialog</class>
  1228. + <class>GoogleSettingsWidget</class>
  1229. <widget class="QWidget" name="GoogleSettingsDialog">
  1230. <property name="geometry">
  1231. <rect>
  1232. diff --git a/resources/google-groupware/settingsbase.kcfg b/resources/google-groupware/settingsbase.kcfg
  1233. index ac58395d71..7dfe8dfe92 100644
  1234. --- a/resources/google-groupware/settingsbase.kcfg
  1235. +++ b/resources/google-groupware/settingsbase.kcfg
  1236. @@ -4,7 +4,7 @@
  1237. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  1238. xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
  1239. http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
  1240. - <kcfgfile/>
  1241. + <kcfgfile arg="true"/>
  1242. <group name="General">
  1243. <entry name="Account" type="String">
  1244. </entry>
  1245. --
  1246. GitLab