summaryrefslogtreecommitdiff
path: root/endless/hello.c
blob: 1db6f9008946e321d9ea7c55ba1c2011721b649a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright 2013 Endless Mobile, Inc. */

#include <string.h>
#include <glib.h>
#include <gio/gio.h>

#include <endless/endless.h>

/**
 * SECTION:hello
 * @short_description: Sample skeleton function
 * @title: Hello
 *
 * This is a sample skeleton function that says hello either to the terminal or
 * a file.
 */

/**
 * eos_hello_sample_function:
 * @file: (allow-none): #GFile to write to, or %NULL
 * @error: (allow-none): Return location for a #GError, or %NULL to ignore.
 *
 * A sample API function to say hello with. Prints on the terminal if @file is
 * %NULL, or else appends it to @file.
 *
 * Returns: %TRUE on success, %FALSE on error.
 */
gboolean
eos_hello_sample_function(GFile   *file,
                          GError **error)
{
  char hello_string[] = "Hello, world!\n";
  GFileOutputStream *stream;
  ssize_t write_count;
  gboolean success;

  g_return_val_if_fail (G_IS_FILE (file) || file == NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  /* Print to terminal */
  if (file == NULL)
    {
      g_print ("%s", hello_string);
      return TRUE;
    }

  stream = g_file_append_to (file,
                             G_FILE_CREATE_NONE,
                             NULL, /* cancellable */
                             error);
  if(!stream)
    return FALSE;

  write_count = g_output_stream_write (G_OUTPUT_STREAM (stream),
                                       hello_string,
                                       strlen (hello_string),
                                       NULL, /* cancellable */
                                       error);
  success = g_output_stream_close (G_OUTPUT_STREAM (stream),
                                   NULL, /* cancellable */
                                   error);
  g_object_unref (stream);

  if (write_count == -1 || !success)
    return FALSE;

  return TRUE;
}