1 /* -*- Mode: C ; c-basic-offset: 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"
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 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;
100 if (!dbus_message_iter_append_basic(&struct_iter
, DBUS_TYPE_STRING
, &project_path
))
102 ctx_ptr
->error
= true;
106 if (!dbus_message_iter_open_container(&struct_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
))
108 ctx_ptr
->error
= true;
112 if (!cdbus_maybe_add_dict_entry_string(&dict_iter
, "name", name
))
114 ctx_ptr
->error
= true;
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;
131 free(name
); /* safe if name is NULL */
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;
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
);
158 if (ctx
.max_items
== 0)
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
)
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
))
181 ladish_recent_store_iterate_items(g_recent_projects_store
, &ctx
, recent_projects_callback
);
187 if (!dbus_message_iter_close_container(&iter
, &ctx
.array_iter
))
195 dbus_message_unref(call_ptr
->reply
);
196 call_ptr
->reply
= NULL
;
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
208 CDBUS_METHOD_DESCRIBE(get
, get
)
211 CDBUS_INTERFACE_DEFAULT_HANDLER_METHODS_ONLY(g_iface_recent_items
, IFACE_RECENT_ITEMS
)