Set locale based on LANG and LC_ALL environment variables set
[profile/ivi/lemolo.git] / dialer / gui.c
index 6bc3dc4..b181804 100644 (file)
@@ -8,6 +8,18 @@
 #include "keypad.h"
 #include "history.h"
 #include "callscreen.h"
+#include "ussd.h"
+#include "util.h"
+#include "simple-popup.h"
+#include "i18n.h"
+
+#ifdef HAVE_TIZEN
+#include <appcore-efl.h>
+#ifdef HAVE_UI_GADGET
+#include <ui-gadget.h>
+#endif
+#include <Ecore_X.h>
+#endif
 
 static Evas_Object *win = NULL;
 static Evas_Object *main_layout = NULL;
@@ -16,31 +28,73 @@ static Evas_Object *contacts = NULL;
 static Evas_Object *history = NULL;
 static Evas_Object *cs = NULL;
 static Evas_Object *flip = NULL;
-static char def_theme[PATH_MAX] = "";
+static Evas_Object *current_view = NULL;
+
+static OFono_Callback_List_Modem_Node *callback_node_modem_changed = NULL;
+static OFono_Callback_List_USSD_Notify_Node *callback_node_ussd_notify = NULL;
+static AMB_Callback_List_Node *callback_node_amb_properties_changed = NULL;
+static Locale_Callback_List_Node *callback_node_locale_properties_changed = NULL;
 
 /* XXX elm_flip should just do the right thing, but it does not */
 static Eina_Bool in_call = EINA_FALSE;
 static Eina_Bool in_flip_anim = EINA_FALSE;
 
+static Eina_Bool night_mode = EINA_FALSE;
+
 Contact_Info *gui_contact_search(const char *number, const char **type)
 {
        return contact_search(contacts, number, type);
 }
 
