25
25
using System . Collections ;
26
26
using System . Collections . Generic ;
27
27
using System . Globalization ;
28
+ using System . Linq ;
28
29
29
30
namespace TagLib . Mpeg4
30
31
{
@@ -402,9 +403,28 @@ public void DetachIlst ()
402
403
/// <returns>Text string from data box</returns>
403
404
public string GetDashBox ( string meanstring , string namestring )
404
405
{
405
- AppleDataBox data_box = GetDashAtoms ( meanstring , namestring ) ;
406
- if ( data_box != null ) {
407
- return data_box . Text ;
406
+ List < AppleDataBox > data_boxes = GetDashAtoms ( meanstring , namestring ) ;
407
+ if ( data_boxes != null ) {
408
+ return data_boxes [ 0 ] . Text ;
409
+ } else {
410
+ return null ;
411
+ }
412
+ }
413
+
414
+ /// <summary>
415
+ /// Gets the text strings from a specific data boxes in Dash (----) atoms
416
+ /// </summary>
417
+ /// <param name="meanstring">String specifying text from mean box</param>
418
+ /// <param name="namestring">String specifying text from name box</param>
419
+ /// <returns>Text string from data box</returns>
420
+ public string [ ] GetDashBoxes ( string meanstring , string namestring )
421
+ {
422
+ List < AppleDataBox > data_boxes = GetDashAtoms ( meanstring , namestring ) ;
423
+ if ( data_boxes != null ) {
424
+ string [ ] box_text = new string [ data_boxes . Count ] ;
425
+ for ( int i = 0 ; i < data_boxes . Count ; i ++ )
426
+ box_text [ i ] = data_boxes [ i ] . Text ;
427
+ return box_text ;
408
428
} else {
409
429
return null ;
410
430
}
@@ -420,7 +440,7 @@ public string GetDashBox (string meanstring, string namestring)
420
440
/// <param name="datastring">String specifying text for data box</param>
421
441
public void SetDashBox ( string meanstring , string namestring , string datastring )
422
442
{
423
- AppleDataBox data_box = GetDashAtoms ( meanstring , namestring ) ;
443
+ AppleDataBox data_box = GetDashAtom ( meanstring , namestring ) ;
424
444
425
445
// If we did find a data_box and we have an empty datastring we should
426
446
// remove the entire dash box.
@@ -449,13 +469,95 @@ public void SetDashBox (string meanstring, string namestring, string datastring)
449
469
}
450
470
}
451
471
472
+ /// <summary>
473
+ /// Sets specific strings in Dash (----) atom. This method updates
474
+ /// existing atoms, or creates new one. If an empty datastring is
475
+ /// specified, the Dash boxes and its children are removed.
476
+ /// </summary>
477
+ /// <param name="meanstring">String specifying text for mean box</param>
478
+ /// <param name="namestring">String specifying text for name box</param>
479
+ /// <param name="datastring">String values specifying text for data boxes</param>
480
+ public void SetDashBoxes ( string meanstring , string namestring , string [ ] datastring )
481
+ {
482
+ List < AppleDataBox > data_boxes = GetDashAtoms ( meanstring , namestring ) ;
483
+
484
+ // If we did find a data_box and we have an empty datastring we should
485
+ // remove the entire dash box.
486
+ if ( data_boxes != null && string . IsNullOrEmpty ( datastring [ 0 ] ) ) {
487
+ AppleAnnotationBox dash_box = GetParentDashBox ( meanstring , namestring ) ;
488
+ dash_box . ClearChildren ( ) ;
489
+ ilst_box . RemoveChild ( dash_box ) ;
490
+ return ;
491
+ }
492
+
493
+ if ( data_boxes != null && data_boxes . Count == datastring . Length ) {
494
+ for ( int i = 0 ; i < data_boxes . Count ; i ++ )
495
+ data_boxes [ i ] . Text = datastring [ i ] ;
496
+ } else {
497
+ // Remove all Boxes
498
+ AppleAnnotationBox dash_box = GetParentDashBox ( meanstring , namestring ) ;
499
+ if ( dash_box != null ) {
500
+ dash_box . ClearChildren ( ) ;
501
+ ilst_box . RemoveChild ( dash_box ) ;
502
+ }
503
+
504
+ var whole_box = new AppleAnnotationBox ( BoxType . DASH ) ;
505
+ foreach ( var text in datastring )
506
+ {
507
+ //Create the new boxes, should use 1 for text as a flag
508
+ var amean_box = new AppleAdditionalInfoBox ( BoxType . Mean , 0 , 1 ) ;
509
+ var aname_box = new AppleAdditionalInfoBox ( BoxType . Name , 0 , 1 ) ;
510
+ var adata_box = new AppleDataBox ( BoxType . Data , 1 ) ;
511
+ amean_box . Text = meanstring ;
512
+ aname_box . Text = namestring ;
513
+ adata_box . Text = text ;
514
+ whole_box . AddChild ( amean_box ) ;
515
+ whole_box . AddChild ( aname_box ) ;
516
+ whole_box . AddChild ( adata_box ) ;
517
+ ilst_box . AddChild ( whole_box ) ;
518
+ }
519
+ }
520
+ }
521
+
452
522
/// <summary>
453
523
/// Gets the AppleDataBox that corresponds to the specified mean and name values.
454
524
/// </summary>
455
525
/// <param name="meanstring">String specifying text for mean box</param>
456
526
/// <param name="namestring">String specifying text for name box</param>
457
527
/// <returns>Existing AppleDataBox or null if one does not exist</returns>
458
- AppleDataBox GetDashAtoms ( string meanstring , string namestring )
528
+ AppleDataBox GetDashAtom ( string meanstring , string namestring )
529
+ {
530
+ foreach ( Box box in ilst_box . Children ) {
531
+ if ( box . BoxType != BoxType . DASH )
532
+ continue ;
533
+
534
+ // Get the mean and name boxes, make sure
535
+ // they're legit, check the Text fields for
536
+ // a match. If we have a match return
537
+ // the AppleDatabox containing the data
538
+
539
+ var mean_box = ( AppleAdditionalInfoBox ) box . GetChild ( BoxType . Mean ) ;
540
+ var name_box = ( AppleAdditionalInfoBox ) box . GetChild ( BoxType . Name ) ;
541
+
542
+ if ( mean_box == null || name_box == null ||
543
+ mean_box . Text != meanstring ||
544
+ ! name_box . Text . Equals ( namestring , StringComparison . OrdinalIgnoreCase ) ) {
545
+ continue ;
546
+ } else {
547
+ return ( AppleDataBox ) box . GetChild ( BoxType . Data ) ;
548
+ }
549
+ }
550
+ // If we haven't returned the found box yet, there isn't one, return null
551
+ return null ;
552
+ }
553
+
554
+ /// <summary>
555
+ /// Gets the AppleDataBox that corresponds to the specified mean and name values.
556
+ /// </summary>
557
+ /// <param name="meanstring">String specifying text for mean box</param>
558
+ /// <param name="namestring">String specifying text for name box</param>
559
+ /// <returns>Existing AppleDataBox or null if one does not exist</returns>
560
+ List < AppleDataBox > GetDashAtoms ( string meanstring , string namestring )
459
561
{
460
562
foreach ( Box box in ilst_box . Children ) {
461
563
if ( box . BoxType != BoxType . DASH )
@@ -474,7 +576,7 @@ AppleDataBox GetDashAtoms (string meanstring, string namestring)
474
576
! name_box . Text . Equals ( namestring , StringComparison . OrdinalIgnoreCase ) ) {
475
577
continue ;
476
578
} else {
477
- return ( AppleDataBox ) box . GetChild ( BoxType . Data ) ;
579
+ return box . GetChildren ( BoxType . Data ) . Cast < AppleDataBox > ( ) . ToList ( ) ;
478
580
}
479
581
}
480
582
// If we haven't returned the found box yet, there isn't one, return null
@@ -1292,8 +1394,14 @@ public override string TitleSort {
1292
1394
/// http://musicbrainz.org/doc/PicardTagMapping
1293
1395
/// </remarks>
1294
1396
public override string MusicBrainzArtistId {
1295
- get { return GetDashBox ( "com.apple.iTunes" , "MusicBrainz Artist Id" ) ; }
1296
- set { SetDashBox ( "com.apple.iTunes" , "MusicBrainz Artist Id" , value ) ; }
1397
+ get {
1398
+ string [ ] artistIds = GetDashBoxes ( "com.apple.iTunes" , "MusicBrainz Artist Id" ) ;
1399
+ return artistIds == null ? null : string . Join ( "/" , artistIds ) ;
1400
+ }
1401
+ set {
1402
+ string [ ] artistIds = value . Split ( '/' ) ;
1403
+ SetDashBoxes ( "com.apple.iTunes" , "MusicBrainz Artist Id" , artistIds ) ;
1404
+ }
1297
1405
}
1298
1406
1299
1407
/// <summary>
@@ -1343,8 +1451,14 @@ public override string MusicBrainzReleaseId {
1343
1451
/// http://musicbrainz.org/doc/PicardTagMapping
1344
1452
/// </remarks>
1345
1453
public override string MusicBrainzReleaseArtistId {
1346
- get { return GetDashBox ( "com.apple.iTunes" , "MusicBrainz Album Artist Id" ) ; }
1347
- set { SetDashBox ( "com.apple.iTunes" , "MusicBrainz Album Artist Id" , value ) ; }
1454
+ get {
1455
+ string [ ] releaseArtistIds = GetDashBoxes ( "com.apple.iTunes" , "MusicBrainz Album Artist Id" ) ;
1456
+ return releaseArtistIds == null ? null : string . Join ( "/" , releaseArtistIds ) ;
1457
+ }
1458
+ set {
1459
+ string [ ] releaseArtistIds = value . Split ( '/' ) ;
1460
+ SetDashBoxes ( "com.apple.iTunes" , "MusicBrainz Album Artist Id" , releaseArtistIds ) ;
1461
+ }
1348
1462
}
1349
1463
1350
1464
/// <summary>
0 commit comments