example-create-person

   1 #include "example.h"
   2 #include "libsoylent/soylent.h"
   3 
   4 void
   5 create_person (void)
   6 {
   7   GError *error = NULL;
   8   
   9   /* initialize libsoylent */
  10   if (!sl_init (&error))
  11     {
  12       example_error (error);
  13     }
  14 
  15   /* create a new person named "Tim Smith" */
  16   SlPerson *tim = sl_person_new (g_strdup ("Tim"), g_strdup ("Smith"));
  17 
  18   /* add two email-addresses */
  19   sl_person_add (tim, SL_ATTR_EMAIL, g_strdup ("tim@example.org"));
  20   sl_person_add (tim, SL_ATTR_EMAIL, g_strdup ("tim@jabber.org"));
  21 
  22   /* store the person in the default addressbook */
  23   if (!sl_book_add_person (SL_BOOK_DEFAULT, tim, &error))
  24     {
  25       example_error (error);
  26     }
  27 
  28   /* set Tims phone-number */
  29   sl_person_set (tim, SL_ATTR_TELEPHONE, g_strdup ("007 123456789"));
  30   
  31   /* add another phone-number */
  32   sl_person_add (tim, SL_ATTR_TELEPHONE, g_strdup ("007 987654321"));
  33   
  34   /* set Tims address... */
  35   SlAddress *addr = sl_address_new ();
  36   addr->street = "Abbey Road";
  37   addr->country = "Free-World";
  38   sl_person_set (tim, SL_ATTR_ADDRESS, addr);
  39   
  40   /* ... and his birthday */
  41   GDate *bday = g_date_new_dmy (3, 2, 1980);
  42   sl_person_set (tim, SL_ATTR_BIRTHDAY, bday);
  43   
  44   /* Tims second email-address has changed */
  45   sl_person_set_at (tim, SL_ATTR_EMAIL, 1, g_strdup ("tim_smith@jabber.org"));
  46   
  47   /* give Tim a nice nick-name */
  48   sl_person_set_nick (tim, g_strdup ("Timmy"));
  49   /* is the same as sl_person_set (tim, SL_ATTR_NICK, g_strdup ("Timmy")) */
  50   
  51   /* add an arbitrary attribute */
  52   sl_person_set (tim, "mood", g_strdup ("happy"));
  53   
  54   /* commit changes of Tim */
  55   if (!sl_person_commit (tim, &error))
  56     {
  57       example_error (error);
  58     }
  59   
  60   /* cleanup */
  61   g_object_unref (tim);
  62   sl_cleanup ();
  63 }

Projects/Soylent/libsoylent/example-create-person (last edited 2013-11-22 19:18:57 by WilliamJonMcCann)