1
package org.herac.tuxguitar.player.impl.jsa.assistant;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.InputStream;
7
import java.io.OutputStream;
9
import java.util.zip.ZipEntry;
10
import java.util.zip.ZipInputStream;
12
import org.herac.tuxguitar.gui.TuxGuitar;
13
import org.herac.tuxguitar.gui.system.config.TGConfigManager;
14
import org.herac.tuxguitar.player.base.MidiPlayerException;
15
import org.herac.tuxguitar.player.impl.jsa.midiport.MidiPortSynthesizer;
16
import org.herac.tuxguitar.player.impl.jsa.utils.MidiConfigUtils;
18
public class SBInstaller {
20
private static final String SB_PREFIX = "soundbank";
21
private static final String SB_EXTENSION = ".gm";
23
private boolean cancelled;
29
private MidiPortSynthesizer synthesizer;
30
private SBInstallerlistener listener;
32
public SBInstaller(URL url,File tmpPath,File dstPath,MidiPortSynthesizer synthesizer, SBInstallerlistener listener){
34
this.tmpPath = tmpPath;
35
this.dstPath = dstPath;
36
this.synthesizer = synthesizer;
37
this.listener = listener;
38
this.cancelled = false;
41
public void process(){
42
File tmpFile = new File(this.tmpPath.getAbsolutePath() + File.separator + "soundbank.zip");
43
boolean success = download(this.url, tmpFile);
44
if(success && !isCancelled()){
45
File sbFile = uncompress(tmpFile);
46
if(!isCancelled() && sbFile != null){
50
if( tmpFile.exists() ){
53
this.listener.notifyFinish();
56
private boolean download(URL url, File dst){
58
this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.downloading"));
60
InputStream is = url.openStream();
62
OutputStream os = new FileOutputStream(dst);
64
byte[] buffer = new byte[1024];
66
while(!isCancelled() && (length = is.read(buffer)) != -1 ){
67
os.write(buffer,0,length);
74
}catch(Throwable throwable){
75
throwable.printStackTrace();
80
private File uncompress(File file){
82
this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.uncompressing",new String[]{file.getAbsolutePath()}));
85
File soundbank = null;
87
ZipInputStream is = new ZipInputStream(new FileInputStream(file));
88
ZipEntry entry = null;
89
while( ( entry = is.getNextEntry() ) != null ){
91
String name = entry.getName();
92
if(name.indexOf(SB_PREFIX) == 0 && name.indexOf(SB_EXTENSION) == (name.length() - SB_EXTENSION.length())){
93
soundbank = new File(this.dstPath.getAbsolutePath() + File.separator + name);
94
OutputStream os = new FileOutputStream(soundbank);
95
byte[] buffer = new byte[1024];
97
while( (length = is.read(buffer)) != -1){
98
os.write(buffer,0,length);
109
}catch(Throwable throwable){
110
throwable.printStackTrace();
115
private void install(File file){
117
this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.installing",new String[]{file.getAbsolutePath()}));
119
if( ! this.synthesizer.loadSoundbank(file) ){
120
this.listener.notifyFailed(new MidiPlayerException(TuxGuitar.getProperty("jsa.error.soundbank.custom")));
124
TGConfigManager config = MidiConfigUtils.getConfig();
125
config.setProperty(MidiConfigUtils.SOUNDBANK_KEY,file.getAbsolutePath());
127
} catch (Throwable throwable) {
128
throwable.printStackTrace();
132
public boolean isCancelled() {
133
return this.cancelled;
136
public void setCancelled(boolean cancelled) {
137
this.cancelled = cancelled;