summaryrefslogtreecommitdiff
path: root/src/examples/eio/eio_job_open.c
blob: fb235c2e0695dff4e282a6d195ccd7cab4ca350a (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
78
79
80
#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(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(int *result 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, (Eina_Promise_Cb)&done_closing_cb, (Eina_Promise_Error_Cb)&error_cb, job);
}

void done_open_cb(void *data, Eina_File **file)
{
    EINA_SAFETY_ON_NULL_RETURN(data);
    EINA_SAFETY_ON_NULL_RETURN(file);
    EINA_SAFETY_ON_NULL_RETURN(*file);

    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)
{
    Eina_Promise *promise;

    Eio_Job *job = eo_add(EIO_JOB_CLASS, NULL);
    eio_job_file_open(job, path, EINA_FALSE, &promise);
    eina_promise_then(promise, (Eina_Promise_Cb)&done_open_cb, (Eina_Promise_Error_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;
}