1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 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"
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)
43 path
= catdup(g_base_dir
, "/" RECENT_PROJECTS_STORE_FILE
);
46 log_error("catdup() failed to compose recent projects store file path");
50 ret
= ladish_recent_store_create(path
, RECENT_PROJECTS_STORE_MAX_ITEMS
, &g_recent_projects_store
);
53 log_error("creation of recent projects store failed");
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 /**********************************************************************************/
73 /**********************************************************************************/
75 struct recent_projects_callback_context
77 DBusMessageIter array_iter
;
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
;
90 ASSERT(ctx_ptr
->max_items
> 0);
92 if (!dbus_message_iter_open_container(&ctx_ptr
->array_iter
, DBUS_TYPE_STRUCT
, NULL
, &struct_iter
))
94 ctx_ptr
->error
= true;
98 if (!dbus_message_iter_append_basic(&struct_iter
, DBUS_TYPE_STRING
, &project_path
))
100 ctx_ptr
->error
= true;
104 if (!dbus_message_iter_open_container(&struct_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
))
106 ctx_ptr
->error
= true;
110 /* if (!dbus_add_dict_entry_uint32(&dict_iter, "name", project_name)) */
112 /* ctx_ptr->error = true; */
113 /* goto close_dict; */
119 if (!dbus_message_iter_close_container(&struct_iter
, &dict_iter
))
121 ctx_ptr
->error
= true;
125 if (!dbus_message_iter_close_container(&ctx_ptr
->array_iter
, &struct_iter
))
127 ctx_ptr
->error
= true;
133 return false; /* stop the iteration if error occurs */
136 ctx_ptr
->max_items
--;
138 /* stop iteration if the requested max is reached */
139 return ctx_ptr
->max_items
> 0;
144 static void get(struct dbus_method_call
* call_ptr
)
146 DBusMessageIter iter
;
147 struct recent_projects_callback_context ctx
;
149 if (!dbus_message_get_args(call_ptr
->message
, &g_dbus_error
, DBUS_TYPE_UINT16
, &ctx
.max_items
, DBUS_TYPE_INVALID
))
151 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
152 dbus_error_free(&g_dbus_error
);
156 if (ctx
.max_items
== 0)
160 LASH_DBUS_ERROR_INVALID_ARGS
,
161 "Invalid arguments to method \"%s\": max cannot be 0",
162 call_ptr
->method_name
);
165 call_ptr
->reply
= dbus_message_new_method_return(call_ptr
->message
);
166 if (call_ptr
->reply
== NULL
)
171 dbus_message_iter_init_append(call_ptr
->reply
, &iter
);
173 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(sa{sv})", &ctx
.array_iter
))
179 ladish_recent_store_iterate_items(g_recent_projects_store
, &ctx
, recent_projects_callback
);
185 if (!dbus_message_iter_close_container(&iter
, &ctx
.array_iter
))
193 dbus_message_unref(call_ptr
->reply
);
194 call_ptr
->reply
= NULL
;
197 log_error("Ran out of memory trying to construct method return");
200 METHOD_ARGS_BEGIN(get
, "Get list of recent items")
201 METHOD_ARG_DESCRIBE_IN("max", "q", "Max number of items caller is interested in")
202 METHOD_ARG_DESCRIBE_OUT("items", "a(sa{sv})", "Item list")
206 METHOD_DESCRIBE(get
, get
)
209 INTERFACE_BEGIN(g_iface_recent_items
, IFACE_RECENT_ITEMS
)
210 INTERFACE_DEFAULT_HANDLER
211 INTERFACE_EXPOSE_METHODS