/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Authors: Lauris Kaplinski <lauris@helixcode.com>
 *         
 */

#include <config.h>
#include <unicode.h>
#include "e-unicode.h"

const gchar *
e_utf8_strstrcase (const gchar *haystack, const gchar *needle)
{
	gchar *p;
	unicode_char_t *huni, *nuni;
	unicode_char_t unival;
	gint hlen, nlen, hp, np;

	if (haystack == NULL) return NULL;
	if (needle == NULL) return NULL;
	if (strlen (needle) == 0) return haystack;

	huni = alloca (sizeof (unicode_char_t) * strlen (haystack));

	for (hlen = 0, p = unicode_get_utf8 (haystack, &unival); p && unival; hlen++, p = unicode_get_utf8 (p, &unival)) {
		huni[hlen] = unicode_tolower (unival);
	}

	if (!p) return NULL;
	if (hlen == 0) return NULL;

	nuni = alloca (sizeof (unicode_char_t) * strlen (needle));

	for (nlen = 0, p = unicode_get_utf8 (needle, &unival); p && unival; nlen++, p = unicode_get_utf8 (p, &unival)) {
		nuni[nlen] = unicode_tolower (unival);
	}

	if (!p) return NULL;
	if (nlen == 0) return NULL;

	if (hlen < nlen) return NULL;

	for (hp = 0; hp <= hlen - nlen; hp++) {
		for (np = 0; np < nlen; np++) {
			if (huni[hp + np] != nuni[np]) break;
		}
		if (np == nlen) return haystack + unicode_offset_to_index (haystack, hp);
	}

	return NULL;
}

gchar *
e_utf8_from_gtk_event_key (GtkWidget *widget, guint keyval, const gchar *string)
{
	/* test it out with iso-8859-1 */

	static gboolean uinit = FALSE;
	static gboolean uerror = FALSE;
	static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
	char *new, *ob;
	size_t ibl, obl;

	if (uerror) return NULL;

	if (!string) return NULL;

	if (!uinit) {
		unicode_init ();
		uiconv = unicode_iconv_open ("UTF-8", "iso-8859-1");
		if (uiconv == (unicode_iconv_t) -1) {
			uerror = TRUE;
			return NULL;
		} else {
			uinit = TRUE;
		}
	}

	ibl = strlen (string);
	new = ob = g_new (gchar, ibl * 6 + 1);
	obl = ibl * 6 + 1;

	unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

	*ob = '\0';

	return new;
}

gchar *
e_utf8_from_gtk_string (GtkWidget *widget, const gchar *string)
{
	/* test it out with iso-8859-1 */

	static gboolean uinit = FALSE;
	static gboolean uerror = FALSE;
	static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
	char *new, *ob;
	size_t ibl, obl;

	if (uerror) return NULL;

	if (!string) return NULL;

	if (!uinit) {
		unicode_init ();
		uiconv = unicode_iconv_open ("UTF-8", "iso-8859-1");
		if (uiconv == (unicode_iconv_t) -1) {
			uerror = TRUE;
			return NULL;
		} else {
			uinit = TRUE;
		}
	}

	ibl = strlen (string);
	new = ob = g_new (gchar, ibl * 6 + 1);
	obl = ibl * 6 + 1;

	unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

	*ob = '\0';

	return new;
}

gchar *
e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string)
{
	/* test it out with iso-8859-1 */

	static gboolean uinit = FALSE;
	static gboolean uerror = FALSE;
	static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
	char *new, *ob;
	size_t ibl, obl;

	if (uerror) return NULL;

	if (!string) return NULL;

	if (!uinit) {
		unicode_init ();
		uiconv = unicode_iconv_open ("iso-8859-1", "UTF-8");
		if (uiconv == (unicode_iconv_t) -1) {
			uerror = TRUE;
			return NULL;
		} else {
			uinit = TRUE;
		}
	}

	ibl = strlen (string);
	new = ob = g_new (gchar, ibl * 2 + 1);
	obl = ibl * 2 + 1;

	unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

	*ob = '\0';

	return new;
}

