1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.context.audioobject;
22 
23 import net.sourceforge.atunes.kernel.modules.webservices.lyrics.Lyrics;
24 import net.sourceforge.atunes.model.IAudioObject;
25 import net.sourceforge.atunes.model.IContextInformationSource;
26 import net.sourceforge.atunes.model.ILyrics;
27 import net.sourceforge.atunes.model.ILyricsRetrieveOperation;
28 import net.sourceforge.atunes.model.ILyricsService;
29 import net.sourceforge.atunes.model.IUnknownObjectChecker;
30 
31 /**
32  * Context data source to retrieve lyrics
33  * @author alex
34  *
35  */
36 public class LyricsDataSource implements IContextInformationSource {
37 
38 	private ILyricsService lyricsService;
39 
40 	private ILyrics lyrics;
41 
42 	private IAudioObject audioObject;
43 
44 	private ILyricsRetrieveOperation lyricsRetrieveOperation;
45 
46 	private IUnknownObjectChecker unknownObjectChecker;
47 
48 	/**
49 	 * @param unknownObjectChecker
50 	 */
setUnknownObjectChecker( final IUnknownObjectChecker unknownObjectChecker)51 	public void setUnknownObjectChecker(
52 			final IUnknownObjectChecker unknownObjectChecker) {
53 		this.unknownObjectChecker = unknownObjectChecker;
54 	}
55 
56 	@Override
getData(final IAudioObject audioObject)57 	public void getData(final IAudioObject audioObject) {
58 		this.audioObject = audioObject;
59 		this.lyrics = getLyricsData(audioObject);
60 	}
61 
62 	/**
63 	 * @return
64 	 */
getAudioObject()65 	public IAudioObject getAudioObject() {
66 		return audioObject;
67 	}
68 
69 	/**
70 	 * @return
71 	 */
getLyrics()72 	public ILyrics getLyrics() {
73 		return lyrics;
74 	}
75 
76 	/**
77 	 * Returns lyrics
78 	 *
79 	 * @param audioObject
80 	 * @return
81 	 */
getLyricsData(final IAudioObject audioObject)82 	private ILyrics getLyricsData(final IAudioObject audioObject) {
83 		ILyrics lyricsData = null;
84 		// First check if tag contains the lyrics. Favour this over internet services.
85 		if (!audioObject.getLyrics().trim().isEmpty()) {
86 			lyricsData = new Lyrics(audioObject.getLyrics(), null);
87 		}
88 		// Query internet service for lyrics
89 		else {
90 			if (!audioObject.getTitle().trim().isEmpty() && !audioObject.getArtist(unknownObjectChecker).trim().isEmpty() && !audioObject.getArtist(unknownObjectChecker).equals(unknownObjectChecker.getUnknownArtist())) {
91 				lyricsRetrieveOperation = lyricsService.getLyricsRetrieveOperation(audioObject.getArtist(unknownObjectChecker).trim(), audioObject.getTitle().trim());
92 				lyricsData = lyricsRetrieveOperation.getLyrics();
93 			}
94 		}
95 
96 		if (lyricsData == null) {
97 			lyricsData = new Lyrics("", "");
98 		}
99 
100 		return lyricsData;
101 	}
102 
103 	/**
104 	 * @param lyricsService
105 	 */
setLyricsService(final ILyricsService lyricsService)106 	public void setLyricsService(final ILyricsService lyricsService) {
107 		this.lyricsService = lyricsService;
108 	}
109 
110 	@Override
cancel()111 	public void cancel() {
112 		if (lyricsRetrieveOperation != null) {
113 			lyricsRetrieveOperation.cancelRetrieve();
114 		}
115 	}
116 }
117