RSS Feed

Using Tizen’s C API

Posted on

Hello everyone, I have dedicated some time to hack the Tizen’s source avaliable at https://review.tizen.org/git and to be more specific I’m focusing my effort on contacts integration. So I found a way to communicate with the Tizen’s contacts database, there is a C API for that and you can find the code here: https://review.tizen.org/git/?p=api/contacts.git;a=summary . To be honest I didn’t like this API and here are the reasons:

  • Some search functions like contact_foreach_contact_from_db() does not return all contact information, like the phone number. And I can’t understand why.
  • It’s difficult to find the libs you want to use it in the repository. Because sometimes you just don’t have a clue what its name is.
  • It would be great if we had a docs page, because going around the source code too nice.

Saying that, I created a very, very simple contacts list based on EFL and of course using the Tizen’s C API for contacts.

So let’s begin !

  • EFL App Core

When we are programming an EFL application on Tizen, the right way to do it is using an API called “appcore-efl“. It is used because we must be able to handle events like our program is moved to background or it is moved to foreground as well as creating and terminating the App. The image below exemplifies the life cycle of your application.

Figure 1: App life cyle. Source: Samsung.

So before we enter in the mainloop we must set our callbacks for create, reset, pause, resume and terminate. If you don’t want to set them just use NULL. These callbacks are called when:

  • Create: When the App is starting. duh 😉
  • Reset: You can reset your application via another application by passing arguments to it using an API called aul.
  • Pause: Called when your window goes to the background. (When you press the home button)
  • Resume: Called when your window goes to the foreground. E.g. Your App was in background and them you you ask to open it again.
  • Terminate: Before your app closes.

Here is an example of how you can register our callbacks and launch your App:

#include <appcore-efl.h>
#include <stdlib.h>
#include <stdio.h>
#include <bundle.h>

/*
   If you do not return 0, the mainloop will not be called
*/
static int _create_app(void *data)
{
   printf("My app is going alive!\n");
   return 0;
}

static int _terminate_app(void *data)
{
   printf("My app is gone :(\n");
   return 0;
}

static int _pausing(void *data)
{
   printf("My App is now on background\n");
   return 0;
}

static int _resuming(void *data)
{
   printf("My App is now on foreground\n");
   return 0;
}

/*
   bundle is a dictionary used in tizen. So you can
   store [key, value] based data.
*/
static int _reseting(bundle *b, void *data)
{
   printf("Reseted\n");
   return 0;
}

int main(int argc, char **argv)
{
   struct appcore_ops ops =
   {
     .create = _create_app,
     .pause = _pausing,
     .resume = _resuming,
     .reset = _reseting,
     .terminate = _terminate_app,
   };
   ops.data = NULL; /* This is going to be passed to all your callbacks */

   return appcore_efl_main("example", &argc, &argv, &ops);
}

The function appcore_efl_main() will start our application and we will find yourselves executing the main loop. Also the first parameter is the name of our program.

  • Contacts C API

Using the contacts API is very easy, you just need to connect to the database, do your stuff and from it. There is not much to explain here, you just need to know which function you need to use or don’t know it,  just check it in the header files. 🙂

So here is an example:

/*
   We're going to fetch all contacts from the database and display it on the console.
*/
#include <contacts.h>
#include <stdio.h>

int main(int argc, char **argv)
{
   int size, i;
   contact_query_number_array contacts;

   if (contacts_connect() < 0) /* error */
      return -1;

   if (contact_get_number_contact_from_db(&contacts, &size) < 0) /* error*/
      return -1;

   for (i = 0; i < size; i++)
   {
      printf("First name:%s\n", contacts[i]->first_name);
      printf("Last name:%s\n", contacts[i]->last_name);
      printf("Phone number:%s\n", contacts[i]->phone_number);
   }

   contact_query_number_array_free(contacts, size);

   if (contacts_disconnect() < 0) /* error */
      return -1;

   return 0;
}

Now that we know a little more about native Tizen code, I made a program that illustrates a real scenario. I’m not going to explain the code here, because it’s not very complicated and you have learned all its basics by now. So here is the link: http://git.profusion.mobi/cgit.cgi/iscaro/contacts/

Note: If you’re going to compile it, you must use SBS.

Pictures of the application:

Cya.

Advertisements

2 responses »

  1. Hi! nice tutorial. I just wonder why EFL/C API is not OOP based?

    Reply
    • Hello Marcoci, the main reason is because EFL is construct using C and C isn’t a OOP language. Of course you can program in C using OO, but it’s not a good approach to mix things.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: