~sil/libubuntuone/restrict-domains

« back to all changes in this revision

Viewing changes to libubuntuone/u1-music-store.c

  • Committer: Tarmac
  • Author(s): Rodrigo Moya
  • Date: 2010-03-04 15:51:39 UTC
  • mfrom: (50.1.4 add-useful-signals)
  • Revision ID: rodrigo@megeve-20100304155139-48xiaiggk4fjzpub
Added signals to the music store widget needed for the Rhythmbox plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
enum {
58
58
        PREVIEW_MP3_SIGNAL,
59
59
        PLAY_LIBRARY_SIGNAL,
 
60
        URL_LOADED_SIGNAL,
 
61
        DOWNLOAD_FINISHED_SIGNAL,
60
62
        LAST_SIGNAL
61
63
};
62
64
 
116
118
                                                                    g_cclosure_marshal_VOID__STRING,
117
119
                                                                    G_TYPE_NONE, 1,
118
120
                                                                    G_TYPE_STRING);
 
121
        u1_music_store_signals[URL_LOADED_SIGNAL] = g_signal_new ("url-loaded",
 
122
                                                                  G_TYPE_FROM_CLASS (klass),
 
123
                                                                  (GSignalFlags) G_SIGNAL_RUN_LAST,
 
124
                                                                  G_STRUCT_OFFSET (U1MusicStoreClass, url_loaded),
 
125
                                                                  NULL,
 
126
                                                                  NULL,
 
127
                                                                  g_cclosure_marshal_VOID__STRING,
 
128
                                                                  G_TYPE_NONE, 1,
 
129
                                                                  G_TYPE_STRING);
 
130
        u1_music_store_signals[DOWNLOAD_FINISHED_SIGNAL] = g_signal_new ("download-finished",
 
131
                                                                         G_TYPE_FROM_CLASS (klass),
 
132
                                                                         (GSignalFlags) G_SIGNAL_RUN_LAST,
 
133
                                                                         G_STRUCT_OFFSET (U1MusicStoreClass, download_finished),
 
134
                                                                         NULL,
 
135
                                                                         NULL,
 
136
                                                                         g_cclosure_marshal_VOID__STRING,
 
137
                                                                         G_TYPE_NONE, 1,
 
138
                                                                         G_TYPE_STRING);
 
139
 
119
140
        object_class->finalize = u1_music_store_finalize;
120
141
}
121
142
 
185
206
        gchar **uri_parts;
186
207
        NotRegisteredReplacementData *nrrd = (NotRegisteredReplacementData *) user_data;
187
208
 
188
 
        g_debug ("got_new_credentials called");
189
 
 
190
209
        get_credentials_from_keyring (&oauth_token, &oauth_token_secret);
191
210
 
192
211
        /* Get the return_url parameter from the not-registered url */