-Evas_Object *gui_layout_add(Evas_Object *parent, const char *style)
+static void _dial_reply(void *data, OFono_Error err,
+                       OFono_Call *call __UNUSED__)
 {
-       Evas_Object *layout = elm_layout_add(parent);
-       if (!elm_layout_theme_set(layout, "layout", "dialer", style)) {
-               CRITICAL("No theme for 'elm/layout/dialer/%s' at %s",
-                               style, def_theme);
-               evas_object_del(layout);
-               return NULL;
+       char *number = data;
+
+       if (err != OFONO_ERROR_NONE) {
+               char buf[1024];
+               const char *msg = ofono_error_message_get(err);
+               snprintf(buf, sizeof(buf), _("Could not call %s: %s"),
+                               number, msg);
+               gui_simple_popup(_("Error"), buf);
        }
-       return layout;
+
+       free(number);
+}
+
+OFono_Pending *gui_dial(const char *number)
+{
+       char *copy = strdup(number);
+       return ofono_dial(copy, NULL, _dial_reply, copy);
+}
+
+static void _gui_set_text(void)
+{
+       elm_object_part_text_set(cs, "label.hide-keypad", _("Hide Keypad"));
+       elm_object_part_text_set(cs, "label.answer", _("Answer"));
+       elm_object_part_text_set(cs, "label.hangup", _("Hangup"));
+       elm_object_part_text_set(cs, "label.multiparty", _("Conference"));
+       elm_object_part_text_set(cs, "label.multiparty-details-back", _("Back"));
+       elm_object_part_text_set(cs, "label.waiting", _("Waiting..."));
+       elm_object_part_text_set(cs, "elm.text.name", _("Unknown"));
+       elm_object_part_text_set(cs, "elm.text.waiting", _("Unknown"));
+
+       elm_object_part_text_set(keypad, "label.all", _("Decline"));
+       elm_object_part_text_set(keypad, "label.missed", _("Hold + Answer"));
+       elm_object_part_text_set(keypad, "label.clear", _("Hangup + Answer"));
+       elm_object_part_text_set(keypad, "label.edit,done", _("Redial"));
+
+       elm_object_part_text_set(history, "label.all", _("All"));
+       elm_object_part_text_set(history, "label.missed", _("Missed"));
+       elm_object_part_text_set(history, "label.clear", _("Clear"));
+       elm_object_part_text_set(history, "label.edit,done", _("Done"));
 }
 
 static void _gui_show(Evas_Object *o)
 {
+       Last_User_Mode *last;
+
+       last = util_get_last_user_mode();
        if (o == keypad)
                elm_object_signal_emit(main_layout, "show,keypad", "gui");
        else if (o == contacts)
@@ -48,28 +102,23 @@ static void _gui_show(Evas_Object *o)
        else if (o == history)
                elm_object_signal_emit(main_layout, "show,history", "gui");
        elm_object_focus_set(o, EINA_TRUE);
-}
+       current_view = o;
 
-static void _popup_close(void *data, Evas_Object *bt __UNUSED__, void *event __UNUSED__)
-{
-       Evas_Object *popup = data;
-       evas_object_del(popup);
+       if (last) {
+               if (current_view == keypad)
+                       last->last_view = DIALER_LAST_VIEW_KEYPAD;
+               else if (current_view == contacts)
+                       last->last_view = DIALER_LAST_VIEW_CONTACTS;
+               else if (current_view == history)
+                       last->last_view = DIALER_LAST_VIEW_HISTORY;
+
+               util_set_last_user_mode(last);
+       }
 }
 
-void gui_simple_popup(const char *title, const char *message)
+Evas_Object *gui_simple_popup(const char *title, const char *message)
 {
-       Evas_Object *p = elm_popup_add(win);
-       Evas_Object *bt;
-
-       evas_object_size_hint_weight_set(p, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
-       elm_object_text_set(p, message);
-       elm_object_part_text_set(p, "title,text", title);
-
-       bt = elm_button_add(p);
-       elm_object_text_set(bt, "Close");
-       elm_object_part_content_set(p, "button1", bt);
-       evas_object_smart_callback_add(bt, "clicked", _popup_close, p);
-       evas_object_show(p);
+       return simple_popup_add(win, title, message);
 }
 
 void gui_activate(void)
@@ -77,6 +126,11 @@ void gui_activate(void)
        elm_win_raise(win);
        elm_win_activate(win);
        evas_object_show(win);
+
+       /* Automatically enable hfp modem if app is in the foreground */
+       if (!ofono_voice_is_online()) {
+               ofono_powered_set(EINA_TRUE, NULL, NULL);
+       }
 }
 
 void gui_number_set(const char *number, Eina_Bool auto_dial)
@@ -99,6 +153,12 @@ void gui_activecall_set(Evas_Object *o)
        }
 }
 
