Merge branch 'stable' into 'main'
[ladish.git] / daemon / recent_projects.c
blob3f4cb41d04d153aade3eaed43027e0083e4be2a8
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 "room.h"
33 #define RECENT_PROJECTS_STORE_FILE "recent_projects"
34 #define RECENT_PROJECTS_STORE_MAX_ITEMS 50
36 static ladish_recent_store_handle g_recent_projects_store;
38 bool ladish_recent_projects_init(void)
40 char * path;
41 bool ret;
43 path = catdup(g_base_dir, "/" RECENT_PROJECTS_STORE_FILE);
44 if (path == NULL)
46 log_error("catdup() failed to compose recent projects store file path");
47 return false;
50 ret = ladish_recent_store_create(path, RECENT_PROJECTS_STORE_MAX_ITEMS, &g_recent_projects_store);
51 if (!ret)
53 log_error("creation of recent projects store failed");
56 free(path);
58 return ret;
61 void ladish_recent_projects_uninit(void)
63 ladish_recent_store_destroy(g_recent_projects_store);
66 void ladish_recent_project_use(const char * project_path)
68 ladish_recent_store_use_item(g_recent_projects_store, project_path);
71 /**********************************************************************************/
72 /* D-Bus methods */
73 /**********************************************************************************/
75 struct recent_projects_callback_context
77 DBusMessageIter array_iter;
78 uint16_t max_items;
79 bool error;
82 #define ctx_ptr ((struct recent_projects_callback_context *)callback_context)
84 bool recent_projects_callback(void * callback_context, const char * project_path)
86 DBusMessageIter struct_iter;
87 DBusMessageIter dict_iter;
88 char * name;
90 ASSERT(ctx_ptr->max_items > 0);
92 name = ladish_get_project_name(project_path);
94 if (!dbus_message_iter_open_container(&ctx_ptr->array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
96 ctx_ptr->error = true;
97 goto exit;
100 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &project_path))
102 ctx_ptr->error = true;
103 goto close_struct;
106 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
108 ctx_ptr->error = true;
109 goto close_struct;
112 if (!cdbus_maybe_add_dict_entry_string(&dict_iter, "name", name))
114 ctx_ptr->error = true;
115 goto close_dict;
118 close_dict:
119 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
121 ctx_ptr->error = true;
124 close_struct:
125 if (!dbus_message_iter_close_container(&ctx_ptr->array_iter, &struct_iter))
127 ctx_ptr->error = true;
130 exit:
131 free(name); /* safe if name is NULL */
133 if (ctx_ptr->error)
135 return false; /* stop the iteration if error occurs */
138 ctx_ptr->max_items--;
140 /* stop iteration if the requested max is reached */
141 return ctx_ptr->max_items > 0;
144 #undef ctx_ptr
146 static void get(struct cdbus_method_call * call_ptr)
148 DBusMessageIter iter;
149 struct recent_projects_callback_context ctx;
151 if (!dbus_message_get_args(call_ptr->message, &cdbus_g_dbus_error, DBUS_TYPE_UINT16, &ctx.max_items, DBUS_TYPE_INVALID))
153 cdbus_error(call_ptr, DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, cdbus_g_dbus_error.message);
154 dbus_error_free(&cdbus_g_dbus_error);
155 return;
158 if (ctx.max_items == 0)
160 cdbus_error(
161 call_ptr,
162 DBUS_ERROR_INVALID_ARGS,
163 "Invalid arguments to method \"%s\": max cannot be 0",
164 call_ptr->method_name);
167 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
168 if (call_ptr->reply == NULL)
170 goto fail;
173 dbus_message_iter_init_append(call_ptr->reply, &iter);
175 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &ctx.array_iter))
177 goto fail_unref;
180 ctx.error = false;
181 ladish_recent_store_iterate_items(g_recent_projects_store, &ctx, recent_projects_callback);
182 if (ctx.error)
184 goto fail_unref;
187 if (!dbus_message_iter_close_container(&iter, &ctx.array_iter))
189 goto fail_unref;
192 return;
194 fail_unref:
195 dbus_message_unref(call_ptr->reply);
196 call_ptr->reply = NULL;
198 fail:
199 log_error("Ran out of memory trying to construct method return");
202 CDBUS_METHOD_ARGS_BEGIN(get, "Get list of recent items")
203 CDBUS_METHOD_ARG_DESCRIBE_IN("max", "q", "Max number of items caller is interested in")
204 CDBUS_METHOD_ARG_DESCRIBE_OUT("items", "a(sa{sv})", "Item list")
205 CDBUS_METHOD_ARGS_END
207 CDBUS_METHODS_BEGIN
208 CDBUS_METHOD_DESCRIBE(get, get)
209 CDBUS_METHODS_END
211 CDBUS_INTERFACE_DEFAULT_HANDLER_METHODS_ONLY(g_iface_recent_items, IFACE_RECENT_ITEMS)