242
261
{
243
262
        NotRegisteredReplacementData *nrrd = (NotRegisteredReplacementData *) user_data;
244
263
 
245
 
        g_debug ("Authorization denied");
246
 
 
247
264
        /* Free memory */
248
265
        g_free (nrrd->not_registered_url);
249
266
        g_free (nrrd);
271
288
{
272
289
        U1MusicStore *music_store = U1_MUSIC_STORE (user_data);
273
290
 
274
 
        g_debug ("Loading URL: %s", webkit_network_request_get_uri (request));
275
 
 
276
291
        /* Remove watching callback for the page */
277
292
        if (music_store->priv->watch_id != 0) {
278
293
                g_source_remove (music_store->priv->watch_id);
386
401
}
387
402
 
388
403
static void
389
 
parse_html_node (WebKitWebView *web_view, htmlNodePtr node, GHashTable *current_downloads)
 
404
add_watched_downloads_from_hash (gpointer key, gpointer value, gpointer user_data)
 
405
{
 
406
        GHashTable *watched_downloads = (GHashTable *) user_data;
 
407
 
 
408
        g_hash_table_insert (watched_downloads, g_strdup (key), g_strdup (key));
 
409
}
 
410
 
 
411
static void
 
412
parse_html_node (U1MusicStore *music_store, WebKitWebView *web_view, htmlNodePtr node, GHashTable *current_downloads)
390
413
{
391
414
        htmlNodePtr subnode;
 
415
        static GHashTable *watched_downloads = NULL;
392
416
 
393
417
        if (node == NULL)
394
418
                return;
395
419
 
 
420
        /* We add all tracked downloads to know which ones were started from us */
 
421
        if (watched_downloads == NULL)
 
422
                watched_downloads = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
423
 
 
424
        g_hash_table_foreach (current_downloads, (GHFunc) add_watched_downloads_from_hash, watched_downloads);
 
425
 
396
426
        for (subnode = node->children; subnode != NULL; subnode = subnode->next) {
397
427
                if (!g_strcmp0 ((const gchar *) subnode->name, "li")) {
398
428
                        xmlChar *prop;
414
444
                                        escaped_prop = g_string_append_c (escaped_prop, prop[i]);
415
445
                        }
416
446
 
417
 
                        g_debug ("Escaped prop = %s", escaped_prop->str);
418
 
 
419
447
                        full_path = g_build_filename (g_get_home_dir (),
420
448
                                                      ".ubuntuone/Purchased from Ubuntu One",
421
449
                                                      prop, NULL);
422
 
                        g_debug ("Full path = %s", full_path);
423
450
 
424
451
                        download = g_hash_table_lookup (current_downloads, full_path);
425
452
                        if (download != NULL) {
431
458
                                        percent = 0.0;
432
459
                                script = g_strdup_printf ("setProgressBar('%s', %.2f, '%ld of %ld bytes read');",
433
460
                                                          escaped_prop->str, percent, download->n_bytes_read, download->deflated_size);
 
461
                                if (percent == 1.0)
 
462
                                        g_signal_emit (music_store, u1_music_store_signals[DOWNLOAD_FINISHED_SIGNAL], 0,
 
463
                                                       (const gchar *) full_path);
434
464
                        } else {
435
465
                                /* If the file exists, the download is done */
436
 
                                if (g_file_test (full_path, G_FILE_TEST_EXISTS))
437
 
                                        script = g_strdup_printf ("setProgressBar('%s', 1.0, 'Completed', false);", escaped_prop->str);
438
 
                                else
 
466
                                if (g_file_test (full_path, G_FILE_TEST_EXISTS)) {
 
467
                                        if (g_hash_table_lookup (watched_downloads, (gconstpointer) full_path)) {
 
468
                                                script = g_strdup_printf ("setProgressBar('%s', 1.0, 'Completed', false);", escaped_prop->str);
 
469
                                                g_signal_emit (music_store, u1_music_store_signals[DOWNLOAD_FINISHED_SIGNAL], 0,
 
470
                                                               (const gchar *) full_path);
 
471
 
 
472
                                                /* Remove the file from the watched downloads */
 
473
                                                g_hash_table_remove (watched_downloads, (gconstpointer) full_path);
 
474
                                        } else
 
475
                                                script = g_strdup_printf ("setProgressBar('%s', 1.0, 'Completed', true);", escaped_prop->str);
 
476
                                } else
439
477
                                        script = g_strdup_printf ("setProgressBar('%s', 0.0, 'Transferring to your Ubuntu One storage');", escaped_prop->str);
440
478
                        }
441
479
 
445
483
                        g_free (full_path);
446
484
                        g_string_free (escaped_prop, TRUE);
447
485
                } else
448
 
                        parse_html_node (web_view, subnode, current_downloads);
 
486
                        parse_html_node (music_store, web_view, subnode, current_downloads);
449
487
        }
450
488
}
451
489
 
452
490
static void
453
 
update_status_in_library_page (WebKitWebView *web_view, WebKitWebFrame *frame)
 
491
update_status_in_library_page (U1MusicStore *music_store, WebKitWebView *web_view, WebKitWebFrame *frame)
454
492
{
455
493
        GString *html;
456
494
        WebKitWebDataSource *data_source;
468
506
                /* Parse HTML */
469
507
                html_doc = htmlParseDoc (html->str, NULL);
470
508
                root_node = xmlDocGetRootElement (html_doc);
471
 
                parse_html_node (web_view, root_node, current_downloads);
 
509
                parse_html_node (music_store, web_view, root_node, current_downloads);
472
510
 
473
511
                /* Free memory */
474
512
                xmlFreeDoc (html_doc);
481
519
{
482
520
        U1MusicStore *music_store = U1_MUSIC_STORE (user_data);
483
521
 
484
 
        update_status_in_library_page (WEBKIT_WEB_VIEW (music_store->priv->web_viewer),
 
522
        update_status_in_library_page (music_store,
 
523
                                       WEBKIT_WEB_VIEW (music_store->priv->web_viewer),
485
524
                                       music_store->priv->frame_to_use);
486
525
 
487
526
        return TRUE;
510
549
                if (library_override != NULL) {
511
550
                        g_debug ("overriding the library page to ours");
512
551
                        webkit_web_view_open (web_view, library_override);
 
552
                        return;
513
553
                }
514
554
        }
515
555
 
517
557
        tmp_url = g_strdup_printf ("%s%s", music_store->priv->base_url, U1_LIBRARY_URL);
518
558
        if (g_str_has_prefix (webkit_web_view_get_uri (web_view), tmp_url)) {
519
559
                music_store->priv->frame_to_use = frame;
520
 
                update_status_in_library_page (web_view, frame);
 
560
                update_status_in_library_page (music_store, web_view, frame);
521
561
 
522
562
                /* Inject JavaScript to make songs clickable */
523
563
                execute_script (web_view, "u1-songs-clickable.js", NULL);
527
567
        }
528
568
 
529
569
        g_free (tmp_url);
 
570
 
 
571
        /* Signal users the URL finished loading */
 
572
        g_signal_emit (music_store, u1_music_store_signals[URL_LOADED_SIGNAL], 0,
 
573
                       (const gchar *) webkit_web_view_get_uri (music_store->priv->web_viewer));
530
574
}
531
575
 
532
576
static void
627
671
        gchar *new_user_agent;
628
672
        GError *error = NULL;
629
673
 
 
674
        if (!g_thread_get_initialized ())
 
675
                g_thread_init (NULL);
 
676
 
630
677
        music_store->priv = g_new0 (U1MusicStorePrivate, 1);
631
678
 
632
679
        /* Initialize DBus */
664
711
        new_user_agent = g_strdup_printf ("%s U1MusicStore/" VERSION ,
665
712
                                          webkit_web_settings_get_user_agent (
666
713
                                                  webkit_web_view_get_settings (WEBKIT_WEB_VIEW (music_store->priv->web_viewer))));
667
 
        g_debug (new_user_agent);
668
714
        
669
715
        g_object_set (G_OBJECT (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (music_store->priv->web_viewer))),
670
716
                      "user-agent", new_user_agent,