~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-jsa/src/org/herac/tuxguitar/player/impl/jsa/assistant/SBInstaller.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.player.impl.jsa.assistant;
 
2
 
 
3
import java.io.File;
 
4
import java.io.FileInputStream;
 
5
import java.io.FileOutputStream;
 
6
import java.io.InputStream;
 
7
import java.io.OutputStream;
 
8
import java.net.URL;
 
9
import java.util.zip.ZipEntry;
 
10
import java.util.zip.ZipInputStream;
 
11
 
 
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;
 
17
 
 
18
public class SBInstaller {
 
19
 
 
20
        private static final String SB_PREFIX = "soundbank";
 
21
        private static final String SB_EXTENSION = ".gm";
 
22
        
 
23
        private boolean cancelled;
 
24
        
 
25
        private URL url;
 
26
        private File tmpPath;
 
27
        private File dstPath;
 
28
        
 
29
        private MidiPortSynthesizer synthesizer;
 
30
        private SBInstallerlistener listener;
 
31
        
 
32
        public SBInstaller(URL url,File tmpPath,File dstPath,MidiPortSynthesizer synthesizer, SBInstallerlistener listener){
 
33
                this.url = url;
 
34
                this.tmpPath = tmpPath;
 
35
                this.dstPath = dstPath;
 
36
                this.synthesizer = synthesizer;
 
37
                this.listener = listener;
 
38
                this.cancelled = false;
 
39
        }
 
40
        
 
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){
 
47
                                install(sbFile);
 
48
                        }
 
49
                }
 
50
                if( tmpFile.exists() ){
 
51
                        tmpFile.delete();
 
52
                }
 
53
                this.listener.notifyFinish();
 
54
        }
 
55
        
 
56
        private boolean download(URL url, File dst){
 
57
                try{
 
58
                        this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.downloading"));
 
59
                        
 
60
                        InputStream is = url.openStream();
 
61
                        
 
62
                        OutputStream os = new FileOutputStream(dst);
 
63
                        
 
64
                        byte[] buffer = new byte[1024];
 
65
                        int length = 0;
 
66
                        while(!isCancelled() && (length = is.read(buffer)) != -1 ){
 
67
                                os.write(buffer,0,length);
 
68
                        }
 
69
                        is.close();
 
70
                        os.flush();
 
71
                        os.close();
 
72
                        
 
73
                        return true;
 
74
                }catch(Throwable throwable){
 
75
                        throwable.printStackTrace();
 
76
                }
 
77
                return false;
 
78
        }
 
79
        
 
80
        private File uncompress(File file){
 
81
                try{
 
82
                        this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.uncompressing",new String[]{file.getAbsolutePath()}));
 
83
                        
 
84
                        if(file.exists()){
 
85
                                File soundbank = null;
 
86
                                
 
87
                                ZipInputStream is = new ZipInputStream(new FileInputStream(file));
 
88
                                ZipEntry entry = null;
 
89
                                while( ( entry = is.getNextEntry() ) != null ){
 
90
                                        
 
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];
 
96
                                                int length = 0;
 
97
                                                while( (length = is.read(buffer)) != -1){
 
98
                                                        os.write(buffer,0,length);
 
99
                                                }
 
100
                                                os.flush();
 
101
                                                os.close();
 
102
                                        }
 
103
                                }
 
104
                                is.close();
 
105
                                
 
106
                                
 
107
                                return soundbank;
 
108
                        }
 
109
                }catch(Throwable throwable){
 
110
                        throwable.printStackTrace();
 
111
                }
 
112
                return null;
 
113
        }
 
114
        
 
115
        private void install(File file){
 
116
                try {
 
117
                        this.listener.notifyProcess(TuxGuitar.getProperty("jsa.soundbank-assistant.process.installing",new String[]{file.getAbsolutePath()}));
 
118
                        
 
119
                        if( ! this.synthesizer.loadSoundbank(file) ){
 
120
                                this.listener.notifyFailed(new MidiPlayerException(TuxGuitar.getProperty("jsa.error.soundbank.custom")));
 
121
                                return;
 
122
                        }
 
123
                        
 
124
                        TGConfigManager config = MidiConfigUtils.getConfig();
 
125
                        config.setProperty(MidiConfigUtils.SOUNDBANK_KEY,file.getAbsolutePath());
 
126
                        config.save();
 
127
                } catch (Throwable throwable) {
 
128
                        throwable.printStackTrace();
 
129
                }
 
130
        }
 
131
        
 
132
        public boolean isCancelled() {
 
133
                return this.cancelled;
 
134
        }
 
135
 
 
136
        public void setCancelled(boolean cancelled) {
 
137
                this.cancelled = cancelled;
 
138
        }
 
139
 
 
140
}