#ifndef SESSION_H
#define SESSION_H

/*
 * @(#)Session.h
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2003 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 */

#include <stdio.h>
#include "File.h"

class Server;
class AudioToWAV;

/**
 * A session on the server.
 *
 * @version 20030303
 * @author Jörg P. M. Haeger
 */
class Session {
	Server *server;
	const char *ID;
	File *dir;
	FILE *fileOutputStream;
	File currentFile;
	File *currentTrack;
	int currentTrackNo;
	int filesNum, tracksNum, sizeInKBytes;
	int linksNum;
	AudioToWAV *audioToWAV;

public:
	~Session();
	void checkContainsNoLink(const char *fileName);
	File *getFirstTrack();
	int getFilesNum() { return filesNum; }
	const char *getID() { return ID; }
	static void getInfo(const char *ID,
		int &numOfFiles, int &numOfTracks, int &size_KB);
	int getLinksNum() { return linksNum; }
	File *getNextTrack();
	Server *getServer() { return server; }
	static int getSessionSize(File &dir);
	int getSize() { return 1024L * sizeInKBytes; }
	int getSizeKB() { return sizeInKBytes; }
	int getTracksNum() { return tracksNum; }
	static int isSessionDir(File &dir);
	void linkFile(File &file, const char *linkName);
	void linkTrack(File &track, const char *type);
	void makeDir(const char *fileName, int mode, int time);
	void newTrack(const char *trackType, int link = 0);
	void putFile(FILE *inStream,
		const char *fileName, int length, int mode, int time);
	void putFileContinue(FILE *inStream, int length);
	void putFileEnd(int mode = -1, int time = 0);
	void putFileStart(const char *fileName);
	void putTrack(FILE *inStream, int length);
	void remove(const char *fileName);

private:
	Session(Server *server, const char *ID, const char *dir);
	void closeAudioTrack();
	void copy(FILE *inStream, FILE *outStream, int length);
	void copy(const char *buf, FILE *outStream, int length);
	void copy2(FILE *inStream, FILE *outStream, int &length);
	void countFile(const char *pathName);
	void create();
	void readInfo();
	static void readInfo(File *dir,
		int &numOfFiles, int &numOfTracks, int &size_KB);

	static int sizeToKBytes(int size) {
		return (size + 1023) / 1024;
	}

	void writeInfo();

	friend class Store;
};

#endif
