summaryrefslogtreecommitdiffstats
path: root/src/amarokscript/soundKonverter.rb
blob: 92c3d06e3f791d6fef79bc656a1f64302d1a7a19 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env ruby
#
# amaroK-Script for integrating soundKonverter into amaroK
#
# (c) 2005 Daniel Faust <daniel@kaligames.de>
# License: GNU General Public License V2


# FIXME after adding some files to soundkonverter, it is impossible to repeat that, until soundkonverter gets closed.

# FIXME don't open files on every request. load options on startup and save on exit!

#`dcop amarok playlist addMedia KURL`
#`dcop amarok playlist addMediaList KURL::List`
#`dcop amarok collection scanCollectionChanges`


require "uri"

begin
    require "Qt"
rescue LoadError
    error = 'Qt Ruby bindings are required for this script.'
    `dcop amarok playlist popupMessage "soundKonverter: #{error}"`
    exit
end

class MainWidget < TQt::Dialog

slots 'accept()'

def initialize(parent = nil, name = nil)
    super

    box = TQt::VBoxLayout.new( self, 11, 6 );

    lTitle = TQt::Label.new('soundKonverter plugin settings', self)
    box.addWidget( lTitle )
    lTitle.setAlignment(TQt::AlignCenter)
    font = TQt::Font.new()
    font.setPixelSize(18)
    font.setBold(true)
    lTitle.setFont(font)

    box.addSpacing( 5 )

    mediaDeviceBox = TQt::GroupBox.new( 1, TQt::Vertical, "Options for transfering to media devices", self )
    box.addWidget( mediaDeviceBox )
    mediaDeviceBox.layout().setSpacing( 6 )
    mediaDeviceBox.layout().setMargin( 6 )
    TQt::Label.new('Profile for lossy conversion:', mediaDeviceBox)
    @cTranscodeProfile = TQt::ComboBox.new(mediaDeviceBox)
    @cTranscodeProfile.insertItem("Very low")
    @cTranscodeProfile.insertItem("Low")
    @cTranscodeProfile.insertItem("Medium")
    @cTranscodeProfile.insertItem("High")
    @cTranscodeProfile.insertItem("Very high")
    @cTranscodeProfile.setCurrentItem( 1 )

#     box.addSpacing( 5 )
# 
#     rippingBox = TQt::GroupBox.new( 2, TQt::Horizontal, "Use pre-defined options for CD ripping", self )
#     box.addWidget( rippingBox )
#     rippingBox.layout().setSpacing( 6 )
#     rippingBox.layout().setMargin( 6 )
#     rippingBox.setCheckable( true )
#     rippingBox.setEnabled( false )
# 
#     profileBox = TQt::HBoxLayout.new( rippingBox, 6 );
#     lRippingProfile = TQt::Label.new('Profile:', rippingBox)
#     profileBox.addWidget( lRippingProfile )
#     @cRippingProfile = TQt::ComboBox.new(rippingBox)
#     profileBox.addWidget( @cRippingProfile )
#     @cRippingProfile.insertItem("Very low")
#     @cRippingProfile.insertItem("Low")
#     @cRippingProfile.insertItem("Medium")
#     @cRippingProfile.insertItem("High")
#     @cRippingProfile.insertItem("Very high")
#     @cRippingProfile.insertItem("Lossless")
#     @cRippingProfile.insertItem("Last used")
#     @cRippingProfile.setCurrentItem(3)
#     lRippingFormat = TQt::Label.new('Format:', rippingBox)
#     profileBox.addWidget( lRippingFormat )
#     @cRippingFormat = TQt::ComboBox.new(rippingBox)
#     profileBox.addWidget( @cRippingFormat )
#     @cRippingFormat.insertItem("ogg")
#     @cRippingFormat.insertItem("mp3")
#     @cRippingFormat.insertItem("flac")
#     directoryBox = TQt::HBoxLayout.new( rippingBox, 6 );
#     lRippingDirectory = TQt::Label.new('Directory:', rippingBox)
#     directoryBox.addWidget( lRippingDirectory )
#     @cRippingDirectory = TQt::ComboBox.new(rippingBox)
#     directoryBox.addWidget( @cRippingDirectory )
#     @cRippingDirectory.setEditable(true)
#     @cRippingDirectory.insertItem("/home/daniel/soundKonverter/%b/%d - %n - %t")
#     @cRippingDirectory.insertItem("Last used")

    box.addSpacing( 5 )

    buttonsBox = TQt::HBoxLayout.new(box)
    buttonsBox.setSpacing(6)
    buttonsBox.addStretch()
    okPushButton = TQt::PushButton.new( self, "ok" )
    buttonsBox.addWidget( okPushButton )
    okPushButton.setText( "OK" )
    okPushButton.setDefault( true )

    connect( okPushButton, SIGNAL( 'clicked()' ),
             self, SLOT( 'accept()' )
           )

    cancelPushButton = TQt::PushButton.new( self, "cancel" )
    buttonsBox.addWidget( cancelPushButton )
    cancelPushButton.setText( "Cancel" )
    cancelPushButton.setAccel( TQt::KeySequence.new(Key_Escape) )

    connect( cancelPushButton, SIGNAL( 'clicked()' ),
             self, SLOT( 'reject()' )
           )

    file = TQt::File.new( File.dirname( File.expand_path( __FILE__ ) ) + "/config" )
    if file.open( TQt::IO_ReadOnly )
        ts = TQt::TextStream.new( file )
        content = ''
        ts >> content
        if content == 'Very_low'
            @cTranscodeProfile.setCurrentItem( 0 )
        elsif content == 'Low'
            @cTranscodeProfile.setCurrentItem( 1 )
        elsif content == 'Medium'
            @cTranscodeProfile.setCurrentItem( 2 )
        elsif content == 'High'
            @cTranscodeProfile.setCurrentItem( 3 )
        elsif content == 'Very_high'
            @cTranscodeProfile.setCurrentItem( 4 )
        end
        file.close()
    end