gchar *
e_utf8_gtk_entry_get_text (GtkEntry *entry)
{
	gchar *s, *u;

	s = gtk_entry_get_text (entry);
	if (!s) return NULL;
	u = e_utf8_from_gtk_string ((GtkWidget *) entry, s);
	return u;
}

gchar *
e_utf8_gtk_editable_get_chars (GtkEditable *editable, gint start, gint end)
{
	gchar *s, *u;

	s = gtk_editable_get_chars (editable, start, end);
	if (!s) return NULL;
	u = e_utf8_from_gtk_string ((GtkWidget *) editable, s);
	g_free (s);
	return u;
}

void
e_utf8_gtk_entry_set_text (GtkEntry *entry, const gchar *text)
{
	gchar *s;

	if (!text) return;

	s = e_utf8_to_gtk_string ((GtkWidget *) entry, text);
	gtk_entry_set_text (entry, s);

	if (s) g_free (s);
}

GtkWidget *
e_utf8_gtk_menu_item_new_with_label (const gchar *label)
{
	GtkWidget *w;
	gchar *s;

	if (!label) return NULL;

	s = e_utf8_to_gtk_string (NULL, label);
	w = gtk_menu_item_new_with_label (s);

	if (s) g_free (s);

	return w;
}


er'>gstreamer</option>
<option value='gstreamer-1.16'>gstreamer-1.16</option>
<option value='gstreamer0.10-removal'>gstreamer0.10-removal</option>
<option value='main' selected='selected'>main</option>
<option value='master'>master</option>
<option value='mate-1.16'>mate-1.16</option>
<option value='mate-1.18'>mate-1.18</option>
<option value='mate-1.20'>mate-1.20</option>
<option value='mate-1.22'>mate-1.22</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD GNOME current development ports (https://github.com/freebsd/freebsd-ports-gnome)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/about/'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/refs/'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel/files/patch-redis.conf'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/tree/databases/redis-devel/files/patch-redis.conf'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/diff/databases/redis-devel/files/patch-redis.conf'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/stats/databases/redis-devel/files/patch-redis.conf'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel/files/patch-redis.conf'>
<select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases'>databases</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel'>redis-devel</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel/files'>files</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel/files/patch-redis.conf'>patch-redis.conf</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/redis-devel/files/patch-redis.conf?showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf?id=5eac3b8e580c38e04877e0206b407af917a78f5c'>Update to the recent development version 6.2.0rc2.</a></td><td>Sergey A. Osokin</td><td><span title='2021-01-14 01:31:22 +0800'>2021-01-14</span></td><td>1</td><td><span class='deletions'>-9</span>/<span class='insertions'>+9</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf?id=80b2ecc5f4e009d1bdd3671ac4d53ec41e2a4b65'>Update to the recent development version from the unstable branch.</a></td><td>Sergey A. Osokin</td><td><span title='2019-11-28 02:20:53 +0800'>2019-11-28</span></td><td>1</td><td><span class='deletions'>-5</span>/<span class='insertions'>+5</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf?id=41e56db75f1139b60b505a970726239ea732be5a'>Update from 5.0.5 to 5.0.6.</a></td><td>Sergey A. Osokin</td><td><span title='2019-11-06 05:38:26 +0800'>2019-11-06</span></td><td>1</td><td><span class='deletions'>-6</span>/<span class='insertions'>+6</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf?id=5bae44dae0b82bdfca7744c4499dcb24feda1032'>Upgrade from 3.2.0 to 3.2.1.</a></td><td>Sergey A. Osokin</td><td><span title='2016-06-19 00:03:17 +0800'>2016-06-19</span></td><td>1</td><td><span class='deletions'>-7</span>/<span class='insertions'>+7</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/redis-devel/files/patch-redis.conf?id=d503655b3566b8a7a412d1582bf44c99f41ff1a7'>Upgrade from 3.0.7 to latest 3.2.0-rc3.</a></td><td>Sergey A. Osokin</td><td><span title='2016-02-03 09:02:21 +0800'>2016-02-03</span></td><td>1</td><td><span class='deletions'>-11</span>/<span class='insertions'>+15</span></td></tr>