/*
 * ImageSet.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 "ImageSet.h"
#include "ImageInfo.h"

#include <qlist.h>
#include <qfile.h>
#include <qpe/global.h>
#include <qpe/applnk.h>

ImageSet::ImageSet(const QString& dir, QObject *parent, const char *name)
: QObject(parent, name)
{
  setBaseDirectory(dir);
  imageList.setAutoDelete(true);
}

ImageSet::~ImageSet()
{
}

void ImageSet::setBaseDirectory(const QString& dir)
{
  DocLnkSet *dSet;

  theDir = dir;
  if(dir.isEmpty() || dir.isNull())
  {
    dSet = new DocLnkSet();
    Global::findDocuments(dSet, "image/jpeg");
  }
  else
  {
    dSet = new DocLnkSet(dir, "image/jpeg");
  }

  imageList.clear();
  addImages(dSet);
  delete dSet;

  emit changed();
}

void ImageSet::update()
{
  if(!theDir.isEmpty() && !theDir.isNull() && !QFile::exists(theDir))
  {
    // handle case when a memory card is removed
    imageList.clear();
    emit changed();
  }
  else
  {
    setBaseDirectory(theDir);
  }
}

void ImageSet::addImages(DocLnkSet *dls)
{
  QListIterator<DocLnk> dit(dls->children());
  ImageInfo *inf;

  for( ; dit.current(); ++dit)
  {
    inf = new ImageInfo((*dit)->file());
    imageList.append(inf);
  }
  imageList.sort();
}

QList<ImageInfo>& ImageSet::children()
{
  return imageList;
}
