refactor: make Resource use glib functions

This commit is contained in:
Michael Vetter
2026-02-26 17:40:57 +01:00
parent 8a36ca6d97
commit abffffb499
2 changed files with 18 additions and 16 deletions

View File

@@ -39,18 +39,20 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <glib.h>
#include "common.h" #include "common.h"
#include "xmpp/resource.h" #include "xmpp/resource.h"
Resource* Resource*
resource_new(const char* const name, resource_presence_t presence, const char* const status, const int priority) resource_new(const gchar* const name, resource_presence_t presence, const gchar* const status, const int priority)
{ {
assert(name != NULL); assert(name != NULL);
Resource* new_resource = malloc(sizeof(struct resource_t)); Resource* new_resource = g_new(Resource, 1);
new_resource->name = strdup(name); new_resource->name = g_strdup(name);
new_resource->presence = presence; new_resource->presence = presence;
if (status) { if (status) {
new_resource->status = strdup(status); new_resource->status = g_strdup(status);
} else { } else {
new_resource->status = NULL; new_resource->status = NULL;
} }
@@ -93,14 +95,14 @@ void
resource_destroy(Resource* resource) resource_destroy(Resource* resource)
{ {
if (resource) { if (resource) {
free(resource->name); g_free(resource->name);
free(resource->status); g_free(resource->status);
free(resource); g_free(resource);
} }
} }
gboolean gboolean
valid_resource_presence_string(const char* const str) valid_resource_presence_string(const gchar* const str)
{ {
assert(str != NULL); assert(str != NULL);
if ((strcmp(str, "online") == 0) || (strcmp(str, "chat") == 0) || (strcmp(str, "away") == 0) || (strcmp(str, "xa") == 0) || (strcmp(str, "dnd") == 0)) { if ((strcmp(str, "online") == 0) || (strcmp(str, "chat") == 0) || (strcmp(str, "away") == 0) || (strcmp(str, "xa") == 0) || (strcmp(str, "dnd") == 0)) {
@@ -110,7 +112,7 @@ valid_resource_presence_string(const char* const str)
} }
} }
const char* const gchar*
string_from_resource_presence(resource_presence_t presence) string_from_resource_presence(resource_presence_t presence)
{ {
switch (presence) { switch (presence) {
@@ -128,7 +130,7 @@ string_from_resource_presence(resource_presence_t presence)
} }
resource_presence_t resource_presence_t
resource_presence_from_string(const char* const str) resource_presence_from_string(const gchar* const str)
{ {
if (str == NULL) { if (str == NULL) {
return RESOURCE_ONLINE; return RESOURCE_ONLINE;

View File

@@ -40,20 +40,20 @@
typedef struct resource_t typedef struct resource_t
{ {
char* name; gchar* name;
resource_presence_t presence; resource_presence_t presence;
char* status; gchar* status;
int priority; int priority;
} Resource; } Resource;
Resource* resource_new(const char* const name, resource_presence_t presence, const char* const status, Resource* resource_new(const gchar* const name, resource_presence_t presence, const gchar* const status,
const int priority); const int priority);
void resource_destroy(Resource* resource); void resource_destroy(Resource* resource);
int resource_compare_availability(Resource* first, Resource* second); int resource_compare_availability(Resource* first, Resource* second);
gboolean valid_resource_presence_string(const char* const str); gboolean valid_resource_presence_string(const gchar* const str);
const char* string_from_resource_presence(resource_presence_t presence); const gchar* string_from_resource_presence(resource_presence_t presence);
resource_presence_t resource_presence_from_string(const char* const str); resource_presence_t resource_presence_from_string(const gchar* const str);
contact_presence_t contact_presence_from_resource_presence(resource_presence_t resource_presence); contact_presence_t contact_presence_from_resource_presence(resource_presence_t resource_presence);
#endif #endif