/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2009-2011 Jannis Pohlmann <jannis@xfce.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <tumbler/tumbler-cache.h>
#include <tumbler/tumbler-cache-plugin.h>



G_DEFINE_INTERFACE(TumblerCache, tumbler_cache, G_TYPE_OBJECT)



static void
tumbler_cache_default_init (TumblerCacheIface *iface)
{
}



TumblerCache *
tumbler_cache_get_default (void)
{
  static TumblerCache *cache = NULL;
  GTypeModule         *plugin;

  if (cache == NULL)
    {
      plugin = tumbler_cache_plugin_get_default ();

      if (plugin != NULL)
        {
          cache = tumbler_cache_plugin_get_cache (TUMBLER_CACHE_PLUGIN (plugin));
          g_object_add_weak_pointer (G_OBJECT (cache), (gpointer) &cache);
          g_type_module_unuse (plugin);
        }
    }
  else
    {
      g_object_ref (cache);
    }
     
  return cache;
}



TumblerThumbnail *
tumbler_cache_get_thumbnail (TumblerCache           *cache,
                             const gchar            *uri,
                             TumblerThumbnailFlavor *flavor)
{
  g_return_val_if_fail (TUMBLER_IS_CACHE (cache), NULL);
  g_return_val_if_fail (uri != NULL && *uri != '\0', NULL);
  g_return_val_if_fail (flavor == NULL || TUMBLER_IS_THUMBNAIL_FLAVOR (flavor), NULL);
  g_return_val_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->get_thumbnail != NULL, NULL);

  if (flavor == NULL)
    return NULL;

  return (TUMBLER_CACHE_GET_IFACE (cache)->get_thumbnail) (cache, uri, flavor);
}



void
tumbler_cache_cleanup (TumblerCache       *cache,
                       const gchar *const *base_uris,
                       gdouble             since)
{
  g_return_if_fail (TUMBLER_IS_CACHE (cache));
  g_return_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->cleanup != NULL);

  (TUMBLER_CACHE_GET_IFACE (cache)->cleanup) (cache, base_uris, since);
}



void
tumbler_cache_delete (TumblerCache       *cache,
                      const gchar *const *uris)
{
  g_return_if_fail (TUMBLER_IS_CACHE (cache));
  g_return_if_fail (uris != NULL);
  g_return_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->do_delete != NULL);

  (TUMBLER_CACHE_GET_IFACE (cache)->do_delete) (cache, uris);
}



void
tumbler_cache_copy (TumblerCache       *cache,
                    const gchar *const *from_uris,
                    const gchar *const *to_uris)
{
  g_return_if_fail (TUMBLER_IS_CACHE (cache));
  g_return_if_fail (from_uris != NULL);
  g_return_if_fail (to_uris != NULL);
  g_return_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->copy != NULL);

  (TUMBLER_CACHE_GET_IFACE (cache)->copy) (cache, from_uris, to_uris);
}



void
tumbler_cache_move (TumblerCache       *cache,
                    const gchar *const *from_uris,
                    const gchar *const *to_uris)
{
  g_return_if_fail (TUMBLER_IS_CACHE (cache));
  g_return_if_fail (from_uris != NULL);
  g_return_if_fail (to_uris != NULL);
  g_return_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->move != NULL);

  (TUMBLER_CACHE_GET_IFACE (cache)->move) (cache, from_uris, to_uris);
}



gboolean
tumbler_cache_is_thumbnail (TumblerCache *cache,
                            const gchar  *uri)
{
  g_return_val_if_fail (TUMBLER_IS_CACHE (cache), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);
  g_return_val_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->is_thumbnail != NULL, FALSE);

  return (TUMBLER_CACHE_GET_IFACE (cache)->is_thumbnail) (cache, uri);
}



GList *
tumbler_cache_get_flavors (TumblerCache *cache)
{
  g_return_val_if_fail (TUMBLER_IS_CACHE (cache), NULL);
  g_return_val_if_fail (TUMBLER_CACHE_GET_IFACE (cache)->get_flavors != NULL, NULL);

  return (TUMBLER_CACHE_GET_IFACE (cache)->get_flavors) (cache);
}



TumblerThumbnailFlavor *
tumbler_cache_get_flavor (TumblerCache *cache,
                          const gchar  *name)
{
  TumblerThumbnailFlavor *flavor = NULL;
  GList                  *flavors;
  GList                  *iter;

  g_return_val_if_fail (TUMBLER_IS_CACHE (cache), NULL);
  g_return_val_if_fail (name != NULL && *name != '\0', NULL);

  flavors = tumbler_cache_get_flavors (cache);

  for (iter = flavors; flavor == NULL && iter != NULL; iter = iter->next)
    if (g_strcmp0 (tumbler_thumbnail_flavor_get_name (iter->data), name) == 0)
      flavor = g_object_ref (iter->data);

  g_list_free_full (flavors, g_object_unref);

  return flavor;
}
