fix compiler warning
[ladish.git] / daemon / recent_projects.c
blob89cfae6930c4e0a8b239013fbade1b355b3ff362
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file implements the recent project functionality
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "recent_projects.h"
28 #include "recent_store.h"
29 #include "../common/catdup.h"
30 #include "../dbus_constants.h"
31 #include "../dbus/error.h"
32 #include "room.h"
34 #define RECENT_PROJECTS_STORE_FILE "recent_projects"
35 #define RECENT_PROJECTS_STORE_MAX_ITEMS 50
37 static ladish_recent_store_handle g_recent_projects_store;
39 bool ladish_recent_projects_init(void)
41 char * path;
42 bool ret;
44 path = catdup(g_base_dir, "/" RECENT_PROJECTS_STORE_FILE);
45 if (path == NULL)
47 log_error("catdup() failed to compose recent projects store file path");
48 return false;
51 ret = ladish_recent_store_create(path, RECENT_PROJECTS_STORE_MAX_ITEMS, &g_recent_projects_store);
52 if (!ret)
54 log_error("creation of recent projects store failed");
57 free(path);
59 return ret;
62 void ladish_recent_projects_uninit(void)
64 ladish_recent_store_destroy(g_recent_projects_store);
67 void ladish_recent_project_use(const char * project_path)
69 ladish_recent_store_use_item(g_recent_projects_store, project_path);
72 /**********************************************************************************/
73 /* D-Bus methods */
74 /**********************************************************************************/
76 struct recent_projects_callback_context
78 DBusMessageIter array_iter;
79 uint16_t max_items;
80 bool error;
83 #define ctx_ptr ((struct recent_projects_callback_context *)callback_context)
85 bool recent_projects_callback(void * callback_context, const char * project_path)
87 DBusMessageIter struct_iter;
88 DBusMessageIter dict_iter;
89 char * name;
91 ASSERT(ctx_ptr->max_items > 0);
93 name = ladish_get_project_name(project_path);
95 if (!dbus_message_iter_open_container(&ctx_ptr->array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
97 ctx_ptr->error = true;
98 goto exit;
101 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &project_path))
103 ctx_ptr->error = true;
104 goto close_struct;
107 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
109 ctx_ptr->error = true;
110 goto close_struct;
113 if (!dbus_maybe_add_dict_entry_string(&dict_iter, "name", name))
115 ctx_ptr->error = true;
116 goto close_dict;
119 close_dict:
120 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
122 ctx_ptr->error = true;
125 close_struct:
126 if (!dbus_message_iter_close_container(&ctx_ptr->array_iter, &struct_iter))
128 ctx_ptr->error = true;
131 exit:
132 free(name); /* safe if name is NULL */
134 if (ctx_ptr->error)
136 return false; /* stop the iteration if error occurs */
139 ctx_ptr->max_items--;
141 /* stop iteration if the requested max is reached */
142 return ctx_ptr->max_items > 0;
145 #undef ctx_ptr
147 static void get(struct dbus_method_call * call_ptr)
149 DBusMessageIter iter;
150 struct recent_projects_callback_context ctx;
152 if (!dbus_message_get_args(call_ptr->message, &cdbus_g_dbus_error, DBUS_TYPE_UINT16, &ctx.max_items, DBUS_TYPE_INVALID))
154 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, cdbus_g_dbus_error.message);
155 dbus_error_free(&cdbus_g_dbus_error);
156 return;
159 if (ctx.max_items == 0)
161 lash_dbus_error(
162 call_ptr,
163 LASH_DBUS_ERROR_INVALID_ARGS,
164 "Invalid arguments to method \"%s\": max cannot be 0",
165 call_ptr->method_name);
168 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
169 if (call_ptr->reply == NULL)
171 goto fail;
174 dbus_message_iter_init_append(call_ptr->reply, &iter);
176 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &ctx.array_iter))
178 goto fail_unref;
181 ctx.error = false;
182 ladish_recent_store_iterate_items(g_recent_projects_store, &ctx, recent_projects_callback);
183 if (ctx.error)
185 goto fail_unref;
188 if (!dbus_message_iter_close_container(&iter, &ctx.array_iter))
190 goto fail_unref;
193 return;
195 fail_unref:
196 dbus_message_unref(call_ptr->reply);
197 call_ptr->reply = NULL;
199 fail:
200 log_error("Ran out of memory trying to construct method return");
203 METHOD_ARGS_BEGIN(get, "Get list of recent items")
204 METHOD_ARG_DESCRIBE_IN("max", "q", "Max number of items caller is interested in")
205 METHOD_ARG_DESCRIBE_OUT("items", "a(sa{sv})", "Item list")
206 METHOD_ARGS_END
208 METHODS_BEGIN
209 METHOD_DESCRIBE(get, get)
210 METHODS_END
212 INTERFACE_BEGIN(g_iface_recent_items, IFACE_RECENT_ITEMS)
213 INTERFACE_DEFAULT_HANDLER
214 INTERFACE_EXPOSE_METHODS
215 INTERFACE_END