foxygit / ytmdl Log in
commits tags

/c/src/queue.h · 1.6 KB

raw
#ifndef YTMDL_QUEUE_H
#define YTMDL_QUEUE_H

#include <pthread.h>

#define MAX_QUEUE_ITEMS 4096
#define URL_CAP 2048
#define TITLE_CAP 512
#define SUBDIR_CAP 256
#define SPEED_CAP 32
#define ETA_CAP 16
#define ERROR_CAP 256

typedef enum {
    STATUS_PENDING,
    STATUS_DOWNLOADING,
    STATUS_DONE,
    STATUS_ERROR,
} ItemStatus;

typedef struct {
    int id;          /* stable unique id; -1 means unused slot */
    char url[URL_CAP];
    char title[TITLE_CAP];
    char subdir[SUBDIR_CAP];
    ItemStatus status;
    float percent;
    char speed[SPEED_CAP];
    char eta[ETA_CAP];
    char error[ERROR_CAP];
} QueueItem;

typedef struct {
    QueueItem items[MAX_QUEUE_ITEMS];
    int count;      /* number of active slots, compacted */
    int next_id;
    pthread_mutex_t lock;
} Queue;

void queue_init(Queue *q);

/* Adds a new pending item, returns its id, or -1 if the queue is full. */
int queue_add(Queue *q, const char *url, const char *title, const char *subdir);

/* Looks an item up by id and copies it into *out. Returns 1 if found, 0 otherwise. */
int queue_get(Queue *q, int id, QueueItem *out);

void queue_set_status(Queue *q, int id, ItemStatus status, const char *error);
void queue_set_progress(Queue *q, int id, float percent, const char *speed, const char *eta);
void queue_reset_for_retry(Queue *q, int id);

/* Removes a single item by id. No-op if not found or currently downloading. */
void queue_remove(Queue *q, int id);

/* Removes every successfully DONE item (errored items are left so they can
 * still be inspected/retried). Returns how many were removed. */
int queue_clear_finished(Queue *q);

#endif