ide-configuration-provider.c 13.7 KB
Newer Older
1 2
/* ide-configuration-provider.c
 *
3
 * Copyright 2016 Matthew Leeds <mleeds@redhat.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 *
 * 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.
 *
 * 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
#define G_LOG_DOMAIN "ide-configuration-provider"

21 22
#include "config.h"

23
#include "application/ide-application.h"
24
#include "config/ide-configuration.h"
25 26
#include "config/ide-configuration-manager.h"
#include "config/ide-configuration-provider.h"
27

28
G_DEFINE_INTERFACE (IdeConfigurationProvider, ide_configuration_provider, IDE_TYPE_OBJECT)
29

30 31 32 33 34 35 36 37
enum {
  ADDED,
  REMOVED,
  N_SIGNALS
};

static guint signals [N_SIGNALS];

38
static void
39 40 41 42
ide_configuration_provider_real_load_async (IdeConfigurationProvider *self,
                                            GCancellable             *cancellable,
                                            GAsyncReadyCallback       callback,
                                            gpointer                  user_data)
43
{
44 45 46 47 48 49 50 51 52 53
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  g_task_report_new_error (self, callback, user_data,
                           ide_configuration_provider_real_load_async,
                           G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           "%s does not implement load_async",
                           G_OBJECT_TYPE_NAME (self));
54 55
}

56
static gboolean
57 58 59 60
ide_configuration_provider_real_load_finish (IdeConfigurationProvider  *self,
                                             GAsyncResult              *result,
                                             GError                   **error)
{
61 62 63 64
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_assert (G_IS_TASK (result));
  g_assert (g_task_is_valid (G_TASK (result), self));
65

66
  return g_task_propagate_boolean (G_TASK (self), error);
67 68 69
}

static void
70 71 72 73 74 75 76 77 78 79 80
ide_configuration_provider_real_duplicate (IdeConfigurationProvider *self,
                                           IdeConfiguration         *config)
{
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_assert (IDE_IS_CONFIGURATION (config));

}

static void
ide_configuration_provider_real_unload (IdeConfigurationProvider *self)
81
{
82 83
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
84

85 86
}

87
static void
88 89 90 91 92
ide_configuration_provider_real_save_async (IdeConfigurationProvider *self,
                                            GCancellable             *cancellable,
                                            GAsyncReadyCallback       callback,
                                            gpointer                  user_data)
{
93 94 95 96 97 98 99 100 101 102
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  g_task_report_new_error (self, callback, user_data,
                           ide_configuration_provider_real_save_async,
                           G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           "%s does not implement save_async",
                           G_OBJECT_TYPE_NAME (self));
103 104
}

105
static gboolean
106 107 108 109
ide_configuration_provider_real_save_finish (IdeConfigurationProvider  *self,
                                             GAsyncResult              *result,
                                             GError                   **error)
{
110 111 112 113
  g_assert (IDE_IS_MAIN_THREAD ());
  g_assert (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_assert (G_IS_TASK (result));
  g_assert (g_task_is_valid (G_TASK (result), self));
114

115
  return g_task_propagate_boolean (G_TASK (self), error);
116 117 118 119 120
}

static void
ide_configuration_provider_default_init (IdeConfigurationProviderInterface *iface)
{
121 122
  iface->load_async = ide_configuration_provider_real_load_async;
  iface->load_finish = ide_configuration_provider_real_load_finish;
123
  iface->duplicate = ide_configuration_provider_real_duplicate;
124 125 126
  iface->unload = ide_configuration_provider_real_unload;
  iface->save_async = ide_configuration_provider_real_save_async;
  iface->save_finish = ide_configuration_provider_real_save_finish;
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

  /**
   * IdeConfigurationProvider:added:
   * @self: an #IdeConfigurationProvider
   * @config: an #IdeConfiguration
   *
   * The "added" signal is emitted when a configuration
   * has been added to a configuration provider.
   *
   * Since: 3.28
   */
  signals [ADDED] =
    g_signal_new ("added",
                  G_TYPE_FROM_INTERFACE (iface),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (IdeConfigurationProviderInterface, added),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1, IDE_TYPE_CONFIGURATION);
  g_signal_set_va_marshaller (signals [ADDED],
                              G_TYPE_FROM_INTERFACE (iface),
                              g_cclosure_marshal_VOID__OBJECTv);

  /**
   * IdeConfigurationProvider:removed:
   * @self: an #IdeConfigurationProvider
   * @config: an #IdeConfiguration
   *
   * The "removed" signal is emitted when a configuration
   * has been removed from a configuration provider.
   *
   * Since: 3.28
   */
  signals [REMOVED] =
    g_signal_new ("removed",
                  G_TYPE_FROM_INTERFACE (iface),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (IdeConfigurationProviderInterface, removed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1, IDE_TYPE_CONFIGURATION);
  g_signal_set_va_marshaller (signals [REMOVED],
                              G_TYPE_FROM_INTERFACE (iface),
                              g_cclosure_marshal_VOID__OBJECTv);

172 173
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * ide_configuration_provider_load_async:
 * @self: a #IdeConfigurationProvider
 * @cancellable: (nullable): a #GCancellable or %NULL
 * @callback: a callback to execute upon completion
 * @user_data: closure data for @callback
 *
 * This function is called to initialize the configuration provider after
 * the plugin instance has been created. The provider should locate any
 * build configurations within the project and call
 * ide_configuration_provider_emit_added() before completing the
 * asynchronous function so that the configuration manager may be made
 * aware of the configurations.
 *
 * Since: 3.28
 */
190
void
191 192 193 194
ide_configuration_provider_load_async (IdeConfigurationProvider *self,
                                       GCancellable             *cancellable,
                                       GAsyncReadyCallback       callback,
                                       gpointer                  user_data)
195
{
196
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
197
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
198 199
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

200
  IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->load_async (self, cancellable, callback, user_data);
201 202
}

203 204 205 206 207 208 209 210 211 212
/**
 * ide_configuration_provider_load_finish:
 * @self: a #IdeConfigurationProvider
 * @result: a #GAsyncResult
 * @error: a location for a #GError, or %NULL
 *
 * Completes an asynchronous request to ide_configuration_provider_load_async().
 *
 * Returns: %TRUE if successful; otherwise %FALSE and @error is set.
 */
213 214 215 216 217
gboolean
ide_configuration_provider_load_finish (IdeConfigurationProvider  *self,
                                        GAsyncResult              *result,
                                        GError                   **error)
{
218
  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), FALSE);
