~vcs-imports/gnome-media/main

« back to all changes in this revision

Viewing changes to cddb-slave2/cddb-slave.c

  • Committer: thomasvs
  • Date: 2004-05-10 08:52:25 UTC
  • Revision ID: vcs-imports@canonical.com-20040510085225-zb6tqc12yi1h08gd
remove unneccesary whitespace

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * cddb-slave.c: Implementation for the GNOME/Media/CDDBSlave2 interface.
3
3
 *
4
4
 * Copyright (C) 2001-2002 Iain Holmes.
5
 
 * 
 
5
 *
6
6
 * Authors: Iain Holmes  <iain@ximian.com>
7
7
 */
8
8
 
83
83
        GTcpSocket *socket;
84
84
        GIOChannel *iochannel;
85
85
        guint tag;
86
 
        
 
86
 
87
87
        ConnectionMode mode;
88
 
        
 
88
 
89
89
        char *discid;
90
90
        int ntrks;
91
91
        char *offsets;
118
118
        CORBA_any any;
119
119
        GNOME_Media_CDDBSlave2_QueryResult qr;
120
120
        GList *l;
121
 
        
 
121
 
122
122
        g_return_if_fail (cddb != NULL);
123
123
        g_return_if_fail (IS_CDDB_SLAVE (cddb));
124
124
 
136
136
                CDDBSlave *slave;
137
137
 
138
138
                slave = CDDB_SLAVE (l->data);
139
 
                
 
139
 
140
140
                bonobo_event_source_notify_listeners (slave->priv->event_source,
141
141
                                                      CDDB_SLAVE_CDDB_FINISHED,
142
142
                                                      &any, NULL);
171
171
        if (cd->tag) {
172
172
                g_source_remove (cd->tag);
173
173
        }
174
 
        
 
174
 
175
175
        gnet_tcp_socket_unref (cd->socket);
176
176
        g_io_channel_unref (cd->iochannel);
177
 
        
 
177
 
178
178
        /* Notify listeners */
179
179
        cddb_slave_notify_listeners (cddb, cd->discid, GNOME_Media_CDDBSlave2_OK);
180
180
 
195
195
                g_list_free (cd->matches);
196
196
        }
197
197
        g_free (cd);
198
 
        
 
198
 
199
199
        return FALSE;
200
200
}
201
201
 
229
229
        char *quit;
230
230
        guint bytes_writen;
231
231
        GIOError status;
232
 
        
 
232
 
233
233
        /* Send quit command */
234
234
        status = gnet_io_channel_writen (cd->iochannel, "quit\n",
235
235
                                         5, &bytes_writen);
251
251
        gboolean disconnect = FALSE;
252
252
        static gboolean waiting_for_terminator = FALSE;
253
253
        static FILE *handle = NULL;
254
 
        
 
254
 
255
255
        if (waiting_for_terminator == TRUE) {
256
256
                code = 210;
257
257
        } else {
264
264
                        g_print ("CDDB: Received data.\n");
265
265
                        g_print ("CDDB: %s\n", response);
266
266
                }
267
 
                
 
267
 
