File indexing completed on 2024-12-15 04:51:47
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "localresourcecreator.h" 0007 0008 #include <Akonadi/AgentInstanceCreateJob> 0009 #include <Akonadi/AgentManager> 0010 #include <Akonadi/ServerManager> 0011 0012 #include "maildirsettings.h" 0013 0014 #include <Akonadi/NoteUtils> 0015 0016 #include "noteshared_debug.h" 0017 #include <Akonadi/ResourceSynchronizationJob> 0018 #include <KLocalizedString> 0019 0020 using namespace NoteShared; 0021 0022 LocalResourceCreator::LocalResourceCreator(QObject *parent) 0023 : QObject(parent) 0024 { 0025 } 0026 0027 QString LocalResourceCreator::akonadiNotesInstanceName() 0028 { 0029 return QStringLiteral("akonadi_akonotes_resource"); 0030 } 0031 0032 void LocalResourceCreator::createIfMissing() 0033 { 0034 const Akonadi::AgentInstance::List instances = Akonadi::AgentManager::self()->instances(); 0035 bool found = false; 0036 for (const Akonadi::AgentInstance &instance : instances) { 0037 if (instance.type().identifier() == akonadiNotesInstanceName()) { 0038 found = true; 0039 break; 0040 } 0041 } 0042 if (found) { 0043 deleteLater(); 0044 return; 0045 } 0046 createInstance(); 0047 } 0048 0049 void LocalResourceCreator::createInstance() 0050 { 0051 Akonadi::AgentType notesType = Akonadi::AgentManager::self()->type(akonadiNotesInstanceName()); 0052 0053 auto job = new Akonadi::AgentInstanceCreateJob(notesType); 0054 connect(job, &Akonadi::AgentInstanceCreateJob::result, this, &LocalResourceCreator::slotInstanceCreated); 0055 0056 job->start(); 0057 } 0058 0059 void LocalResourceCreator::slotInstanceCreated(KJob *job) 0060 { 0061 if (job->error()) { 0062 qCWarning(NOTESHARED_LOG) << job->errorString(); 0063 deleteLater(); 0064 return; 0065 } 0066 0067 auto createJob = qobject_cast<Akonadi::AgentInstanceCreateJob *>(job); 0068 Akonadi::AgentInstance instance = createJob->instance(); 0069 0070 instance.setName(i18nc("Default name for resource holding notes", "Local Notes")); 0071 const auto service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, instance.identifier()); 0072 org::kde::Akonadi::Maildir::Settings iface(service, QStringLiteral("/Settings"), QDBusConnection::sessionBus(), this); 0073 0074 // TODO: Make errors user-visible. 0075 if (!iface.isValid()) { 0076 qCWarning(NOTESHARED_LOG) << "Failed to obtain D-Bus interface for remote configuration."; 0077 deleteLater(); 0078 return; 0079 } 0080 instance.reconfigure(); 0081 0082 auto syncJob = new Akonadi::ResourceSynchronizationJob(instance, this); 0083 connect(syncJob, &Akonadi::ResourceSynchronizationJob::result, this, &LocalResourceCreator::slotSyncDone); 0084 syncJob->start(); 0085 } 0086 0087 void LocalResourceCreator::slotSyncDone(KJob *job) 0088 { 0089 if (job->error()) { 0090 qCWarning(NOTESHARED_LOG) << "Synchronizing the resource failed:" << job->errorString(); 0091 deleteLater(); 0092 return; 0093 } 0094 0095 qCWarning(NOTESHARED_LOG) << "Instance synchronized"; 0096 deleteLater(); 0097 } 0098 0099 #include "moc_localresourcecreator.cpp"