Version bump
[profile/ivi/lemolo.git] / tizen / dialer_open.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include <Elementary.h>
5 #include <appcore-efl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <Evas.h>
9 #include <E_DBus.h>
10
11 #define APP_NAME "org.tizen.call"
12 #define BUS_NAME "org.tizen.dialer"
13 #define PATH "/"
14 #define IFACE "org.tizen.dialer.Control"
15
16 static E_DBus_Connection *bus_conn = NULL;
17
18 static void _activate_cb(void *data __UNUSED__, DBusMessage *msg __UNUSED__,
19                                 DBusError *error)
20 {
21         if (dbus_error_is_set(error)) {
22                 fprintf(stderr, "Error: %s %s", error->name, error->message);
23         }
24         elm_exit();
25 }
26
27 static void _bring_to_foreground(void)
28 {
29         DBusMessage *msg;
30
31         msg = dbus_message_new_method_call(BUS_NAME, PATH, IFACE, "Activate");
32
33         EINA_SAFETY_ON_NULL_RETURN(msg);
34
35         e_dbus_message_send(bus_conn, msg, _activate_cb, -1, NULL);
36         dbus_message_unref(msg);
37 }
38
39 static void _start_cb(void *data __UNUSED__, DBusMessage *msg __UNUSED__,
40                         DBusError *error)
41 {
42         if (dbus_error_is_set(error)) {
43                 fprintf(stderr, "Error: %s %s", error->name, error->message);
44                 elm_exit();
45                 return;
46         }
47
48         _bring_to_foreground();
49 }
50
51 static void _has_owner_cb(void *data __UNUSED__, DBusMessage *msg,
52                                 DBusError *error)
53 {
54         dbus_bool_t online;
55         DBusError err;
56
57         if (dbus_error_is_set(error)) {
58                 fprintf(stderr, "Error: %s %s", error->name, error->message);
59                 elm_exit();
60                 return;
61         }
62         dbus_error_init(&err);
63         dbus_message_get_args(msg, &err, DBUS_TYPE_BOOLEAN, &online,
64                                 DBUS_TYPE_INVALID);
65
66         if (!online)
67                 e_dbus_start_service_by_name(bus_conn, BUS_NAME, 0, _start_cb, NULL);
68         else
69                 _bring_to_foreground();
70 }
71
72 static Eina_Bool _dbus_init(void)
73 {
74         DBusMessage *msg;
75         char *bus_name = BUS_NAME;
76
77         bus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
78         if (!bus_conn) {
79                 fprintf(stderr, "Could not fetch the DBus session");
80                 return EINA_FALSE;
81         }
82
83         msg = dbus_message_new_method_call(E_DBUS_FDO_BUS, E_DBUS_FDO_PATH,
84                                                 E_DBUS_FDO_INTERFACE,
85                                                 "NameHasOwner");
86
87         EINA_SAFETY_ON_NULL_RETURN_VAL(msg, EINA_FALSE);
88
89         if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &bus_name,
90                                         DBUS_TYPE_INVALID))
91                 goto err_msg;
92
93         e_dbus_message_send(bus_conn, msg, _has_owner_cb, -1, NULL);
94         dbus_message_unref(msg);
95
96         return EINA_TRUE;
97 err_msg:
98         dbus_message_unref(msg);
99         return EINA_FALSE;
100 }
101
102 static int _create(void *data __UNUSED__)
103 {
104         return 0;
105 }
106
107 static int _reset(bundle *b __UNUSED__, void *data __UNUSED__)
108 {
109         return 0;
110 }
111
112 static int _resume(void *data __UNUSED__)
113 {
114         return 0;
115 }
116
117 static int _pause(void *data __UNUSED__)
118 {
119         return 0;
120 }
121
122 static int _terminate(void *data __UNUSED__)
123 {
124         return 0;
125 }
126
127 int main(int argc __UNUSED__, char **argv __UNUSED__)
128 {
129         int r;
130         struct appcore_ops ops = {
131                 .create = _create,
132                 .resume = _resume,
133                 .reset = _reset,
134                 .pause = _pause,
135                 .terminate = _terminate,
136         };
137         ops.data = NULL;
138
139         e_dbus_init();
140         EINA_SAFETY_ON_FALSE_RETURN_VAL(_dbus_init(), -1);
141         r = appcore_efl_main(APP_NAME, &argc, &argv, &ops);
142         e_dbus_connection_close(bus_conn);
143
144         e_dbus_shutdown();
145         return r;
146 }