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"
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)
44 path
= catdup(g_base_dir
, "/" RECENT_PROJECTS_STORE_FILE
);
47 log_error("catdup() failed to compose recent projects store file path");
51 ret
= ladish_recent_store_create(path
, RECENT_PROJECTS_STORE_MAX_ITEMS
, &g_recent_projects_store
);
54 log_error("creation of recent projects store failed");
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 /**********************************************************************************/
74 /**********************************************************************************/
76 struct recent_projects_callback_context
78 DBusMessageIter array_iter
;
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
;
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;
101 if (!dbus_message_iter_append_basic(&struct_iter
, DBUS_TYPE_STRING
, &project_path
))
103 ctx_ptr
->error
= true;
107 if (!dbus_message_iter_open_container(&struct_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
))
109 ctx_ptr
->error
= true;
113 if (!dbus_maybe_add_dict_entry_string(&dict_iter
, "name", name
))
115 ctx_ptr
->error
= true;
120 if (!dbus_message_iter_close_container(&struct_iter
, &dict_iter
))
122 ctx_ptr
->error
= true;
126 if (!dbus_message_iter_close_container(&ctx_ptr
->array_iter
, &struct_iter
))
128 ctx_ptr
->error
= true;
132 free(name
); /* safe if name is NULL */
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;
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
, &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
, g_dbus_error
.message
);
155 dbus_error_free(&g_dbus_error
);
159 if (ctx
.max_items
== 0)
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
)
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
))
182 ladish_recent_store_iterate_items(g_recent_projects_store
, &ctx
, recent_projects_callback
);
188 if (!dbus_message_iter_close_container(&iter
, &ctx
.array_iter
))
196 dbus_message_unref(call_ptr
->reply
);
197 call_ptr
->reply
= NULL
;
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")
209 METHOD_DESCRIBE(get
, get
)
212 INTERFACE_BEGIN(g_iface_recent_items
, IFACE_RECENT_ITEMS
)
213 INTERFACE_DEFAULT_HANDLER
214 INTERFACE_EXPOSE_METHODS