forked from notpod/Notpod-1.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncPatternTranslator.cs
155 lines (139 loc) · 6.28 KB
/
SyncPatternTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Text;
using iTunesLib;
using Notpod.Configuration12;
using log4net;
namespace Notpod
{
/// <summary>
/// Contains methods for translating a synchronization patterns into a file paths.
/// </summary>
public class SyncPatternTranslator
{
private static ILog l = LogManager.GetLogger(typeof(SyncPatternTranslator));
/// <summary>
/// Translate a SyncPattern into a string using the provided IITFileOrCDTrack.
/// Currently translating the following macros:
/// %ARTIST% = The artist name
/// %ALBUM% = The album name
/// %NAME% = The track name
/// %TRACKNUMSPACE% = The track number with a trailing space
/// %TRACKNUM% = The track number (no trailing space)
/// </summary>
/// <param name="pattern">SyncPattern to translate.</param>
/// <param name="track">iTunes track containing track information.</param>
/// <returns>A string representation of pattern and track.</returns>
public static string Translate(SyncPattern pattern, IITFileOrCDTrack track)
{
try
{
if (track == null)
l.Debug("Track is null!");
if (track.Location == null)
{
l.Debug("track.Location is null!");
throw new MissingTrackException(track);
}
string patternstring = ((track.Compilation && pattern.CompilationsPattern != null && pattern.CompilationsPattern.Length > 0) ? pattern.CompilationsPattern : pattern.Pattern);
patternstring = TranslateArtist(pattern, track, patternstring);
patternstring = TranslateAlbum(track.Album, patternstring);
patternstring = TranslateName(track, patternstring);
patternstring = TranslateTrackNumber(track, patternstring);
patternstring = TranslateExtension(track, patternstring);
l.Debug("patternstring=" + patternstring);
return FileNameUtils.ConvertIllegalCharacters(patternstring);
}
catch (MissingTrackException ex)
{
throw ex;
}
catch (Exception ex)
{
string message = "Failed to translate track information according to the "
+ "defined pattern for this device (" + ex.Message + ").";
l.Error(message, ex);
throw new ArgumentException(message, ex);
}
}
/// <summary>
/// Translate file extension.
/// </summary>
/// <param name="track"></param>
/// <param name="patternstring"></param>
/// <returns></returns>
private static string TranslateExtension(IITFileOrCDTrack track, string patternstring)
{
int extensionstart = track.Location.LastIndexOf(".");
patternstring = patternstring + track.Location.Substring(extensionstart, track.Location.Length - extensionstart);
return patternstring;
}
/// <summary>
/// Translate track name.
/// </summary>
/// <param name="track"></param>
/// <param name="patternstring"></param>
/// <returns></returns>
private static string TranslateName(IITFileOrCDTrack track, string patternstring)
{
patternstring = patternstring.Replace("%NAME%", (track.Name == null ? "Unknown Track" : track.Name));
return patternstring;
}
/// <summary>
/// Translate track number.
/// </summary>
/// <param name="track"></param>
/// <param name="patternstring"></param>
/// <returns></returns>
private static string TranslateTrackNumber(IITFileOrCDTrack track, string patternstring)
{
//Replace track number with a number only if the iTunes track has this field set
if (track.TrackNumber != 0)
{
//%TRACKNUMSPACE%
patternstring = patternstring.Replace("%TRACKNUMSPACE%",
(track.TrackNumber.ToString().Length == 1 ?
"0" + track.TrackNumber.ToString() : track.TrackNumber.ToString()) + " ");
//%TRACKNUM%
patternstring = patternstring.Replace("%TRACKNUM%",
(track.TrackNumber.ToString().Length == 1 ?
"0" + track.TrackNumber.ToString() : track.TrackNumber.ToString()));
}
else //If there are no track number set for the track
{
//%TRACKNUMSPACE%
patternstring = patternstring.Replace("%TRACKNUMSPACE%", "");
//%TRACKNUM%
patternstring = patternstring.Replace("%TRACKNUM%", "");
}
return patternstring;
}
/// <summary>
/// Translate track album.
/// </summary>
/// <param name="trackAlbum"></param>
/// <param name="patternstring"></param>
/// <returns></returns>
private static string TranslateAlbum(string trackAlbum, string patternstring)
{
string album = (trackAlbum == null ? "Unknown Album" : trackAlbum);
patternstring = patternstring.Replace("%ALBUM%", album);
return patternstring;
}
/// <summary>
/// Translate track artist.
/// </summary>
/// <param name="pattern"></param>
/// <param name="track"></param>
/// <param name="patternstring"></param>
/// <returns></returns>
private static string TranslateArtist(SyncPattern pattern, IITFileOrCDTrack track, string patternstring)
{
string artist = (track.Artist == null ? "Unknown Artist" : track.Artist);
if (track.Compilation && pattern.CompilationsPattern != null && pattern.CompilationsPattern.Length > 0)
artist = "Compilations";
patternstring = patternstring.Replace("%ARTIST%", artist);
return patternstring;
}
}
}