/*
    ragmaan/sort.c            (C) 2003 Raymond (zandbergen@home.nl)

    "ragmaan" is an anagram generator

    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
*/

#include <glib.h>
#include <gtk/gtk.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ragmaan.h"
#include "sort.h"
gint
sort_sub (GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b,
	  gpointer unused)
{
  gchar *string_a, *string_b;
  gint result;
  gtk_tree_model_get (model, a, COLUMN_SUBWORD, &string_a, -1);
  gtk_tree_model_get (model, b, COLUMN_SUBWORD, &string_b, -1);
  result = g_ascii_strcasecmp (string_a, string_b);
  g_free (string_a);
  g_free (string_b);
  return result;
}

gint
sort_rest (GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b,
	   gpointer unused)
{
  gchar *string_a, *string_b;
  gint len_a, len_b;
  gint result;
  /* this looks expensive to me */
  gtk_tree_model_get (model, a, COLUMN_REST, &string_a, -1);
  gtk_tree_model_get (model, b, COLUMN_REST, &string_b, -1);
  len_a = strlen (string_a);
  len_b = strlen (string_b);
  g_free (string_a);
  g_free (string_b);
  if (len_a == len_b)
    result = sort_sub (model, a, b, NULL);
  else
    result = CLAMP (len_a - len_b, -1, 1);
  return result;
}

gint
sort_status (GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b,
	     gpointer unused)
{
  gint status_a, status_b;
  gint result;
  gtk_tree_model_get (model, a, COLUMN_STATUS, &status_a, -1);
  gtk_tree_model_get (model, b, COLUMN_STATUS, &status_b, -1);
  if (status_a == status_b)
    result = sort_sub (model, a, b, NULL);
  else
    result = CLAMP (status_a - status_b, -1, 1);
  return result;
}
