static void
check_insert_create_zeitgeist (BijiNoteObj *note)
{
  gchar *uri;
  ZeitgeistLog       *log;
  GPtrArray          *templates;
  ZeitgeistEvent     *event;
  ZeitgeistSubject   *subject;
  
  uri = g_strdup_printf ("file://%s", biji_item_get_uuid (BIJI_ITEM (note)));
  log = biji_note_book_get_zg_log (biji_item_get_book (BIJI_ITEM (note)));
  
  templates = g_ptr_array_new ();
  event = zeitgeist_event_new_full (ZEITGEIST_ZG_CREATE_EVENT, 
                                    NULL,
                                    "application://bijiben.desktop",
                                    NULL, NULL);
  subject = zeitgeist_subject_new ();
  zeitgeist_subject_set_uri (subject, uri);
  zeitgeist_event_add_subject (event, subject);
  g_ptr_array_add (templates, event);
  
  zeitgeist_log_find_events (log,
                             zeitgeist_time_range_new_to_now (),
                             templates,
                             ZEITGEIST_STORAGE_STATE_ANY,
                             10,
                             ZEITGEIST_RESULT_TYPE_LEAST_RECENT_EVENTS,
                             NULL,
                             (GAsyncReadyCallback) on_find_create_event,
                             note);
}
ZeitgeistLog *
biji_zeitgeist_init (void)
{
  GPtrArray *ptr_arr;
  ZeitgeistEvent *event;
  ZeitgeistDataSource *ds;
  ZeitgeistDataSourceRegistry *zg_dsr = NULL;
  ZeitgeistLog *log;

  log = zeitgeist_log_new ();
  event = zeitgeist_event_new_full (
    NULL, NULL, "application://bijiben.desktop", NULL, NULL);

  ptr_arr = g_ptr_array_new ();
  g_ptr_array_add (ptr_arr, event);

  ds = zeitgeist_data_source_new_full ("org.gnome.bijiben,dataprovider",
                                       "Notes dataprovider",
                                       "Logs events about accessed notes",
                                       ptr_arr),

  zg_dsr = zeitgeist_data_source_registry_new ();
  zeitgeist_data_source_registry_register_data_source (zg_dsr, ds,
                                                       NULL, NULL, NULL);
  g_ptr_array_set_free_func (ptr_arr, g_object_unref);
  g_ptr_array_unref (ptr_arr);

  return log;
}
void
insert_zeitgeist (BijiNoteObj *note,
                  gchar *zg_interpretation)
{
  gchar *uri;
  const gchar *title;
  ZeitgeistEvent     *event;
  ZeitgeistSubject   *subject;
  ZeitgeistLog       *log;

  /* Make sure that only notes with a title log their events.
  If a note is closed without a title, it is deleted. This
  section prevents the ACCESS_EVENT being called immediately
  after the note is created and the note is empty */

  title = biji_item_get_title (BIJI_ITEM (note));
  if (title == NULL ||
      g_utf8_strlen (title, -1) <= 0)
    return;

  /* Insert requested log */

  log = biji_note_book_get_zg_log (biji_item_get_book (BIJI_ITEM (note)));
  uri = g_strdup_printf ("file://%s", biji_item_get_uuid (BIJI_ITEM (note)));

  subject = zeitgeist_subject_new_full (uri,
                                        ZEITGEIST_NFO_DOCUMENT,
                                        ZEITGEIST_NFO_FILE_DATA_OBJECT,
                                        "application/x-note",
                                        "",
                                        title,
                                        "");

  event = zeitgeist_event_new_full (zg_interpretation,
                                    ZEITGEIST_ZG_USER_ACTIVITY,
                                    "application://bijiben.desktop",
                                    "",
                                    subject,
                                    NULL);


  if (g_strcmp0 (zg_interpretation, ZEITGEIST_ZG_CREATE_EVENT) ==0)
    zeitgeist_event_set_timestamp (event,
                                   biji_note_obj_get_create_date (note)/1000);

  zeitgeist_log_insert_event_no_reply (log, event, NULL);
  g_free (uri);


  /* 
   * Check if the note
   * was already created into zeitgeist
   */

  if (g_strcmp0 (zg_interpretation, ZEITGEIST_ZG_MODIFY_EVENT) ==0)
    check_insert_create_zeitgeist (note);
}
/* Zeitgeist templates are handled in a strange way within libzeitgeist:
 * the GDestroyFunc is overriden with NULL and each item is unreferenced after
 * having been evaluated (taking the ownership of a floating ref if needed).
 *
 * To avoid problems it's safer and easier to create a new template each time
 * it is used, given a default configuration */
