summaryrefslogtreecommitdiff
path: root/src/examples/eio/eio_job_open.c
blob: f31491b4f53e786d4d43ff83b12fd83e861f7a62 (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
70
71
72
73
74
75
76
77
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <Eina.h>
#include <Eio.h>
#include <Ecore.h>

void error_cb(void *data, Eina_Error error)
{
    EINA_SAFETY_ON_NULL_RETURN(data);

    const char *msg = eina_error_msg_get(error);
    EINA_LOG_ERR("error: %s", msg);

    ecore_main_loop_quit();
}

void done_closing_cb(void* data EINA_UNUSED, void *value EINA_UNUSED)
{
    printf("%s closed file.\n", __FUNCTION__);

    ecore_main_loop_quit();
}

void closing_job(Eio_Job *job, Eina_File *file)
{
    Eina_Promise *promise = NULL;
    printf("%s Will close the file...\n", __FUNCTION__);
    eio_job_file_close(job, file, &promise);
    eina_promise_then(promise, &done_closing_cb, &error_cb, job);
}

void done_open_cb(void *data, void* value)
{
    EINA_SAFETY_ON_NULL_RETURN(data);
    EINA_SAFETY_ON_NULL_RETURN(value);

    Eina_File *file = eina_file_dup(value);

    Eio_Job *job = data;

    const char *name = eina_file_filename_get(file);
    printf("%s opened file %s\n", __FUNCTION__, name);

    closing_job(job, file);
}

void open_file(const char *path)
{
    Eio_Job *job = eo_add(EIO_JOB_CLASS, NULL);
    eina_promise_then(eio_job_file_open(job, path, EINA_FALSE), &done_open_cb, &error_cb, job);

    eo_unref(job);
}

int main(int argc, char const *argv[])
{
    eio_init();
    ecore_init();

    const char *path = getenv("HOME");

    if (argc > 1)
        path = argv[1];

    open_file(path);

    ecore_main_loop_begin();

    ecore_shutdown();
    eio_shutdown();
    return 0;
}