ide-service.c 3.77 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
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20 21 22
#define G_LOG_DOMAIN "ide-service"

#include "config.h"

23 24
#include <glib/gi18n.h>

25
#include "ide-context.h"
26 27
#include "ide-service.h"

28
G_DEFINE_INTERFACE (IdeService, ide_service, IDE_TYPE_OBJECT)
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * 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
 */

45
enum {
46
  CONTEXT_LOADED,
47
  N_SIGNALS
48 49
};

50
static guint signals [N_SIGNALS];
51

52 53 54 55 56 57 58 59 60 61 62
/**
 * 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
 */
63
const gchar *
64
ide_service_get_name (IdeService *self)
65
{
66
  g_return_val_if_fail (IDE_IS_SERVICE (self), NULL);
67

68
  return IDE_SERVICE_GET_IFACE (self)->get_name (self);
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82
/**
 * 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
 */
83
void
84
ide_service_start (IdeService *self)
85
{
86
  g_return_if_fail (IDE_IS_SERVICE (self));
87

88 89
  if (IDE_SERVICE_GET_IFACE (self)->start)
    IDE_SERVICE_GET_IFACE (self)->start (self);
90 91
}

92 93 94 95 96 97 98 99 100 101 102 103
/**
 * 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
 */
104
void
105
ide_service_stop (IdeService *self)
106
{
107
  g_return_if_fail (IDE_IS_SERVICE (self));
108

109 110
  if (IDE_SERVICE_GET_IFACE (self)->stop)
    IDE_SERVICE_GET_IFACE (self)->stop (self);
111 112
}

113
void
114
_ide_service_emit_context_loaded (IdeService *self)
115
{
116
  g_return_if_fail (IDE_IS_SERVICE (self));
117

118
  g_signal_emit (self, signals [CONTEXT_LOADED], 0);
119
}
120

121
static const gchar *
122
ide_service_real_get_name (IdeService *self)
123
{
124
  return G_OBJECT_TYPE_NAME (self);
125 126 127
}

static void
128
ide_service_default_init (IdeServiceInterface *iface)
129
{
130 131
  iface->get_name = ide_service_real_get_name;

132 133 134 135 136 137 138 139 140 141
  /**
   * 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
   */
142
  signals [CONTEXT_LOADED] =
143
    g_signal_new ("context-loaded",
144
                  G_TYPE_FROM_INTERFACE (iface),
145
                  G_SIGNAL_RUN_LAST,
146
                  G_STRUCT_OFFSET (IdeServiceInterface, context_loaded),
147
                  NULL, NULL, NULL,
148 149
                  G_TYPE_NONE,
                  0);
150
}