World Clock Widget / src /

main.c

/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.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 <tizen.h>
#include <widget_app.h>
#include <dlog.h>
#include "log.h"
#include "view.h"
#include "data.h"

static void clicked_cb(void *user_data);


static int widget_instance_create(widget_context_h context, bundle *content, int w, int h, void *user_data)
{
	void *wid = NULL;
	char *region_text = NULL;
	char *time_text = NULL;
	char *date_text = NULL;

	if (content != NULL) {
		/* Recover the previous status with the bundle object. */
	}

	/*
	 * @note
	 * view_create() function makes empty view frame.
	 */
	wid = view_create(context, w, h);
	if (wid == NULL) {
		dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a view.");
		return WIDGET_ERROR_FAULT;
	}

	/*
	 * @note
	 * view_set_image() function put a image to the part.
	 * Pass the part name which you want to put in as a second parameter,
	 * and name of image file including its directory path as a third parameter.
	 */
	view_set_image(wid, "widget.bg", "images/bg.png");

	/*
	 * @note
	 * data_get_text() function get the text you want to use.
	 * Pass the part name which you want to fill out as parameter.
	 */
	region_text = data_get_text("widget.region");
	/*
	 * @note
	 * view_set_text() function fill out the part with the text.
	 * Pass the part name as a first parameter and the text as a second parameter.
	 */
	view_set_text(wid, "widget.region", region_text);
	free(region_text);

	/*
	 * @note
	 * This function set will be called several times as much as you want to use.
	 */
	time_text = data_get_text("widget.time");
	view_set_text(wid, "widget.time", time_text);
	free(time_text);

	date_text = data_get_text("widget.date");
	view_set_text(wid, "widget.date", date_text);
	free(date_text);

	/*
	 * @note
	 * view_set_event_callback() function launch widget's application.
	 * Decide a third parameter of the function to designate the moment that application be launched.
	 * There are three choices, WIDGET_EVENT_PRESSED, WIDGET_EVENT_RELEASED, WIDGET_EVENT_CLICKED.
	 */
	view_set_event_callback(wid, "widget.event", WIDGET_EVENT_CLICKED, (void *) clicked_cb);

	widget_app_context_set_tag(context, wid);

	return WIDGET_ERROR_NONE;
}

static int widget_instance_destroy(widget_context_h context, widget_app_destroy_type_e reason, bundle *content_info, void *user_data)
{
	void *wid = NULL;
	widget_app_context_get_tag(context, (void**) &wid);

	view_destroy(wid);

	return WIDGET_ERROR_NONE;
}

static int widget_instance_pause(widget_context_h context, void *user_data)
{
	/* Take necessary actions when widget instance becomes invisible. */
	return WIDGET_ERROR_NONE;
}

static int widget_instance_resume(widget_context_h context, void *user_data)
{
	/* Take necessary actions when widget instance becomes visible. */
	return WIDGET_ERROR_NONE;
}

static int widget_instance_update(widget_context_h context, bundle *content, int force, void *user_data)
{
	/* Take necessary actions when widget instance should be updated. */
	return WIDGET_ERROR_NONE;
}

static int widget_instance_resize(widget_context_h context, int w, int h, void *user_data)
{
	void *wid = NULL;

	widget_app_context_get_tag(context, (void**) &wid);
	view_resize(wid, w, h);

	return WIDGET_ERROR_NONE;
}

static void widget_app_lang_changed(app_event_info_h event_info, void *user_data)
{
	/* APP_EVENT_LANGUAGE_CHANGED */
	char *locale = NULL;
	app_event_get_language(event_info, &locale);
	elm_language_set(locale);
	free(locale);
}

static void widget_app_region_changed(app_event_info_h event_info, void *user_data)
{
	/* APP_EVENT_REGION_FORMAT_CHANGED */
}

static widget_class_h widget_app_create(void *user_data)
{
	app_event_handler_h handlers[5] = {NULL, };

	widget_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
			APP_EVENT_LANGUAGE_CHANGED, widget_app_lang_changed, user_data);
	widget_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
			APP_EVENT_REGION_FORMAT_CHANGED, widget_app_region_changed,
			user_data);

	widget_instance_lifecycle_callback_s ops = {
			.create = widget_instance_create,
			.destroy = widget_instance_destroy,
			.pause = widget_instance_pause,
			.resume = widget_instance_resume,
			.update = widget_instance_update,
			.resize = widget_instance_resize,
	};

	return widget_app_class_create(ops, user_data);
}

static void widget_app_terminate(void *user_data)
{
	/* Release all resources. */
}

int main(int argc, char *argv[])
{
	widget_app_lifecycle_callback_s ops = {0, };
	int ret;

	ops.create = widget_app_create;
	ops.terminate = widget_app_terminate;

	ret = widget_app_main(argc, argv, &ops, NULL);
	if (ret != WIDGET_ERROR_NONE) {
		dlog_print(DLOG_ERROR, LOG_TAG, "widget_app_main() is failed. err = %d", ret);
	}

	return ret;
}


/*
 * @note
 * Below functions are static functions.
 */

/*
 * @brief: This function will be operated when the specific Evas object is clicked
 * @param[user_data]: data needed in this function
 */
static void clicked_cb(void *user_data)
{
	/*
	 * @note
	 * In this function, you launch the application.
	 */
	dlog_print(DLOG_DEBUG, LOG_TAG, "clicked a widget.");
}

/* End of file */