
/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: config.c,v 1.1.1.1 2004/11/19 16:45:31 lordjaxom Exp $
 */

#include "config.h"
#include "jobs.h"
#include "xml.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

// --- cSubmuxConfig ----------------------------------------------------------

cSubmuxConfig::cSubmuxConfig(const char *Format,...)
{
    mButtonsNormal = NULL;
    mButtonsSelect = NULL;
    mButtonsHighlight = NULL;

    va_list ap;
    va_start(ap, Format);
    vasprintf(&mFileName, Format, ap);
    va_end(ap);
}

cSubmuxConfig::~cSubmuxConfig()
{
    free(mFileName);
    free(mButtonsNormal);
    free(mButtonsSelect);
    free(mButtonsHighlight);
}

void cSubmuxConfig::Write(void)
{
    FILE *f;
    if ((f = fopen(mFileName, "w")) != NULL) {
        fprintf(f, "<subpictures><stream><spu force=\"yes\" "
                "start=\"00:00:00.00\" image=\"%s\" select=\"%s\" highlight=\"%s\" "
                "autooutline=\"infer\" outlinewidth=\"5\" autoorder=\"columns\"/></stream>"
                "</subpictures>", mButtonsNormal, mButtonsSelect,
                mButtonsHighlight);
        fclose(f);
    }
}

void cSubmuxConfig::SetButtonNormal(const char *Format, ...)
{
    va_list ap;
    va_start(ap, Format);
    vasprintf(&mButtonsNormal, Format, ap);
    va_end(ap);
}

void cSubmuxConfig::SetButtonSelect(const char *Format, ...)
{
    va_list ap;
    va_start(ap, Format);
    vasprintf(&mButtonsSelect, Format, ap);
    va_end(ap);
}

void cSubmuxConfig::SetButtonHighlight(const char *Format, ...)
{
    va_list ap;
    va_start(ap, Format);
    vasprintf(&mButtonsHighlight, Format, ap);
    va_end(ap);
}

// --- cDvdauthorConfig  ------------------------------------------------------

cDvdauthorConfig::cDvdauthorConfig(cBurnJob * Job, const char *Format, ...)
{
    mJob = Job;
    mTempDir = NULL;

    va_list ap;
    va_start(ap, Format);
    vasprintf(&mFileName, Format, ap);
    va_end(ap);
}

cDvdauthorConfig::~cDvdauthorConfig()
{
    free(mFileName);
    free(mTempDir);
}

void cDvdauthorConfig::Write(void)
{
    FILE *f;
    Dprintf("writing: %s\n", mFileName);
    if ((f = fopen(mFileName, "w")) != NULL) {
        int pages = ScanPageCount(mTempDir);
        cBurnRecording *rec;

        fprintf(f, DVDAuthorHeader, mTempDir);
        fprintf(f, DVDAuthorVMGMHeader);

        for (int p = 0; p < pages; ++p) {
            if (p == 0) {
                fprintf(f, DVDAuthorVMGMEntryTitleHeader);
                for (int i = 1; i < pages; i++) {
                    fprintf(f, DVDAuthorVMGMEntryTitleHeaderPre, i + 1,
                            i + 1);
                }
                fprintf(f, DVDAuthorVMGMEntryTitleHeader2, mTempDir, p);
            } else {
                fprintf(f, DVDAuthorVMGMEntryHeader, mTempDir, p);
            }

            for (int i = p * MAXPAGETITLES;
                    i < min(p * MAXPAGETITLES + MAXPAGETITLES, mJob->Count());
                    ++i)
            {
                fprintf(f, DVDAuthorVMGMEntryButton1, i + 1);
            }

            if (p > 0) {
                fprintf(f, DVDAuthorVMGMEntryButton2, p, 4096, p);
            }
            if (p < pages - 1) {
                fprintf(f, DVDAuthorVMGMEntryButton2, p + 2, 1024, p + 2);
            }

            fprintf(f, DVDAuthorVMGMEntryFooter);
        }
        fprintf(f, DVDAuthorVMGMFooter);

        for (rec = mJob->First(); rec; rec = mJob->Next(rec)) {
            pages = ScanPageCount(rec->TempDir());
            fprintf(f, DVDAuthorTitleHeader);
            for (int i = 0; i < pages; ++i) {
                fprintf(f, i == 0 ? DVDAuthorTitleMenuRootHeader
                        : DVDAuthorTitleMenuHeader, rec->TempDir(), i);
                if (i > 0) {     // Button: previous
                    fprintf(f, DVDAuthorTitleMenuButton, 2, i);
                }
                fprintf(f, DVDAuthorTitleMenuFilm);

                if (i < pages - 1) {     // Button: next
                    fprintf(f, DVDAuthorTitleMenuButton,
                            i == pages - 2 ? 3 : 1, i + 2);
                }

                fprintf(f, DVDAuthorTitleMenuFooter);
            }
            fprintf(f, DVDAuthorTitleFooter, rec->TempDir(),
                    rec->GetChapters(mJob->ChaptersMode()));
        }
        fprintf(f, DVDAuthorFooter);
        fclose(f);
    }
}

void cDvdauthorConfig::SetTempDir(const char *TempDir)
{
    free(mTempDir);
    mTempDir = strdup(TempDir);
}

