/*
 * cdda_lib.h: The cddb acccess implementation
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: cdda_cddb.h,v 1.1.1.2 2005/06/27 13:34:02 cm Exp $
 */

#ifndef __CDDACDDB_H
#define __CDDACDDB_H

#include <vdr/thread.h>
#include <netinet/in.h>
#include <unistd.h>
#include "cdda_device.h"

class cCddaCddbCommon {
  private:
    cCddaDisc *po_disc;
  protected:
    cCddaCddbCommon(cCddaDisc *disc);
    ~cCddaCddbCommon();
    char *ReadAllFromSocket(int fd, int &length);
    void UpdateCdtext(const char *genre, const char *key, const char *value);
    char *FixString(char *string);
};

class cCddaCddbCache : public cCddaCddbCommon {
  private:
    char *p_directory;
    bool TestDirectory(const char *dir);
  public:
    cCddaCddbCache(cCddaDisc *disc, const char *dir);
    ~cCddaCddbCache();
    bool Write(unsigned long cddbId, const char *genre, const char *data, int length);
    bool Query(unsigned long cddbId);
};

class cCddaCddbServer : public cCddaCddbCommon {
  private:
    int sockfd;
    struct sockaddr_in serverAddr;
    char *p_fqdn;
    char *p_username;
    char *p_hostname;
    char *p_clientname;
    char *p_version;
    cCddaCddbCache *po_cache;
    bool LookupHost(void);
  public:
    cCddaCddbServer(cCddaDisc *disc, const char *fqdn, unsigned int port);
    ~cCddaCddbServer();
    bool Connect(void);
    void Disconnect(void);
    bool isConnected(void) { return -1 != sockfd; };
    bool Query(unsigned long cddbId, unsigned char num, unsigned int length, unsigned int offsets[]);
    void SetUser(const char *name);
    void SetHost(const char *name);
    void SetClient(const char *name);
    void SetVersion(const char *version);
    void SetCache(cCddaCddbCache *cache);
};

class cCddaCddb : cThread {
  private:
    bool active;
    unsigned char numTracks;
    unsigned int discLength;
    unsigned int *p_trackOffsets;
    unsigned long id;
    cCddaDisc *po_disc;
    cCddaCddbCache oCache;
    cCddaCddbServer oServer;
    void Activate(bool On);
    void Clear(void);
  protected:
    void Action();
  public:
    cCddaCddb(cCddaDisc *disc, const char *fqdn, unsigned int port, const char *cacheDir);
    ~cCddaCddb();
    bool Query(unsigned long cddbId, unsigned char num, unsigned int length, unsigned int offsets[]);
    void SetUser(const char *name);
    void SetHost(const char *name);
    void SetClient(const char *name);
    void SetVersion(const char *version);
};

#endif