static GPtrArray *
_default_template_factory (void)
{
  GPtrArray *ret = g_ptr_array_new ();

  g_ptr_array_add (ret, zeitgeist_event_new_full (
        NULL, /* interpretation */
        ZEITGEIST_ZG_USER_ACTIVITY, /* manifestation */
        "application:*", /* actor */
        zeitgeist_subject_new_full (
          "file:*", /* uri, just local files */
          NULL, /* interpretation */
          NULL, /* manifestation */
          NULL, /* mime-type */
          NULL, /* origin */
          NULL, /* text */
          NULL /* storage - auto-guess */
          ),
        NULL));

  return ret;
}
static ZeitgeistEvent *_get_event_template_for_category (CDEventType iCategory)
{
	if (myData.pEvents == NULL)
	{
		myData.pEvents = g_new0 (ZeitgeistEvent*, CD_NB_EVENT_TYPES);
		
		ZeitgeistSubject *subj;
		subj = zeitgeist_subject_new_full ("",  // any type of uri.
			"",  // interpretation
			"",  // manifestation (ZEITGEIST_NFO_FILE_DATA_OBJECT/ZEITGEIST_NFO_REMOTE_DATA_OBJECT)
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_ALL] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("application://*",  // ex.: application://firefox.desktop
			"",  // interpretation
			"",  // manifestation (ZEITGEIST_NFO_FILE_DATA_OBJECT/ZEITGEIST_NFO_REMOTE_DATA_OBJECT)
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_APPLICATION] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
			ZEITGEIST_NFO_DOCUMENT,  // interpretation
			ZEITGEIST_NFO_FILE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_DOCUMENT] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		///subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
		/**	ZEITGEIST_NFO_FOLDER,  // interpretation
			ZEITGEIST_NFO_FILE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_FOLDER] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL*/
		
		subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
			ZEITGEIST_NFO_IMAGE,  // interpretation
			ZEITGEIST_NFO_FILE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_IMAGE] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
			ZEITGEIST_NFO_AUDIO,  // interpretation
			ZEITGEIST_NFO_FILE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_AUDIO] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
			ZEITGEIST_NFO_VIDEO,  // interpretation
			ZEITGEIST_NFO_FILE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_VIDEO] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("",  // url
			ZEITGEIST_NFO_WEBSITE,  // interpretation
			ZEITGEIST_NFO_REMOTE_DATA_OBJECT,  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_WEB] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		
		subj = zeitgeist_subject_new_full ("file://*",  // uri, application://* for apps
			"!"ZEITGEIST_NFO_DOCUMENT,  // interpretation
			"",  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		myData.pEvents[CD_EVENT_OTHER] = zeitgeist_event_new_full (
			ZEITGEIST_ZG_ACCESS_EVENT,  // interpretation type of the event (ZEITGEIST_ZG_EVENT_INTERPRETATION)
			ZEITGEIST_ZG_USER_ACTIVITY,  // manifestation type of the event (ZEITGEIST_ZG_EVENT_MANIFESTATION)
			"",  // actor (the party responsible for triggering the event, eg: app://firefox.desktop)
			#ifndef ZEITGEIST_1_0
			"", // origin
			#endif
			subj,  // a list of subjects
			NULL);  // terminated with NULL
		///subj = zeitgeist_subject_new_full ("",  // uri, application://* for apps
		/**	"!"ZEITGEIST_NFO_FOLDER,  // interpretation
			"",  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		zeitgeist_event_add_subject (myData.pEvents[CD_EVENT_OTHER], subj);*/
		subj = zeitgeist_subject_new_full ("",  // uri, application://* for apps
			"!"ZEITGEIST_NFO_IMAGE,  // interpretation
			"",  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		zeitgeist_event_add_subject (myData.pEvents[CD_EVENT_OTHER], subj);
		subj = zeitgeist_subject_new_full ("",  // uri, application://* for apps
			"!"ZEITGEIST_NFO_AUDIO,  // interpretation
			"",  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		zeitgeist_event_add_subject (myData.pEvents[CD_EVENT_OTHER], subj);
		subj = zeitgeist_subject_new_full ("",  // uri, application://* for apps
			"!"ZEITGEIST_NFO_VIDEO,  // interpretation
			"",  // manifestation
			"",  // mimetype
			"",  // origin
			"",  // text
			"");  // storage
		zeitgeist_event_add_subject (myData.pEvents[CD_EVENT_OTHER], subj);
	}