/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2004 Ximian, Inc. (www.ximian.com)
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


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

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include "e-folder-map.h"

#define d(x)

static gboolean
is_type_folder (const char *metadata, const char *search_type)
{
	xmlNodePtr node;
	xmlDocPtr doc;
	char *type;
	
	if (!(doc = xmlParseFile (metadata))) {
		g_warning ("Cannot parse `%s'", metadata);
		return FALSE;
	}
	
	if (!(node = xmlDocGetRootElement (doc))) {
		g_warning ("`%s' corrupt: document contains no root node", metadata);
		xmlFreeDoc (doc);
		return FALSE;
	}
	
	if (!node->name || strcmp (node->name, "efolder") != 0) {
		g_warning ("`%s' corrupt: root node is not 'efolder'", metadata);
		xmlFreeDoc (doc);
		return FALSE;
	}
	
	node = node->children;
	while (node != NULL) {
		if (node->name && !strcmp (node->name, "type")) {
			type = xmlNodeGetContent (node);
			if (!strcmp (type, search_type)) {
				xmlFreeDoc (doc);
				xmlFree (type);
				
				return TRUE;
			}
			
			xmlFree (type);
			
			break;
		}
		
		node = node->next;
	}
	
	xmlFreeDoc (doc);
	
	return FALSE;
}

static void
e_folder_map_dir (const char *dirname, const char *type, GSList **dir_list)
{
	char *path;
	const char *name;
	GDir *dir;
	GError *error = NULL;

	path = g_build_filename (dirname, "folder-metadata.xml", NULL);
	if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
		g_free (path);
		return;
	}
	
	if (!is_type_folder (path, type)) {
		g_free (path);
		goto try_subdirs;
	}

	d(g_message ("Found '%s'", dirname));
	*dir_list = g_slist_prepend (*dir_list, g_strdup (dirname));
	
	g_free (path);	

 try_subdirs:

	path = g_build_filename (dirname, "subfolders", NULL);
	if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		g_free (path);
		return;
	}
	
	if (!(dir = g_dir_open (path, 0, &error))) {
		g_warning ("cannot open `%s': %s", path, error->message);
		g_error_free (error);
		g_free (path);
		return;
	}
	
	while ((name = g_dir_read_name (dir))) {
		char *full_path;
		
		if (*name == '.')
			continue;
		
		full_path = g_build_filename (path, name, NULL);
		if (!g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
			g_free (full_path);
			continue;
		}
		
		e_folder_map_dir (full_path, type, dir_list);
		g_free (full_path);
	}
	
	g_dir_close (dir);
	
	g_free (path);
}

GSList *
e_folder_map_local_folders (char *local_dir, char *type)
{
	const char *name;
	GDir *dir;	
	GSList *dir_list = NULL;
	GError *error = NULL;
	
	if (!(dir = g_dir_open (local_dir, 0, &error))) {
		g_warning ("cannot open `%s': %s", local_dir, error->message);
		g_error_free (error);
		return NULL;
	}
	
	while ((name = g_dir_read_name (dir))) {
		char *full_path;
		
		if (*name == '.')
			continue;
		
		full_path = g_build_filename (local_dir, name, NULL);
		d(g_message ("Looking in %s", full_path));
		if (!g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
			g_free (full_path);
			continue;
		}
		
		e_folder_map_dir (full_path, type, &dir_list);

		g_free (full_path);
	}
	
	g_dir_close (dir);

	return dir_list;
}
>
<option value='gnome-3.22'>gnome-3.22</option>
<option value='gnome-3.24' selected='selected'>gnome-3.24</option>
<option value='gnome-3.26'>gnome-3.26</option>
<option value='gnome-3.28'>gnome-3.28</option>
<option value='gnome-3.32'>gnome-3.32</option>
<option value='gnome-3.36'>gnome-3.36</option>
<option value='gstreamer'>gstreamer</option>
<option value='gstreamer-1.16'>gstreamer-1.16</option>
<option value='gstreamer0.10-removal'>gstreamer0.10-removal</option>
<option value='main'>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/?h=gnome-3.24'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/?h=gnome-3.24'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/refs/?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/math/ploticus?h=gnome-3.24'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/tree/math/ploticus?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/math/ploticus?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/diff/math/ploticus?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/stats/math/ploticus?h=gnome-3.24'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/math/ploticus'>
<input type='hidden' name='h' value='gnome-3.24'/><input type='hidden' name='id' value='a84ffad35d3058a7a58bc2edfa922757debb0384'/><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/?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/math?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>math</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/math/ploticus?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384'>ploticus</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/math/ploticus?h=gnome-3.24&amp;id=a84ffad35d3058a7a58bc2edfa922757debb0384&amp;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/math/ploticus?h=gnome-3.24&amp;id=dd97722379d86bbad461eb7c042a07cd39ba24f9'>Add NO_STAGE all over the place in preparation for the staging support (cat: ...</a></td><td>bapt</td><td><span title='2013-09-21 04:55:04 +0800'>2013-09-21</span></td><td>1</td><td><span class='deletions'>-6</span>/<span class='insertions'>+2</span></td></tr>