Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / test / test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <glib-object.h>
5 #include <glib-unix.h>
6
7 #include "service_adaptor.h"
8
9 service_plugin_h service_plugin = NULL;
10
11 void _service_auth_oauth1_cb(int result, service_auth_oauth1_h oauth1, void *user_data)
12 {
13         char *access_token = NULL;
14         service_auth_oauth1_get_access_token(oauth1, &access_token);
15
16         printf("access_token = %s (%s)\n", access_token, (char *) user_data);
17 }
18
19 bool _service_storage_cloud_file_cb(int result, service_storage_cloud_file_h file, void *user_data)
20 {
21         char *cloud_path = NULL;
22         service_storage_cloud_file_get_cloud_path(file, &cloud_path);
23
24         printf("cloud_path = %s (%s)\n", cloud_path, (char *) user_data);
25
26         return true;
27 }
28
29 void _service_plugin_login_callback(int result, void *user_data)
30 {
31         int ret = SERVICE_ADAPTOR_ERROR_NONE;
32
33         ret = service_plugin_start(service_plugin);
34
35         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
36         {
37                 printf("service_plugin_start Failed(%d)\n", ret);
38                 return;
39         }
40
41         /* operation */
42         service_auth_oauth1_h oauth1 = NULL;
43         ret = service_auth_oauth1_create(service_plugin, &oauth1);
44         ret = service_auth_oauth1_set_callback(oauth1, _service_auth_oauth1_cb, NULL);
45         ret = service_auth_oauth1_set_operation(oauth1, SERVICE_AUTH_OAUTH1_0_GET_ACCESS_TOKEN_URI);
46
47         service_task_h auth_task = NULL;
48         ret = service_auth_oauth1_create_task(oauth1, &auth_task);
49         ret = service_task_start(auth_task);
50         if (SERVICE_ADAPTOR_ERROR_NONE == ret)
51         {
52                 printf("service_auth_oauth1_get_access_token() Request Successed\n");
53         }
54
55         char *cloud_path = "/root/cloud";
56         service_storage_cloud_file_h file = NULL;
57         ret = service_storage_cloud_file_create(service_plugin, &file);
58         ret = service_storage_cloud_file_set_callback(file, _service_storage_cloud_file_cb, NULL);
59         ret = service_storage_cloud_file_set_cloud_path(file, cloud_path);
60         ret = service_storage_cloud_file_set_operation(file, SERVICE_STORAGE_CLOUD_REMOVE_FILE_URI);
61
62         service_task_h task = NULL;
63         ret = service_storage_cloud_file_create_task(file, &task);
64         ret = service_task_start(task);
65         if (SERVICE_ADAPTOR_ERROR_NONE == ret)
66         {
67                 printf("service_storage_cloud_remove_file() Request Successed: %s\n", cloud_path);
68         }
69 }
70
71 bool _service_adaptor_plugin_callback(const char *uri, int service_mask, void *user_data)
72 {
73         if (0 != strcmp(uri, "org.tizen.service-plugin-sample"))
74         {
75                 return true;
76         }
77
78         int ret = SERVICE_ADAPTOR_ERROR_NONE;
79
80         ret = service_plugin_create(uri, &service_plugin);
81
82         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
83         {
84                 printf("service_plugin_create(%s) Failed(%d)\n", uri, ret);
85                 return true;
86         }
87
88         ret = service_plugin_login(service_plugin, _service_plugin_login_callback, NULL);
89
90         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
91         {
92                 printf("service_plugin_login(%s) Failed(%d)\n", uri, ret);
93                 return true;
94         }
95
96         return true;
97 }
98
99 static gint _sigterm_callback(void *data)
100 {
101         g_main_loop_quit((GMainLoop*)data);
102
103         return 0;
104 }
105
106 int main()
107 {
108         GMainLoop *loop = NULL;
109
110 #if !GLIB_CHECK_VERSION(2,32,0)
111         g_thread_init(NULL);
112 #endif
113 #if !GLIB_CHECK_VERSION(2,35,0)
114         g_type_init();
115 #endif
116
117         /* init */
118         int ret = SERVICE_ADAPTOR_ERROR_NONE;
119
120         ret = service_adaptor_connect();
121         ret = service_adaptor_foreach_plugin(_service_adaptor_plugin_callback, NULL);
122
123         loop = g_main_loop_new(NULL, FALSE);
124
125         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT,
126                         _sigterm_callback, loop, NULL );
127         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM,
128                         _sigterm_callback, loop, NULL );
129
130         g_main_loop_run(loop);
131
132         g_main_loop_unref(loop);
133
134         /* deinit */
135         ret = service_plugin_stop(service_plugin);
136         ret = service_plugin_destroy(service_plugin);
137         ret = service_adaptor_disconnect();
138
139         return ret;
140 }