/*
 * ViewerSettings.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "ViewerSettings.h"
#include <qpe/config.h>
#include "Bookmark.h"

#include <stdio.h>

ViewerSettings *ViewerSettings::theInstance = 0;
int ViewerSettings::refCount = 0;
QList<Bookmark> ViewerSettings::bmList;

ViewerSettings::ViewerSettings()
{
  bmList.setAutoDelete(true);
  load();
}

ViewerSettings *ViewerSettings::getInstance()
{
  if(!theInstance)
    theInstance = new ViewerSettings;
  return theInstance;
}

bool ViewerSettings::allowRotation() const
{
  return rotationAllowed;
}

void ViewerSettings::setAllowRotation(bool allow)
{
  rotationAllowed = allow;
}

RotationMode ViewerSettings::rotation() const
{
  return rot;
}

void ViewerSettings::setRotation(RotationMode r)
{
  rot = r;
}

void ViewerSettings::setCurrentBookmark(const Bookmark *bm)
{
  Bookmark *bmIt;
  
  currBookmarkIndex = -1;
  if(!bm)
  {
    // if it's not a real bookmark we default to -1
    // which is the Documents tab
    return;
  }

  for(bmIt = bmList.first() ; bmIt; bmIt = bmList.next())
  {
    if(*bmIt == *bm)
      currBookmarkIndex = bmList.at();
  }

  if(currBookmarkIndex == -1)
  {
    fprintf(stderr, "setCurrentBookmark(): Not found in list\n");
  }
}

Bookmark *ViewerSettings::currentBookmark()
{
  if(currBookmarkIndex == -1)
    return NULL;

  return bmList.at(currBookmarkIndex);
}

QList<Bookmark>& ViewerSettings::bookmarks()
{
  return bmList;
}

void ViewerSettings::load()
{
  int numBookmarks;
  Bookmark *bm;
  QString nameTag("name%1");
  QString dirTag("dir%1");
  QString storageTag("storage%1");

  Config config("MooView");

  config.setGroup("Settings");
  rotationAllowed = config.readBoolEntry("allowRotation", false);
  rot = (RotationMode)config.readNumEntry("rotation", ROTATION_90);

  config.setGroup("Bookmarks");
  bmList.clear();
  numBookmarks = config.readNumEntry("numBookmarks", 0);
  currBookmarkIndex = config.readNumEntry("currBookmark", -1);
  for(int i=0; i<numBookmarks; i++)
  {
    bm = new Bookmark;
    bm->setName(config.readEntry(nameTag.arg(i)));
    bm->setDirectory(config.readEntry(dirTag.arg(i)));
    bm->setStorage((enum Bookmark::StorageType)
                   config.readNumEntry(storageTag.arg(i)));
    bmList.append(bm);
  }
}

void ViewerSettings::save()
{
  Config config("MooView");
  Bookmark *bm;
  QString nameTag("name%1");
  QString dirTag("dir%1");
  QString storageTag("storage%1");

  config.setGroup("Settings");
  config.writeEntry("allowRotation", rotationAllowed);
  config.writeEntry("rotation", rot);

  config.setGroup("Bookmarks");
  config.writeEntry("numBookmarks", (int)bmList.count());
  config.writeEntry("currBookmark", (int)currBookmarkIndex);
  QListIterator<Bookmark> bmIt(bmList);
  for(int i=0 ; bmIt.current(); ++bmIt, i++)
  {
    config.writeEntry(nameTag.arg(i), (*bmIt)->name());
    config.writeEntry(dirTag.arg(i), (*bmIt)->directory());
    config.writeEntry(storageTag.arg(i), (int)(*bmIt)->storage());
  }
}
