6 #include <QCoreApplication>
8 #include <QStringBuilder>
30 const QString &cachedEscapedApplicationName()
32 static const QString appName = toDBusPathElement(QCoreApplication::applicationName());
37 const QString &cachedLocalHostName()
39 static const QString hostName = QHostInfo::localHostName();
45 UuidStringLen =
sizeof(
"00000000-0000-0000-0000-000000000000")
48 QByteArray cachedMachineUniqueId()
50 static const QByteArray machineUniqueId = QSysInfo::machineUniqueId();
51 return machineUniqueId;
56 CIdentifier::CIdentifier(
const QString &name)
57 : m_name(name.trimmed()), m_machineIdBase64(cachedMachineUniqueId().toBase64(QByteArray::OmitTrailingEquals)),
58 m_machineName(cachedLocalHostName()), m_processName(cachedEscapedApplicationName()),
59 m_processId(QCoreApplication::applicationPid())
62 CIdentifier::CIdentifier(
const QString &name, QObject *
object) :
CIdentifier(name)
72 CIdentifier::CIdentifier(
const QString &name,
const QString &machineId,
const QString &machineName,
73 const QString &processName, qint64 processId)
74 : m_name(name), m_machineIdBase64(machineId), m_machineName(machineName), m_processName(processName),
75 m_processId(processId)
93 "fake", QByteArrayLiteral(
"00000000-0000-0000-0000-000000000000").toBase64(QByteArray::OmitTrailingEquals),
94 "fake machine",
"fake process", 0);
100 static const QUuid ns = QUuid::createUuid();
103 baseData.append(
reinterpret_cast<const char *
>(&m_processId),
sizeof(m_processId));
104 baseData.append(
getName().toUtf8());
105 return QUuid::createUuidV5(ns, baseData);
112 if (m_name.endsWith(name)) {
return; }
113 if (name.isEmpty()) {
return; }
114 const int index = m_name.lastIndexOf(
':');
115 if (index >= 0) { m_name = m_name.left(index); }
116 m_name += QStringLiteral(
":") + name;
121 if (!
object) {
return; }
122 QObject::connect(
object, &QObject::objectNameChanged,
object,
123 [=](
const QString &name) { this->
appendName(name); });
128 return *QByteArray::fromBase64Encoding(m_machineIdBase64.toLocal8Bit());
134 path +=
'/' % toDBusPathElement(m_machineName) %
"__" % toDBusPathElement(m_machineIdBase64);
135 path +=
'/' % toDBusPathElement(m_processName) %
"__" % QString::number(m_processId);
137 const QString name = toDBusPath(m_name);
138 Q_ASSERT_X(!name.contains(
"//") && !name.startsWith(
'/') && !name.endsWith(
'/'), Q_FUNC_INFO,
"Invalid name");
139 if (!name.isEmpty()) { path +=
'/' % name; }
145 const QString relative = path.startsWith(root) ? path.mid(root.length()) : path;
146 const QString machine = relative.section(
'/', 1, 1);
147 const QString process = relative.section(
'/', 2, 2);
148 const QString name = relative.section(
'/', 3, -1);
151 result.m_machineIdBase64 = fromDBusPathElement(machine.section(
"__", 1, 1));
152 result.m_machineName = fromDBusPathElement(machine.section(
"__", 0, 0));
153 result.m_processId = process.section(
"__", 1, 1).toInt();
154 result.m_processName = fromDBusPathElement(process.section(
"__", 0, 0));
165 return !m_machineIdBase64.isEmpty() && m_machineIdBase64 == other.m_machineIdBase64;
186 return cachedEscapedApplicationName() == toDBusPathElement(
getProcessName());
195 m_machineIdBase64 = cachedMachineUniqueId().toBase64(QByteArray::OmitTrailingEquals);
196 m_machineName = cachedLocalHostName();
201 m_processName = QCoreApplication::applicationName();
202 m_processId = QCoreApplication::applicationPid();
208 const QString s = m_name % u
' ' % m_machineIdBase64 % u
' ' % m_machineName % u
' ' %
209 QString::number(m_processId) % u
' ' % m_processName;
215 if (index.
isMyself()) {
return QVariant::fromValue(*
this); }
221 case IndexName:
return QVariant::fromValue(m_name);
222 case IndexMachineIdBase64:
return QVariant::fromValue(m_machineIdBase64);
223 case IndexMachineName:
return QVariant::fromValue(
getMachineName());
224 case IndexMachineId:
return QVariant::fromValue(
getMachineId());
225 case IndexProcessId:
return QVariant::fromValue(m_processId);
226 case IndexProcessName:
return QVariant::fromValue(m_processName);
236 if (index.
isMyself()) {
return Compare::compare(m_processId, compareValue.m_processId); }
242 case IndexName:
return m_name.compare(compareValue.m_name, Qt::CaseInsensitive);
243 case IndexMachineIdBase64:
return m_machineIdBase64.compare(compareValue.m_machineIdBase64);
244 case IndexMachineName:
return m_machineName.compare(compareValue.m_machineName, Qt::CaseInsensitive);
245 case IndexMachineId:
return m_machineName.compare(compareValue.m_machineName, Qt::CaseInsensitive);
246 case IndexProcessId:
return Compare::compare(m_processId, compareValue.m_processId);
247 case IndexProcessName:
return m_processName.compare(compareValue.m_processName, Qt::CaseInsensitive);
248 case IndexIsFromLocalMachine:
250 case IndexIsFromSameProcess:
252 case IndexIsFromSameProcessName:
Value object encapsulating information identifying a component of a modular distributed swift process...
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool hasApplicationProcessName() const
Check if originating from the same process name.
static const CIdentifier & anonymous()
Returns an anonymous identifier, which is a valid identifier without name.
bool hasSameMachineId(const CIdentifier &other) const
Check if other identifier is from the same machine id.
void appendName(const QString &name)
Set name or append name.
bool isNull() const
Null identifier (no name, ids etc)
ColumnIndex
Properties by index.
bool isAnonymous() const
Check if it is anonymous identifier.
qint64 getProcessId() const
Get process id.
QUuid toUuid() const
Produces a UUID generated from the identifier.
QByteArray getMachineId() const
Get machine id.
bool hasApplicationProcessId() const
Check if originating from the same process id.
const QString & getProcessName() const
Get process name.
const QString & getName() const
Name.
static CIdentifier fromDBusObjectPath(const QString &path, const QString &root={})
Reconstruct an identifier from a DBus object path.
static const CIdentifier & fake()
Returns a fake identifier.
const QString & getMachineName() const
Machine name.
bool hasSameMachineName(const CIdentifier &other) const
Check if the other identifier has the same machine name.
void updateToCurrentProcess()
Update to current process.
bool isFromLocalMachine() const
Check if originating from the same local machine.
void updateToCurrentMachine()
Update to current machine.
void linkWithQObjectName(QObject *object)
Reflect changes of QObject::
bool hasSameMachineNameOrId(const CIdentifier &other) const
Same machine or id?
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
static const CIdentifier & null()
Null (empty) identifier.
QString toUuidString() const
UUID string.
QString convertToQString(bool i18n=false) const
Cast as QString.
QString toDBusObjectPath(const QString &root={}) const
Produces a DBus object path from the identifier.
int comparePropertyByIndex(CPropertyIndexRef index, const CIdentifier &compareValue) const
Compare for index.
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Free functions in swift::misc.
SWIFT_MISC_EXPORT QByteArray utfToPercentEncoding(const QString &s, const QByteArray &allow={}, char percent='%')
Extended percent encoding supporting UTF-16.
SWIFT_MISC_EXPORT QString utfFromPercentEncoding(const QByteArray &ba, char percent='%')
Reverse utfFromPercentEncoding.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.