示例#1
0
static char *
get_author_icon_url (SwYoutubeItemView *youtube, const char *author)
{
  SwYoutubeItemViewPrivate *priv = GET_PRIVATE (youtube);
  RestProxyCall *call;
  RestXmlNode *root, *node;
  char *function, *url;

  url = g_hash_table_lookup (priv->thumb_map, author);
  if (url)
    return g_strdup (url);

  call = rest_proxy_new_call (priv->proxy);
  function = g_strdup_printf ("users/%s", author);
  rest_proxy_call_set_function (call, function);
  rest_proxy_call_sync (call, NULL);

  root = xml_node_from_call (call, "Youtube");
  if (!root)
    return NULL;

  node = rest_xml_node_find (root, "media:thumbnail");
  if (!node)
    return NULL;

  url = g_strdup (rest_xml_node_get_attr (node, "url"));

  g_free (function);

  if (url)
    g_hash_table_insert(priv->thumb_map, (gpointer)author, (gpointer)g_strdup (url));

  return url;
}
示例#2
0
static RestXmlNode *
node_from_call (RestProxyCall *call, GError **error)
{
  static RestXmlParser *parser = NULL;
  RestXmlNode *node;

  if (call == NULL)
    return NULL;

  if (parser == NULL)
    parser = rest_xml_parser_new ();

  if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) {
    g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR,
                 "HTTP error: %s (%d)",
                 rest_proxy_call_get_status_message (call),
                 rest_proxy_call_get_status_code (call));
    return NULL;
  }

  node = rest_xml_parser_parse_from_data (parser,
                                          rest_proxy_call_get_payload (call),
                                          rest_proxy_call_get_payload_length (call));

  /* Invalid XML, or incorrect root */
  if (node == NULL || !g_str_equal (node->name, "rsp")) {
    g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR,
                 "malformed remote response: %s",
                 rest_proxy_call_get_payload (call));
    if (node)
      rest_xml_node_unref (node);
    return NULL;
  }

  if (g_strcmp0 (rest_xml_node_get_attr (node, "stat"), "ok") != 0) {
    RestXmlNode *err;
    err = rest_xml_node_find (node, "err");
    g_set_error (error, SW_SERVICE_ERROR, SW_SERVICE_ERROR_REMOTE_ERROR,
                 "remote Vimeo error: %s", err ?
                 rest_xml_node_get_attr (err, "msg") : "unknown");
    rest_xml_node_unref (node);
    return NULL;
  }

  return node;
}
static RestXmlNode *
node_from_call (RestProxyCall *call)
{
  static RestXmlParser *parser = NULL;
  RestXmlNode *node;

  if (call == NULL)
    return NULL;

  if (parser == NULL)
    parser = rest_xml_parser_new ();

  if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) {
    g_message (G_STRLOC ": error from Last.fm: %s (%d)",
               rest_proxy_call_get_status_message (call),
               rest_proxy_call_get_status_code (call));
    return NULL;
  }

  node = rest_xml_parser_parse_from_data (parser,
                                          rest_proxy_call_get_payload (call),
                                          rest_proxy_call_get_payload_length (call));

  /* No content, or wrong content */
  if (node == NULL || strcmp (node->name, "lfm") != 0) {
    g_message (G_STRLOC ": cannot make Last.fm call");
    /* TODO: display the payload if its short */
    if (node) rest_xml_node_unref (node);
    return NULL;
  }

  if (strcmp (rest_xml_node_get_attr (node, "status"), "ok") != 0) {
    RestXmlNode *err_node;
    err_node = rest_xml_node_find (node, "error");
    g_message (G_STRLOC ": cannot make Last.fm call: %s (code %s)",
                err_node->content,
                rest_xml_node_get_attr (err_node, "code"));
    rest_xml_node_unref (node);
    return NULL;
  }

  return node;
}
示例#4
0
static SwItem *
make_item (SwYoutubeItemView *item_view,
           SwService        *service,
           RestXmlNode      *node)
{
  SwItem *item;
  char *author, *date, *url;
  RestXmlNode *subnode, *thumb_node;

  item = sw_item_new ();
  sw_item_set_service (item, service);
  /*
  <rss>
    <channel>
      <item>
        <guid isPermaLink="false">http://gdata.youtube.com/feeds/api/videos/<videoid></guid>
        <atom:updated>2010-02-13T06:17:32.000Z</atom:updated>
        <title>Video Title</title>
        <author>Author Name</author>
        <link>http://www.youtube.com/watch?v=<videoid>&amp;feature=youtube_gdata</link>
        <media:group>
          <media:thumbnail url="http://i.ytimg.com/vi/<videoid>/default.jpg" height="90" width="120" time="00:03:00.500"/>
        </media:group>
      </item>
    </channel>
  </rss>
  */

  sw_item_put (item, "id", xml_get_child_node_value (node, "guid"));

  date = xml_get_child_node_value (node, "atom:updated");
  if (date != NULL)
    sw_item_put (item, "date", get_utc_date(date));

  sw_item_put (item, "title", xml_get_child_node_value (node, "title"));
  sw_item_put (item, "url", xml_get_child_node_value (node, "link"));
  author = xml_get_child_node_value (node, "author");
  sw_item_put (item, "author", author);

  /* media:group */
  subnode = rest_xml_node_find (node, "media:group");
  if (subnode){
    thumb_node = rest_xml_node_find (subnode, "media:thumbnail");
    url = (char *) rest_xml_node_get_attr (thumb_node, "url");
    sw_item_request_image_fetch (item, TRUE, "thumbnail", url);
  }

  url = get_author_icon_url (item_view, author);
  sw_item_request_image_fetch (item, FALSE, "authoricon", url);
  g_free (url);

  return item;
}
示例#5
0
/**
 * flickr_proxy_is_successful:
 * @root: The root node of a parsed Flickr response
 * @error: #GError to set if the response was an error
 *
 * Examines the Flickr response and if it not a successful reply, set @error and
 * return FALSE.
 *
 * Returns: %TRUE if this response is successful, %FALSE otherwise.
 */
