File indexing completed on 2024-05-12 05:20:58
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "notesagent.h" 0008 #include "notesagentadaptor.h" 0009 #include "notesagentsettings.h" 0010 #include "notesagentsettingsdialog.h" 0011 #include "notesharedglobalconfig.h" 0012 #include "notesmanager.h" 0013 0014 #include <Akonadi/ServerManager> 0015 0016 #include <KWindowSystem> 0017 #include <QDBusConnection> 0018 0019 NotesAgent::NotesAgent(const QString &id) 0020 : Akonadi::AgentBase(id) 0021 { 0022 mNotesManager = new NotesManager(this); 0023 new NotesAgentAdaptor(this); 0024 QDBusConnection::sessionBus().registerObject(QStringLiteral("/NotesAgent"), this, QDBusConnection::ExportAdaptors); 0025 0026 setNeedsNetwork(true); 0027 0028 if (NotesAgentSettings::enabled()) { 0029 QTimer::singleShot(60 * 1000, this, &NotesAgent::slotStartAgent); 0030 } 0031 } 0032 0033 NotesAgent::~NotesAgent() = default; 0034 0035 void NotesAgent::doSetOnline(bool online) 0036 { 0037 if (mAgentInitialized) { 0038 if (online) { 0039 reload(); 0040 } else { 0041 mNotesManager->stopAll(); 0042 } 0043 } 0044 } 0045 0046 void NotesAgent::slotStartAgent() 0047 { 0048 mAgentInitialized = true; 0049 if (isOnline()) { 0050 mNotesManager->load(); 0051 } 0052 } 0053 0054 void NotesAgent::reload() 0055 { 0056 if (NotesAgentSettings::enabled()) { 0057 mNotesManager->load(); 0058 } 0059 } 0060 0061 void NotesAgent::setEnableAgent(bool enabled) 0062 { 0063 if (NotesAgentSettings::enabled() == enabled) { 0064 return; 0065 } 0066 0067 NotesAgentSettings::setEnabled(enabled); 0068 NotesAgentSettings::self()->save(); 0069 if (enabled) { 0070 mNotesManager->load(); 0071 } else { 0072 mNotesManager->stopAll(); 0073 } 0074 } 0075 0076 bool NotesAgent::enabledAgent() const 0077 { 0078 return NotesAgentSettings::enabled(); 0079 } 0080 0081 void NotesAgent::configure(WId windowId) 0082 { 0083 showConfigureDialog((qlonglong)windowId); 0084 } 0085 0086 void NotesAgent::showConfigureDialog(qlonglong windowId) 0087 { 0088 QPointer<NotesAgentSettingsDialog> dialog = new NotesAgentSettingsDialog; 0089 if (windowId) { 0090 dialog->setAttribute(Qt::WA_NativeWindow, true); 0091 KWindowSystem::setMainWindow(dialog->windowHandle(), windowId); 0092 } 0093 if (dialog->exec()) { 0094 mNotesManager->load(); 0095 } 0096 delete dialog; 0097 } 0098 0099 bool NotesAgent::receiveNotes() const 0100 { 0101 return NoteShared::NoteSharedGlobalConfig::receiveNotes(); 0102 } 0103 0104 void NotesAgent::setReceiveNotes(bool b) 0105 { 0106 if (NoteShared::NoteSharedGlobalConfig::receiveNotes() != b) { 0107 NoteShared::NoteSharedGlobalConfig::setReceiveNotes(b); 0108 NoteShared::NoteSharedGlobalConfig::self()->save(); 0109 mNotesManager->updateNetworkListener(); 0110 } 0111 } 0112 0113 int NotesAgent::port() const 0114 { 0115 return NoteShared::NoteSharedGlobalConfig::port(); 0116 } 0117 0118 void NotesAgent::setPort(int value) 0119 { 0120 if (value < 0) { 0121 return; 0122 } 0123 0124 if (NoteShared::NoteSharedGlobalConfig::port() != static_cast<uint>(value)) { 0125 NoteShared::NoteSharedGlobalConfig::setPort(value); 0126 NoteShared::NoteSharedGlobalConfig::self()->save(); 0127 if (NotesAgentSettings::enabled()) { 0128 mNotesManager->updateNetworkListener(); 0129 } 0130 } 0131 } 0132 0133 void NotesAgent::setAlarmCheckInterval(int value) 0134 { 0135 if (value < 0) { 0136 return; 0137 } 0138 0139 if (NoteShared::NoteSharedGlobalConfig::checkInterval() != static_cast<uint>(value)) { 0140 NoteShared::NoteSharedGlobalConfig::setCheckInterval(value); 0141 NoteShared::NoteSharedGlobalConfig::self()->save(); 0142 reload(); 0143 } 0144 } 0145 0146 void NotesAgent::configurationChanged() 0147 { 0148 NoteShared::NoteSharedGlobalConfig::self()->config()->reparseConfiguration(); 0149 mNotesManager->updateNetworkListener(); 0150 } 0151 0152 int NotesAgent::alarmCheckInterval() const 0153 { 0154 return NoteShared::NoteSharedGlobalConfig::checkInterval(); 0155 } 0156 0157 AKONADI_AGENT_MAIN(NotesAgent) 0158 0159 #include "moc_notesagent.cpp"