//  BMP
//  Copyright (C) 2005 BMP development.
//
//  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.
//
//  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.
//
//  --
//
//  The BMPx project hereby grants permission for non-GPL compatible GStreamer
//  plugins to be used and distributed together with GStreamer and BMPx. This
//  permission is above and beyond the permissions granted by the GPL license
//  BMPx is covered by.

#include <glibmm.h>

// BMP Audio
#include "src/audio/audio.hh"

// BMP Types
#include "bmp/types/types-library.hh"

// BMP Misc
#include "src/util-string.hh"
#include "src/util-file.hh"
#include "src/uri.hh"
#include "src/vfs.hh"
#include "src/database.hh"

using namespace Glib;


namespace Bmp
{
  namespace VFS
  {
      class PluginContainerFolder
          : public Bmp::VFS::PluginContainerBase
      {
        public:

          virtual bool
          can_process (ustring const& uri)
          {
            try{
                Bmp::URI u (uri);
                if (u.get_protocol () != Bmp::URI::PROTOCOL_FILE) return false;
                u.unescape ();
                ustring uri ((u));
                std::string path = filename_from_uri (uri);
                return file_test (path, FILE_TEST_IS_DIR);
              }
            catch (...) { return false; }
            return false;
          }

          virtual void 
          handle_read	(Handle & handle, VUri & list) throw (ProcessingError)
          {
            Bmp::URI u (handle.get_uri());
            u.unescape ();
            ustring const& uri ((u));
            try{
                Util::FileList file_list;
                Util::collect_audio_paths (filename_from_uri (uri), file_list);

                for (Util::FileList::const_iterator i = file_list.begin (); i != file_list.end (); ++i)
                { 
                  if (::Bmp::Audio::is_audio_file ((i.operator*())))
                    {
                      list.push_back (filename_to_uri (i.operator*()));
                    }
                }
              }
            catch (...) {}
          }

          virtual bool
          can_write ()
          {
            return false;
          }

          virtual void
          handle_write  (Handle &	handle, TrackV const& list)
            throw (ProcessingError)
          {
          }

          virtual Bmp::VFS::ExportData const&
          get_export_data ()
          {
            static Bmp::VFS::ExportData export_data ("Folder Container Plugin", ""); 
            return export_data;
          }
      };
  }
}
  
extern "C" Bmp::VFS::PluginContainerBase* plugin_create ()
{
  return new Bmp::VFS::PluginContainerFolder;
}

extern "C" void plugin_delete (Bmp::VFS::PluginContainerFolder* p)
{
  delete p;
}
