Esempio n. 1
0
static void
content_chooser_done (GObject      *object,
                      GAsyncResult *result,
                      gpointer      user_data)
{
  g_autoptr(GSubprocess) subprocess = G_SUBPROCESS (object);
  ContentChooserData *data = user_data;
  g_autoptr(GBytes) stdout_buf = NULL;
  g_autoptr(GError) error = NULL;
  const char *uri = NULL;
  g_autoptr (GFile) file = NULL;
  g_autoptr(GDBusConnection) connection = NULL;

  if (!g_subprocess_communicate_finish (subprocess, result, &stdout_buf, NULL, &error))
    {
      g_dbus_method_invocation_return_error (data->invocation,
                                             XDP_ERROR, XDP_ERROR_FAILED,
                                             "Content chooser failed: %s", error->message);
      g_free (data);
      return;
    }

  if (!g_subprocess_get_if_exited (subprocess) ||
      g_subprocess_get_exit_status (subprocess) != 0)
    {
      g_dbus_method_invocation_return_error (data->invocation,
                                             XDP_ERROR, XDP_ERROR_FAILED,
                                             "Content chooser exit %d", g_subprocess_get_exit_status (subprocess));
      g_free (data);
      return;
    }

  uri = g_bytes_get_data (stdout_buf, NULL);
  file = g_file_new_for_uri (uri);
  data->basename = g_file_get_basename (file);

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
  g_dbus_connection_call (connection,
                          "org.freedesktop.portal.Documents",
                          "/org/freedesktop/portal/documents",
                          "org.freedesktop.portal.Documents",
                          "Add",
                          g_variant_new ("(s)", uri),
                          G_VARIANT_TYPE ("(u)"),
                          G_DBUS_CALL_FLAGS_NONE,
                          30000,
                          NULL,
                          got_document_handle,
                          data);
}
Esempio n. 2
0
static void
on_session_finished (GSubprocess  *subprocess,
                     GAsyncResult *result,
                     State        *state)
{
        gboolean cancelled;

        cancelled = !g_subprocess_wait_finish (subprocess, result, NULL);

        if (cancelled) {
                goto out;
        }

        if (g_subprocess_get_if_exited (subprocess)) {
                int exit_status;

                exit_status = g_subprocess_get_exit_status (subprocess);

                g_debug ("session exited with status %d", exit_status);

                state->session_exit_status = exit_status;
        } else {
                int signal_number;

                signal_number = g_subprocess_get_term_sig (subprocess);
                g_debug ("session was killed with status %d", signal_number);
        }

        g_clear_object (&state->session_subprocess);
out:
        g_main_loop_quit (state->main_loop);
}
Esempio n. 3
0
static void
process_communicate_utf8_cb (GObject      *object,
                             GAsyncResult *result,
                             gpointer      user_data)
{
  g_autoptr (GSubprocess) process = (GSubprocess *)object;
  g_autoptr (GTask) task = (GTask *)user_data;
  g_autofree gchar *stdout_str = NULL;
  g_autofree gchar *stderr_str = NULL;
  g_autoptr(GError) error = NULL;
  GtkSourceCompletion *completion;
  GtkTextBuffer *buffer;
  GtkTextIter begin;
  GtkTextIter end;
  ProcessState *state;
  gboolean status;

  g_assert (G_IS_SUBPROCESS (process));
  g_assert (G_IS_ASYNC_RESULT (result));
  g_assert (G_IS_TASK (task));

  if (!g_subprocess_communicate_utf8_finish (process, result, &stdout_str, &stderr_str, &error))
    {
      g_task_return_error (task, g_steal_pointer (&error));
      return;
    }

  if (g_task_return_error_if_cancelled (task))
    return;

  state = (ProcessState *)g_task_get_task_data (task);
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (state->source_view));
  completion = gtk_source_view_get_completion (GTK_SOURCE_VIEW (state->source_view));

  if (!ide_str_empty0 (stdout_str))
    {
      gtk_source_completion_block_interactive (completion);
      gtk_text_buffer_begin_user_action (buffer);

      gtk_text_buffer_get_iter_at_mark (buffer, &begin, state->begin_mark);
      gtk_text_buffer_get_iter_at_mark (buffer, &end, state->end_mark);
      gtk_text_buffer_delete (buffer, &begin, &end);
      gtk_text_buffer_insert (buffer, &begin, stdout_str, -1);

      /* Get valid iters from marks */
      gtk_text_buffer_get_iter_at_mark (buffer, &begin, state->begin_mark);
      gtk_text_buffer_get_iter_at_mark (buffer, &end, state->end_mark);
      gtk_text_buffer_select_range (buffer, &begin, &end);
      g_signal_emit_by_name (state->source_view, "selection-theatric", IDE_SOURCE_VIEW_THEATRIC_EXPAND);

      gtk_text_buffer_end_user_action (buffer);
      gtk_source_completion_unblock_interactive (completion);

      g_task_return_boolean (task, TRUE);
    }
  else
    g_warning ("beautify plugin: output empty");

  if (g_subprocess_get_if_exited (process))
    {
      status = g_subprocess_get_exit_status (process);
      if (status != 0 &&
          stderr_str != NULL &&
          !ide_str_empty0 (stderr_str))
        {
          g_warning ("beautify plugin stderr:\n%s", stderr_str);
        }
    }
}
Esempio n. 4
0
static void
subprocess_wait_cb (GObject      *object,
                    GAsyncResult *res,
                    gpointer      user_data)
{
  GSubprocess *subprocess = (GSubprocess *)object;
  g_autofree gchar *input_prefix = NULL;
  g_autoptr(IdeDiagnostics) local_diags = NULL;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GPtrArray) array = NULL;
  g_autoptr(GDataInputStream) stderr_data_input = NULL;
  GInputStream *stderr_input = NULL;
  g_autoptr(IdeGettextDiagnostics) diags = NULL;
  TranslationUnit *unit;
  GError *error = NULL;

  g_assert (G_IS_SUBPROCESS (subprocess));
  g_assert (G_IS_TASK (task));

  unit = g_task_get_task_data (task);

  g_assert (unit != NULL);

  if (!g_subprocess_wait_finish (subprocess, res, &error))
    {
      g_task_return_error (task, error);
      return;
    }

  array = g_ptr_array_new_with_free_func ((GDestroyNotify)ide_diagnostic_unref);
  if (g_subprocess_get_exit_status (subprocess) == 0)
    goto out;

  stderr_input = g_subprocess_get_stderr_pipe (subprocess);
  stderr_data_input = g_data_input_stream_new (stderr_input);
  input_prefix = g_strdup_printf ("%s:", ide_unsaved_file_get_temp_path (unit->unsaved_file));

  for (;;)
    {
      g_autofree gchar *line = NULL;
      gsize length;

      line = g_data_input_stream_read_line (stderr_data_input,
                                            &length,
                                            g_task_get_cancellable (task),
                                            &error);
      if (line == NULL)
        break;

      if (g_str_has_prefix (line, input_prefix))
        {
          gchar *p = line + strlen (input_prefix);

          if (g_ascii_isdigit (*p))
            {
              gulong line_number = strtoul (p, &p, 10);
              IdeSourceLocation *loc;
              IdeDiagnostic *diag;

              loc = ide_source_location_new (unit->file,
                                             line_number - 1,
                                             0,
                                             0);
              diag = ide_diagnostic_new (IDE_DIAGNOSTIC_WARNING,
                                         g_strstrip (p + 1),
                                         loc);
              g_ptr_array_add (array, diag);
            }
        }
    }

 out:
  local_diags = ide_diagnostics_new (g_steal_pointer (&array));
  diags = g_object_new (IDE_TYPE_GETTEXT_DIAGNOSTICS,
                        "diagnostics", local_diags,
                        "sequence", ide_unsaved_file_get_sequence (unit->unsaved_file),
                        NULL);
  g_task_return_pointer (task, g_steal_pointer (&diags), g_object_unref);
}