예제 #1
0
static void
tp_chat_build_message (EmpathyTpChat *chat,
		       guint          id,
		       guint          type,
		       guint          timestamp,
		       guint          from_handle,
		       const gchar   *message_body)
{
	EmpathyTpChatPriv *priv;
	EmpathyMessage    *message;

	priv = GET_PRIV (chat);

	message = empathy_message_new (message_body);
	empathy_message_set_tptype (message, type);
	empathy_message_set_receiver (message, priv->user);
	empathy_message_set_timestamp (message, timestamp);
	empathy_message_set_id (message, id);
	g_queue_push_tail (priv->messages_queue, message);

	if (from_handle == 0) {
		empathy_message_set_sender (message, priv->user);
		tp_chat_emit_queued_messages (chat);
	} else {
		empathy_tp_contact_factory_get_from_handle (priv->factory,
			from_handle,
			tp_chat_got_sender_cb,
			message, NULL, G_OBJECT (chat));
	}
}
static GList *
log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
                                         TpAccount *account,
                                         const gchar *filename)
{
  GList *messages = NULL;
  xmlParserCtxtPtr ctxt;
  xmlDocPtr doc;
  xmlNodePtr log_node;
  xmlNodePtr node;

  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
  g_return_val_if_fail (filename != NULL, NULL);

  DEBUG ("Attempting to parse filename:'%s'...", filename);

  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
    {
      DEBUG ("Filename:'%s' does not exist", filename);
      return NULL;
    }

  /* Create parser. */
  ctxt = xmlNewParserCtxt ();

  /* Parse and validate the file. */
  doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
  if (!doc)
    {
      g_warning ("Failed to parse file:'%s'", filename);
      xmlFreeParserCtxt (ctxt);
      return NULL;
    }

  /* The root node, presets. */
  log_node = xmlDocGetRootElement (doc);
  if (!log_node)
    {
      xmlFreeDoc (doc);
      xmlFreeParserCtxt (ctxt);
      return NULL;
    }

  /* Now get the messages. */
  for (node = log_node->children; node; node = node->next)
    {
      EmpathyMessage *message;
      EmpathyContact *sender;
      gchar *time_;
      time_t t;
      gchar *sender_id;
      gchar *sender_name;
      gchar *sender_avatar_token;
      gchar *body;
      gchar *is_user_str;
      gboolean is_user = FALSE;
      gchar *msg_type_str;
      gchar *cm_id_str;
      guint cm_id;
      TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;

      if (strcmp ((const gchar *) node->name, "message") != 0)
        continue;

      body = (gchar *) xmlNodeGetContent (node);
      time_ = (gchar *) xmlGetProp (node, (const xmlChar *) "time");
      sender_id = (gchar *) xmlGetProp (node, (const xmlChar *) "id");
      sender_name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
      sender_avatar_token = (gchar *) xmlGetProp (node,
          (const xmlChar *) "token");
      is_user_str = (gchar *) xmlGetProp (node, (const xmlChar *) "isuser");
      msg_type_str = (gchar *) xmlGetProp (node, (const xmlChar *) "type");
      cm_id_str = (gchar *) xmlGetProp (node, (const xmlChar *) "cm_id");

      if (is_user_str)
        is_user = strcmp (is_user_str, "true") == 0;

      if (msg_type_str)
        msg_type = empathy_message_type_from_str (msg_type_str);

      if (cm_id_str)
        cm_id = atoi (cm_id_str);

      t = empathy_time_parse (time_);

      sender = empathy_contact_new_for_log (account, sender_id, sender_name,
					    is_user);

      if (!EMP_STR_EMPTY (sender_avatar_token))
        empathy_contact_load_avatar_cache (sender,
            sender_avatar_token);

      message = empathy_message_new (body);
      empathy_message_set_sender (message, sender);
      empathy_message_set_timestamp (message, t);
      empathy_message_set_tptype (message, msg_type);
      empathy_message_set_is_backlog (message, TRUE);

      if (cm_id_str)
        empathy_message_set_id (message, cm_id);

      messages = g_list_append (messages, message);

      g_object_unref (sender);
      xmlFree (time_);
      xmlFree (sender_id);
      xmlFree (sender_name);
      xmlFree (body);
      xmlFree (is_user_str);
      xmlFree (msg_type_str);
      xmlFree (cm_id_str);
      xmlFree (sender_avatar_token);
    }

  DEBUG ("Parsed %d messages", g_list_length (messages));

  xmlFreeDoc (doc);
  xmlFreeParserCtxt (ctxt);

  return messages;
}