ladishd: return project name in recent project list

This commit is contained in:
Nedko Arnaudov 2010-10-09 17:37:57 +03:00
parent 78e99c4d64
commit 3ac3105fdb
4 changed files with 118 additions and 9 deletions

View File

@ -71,6 +71,7 @@ struct ladish_parse_context
bool terminal;
bool autorun;
uint8_t level;
void * parser;
};
void ladish_dump_element_stack(struct ladish_parse_context * context_ptr);

View File

@ -29,6 +29,7 @@
#include "../common/catdup.h"
#include "../dbus_constants.h"
#include "../dbus/error.h"
#include "room.h"
#define RECENT_PROJECTS_STORE_FILE "recent_projects"
#define RECENT_PROJECTS_STORE_MAX_ITEMS 50
@ -85,10 +86,12 @@ bool recent_projects_callback(void * callback_context, const char * project_path
{
DBusMessageIter struct_iter;
DBusMessageIter dict_iter;
bool ret;
char * name;
ASSERT(ctx_ptr->max_items > 0);
name = ladish_get_project_name(project_path);
if (!dbus_message_iter_open_container(&ctx_ptr->array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
{
ctx_ptr->error = true;
@ -107,15 +110,13 @@ bool recent_projects_callback(void * callback_context, const char * project_path
goto close_struct;
}
/* if (!dbus_add_dict_entry_uint32(&dict_iter, "name", project_name)) */
/* { */
/* ctx_ptr->error = true; */
/* goto close_dict; */
/* } */
if (!dbus_maybe_add_dict_entry_string(&dict_iter, "name", name))
{
ctx_ptr->error = true;
goto close_dict;
}
ret = true;
/* close_dict: */
close_dict:
if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
{
ctx_ptr->error = true;
@ -128,6 +129,8 @@ close_struct:
}
exit:
free(name); /* safe if name is NULL */
if (ctx_ptr->error)
{
return false; /* stop the iteration if error occurs */

View File

@ -90,4 +90,6 @@ bool ladish_room_save_project(ladish_room_handle room_handle, const char * proje
bool ladish_room_unload_project(ladish_room_handle room_handle);
bool ladish_room_load_project(ladish_room_handle room_handle, const char * project_dir);
char * ladish_get_project_name(const char * project_dir);
#endif /* #ifndef ROOM_H__9A1CF253_0A17_402A_BDF8_9BD72B467118__INCLUDED */

View File

@ -760,3 +760,106 @@ exit:
}
#undef room_ptr
#define context_ptr ((struct ladish_parse_context *)data)
static void project_name_elstart_callback(void * data, const char * el, const char ** attr)
{
const char * name;
const char * uuid_str;
uuid_t uuid;
if (strcmp(el, "project") == 0)
{
if (ladish_get_name_and_uuid_attributes("/project", attr, &name, &uuid_str, uuid))
{
context_ptr->str = strdup(name);
if (context_ptr->str == NULL)
{
log_error("strdup() failed for project name");
}
}
XML_StopParser(context_ptr->parser, XML_TRUE);
return;
}
}
#undef context_ptr
char * ladish_get_project_name(const char * project_dir)
{
char * path;
struct stat st;
XML_Parser parser;
int bytes_read;
void * buffer;
int fd;
enum XML_Status xmls;
struct ladish_parse_context parse_context;
parse_context.str = NULL;
path = catdup(project_dir, LADISH_PROJECT_FILENAME);
if (path == NULL)
{
log_error("catdup() failed to compose xml file path");
goto exit;
}
if (stat(path, &st) != 0)
{
log_error("failed to stat '%s': %d (%s)", path, errno, strerror(errno));
goto free_path;
}
fd = open(path, O_RDONLY);
if (fd == -1)
{
log_error("failed to open '%s': %d (%s)", path, errno, strerror(errno));
goto free_path;
}
parser = XML_ParserCreate(NULL);
if (parser == NULL)
{
log_error("XML_ParserCreate() failed to create parser object.");
goto close;
}
/* we are expecting that conf file has small enough size to fit in memory */
buffer = XML_GetBuffer(parser, st.st_size);
if (buffer == NULL)
{
log_error("XML_GetBuffer() failed.");
goto free_parser;
}
bytes_read = read(fd, buffer, st.st_size);
if (bytes_read != st.st_size)
{
log_error("read() returned unexpected result.");
goto free_parser;
}
XML_SetElementHandler(parser, project_name_elstart_callback, NULL);
XML_SetUserData(parser, &parse_context);
parse_context.parser = parser;
xmls = XML_ParseBuffer(parser, bytes_read, XML_TRUE);
if (xmls == XML_STATUS_ERROR)
{
log_error("XML_ParseBuffer() failed.");
}
free_parser:
XML_ParserFree(parser);
close:
close(fd);
free_path:
free(path);
exit:
return parse_context.str;
}