Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #pragma once
5 : :
6 : : #if !defined (VALENT_INSIDE) && !defined (VALENT_COMPILATION)
7 : : # error "Only <valent.h> can be included directly."
8 : : #endif
9 : :
10 : : #include <json-glib/json-glib.h>
11 : :
12 : : #include "../core/valent-object.h"
13 : :
14 : : G_BEGIN_DECLS
15 : :
16 : :
17 : : /**
18 : : * ValentPacketError:
19 : : * @VALENT_PACKET_ERROR_UNKNOWN: an unknown error
20 : : * @VALENT_PACKET_ERROR_INVALID_DATA: the packet is %NULL or not JSON
21 : : * @VALENT_PACKET_ERROR_MALFORMED: the packet structure is malformed
22 : : * @VALENT_PACKET_ERROR_INVALID_FIELD: an expected field holds an invalid type
23 : : * @VALENT_PACKET_ERROR_MISSING_FIELD: an expected field is missing
24 : : *
25 : : * Error enumeration for KDE Connect packet validation.
26 : : *
27 : : * This enumeration can be extended at later date
28 : : *
29 : : * Since: 1.0
30 : : */
31 : : typedef enum {
32 : : VALENT_PACKET_ERROR_UNKNOWN,
33 : : VALENT_PACKET_ERROR_INVALID_DATA,
34 : : VALENT_PACKET_ERROR_MALFORMED,
35 : : VALENT_PACKET_ERROR_INVALID_FIELD,
36 : : VALENT_PACKET_ERROR_MISSING_FIELD,
37 : : } ValentPacketError;
38 : :
39 : : VALENT_AVAILABLE_IN_1_0
40 : : GQuark valent_packet_error_quark (void);
41 : : #define VALENT_PACKET_ERROR (valent_packet_error_quark ())
42 : :
43 : :
44 : : /**
45 : : * valent_packet_is_valid:
46 : : * @packet: (nullable): a `JsonNode`
47 : : *
48 : : * Check if @packet is a well-formed KDE Connect packet. This can be used in
49 : : * g_return_if_fail() checks.
50 : : *
51 : : * Returns: %TRUE if @packet is valid, or %FALSE if not
52 : : */
53 : : static inline gboolean
54 : 2009 : valent_packet_is_valid (JsonNode *packet)
55 : : {
56 : 2009 : JsonObject *root;
57 : 2009 : JsonNode *node;
58 : :
59 [ + - - + ]: 2009 : if G_UNLIKELY (packet == NULL || !JSON_NODE_HOLDS_OBJECT (packet))
60 : 0 : return FALSE;
61 : :
62 : 2009 : root = json_node_get_object (packet);
63 : :
64 [ + - + - ]: 2009 : if G_UNLIKELY ((node = json_object_get_member (root, "type")) == NULL ||
65 : : json_node_get_value_type (node) != G_TYPE_STRING)
66 : 0 : return FALSE;
67 : :
68 [ + - + - ]: 2009 : if G_UNLIKELY ((node = json_object_get_member (root, "body")) == NULL ||
69 : : json_node_get_node_type (node) != JSON_NODE_OBJECT)
70 : 0 : return FALSE;
71 : :
72 : : /* These two are optional, but have defined value types */
73 [ + + - + ]: 2009 : if G_UNLIKELY ((node = json_object_get_member (root, "payloadSize")) != NULL &&
74 : : json_node_get_value_type (node) != G_TYPE_INT64)
75 : : return FALSE;
76 : :
77 [ + + - + ]: 2009 : if G_UNLIKELY ((node = json_object_get_member (root, "payloadTransferInfo")) != NULL &&
78 : : json_node_get_node_type (node) != JSON_NODE_OBJECT)
79 : : return FALSE;
80 : :
81 : : return TRUE;
82 : : }
83 : : #define VALENT_IS_PACKET(packet) (valent_packet_is_valid (packet))
84 : :
85 : :
86 : : /* Packet Helpers */
87 : : VALENT_AVAILABLE_IN_1_0
88 : : JsonNode * valent_packet_new (const char *type);
89 : : VALENT_AVAILABLE_IN_1_0
90 : : void valent_packet_init (JsonBuilder **builder,
91 : : const char *type);
92 : : VALENT_AVAILABLE_IN_1_0
93 : : JsonNode * valent_packet_end (JsonBuilder **builder);
94 : : VALENT_AVAILABLE_IN_1_0
95 : : int64_t valent_packet_get_id (JsonNode *packet);
96 : : VALENT_AVAILABLE_IN_1_0
97 : : const char * valent_packet_get_type (JsonNode *packet);
98 : : VALENT_AVAILABLE_IN_1_0
99 : : JsonObject * valent_packet_get_body (JsonNode *packet);
100 : : VALENT_AVAILABLE_IN_1_0
101 : : gboolean valent_packet_has_payload (JsonNode *packet);
102 : : VALENT_AVAILABLE_IN_1_0
103 : : JsonObject * valent_packet_get_payload_full (JsonNode *packet,
104 : : goffset *size,
105 : : GError **error);
106 : : VALENT_AVAILABLE_IN_1_0
107 : : void valent_packet_set_payload_full (JsonNode *packet,
108 : : JsonObject *info,
109 : : goffset size);
110 : : VALENT_AVAILABLE_IN_1_0
111 : : JsonObject * valent_packet_get_payload_info (JsonNode *packet);
112 : : VALENT_AVAILABLE_IN_1_0
113 : : void valent_packet_set_payload_info (JsonNode *packet,
114 : : JsonObject *info);
115 : : VALENT_AVAILABLE_IN_1_0
116 : : goffset valent_packet_get_payload_size (JsonNode *packet);
117 : : VALENT_AVAILABLE_IN_1_0
118 : : void valent_packet_set_payload_size (JsonNode *packet,
119 : : goffset size);
120 : :
121 : : /* Field Helpers */
122 : : VALENT_AVAILABLE_IN_1_0
123 : : gboolean valent_packet_check_field (JsonNode *packet,
124 : : const char *field);
125 : : VALENT_AVAILABLE_IN_1_0
126 : : gboolean valent_packet_get_boolean (JsonNode *packet,
127 : : const char *field,
128 : : gboolean *value);
129 : : VALENT_AVAILABLE_IN_1_0
130 : : gboolean valent_packet_get_double (JsonNode *packet,
131 : : const char *field,
132 : : double *value);
133 : : VALENT_AVAILABLE_IN_1_0
134 : : gboolean valent_packet_get_int (JsonNode *packet,
135 : : const char *field,
136 : : int64_t *value);
137 : : VALENT_AVAILABLE_IN_1_0
138 : : gboolean valent_packet_get_string (JsonNode *packet,
139 : : const char *field,
140 : : const char **value);
141 : : VALENT_AVAILABLE_IN_1_0
142 : : gboolean valent_packet_get_array (JsonNode *packet,
143 : : const char *field,
144 : : JsonArray **value);
145 : : VALENT_AVAILABLE_IN_1_0
146 : : gboolean valent_packet_get_object (JsonNode *packet,
147 : : const char *field,
148 : : JsonObject **value);
149 : : VALENT_AVAILABLE_IN_1_0
150 : : GStrv valent_packet_dup_strv (JsonNode *packet,
151 : : const char *field);
152 : :
153 : : /* I/O Helpers */
154 : : VALENT_AVAILABLE_IN_1_0
155 : : gboolean valent_packet_validate (JsonNode *packet,
156 : : GError **error);
157 : : VALENT_AVAILABLE_IN_1_0
158 : : JsonNode * valent_packet_from_stream (GInputStream *stream,
159 : : gssize max_len,
160 : : GCancellable *cancellable,
161 : : GError **error);
162 : : VALENT_AVAILABLE_IN_1_0
163 : : gboolean valent_packet_to_stream (GOutputStream *stream,
164 : : JsonNode *packet,
165 : : GCancellable *cancellable,
166 : : GError **error);
167 : : VALENT_AVAILABLE_IN_1_0
168 : : char * valent_packet_serialize (JsonNode *packet);
169 : : VALENT_AVAILABLE_IN_1_0
170 : : JsonNode * valent_packet_deserialize (const char *json,
171 : : GError **error);
172 : :
173 : : G_END_DECLS
|