268
268
                if (waiting_for_terminator == FALSE) {
269
269
                        /* Open the file */
270
270
                        char *filename, *dirname;
276
276
                        if (cddb_debugging == TRUE) {
277
277
                                g_print ("CDDB: Opening %s\n", filename);
278
278
                        }
279
 
                        
 
279
 
280
280
                        handle = fopen (filename, "w");
281
 
                        
 
281
 
282
282
                        if (handle == NULL) {
283
283
                                g_warning ("Could not open %s\n", filename);
284
284
                                g_free (filename);
290
290
                } else {
291
291
 
292
292
                        g_assert (handle != NULL);
293
 
                
 
293
 
294
294
                        /* Write the line */
295
295
                        if (cddb_debugging == TRUE) {
296
296
                                g_print ("CDDB: Writing %s\n", response);
297
297
                        }
298
 
                        
 
298
 
299
299
                        fputs (response, handle);
300
300
                }
301
301
 
302
302
                if (response[0] == '.') {
303
303
                        CDDBEntry *entry;
304
304
                        char *filename, *dirname;
305
 
                        
 
305
 
306
306
                        /* Found terminator */
307
307
                        fclose (handle);
308
308
 
309
309
                        dirname = gnome_util_prepend_user_home (".cddbslave");
310
310
                        filename = g_concat_dir_and_file (dirname, cd->discid);
311
311
                        g_free (dirname);
312
 
                        
 
312
 
313
313
                        entry = g_hash_table_lookup (cddb_cache, cd->discid);
314
314
                        if (cddb_debugging == TRUE) {
315
315
                                g_print ("CDDB: Parsing %s\n", filename);
316
316
                        }
317
317
                        cddb_entry_parse_file (entry, filename);
318
318
                        g_free (filename);
319
 
                        
 
319
 
320
320
                        /* Reset the static variables for next time */
321
321
                        handle = NULL;
322
322
                        more = FALSE;
374
374
        if (disconnect == TRUE) {
375
375
                do_goodbye (cd);
376
376
        }
377
 
                
 
377
 
378
378
        return more;
379
379
}
380
380
 
387
387
        char *query;
388
388
        guint bytes_writen;
389
389
        GIOError status;
390
 
        
 
390
 
391
391
        /* Send read command */
392
392
        query = g_strdup_printf ("cddb read %s %s\n", cat, discid);
393
393
        status = gnet_io_channel_writen (cd->iochannel, query,
412
412
        for (l = list; l; l = l->next) {
413
413
                char **vector;
414
414
                char *end;
415
 
                
 
415
 
416
416
                vector = (char **) l->data;
417
417
                /* Strip newlines and \r */
418
418
                vector[2][strlen (vector[2]) - 1] = 0;
420
420
                if (end != NULL) {
421
421
                        *end = 0;
422
422
                }
423
 
                
 
423
 
424
424
                gtk_list_store_append (store, &iter);
425
425
                gtk_list_store_set (store, &iter, 0, vector[0], 1, vector[1], 2, vector[2], -1);
426
426
        }
427
427
 
428
428
        return GTK_TREE_MODEL (store);
429
429
}
430
 
                
 
430
 
431
431
static char **
432
432
display_results (ConnectionData *cd)
433
433
{
439
439
        GtkTreeIter iter;
440
440
        char **vector;
441
441
        int i;
442
 
        
 
442
 
443
443
        window = gtk_dialog_new_with_buttons (_("Multiple matches..."),
444
444
                                              NULL, 0,
445
445
                                              GTK_STOCK_CANCEL, 0, GTK_STOCK_OK, 1, NULL);
450
450
                                 "best match"));
451
451
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), label, FALSE, FALSE, 0);
452
452
        gtk_widget_show (label);
453
 
        
 
453
 
454
454
        model = create_model_from_list (cd->matches);
455
455
        list = gtk_tree_view_new_with_model (model);
456
456
        g_object_unref (model);
459
459
        col = gtk_tree_view_column_new_with_attributes (_("Category"), cell,
460
460
                                                        "text", 0, NULL);
461
461
        gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
462
 
        
 
462
 
463
463
        col = gtk_tree_view_column_new_with_attributes (_("Disc ID"), cell,
464
464
                                                        "text", 1, NULL);
465
465
        gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
466
 
        
 
466
 
467
467
        col = gtk_tree_view_column_new_with_attributes (_("Artist and Title"), cell,
468
468
                                                        "text", 2, NULL);
469
469
        gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
470
 
        
 
470
 
471
471
        sw = gtk_scrolled_window_new (NULL, NULL);
472
472
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
473
473
                                        GTK_POLICY_AUTOMATIC,
495
495
                        gtk_widget_destroy (window);
496
496
                        return NULL;
497
497
                }
498
 
                
 
498
 
499
499
                gtk_widget_destroy (window);
500
500
                return NULL;
501
501
 
518
518
        char **vector;
519
519
        static gboolean waiting_for_terminator = FALSE;
520
520
        gboolean disconnect = FALSE;
521
 
        
 
521
 
522
522
        if (waiting_for_terminator == TRUE) {
523
523
                code = 211;
524
524
        } else {
525
525
                code = atoi (response);
526
526
        }
527
 
        
 
527
 