219 220
  g_return_val_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self), FALSE);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
221

222
  return IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->load_finish (self, result, error);
223 224
}

225 226 227 228 229 230 231 232 233 234 235
/**
 * ide_configuration_provider_unload:
 * @self: a #IdeConfigurationProvider
 *
 * Requests that the configuration provider unload any state. This is called
 * shortly before the configuration provider is finalized.
 *
 * Implementations of #IdeConfigurationProvider should emit removed
 * for every configuration they have registered so that the
 * #IdeConfigurationManager has correct information.
 */
236
void
237
ide_configuration_provider_unload (IdeConfigurationProvider *self)
238
{
239
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
240 241
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));

242
  IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->unload (self);
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257
/**
 * ide_configuration_provider_save_async:
 * @self: a #IdeConfigurationProvider
 * @cancellable: (nullable): a #GCancellable or %NULL
 * @callback: a callback to execute upon completion
 * @user_data: closure data for @callback
 *
 * This function is called to request that the configuration provider
 * persist any changed configurations back to disk.
 *
 * This function will be called before unloading the configuration provider
 * so that it has a chance to persist any outstanding changes.
 */
258 259 260 261 262 263
void
ide_configuration_provider_save_async (IdeConfigurationProvider *self,
                                       GCancellable             *cancellable,
                                       GAsyncReadyCallback       callback,
                                       gpointer                  user_data)
{
264
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
265 266 267 268 269 270
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

  IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->save_async (self, cancellable, callback, user_data);
}

