Esempio n. 1
0
static gboolean
load_doap (IdeDoap       *self,
           XmlReader     *reader,
           GError       **error)
{
  if (!xml_reader_read_start_element (reader, "Project"))
    {
      g_set_error (error,
                   IDE_DOAP_ERROR,
                   IDE_DOAP_ERROR_INVALID_FORMAT,
                   "Project element is missing from doap.");
      return FALSE;
    }

  g_object_freeze_notify (G_OBJECT (self));

  xml_reader_read (reader);

  do
    {
      const gchar *element_name;

      element_name = xml_reader_get_local_name (reader);

      if (g_strcmp0 (element_name, "name") == 0 ||
          g_strcmp0 (element_name, "shortdesc") == 0 ||
          g_strcmp0 (element_name, "description") == 0)
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "category") == 0 ||
               g_strcmp0 (element_name, "homepage") == 0 ||
               g_strcmp0 (element_name, "download-page") == 0 ||
               g_strcmp0 (element_name, "bug-database") == 0)
        {
          gchar *str;

          str = xml_reader_get_attribute (reader, "rdf:resource");
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "programming-language") == 0)
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL && str[0] != '\0')
            ide_doap_add_language (self, g_strstrip (str));
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "maintainer") == 0)
        {
          if (!ide_doap_parse_maintainer (self, reader))
            break;
        }
    }
  while (xml_reader_read_to_next (reader));

  g_object_thaw_notify (G_OBJECT (self));

  return TRUE;
}
Esempio n. 2
0
static gboolean
news_format_rss_read_from_xml (NewsFormatRss  *rss,
                        XmlReader      *reader,
                        NewsFeed       *feed,
                        GError        **error)
{
   GHashTable *categories = NULL;
   GHashTable *persons = NULL;
   GHashTable *links = NULL;
   GomAdapter *adapter;
   gchar *str;

   ENTRY;

   g_return_val_if_fail(NEWS_IS_FORMAT_RSS(rss), FALSE);
   g_return_val_if_fail(XML_IS_READER(reader), FALSE);
   g_return_val_if_fail(NEWS_IS_FEED(feed), FALSE);

   adapter = news_format_get_adapter(NEWS_FORMAT(rss));
   g_assert(adapter);

skip:
   if (!xml_reader_read_to_next(reader) ||
       !xml_reader_is_a(reader, "rss")) {
      if (xml_reader_is_a(reader, "xml-stylesheet") ||
          xml_reader_is_a(reader, "#comment")) {
         goto skip;
      }
      RETURN_ERROR;
   }

   if (!xml_reader_is_a_local(reader, "rss")) {
      RETURN_ERROR;
   }

   if ((str = xml_reader_get_attribute(reader, "version"))) {
      if (!!g_strcmp0(str, "2.0")) {
         g_set_error(error, XML_READER_ERROR, XML_READER_ERROR_INVALID,
                  _("RSS Version \"%s\" is not supported."), str);
         g_free(str);
         RETURN(FALSE);
      }
      g_free(str);
   }

   if (!xml_reader_read_start_element(reader, "channel")) {
      RETURN_ERROR;
   }

   persons = g_hash_table_new_full(g_str_hash, g_str_equal,
                                   g_free, g_object_unref);
   categories = g_hash_table_new_full(g_str_hash, g_str_equal,
                                      g_free, g_object_unref);
   links = g_hash_table_new_full(g_str_hash, g_str_equal,
                                 g_free, g_object_unref);

   xml_reader_read(reader);

   for (xml_reader_read_to_next(reader);
        xml_reader_get_node_type(reader) != XML_READER_TYPE_END_ELEMENT;
        xml_reader_read_to_next(reader)) {

      if ((xml_reader_get_node_type(reader) != XML_READER_TYPE_ELEMENT) || rss->priv->error) {
         g_hash_table_destroy(categories);
         g_hash_table_destroy(persons);
         g_hash_table_destroy(links);
         RETURN_ERROR;
      }

      if (xml_reader_is_a_local(reader, "title")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "title", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "link")) {
         GomCollection *collection;
         NewsLink *link_;

         g_object_get(feed, "links", &collection, NULL);
         if ((link_ = news_format_rss_read_link(rss, reader, links))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(link_));
            g_object_unref(link_);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "description")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "description", str, NULL);
         g_free(str);
         xml_reader_read_end_element(reader);
      } else if (xml_reader_is_a_local(reader, "language")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "language", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "copyright")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "copyright", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "managingEditor")) {
         GomCollection *collection;
         NewsPerson *person;

         g_object_get(feed, "authors", &collection, NULL);
         if ((person = news_format_rss_read_person(rss, reader, persons))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(person));
            g_object_unref(person);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "pubDate") ||
                 xml_reader_is_a_local(reader, "lastBuildDate")) {
         GDateTime *dt;
         GTimeVal tv;

         if ((str = xml_reader_read_string(reader))) {
            if (g_time_val_from_iso8601(str, &tv)) {
               if ((dt = g_date_time_new_from_timeval_utc(&tv))) {
                  g_object_set(feed,
                            "last-updated-at", dt,
                            NULL);
                  g_date_time_unref(dt);
               }
            }
            g_free(str);
         }
      } else if (xml_reader_is_a_local(reader, "category")) {
         GomCollection *collection;
         NewsCategory *category;

         g_object_get(feed, "categories", &collection, NULL);
         if ((category = news_format_rss_read_category(rss, reader, categories))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(category));
            g_object_unref(category);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "generator")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "generator", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "item")) {
         GomCollection *collection;
         NewsItem *item;

         g_object_get(feed, "items", &collection, NULL);
         item = g_object_new(NEWS_TYPE_ITEM,
                        "adapter", adapter,
                        "is-new", TRUE,
                        NULL);
         if (news_format_rss_read_item(rss, reader, item, persons, categories, links)) {
            gom_collection_add_resource(collection,
                                 GOM_RESOURCE(item));
         }
         g_object_unref(item);
         g_object_unref(collection);
      }
   }

   g_hash_table_destroy(categories);
   g_hash_table_destroy(links);
   g_hash_table_destroy(persons);

   RETURN(TRUE);
}
Esempio n. 3
0
gboolean
ide_doap_load_from_file (IdeDoap       *self,
                         GFile         *file,
                         GCancellable  *cancellable,
                         GError       **error)
{
  g_autoptr(XmlReader) reader = NULL;

  g_return_val_if_fail (IDE_IS_DOAP (self), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);

  reader = xml_reader_new ();

  if (!xml_reader_load_from_file (reader, file, cancellable, error))
    return FALSE;

  if (!xml_reader_read_start_element (reader, "Project"))
    {
      g_set_error (error,
                   IDE_DOAP_ERROR,
                   IDE_DOAP_ERROR_INVALID_FORMAT,
                   "Project element is missing from doap.");
      return FALSE;
    }

  g_object_freeze_notify (G_OBJECT (self));

  xml_reader_read (reader);

  do
    {
      const gchar *element_name;

      element_name = xml_reader_get_local_name (reader);

      if (ide_str_equal0 (element_name, "name") ||
          ide_str_equal0 (element_name, "shortdesc") ||
          ide_str_equal0 (element_name, "description"))
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "category") ||
               ide_str_equal0 (element_name, "homepage") ||
               ide_str_equal0 (element_name, "download-page") ||
               ide_str_equal0 (element_name, "bug-database"))
        {
          gchar *str;

          str = xml_reader_get_attribute (reader, "rdf:resource");
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "programming-language"))
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (!ide_str_empty0 (str))
            ide_doap_add_language (self, g_strstrip (str));
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "maintainer"))
        {
          if (!ide_doap_parse_maintainer (self, reader))
            break;
        }
    }
  while (xml_reader_read_to_next (reader));

  g_object_thaw_notify (G_OBJECT (self));

  return TRUE;
}
static void
parse_opml (IrisTask *task,
            gpointer  user_data)
{
  MarinaGoogleReaderSource *google_source;
  XmlReader                *reader;
  GList                    *sources = NULL;
  MarinaSource             *source;
  gchar                    *id;
  GValue                    value = {0,};

  g_return_if_fail (MARINA_IS_GOOGLE_READER_SOURCE (user_data));
  
  google_source = MARINA_GOOGLE_READER_SOURCE (user_data);
  reader = xml_reader_new ();

  iris_task_get_result (task, &value);
  
  if (!xml_reader_load_from_data (reader,
                                  g_value_get_string (&value),
                                  -1, OPML_URI, NULL))
    goto cleanup;
  
  if (!xml_reader_read_start_element (reader, "opml"))
    // set error
    goto cleanup;
    
  if (!xml_reader_read_start_element (reader, "body"))
    // set error
    goto cleanup;
    
  while (xml_reader_read_start_element (reader, "outline"))
    {
      source = marina_soup_source_new ();
      id = g_strconcat (marina_source_get_id (MARINA_SOURCE (google_source)),
                        "+",
                        xml_reader_get_attribute (reader, "xmlUrl"),
                        NULL);
      MARINA_SOURCE_BASE (source)->id = g_strdup (id);
      g_object_set (source,
                    "title", xml_reader_get_attribute (reader, "title"),
                    "uri", xml_reader_get_attribute (reader, "xmlUrl"),
                    NULL);
      sources = g_list_prepend (sources, source);
      g_free (id);
    }
  
  sources = g_list_reverse (sources);
  
  if (!google_source->priv->children)
    {
      google_source->priv->children = sources;
      
      GList *iter;
      for (iter = sources; iter; iter = iter->next)
        {
          marina_source_child_added (MARINA_SOURCE (google_source), iter->data);
          marina_sync_source (iter->data);
        }
    }
  else
    {
      g_list_foreach (sources, (GFunc)g_object_unref, NULL);
      g_list_free (sources);
      sources = NULL;
      // set error
    }
  
  // update the unread counts
  marina_sources_stat (marina_sources_get_default ());
  
cleanup:
  g_object_unref (reader);
}