File indexing completed on 2024-05-12 05:14:44

0001 /*
0002  *  eventid.cpp  -  KAlarm unique event identifier for resources
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2012-2021 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 
0010 #include "eventid.h"
0011 
0012 #include "resources/resources.h"
0013 
0014 #include <QRegularExpression>
0015 
0016 /** Set by event ID prefixed by optional resource ID, in the format "[rid:]eid". */
0017 EventId::EventId(const QString& resourceEventId)
0018 {
0019     const QString resourceIdString = extractIDs(resourceEventId, mEventId);
0020     if (!resourceIdString.isEmpty())
0021         mResourceId = getResourceId(resourceIdString);  // convert the resource ID string
0022 }
0023 
0024 bool EventId::operator==(const EventId& other) const
0025 {
0026     return mEventId == other.mEventId  &&  mResourceId == other.mResourceId;
0027 }
0028 
0029 ResourceId EventId::resourceDisplayId() const
0030 {
0031     return (mResourceId > 0) ? (mResourceId & ~ResourceType::IdFlag) : mResourceId;
0032 }
0033 
0034 QString EventId::extractIDs(const QString& resourceEventId, QString& eventId)
0035 {
0036     static const QRegularExpression rx(QStringLiteral("^(\\w+):(.*)$"));
0037     QRegularExpressionMatch rxmatch = rx.match(resourceEventId);
0038     if (!rxmatch.hasMatch())
0039     {
0040         eventId = resourceEventId;   // no resource ID supplied
0041         return {};
0042     }
0043 
0044     // A resource ID has been supplied
0045     eventId = rxmatch.captured(2);
0046     return rxmatch.captured(1);
0047 }
0048 
0049 ResourceId EventId::getResourceId(const QString& resourceIdString)
0050 {
0051     // Check if a resource configuration name has been supplied.
0052     Resource res = Resources::resourceForConfigName(resourceIdString);
0053     if (res.isValid())
0054         return res.id();
0055 
0056     // Check if a resource ID number has been supplied.
0057     bool ok;
0058     const ResourceId id = resourceIdString.toLongLong(&ok);
0059     if (ok)
0060     {
0061         res = Resources::resourceFromDisplayId(id);
0062         if (res.isValid())
0063             return res.id();
0064     }
0065     return -1;
0066 }
0067 
0068 // vim: et sw=4: