/*
 * Copyright (C) 2013-2014 Jolla Ltd. <andrew.den.exter@jollamobile.com>
 * Author: Philip Van Hoof <philip@codeminded.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

#include "config.h"

#include <libtracker-common/tracker-common.h>
#include <libtracker-extract/tracker-extract.h>

static gboolean
get_img_resolution (const GFile *file,
                    gint64 *width,
                    gint64 *height)
{
    GFileInputStream *stream;
    GError *error = NULL;
    char   bfType[2];

    *width = 0;
    *height = 0;

    stream = g_file_read (file, NULL, &error);
    if (error) {
        g_debug ("Could not read bmp file : %s", error->message);
        g_error_free (error);
        return FALSE;
    }

    if (!g_input_stream_read (G_INPUT_STREAM (stream), bfType, 2, NULL, &error))
    {
        g_debug ("Error skip bmp file header from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    if(bfType[0] != 'B' || bfType[1] != 'M')
    {
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    /*skip some useless bytes*/
    if (!g_input_stream_skip(G_INPUT_STREAM (stream), 16, NULL, &error))
    {
        g_debug ("Error skip bmp file header from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    if (!g_input_stream_read (G_INPUT_STREAM (stream), width, sizeof(uint), NULL, &error))
    {
        g_debug ("Error read bmp file width from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    if (!g_input_stream_read (G_INPUT_STREAM (stream), height, sizeof(uint), NULL, &error))
    {
        g_debug ("Error read bmp file height from stream: '%s'", error->message);
        g_error_free (error);
        g_object_unref(stream);
        return FALSE;
    }

    g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
    g_object_unref(stream);
    return TRUE;
}

G_MODULE_EXPORT gboolean
tracker_extract_get_metadata (TrackerExtractInfo *info)
{
	TrackerSparqlBuilder *metadata;
	goffset size;
	gchar *filename;
	GFile *file;
    gint64 bmpwidth = 0, bmpheight = 0;

	metadata = tracker_extract_info_get_metadata_builder (info);

	file = tracker_extract_info_get_file (info);
	filename = g_file_get_path (file);
	size = tracker_file_get_size (filename);

	if (size < 14) {
		/* Smaller than BMP header, can't be a real BMP file */
		g_free (filename);
        g_object_unref(file);
		return FALSE;
	}

	tracker_sparql_builder_predicate (metadata, "a");
	tracker_sparql_builder_object (metadata, "nfo:Image");
	tracker_sparql_builder_object (metadata, "nmm:Photo");

    if (get_img_resolution (file, &bmpwidth, &bmpheight)) {
        if (bmpwidth > 0) {
            tracker_sparql_builder_predicate (metadata, "nfo:width");
            tracker_sparql_builder_object_int64 (metadata, bmpwidth);
        }
        if (bmpheight > 0) {
            tracker_sparql_builder_predicate (metadata, "nfo:height");
            tracker_sparql_builder_object_int64 (metadata, bmpheight);
        }
    }

	g_free (filename);
    g_object_unref(file);

	return TRUE;
}
