File: foundry-intent-manager.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (152 lines) | stat: -rw-r--r-- 4,968 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* foundry-intent-manager.c
 *
 * Copyright 2025 Christian Hergert
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "foundry-extension-set.h"
#include "foundry-intent.h"
#include "foundry-intent-handler.h"
#include "foundry-intent-manager.h"
#include "foundry-util.h"

/**
 * FoundryIntentManager:
 *
 * The intent manager provides a generic mechanism to handle intents
 * within an application.
 *
 * This could include opening a file with the OpeFileIntent, or other
 * custom intents.
 *
 * [class@Foundry.IntentHandler] may be registered to handle these
 * intents or ignore them for the next handler to handle them.
 *
 * Since: 1.1
 */

struct _FoundryIntentManager
{
  FoundryService parent_instance;
};

G_DEFINE_FINAL_TYPE (FoundryIntentManager, foundry_intent_manager, FOUNDRY_TYPE_SERVICE)

static void
foundry_intent_manager_class_init (FoundryIntentManagerClass *klass)
{
}

static void
foundry_intent_manager_init (FoundryIntentManager *self)
{
}

static void
collect_addins_cb (FoundryExtensionSet *set,
                   PeasPluginInfo      *plugin_info,
                   GObject             *extension,
                   gpointer             user_data)
{
  GPtrArray *collect = user_data;
  g_ptr_array_add (collect, g_object_ref (extension));
}

static DexFuture *
foundry_intent_manager_dispatch_fiber (FoundryIntentManager *self,
                                       FoundryIntent        *intent)
{
  g_autoptr(FoundryExtensionSet) addins = NULL;
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GPtrArray) collect = NULL;
  g_autoptr(GError) error = NULL;

  g_assert (FOUNDRY_IS_INTENT_MANAGER (self));
  g_assert (FOUNDRY_IS_INTENT (intent));

  if (!(context = foundry_contextual_acquire (FOUNDRY_CONTEXTUAL (self), &error)))
    return dex_future_new_for_error (g_steal_pointer (&error));

  /* Collect all addins first so that plugin unloading will not
   * affect the runtime operation of our async awaits.
   */
  collect = g_ptr_array_new_with_free_func (g_object_unref);
  addins = foundry_extension_set_new (context,
                                      peas_engine_get_default (),
                                      FOUNDRY_TYPE_INTENT_HANDLER,
                                      "Intent-Handler", "*", NULL);
  foundry_extension_set_foreach_by_priority (addins, collect_addins_cb, collect);
  g_clear_object (&addins);

  g_debug ("Trying intent with %u handlers", collect->len);

  if (collect->len == 0)
    return foundry_future_new_not_supported ();

  /* Dispatch to handlers by priority. */
  for (guint i = 0; i < collect->len; i++)
    {
      FoundryIntentHandler *handler = g_ptr_array_index (collect, i);
      g_autoptr(GListModel) results = NULL;
      g_autoptr(DexFuture) future = NULL;
      g_autoptr(GError) dispatch_error = NULL;

      g_debug ("Trying intent with `%s`", G_OBJECT_TYPE_NAME (handler));

      future = foundry_intent_handler_dispatch (handler, intent);
      if (dex_await (dex_ref (future), &dispatch_error))
        return dex_ref (future);

      if (error == NULL &&
          !g_error_matches (dispatch_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
        error = g_steal_pointer (&dispatch_error);
    }

  /* Bail with first non-synthetic error */
  if (error != NULL)
    return dex_future_new_for_error (g_steal_pointer (&error));

  return foundry_future_new_not_supported ();
}

/**
 * foundry_intent_manager_dispatch:
 * @self: a [class@Foundry.IntentManager]
 * @intent: a [class@Foundry.Intent]
 *
 * Dispatch the intent to the first handler which can handle it.
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to any value
 *   if sucessful or rejects with error.
 *
 * Since: 1.1
 */
DexFuture *
foundry_intent_manager_dispatch (FoundryIntentManager *self,
                                 FoundryIntent        *intent)
{
  dex_return_error_if_fail (FOUNDRY_IS_INTENT_MANAGER (self));
  dex_return_error_if_fail (FOUNDRY_IS_INTENT (intent));

  return foundry_scheduler_spawn (NULL, 0,
                                  G_CALLBACK (foundry_intent_manager_dispatch_fiber),
                                  2,
                                  FOUNDRY_TYPE_INTENT_MANAGER, self,
                                  FOUNDRY_TYPE_INTENT, intent);
}