swift
contextapplicationimpl.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QFile>
7 #include <QFlags>
8 #include <QIODevice>
9 #include <QTextStream>
10 #include <QtGlobal>
11 
12 #include "core/application.h"
13 #include "core/inputmanager.h"
14 #include "misc/dbusserver.h"
15 #include "misc/logcategories.h"
16 #include "misc/logmessage.h"
17 #include "misc/settingscache.h"
19 
20 using namespace swift::misc;
21 
22 namespace swift::core::context
23 {
24  CContextApplication::CContextApplication(CCoreFacadeConfig::ContextMode mode, CCoreFacade *runtime)
25  : IContextApplication(mode, runtime), CIdentifiable(this)
26  {}
27 
29  {
30  // Intentionally don't check for round trip here
31  emit this->settingsChanged(settings, origin);
32  }
33 
35  {
36  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
38  }
39 
41  {
42  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
44  }
45 
47  {
48  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
49  const QStringList keys = CSettingsCache::instance()->getAllUnsavedKeys();
50  CSettingsDictionary result;
51  for (const QString &key : keys) { result.insert(key, CSettingsCache::instance()->getHumanReadableName(key)); }
52  return result;
53  }
54 
56  {
57  // no-op: proxy implements this method by calling getAllSettings
58  }
59 
61  {
62  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO << keyPrefix; }
63  return CSettingsCache::instance()->saveToStore(keyPrefix);
64  }
65 
67  {
68  if (isDebugEnabled())
69  {
70  CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO << keys.join(", ");
71  }
72  return CSettingsCache::instance()->saveToStore(keys);
73  }
74 
76  {
77  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
79  }
80 
81  void CContextApplication::registerHotkeyActions(const QStringList &actions, const CIdentifier &origin)
82  {
83  // Intentionally don't check for round trip here
84  emit this->hotkeyActionsRegistered(actions, origin);
85  }
86 
87  void CContextApplication::callHotkeyActionRemotely(const QString &action, bool argument, const CIdentifier &origin)
88  {
89  if (origin.hasApplicationProcessId())
90  {
91  // If it originated from this process, then we are going to emit a signal
92  emit this->remoteHotkeyAction(action, argument, origin);
93  }
94  else
95  {
96  // action came from a different process but on the same machine. Ignore
97  if (origin.isFromLocalMachine()) { return; }
98 
99  // Different process and different machine. Process it.
100  // However, it should not emit a remote action itself.
101  sApp->getInputManager()->callFunctionsBy(action, argument, false);
102  }
103  }
104 
106  {
107  if (isDebugEnabled())
108  {
109  CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO << application;
110  }
111 
112  if (!m_registeredApplications.contains(application))
113  {
114  m_registeredApplications.push_back(application);
115  emit this->registrationChanged();
117  }
118 
119  this->cleanupRegisteredApplications();
120  return application;
121  }
122 
124  {
125  if (isDebugEnabled())
126  {
127  CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO << application;
128  }
129  int r = m_registeredApplications.remove(application);
130  this->cleanupRegisteredApplications();
131  if (r > 0) { emit registrationChanged(); }
132  }
133 
134  void CContextApplication::cleanupRegisteredApplications()
135  {
136  // static const int outdatedMs = qRound(1.5 * PingIdentifiersMs);
137  // m_registeredApplications.removeOlderThanNowMinusOffset(outdatedMs);
138  }
139 
141  {
142  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
143  // static const int outdatedMs = qRound(1.5 * PingIdentifiersMs);
144  // return m_registeredApplications.findAfterNowMinusOffset(outdatedMs);
145  return m_registeredApplications;
146  }
147 
149  {
150  if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO; }
151  return this->identifier();
152  }
153 } // namespace swift::core::context
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
CInputManager * getInputManager() const
The input manager, if available.
Definition: application.h:302
ContextMode
How to handle a given context.
The class providing facades (the contexts) for all DBus relevant operations.
Definition: corefacade.h:57
void callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit=true)
Call functions by hotkeyfunction.
QStringList allAvailableActions() const
Get all available and known actions.
Definition: inputmanager.h:75
virtual void callHotkeyActionRemotely(const QString &action, bool argument, const swift::misc::CIdentifier &origin)
Call a hotkey action on a remote process.
virtual swift::misc::CStatusMessage loadSettings()
Load core settings from disk.
virtual void unregisterApplication(const swift::misc::CIdentifier &application)
Unregister application.
virtual swift::misc::CValueCachePacket getAllSettings() const
Get all settings currently in core settings cache.
virtual swift::misc::CStatusMessage saveSettings(const QString &keyPrefix={})
Save core settings to disk.
virtual swift::misc::CIdentifier getApplicationIdentifier() const
Identifier of application, remote side if distributed.
virtual swift::misc::CStatusMessage saveSettingsByKey(const QStringList &keys)
Save core settings to disk.
virtual void registerHotkeyActions(const QStringList &actions, const swift::misc::CIdentifier &origin)
Register hotkey action implemented by another process.
virtual void synchronizeLocalSettings()
Update local settings with settings from core.
virtual void changeSettings(const swift::misc::CValueCachePacket &settings, const swift::misc::CIdentifier &origin)
Ratify some settings changed by another process.
virtual QStringList getUnsavedSettingsKeys() const
Get keys of all unsaved settings currently in core settings cache.
virtual swift::misc::CIdentifierList getRegisteredApplications() const
All registered applications.
virtual swift::misc::CIdentifier registerApplication(const swift::misc::CIdentifier &application)
Register application, can also be used for ping.
virtual swift::core::context::CSettingsDictionary getUnsavedSettingsKeysDescribed() const
Get keys and descriptions of all unsaved settings currently in core settings cache.
void remoteHotkeyAction(const QString &action, bool argument, const swift::misc::CIdentifier &origin)
Call a hotkey action on a remote process.
void registrationChanged()
A component changes.
void hotkeyActionsRegistered(const QStringList &actions, const swift::misc::CIdentifier &origin)
New action was registered.
void settingsChanged(const swift::misc::CValueCachePacket &settings, const swift::misc::CIdentifier &origin)
One or more settings were changed.
bool isDebugEnabled() const
Debug enabled?
Definition: context.cpp:59
iterator insert(const Key &key, const Value &value)
Insert new item with key and value.
Definition: dictionary.h:374
Base class with a member CIdentifier to be inherited by a class which has an identity in the environm...
Definition: identifiable.h:24
const CIdentifier & identifier() const
Get identifier.
Definition: identifiable.h:27
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition: identifier.h:29
bool hasApplicationProcessId() const
Check if originating from the same process id.
Definition: identifier.cpp:179
bool isFromLocalMachine() const
Check if originating from the same local machine.
Definition: identifier.cpp:173
Value object encapsulating a list of object identifiers.
static const QString & contextSlot()
Context slots.
Definition: logcategories.h:87
Class for emitting a log message.
Definition: logmessage.h:27
Derived & debug()
Set the severity to debug.
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
int remove(const T &object)
Remove all elements equal to the given object, if it is contained.
Definition: sequence.h:435
CStatusMessage loadFromStore()
Load settings from disk.
static CSettingsCache * instance()
Return the singleton instance.
CStatusMessage saveToStore(const QString &keyPrefix={})
Save settings to disk.
Streamable status message, e.g.
swift::misc::CValueCachePacket getAllValuesWithTimestamps(const QString &keyPrefix={}) const
Return map containing all values in the cache, and timestamps when they were modified.
QStringList getAllUnsavedKeys(const QString &keyPrefix={}) const
Return keys of all values which have been changed but not saved.
Value class used for signalling changed values in the cache.
Definition: valuecache.h:67
Free functions in swift::misc.