/* application.ccg
 *
 * Copyright (C) 2016 Marcin Kolny <marcin.kolny@gmail.com>
 *
 * 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/>.
 */

#include "application.h"

namespace {

static void
SignalProxy_async_callback(GObject*, GAsyncResult* res, gpointer data)
{
  auto the_slot = static_cast<Gio::SlotAsyncReady*>(data);

  try
  {
    auto result = Glib::wrap(res, true /* take copy */);
    (*the_slot)(result);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  delete the_slot;
}

}

namespace Ide {

Application::Application()
:
  _CONSTRUCT("application-id", "org.gnome.Builder", "flags", G_APPLICATION_HANDLES_OPEN)
{}


void Application::get_worker_async(const Glib::ustring& plugin_name, const Gio::SlotAsyncReady& slot, const Glib::RefPtr<Gio::Cancellable>& cancellable)
{
  // slot_copy will be deleted in the callback.
  auto slot_copy = new Gio::SlotAsyncReady(slot);

  ide_application_get_worker_async(gobj(), plugin_name.c_str(), Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
}

void Application::get_worker_async(const Glib::ustring& plugin_name, const Gio::SlotAsyncReady& slot)
{
  // slot_copy will be deleted in the callback.
  auto slot_copy = new Gio::SlotAsyncReady(slot);

  ide_application_get_worker_async(gobj(), plugin_name.c_str(), nullptr, &SignalProxy_async_callback, slot_copy);
}

}
