Commit 4b329727 authored by Thomas Haller's avatar Thomas Haller

cli: rework connection handling for multiple results

Functions like nmc_find_connection() and nmc_find_active_connection()
can easily find multiple matching results. For example, the
"connection.id" in NetworkManager is not enforced to be unique,
so if the user adds multiple connections with the same name,
they should be all selected.

The previous API had a @pos argument, that allowed to iterate over
the results. Change that, to return all matches in a GPtrArray.

Also, extend connection-show and other places, to anticipate that
a connection might be active multiple times in any moment.
parent 4029d4df
...@@ -412,8 +412,11 @@ print_dhcp_config (NMDhcpConfig *dhcp, ...@@ -412,8 +412,11 @@ print_dhcp_config (NMDhcpConfig *dhcp,
* @connections: array of NMConnections to search in * @connections: array of NMConnections to search in
* @filter_type: "id", "uuid", "path" or %NULL * @filter_type: "id", "uuid", "path" or %NULL
* @filter_val: connection to find (connection name, UUID or path) * @filter_val: connection to find (connection name, UUID or path)
* @start: where to start in @list. The location is updated so that the function * @out_result: if not NULL, attach all matching connection to this
* can be called multiple times (for connections with the same name). * list. If necessary, a new array will be allocated. If the array
* already contains a connection, it will not be added a second time.
* All object are referenced by the array. If the function allocates
* a new array, it will set the free function to g_object_unref.
* @complete: print possible completions * @complete: print possible completions
* *
* Find a connection in @list according to @filter_val. @filter_type determines * Find a connection in @list according to @filter_val. @filter_type determines
...@@ -428,17 +431,18 @@ NMConnection * ...@@ -428,17 +431,18 @@ NMConnection *
nmc_find_connection (const GPtrArray *connections, nmc_find_connection (const GPtrArray *connections,
const char *filter_type, const char *filter_type,
const char *filter_val, const char *filter_val,
int *start, GPtrArray **out_result,
gboolean complete) gboolean complete)
{ {
NMConnection *connection; NMConnection *connection;
NMConnection *found = NULL; NMConnection *best_candidate = NULL;
guint i; GPtrArray *result = out_result ? *out_result : NULL;
guint i, j;
nm_assert (connections); nm_assert (connections);
nm_assert (filter_val); nm_assert (filter_val);
for (i = start ? *start : 0; i < connections->len; i++) { for (i = 0; i < connections->len; i++) {
const char *v, *v_num; const char *v, *v_num;
connection = NM_CONNECTION (connections->pdata[i]); connection = NM_CONNECTION (connections->pdata[i]);
...@@ -476,35 +480,39 @@ nmc_find_connection (const GPtrArray *connections, ...@@ -476,35 +480,39 @@ nmc_find_connection (const GPtrArray *connections,
continue; continue;
found: found:
if (!start) if (!out_result)
return connection; return connection;
if (found) { if (!best_candidate)
*start = i; best_candidate = connection;
return found; if (!result)
result = g_ptr_array_new_with_free_func (g_object_unref);
for (j = 0; j < result->len; j++) {
if (connection == result->pdata[j])
break;
} }
found = connection; if (j == result->len)
g_ptr_array_add (result, g_object_ref (connection));
} }
if (start) NM_SET_OUT (out_result, result);
*start = 0; return best_candidate;
return found;
} }
NMActiveConnection * NMActiveConnection *
nmc_find_active_connection (const GPtrArray *active_cons, nmc_find_active_connection (const GPtrArray *active_cons,
const char *filter_type, const char *filter_type,
const char *filter_val, const char *filter_val,
int *idx, GPtrArray **out_result,
gboolean complete) gboolean complete)
{ {
int i; guint i, j;
int start = (idx && *idx > 0) ? *idx : 0; NMActiveConnection *best_candidate = NULL;
NMRemoteConnection *con; GPtrArray *result = out_result ? *out_result : NULL;
NMActiveConnection *found = NULL;
nm_assert (filter_val); nm_assert (filter_val);
for (i = start; i < active_cons->len; i++) { for (i = 0; i < active_cons->len; i++) {
NMRemoteConnection *con;
NMActiveConnection *candidate = g_ptr_array_index (active_cons, i); NMActiveConnection *candidate = g_ptr_array_index (active_cons, i);
const char *v, *v_num; const char *v, *v_num;
...@@ -552,19 +560,24 @@ nmc_find_active_connection (const GPtrArray *active_cons, ...@@ -552,19 +560,24 @@ nmc_find_active_connection (const GPtrArray *active_cons,
} }
continue; continue;
found: found:
if (!idx) if (!out_result)
return candidate; return candidate;
if (found) { if (!best_candidate)
*idx = i; best_candidate = candidate;
return found; if (!result)
result = g_ptr_array_new_with_free_func (g_object_unref);
for (j = 0; j < result->len; j++) {
if (candidate == result->pdata[j])
break;
} }
found = candidate; if (j == result->len)
g_ptr_array_add (result, g_object_ref (candidate));
} }
if (idx) NM_SET_OUT (out_result, result);
*idx = 0; return best_candidate;
return found;
} }
static gboolean static gboolean
......
...@@ -32,13 +32,13 @@ gboolean print_dhcp_config (NMDhcpConfig *dhcp, const NmcConfig *nmc_config, con ...@@ -32,13 +32,13 @@ gboolean print_dhcp_config (NMDhcpConfig *dhcp, const NmcConfig *nmc_config, con
NMConnection *nmc_find_connection (const GPtrArray *connections, NMConnection *nmc_find_connection (const GPtrArray *connections,
const char *filter_type, const char *filter_type,
const char *filter_val, const char *filter_val,
int *start, GPtrArray **out_result,
gboolean complete); gboolean complete);
NMActiveConnection *nmc_find_active_connection (const GPtrArray *active_cons, NMActiveConnection *nmc_find_active_connection (const GPtrArray *active_cons,
const char *filter_type, const char *filter_type,
const char *filter_val, const char *filter_val,
int *idx, GPtrArray **out_result,
gboolean complete); gboolean complete);
void nmc_secrets_requested (NMSecretAgentSimple *agent, void nmc_secrets_requested (NMSecretAgentSimple *agent,
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment