Implement koto_utils_is_valid_string to replace repetitive NULL and g_strcmp0 code.

Reduce usage of g_return_if_fail since it results in unnecessary warnings.
This commit is contained in:
Joshua Strobl 2021-05-07 21:52:42 +03:00
parent 0aafa68a35
commit b4ffba62c7
17 changed files with 121 additions and 65 deletions

View file

@ -171,7 +171,7 @@ void koto_create_modify_playlist_dialog_handle_create_click(GtkButton *button, g
}
KotoPlaylist *playlist = NULL;
gboolean modify_existing_playlist = ((self->playlist_uuid != NULL) && (g_strcmp0(self->playlist_uuid, "") != 0));
gboolean modify_existing_playlist = koto_utils_is_string_valid(self->playlist_uuid);
if (modify_existing_playlist) { // Modifying an existing playlist
playlist = koto_cartographer_get_playlist_by_uuid(koto_maps, self->playlist_uuid);
@ -263,7 +263,7 @@ void koto_create_modify_playlist_dialog_reset(KotoCreateModifyPlaylistDialog *se
}
void koto_create_modify_playlist_dialog_set_playlist_uuid(KotoCreateModifyPlaylistDialog *self, gchar *playlist_uuid) {
if ((playlist_uuid == NULL) || g_strcmp0(playlist_uuid, "") == 0) { // Is an empty string or not a string at all
if (!koto_utils_is_string_valid(playlist_uuid)) { // Not a valid playlist UUID string
return;
}
@ -279,7 +279,7 @@ void koto_create_modify_playlist_dialog_set_playlist_uuid(KotoCreateModifyPlayli
gchar *art = koto_playlist_get_artwork(playlist);
if ((art == NULL) || (g_strcmp0(art, "") == 0)) { // Is an empty string or not set
if (!koto_utils_is_string_valid(art)) { // If art is not defined
gtk_image_set_from_icon_name(GTK_IMAGE(self->playlist_image), "insert-image-symbolic"); // Reset the image
} else {
gtk_image_set_from_file(GTK_IMAGE(self->playlist_image), art);

View file

@ -445,7 +445,7 @@ gchar* koto_playlist_go_to_next(KotoPlaylist *self) {
return random_track_uuid;
}
if (self->current_uuid == NULL || (g_strcmp0(self->current_uuid, "") == 0)) {
if (!koto_utils_is_string_valid(self->current_uuid)) { // No valid UUID yet
self->current_position++;
} else { // Have a UUID currently
KotoIndexedTrack *track = koto_cartographer_get_track_by_uuid(koto_maps, self->current_uuid);
@ -474,7 +474,7 @@ gchar* koto_playlist_go_to_previous(KotoPlaylist *self) {
return koto_playlist_get_random_track(self); // Get a random track
}
if (self->current_uuid == NULL || (g_strcmp0(self->current_uuid, "") == 0)) {
if (!koto_utils_is_string_valid(self->current_uuid)) { // No valid UUID
return NULL;
}
@ -736,8 +736,10 @@ void koto_playlist_set_position(KotoPlaylist *self, gint position) {
void koto_playlist_set_track_as_current(KotoPlaylist *self, gchar *track_uuid) {
gint position_of_track = g_queue_index(self->sorted_tracks, track_uuid); // Get the position of the UUID in our tracks
g_return_if_fail(position_of_track != -1);
self->current_position = position_of_track;
if (position_of_track != -1) { // In tracks
self->current_position = position_of_track;
}
}
void koto_playlist_set_uuid(KotoPlaylist *self, const gchar *uuid) {