271 272 273 274 275 276 277 278 279 280
/**
 * ide_configuration_provider_save_finish:
 * @self: a #IdeConfigurationProvider
 * @result: a #GAsyncResult
 * @error: a location for a #GError, or %NULL
 *
 * Completes an asynchronous request to ide_configuration_provider_save_async().
 *
 * Returns: %TRUE if successful; otherwise %FALSE and @error is set.
 */
281 282 283 284 285
gboolean
ide_configuration_provider_save_finish (IdeConfigurationProvider  *self,
                                        GAsyncResult              *result,
                                        GError                   **error)
{
286
  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), FALSE);
287 288 289 290 291
  g_return_val_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self), FALSE);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);

  return IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->save_finish (self, result, error);
}
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

/**
 * ide_configuration_provider_emit_added:
 * @self: an #IdeConfigurationProvider
 * @config: an #IdeConfiguration
 *
 * #IdeConfigurationProvider implementations should call this function with
 * a @config when it has discovered a new configuration.
 *
 * Since: 3.28
 */
void
ide_configuration_provider_emit_added (IdeConfigurationProvider *self,
                                       IdeConfiguration         *config)
{
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_return_if_fail (IDE_IS_CONFIGURATION (config));

  g_signal_emit (self, signals [ADDED], 0, config);
}

/**
 * ide_configuration_provider_emit_removed:
 * @self: an #IdeConfigurationProvider
 * @config: an #IdeConfiguration
 *
 * #IdeConfigurationProvider implementations should call this function with
 * a @config when it has discovered it was removed.
 *
 * Since: 3.28
 */
void
ide_configuration_provider_emit_removed (IdeConfigurationProvider *self,
                                         IdeConfiguration         *config)
{
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_return_if_fail (IDE_IS_CONFIGURATION (config));

  g_signal_emit (self, signals [REMOVED], 0, config);
}

/**
 * ide_configuration_provider_delete:
 * @self: a #IdeConfigurationProvider
 * @config: an #IdeConfiguration owned by the provider
 *
 * Requests that the configuration provider delete the configuration.
 *
 * ide_configuration_provider_save_async() will be called by the
 * #IdeConfigurationManager after calling this function.
 *
 * Since: 3.28
 */
void
ide_configuration_provider_delete (IdeConfigurationProvider *self,
                                   IdeConfiguration         *config)
{
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_return_if_fail (IDE_IS_CONFIGURATION (config));

  if (IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->delete)
    IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->delete (self, config);
  else
    g_warning ("Cannot delete configuration %s",
               ide_configuration_get_id (config));
}

/**
 * ide_configuration_provider_duplicate:
 * @self: an #IdeConfigurationProvider
 * @config: an #IdeConfiguration
 *
 * Requests that the configuration provider duplicate the configuration.
 *
 * This is useful when the user wants to experiment with alternate settings
 * without breaking a previous configuration.
 *
 * The configuration provider does not need to persist the configuration
 * in this function, ide_configuration_provider_save_async() will be called
 * afterwards to persist configurations to disk.
 *
 * It is expected that the #IdeConfigurationProvider will emit
 * #IdeConfigurationProvider::added with the new configuration.
 *
 * Since: 3.28
 */
void
ide_configuration_provider_duplicate (IdeConfigurationProvider *self,
                                      IdeConfiguration         *config)
{
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
  g_return_if_fail (IDE_IS_CONFIGURATION_PROVIDER (self));
  g_return_if_fail (IDE_IS_CONFIGURATION (config));

  IDE_CONFIGURATION_PROVIDER_GET_IFACE (self)->duplicate (self, config);
}