+void gui_contacts_show(void)
+{
+       _gui_show(contacts);
+       gui_call_exit();
+}
+
 void gui_call_enter(void)
 {
        gui_activate();
@@ -137,6 +197,26 @@ static void _gui_call_sync(void *data __UNUSED__, Evas_Object *o __UNUSED__,
        in_flip_anim = EINA_FALSE;
 }
 
+static void _gui_voicemail(void)
+{
+       const char *number = ofono_voicemail_number_get();
+       EINA_SAFETY_ON_NULL_RETURN(number);
+       EINA_SAFETY_ON_FALSE_RETURN(*number != '\0');
+
+       gui_dial(number);
+}
+
+static void _gui_quit(void)
+{
+       elm_exit();
+}
+
+static void _on_del(void *data __UNUSED__, Evas *e __UNUSED__,
+                        Evas_Object *obj __UNUSED__, void *event __UNUSED__)
+{
+       _gui_quit();
+}
+
 static void _on_clicked(void *data __UNUSED__, Evas_Object *o __UNUSED__,
                        const char *emission, const char *source __UNUSED__)
 {
@@ -151,44 +231,159 @@ static void _on_clicked(void *data __UNUSED__, Evas_Object *o __UNUSED__,
                _gui_show(contacts);
        else if (strcmp(emission, "history") == 0)
                _gui_show(history);
+       else if (strcmp(emission, "voicemail") == 0)
+               _gui_voicemail();
+       else if (strcmp(emission, "quit") == 0)
+               _gui_quit();
+}
+
+static void _ofono_changed(void *data __UNUSED__)
+{
+       const char *number;
+       Eina_Bool waiting;
+       unsigned char count;
+       char buf[32];
+
+       if ((ofono_modem_api_get() & OFONO_API_MSG_WAITING) == 0) {
+               elm_object_signal_emit(main_layout, "disable,voicemail", "gui");
+               elm_object_signal_emit(main_layout,
+                                       "toggle,off,voicemail", "gui");
+               elm_object_part_text_set(main_layout, "elm.text.voicemail", "");
+               return;
+       }
+
+       number = ofono_voicemail_number_get();
+       waiting = ofono_voicemail_waiting_get();
+       count = ofono_voicemail_count_get();
+
+       if (number)
+               elm_object_signal_emit(main_layout, "enable,voicemail", "gui");
+       else
+               elm_object_signal_emit(main_layout, "disable,voicemail", "gui");
+
+       if (waiting) {
+               if (count == 0)
+                       snprintf(buf, sizeof(buf), "*");
+               else if (count < 255)
+                       snprintf(buf, sizeof(buf), "%d", count);
+               else
+                       snprintf(buf, sizeof(buf), "255+");
+
+               elm_object_signal_emit(main_layout,
+                                       "toggle,on,voicemail", "gui");
+       } else {
+               buf[0] = '\0';
+               elm_object_signal_emit(main_layout,
+                                       "toggle,off,voicemail", "gui");
+       }
+
+       elm_object_part_text_set(main_layout, "elm.text.voicemail", buf);
+}
+
+static void _amb_properties_changed(void *data __UNUSED__)
+{
+       Eina_Bool night_mode_changed = amb_night_mode_get();
+       if (night_mode != night_mode_changed) {
+               DBG("Night mode changed to %d", night_mode_changed);
+               night_mode = night_mode_changed;
+               util_set_night_mode(night_mode);
+
+               if (current_view)
+                       _gui_show(current_view);
+       }
+}
+
+static void _locale_properties_changed(void *data __UNUSED__)
+{
+       const char *new_lang = locale_lang_get();
+       if (new_lang) {
+               if (strncmp(new_lang, "LANG=", 5) == 0)
+                       new_lang += 5;
+
+               DBG("Switching UI language to %s", new_lang);
+               setenv ("LANG", new_lang, 1);
+               setlocale (LC_ALL, new_lang);
+               _gui_set_text();
+       }
+}
+
+static void _ofono_ussd_notify(void *data __UNUSED__, Eina_Bool needs_reply,
+                               const char *message)
+{
+       DBG("needs_reply=%d, message=%s", needs_reply, message);
+       ussd_start(message);
+}
+
+static void _on_contacts_selected(void *data __UNUSED__,
+                                       Evas_Object *obj __UNUSED__,
+                                       void *event_info)
+{
+       const char *number = event_info;
+       DBG("Contact selected: %s", number);
+       gui_dial(number);
+}
+
+static void _load_last_user_view() {
+       Last_User_Mode *last;
+
+       last = util_get_last_user_mode();
+       if (!last) {
+               DBG("Last user config file not found - create default view");
+               last = calloc(1, sizeof(Last_User_Mode));
+               last->last_view = DIALER_LAST_VIEW_KEYPAD;
+               last->last_history_view = DIALER_LAST_HISTORY_VIEW_ALL;
+               last->last_number = eina_stringshare_add("");
+               util_set_last_user_mode(last);
+       }
+
+       if(last->last_number) {
+               keypad_number_set(keypad, last->last_number, EINA_FALSE);
+       }
+       if (last->last_view == DIALER_LAST_VIEW_KEYPAD) {
+               _gui_show(keypad);
+       } else if (last->last_view == DIALER_LAST_VIEW_CONTACTS) {
+               _gui_show(contacts);
+       } else if (last->last_view == DIALER_LAST_VIEW_HISTORY) {
+               _gui_show(history);
+               if (last->last_history_view != DIALER_LAST_HISTORY_VIEW_ALL) {
+                       elm_object_signal_emit(history, "show,missed", "gui");
+               }
+       }
+       eina_stringshare_del(last->last_number);
+       free(last);
 }
 
-Eina_Bool gui_init(const char *theme)
+Eina_Bool gui_init(void)
 {
        Evas_Object *lay, *obj;
        Evas_Coord w, h;
+       const char *lang;
 
        /* dialer should never, ever quit */
-       elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_NONE);
-
-       elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
-       elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
-       elm_app_info_set(gui_init, "ofono-efl", "themes/default.edj");
-
-       snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
-                       elm_app_data_dir_get());
-
-       elm_theme_extension_add(NULL, def_theme);
-       if (!theme)
-               elm_theme_overlay_add(NULL, def_theme);
-       else {
-               char tmp[PATH_MAX];
-               if (theme[0] != '/') {
-                       if (eina_str_has_suffix(theme, ".edj")) {
-                               snprintf(tmp, sizeof(tmp), "%s/themes/%s",
-                                               elm_app_data_dir_get(), theme);
-                       } else {
-                               snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
-                                               elm_app_data_dir_get(), theme);
-                       }
-                       theme = tmp;
-               }
-               elm_theme_overlay_add(NULL, theme);
-       }
+       elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
 
        win = elm_win_util_standard_add("ofono-dialer", "oFono Dialer");
        EINA_SAFETY_ON_NULL_RETURN_VAL(win, EINA_FALSE);
