2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <Elementary.h>
27 #include <ui-gadget-module.h>
28 #include <ui-gadget.h>
31 #include <vconf-keys.h>
33 #include "image-viewer.h"
35 #define PACKAGE "image-viewer"
36 #define LOCALEDIR "/opt/apps/com.image-viewer/res/locale"
38 #define UG_MODE_SINGLE "SINGLE"
39 #define UG_STANDALONE_VALUE "TRUE"
41 #define UG_BUNDLE_KEY_VIEW_MODE "View Mode"
42 #define UG_BUNDLE_KEY_PATH "Path"
43 #define UG_BUNDLE_KEY_SETAS_TYPE "Setas type"
44 #define UG_BUNDLE_KEY_STANDALONE "Standalone"
46 #define SERVICE_OPERATION_CROP "http://tizen.org/appsvc/operation/crop"
48 static const char *_conver_error(int err)
52 case SERVICE_ERROR_NONE:
54 case SERVICE_ERROR_INVALID_PARAMETER:
55 return "Invalid parameter";
56 case SERVICE_ERROR_KEY_NOT_FOUND:
57 return "Specified key not found";
58 case SERVICE_ERROR_OUT_OF_MEMORY:
59 return "Out of memory";
60 case SERVICE_ERROR_INVALID_DATA_TYPE:
61 return "Invalid data type";
64 static char error[128];
65 sprintf(error, "Unknow Error : %d(0x%08x)", err, err);
72 void _orient_changed(app_device_orientation_e orientation, void *user_data);
76 * @brief The win delete function
82 static void _win_del(void *data, Evas_Object *obj, void *event_info)
89 * @brief Create the main window
91 * @param name The title of the window
93 * @return the win on sucess
95 static Evas_Object* _create_win(const char *name)
97 iv_retv_if(name == NULL, NULL);
101 eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
104 elm_win_title_set(eo, name);
105 elm_win_borderless_set(eo, EINA_TRUE);
106 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
107 ecore_x_window_size_get(ecore_x_window_root_first_get(),
109 evas_object_resize(eo, w, h);
116 * @brief Create the background
118 * @param parent object
120 static Evas_Object* _create_bgimg(Evas_Object* parent)
122 Evas_Object *bg = elm_bg_add(parent);
123 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
124 evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
126 elm_win_resize_object_add(parent, bg);
128 evas_object_show(bg);
134 * @brief The layout callback after ug created
136 * @param ug The ug created
137 * @param mode The UG mode, FULLVIEW or FRAMEVIEW
138 * @param priv The privite data
140 void _ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void* priv)
142 IV_MSG_HIGH("ug Layout CB. UG=0x%08x Priv=0x%08x Mode=%d", ug, priv, mode);
144 Evas_Object *base, *win;
148 IV_MSG_ERROR("Abnormal value. UG=0x%08x Priv=0x%08x Mode=%d", ug, priv, mode);
152 appdata_iv *ap = (appdata_iv *)priv;
154 base = ug_get_layout(ug);
157 IV_MSG_HIGH( "ug_get_layout is fail");
160 ap->ug_layout = base;
162 win = ug_get_window();
166 case UG_MODE_FULLVIEW:
167 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
168 elm_win_resize_object_add(win, base);
169 ug_disable_effect(ug);
170 evas_object_show(base);
173 IV_MSG_HIGH("Unknow UG mode : %d", mode);
180 * @brief The return callback on ug send result
182 * @param ug The ug created
183 * @param result The result received from ug
184 * @param priv The privite data
186 void _ug_result_cb(ui_gadget_h ug, service_h reply, void *priv)
188 IV_MSG_HIGH("UG Result CB.");
192 IV_MSG_ERROR("Abnormal value. UG=0x%08x Priv=0x%08x", ug, priv);
196 appdata_iv *ap = (appdata_iv *)priv;
198 int ret = service_reply_to_launch_request(reply, ap->service_handle, SERVICE_RESULT_SUCCEEDED);
199 if (ret != SERVICE_ERROR_NONE)
201 IV_MSG_ERROR("service_reply_to_launch_request failed! [%s]", _conver_error(ret));
206 * @brief The close callback on ug destroy
208 * @param ug The ug created
209 * @param priv The privite data
211 void _ug_closed_cb(ui_gadget_h ug, void *priv)
213 IV_MSG_HIGH("UG Destroyed..");
217 IV_MSG_ERROR("Abnormal value. UG=0x%08x Priv=0x%08x", ug, priv);
221 //appdata_iv *ap = (appdata_iv *)priv;
223 elm_exit(); // will tirgger app_terminated
227 * @brief Create the image-viewer-ug
229 * @param data The app data
231 * @return ug on sucess, NULL on fail
233 ui_gadget_h _create_ug(void* data)
235 iv_retv_if(data == NULL, NULL);
237 appdata_iv *ap = (appdata_iv *)data;
240 struct ug_cbs cbs = {0,};
242 cbs.layout_cb = _ug_layout_cb;
243 cbs.result_cb = _ug_result_cb;
244 cbs.destroy_cb = _ug_closed_cb;
247 ug = ug_create(NULL, "image-viewer-efl", UG_MODE_FULLVIEW, ap->service_handle, &cbs);
249 IV_MSG_HIGH("UG Created!");
253 static bool _is_rotation_lock(void)
258 ret = vconf_get_bool(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, &lock);
260 if ( ret != 0) // Failed
262 IV_MSG_HIGH( "Rotation locked state is [%d].", lock);
268 IV_MSG_HIGH("Rotation is locked");
275 static void _on_portrait_lock_changed(keynode_t* node, void *user_data)
277 IV_MSG_HIGH("State changed. %s=%d", vconf_keynode_get_name(node), vconf_keynode_get_bool(node));
279 if ( vconf_keynode_get_bool(node) == true )
281 // When Set portrait lock, Set degree as 0.
282 _orient_changed(APP_DEVICE_ORIENTATION_0, user_data);
286 static bool _app_create(void *user_data)
288 // Hook to take necessary actions before main event loop starts
289 // Initialize UI resources and application's data
290 // If this function returns true, the main loop of application starts
291 // If this function returns false, the application is terminated
292 IV_ASSERT(user_data != NULL);
294 IV_MSG_HIGH("App Create");
296 elm_config_preferred_engine_set("opengl_x11"); //enabling the OpenGL as the backend of the EFL.
298 appdata_iv *ap = (appdata_iv *) user_data;
300 Evas_Object *win = _create_win(PACKAGE);
303 IV_MSG_ERROR("Cannot create app. pkg=%s", PACKAGE);
309 UG_INIT_EFL(ap->win_main, UG_OPT_INDICATOR_ENABLE); // Init UG module
311 ap->bg = _create_bgimg(ap->win_main);
312 if ( ap->bg == NULL )
314 IV_MSG_ERROR("Cannot create bg");
320 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL, _on_portrait_lock_changed, user_data);
323 IV_MSG_ERROR("Register %s is failed", VCONFKEY_SETAPPL_ROTATE_LOCK_BOOL);
327 _orient_changed(app_get_device_orientation(), user_data); // Set initial degree
332 bool _iter_extra_data_cb(service_h service, const char *key, void *user_data)
336 IV_MSG_HIGH(" Key=%s Value=%s", key, value);
338 int ret = service_get_extra_data(service, key, &value);
339 if (ret != SERVICE_ERROR_NONE)
341 IV_MSG_ERROR("service_get_extra_data %s failed, [%s]", key, _conver_error(ret));
351 static void _app_service(service_h service, void *user_data)
353 // How to get Alarm service????
354 appdata_iv *ap = (appdata_iv *)user_data;
356 IV_MSG_HIGH("%s", __func__);
358 char *operation = NULL;
361 int ret = service_get_operation(service, &operation);
362 if (ret != SERVICE_ERROR_NONE)
364 IV_MSG_ERROR("service_get_operation failed, [%s]", _conver_error(ret));
368 if ( operation == NULL )
370 IV_MSG_ERROR("Operation cannot be NULL.");
374 ret = service_foreach_extra_data(service, _iter_extra_data_cb, NULL);
375 if (ret != SERVICE_ERROR_NONE)
377 IV_MSG_ERROR("service_foreach_extra_data failed, [%s]", _conver_error(ret));
381 ret = service_get_uri(service, &uri);
382 if (ret != SERVICE_ERROR_NONE)
384 IV_MSG_ERROR("service_get_uri failed, [%s]", _conver_error(ret));
388 IV_MSG_HIGH( "Operation=%s URI=%s", operation, uri);
390 ret = service_clone(&ap->service_handle, service);
391 if (ret != SERVICE_ERROR_NONE)
393 IV_MSG_ERROR("service_clone failed, [%s]", _conver_error(ret));
397 if( strcmp(operation, SERVICE_OPERATION_VIEW) == 0 || strcmp(operation, SERVICE_OPERATION_DEFAULT) == 0)
399 if(uri) // uri can entered through argv[1]
401 ap->iv_param_path = uri;
403 if ( ap->iv_param_path == NULL )
405 IV_MSG_ERROR("Entered path is NULL");
409 IV_MSG_HIGH("*****************************");
410 IV_MSG_HIGH("com.samsung.image-viewer : Case View");
411 IV_MSG_HIGH(" URI path : %s", ap->iv_param_path);
412 IV_MSG_HIGH("*****************************");
416 else if( strcmp(operation, SERVICE_OPERATION_CROP) == 0)
418 char *value = NULL; // Will freed on terminate
420 if(uri) // uri can entered through argv[1]
422 ap->iv_param_path = uri;
424 if ( ap->iv_param_path == NULL )
426 IV_MSG_ERROR("Entered path is NULL");
430 service_add_extra_data(ap->service_handle, UG_BUNDLE_KEY_VIEW_MODE, "SETAS");
432 service_get_extra_data(ap->service_handle, UG_BUNDLE_KEY_SETAS_TYPE, &value);
435 service_add_extra_data(ap->service_handle, UG_BUNDLE_KEY_SETAS_TYPE, "Crop");
438 IV_MSG_HIGH("*****************************");
439 IV_MSG_HIGH("com.samsung.image-viewer : Case Crop");
440 IV_MSG_HIGH(" URI path : %s", ap->iv_param_path);
441 IV_MSG_HIGH("*****************************");
447 IV_MSG_ERROR("Unknown operation. %s", operation);
450 if ( ap->iv_param_path == NULL )
452 IV_MSG_ERROR("Entered path is NULL");
457 if(ap->iv_param_path == NULL)
459 IV_MSG_ERROR("File path is NULL. cannot create UG");
463 elm_win_activate(ap->win_main);
464 evas_object_show(ap->win_main);
466 if(ap->iv_ug != NULL)
468 // Create Previous UG if exist
469 IV_MSG_HIGH("Removing previous UG=0x%08x", ap->iv_ug);
470 ug_destroy(ap->iv_ug);
474 char *view_mode = NULL;
475 service_get_extra_data(ap->service_handle, UG_BUNDLE_KEY_VIEW_MODE, &view_mode);
476 if(view_mode == NULL)
478 service_add_extra_data(ap->service_handle, UG_BUNDLE_KEY_VIEW_MODE, UG_MODE_SINGLE);
481 service_add_extra_data(ap->service_handle, UG_BUNDLE_KEY_PATH, ap->iv_param_path);
483 service_add_extra_data(ap->service_handle, UG_BUNDLE_KEY_STANDALONE, UG_STANDALONE_VALUE);
485 ap->iv_ug = _create_ug(ap);
486 if(ap->iv_ug == NULL)
488 IV_MSG_HIGH("create_ug failed. Terminated");
496 if(ap->service_handle)
497 service_destroy(ap->service_handle);
498 ap->service_handle = NULL;
505 void _app_pause(void *user_data)
507 // Take necessary actions when application becomes invisible.
508 IV_MSG_HIGH("%s", __func__);
513 void _app_resume(void *user_data)
515 // Take necessary actions when application becomes visible.
516 IV_MSG_HIGH("%s", __func__);
521 void _app_terminate(void *user_data)
523 // Release all resources
524 IV_MSG_HIGH("%s", __func__);
526 appdata_iv *ap = (appdata_iv *)user_data;
528 if(ap->service_handle) {
529 service_destroy(ap->service_handle);
530 ap->service_handle = NULL;
535 ug_destroy(ap->iv_ug);
541 evas_object_del(ap->ug_layout);
542 ap->ug_layout = NULL;
545 IV_MSG_HIGH("%s terminated.", __func__);
548 void _region_changed(void *user_data)
550 IV_MSG_HIGH("%s", __func__);
554 void _low_battery(void *user_data)
556 IV_MSG_HIGH("%s", __func__);
557 ug_send_event( UG_EVENT_LOW_BATTERY );
560 void _low_memory(void *user_data)
562 IV_MSG_HIGH("%s", __func__);
563 ug_send_event( UG_EVENT_LOW_MEMORY );
567 void _lang_changed(void *user_data)
569 IV_MSG_HIGH("%s", __func__);
570 ug_send_event( UG_EVENT_LANG_CHANGE );
574 void _orient_changed(app_device_orientation_e orientation, void *user_data)
576 IV_MSG_HIGH("%s. orientation=%d", __func__, orientation);
579 enum app_device_orientation_e
581 APP_DEVICE_ORIENTATION_0 The device is oriented in natural position
583 APP_DEVICE_ORIENTATION_90 The device's left side is at the top
585 APP_DEVICE_ORIENTATION_180 The device is upside down
587 APP_DEVICE_ORIENTATION_270 The device's right side is to the top
590 appdata_iv *ap = (appdata_iv *)user_data;
593 enum ug_event evt = UG_EVENT_NONE;
595 if ( _is_rotation_lock() == true ) // When rotatation lock is set, show application as portrate mode.
597 orientation = APP_DEVICE_ORIENTATION_0;
602 case APP_DEVICE_ORIENTATION_0:
604 evt = UG_EVENT_ROTATE_PORTRAIT;
606 case APP_DEVICE_ORIENTATION_90:
608 evt = UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN;
611 case APP_DEVICE_ORIENTATION_180:
613 evt = UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN;
616 case APP_DEVICE_ORIENTATION_270:
617 evt = UG_EVENT_ROTATE_LANDSCAPE;
622 ug_send_event( evt );
624 // IV_MSG_HIGH( "evt = %d, r = %d", evt, r);
626 if ( ap->win_main != NULL )
628 elm_win_rotation_with_resize_set(ap->win_main, degree);
634 * @brief The main function
636 * @param argc The number of arguments
637 * @param argv[] The arguments
639 * @return 0 on sucess, others on fail
641 int main(int argc, char *argv[])
643 //setenv("ELM_ENGINE", "gl", 1); //changed to elm_config_preferred_engine_set("opengl_x11") at _app_create
645 appdata_iv ad = {0,};
647 app_event_callback_s event_callback = {0,};
649 event_callback.create = _app_create;
650 event_callback.terminate = _app_terminate;
651 event_callback.pause = _app_pause;
652 event_callback.resume = _app_resume;
653 event_callback.service = _app_service;
655 event_callback.low_memory = _low_memory;
656 event_callback.low_battery = _low_battery;
657 event_callback.device_orientation = _orient_changed; /* Rotate is not supported in FMRadio */
658 event_callback.language_changed = _lang_changed; /* Currently, Application will terminated when change language */
659 event_callback.region_format_changed = NULL;
661 if ( argc == 2 ) // For command line options.
663 IV_MSG_HIGH("Parsing from cmd line. file=%s", argv[1]);
664 ad.iv_param_path = strdup(argv[1]);
667 int ret = APP_ERROR_NONE;
669 ret = app_efl_main(&argc, &argv, &event_callback, &ad);
671 if ( ret != APP_ERROR_NONE )
673 IV_MSG_ERROR("app_efl_main() is failed. err=%d", ret);