/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *         (C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#ifndef __DISK_H
#define __DISK_H

#include <size.h>
#include <device.h>

/* std::out_of_range */
#include <stdexcept>

/* Qt */
#include <qstring.h>
#include <qptrlist.h>
#include <qstringlist.h>

/* libdisk */
#include <sys/types.h>
#include <libdisk.h>


/** @brief Class for reading and writing a disk's partition table.
  *
  * This class uses libdisk(3). This library has some rough edges
  * because a few NULL-pointers can lead to many problems, be careful
  * when changing something here.
  */
class Disk : public Device {
public:
    /** Forward-Declaration of Disk::Chunk */
    class Chunk;

    enum BootMgr {
        BootMenu,
        StandardMBR
    };

    Disk(const Disk&);
    Disk(const QString&);
    virtual ~Disk();

    virtual QString getDesc() const;

    void  allFreeBSD();
    bool  setBoot(BootMgr);

    bool  writeChanges();
    void  undoChanges();

    uint  chunkCount() const;
    uint  usedChunkCount() const;
    const Size&  size() const;

    bool  saneBIOSGeom() const;
    void  sanitizeBIOSGeom();
    void  setBIOSGeometry(ulong cyl, ulong heads, ulong sect);
    ulong getBIOSCyl();
    ulong getBIOSHeads();
    ulong getBIOSSectors();

    Chunk  operator[] (uint) throw (std::out_of_range);
    Disk&  operator= (const Disk&);

    bool isMounted() const;

	 static QPtrList<Disk> getAllDevices();
	 static QStringList getAllDeviceNames();

private:
    void readDescription();
    void update();
    chunk *findChnk(uint) const;
    static uchar *bootAlloc(const QString&,  size_t* = NULL);

    /** Proper description, e.g. "IDE 1 Master" */
    QString desc;

    struct disk  *dsk;  /**< The disk structure used in libdisk. */

    uint  chunks;       /**< The disk's chunk count. */
    Size  totalSize;    /**< The disk's total size   */
};

#endif /* __DISK_H */
