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

0001 /*
0002  *  eventid.h  -  KAlarm unique event identifier for resources
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2012-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "kalarmcalendar/kaevent.h"
0012 #include "kalarm_debug.h"
0013 
0014 using namespace KAlarmCal;
0015 
0016 /**
0017  * Unique event identifier for resources.
0018  * This consists of the event UID within the individual calendar, plus the
0019  * resource ID.
0020  *
0021  * Note that the resource ID of the display calendar is -1, since it is not a
0022  * resources calendar.
0023  */
0024 class EventId
0025 {
0026 public:
0027     EventId() = default;
0028     EventId(ResourceId c, const QString& e)
0029         : mEventId(e)
0030         , mResourceId(c)
0031     {}
0032     explicit EventId(const KAEvent& event)
0033         : mEventId(event.id())
0034         , mResourceId(event.resourceId())
0035     {}
0036 
0037     /** Set by event ID prefixed by optional resource ID, in the format "[rid:]eid".
0038      *  "rid" can be the resource configuration name, or the resource ID number in
0039      *  string format.
0040      *  @note  The resource ID number is the ID as used in the config and as known
0041      *         to the user, not the internal ID (where different).
0042      *  @note  Resources must have been created before calling this method;
0043      *         otherwise, the resource ID will be invalid (-1).
0044      */
0045     explicit EventId(const QString& resourceEventId);
0046 
0047     bool operator==(const EventId&) const;
0048     bool operator!=(const EventId& other) const   { return !operator==(other); }
0049 
0050     void clear()          { mResourceId = -1; mEventId.clear(); }
0051 
0052     /** Return whether the instance contains any data. */
0053     bool isEmpty() const  { return mEventId.isEmpty(); }
0054 
0055     ResourceId resourceId() const            { return mResourceId; }
0056     ResourceId resourceDisplayId() const;
0057     QString    eventId() const               { return mEventId; }
0058     void       setResourceId(ResourceId id)  { mResourceId = id; }
0059 
0060     /** Extract the resource and event ID strings from an ID in the format "[rid:]eid".
0061      *  "rid" can be the resource configuration name, or the resource ID number in
0062      *  string format.
0063      *  @note  The resource ID number is the ID as used in the config and as known
0064      *         to the user, not the internal ID (where different).
0065      *
0066      *  @param resourceEventId  Full ID "[rid:]eid"
0067      *  @param eventId          Receives the event ID "eid"
0068      *  @return  The resource ID "rid" (see note above).
0069      */
0070     static QString extractIDs(const QString& resourceEventId, QString& eventId);
0071 
0072     /** Get the numerical resource ID from a resource ID string.
0073      *  The string can be the resource configuration name, or the resource ID
0074      *  number in string format.
0075      *  @note  The resource ID number is the ID as used in the config and as known
0076      *         to the user, not the internal ID (where different).
0077      *  @note  Resources must have been created before calling this function;
0078      *         otherwise, the returned resource ID will be invalid (-1).
0079      *
0080      *  @param resourceIdString  Resource ID string "rid"
0081      *  @return  The resource ID, or -1 if not found.
0082      */
0083     static ResourceId getResourceId(const QString& resourceIdString);
0084 
0085 private:
0086     QString    mEventId;
0087     ResourceId mResourceId {-1};
0088 };
0089 
0090 // Declare as a movable type (note that QString is movable).
0091 Q_DECLARE_TYPEINFO(EventId, Q_RELOCATABLE_TYPE);
0092 
0093 inline size_t qHash(const EventId& eid, size_t seed = 0)
0094 {
0095     size_t h1 = qHash(eid.eventId(), seed);
0096     size_t h2 = qHash(eid.resourceId(), seed);
0097     return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
0098 }
0099 
0100 inline QDebug operator<<(QDebug s, const EventId& id)
0101 {
0102     s.nospace() << "\"" << id.resourceDisplayId() << "::" << id.eventId().toLatin1().constData() << "\"";
0103     return s.space();
0104 }
0105 
0106 // vim: et sw=4: