/*
 * registry.h by Matthias Braun <matze@braunis.de>
 *
 * Copyright (C) 2002 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) 
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation (version 2 of the License)
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
#ifndef __FILETREE_H__
#define __FILETREE_H__

#include <csutil/ref.h>
#include <csutil/array.h>
#include "registryfile.h"
#include "changelist.h"

struct iObjectRegistry;
struct iDocumentSystem;
struct iVFS;

namespace updater
{

        /** A module combines a list of files. Modules can be used to group files
     * (for things like operating system, basic/extended install)
     */
class Module : public FileList
{
    public:
    /** Create a new module */
    Module (const char* name);
    ~Module ();
           
        /// returns the name of the module
    const char* GetName() const;

    RegistryFile* GetRegistryFile(size_t id);
    void AddRegistryFile(RegistryFile* file) {list.Push(file); }
    size_t GetRegistryLength() { return list.Length(); }

    bool GetUsed() { return used; }
    void SetUsed(bool v) { used = v; }

    private:  
    FileList list;    
    char* name;
    bool used;
};

/** This class holds a tree structure (called registry) which represents 
 * the state of a repository (server or client). It is possible to
 * Save/Load/Download registry files and to compare them to produce
 * Changelists. 
 */
class Registry
{
public:
    Registry (iObjectRegistry* objreg);
    virtual ~Registry ();

    /// Load a registry file from disk (vfs path)
    bool Load (const char* filename);
    /// Save a registry file to disk (vfs path)
    void Save (const char* filename);

    /// Returns the module with name modulename
    Module* GetModule (const char* modulename);

    /// Create a new module with name modulename
    Module* CreateModule (const char* modulename);

    /// compared 2 registries
    bool operator == (Registry* other) const;

    /// Necessary CS definition and stub function
    virtual void IncRef ()
    {}
    /// Necessary CS definition and stub function
    virtual void DecRef ()
    {}

    int GetUpdaterVersion() { return updaterVersion;}
    const char* GetUpdaterMD5();
    const char* GetUpdaterPath();

    void SetReposVersion(const char* ver) { version = ver; };
    const char* GetReposVersion() { return version;};

private:
    /// Parse 1 directory/module entry.
    void ParseModule (iDocumentNode* node, Module* objectlist);
    /// Write 1 directory/module entry.
    void WriteModule (iDocumentNode* node, Module* mod);

    
    /// The module list
    csPDelArray<Module> modules;
    
    iObjectRegistry* objreg;
    csRef<iVFS> vfs;
    csRef<iDocumentSystem> xml;
    int updaterVersion;
    
    csString updaterWin,updaterMac,updaterLinux;
    csString md5Win,md5Mac, md5Linux;
    csString version;
};

} // end of namespace updater

#endif

