summaryrefslogtreecommitdiff
path: root/viewer/viewer.c
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/viewer.c')
-rwxr-xr-xviewer/viewer.c453
1 files changed, 453 insertions, 0 deletions
diff --git a/viewer/viewer.c b/viewer/viewer.c
new file mode 100755
index 0000000..d31b570
--- /dev/null
+++ b/viewer/viewer.c
@@ -0,0 +1,453 @@
+/*
+ * Copyright 2012 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.tizenopensource.org/license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <aul.h>
+#include <Ecore_X.h>
+#include <ui-gadget.h>
+#include <contacts-svc.h>
+#include <app.h>
+
+#include "phone.h"
+#include "ct-detail.h"
+#include "ct-list.h"
+
+#if !defined(CV_PACKAGE)
+# define CV_PACKAGE "contacts-viewer"
+#endif
+
+typedef struct {
+ char *path;
+ Evas_Object *win;
+ Evas_Object *content;
+ Evas_Object *popup;
+ Eina_List *list;
+ ph_progress_info *p_info;
+} ct_viewer_data;
+
+// for removing build warning ////////////////////////////////////
+int contacts_svc_normalize_str(const char *src, char *dest, const int dest_len);
+//////////////////////////////////////////////////////////////
+
+static void cv_win_del(void *data, Evas_Object *obj, void *event)
+{
+ elm_exit();
+}
+
+static Evas_Object* cv_create_win(const char *name)
+{
+ Evas_Object *eo;
+ int w, h;
+
+ eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
+ if (eo) {
+ elm_win_title_set(eo, name);
+ elm_win_borderless_set(eo, EINA_TRUE);
+ evas_object_smart_callback_add(eo, "delete,request", cv_win_del, NULL);
+ ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
+ evas_object_resize(eo, w, h);
+ elm_win_indicator_mode_set(eo, ELM_WIN_INDICATOR_SHOW);
+ }
+
+ return eo;
+}
+
+static bool viewer_create(void *data)
+{
+ ct_viewer_data *cviewer_d = data;
+ Evas_Object *win;
+
+ contacts_svc_connect();
+
+ /* create window */
+ win = cv_create_win(CV_PACKAGE);
+ p_retvm_if(NULL == win, false, "ctapp_create_win() Failed");
+ cviewer_d->win = win;
+
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ evas_object_show(win);
+
+ UG_INIT_EFL(cviewer_d->win, UG_OPT_INDICATOR_PORTRAIT_ONLY);
+
+ return true;
+}
+
+static void viewer_terminate(void *data)
+{
+ int ret;
+ ct_viewer_data *cviewer_d = data;
+
+ if (cviewer_d->p_info) {
+ cviewer_d->p_info->alive = false;
+ pthread_join(cviewer_d->p_info->thread, NULL);
+ ecore_timer_del(cviewer_d->p_info->timer);
+ free(cviewer_d->p_info);
+ }
+
+ free(cviewer_d->path);
+
+ if (cviewer_d->win)
+ evas_object_del(cviewer_d->win);
+
+ ret = contacts_svc_disconnect();
+ p_warn_if(CTS_SUCCESS != ret, "contacts_svc_disconnect() Failed(%d)", ret);
+
+ return;
+}
+
+static void viewer_pause(void *data)
+{
+ //ct_viewer_data *ad = data;
+ return;
+}
+
+static void viewer_resume(void *data)
+{
+ //ct_viewer_data *ad = data;
+ return;
+}
+
+static int viewer_vcard_foreach_cb(const char *vcard_stream, void *data)
+{
+ int ret;
+ CTSstruct *contact;
+ CTSvalue *value;
+ ct_viewer_data *cviewer_d = data;
+ ct_contact_list *info;
+ ph_progress_info *p_info = cviewer_d->p_info;
+ char display[PH_TEXT_MAX_LEN];
+
+ if (!p_info->alive) {
+ p_info->result = FALSE;
+ p_info->completed = true;
+ return CTS_ERR_FAIL;
+ }
+
+ p_info->cnt++;
+ ret = contacts_svc_get_contact_from_vcard(vcard_stream, &contact);
+ if (ret < CTS_SUCCESS) {
+ ERR("contacts_svc_get_contact_from_vcard() Failed(%d)", ret);
+ p_info->result = FALSE;
+ if (p_info->cnt == p_info->cnt_checked_total)
+ p_info->completed = true;
+ return ret;
+ }
+
+ info = calloc(1, sizeof(ct_contact_list));
+ if (NULL == info) {
+ ERR("calloc() return NULL");
+ p_info->result = FALSE;
+ if (p_info->cnt == p_info->cnt_checked_total)
+ p_info->completed = true;
+ return CTS_ERR_FAIL;
+ }
+
+ info->id = p_info->cnt;
+
+ ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &value);
+ if (CTS_SUCCESS != ret) {
+ ERR("contacts_svc_struct_get_value() Failed(%d)", ret);
+ p_info->result = FALSE;
+ if (p_info->cnt == p_info->cnt_checked_total)
+ p_info->completed = true;
+ return CTS_ERR_FAIL;
+ }
+
+ ret = ctui_get_display_name(value, display, sizeof(display));
+ if (ret) {
+ info->display = strdup(display);
+ contacts_svc_normalize_str(info->display, display, sizeof(display));
+ info->normalize = strdup(display);
+ }
+ info->id_ab = 0;
+ info->img_loaded = true;
+ cviewer_d->list = eina_list_append(cviewer_d->list, info);
+
+ contacts_svc_struct_free(contact);
+
+ if (p_info->cnt == p_info->cnt_checked_total)
+ p_info->completed = true;
+
+ return CTS_SUCCESS;
+}
+
+static PTHREAD_FN viewer_load_vcard_contacts_thread_fn(void* data)
+{
+ ct_viewer_data *cviewer_d = data;
+ contacts_svc_vcard_foreach(cviewer_d->path, viewer_vcard_foreach_cb, cviewer_d);
+ pthread_exit(NULL);
+}
+
+static inline void viewer_get_filename(char *fullpath, char *dest, int size_dest)
+{
+ char* index;
+ index = strrchr(fullpath, '/');
+ snprintf(dest, size_dest, "%s", index + 1);
+}
+
+static Eina_Bool viewer_load_vcard_timer_cb(void *data)
+{
+ ct_viewer_data *cviewer_d = data;
+ ph_progress_info *p_info;
+
+ p_retvm_if(NULL == cviewer_d , ECORE_CALLBACK_CANCEL, "parameter(ct_viewer_data) is NULL");
+ p_info = cviewer_d->p_info;
+
+ if (!p_info->completed) {
+ char count[PH_TEXT_SHORT_LEN];
+ char percent[PH_TEXT_SHORT_LEN];
+ double value = (double)p_info->cnt / (double)p_info->cnt_checked_total;
+
+ if (!p_info->alive) {
+ p_info->result = FALSE;
+ p_info->completed = true;
+ return ECORE_CALLBACK_RENEW;
+ }
+ elm_progressbar_value_set(p_info->progressbar, value);
+ snprintf(percent, sizeof(percent), "%d%%", (int)(100.0 * (double)p_info->cnt/(double)p_info->cnt_checked_total));
+ snprintf(count, sizeof(count), "%d/%d", p_info->cnt, p_info->cnt_checked_total);
+ edje_object_part_text_set(elm_layout_edje_get(p_info->layout), "elm.text.subtext1", percent);
+ edje_object_part_text_set(elm_layout_edje_get(p_info->layout), "elm.text.subtext2", count);
+
+ if (p_info->cnt == p_info->cnt_checked_total)
+ p_info->completed = true;
+
+ return ECORE_CALLBACK_RENEW;
+ }
+ else {
+ pthread_join(p_info->thread, NULL);
+ evas_object_del(cviewer_d->popup);
+ cviewer_d->popup = NULL;
+
+ if (!p_info->result) {
+ elm_exit();
+ return ECORE_CALLBACK_CANCEL;
+ }
+
+ free(p_info);
+ cviewer_d->p_info = NULL;
+ cviewer_d->content = ctui_create_vcard_list_view(cviewer_d->win, cviewer_d->list, cviewer_d->path);
+ p_retvm_if(NULL == cviewer_d->content, ECORE_CALLBACK_CANCEL, "ctui_create_vcard_list_view() return NULL");
+
+ return ECORE_CALLBACK_CANCEL;
+ }
+}
+
+static int viewer_load_vcard_contact(ct_viewer_data *cviewer_d)
+{
+ int ret;
+ FILE *fp;
+ char vcard_stream[1024*100];
+
+ fp = fopen(cviewer_d->path, "r");
+ if (NULL == fp) {
+ ERR("fopen() return NULL");
+ elm_exit();
+ return -1;
+ }
+ ret = fread(vcard_stream, 1, sizeof(vcard_stream), fp);
+ if (0 < ret && ret < sizeof(vcard_stream)) {
+ vcard_stream[ret] = '\0';
+ cviewer_d->content = ctui_contacts_viewer(cviewer_d->win, NULL, 0, vcard_stream);
+ if (NULL == cviewer_d->content) {
+ ERR("ctui_contacts_viewer() return NULL");
+ fclose(fp);
+ elm_exit();
+ return -1;
+ }
+ }
+ else {
+ Evas_Object *popup;
+ ERR("vcard is invalid(%d)", ret);
+ popup = phone_show_popup(cviewer_d->win, S_(CT_SYS_POP_ERROR), 1.5);
+ evas_object_smart_callback_add(popup, "timeout", cv_win_del, NULL);
+ }
+ fclose(fp);
+ return 0;
+}
+
+static int viewer_load_vcard_contacts(ct_viewer_data *cviewer_d, int cnt_vcard)
+{
+ int ret;
+ char title[PH_TEXT_MAX_LEN];
+ char filepath[PH_TEXT_MAX_LEN];
+ ph_progress_info *p_info;
+
+ p_info = calloc(1, sizeof(ph_progress_info));
+ if (NULL == p_info) {
+ ERR("calloc() return NULL");
+ elm_exit();
+ return -1;
+ }
+
+ cviewer_d->p_info = p_info;
+ cviewer_d->list = NULL;
+
+ p_info->cnt = 0;
+ p_info->cnt_checked_total = cnt_vcard;
+ p_info->alive = true;
+ p_info->result = TRUE;
+ p_info->completed = false;
+
+ viewer_get_filename(cviewer_d->path, filepath, sizeof(filepath));
+ snprintf(title, sizeof(title), "%s<br>%s", T_(CT_GET_TEXT_BASIC, CTTEXT_LOADING_VCARD), filepath);
+
+ ret = pthread_create(&p_info->thread, NULL, viewer_load_vcard_contacts_thread_fn, cviewer_d);
+ if (0 != ret) {
+ ERR("Thread creation failed(%d)", ret);
+ free(p_info);
+ cviewer_d->p_info = NULL;
+ elm_exit();
+ return -1;
+ }
+ p_info->timer = ecore_timer_add(0.1, viewer_load_vcard_timer_cb, cviewer_d);
+ if (NULL == p_info->timer) {
+ ERR("ecore_timer_add() return NULL");
+
+ p_info->alive = false;
+ pthread_join(p_info->thread, NULL);
+
+ free(p_info);
+ cviewer_d->p_info = NULL;
+ elm_exit();
+ return -1;
+ }
+ cviewer_d->popup = phone_progressbar_popup(cviewer_d->win, p_info, title);
+ return 0;
+}
+
+static void viewer_popup_hide_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ elm_exit();
+}
+
+
+static void viewer_service(service_h service, void *data)
+{
+ int ret;
+ int ct_id;
+ char *val = NULL;
+ char val_vcard[PH_TEXT_MAX_LEN];
+ ct_viewer_data *cviewer_d = data;
+
+ if (NULL == cviewer_d->win) {
+ ERR("win is NULL");
+ elm_exit();
+ return;
+ }
+
+ if (cviewer_d->content)
+ evas_object_del(cviewer_d->content);
+
+ ret = service_get_extra_data(service, "ct_id", &val);
+ p_warn_if(SERVICE_ERROR_NONE != ret, "service_get_operation is failed(%d)", ret);
+
+ if (val && *val) {
+ ct_id = atoi(val);
+ if (ct_id <= 0) {
+ ERR("service value(ct_id) is wrong(%d)", ct_id);
+ }
+ else {
+ cviewer_d->content = ctui_contacts_viewer(cviewer_d->win, NULL, ct_id, NULL);
+ if (NULL == cviewer_d->content) {
+ Evas_Object *popup;
+ ERR("ctui_contacts_viewer() return NULL");
+ popup = elm_popup_add(cviewer_d->win);
+ evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+ elm_object_text_set(popup, S_(PH_SYS_POP_FAILED));
+ elm_popup_timeout_set(popup, 2.0);
+ evas_object_smart_callback_add(popup, "timeout", viewer_popup_hide_cb, NULL);
+
+ evas_object_show(popup);
+ }
+ free(val);
+ }
+ }
+ else {
+ int cnt_vcard = 0;
+ ret = service_get_uri(service, &val);
+ p_warn_if(SERVICE_ERROR_NONE != ret, "service_get_uri is failed(%d)", ret);
+
+ val_vcard[0] = '\0';
+
+ if (val && *val) {
+ if (0 == strncmp(val, "file://", 7))
+ snprintf(val_vcard, sizeof(val_vcard), "%s", val+7);
+ else
+ snprintf(val_vcard, sizeof(val_vcard), "%s", val);
+ }
+ else {
+ ret = service_get_extra_data(service, AUL_K_MIME_CONTENT, &val);
+ p_warn_if(SERVICE_ERROR_NONE != ret, "service_get_extra_data is failed(%d)", ret);
+ if (val && *val)
+ snprintf(val_vcard, sizeof(val_vcard), "%s", val);
+ }
+ free(val);
+
+ if (NULL == val_vcard || '\0' == *val_vcard) {
+ ERR("service value is NULL");
+ elm_exit();
+ return;
+ }
+
+ free(cviewer_d->path);
+ cviewer_d->path = strdup(val_vcard);
+ cnt_vcard = contacts_svc_vcard_count(cviewer_d->path);
+
+ if (cnt_vcard < 1) {
+ ERR("invalid vcard(%s)", cviewer_d->path);
+ elm_exit();
+ return;
+ }
+ else if (1 == cnt_vcard) {
+ ret = viewer_load_vcard_contact(cviewer_d);
+ if (0 != ret) {
+ ERR("viewer_load_vcard_contact() Failed(%d)", ret);
+ elm_exit();
+ return;
+ }
+ }
+ else {
+ ret = viewer_load_vcard_contacts(cviewer_d, cnt_vcard);
+ if (0 != ret) {
+ ERR("viewer_load_vcard_contact() Failed(%d)", ret);
+ elm_exit();
+ return;
+ }
+ }
+ }
+ elm_win_activate(cviewer_d->win);
+ return;
+}
+
+API int main(int argc, char *argv[])
+{
+ ct_viewer_data ad = {0};
+ app_event_callback_s event_callback = {0,};
+
+ event_callback.create = viewer_create;
+ event_callback.terminate = viewer_terminate;
+ event_callback.pause = viewer_pause;
+ event_callback.resume = viewer_resume;
+ event_callback.service = viewer_service;
+ event_callback.low_memory = NULL;
+ event_callback.low_battery = NULL;
+ event_callback.device_orientation = NULL;
+ event_callback.language_changed = NULL;
+ event_callback.region_format_changed = NULL;
+
+ return app_efl_main(&argc, &argv, &event_callback, &ad);
+}