/*------------------------------------------------------------------------------
 src/lib/djWidget/imageIoWidget.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djImageIoWidget.h"
#include <dlfcn.h>

namespace dj {

ImageLoadWidget::ImageLoadWidget() :
  _load(0)
{}

ImageLoadWidget::~ImageLoadWidget() {}

void ImageLoadWidget::set(ImageLoad * in)
{
  _load = in;
}

ImageSaveWidget::ImageSaveWidget() :
  _save(0)
{}

ImageSaveWidget::~ImageSaveWidget() {}

void ImageSaveWidget::set(ImageSave * in)
{
  _save = in;
}

ImageLoadWidgetFactory::~ImageLoadWidgetFactory()
{
  del();
}

ImageLoadWidget * ImageLoadWidgetFactory::pluginCopy(const Str & in)
{
  for (ListIt<ImageLoadWidget *> it = _pluginList; it.isValid(); ++it)
    if (lowercase(in) == lowercase(it()->name()))
      return static_cast<ImageLoadWidget *>(it()->copy());
  return 0;
}

void ImageLoadWidgetFactory::pluginAdd(const List<ImageLoadWidget *> & in)
{
  _pluginList += in;
}

bool ImageLoadWidgetFactory::init(void * in)
{
  ImageLoadWidgetFnc * fnc =
    (ImageLoadWidgetFnc *)dlsym(in, "djImageLoadWidget");
  if (! fnc) return false;
  _pluginList += fnc();
  return true;
}

void ImageLoadWidgetFactory::del()
{
  for (ListIt<ImageLoadWidget *> it = _pluginList; it.isValid(); ++it)
    delete it();
  _pluginList.del();
  PluginFactory::del();
}

ImageSaveWidgetFactory::~ImageSaveWidgetFactory()
{
  del();
}

ImageSaveWidget * ImageSaveWidgetFactory::pluginCopy(const Str & in)
{
  for (ListIt<ImageSaveWidget *> it = _pluginList; it.isValid(); ++it)
    if (lowercase(in) == lowercase(it()->name()))
      return static_cast<ImageSaveWidget *>(it()->copy());
  return 0;
}

void ImageSaveWidgetFactory::pluginAdd(const List<ImageSaveWidget *> & in)
{
  _pluginList += in;
}

bool ImageSaveWidgetFactory::init(void * in)
{
  ImageSaveWidgetFnc * fnc =
    (ImageSaveWidgetFnc *)dlsym(in, "djImageSaveWidget");
  if (! fnc) return false;
  _pluginList += fnc();
  return true;
}

void ImageSaveWidgetFactory::del()
{
  for (ListIt<ImageSaveWidget *> it = _pluginList; it.isValid(); ++it)
    delete it();
  _pluginList.del();
  PluginFactory::del();
}

}