-       elm_win_autodel_set(win, EINA_FALSE);
+       elm_win_autodel_set(win, EINA_TRUE);
+       evas_object_event_callback_add(win, EVAS_CALLBACK_DEL, _on_del, NULL);
+
+#ifdef HAVE_TIZEN
+#ifdef HAVE_UI_GADGET
+       UG_INIT_EFL(win, UG_OPT_INDICATOR_PORTRAIT_ONLY);
+#endif
+#endif
+
+       /*
+        * check LANG and LC_ALL environment variables,
+        * LC_ALL takes precedence
+        */
+       lang = getenv("LC_ALL");
+       if (!lang)
+               lang = getenv("LANG");
+
+       setlocale(LC_ALL, lang);
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
 
        flip = elm_flip_add(win);
        evas_object_size_hint_weight_set(flip,
@@ -199,7 +394,7 @@ Eina_Bool gui_init(const char *theme)
                                        _gui_call_sync, NULL);
        evas_object_show(flip);
 
-       main_layout = lay = gui_layout_add(win, "main");
+       main_layout = lay = layout_add(win, "dialer");
        EINA_SAFETY_ON_NULL_RETURN_VAL(lay, EINA_FALSE);
        evas_object_size_hint_weight_set(lay,
                                EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
@@ -215,6 +410,8 @@ Eina_Bool gui_init(const char *theme)
        elm_object_part_content_set(lay, "elm.swallow.keypad", obj);
 
        contacts = obj = contacts_add(win);
+       evas_object_smart_callback_add(contacts, "selected",
+                                       _on_contacts_selected, NULL);
        EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
        elm_object_part_content_set(lay, "elm.swallow.contacts", obj);
 
@@ -222,8 +419,6 @@ Eina_Bool gui_init(const char *theme)
        EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
        elm_object_part_content_set(lay, "elm.swallow.history", obj);
 
-       _gui_show(keypad);
-
        cs = obj = callscreen_add(win);
        EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
        evas_object_size_hint_weight_set(obj,
@@ -232,6 +427,18 @@ Eina_Bool gui_init(const char *theme)
        elm_object_part_content_set(flip, "back", obj);
        evas_object_show(obj);
 
+       _gui_set_text();
+       _load_last_user_view();
+
+       callback_node_modem_changed =
+               ofono_modem_changed_cb_add(_ofono_changed, NULL);
+       callback_node_ussd_notify =
+               ofono_ussd_notify_cb_add(_ofono_ussd_notify, NULL);
+       callback_node_amb_properties_changed =
+               amb_properties_changed_cb_add(_amb_properties_changed, NULL);
+       callback_node_locale_properties_changed =
+               locale_properties_changed_cb_add(_locale_properties_changed, NULL);
+
        /* TODO: make it match better with Tizen: icon and other properties */
        obj = elm_layout_edje_get(lay);
        edje_object_size_min_get(obj, &w, &h);