528
528
        switch (code) {
529
529
        case 200:
530
530
                if (cddb_debugging == TRUE) {
555
555
                        g_print ("CDDB: Multiple matches found.\n");
556
556
                        g_print ("CDDB: %s\n", response);
557
557
                }
558
 
                
 
558
 
559
559
                if (response[0] == '.') {
560
560
                        /* Terminator */
561
561
                        char **result;
562
562
                        GList *l;
563
 
                        
 
563
 
564
564
                        waiting_for_terminator = FALSE;
565
565
                        if (cd->matches && cd->matches->next == NULL) {
566
566
                                /* There is only one match, even though
578
578
                                        discid = result[1];
579
579
                                        dtitle = result[2];
580
580
                                        g_free (result);
581
 
                                        
 
581
 
582
582
                                } else {
583
583
                                /* Need to disconnect here...
584
584
                                   none of our matches matched */
593
593
                        }
594
594
                        g_list_free (cd->matches);
595
595
                        cd->matches = NULL;
596
 
                        
 
596
 
597
597
                        more = FALSE;
598
598
                        break;
599
599
                }
606
606
                                }
607
607
                                return FALSE;
608
608
                        }
609
 
                        
 
609
 
610
610
                        /* Add the vector to the list of matches */
611
611
                        cd->matches = g_list_append (cd->matches, vector);
612
612
                }
665
665
                clear_entry_from_cache (cd->discid);
666
666
                do_goodbye (cd);
667
667
        }
668
 
        
 
668
 
669
669
        g_free (cat);
670
670
        g_free (discid);
671
671
        g_free (dtitle);
678
678
        char *query;
679
679
        guint bytes_writen;
680
680
        GIOError status;
681
 
        
 
681
 
682
682
        /* Send query command */
683
683
        query = g_strdup_printf ("cddb query %s %d %s %d\n",
684
 
                                 cd->discid, cd->ntrks, 
 
684
                                 cd->discid, cd->ntrks,
685
685
                                 cd->offsets, cd->nsecs);
686
686
        status = gnet_io_channel_writen (cd->iochannel, query,
687
687
                                         strlen (query), &bytes_writen);
748
748
        char *hello;
749
749
        guint bytes_writen;
750
750
        GIOError status;
751
 
        
 
751
 
752
752
        /* Send the Hello command
753
753
           CDDB howto says these shouldn't be hardcoded,
754
754
           but that seems to be a privacy issue */
757
757
                                 cd->cddb->priv->hostname,
758
758
                                 cd->name,
759
759
                                 cd->version);
760
 
        
 
760
 
761
761
        /* Need to check the return of this one */
762
762
        status = gnet_io_channel_writen (cd->iochannel, hello,
763
763
                                         strlen (hello), &bytes_writen);
785
785
                        g_print ("CDDB: Hello ok - Read/Write access allowed\n");
786
786
                        g_print ("CDDB: %s\n", response);
787
787
                }
788
 
                
 
788
 
789
789
                cddb->priv->access = CDDB_ACCESS_READWRITE;
790
790
                break;
791
 
                
 
791
 
792
792
        case 201:
793
793
                if (cddb_debugging == TRUE) {
794
794
                        g_print ("CDDB: Hello ok - Read only access\n");
796
796
                }
797
797
                cddb->priv->access = CDDB_ACCESS_READONLY;
798
798
                break;
799
 
                
 
799
 
800
800
        case 432:
801
801
                if (cddb_debugging == TRUE) {
802
802
                        g_print ("CDDB: No more connections allowed\n");
841
841
 
842
842
        return FALSE;
843
843
}
844
 
        
 
844
 
845
845
static gboolean
846
846
read_from_server (GIOChannel *iochannel,
847
847
                  GIOCondition condition,
864
864
                if (condition & G_IO_NVAL) {
865
865
                        g_print ("G_IO_NVAL\n");
866
866
                }
867
 
                
 
867
 
868
868
                goto error;
869
869
        }
870
870
 
877
877
                                                &bytes_read, NULL, NULL);
