~ubuntu-branches/ubuntu/trusty/soundconverter/trusty-proposed

« back to all changes in this revision

Viewing changes to src/soundconverter.py

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2008-03-29 10:07:55 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080329100755-48jixpmy2y70vxed
Tags: 0.9.9-0ubuntu1
New upstream release. (LP: #208544)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#
4
4
# SoundConverter - GNOME application for converting between audio formats. 
5
5
# Copyright 2004 Lars Wirzenius
6
 
# Copyright 2005-2007 Gautier Portet
 
6
# Copyright 2005-2008 Gautier Portet
7
7
#
8
8
# This program is free software; you can redistribute it and/or modify
9
9
# it under the terms of the GNU General Public License as published by
155
155
# Name and pattern for CustomFileChooser
156
156
filepattern = (
157
157
        ("All files","*.*"),
158
 
        ("MPEG 1 Layer 3 files","*.mp3"),
159
 
        ("Ogg Vorbis files","*.ogg"),
160
 
        ("iTunes AAC files","*.m4a"),
161
 
        ("WAVEform audio format","*.wav"),
162
 
        ("Advanced Audio Coding","*.aac"),
163
 
        ("Free Lossless Audio Codec","*.flac"),
164
 
        ("Audio Codec 3","*.ac3")
 
158
        ("MP3","*.mp3"),
 
159
        ("Ogg Vorbis","*.ogg"),
 
160
        ("iTunes AAC ","*.m4a"),
 
161
        ("Windows WAV","*.wav"),
 
162
        ("AAC","*.aac"),
 
163
        ("FLAC","*.flac"),
 
164
        ("AC3","*.ac3")
165
165
)
166
166
 
167
167
def beautify_uri(uri):
242
242
def unquote_filename(filename):
243
243
 
244
244
        f= urllib.unquote(filename)
245
 
        try:
 
245
        '''try:
246
246
                # files are normaly in utf-8 ?
247
247
                f = unicode(f, "utf-8")
248
248
        except UnicodeDecodeError:
249
249
                # but sometimes they are badly encoded, this is a failback
250
250
                f = unicode(f, "latin1", "replace")
 
251
        '''
251
252
        return f
252
253
 
253
254
 
264
265
        return tag
265
266
 
266
267
def markup_escape(message):
267
 
        if not isinstance(message, basestring):
268
 
                print "markup error: '%s'" % message
269
 
 
270
 
        message = "&".join(message.split("&"))
271
 
        message = "&lt;".join(message.split("<"))
272
 
        message = "&gt;".join(message.split(">"))
273
 
        return message
274
 
 
275
 
def filename_escape(str):
 
268
        return gobject.markup_escape_text(message)
 
269
 
 
270
def __filename_escape(str):
276
271
        str = str.replace("'","\'")
277
272
        str = str.replace("\"","\\\"")
278
273
        str = str.replace("!","\!")
371
366
        def __init__(self, uri, base_path=None):
372
367
 
373
368
                self.uri = uri
 
369
 
374
370
                if base_path:
375
371
                        self.base_path = base_path
376
372
                        self.filename = uri[len(base_path):]
377
373
                else:
378
374
                        self.base_path, self.filename = os.path.split(self.uri)
379
375
                        self.base_path += "/"
380
 
                self.filename_for_display = unquote_filename(self.filename)
 
376
                self.filename_for_display = gobject.filename_display_name(
 
377
                                unquote_filename(self.filename))
381
378
        
382
379
                self.tags = {
383
380
                        "track-number": 0,
459
456
                
460
457
        def _unicode_to_ascii(self, unicode_string):
461
458
                # thanks to http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/251871
462
 
                return unicodedata.normalize('NFKD', unicode_string).encode('ASCII', 'ignore')
 
459
                try:
 
460
                        unicode_string = unicode(unicode_string, "utf-8")
 
461
                        return unicodedata.normalize('NFKD', unicode_string).encode('ASCII', 'ignore')
 
462
                except UnicodeDecodeError:
 
463
                        unicode_string = unicode(unicode_string, "iso-8859-1")
 
464
                        return unicodedata.normalize('NFKD', unicode_string).encode('ASCII', 'replace')
 
465
                        
463
466
        
464
467
        def get_target_name(self, sound_file):
465
468
 
 
469
                assert self.suffix, "you just forgot to call set_target_suffix()"
 
470
 
466
471
                u = gnomevfs.URI(sound_file.get_uri())
467
472
                root, ext = os.path.splitext(u.path)
468
473
                if u.host_port:
491
496
                pattern = os.path.join(self.subfolders, self.basename + self.suffix)
492
497
                result = pattern % dict
493
498
                if self.replace_messy_chars:
494
 
                        result = self._unicode_to_ascii(unicode(result))
 
499
                        result = self._unicode_to_ascii(result)
495
500
                        s = ""
496
501
                        for c in result:
497
502
                                if c not in self.nice_chars:
504
509
                        folder = root
505
510
                else:
506
511
                        folder = self.folder
507
 
                result = os.path.join(folder, urllib.quote(result.encode('utf-8')))
 
512
                result = os.path.join(folder, urllib.quote(result))
508
513
 
509
514
                return result
510
515
 
1227
1232
                        try:
1228
1233
                                info = gnomevfs.get_file_info(gnomevfs.URI(uri))
1229
1234
                        except gnomevfs.NotFoundError:
 
1235
                                log('uri not found: \'%s\'' % uri)
1230
1236
                                continue
1231
1237
                        except gnomevfs.InvalidURIError:
 
1238
                                log('unvalid uri: \'%s\'' % uri)
1232
1239
                                continue
1233
1240
 
1234
1241
                        if info.type == gnomevfs.FILE_TYPE_DIRECTORY:
1294
1301
 
1295
1302
        def append_file(self, sound_file):
1296
1303
 
1297
 
                #print "+", sound_file.get_filename_for_display()
1298
 
                iter = self.model.append()
1299
 
                sound_file.model = iter
1300
 
                self.model.set(iter, 0, self.format_cell(sound_file))
1301
 
                self.model.set(iter, 1, sound_file)
 
1304
                iter = self.model.append([self.format_cell(sound_file), sound_file])
1302
1305
                self.window.progressbar.pulse()
1303
1306
                        
1304
1307
        
1311
1314
                fields["META"] = sound_file
1312
1315
                fields["filename"] = sound_file.get_filename_for_display()
1313
1316
 
1314
 
                self.model.set(sound_file.model, 0, self.format_cell(sound_file))
 
1317
                for i in self.model:
 
1318
                        if i[1] == sound_file:
 
1319
                                i[0] = self.format_cell(sound_file)
1315
1320
                self.window.set_sensitive()
1316
1321
                self.window.progressbar.pulse()
1317
1322
 
1421
1426
                        w = glade.get_widget("into_selected_folder")
1422
1427
                w.set_active(True)
1423
1428
                
1424
 
                self.target_folder_chooser.set_filename(
 
1429
                self.target_folder_chooser.set_uri(
1425
1430
                        self.get_string("selected-folder"))
1426
1431
                self.update_selected_folder()
1427
1432
        
1466
1471
                        w.show()
1467
1472
                
1468
1473
                widget_name = widgets.get(mime_type, None)[1]
1469
 
                print widget_name
 
1474
 
1470
1475
                if widget_name:
1471
1476
                        w = glade.get_widget(widget_name)
1472
1477
                        w.set_active(True)
2155
2160
                ret = self.addchooser.run()
2156
2161
                self.addchooser.hide()
2157
2162
                if ret == gtk.RESPONSE_OK:
2158
 
                        files = []
2159
 
                        #for uri in self.addchooser.get_uris():
2160
 
                        #  files.append(SoundFile(uri))
2161
 
                        #self.filelist.add_files(files)
2162
2163
                        self.filelist.add_uris(self.addchooser.get_uris())
2163
2164
                self.set_sensitive()
2164
2165
 
2323
2324
                
2324
2325
                self.set_status(_("Converting"))
2325
2326
                
2326
 
                self.progressfile.set_markup("<i><small>%s</small></i>" % current_file)
 
2327
                self.progressfile.set_markup("<i><small>%s</small></i>" % markup_escape(current_file))
2327
2328
                fraction = float(done_so_far) / total
2328
2329
        
2329
2330
                self.progressbar.set_fraction( min(fraction, 1.0) )