ide-service.c 3.71 KB
Newer Older
1 2
/* ide-service.c
 *
3
 * Copyright © 2015 Christian Hergert <christian@hergert.me>
4
 *
5 6 7 8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
9
 *
10 11 12 13
 * This program 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 General Public License for more details.
14 15 16 17 18 19 20
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib/gi18n.h>

21
#include "ide-context.h"
22 23
#include "ide-service.h"

24
G_DEFINE_INTERFACE (IdeService, ide_service, IDE_TYPE_OBJECT)
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * SECTION:ide-service
 * @title: IdeService
 * @short_description: Provide project services for plugins
 *
 * The #IdeService inteface is meant as a place to provide utility code to
 * your plugin that should have it's lifetime bound to the lifetime of the
 * loaded project.
 *
 * When the project is created, the service will be started. When the project
 * is closed, the service will be stopped and discarded.
 *
 * Since: 3.16
 */

41
enum {
42
  CONTEXT_LOADED,
43
  N_SIGNALS
44 45
};

46
static guint signals [N_SIGNALS];
47

48 49 50 51 52 53 54 55 56 57 58
/**
 * ide_service_get_name:
 * @self: an #IdeService
 *
 * Gets the name of the service. By default, the name of the object is
 * used as the service name.
 *
 * Returns: (not nullable): A name for the service.
 *
 * Since: 3.16
 */
59
const gchar *
60
ide_service_get_name (IdeService *self)
61
{
62
  g_return_val_if_fail (IDE_IS_SERVICE (self), NULL);
63

64
  return IDE_SERVICE_GET_IFACE (self)->get_name (self);
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78
/**
 * ide_service_start:
 * @self: an #IdeService
 *
 * This calls the #IdeServiceInterface.start virtual function which plugins
 * use to initialize their service.
 *
 * This function is called by the owning #IdeContext and should not be needed
 * by plugins or other internal API in Builder.
 *
 * Since: 3.16
 */
79
void
80
ide_service_start (IdeService *self)
81
{
82
  g_return_if_fail (IDE_IS_SERVICE (self));
83

84 85
  if (IDE_SERVICE_GET_IFACE (self)->start)
    IDE_SERVICE_GET_IFACE (self)->start (self);
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99
/**
 * ide_service_stop:
 * @self: an #IdeService
 *
 * This calls the #IdeServiceInterface.stop virtual function which plugins
 * use to cleanup after their service.
 *
 * This function is called by the owning #IdeContext and should not be needed
 * by plugins or other internal API in Builder.
 *
 * Since: 3.16
 */
100
void
101
ide_service_stop (IdeService *self)
102
{
103
  g_return_if_fail (IDE_IS_SERVICE (self));
104

105 106
  if (IDE_SERVICE_GET_IFACE (self)->stop)
    IDE_SERVICE_GET_IFACE (self)->stop (self);
107 108
}

109
void
110
_ide_service_emit_context_loaded (IdeService *self)
111
{
112
  g_return_if_fail (IDE_IS_SERVICE (self));
113

114
  g_signal_emit (self, signals [CONTEXT_LOADED], 0);
115
}
116

117
static const gchar *
118
ide_service_real_get_name (IdeService *self)
119
{
120
  return G_OBJECT_TYPE_NAME (self);
121 122 123
}

static void
124
ide_service_default_init (IdeServiceInterface *iface)
125
{
126 127
  iface->get_name = ide_service_real_get_name;

128 129 130 131 132 133 134 135 136 137
  /**
   * IdeService::context-loaded:
   * @self: an #IdeService
   *
   * The "context-loaded" signal is emitted when the owning #IdeContext
   * has completed loading the project. This may be useful if you want to
   * defer startup procedures until the context is fully loaded.
   *
   * Since: 3.20
   */
138
  signals [CONTEXT_LOADED] =
139
    g_signal_new ("context-loaded",
140
                  G_TYPE_FROM_INTERFACE (iface),
141
                  G_SIGNAL_RUN_LAST,
142
                  G_STRUCT_OFFSET (IdeServiceInterface, context_loaded),
143
                  NULL, NULL, NULL,
144 145
                  G_TYPE_NONE,
                  0);
146
}