878
878
                while (status == G_IO_STATUS_NORMAL) {
879
879
                        gboolean more = FALSE;
880
 
                        
 
880
 
881
881
                        switch (cd->mode) {
882
882
                        case CONNECTION_MODE_NEED_HELLO:
883
883
                                more = do_open_response (cd, buffer);
898
898
                        case CONNECTION_MODE_NEED_GOODBYE:
899
899
                                more = do_goodbye_response (cd, buffer);
900
900
                                break;
901
 
                                
 
901
 
902
902
                        default:
903
903
                                g_print ("Dunno what to do with %s\n", buffer);
904
904
                                more = FALSE;
952
952
        if (cddb_debugging == TRUE) {
953
953
                g_print ("CDDB: Opened connection, adding watch\n");
954
954
        }
955
 
        
 
955
 
956
956
        cd->tag = g_io_add_watch (sin, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
957
957
                                  read_from_server, data);
958
958
}
967
967
                         data->cddb->priv->server,
968
968
                         data->cddb->priv->port);
969
969
        }
970
 
        
 
970
 
971
971
        sock = gnet_tcp_socket_connect_async (data->cddb->priv->server,
972
972
                                              data->cddb->priv->port,
973
973
                                              open_cb, data);
1080
1080
        /* Make an entry */
1081
1081
        entry = cddb_entry_new (discid, ntrks, offsets, nsecs);
1082
1082
        g_hash_table_insert (cddb_cache, g_strdup (discid), entry);
1083
 
        
 
1083
 
1084
1084
        cd = g_new (ConnectionData, 1);
1085
1085
        cd->cddb = cddb;
1086
1086
        g_object_ref (cddb);
1087
1087
 
1088
1088
        cd->tag = 0;
1089
 
        
 
1089
 
1090
1090
        cd->name = g_strdup_printf ("%s(CDDBSlave2)", name);
1091
1091
        cd->version = g_strdup (version);
1092
1092
        cd->discid = g_strdup (discid);
1107
1107
        CDDBSlave *cddb;
1108
1108
        CDDBEntry *entry;
1109
1109
        gboolean write_ret;
1110
 
        
 
1110
 
1111
1111
        entry = g_hash_table_lookup (cddb_cache, discid);
1112
1112
        if (entry == NULL) {
1113
1113
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1158
1158
        char *split, *artist;
1159
1159
        GString *dtitle;
1160
1160
        CORBA_char *ret;
1161
 
        
 
1161
 
1162
1162
        entry = g_hash_table_lookup (cddb_cache, discid);
1163
1163
        if (entry == NULL) {
1164
1164
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1182
1182
                ret = CORBA_string_dup (artist);
1183
1183
                g_free (artist);
1184
1184
        }
1185
 
        
 
1185
 
1186
1186
        return ret;
1187
1187
}
1188
1188
 
1218
1218
                g_strfreev (split);
1219
1219
        }
1220
1220
}
1221
 
                
 
1221
 