gboolean
flickr_proxy_is_successful (RestXmlNode *root, GError **error)
{
  RestXmlNode *node;

  g_return_val_if_fail (root, FALSE);

  if (strcmp (root->name, "rsp") != 0) {
    g_set_error (error, FLICKR_PROXY_ERROR, 0,
                 "Unexpected response from Flickr (root node %s)",
                 root->name);
    return FALSE;
  }

  if (strcmp (rest_xml_node_get_attr (root, "stat"), "ok") != 0) {
    node = rest_xml_node_find (root, "err");
    g_set_error_literal (error,FLICKR_PROXY_ERROR,
                         atoi (rest_xml_node_get_attr (node, "code")),
                         rest_xml_node_get_attr (node, "msg"));
    return FALSE;
  }

  return TRUE;
}
示例#6
0
static void
populate_store (GtkListStore *store, RestXmlNode *provider_node)
{
  RestXmlNode *p_node, *name_node;

  for (p_node = rest_xml_node_find (provider_node, "apn");
       p_node != NULL;
       p_node = p_node->next) {

    name_node = rest_xml_node_find (p_node, "name");
    if (name_node) {
      gtk_list_store_insert_with_values (store, NULL, 0,
                                         0, p_node,
                                         1, name_node->content,
                                         -1);
    } else {
      gtk_list_store_insert_with_values (store, NULL, 0,
                                         0, p_node,
                                         1, rest_xml_node_get_attr (p_node, "value"),
                                         -1);
    }
  }
}
static void
state_machine (void)
{
  GtkWidget *dialog;
  int old_state;

  while (state != STATE_FINISH) {
    /* For sanity checking state changes later */
    old_state = state;

    switch (state) {
    case STATE_START:
      /*
       * Determine if we need to select a service or can go straight to probing
       * the service.
       */
      if (services) {
        if (services->next) {
          dialog = g_object_new (GGG_TYPE_SERVICE_DIALOG,
                                 "services", services,
                                 NULL);
          switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
          case GTK_RESPONSE_CANCEL:
          case GTK_RESPONSE_DELETE_EVENT:
            state = STATE_FINISH;
            break;
          case GTK_RESPONSE_ACCEPT:
            service = ggg_service_dialog_get_selected (GGG_SERVICE_DIALOG (dialog));
            state = STATE_SERVICE;
            break;
          }
          gtk_widget_destroy (dialog);
        } else {
          service = services->data;
          state = STATE_SERVICE;
        }
      } else {
        g_printerr ("No services found\n");
        state = STATE_FINISH;
      }
      break;
    case STATE_SERVICE:
      /*
       * Exmaine the service and determine if we can probe some information, or
       * have to do guided configuration.
       */
      g_assert (service);

      if (ggg_service_is_roaming (service)) {
        state = STATE_COUNTRY;
      } else {
        provider_node = ggg_mobile_info_get_provider_for_ids
          (ggg_service_get_mcc (service), ggg_service_get_mnc (service));
        /* If we found a provider switch straight to the plan selector, otherwises
           fall back to the manual configuration */
        state = provider_node ? STATE_PLAN : STATE_MANUAL;
      }
      break;
    case STATE_COUNTRY:
      dialog = g_object_new (GGG_TYPE_COUNTRY_DIALOG, NULL);
      switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
      case GTK_RESPONSE_CANCEL:
      case GTK_RESPONSE_DELETE_EVENT:
        state = STATE_FINISH;
        break;
      case GTK_RESPONSE_REJECT:
        state = STATE_MANUAL;
        break;
      case GTK_RESPONSE_ACCEPT:
        country_node = ggg_country_dialog_get_selected (GGG_COUNTRY_DIALOG (dialog));
        state = STATE_PROVIDER;
        break;
      }
      gtk_widget_destroy (dialog);
      break;
    case STATE_PROVIDER:
      g_assert (country_node);
      dialog = g_object_new (GGG_TYPE_PROVIDER_DIALOG,
                             "country", country_node,
                             NULL);
      switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
      case GTK_RESPONSE_CANCEL:
      case GTK_RESPONSE_DELETE_EVENT:
        state = STATE_FINISH;
        break;
      case GTK_RESPONSE_REJECT:
        state = STATE_MANUAL;
        break;
      case GTK_RESPONSE_ACCEPT:
        provider_node = ggg_provider_dialog_get_selected (GGG_PROVIDER_DIALOG (dialog));
        state = STATE_PLAN;
        break;
      }
      gtk_widget_destroy (dialog);
      break;
    case STATE_PLAN:
      g_assert (provider_node);
      dialog = g_object_new (GGG_TYPE_PLAN_DIALOG,
                             "provider", provider_node,
                             NULL);
      switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
      case GTK_RESPONSE_CANCEL:
      case GTK_RESPONSE_DELETE_EVENT:
        state = STATE_FINISH;
        break;
      case GTK_RESPONSE_REJECT:
        state = STATE_MANUAL;
        break;
      case GTK_RESPONSE_ACCEPT:
        plan_node = ggg_plan_dialog_get_selected (GGG_PLAN_DIALOG (dialog));
        state = STATE_SAVE;
        break;
      }
      gtk_widget_destroy (dialog);
      break;
    case STATE_MANUAL:
      dialog = g_object_new (GGG_TYPE_MANUAL_DIALOG, NULL);
      switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
      case GTK_RESPONSE_CANCEL:
      case GTK_RESPONSE_DELETE_EVENT:
        state = STATE_FINISH;
        break;
      case GTK_RESPONSE_ACCEPT:
        plan_node = ggg_manual_dialog_get_plan (GGG_MANUAL_DIALOG (dialog));
        state = STATE_SAVE;
        break;
      }
      break;
    case STATE_SAVE:
      g_assert (plan_node);

      ggg_service_set (service,
                       rest_xml_node_get_attr (plan_node, "value"),
                       get_child_content (plan_node, "username"),
                       get_child_content (plan_node, "password"));

      ggg_service_connect (service);

      state = STATE_FINISH;
      break;
    case STATE_FINISH:
      break;
    }

    g_assert (state != old_state);
  }
}
示例#8
0
static void
mex_suggest_complete_cb (MexDownloadQueue *queue,
                         const gchar      *uri,
                         const gchar      *buffer,
                         gsize             count,
                         const GError     *error,
                         gpointer          userdata)
{
  RestXmlNode *root, *n;
  RestXmlParser *parser;
  MexSearchPlugin *self = userdata;
  MexSearchPluginPrivate *priv = self->priv;

  priv->suggest_id = NULL;

  /* hide spinner */
  mx_spinner_set_animating (MX_SPINNER (priv->spinner), FALSE);
  clutter_actor_hide (priv->spinner);

  if (error)
    {
      g_warning ("Error querying Google suggestions: %s",
                 error->message);
      return;
    }

  parser = rest_xml_parser_new ();
  root = rest_xml_parser_parse_from_data (parser, buffer, count);

  if (!root)
    {
      g_warning ("Unknown error parsing Google suggestions XML");
      g_object_unref (parser);
      return;
    }

  /* Clear model */
  mex_model_clear (MEX_MODEL (priv->suggest_model));

  /* Add new suggestions to model */
  n = rest_xml_node_find (root, "CompleteSuggestion");
  for (; n; n = n->next)
    {
      MexContent *content;
      const gchar *suggestion;

      RestXmlNode *node = rest_xml_node_find (n, "suggestion");

      if (!node)
        continue;

      suggestion = rest_xml_node_get_attr (node, "data");

      if (!suggestion)
        continue;

      content = MEX_CONTENT (mex_program_new (priv->suggest_model));
      mex_content_set_metadata (content, MEX_CONTENT_METADATA_TITLE,
                                suggestion);
      mex_content_set_metadata (content, MEX_CONTENT_METADATA_MIMETYPE,
                                "x-mex/search");
      mex_model_add_content (MEX_MODEL (priv->suggest_model), content);
    }

  /* Unref */
  rest_xml_node_unref (root);
  g_object_unref (parser);
}