#     file = TQt::File.new( File.dirname( File.expand_path( __FILE__ ) ) + "/profiles" )
#     if file.open( TQt::IO_ReadOnly )
#         ts = TQt::TextStream.new( file )
#         while !ts.eof()
#             content = ''
#             ts >> content
#             @cProfile.insertItem( content )
#             ts >> content
#         end
# 
#         file.close()
#     end
end


def accept()
    file = TQt::File.new( File.dirname( File.expand_path( __FILE__ ) ) + "/config" )
    if file.open( TQt::IO_WriteOnly )
        ts = TQt::TextStream.new( file )
        if @cTranscodeProfile.currentText() == 'Very low'
            content = 'Very_low'
        elsif @cTranscodeProfile.currentText() == 'Low'
            content = 'Low'
        elsif @cTranscodeProfile.currentText() == 'Medium'
            content = 'Medium'
        elsif @cTranscodeProfile.currentText() == 'High'
            content = 'High'
        elsif @cTranscodeProfile.currentText() == 'Very high'
            content = 'Very_high'
        end
        ts << content + "\n"
        file.close()
    end

    super
end

end


MenuItemName1 = "soundKonverter \"Convert selected files\""
MenuItemName2 = "soundKonverter \"Add Replay Gain to selected files\""
MenuItemName3 = "soundKonverter \"Rip and play audio CD\""


def cleanup()
    `dcop amarok script removeCustomMenuItem #{MenuItemName1}`
    `dcop amarok script removeCustomMenuItem #{MenuItemName2}`
    `dcop amarok script removeCustomMenuItem #{MenuItemName3}`
end


trap( "SIGTERM" ) { cleanup() }

`dcop amarok script addCustomMenuItem #{MenuItemName1}`
`dcop amarok script addCustomMenuItem #{MenuItemName2}`
`dcop amarok script addCustomMenuItem #{MenuItemName3}`

loop do
    message = gets().chomp()
    command = /[A-Za-z]*/.match( message ).to_s()

    case command
        when "configure"
            app = TQt::Application.new(ARGV)
            widget = MainWidget.new
            app.setMainWidget(widget)
            widget.show()
            app.exec()

        when "transcode"
            args = message.split()
            filename = args[1]
            #uri = URI.parse( args[1] )
            #filename = URI.unescape( uri.path() )
            filetype = args[2]
            profile = ''

            file = TQt::File.new( File.dirname( File.expand_path( __FILE__ ) ) + "/formats" )
            if file.open( TQt::IO_ReadOnly )
                ts = TQt::TextStream.new( file )
                while !ts.eof()
                    mode = ''
                    formats = ''
                    ts >> mode
                    ts >> formats
                    if formats.split(',').include?( filetype )
                        profile = mode
                    end
                end
                file.close()
            end

            if profile == ''
                file = TQt::File.new( File.dirname( File.expand_path( __FILE__ ) ) + "/config" )
                if file.open( TQt::IO_ReadOnly )
                    ts = TQt::TextStream.new( file )
                    content = ''
                    ts >> content
                    if content == 'Very_low'
                        profile = 'Very low'
                elsif content == 'Low'
                        profile = 'Low'
                elsif content == 'Medium'
                        profile = 'Medium'
                elsif content == 'High'
                        profile = 'High'
                elsif content == 'Very_high'
                        profile = 'Very high'
                    end
                    file.close()
              else
                    profile = 'Low'
                end
            end

            `dcop amarok playlist shortStatusMessage "starting soundKonverter in background"`
            `soundkonverter --invisible --profile '#{profile}' --format '#{filetype}' --output '/tmp' --command "dcop amarok mediabrowser transcodingFinished %u file://%o" '#{filename}'`

        when "customMenuClicked"
            if message.include?( "Convert selected files" )
                args = message.split()
                # Remove the command args
                5.times() { args.delete_at( 0 ) }

                # Iterate over all selected files
                files = ''
                args.each() do |arg|
                    uri = URI.parse( arg )
                    file = URI.unescape( uri.path() )
                    files += ' "'+file+'"'
                end
                `dcop amarok playlist shortStatusMessage "starting soundKonverter"`
                `soundkonverter #{files}`
            end
            if message.include?( "Add Replay Gain to selected files" )
                args = message.split()
                # Remove the command args
                8.times() { args.delete_at( 0 ) }

                files = ''
                args.each() do |arg|
                    uri = URI.parse( arg )
                    file = URI.unescape( uri.path() )
                    files += ' "'+file+'"'
                end
                `dcop amarok playlist shortStatusMessage "starting soundKonverter"`
                `soundkonverter --replaygain #{files}`
            end
            if message.include?( "Rip and play audio CD" )
                `dcop amarok playlist popupMessage "Select all tracks to rip and press 'Start' in order to start ripping.\nThe tracks will be added to the playlist, when they are ready."`
                #`dcop amarok playlist shortStatusMessage "starting soundKonverter"`
                `soundkonverter --invisible --rip auto --command "dcop amarok playlist addMedia %o"`
            end
    end
end