1222
1222
static CORBA_char *
1223
1223
impl_GNOME_Media_CDDBSlave2_getDiscTitle (PortableServer_Servant servant,
1224
1224
                                          const CORBA_char *discid,
1311
1311
{
1312
1312
        CDDBEntry *entry;
1313
1313
        int ntrk;
1314
 
        
 
1314
 
1315
1315
        entry = g_hash_table_lookup (cddb_cache, discid);
1316
1316
        if (entry == NULL) {
1317
1317
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1353
1353
                }
1354
1354
 
1355
1355
                (*names_list)->_buffer[ntrk].length = entry->lengths[ntrk];
1356
 
                
 
1356
 
1357
1357
                (*names_list)->_length++;
1358
1358
 
1359
1359
                g_free (name);
1386
1386
{
1387
1387
        CDDBEntry *entry;
1388
1388
        int ntrks, i;
1389
 
        
 
1389
 
1390
1390
        entry = g_hash_table_lookup (cddb_cache, discid);
1391
1391
        if (entry == NULL) {
1392
1392
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1399
1399
        for (i = 0; i < ntrks; i++) {
1400
1400
                char *name, *comment, *key;
1401
1401
                GString *dentry;
1402
 
                
 
1402
 
1403
1403
                name = list->_buffer[i].name;
1404
1404
                comment = list->_buffer[i].comment;
1405
1405
 
1424
1424
{
1425
1425
        CDDBEntry *entry;
1426
1426
        GString *dcomment;
1427
 
        
 
1427
 
1428
1428
        entry = g_hash_table_lookup (cddb_cache, discid);
1429
1429
        if (entry == NULL) {
1430
1430
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1484
1484
                g_warning ("No entry %s", discid);
1485
1485
                return -1;
1486
1486
        }
1487
 
        
 
1487
 
1488
1488
        dyear = g_hash_table_lookup (entry->fields, "DYEAR");
1489
1489
        if (dyear == NULL) {
1490
1490
                return -1;
1501
1501
{
1502
1502
        CDDBEntry *entry;
1503
1503
        GString *dyear;
1504
 
        
 
1504
 
1505
1505
        entry = g_hash_table_lookup (cddb_cache, discid);
1506
1506
        if (entry == NULL) {
1507
1507
                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
1536
1536
                g_warning ("No entry %s", discid);
1537
1537
                return CORBA_string_dup ("");
1538
1538
        }
1539
 
        
 
1539
 
1540
1540
        dgenre = g_hash_table_lookup (entry->fields, "DGENRE");
1541
1541
        if (dgenre == NULL) {
1542
1542
                return CORBA_string_dup ("");
1586
1586
 
1587
1587
        /* Remove this slave from our list */
1588
1588
        cddb_slaves = g_list_remove (cddb_slaves, cddb);
1589
 
        
 
1589
 
1590
1590
        g_free (priv->server);
1591
1591
        g_free (priv->name);
1592
1592
        g_free (priv->hostname);
1593
 
        
 
1593
 
1594
1594
        g_free (priv);
1595
1595
        cddb->priv = NULL;
1596
1596
 
1639
1639
}
1640
1640
 
1641
1641
BONOBO_TYPE_FUNC_FULL (CDDBSlave, GNOME_Media_CDDBSlave2, PARENT_TYPE, cddb_slave);
1642
 
                         
 
1642
 
1643
1643
 
1644
1644
#define CDDB_SERVER "freedb.freedb.org"
1645
1645
#define CDDB_PORT 888
1650
1650
 * @port: Port on @server to connect to.
1651
1651
 * @event_source: #BonoboEventSource on which to emit events.
1652
1652
 *
1653
 
 * Creates a new #CDDBSlave which will connect to @server:@port. Any events 
 
1653
 * Creates a new #CDDBSlave which will connect to @server:@port. Any events
1654
1654
 * that are created will be emitted on @event_source.
1655
1655
 * If @server is NULL, the default server (freedb.freedb.org) is used.
1656
1656
 * If @port is 0, the default port (888) is used.
1686
1686
 
1687
1687
        priv->name = g_strdup (name);
1688
1688
        priv->hostname = g_strdup (hostname);
1689
 
        
 
1689
 
1690
1690
        priv->event_source = event_source;
1691
1691
        bonobo_object_add_interface (BONOBO_OBJECT (cddb),
1692
1692
                                     BONOBO_OBJECT (priv->event_source));
1694
1694
 
1695
1695
        /* Add this slave to our global list of slaves */
1696
1696
        cddb_slaves = g_list_prepend (cddb_slaves, cddb);
1697
 
        
 
1697
 
1698
1698
        return cddb;
1699
1699
}
1700
1700
/**
1702
1702
 * @server: Address of server to connect to.
1703
1703
 * @port: Port on @server to connect to.
1704
1704
 *
1705
 
 * Creates a new #CDDBSlave which will connect to @server:@port. Any events 
 
1705
 * Creates a new #CDDBSlave which will connect to @server:@port. Any events
1706
1706
 * that are created will be emitted on the default event source.
1707
1707
 * If @server is NULL, the default server (freedb.freedb.org) is used.
1708
1708
 * If @port is 0, the default port (888) is used.
1752
1752
                       const char *server)
1753
1753
{
1754
1754
        g_return_if_fail (IS_CDDB_SLAVE (cddb));
1755
 
        
 
1755
 
1756
1756
        g_free (cddb->priv->server);
1757
1757
        if (server == NULL) {
1758
1758
                cddb->priv->server = g_strdup (CDDB_SERVER);