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.actions;
22 
23 import java.util.List;
24 
25 import net.sourceforge.atunes.model.IAudioObject;
26 import net.sourceforge.atunes.model.IPlayListHandler;
27 import net.sourceforge.atunes.model.IRepositoryHandler;
28 import net.sourceforge.atunes.model.IUnknownObjectChecker;
29 import net.sourceforge.atunes.utils.CollectionUtils;
30 
31 import org.apache.commons.lang.StringUtils;
32 
33 /**
34  *
35  * This action by default returns the current song info and with a parameter
36  * exists prints out the info of the file specified.
37  */
38 public class PrintSongInfoRemoteAction extends RemoteAction {
39 
40 	private static final String SEPARATOR = "\n";
41 
42 	private static final long serialVersionUID = 1742363002472924706L;
43 
44 	private IRepositoryHandler repositoryHandler;
45 
46 	private IPlayListHandler playListHandler;
47 
48 	private IUnknownObjectChecker unknownObjectChecker;
49 
50 	/**
51 	 * @param unknownObjectChecker
52 	 */
setUnknownObjectChecker( final IUnknownObjectChecker unknownObjectChecker)53 	public void setUnknownObjectChecker(
54 			final IUnknownObjectChecker unknownObjectChecker) {
55 		this.unknownObjectChecker = unknownObjectChecker;
56 	}
57 
58 	/**
59 	 * @param handler
60 	 */
setPlayListHandler(final IPlayListHandler handler)61 	public void setPlayListHandler(final IPlayListHandler handler) {
62 		this.playListHandler = handler;
63 	}
64 
65 	/**
66 	 * @param handler
67 	 */
setRepositoryHandler(final IRepositoryHandler handler)68 	public void setRepositoryHandler(final IRepositoryHandler handler) {
69 		this.repositoryHandler = handler;
70 	}
71 
72 	@Override
runCommand(final List<String> parameters)73 	public String runCommand(final List<String> parameters) {
74 		String errorResponse = null;
75 		IAudioObject object = null;
76 		if (CollectionUtils.isEmpty(parameters)) {
77 			object = this.playListHandler
78 					.getCurrentAudioObjectFromCurrentPlayList();
79 			if (object == null) {
80 				errorResponse = "Nothing is playing";
81 			}
82 		} else {
83 			String name = StringUtils.join(parameters, ' ');
84 			object = this.repositoryHandler.getFile(name);
85 			if (object == null) {
86 				errorResponse = "File not found";
87 			}
88 		}
89 
90 		if (object != null) {
91 			return returnInformation(object);
92 		}
93 
94 		return errorResponse;
95 	}
96 
97 	/**
98 	 * @param object
99 	 */
returnInformation(final IAudioObject object)100 	private String returnInformation(final IAudioObject object) {
101 		int mins = object.getDuration() / 60;
102 		int sec = object.getDuration() % 60;
103 		return net.sourceforge.atunes.utils.StringUtils.getString("Name: ",
104 				object.getTitleOrFileName(), SEPARATOR, "Artist: ",
105 				object.getArtist(this.unknownObjectChecker), SEPARATOR,
106 				"Album: ", object.getAlbum(this.unknownObjectChecker),
107 				SEPARATOR, "Year: ", object.getYear(this.unknownObjectChecker),
108 				SEPARATOR, "Duration: ", mins, ":", sec);
109 	}
110 
111 	@Override
getHelpText()112 	protected String getHelpText() {
113 		return "Prints out current song information, or the song information of the specified filename";
114 	}
115 
116 	@Override
getOptionalParameters()117 	protected String getOptionalParameters() {
118 		return "[fileName]";
119 	}
120 }
121