4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus.h>
29 #include "vconf-keys.h"
31 #include <sys/types.h>
34 /*Messaging Header Files*/
36 #include "msg_storage.h"
37 #include "msg_storage_types.h"
38 #include "msg_transport.h"
39 #include "msg_transport_types.h"
40 #include "msg_types.h"
43 /*Email Header Files*/
44 #include "email-types.h"
45 #include "email-api-init.h"
46 #include "email-api-account.h"
47 #include "email-api-mailbox.h"
48 #include "email-api-mail.h"
49 #include "email-api-network.h"
52 #include <bluetooth_map_agent.h>
54 #include <map_bmessage.h>
56 #define OBEX_CLIENT_SERVICE "org.openobex.client"
57 #define OBEX_CLIENT_INTERFACE "org.openobex.Client"
58 #define OBEX_CLIENT_PATH "/"
59 #define MNS_CLIENT_INTERFACE "org.openobex.MessageNotification"
61 #define DBUS_STRUCT_STRING_STRING_UINT (dbus_g_type_get_struct("GValueArray", \
62 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID))
64 #define DBUS_STRUCT_MESSAGE_LIST (dbus_g_type_get_struct("GValueArray", \
65 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, \
66 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, \
67 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING, \
68 G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, \
69 G_TYPE_BOOLEAN, G_TYPE_STRING, \
72 static msg_handle_t g_msg_handle = NULL;
74 #define BT_MAP_NEW_MESSAGE "NewMessage"
75 #define BT_MAP_STATUS_CB "sent status callback"
76 #define BT_MAP_MSG_CB "sms message callback"
77 #define BT_MAP_EMAIL_DEFAULTACCOUNT "db/email/defaultaccount"
78 #define BT_MNS_OBJECT_PATH "/org/bluez/mns"
79 #define BT_MNS_INTERFACE "org.bluez.mns"
80 #define BT_MAIL_TEMP_BODY "/tmp/bt_mail.txt"
81 #define BT_MAP_SENT_FOLDER_NAME "SENT"
82 #define BT_MAP_MSG_INFO_MAX 256
83 #define BT_MAP_MSG_HANDLE_MAX 21
84 #define BT_MAP_TIMESTAMP_MAX_LEN 16
85 #define BT_MAP_SUBJECT_MAX_LEN 20
86 #define BT_MAP_MSG_BODY_MAX 1024
87 #define BT_MSG_UPDATE 0
88 #define BT_MSG_DELETE 1
91 #define BT_EMAIL_HANDLE_BASE (G_MAXUINT64 / 2)
92 #define BT_MAIL_ID_MAX_LENGTH 50
94 #define BEGIN_BMSEG "BEGIN:BMSG\r\n"
95 #define END_BMSEG "END:BMSG\r\n"
96 #define BMSEG_VERSION "VERSION:1.0\r\n"
97 #define MSEG_STATUS "STATUS:%s\r\n"
98 #define MSEG_TYPE "TYPE:%s\r\n"
99 #define FOLDER_PATH "FOLDER:%s\r\n"
100 #define VCARD "BEGIN:VCARD\r\nVERSION:2.1\r\nN:%s\r\nTEL:%s\r\nEND:VCARD\r\n"
101 #define BEGIN_BENV "BEGIN:BENV\r\n"
102 #define END_BENV "END:BENV\r\n"
103 #define BEGIN_BBODY "BEGIN:BBODY\r\n"
104 #define END_BBODY "END:BBODY\r\n"
105 #define ENCODING "ENCODING:%s\r\n"
106 #define CHARSET "CHARSET:%s\r\n"
107 #define LANGUAGE "LANGUAGE:%s\r\n"
108 #define LENGTH "LENGTH:%d\r\n"
109 #define MSG_BODY "BEGIN:MSG\r\n%s\r\nEND:MSG\r\n"
111 /* This has been added for testing purpose, will be removed when SMS APIs
113 #define TEST_PDU "06810000000000040681567777000021017101750261A05"\
114 "376BA0D8297E5F3B73BCC4ED3F3A030FB1ECECF41613A"\
115 "5D1E1ED3E7A0B2BD2CCF8362AEA4195407C941ECF77C9"\
116 "E769F41753968FC769BD3E4B27B5C0691EB6510FD0D7AD"\
117 "BCBF27B397D46D343A163990E42BFDB6590BCDC4E93D3"\
118 "E539889E86CF41F437485E26D7C765D0DB5E96DFCBE933"\
119 "9A1E9A36A72063900AA2BF41B5DBED760385E920E9DC357B35A9"
121 GSList* id_list = NULL;
122 guint64 current_push_map_id;
129 struct msg_send_option {
135 struct message_info {
140 char *sender_addressing;
141 char *recipient_name;
142 char *recipient_addressing;
145 char *reception_status;
146 char *attachment_size;
147 char *replyto_addressing;
161 } BluetoothMapAgentClass;
163 GType bluetooth_map_agent_get_type(void);
165 #define BLUETOOTH_MAP_TYPE_AGENT (bluetooth_map_agent_get_type())
167 #define BLUETOOTH_MAP_AGENT(object) \
168 (G_TYPE_CHECK_INSTANCE_CAST((object), \
169 BLUETOOTH_MAP_TYPE_AGENT , BluetoothMapAgent))
170 #define BLUETOOTH_MAP_AGENT_CLASS(klass) \
171 (G_TYPE_CHECK_CLASS_CAST((klass), \
172 BLUETOOTH_MAP_TYPE_AGENT , BluetoothMapAgentClass))
173 #define BLUETOOTH_MAP_IS_AGENT(object) \
174 (G_TYPE_CHECK_INSTANCE_TYPE((object), \
175 BLUETOOTH_MAP_TYPE_AGENT))
176 #define BLUETOOTH_MAP_IS_AGENT_CLASS(klass) \
177 (G_TYPE_CHECK_CLASS_TYPE((klass), \
178 BLUETOOTH_MAP_TYPE_AGENT))
179 #define BLUETOOTH_MAP_AGENT_GET_CLASS(obj) \
180 (G_TYPE_INSTANCE_GET_CLASS((obj), \
181 BLUETOOTH_MAP_TYPE_AGENT , BluetoothMapAgentClass))
183 G_DEFINE_TYPE(BluetoothMapAgent, bluetooth_map_agent, G_TYPE_OBJECT)
185 GMainLoop *g_mainloop = NULL;
186 static DBusGConnection *g_connection = NULL;
187 static char *g_mns_path = NULL;
188 static struct msg_send_option opt;
190 static gboolean bluetooth_map_get_folder_tree(BluetoothMapAgent *agent,
191 DBusGMethodInvocation *context);
192 static gboolean bluetooth_map_get_message_list(BluetoothMapAgent *agent,
193 gchar *folder_name, guint16 max,
194 DBusGMethodInvocation *context);
195 static gboolean bluetooth_map_get_message(BluetoothMapAgent *agent,
197 gboolean attach, gboolean transcode,
198 gboolean first_request,
199 DBusGMethodInvocation *context);
200 static gboolean bluetooth_map_push_message(BluetoothMapAgent *agent,
205 DBusGMethodInvocation *context);
206 static gboolean bluetooth_map_push_message_data(BluetoothMapAgent *agent,
208 DBusGMethodInvocation *context);
209 static gboolean bluetooth_map_update_message(BluetoothMapAgent *agent,
210 DBusGMethodInvocation *context);
211 static gboolean bluetooth_map_set_read_status(BluetoothMapAgent *agent,
212 gchar *handle, gboolean read_status,
213 DBusGMethodInvocation *context);
214 static gboolean bluetooth_map_set_delete_status(BluetoothMapAgent *agent,
215 gchar *handle, gboolean delete_status,
216 DBusGMethodInvocation *context);
217 static gboolean bluetooth_map_noti_registration(BluetoothMapAgent *agent,
220 DBusGMethodInvocation *context);
223 #include "bluetooth_map_agent_glue.h"
225 static void bluetooth_map_agent_init(BluetoothMapAgent *obj)
229 g_assert(obj != NULL);
232 static void bluetooth_map_agent_finalize(GObject *obj)
236 G_OBJECT_CLASS(bluetooth_map_agent_parent_class)->finalize(obj);
239 static void bluetooth_map_agent_class_init(BluetoothMapAgentClass *klass)
241 GObjectClass *object_class = (GObjectClass *) klass;
243 g_assert(klass != NULL);
245 object_class->finalize = bluetooth_map_agent_finalize;
247 dbus_g_object_type_install_info(BLUETOOTH_MAP_TYPE_AGENT,
248 &dbus_glib_bluetooth_map_object_info);
251 static GQuark __bt_map_agent_error_quark(void)
253 static GQuark quark = 0;
255 quark = g_quark_from_static_string("agent");
260 static GError *__bt_map_agent_error(bt_map_agent_error_t error,
263 return g_error_new(BT_MAP_AGENT_ERROR, error, err_msg, NULL);
266 static guint64 _bt_validate_uid(int uid)
269 struct id_info *info;
273 count = g_slist_length(id_list);
274 for (i = 0; i < count; i++) {
275 info = (struct id_info *)g_slist_nth_data(id_list, i);
279 if (info->uid == uid) {
280 printf("uid = %d\n", uid);
288 static guint64 __bt_add_id(int uid)
290 DBG("Add id: %d\n", uid);
291 static guint64 map_id;
292 struct id_info *info;
295 test = _bt_validate_uid(uid);
296 DBG("test: %llx\n", test);
300 info = g_new0(struct id_info, 1);
304 info->map_id = map_id;
306 DBG("map_id = %llx, uid = %d \n", info->map_id, info->uid);
308 id_list = g_slist_append(id_list, info);
313 static int __bt_get_id(guint64 map_id)
316 struct id_info *info;
320 count = g_slist_length(id_list);
322 for (i = 0; i < count; i++) {
323 info = (struct id_info *)g_slist_nth_data(id_list, i);
325 if (info->map_id == map_id)
332 static int __bt_get_uid(gchar *handle)
340 map_id = g_ascii_strtoull(handle, NULL, 16);
344 uid = __bt_get_id(map_id);
349 static int __bt_update_id(guint64 map_id, int new_uid)
352 struct id_info *info;
356 count = g_slist_length(id_list);
358 for (i = 0; i < count; i++) {
359 info = g_slist_nth_data(id_list, i);
361 if (info->map_id == map_id) {
370 static void __bt_remove_list(GSList *id_list)
375 DBG("Removing id list\n");
376 g_slist_free_full(id_list, g_free);
380 static gchar *__bt_get_folder_name(int id)
383 char folder_name[BT_MAP_MSG_INFO_MAX] = {0,};
385 msg_struct_list_s g_folderList;
386 msg_struct_t p_folder;
388 ret = msg_get_folder_list(g_msg_handle, &g_folderList);
389 if (ret != MSG_SUCCESS)
392 p_folder = g_folderList.msg_struct_info[id];
394 ret = msg_get_str_value(p_folder, MSG_FOLDER_INFO_NAME_STR,
395 folder_name, BT_MAP_MSG_INFO_MAX);
396 if (ret != MSG_SUCCESS)
399 return g_strdup_printf("TELECOM/MSG/%s", folder_name);
402 return g_strdup("TELECOM/MSG");
405 static void __get_msg_timestamp(time_t *ltime, char *timestamp)
407 struct tm local_time;
411 if (!localtime_r(ltime, &local_time))
414 year = local_time.tm_year + 1900; /* years since 1900 */
415 month = local_time.tm_mon + 1; /* months since January */
416 snprintf(timestamp, 16, "%04d%02d%02dT%02d%02d%02d", year, month,
417 local_time.tm_mday, local_time.tm_hour,
418 local_time.tm_min, local_time.tm_sec);
423 static char *__bt_prepare_msg_bmseg(msg_struct_t msg_info, gboolean attach,
427 int m_type = MSG_TYPE_SMS;
430 bool read_status = false;
431 char msg_body[BT_MAP_MSG_BODY_MAX] = {0,};
432 char addr_value[MAX_ADDRESS_VAL_LEN] = {0,};
433 char name_value[MAX_ADDRESS_VAL_LEN] = {0,};
436 msg_struct_list_s *addr_list = NULL;
439 msg = g_string_new(BEGIN_BMSEG);
440 g_string_append(msg, BMSEG_VERSION);
442 ret = msg_get_bool_value(msg_info, MSG_MESSAGE_READ_BOOL, &read_status);
443 if (ret == MSG_SUCCESS) {
444 DBG("read_status %d\n", read_status);
448 g_string_append_printf(msg, MSEG_STATUS, "READ");
450 g_string_append_printf(msg, MSEG_STATUS, "UNREAD");
452 ret = msg_get_int_value(msg_info, MSG_MESSAGE_TYPE_INT, &m_type);
453 if (ret == MSG_SUCCESS) {
454 DBG("m_type %d\n", m_type);
459 case MSG_TYPE_MMS_JAVA:
460 case MSG_TYPE_MMS_NOTI:
461 g_string_append_printf(msg, MSEG_TYPE, "MMS");
465 g_string_append_printf(msg, MSEG_TYPE, "SMS_GSM");
469 ret = msg_get_int_value(msg_info, MSG_MESSAGE_FOLDER_ID_INT,
471 if (ret == MSG_SUCCESS) {
472 DBG("folder_id %d\n", folder_id);
475 folder_path = __bt_get_folder_name(folder_id);
476 g_string_append_printf(msg, FOLDER_PATH, folder_path);
479 ret = msg_get_list_handle(msg_info, MSG_MESSAGE_ADDR_LIST_STRUCT,
480 (void **)&addr_list);
481 if (ret == MSG_SUCCESS) {
482 count = addr_list->nCount;
483 DBG("count %d \n", count);
485 msg_struct_t addr_info = NULL;
486 addr_info = addr_list->msg_struct_info[count - 1];
488 msg_get_str_value(addr_info,
489 MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
490 addr_value, MAX_ADDRESS_VAL_LEN);
491 DBG("addr_value %s\n", addr_value);
492 msg_get_str_value(addr_info,
493 MSG_ADDRESS_INFO_DISPLAYNAME_STR,
494 name_value, MAX_ADDRESS_VAL_LEN);
495 if (!strlen(name_value))
496 g_stpcpy(name_value, addr_value);
498 DBG("name_value %s\n", name_value);
500 g_string_append_printf(msg, VCARD, name_value,
506 g_string_append(msg, BEGIN_BENV);
507 g_string_append(msg, BEGIN_BBODY);
510 g_string_append_printf(msg, CHARSET, "UTF-8");
512 if (m_type == MSG_TYPE_MMS)
513 ret = msg_get_str_value(msg_info,
514 MSG_MESSAGE_MMS_TEXT_STR,
515 msg_body, BT_MAP_SUBJECT_MAX_LEN);
517 ret = msg_get_str_value(msg_info,
518 MSG_MESSAGE_SMS_DATA_STR,
519 msg_body, BT_MAP_MSG_BODY_MAX);
521 if (ret == MSG_SUCCESS) {
522 g_string_append_printf(msg, LENGTH, strlen(msg_body));
523 g_string_append_printf(msg, MSG_BODY, msg_body);
527 g_string_append_printf(msg, ENCODING, "G-7BIT");
528 g_string_append_printf(msg, CHARSET, "native");
529 /* The below line has been added for testing purpose,
530 will be removed when SMS APIs are available. */
531 msg_pdu = g_strdup(TEST_PDU);
532 g_string_append_printf(msg, LENGTH, strlen(msg_pdu));
533 g_string_append_printf(msg, MSG_BODY, msg_pdu);
537 g_string_append(msg, END_BBODY);
538 g_string_append(msg, END_BENV);
539 g_string_append(msg, END_BMSEG);
541 return g_string_free(msg, FALSE);
544 static void __bt_message_info_free(struct message_info msg_info)
546 g_free(msg_info.handle);
547 g_free(msg_info.subject);
548 g_free(msg_info.datetime);
549 g_free(msg_info.sender_name);
550 g_free(msg_info.sender_addressing);
551 g_free(msg_info.replyto_addressing);
552 g_free(msg_info.recipient_name);
553 g_free(msg_info.recipient_addressing);
554 g_free(msg_info.type);
555 g_free(msg_info.reception_status);
556 g_free(msg_info.size);
557 g_free(msg_info.attachment_size);
560 static struct message_info __bt_message_info_get(msg_struct_t msg_struct_handle)
562 struct message_info msg_info = {0,};
571 bool protect_status = 0;
572 bool read_status = 0;
574 char msg_handle[BT_MAP_MSG_HANDLE_MAX] = {0,};
575 char msg_subject[BT_MAP_SUBJECT_MAX_LEN] = {0,};
576 char msg_datetime[BT_MAP_TIMESTAMP_MAX_LEN] = {0,};
577 char msg_size[5] = {0,};
578 char msg_body[BT_MAP_MSG_BODY_MAX] = {0,};
579 char addr_value[MAX_ADDRESS_VAL_LEN] = {0,};
580 char name_value[MAX_ADDRESS_VAL_LEN] = {0,};
582 msg_info.text = FALSE;
583 msg_info.protect = FALSE;
584 msg_info.read = FALSE;
585 msg_info.priority = FALSE;
587 msg_struct_t msg = NULL;
588 msg_struct_t send_opt = NULL;
589 msg_struct_list_s *addr_list = NULL;
590 msg_struct_t addr_info = NULL;
592 ret = msg_get_int_value(msg_struct_handle, MSG_MESSAGE_ID_INT, &msg_id);
593 if (ret == MSG_SUCCESS) {
594 uid = __bt_add_id(msg_id);
595 snprintf(msg_handle, sizeof(msg_handle), "%llx", uid);
597 msg_info.handle = g_strdup(msg_handle);
599 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
600 send_opt = msg_create_struct(MSG_STRUCT_SENDOPT);
602 ret = msg_get_message(g_msg_handle,
603 (msg_message_id_t)msg_id,
605 if (ret != MSG_SUCCESS) {
606 DBG("ret = %d\n", ret);
610 ret = msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_STRUCT,
611 (void **)&addr_list);
612 if (ret != MSG_SUCCESS) {
613 DBG("ret = %d\n", ret);
617 addr_info = addr_list->msg_struct_info[0];
619 ret = msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
620 addr_value, MAX_ADDRESS_VAL_LEN);
621 if (ret == MSG_SUCCESS)
622 DBG("addr_value %s\n", addr_value);
624 ret = msg_get_str_value(addr_info, MSG_ADDRESS_INFO_DISPLAYNAME_STR,
625 name_value, MAX_ADDRESS_VAL_LEN);
626 if (ret == MSG_SUCCESS)
627 DBG("name_value %s\n", name_value);
629 if (!strlen(name_value))
630 g_stpcpy(name_value, addr_value);
632 DBG("name_value %s\n", name_value);
634 ret = msg_get_int_value(msg, MSG_MESSAGE_DIRECTION_INT, &direction_type);
635 if (ret != MSG_SUCCESS)
638 if (direction_type == MSG_DIRECTION_TYPE_MT) {
639 msg_info.sender_name = g_strdup(name_value);
640 msg_info.sender_addressing = g_strdup(addr_value);
641 msg_info.recipient_name = g_strdup("Unknown");
642 msg_info.recipient_addressing = g_strdup("0000");
644 msg_info.sender_name = g_strdup("Unknown");
645 msg_info.sender_addressing = g_strdup("0000");
646 msg_info.recipient_name = g_strdup(name_value);
647 msg_info.recipient_addressing = g_strdup(addr_value);
651 msg_release_struct(&msg);
652 msg_release_struct(&send_opt);
654 ret = msg_get_int_value(msg_struct_handle, MSG_MESSAGE_DISPLAY_TIME_INT, &dptime);
655 if (ret == MSG_SUCCESS) {
656 __get_msg_timestamp((time_t *)&dptime, msg_datetime);
658 msg_info.datetime = g_strdup(msg_datetime);
660 ret = msg_get_int_value(msg_struct_handle, MSG_MESSAGE_TYPE_INT,
662 if (ret == MSG_SUCCESS) {
663 DBG("m_type %d\n", m_type);
668 case MSG_TYPE_MMS_JAVA:
669 case MSG_TYPE_MMS_NOTI:
670 msg_info.type = g_strdup("MMS");
674 msg_info.type = g_strdup("SMS_GSM");
678 if (m_type == MSG_TYPE_MMS) {
679 ret = msg_get_str_value(msg_struct_handle,
680 MSG_MESSAGE_SUBJECT_STR, msg_subject,
681 BT_MAP_SUBJECT_MAX_LEN);
682 if (ret == MSG_SUCCESS) {
683 DBG("MMS subject %s", msg_subject);
686 msg_info.subject = g_strdup(msg_subject);
688 ret = msg_get_str_value(msg_struct_handle,
689 MSG_MESSAGE_MMS_TEXT_STR, msg_body,
690 BT_MAP_MSG_BODY_MAX);
691 if (ret == MSG_SUCCESS) {
692 DBG("msg_body %s", msg_body);
693 if (strlen(msg_body))
694 msg_info.text = TRUE ;
697 } else if (m_type == MSG_TYPE_SMS) {
698 ret = msg_get_str_value(msg_struct_handle,
699 MSG_MESSAGE_SMS_DATA_STR, msg_body,
700 BT_MAP_MSG_BODY_MAX);
701 if (ret == MSG_SUCCESS) {
702 DBG("SMS subject %s", msg_body);
703 if (strlen(msg_body)) {
704 msg_info.text = TRUE ;
705 msg_info.subject = g_strndup(msg_body,
706 BT_MAP_SUBJECT_MAX_LEN);
711 ret = msg_get_int_value(msg_struct_handle, MSG_MESSAGE_DATA_SIZE_INT,
713 if (ret == MSG_SUCCESS)
714 snprintf(msg_size, sizeof(msg_size), "%d", data_size);
716 msg_info.size = g_strdup(msg_size);
718 msg_info.reception_status = g_strdup("complete");
719 msg_info.attachment_size = g_strdup("0");
721 ret = msg_get_bool_value(msg_struct_handle, MSG_MESSAGE_PROTECTED_BOOL,
723 if (ret == MSG_SUCCESS) {
725 msg_info.protect = TRUE;
728 ret = msg_get_bool_value(msg_struct_handle, MSG_MESSAGE_READ_BOOL,
730 if (ret == MSG_SUCCESS) {
732 msg_info.read = TRUE;
735 ret = msg_get_int_value(msg_struct_handle, MSG_MESSAGE_PRIORITY_INT,
737 if (ret == MSG_SUCCESS) {
738 if (priority == MSG_MESSAGE_PRIORITY_HIGH)
739 msg_info.priority = TRUE;
745 static void __bluetooth_map_msg_incoming_status_cb(msg_handle_t handle,
749 DBusGProxy *mns_proxy;
750 GError *error = NULL;
754 int ret = MSG_SUCCESS;
756 char *message_type = NULL;
762 ret = msg_get_int_value(msg, MSG_MESSAGE_ID_INT, &msg_id);
763 if (ret != MSG_SUCCESS)
766 uid = __bt_add_id(msg_id);
768 ret = msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &msg_type);
769 if (ret != MSG_SUCCESS)
774 message_type = g_strdup("SMS_GSM");
777 message_type = g_strdup("MMS");
783 mns_proxy = dbus_g_proxy_new_for_name(g_connection, OBEX_CLIENT_SERVICE,
785 MNS_CLIENT_INTERFACE);
786 if (mns_proxy == NULL) {
787 ERR("Failed to get a proxy for D-Bus\n");
788 g_free(message_type);
792 dbus_g_proxy_call(mns_proxy, "SendEvent", &error,
793 G_TYPE_STRING, "NewMessage",
795 G_TYPE_STRING, "TELECOM/MSG/INBOX",
797 G_TYPE_STRING, message_type,
798 G_TYPE_INVALID, G_TYPE_INVALID);
800 DBG("Error [%s]", error->message);
805 g_free(message_type);
806 g_object_unref(mns_proxy);
810 static gboolean __bluetooth_map_start_service()
812 msg_error_t err = MSG_SUCCESS;
813 gboolean msg_ret = TRUE;
815 int email_err = EMAIL_ERROR_NONE;
816 gboolean email_ret = TRUE;
819 err = msg_open_msg_handle(&g_msg_handle);
820 if (err != MSG_SUCCESS) {
821 ERR("msg_open_msg_handle error = %d\n", err);
826 err = msg_reg_sms_message_callback(g_msg_handle,
827 __bluetooth_map_msg_incoming_status_cb,
828 0, (void *)BT_MAP_MSG_CB);
829 if (err != MSG_SUCCESS) {
830 ERR("msg_reg_sms_message_callback error = %d\n", err);
838 email_err = email_service_begin();
839 if (email_err != EMAIL_ERROR_NONE) {
840 ERR("email_service_begin fail error = %d\n", email_err);
844 if (msg_ret || email_ret)
856 static void __bluetooth_map_stop_service()
858 if (NULL != g_msg_handle)
859 msg_close_msg_handle(&g_msg_handle);
864 if (EMAIL_ERROR_NONE != email_service_end())
865 ERR("email_service_end fail \n");
871 static int __bt_store_mail(email_mailbox_type_e type, char *subject,
872 char *body, char *recepients)
877 char from_address[BT_MAIL_ID_MAX_LENGTH] = { 0, };
881 email_account_t *account_data = NULL;
882 email_mailbox_t *mailbox_data = NULL;
883 email_mail_data_t *mail_data = NULL;
885 err = email_load_default_account_id(&account_id);
886 if (EMAIL_ERROR_NONE != err)
889 err = email_get_account(account_id, GET_FULL_DATA_WITHOUT_PASSWORD,
891 if (EMAIL_ERROR_NONE != err)
894 err = email_get_mailbox_by_mailbox_type(account_id, type,
896 if (EMAIL_ERROR_NONE != err)
899 snprintf(from_address, BT_MAIL_ID_MAX_LENGTH, "<%s>",
900 account_data->user_email_address);
901 email_free_account(&account_data, 1);
903 mail_data = calloc(1, sizeof(email_mail_data_t));
904 if (NULL == mail_data) {
905 email_free_mailbox(&mailbox_data, 1);
909 DBG("\n account_id %d\n", account_id);
910 mail_data->account_id = account_id;
911 mail_data->save_status = 1;
912 mail_data->body_download_status = 1;
913 /* mail_data->flags_draft_field = 1; */
914 mail_data->flags_seen_field = 1;
915 mail_data->file_path_plain = g_strdup(BT_MAIL_TEMP_BODY);
917 mail_data->mailbox_id = mailbox_data->mailbox_id;
918 mail_data->mailbox_type = mailbox_data->mailbox_type;
919 email_free_mailbox(&mailbox_data, 1);
921 mail_data->full_address_from = g_strdup(from_address);
922 mail_data->full_address_to = g_strdup(recepients);
923 mail_data->subject = g_strdup(subject);
924 mail_data->report_status = EMAIL_MAIL_REQUEST_DSN |
925 EMAIL_MAIL_REQUEST_MDN;
927 body_file = fopen(BT_MAIL_TEMP_BODY, "w");
928 if (body_file == NULL) {
929 DBG("\n fopen [%s]failed\n", BT_MAIL_TEMP_BODY);
930 email_free_mail_data(&mail_data, 1);
934 fprintf(body_file, body);
938 err = email_add_mail(mail_data, NULL, 0, NULL, 0);
939 if (err != EMAIL_ERROR_NONE) {
940 DBG("email_add_mail failed. [%d]\n", err);
941 if (!stat(mail_data->file_path_plain, &st_buf))
942 remove(mail_data->file_path_plain);
944 email_free_mail_data(&mail_data, 1);
948 DBG("saved mail id = [%d]\n", mail_data->mail_id);
950 mail_id = mail_data->mail_id;
952 email_free_mail_data(&mail_data, 1);
960 static int __bt_email_send(char *subject, char *body, char* recepients)
966 mail_id = __bt_store_mail(EMAIL_MAILBOX_TYPE_OUTBOX, subject,
969 DBG("mail_id = %d\n", mail_id);
970 err = email_send_mail(mail_id, &handle);
971 if (err != EMAIL_ERROR_NONE)
972 DBG("Sending failed[%d]\n", err);
979 static int __bt_get_folder_id(char *folder_path)
984 msg_struct_list_s folder_list;
986 msg_struct_t p_folder;
987 DBG("__bt_get_folder_id\n");
989 folder = strrchr(folder_path, '/');
995 DBG("folderName %s\n", folder);
997 err = msg_get_folder_list(g_msg_handle, &folder_list);
998 if (err != MSG_SUCCESS)
1001 for (i = 0; i < folder_list.nCount; i++) {
1002 p_folder = folder_list.msg_struct_info[i];
1003 char folder_name[BT_MAP_MSG_INFO_MAX] = {0, };
1005 err = msg_get_str_value(p_folder, MSG_FOLDER_INFO_NAME_STR,
1006 folder_name, BT_MAP_MSG_INFO_MAX);
1007 if (err != MSG_SUCCESS)
1010 DBG("folderName %s\n", folder_name);
1011 if (!g_ascii_strncasecmp(folder_name, folder, strlen(folder))) {
1012 err = msg_get_int_value(p_folder, MSG_FOLDER_INFO_ID_INT,
1014 if (err != MSG_SUCCESS)
1025 msg_error_t __bt_send_sms(int msg_id, msg_struct_t pMsg, msg_struct_t pSendOpt)
1030 pReq = msg_create_struct(MSG_STRUCT_REQUEST_INFO);
1032 msg_set_int_value(pMsg, MSG_MESSAGE_ID_INT, msg_id);
1033 msg_set_struct_handle(pReq, MSG_REQUEST_MESSAGE_HND, pMsg);
1034 msg_set_struct_handle(pReq, MSG_REQUEST_SENDOPT_HND, pSendOpt);
1036 err = msg_sms_send_message(g_msg_handle, pReq);
1037 if (err == MSG_SUCCESS)
1038 DBG("Sending Message is successful!!!");
1040 DBG("Sending Message is failed!!! %d", err);
1042 msg_release_struct(&pReq);
1046 static int __bt_push_sms(gboolean send, int folder_id, char *body,
1050 msg_struct_t msg_info = NULL;
1051 msg_struct_t send_opt = NULL;
1052 msg_struct_list_s *addr_list;
1060 msg_info = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1061 if (msg_info == NULL)
1064 err = msg_set_int_value(msg_info, MSG_MESSAGE_TYPE_INT, MSG_TYPE_SMS);
1065 if (err != MSG_SUCCESS)
1069 err = msg_set_str_value(msg_info, MSG_MESSAGE_SMS_DATA_STR, body,
1071 if (err != MSG_SUCCESS)
1074 err = msg_set_str_value(msg_info, MSG_MESSAGE_SMS_DATA_STR,
1076 if (err != MSG_SUCCESS)
1080 DBG("folder_id %d\n", folder_id);
1081 err = msg_set_int_value(msg_info, MSG_MESSAGE_FOLDER_ID_INT,
1083 if (err != MSG_SUCCESS)
1087 count = g_slist_length(recepients);
1088 DBG("Count = %d\n", count);
1089 msg_get_list_handle(msg_info, MSG_MESSAGE_ADDR_LIST_STRUCT,
1090 (void**)&addr_list);
1092 addr_list->nCount = count;
1093 for (i = 0; i < count; i++) {
1094 char *address = (char *)g_slist_nth_data(recepients, i);
1095 if (address == NULL) {
1096 DBG("[ERROR] address is value NULL, skip");
1099 msg_set_int_value(addr_list->msg_struct_info[i],
1100 MSG_ADDRESS_INFO_ADDRESS_TYPE_INT,
1101 MSG_ADDRESS_TYPE_PLMN);
1103 msg_set_int_value(addr_list->msg_struct_info[i],
1104 MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
1105 MSG_RECIPIENTS_TYPE_TO);
1107 msg_set_str_value(addr_list->msg_struct_info[i],
1108 MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
1109 address, strlen(address));
1113 send_opt = msg_create_struct(MSG_STRUCT_SENDOPT);
1115 err = msg_set_bool_value(send_opt, MSG_SEND_OPT_SETTING_BOOL, true);
1116 if (err != MSG_SUCCESS)
1119 /* Do not keep a copy */
1120 err = msg_set_bool_value(send_opt, MSG_SEND_OPT_KEEPCOPY_BOOL,
1122 if (err != MSG_SUCCESS)
1125 msg_id = msg_add_message(g_msg_handle, msg_info, send_opt);
1126 DBG("msg_id = %d\n", msg_id);
1127 uid = __bt_add_id(msg_id);
1130 err = __bt_send_sms(msg_id, msg_info, send_opt);
1131 if (err != MSG_SUCCESS) {
1138 msg_release_struct(&msg_info);
1139 msg_release_struct(&send_opt);
1144 static gboolean __bt_msg_is_mms(int msg_type)
1146 gboolean result = FALSE;
1149 case MSG_TYPE_MMS_NOTI:
1150 case MSG_TYPE_MMS_JAVA:
1161 static void __bt_mns_client_connect(char *address)
1163 DBusGProxy *mns_proxy;
1167 GError *error = NULL;
1168 const char *session_path = NULL;
1170 DBG("+ address %s\n", address);
1172 mns_proxy = dbus_g_proxy_new_for_name(g_connection, OBEX_CLIENT_SERVICE,
1174 OBEX_CLIENT_INTERFACE);
1175 if (mns_proxy == NULL) {
1176 ERR("Failed to get a proxy for D-Bus\n");
1180 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1181 NULL, (GDestroyNotify)g_free);
1183 addr_value = g_new0(GValue, 1);
1184 g_value_init(addr_value, G_TYPE_STRING);
1185 g_value_set_string(addr_value, address);
1186 g_hash_table_insert(hash, "Destination", addr_value);
1188 tgt_value = g_new0(GValue, 1);
1189 g_value_init(tgt_value, G_TYPE_STRING);
1190 g_value_set_string(tgt_value, "MNS");
1191 g_hash_table_insert(hash, "Target", tgt_value);
1193 dbus_g_proxy_call(mns_proxy, "CreateSession", &error,
1194 dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
1195 hash, G_TYPE_INVALID,
1196 DBUS_TYPE_G_OBJECT_PATH, &session_path,
1199 DBG("Error [%s]", error->message);
1200 g_error_free(error);
1201 g_hash_table_destroy(hash);
1202 g_object_unref(mns_proxy);
1206 g_mns_path = g_strdup(session_path);
1207 DBG("g_mns_path = %s\n", g_mns_path);
1209 g_hash_table_destroy(hash);
1210 g_object_unref(mns_proxy);
1216 static void __bt_mns_client_disconnect()
1218 DBusGProxy *mns_proxy;
1219 GError *error = NULL;
1224 mns_proxy = dbus_g_proxy_new_for_name(g_connection, OBEX_CLIENT_SERVICE,
1226 OBEX_CLIENT_INTERFACE);
1227 if (mns_proxy == NULL) {
1228 DBG("Failed to get a proxy for D-Bus\n");
1232 dbus_g_proxy_call(mns_proxy, "RemoveSession", &error,
1233 DBUS_TYPE_G_OBJECT_PATH, g_mns_path,
1234 G_TYPE_INVALID, G_TYPE_INVALID);
1236 DBG("Error [%s]", error->message);
1237 g_error_free(error);
1238 g_object_unref(mns_proxy);
1245 g_object_unref(mns_proxy);
1251 static gboolean bluetooth_map_get_folder_tree(BluetoothMapAgent *agent,
1252 DBusGMethodInvocation *context)
1254 GPtrArray *array = g_ptr_array_new();
1256 GError *error = NULL;
1258 char name[BT_MAP_MSG_INFO_MAX] = {0,};
1259 char folder_name[BT_MAP_MSG_INFO_MAX] = {0,};
1262 gboolean msg_ret = TRUE;
1264 msg_struct_list_s g_folderList;
1265 msg_struct_t p_folder;
1267 #ifdef SUPPORT_EMAIL
1270 int mailbox_count = 0;
1271 gboolean flag = FALSE;
1272 email_mailbox_t *mailbox_list = NULL;
1275 if (g_msg_handle == NULL) {
1280 if (msg_get_folder_list(g_msg_handle, &g_folderList) != MSG_SUCCESS) {
1285 for (i = 0; i < g_folderList.nCount; i++) {
1286 p_folder = g_folderList.msg_struct_info[i];
1287 memset(folder_name, 0x00, BT_MAP_MSG_INFO_MAX);
1289 ret = msg_get_str_value(p_folder, MSG_FOLDER_INFO_NAME_STR,
1290 folder_name, BT_MAP_MSG_INFO_MAX);
1291 if (ret != MSG_SUCCESS)
1294 if (!g_ascii_strncasecmp(folder_name, BT_MAP_SENT_FOLDER_NAME,
1295 strlen(BT_MAP_SENT_FOLDER_NAME))) {
1296 memset(folder_name, 0, sizeof(folder_name));
1297 g_strlcpy(folder_name, BT_MAP_SENT_FOLDER_NAME,
1298 sizeof(folder_name));
1301 g_strlcpy(name, folder_name, sizeof(name));
1302 memset(&value, 0, sizeof(GValue));
1303 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
1304 g_value_take_boxed(&value, dbus_g_type_specialized_construct(
1305 DBUS_STRUCT_STRING_STRING_UINT));
1306 dbus_g_type_struct_set(&value, 0, name, G_MAXUINT);
1307 g_ptr_array_add(array, g_value_get_boxed(&value));
1310 #ifdef SUPPORT_EMAIL
1312 if (EMAIL_ERROR_NONE != email_load_default_account_id(&account_id))
1315 if (EMAIL_ERROR_NONE != email_get_mailbox_list(account_id,
1324 for (i = 0; i < mailbox_count; i++) {
1326 for (j = 0; j < g_folderList.nCount; j++) {
1328 p_folder = g_folderList.msg_struct_info[j];
1329 memset(folder_name, 0x00, BT_MAP_MSG_INFO_MAX);
1331 ret = msg_get_str_value(p_folder,
1332 MSG_FOLDER_INFO_NAME_STR,
1334 BT_MAP_MSG_INFO_MAX);
1335 if (ret != MSG_SUCCESS)
1338 if (!g_ascii_strncasecmp(mailbox_list[i].alias,
1339 folder_name, strlen(mailbox_list[i].alias))) {
1346 g_strlcpy(name, mailbox_list[i].alias, sizeof(name));
1348 if (!g_ascii_strncasecmp(name, BT_MAP_SENT_FOLDER_NAME,
1349 strlen(BT_MAP_SENT_FOLDER_NAME)))
1352 memset(&value, 0, sizeof(GValue));
1353 g_value_init(&value, DBUS_STRUCT_STRING_STRING_UINT);
1354 g_value_take_boxed(&value,
1355 dbus_g_type_specialized_construct(
1356 DBUS_STRUCT_STRING_STRING_UINT));
1357 dbus_g_type_struct_set(&value, 0, name, G_MAXUINT);
1358 g_ptr_array_add(array, g_value_get_boxed(&value));
1362 if (mailbox_list != NULL)
1363 email_free_mailbox(&mailbox_list, mailbox_count);
1368 if (msg_ret == FALSE) {
1369 g_ptr_array_free(array, TRUE);
1371 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
1373 dbus_g_method_return_error(context, error);
1374 g_error_free(error);
1377 dbus_g_method_return(context, array);
1378 g_ptr_array_free(array, TRUE);
1383 static gboolean bluetooth_map_get_message_list(BluetoothMapAgent *agent,
1384 gchar *folder_name, guint16 max,
1385 DBusGMethodInvocation *context)
1387 GPtrArray *array = g_ptr_array_new();
1389 GError *error = NULL;
1391 char *folder = NULL;
1399 msg_struct_list_s g_folderList;
1400 msg_struct_list_s msg_list;
1401 msg_struct_t count_info;
1403 #ifdef SUPPORT_EMAIL
1406 int mailbox_count = 0;
1409 char msg_datetime[BT_MAP_TIMESTAMP_MAX_LEN] = {0,};
1410 email_mailbox_t *mailbox_list = NULL;
1411 email_mail_list_item_t *mail_list = NULL;
1412 email_list_filter_t *filter_list = NULL;
1413 email_list_sorting_rule_t *sorting_rule_list = NULL;
1416 if (g_msg_handle == NULL)
1419 folder = strrchr(folder_name, '/');
1421 folder = folder_name;
1425 ret = msg_get_folder_list(g_msg_handle, &g_folderList);
1426 if (ret != MSG_SUCCESS)
1429 for (i = 0; i < g_folderList.nCount; i++) {
1430 msg_struct_t pFolder = g_folderList.msg_struct_info[i];
1431 char folderName[BT_MAP_MSG_INFO_MAX] = {0, };
1433 ret = msg_get_str_value(pFolder, MSG_FOLDER_INFO_NAME_STR,
1434 folderName, BT_MAP_MSG_INFO_MAX);
1435 if (ret != MSG_SUCCESS)
1438 if (!g_ascii_strncasecmp(folderName, folder, strlen(folder))) {
1439 ret = msg_get_int_value(pFolder, MSG_FOLDER_INFO_ID_INT,
1441 if (ret != MSG_SUCCESS)
1444 DBG("folder_id %d \n", folder_id);
1450 ret = msg_get_folder_view_list(g_msg_handle, folder_id,
1452 if (ret != MSG_SUCCESS)
1455 count = msg_list.nCount;
1457 count_info = msg_create_struct(MSG_STRUCT_COUNT_INFO);
1458 ret = msg_count_message(g_msg_handle, folder_id, count_info);
1459 if (ret != MSG_SUCCESS) {
1460 msg_release_struct(&count_info);
1464 ret = msg_get_int_value(count_info, MSG_COUNT_INFO_UNREAD_INT,
1466 if (ret != MSG_SUCCESS) {
1467 msg_release_struct(&count_info);
1471 if (unread_cnt != 0)
1476 msg_release_struct(&count_info);
1478 DBG("MaxlistCount %d \n", max);
1482 for (i = 0; i < msg_list.nCount; i++) {
1484 struct message_info msg_info;
1486 memset(&value, 0, sizeof(GValue));
1487 g_value_init(&value, DBUS_STRUCT_MESSAGE_LIST);
1488 g_value_take_boxed(&value, dbus_g_type_specialized_construct(
1489 DBUS_STRUCT_MESSAGE_LIST));
1491 msg_info = __bt_message_info_get(msg_list.msg_struct_info[i]);
1493 /* Keeping the bleow debug till stabilization is done. */
1496 DBG("msg_info.handle = %s\n", msg_info.handle);
1497 DBG("msg_info.subject = %s\n", msg_info.subject);
1498 DBG("msg_info.datetime = %s\n", msg_info.datetime);
1499 DBG("msg_info.sender_name = %s\n", msg_info.sender_name);
1500 DBG("msg_info.sender_addressing = %s\n", msg_info.sender_addressing);
1501 DBG("msg_info.replyto_addressing = %s\n", msg_info.replyto_addressing);
1502 DBG("msg_info.recipient_name = %s\n", msg_info.recipient_name);
1503 DBG("msg_info.recipient_addressing = %s\n",
1504 msg_info.recipient_addressing);
1505 DBG("msg_info.type = %s\n", msg_info.type);
1506 DBG("msg_info.reception_status = %s\n", msg_info.reception_status);
1507 DBG("msg_info.size = %s\n", msg_info.size);
1508 DBG("msg_info.attachment_size = %s\n", msg_info.attachment_size);
1509 DBG("msg_info.text = %d\n", msg_info.text);
1510 DBG("msg_info.read = %d\n", msg_info.read);
1511 DBG("msg_info.sent = %d\n", msg_info.sent);
1512 DBG("msg_info.protect = %d\n", msg_info.protect);
1513 DBG("msg_info.priority = %d\n", msg_info.priority);
1516 dbus_g_type_struct_set(&value, 0, msg_info.handle,
1517 1, msg_info.subject,
1518 2, msg_info.datetime,
1519 3, msg_info.sender_name,
1520 4, msg_info.sender_addressing,
1521 5, msg_info.recipient_name,
1522 6, msg_info.recipient_addressing,
1525 9, msg_info.reception_status,
1527 11, msg_info.attachment_size,
1528 12, msg_info.priority,
1531 15, msg_info.protect,
1532 16, msg_info.replyto_addressing, G_MAXUINT);
1533 g_ptr_array_add(array, g_value_get_boxed(&value));
1535 __bt_message_info_free(msg_info);
1538 #ifdef SUPPORT_EMAIL
1540 if (EMAIL_ERROR_NONE != email_load_default_account_id(&account_id)) {
1545 if (EMAIL_ERROR_NONE != email_get_mailbox_list(account_id,
1553 if (mailbox_list == NULL)
1556 for (i = 0; i < mailbox_count; i++) {
1557 DBG("mailbox alias = %s \n", mailbox_list[i].alias);
1558 if (!g_ascii_strncasecmp(mailbox_list[i].alias, folder,
1560 total = mailbox_list[i].total_mail_count_on_server;
1561 DBG("Total mail on sever:%d\n", total);
1562 DBG("mailbox name:%s\n", mailbox_list[i].mailbox_name);
1573 /* Need to modify the filter code, have to make it dynamic
1574 based on remote device request Also to check whether it needs
1575 to be done in agent or in obexd */
1577 filter_list = g_new0(email_list_filter_t, 3);
1578 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
1579 filter_list[0].list_filter_item.rule.target_attribute =
1580 EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
1581 filter_list[0].list_filter_item.rule.rule_type =
1582 EMAIL_LIST_FILTER_RULE_EQUAL;
1583 filter_list[0].list_filter_item.rule.key_value.integer_type_value =
1586 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
1587 filter_list[1].list_filter_item.operator_type =
1588 EMAIL_LIST_FILTER_OPERATOR_AND;
1590 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
1591 filter_list[2].list_filter_item.rule.target_attribute =
1592 EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME;
1593 filter_list[2].list_filter_item.rule.rule_type =
1594 EMAIL_LIST_FILTER_RULE_EQUAL;
1595 type = g_strdup(mailbox_list[i].mailbox_name);
1596 filter_list[2].list_filter_item.rule.key_value.string_type_value = type;
1597 filter_list[2].list_filter_item.rule.case_sensitivity = true;
1599 sorting_rule_list = g_new0(email_list_sorting_rule_t, 1);
1600 sorting_rule_list->target_attribute = EMAIL_MAIL_ATTRIBUTE_DATE_TIME;
1601 sorting_rule_list->sort_order = EMAIL_SORT_ORDER_ASCEND;
1603 ret = email_get_mail_list_ex(filter_list, 3,
1604 sorting_rule_list, 1, 0, total - 1,
1605 &mail_list, &mail_count);
1607 DBG("email API ret %d \n", ret);
1608 if (ret != EMAIL_ERROR_NONE) {
1611 g_free(filter_list);
1612 g_free(sorting_rule_list);
1618 for (i = 0; i < mail_count; ++i) {
1620 memset(&value, 0, sizeof(GValue));
1621 g_value_init(&value, DBUS_STRUCT_MESSAGE_LIST);
1622 g_value_take_boxed(&value, dbus_g_type_specialized_construct(
1623 DBUS_STRUCT_MESSAGE_LIST));
1625 uid = __bt_add_id(mail_list[i].mail_id);
1626 snprintf(msg_handle, sizeof(msg_handle), "%llx", uid);
1628 g_strlcpy(msg_type, "EMAIL", sizeof(msg_type));
1630 time = mail_list[i].date_time;
1631 __get_msg_timestamp(&time, msg_datetime);
1633 dbus_g_type_struct_set(&value, 0, msg_handle, 1, msg_type,
1634 2, msg_datetime, G_MAXUINT);
1635 g_ptr_array_add(array, g_value_get_boxed(&value));
1638 if (mailbox_list != NULL)
1639 email_free_mailbox(&mailbox_list, mailbox_count);
1640 if (mail_list != NULL)
1643 g_free(filter_list);
1644 g_free(sorting_rule_list);
1649 DBG("Request completed \n");
1650 dbus_g_method_return(context, newmsg, count, array);
1651 g_ptr_array_free(array, TRUE);
1652 DBG("Request completed successfully \n");
1656 g_ptr_array_free(array, TRUE);
1657 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
1659 dbus_g_method_return_error(context, error);
1660 g_error_free(error);
1664 static gboolean bluetooth_map_get_message(BluetoothMapAgent *agent,
1665 gchar *message_name,
1668 gboolean first_request,
1669 DBusGMethodInvocation *context)
1674 int msg_type = BT_SMS;
1676 GError *error = NULL;
1678 msg_struct_t msg = NULL;
1679 msg_struct_t send_opt = NULL;
1680 #ifdef SUPPORT_EMAIL
1681 email_mail_data_t *mail_data = NULL;
1683 message_id = __bt_get_uid(message_name);
1684 if (message_id == -1)
1687 DBG("message_id %d \n", message_id);
1688 DBG("msg_type %d \n", msg_type);
1689 DBG("attach %d \n", attach);
1690 DBG("transcode %d \n", transcode);
1691 DBG("first_request %d \n", first_request);
1693 if (msg_type == BT_SMS) {
1694 if (g_msg_handle == NULL)
1697 msg_error_t msg_err;
1699 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1700 send_opt = msg_create_struct(MSG_STRUCT_SENDOPT);
1702 msg_err = msg_get_message(g_msg_handle,
1703 (msg_message_id_t)message_id,
1705 if (msg_err != MSG_SUCCESS)
1708 buf = __bt_prepare_msg_bmseg(msg, attach, transcode);
1710 msg_release_struct(&msg);
1711 msg_release_struct(&send_opt);
1712 #ifdef SUPPORT_EMAIL
1713 } else if (msg_type == BT_EMAIL) {
1720 if (EMAIL_ERROR_NONE !=
1721 email_load_default_account_id(&account_id))
1724 if (EMAIL_ERROR_NONE !=
1725 email_get_mail_data(message_id, &mail_data))
1728 body_file = fopen(mail_data->file_path_plain, "r");
1729 if (body_file == NULL)
1730 body_file = fopen(mail_data->file_path_html, "rb");
1732 if (body_file != NULL) {
1733 fseek(body_file , 0, SEEK_END);
1734 email_size = ftell(body_file);
1737 buf = (char *)g_malloc0(sizeof(char) * email_size);
1739 read_size = fread(buf, 1, email_size, body_file);
1743 if (read_size != email_size)
1746 buf = (char *)g_strdup("");
1748 email_free_mail_data(&mail_data, 1);
1751 DBG("msg_type not supported %d \n", msg_type);
1755 dbus_g_method_return(context, FALSE, buf);
1765 msg_release_struct(&msg);
1768 msg_release_struct(&send_opt);
1770 #ifdef SUPPORT_EMAIL
1772 email_free_mail_data(&mail_data, 1);
1775 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
1777 dbus_g_method_return_error(context, error);
1778 g_error_free(error);
1782 static gboolean bluetooth_map_push_message(BluetoothMapAgent *agent,
1784 gboolean retry_send,
1787 DBusGMethodInvocation *context)
1790 GError *error = NULL;
1794 DBG("folder_name = %s\n", folder_name);
1796 folder_id = __bt_get_folder_id(folder_name);
1797 if (folder_id == -1)
1800 handle = __bt_add_id(-1);
1801 current_push_map_id = handle;
1803 /* FALSE : Keep messages in Sent folder */
1804 /* TRUE : don't keep messages in sent folder */
1805 opt.save_copy = save_copy;
1806 DBG("opt.save_copy = %d\n", opt.save_copy);
1808 /* FALSE : don't retry */
1810 opt.retry_send = retry_send;
1811 DBG("opt.retry_send = %d\n", opt.retry_send);
1813 /* FALSE : native */
1815 opt.native = native;
1816 DBG("opt.native = %d\n", opt.native);
1818 dbus_g_method_return(context, handle);
1822 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
1824 dbus_g_method_return_error(context, error);
1825 g_error_free(error);
1830 static gboolean bluetooth_map_push_message_data(BluetoothMapAgent *agent,
1832 DBusGMethodInvocation *context)
1837 char *folder = NULL;
1839 GSList *recepients = NULL;
1840 gboolean send = FALSE;
1842 GError *error = NULL;
1844 DBG("BMSG is \n %s", bmsg);
1846 struct bmsg_data *bmsg_info = NULL;
1848 bmsg_info = bmsg_parse(bmsg);
1852 folder = bmsg_get_msg_folder(bmsg_info);
1856 folder_id = __bt_get_folder_id(bmsg_info->folder);
1857 if (folder_id == -1)
1860 if (MSG_OUTBOX_ID == folder_id)
1863 body = bmsg_get_msg_body(bmsg_info);
1867 recepients = bmsg_get_msg_recepients(bmsg_info);
1869 id = __bt_push_sms(send, folder_id, body, recepients);
1873 __bt_update_id(current_push_map_id, id);
1878 g_slist_free(recepients);
1882 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
1884 dbus_g_method_return_error(context, error);
1885 g_error_free(error);
1890 dbus_g_method_return(context);
1895 static gboolean bluetooth_map_update_message(BluetoothMapAgent *agent,
1896 DBusGMethodInvocation *context)
1899 #ifdef SUPPORT_EMAIL
1901 err = email_sync_header_for_all_account(&handle);
1903 if (err == EMAIL_ERROR_NONE) {
1904 DBG("Handle to stop download = %d \n", handle);
1906 ERR("Message Update failed \n");
1909 dbus_g_method_return(context, err);
1910 return (err == EMAIL_ERROR_NONE) ? TRUE : FALSE;
1912 dbus_g_method_return(context, err);
1917 static gboolean bluetooth_map_set_read_status(BluetoothMapAgent *agent,
1919 gboolean read_status,
1920 DBusGMethodInvocation *context)
1923 int msg_type = BT_SMS;
1924 #ifdef SUPPORT_EMAIL
1925 email_mail_data_t *mail_data = NULL;
1927 GError *error = NULL;
1931 message_id = __bt_get_uid(handle);
1932 if (message_id == -1)
1935 DBG("message_id = %d, read_status = %d\n", message_id, read_status);
1937 if (msg_type == BT_SMS) {
1938 msg_error_t msg_err;
1939 msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1940 msg_struct_t send_opt = msg_create_struct(MSG_STRUCT_SENDOPT);
1943 msg_err = msg_get_message(g_msg_handle,
1944 (msg_message_id_t)message_id,
1946 if (msg_err != MSG_SUCCESS) {
1947 msg_release_struct(&msg);
1948 msg_release_struct(&send_opt);
1952 msg_err = msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT,
1954 if (msg_err != MSG_SUCCESS) {
1955 msg_release_struct(&msg);
1956 msg_release_struct(&send_opt);
1960 msg_err = msg_update_read_status(g_msg_handle, message_id,
1962 if (msg_err != MSG_SUCCESS) {
1963 msg_release_struct(&msg);
1964 msg_release_struct(&send_opt);
1968 if (__bt_msg_is_mms(msg_type)) {
1969 if (read_status == TRUE)
1970 msg_err = msg_mms_send_read_report(g_msg_handle,
1972 MSG_READ_REPORT_IS_READ);
1974 msg_err = msg_mms_send_read_report(g_msg_handle,
1976 MSG_READ_REPORT_NONE);
1979 msg_release_struct(&msg);
1980 msg_release_struct(&send_opt);
1982 if (msg_err != MSG_SUCCESS)
1984 #ifdef SUPPORT_EMAIL
1985 } else if (msg_type == BT_EMAIL) {
1987 if (email_get_mail_data(message_id, &mail_data) !=
1989 ERR("email_get_mail_data failed\n");
1993 if (email_set_flags_field(mail_data->account_id, &message_id, 1,
1994 EMAIL_FLAGS_SEEN_FIELD, read_status, 0) !=
1996 email_free_mail_data(&mail_data, 1);
2000 email_free_mail_data(&mail_data, 1);
2005 dbus_g_method_return(context);
2010 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
2012 dbus_g_method_return_error(context, error);
2013 g_error_free(error);
2018 static gboolean bluetooth_map_set_delete_status(BluetoothMapAgent *agent,
2020 gboolean delete_status,
2021 DBusGMethodInvocation *context)
2024 int msg_type = BT_SMS;
2025 #ifdef SUPPORT_EMAIL
2026 email_mail_data_t *mail_data = NULL;
2028 GError *error = NULL;
2032 message_id = __bt_get_uid(handle);
2033 if (message_id == -1)
2036 DBG("message_id = %d, delete_status = %d\n", message_id, delete_status);
2038 if (msg_type == BT_SMS) {
2039 if (msg_delete_message(g_msg_handle, message_id) !=
2043 #ifdef SUPPORT_EMAIL
2044 } else if (msg_type == BT_EMAIL) {
2046 if (email_get_mail_data(message_id, &mail_data) !=
2050 if (email_delete_mail(mail_data->mailbox_id, &message_id,
2051 1, 1) != EMAIL_ERROR_NONE) {
2052 email_free_mail_data(&mail_data, 1);
2056 email_free_mail_data(&mail_data, 1);
2061 dbus_g_method_return(context);
2066 error = __bt_map_agent_error(BT_MAP_AGENT_ERROR_INTERNAL,
2068 dbus_g_method_return_error(context, error);
2069 g_error_free(error);
2073 static gboolean bluetooth_map_noti_registration(BluetoothMapAgent *agent,
2076 DBusGMethodInvocation *context)
2078 DBG("remote_addr = %s \n", remote_addr);
2081 __bt_mns_client_connect(remote_addr);
2083 __bt_mns_client_disconnect();
2088 int main(int argc, char **argv)
2090 BluetoothMapAgent *bluetooth_map_obj = NULL;
2091 DBusGProxy *bus_proxy = NULL;
2093 GError *error = NULL;
2097 g_mainloop = g_main_loop_new(NULL, FALSE);
2099 if (g_mainloop == NULL) {
2100 ERR("Couldn't create GMainLoop\n");
2101 return EXIT_FAILURE;
2104 g_connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
2106 if (error != NULL) {
2107 ERR("Couldn't connect to system bus[%s]\n", error->message);
2108 g_error_free(error);
2109 return EXIT_FAILURE;
2112 bus_proxy = dbus_g_proxy_new_for_name(g_connection, DBUS_SERVICE_DBUS,
2114 DBUS_INTERFACE_DBUS);
2115 if (bus_proxy == NULL) {
2116 ERR("Failed to get a proxy for D-Bus\n");
2120 if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error, G_TYPE_STRING,
2121 BT_MAP_SERVICE_NAME, G_TYPE_UINT, 0,
2122 G_TYPE_INVALID, G_TYPE_UINT, &result,
2124 if (error != NULL) {
2125 ERR("RequestName RPC failed[%s]\n", error->message);
2126 g_error_free(error);
2130 DBG("result : %d %d\n", result, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
2131 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
2132 ERR("Failed to get the primary well-known name.\n");
2136 g_object_unref(bus_proxy);
2139 bluetooth_map_obj = g_object_new(BLUETOOTH_MAP_TYPE_AGENT, NULL);
2140 if (bluetooth_map_obj == NULL) {
2141 ERR("Failed to create one BluetoothMapAgent instance.\n");
2145 /* Registering it on the D-Bus */
2146 dbus_g_connection_register_g_object(g_connection,
2147 BT_MAP_SERVICE_OBJECT_PATH,
2148 G_OBJECT(bluetooth_map_obj));
2150 if (__bluetooth_map_start_service() == FALSE)
2153 g_main_loop_run(g_mainloop);
2156 DBG("Terminate the bluetooth-map-agent\n");
2158 __bt_remove_list(id_list);
2161 __bt_mns_client_disconnect();
2163 g_object_unref(bus_proxy);
2164 if (bluetooth_map_obj)
2165 g_object_unref(bluetooth_map_obj);
2167 dbus_g_connection_unref(g_connection);
2170 __bluetooth_map_stop_service();
2171 return EXIT_FAILURE;