Ticket #3661: 0001-Fix-playlist-behaviour-SL-3661.patch

File 0001-Fix-playlist-behaviour-SL-3661.patch, 5.0 KB (added by manuq, 12 years ago)

Proposed fix.

  • jukeboxactivity.py

    From 739ab4dc698ad3058fc94e55a36ae6dd233a2466 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= <manuq@laptop.org>
    Date: Fri, 8 Jun 2012 10:41:33 -0300
    Subject: [PATCH Jukebox] Fix playlist behaviour SL #3661
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    Mail-Followup-To: <sugar-devel@lists.sugarlabs.org>
    
    This makes Jukebox always save a playlist in format M3U which has
    MIME-Type 'audio/x-mpegurl'.
    
    Journal current behaviour is to copy to the Journal a file that is
    stored in an external media, so Jukebox can't avoid that copy in
    today's Journal.
    
    Signed-off-by: Manuel Quiñones <manuq@laptop.org>
    ---
     jukeboxactivity.py |   70 +++++++++++++++++++++++++++++++++++-----------------
     1 file changed, 48 insertions(+), 22 deletions(-)
    
    diff --git a/jukeboxactivity.py b/jukeboxactivity.py
    index 9d234b1..fee9d8d 100644
    a b  
    2323
    2424
    2525import logging
     26import tempfile
    2627from gettext import gettext as _
    2728import os
    2829
    class JukeboxActivity(activity.Activity): 
    7172        self.set_title(_('Jukebox Activity'))
    7273        self.player = None
    7374        self.max_participants = 1
     75        self._playlist_jobject = None
    7476
    7577        if OLD_TOOLBAR:
    7678            toolbox = activity.ActivityToolbox(self)
    class JukeboxActivity(activity.Activity): 
    366368
    367369    def read_file(self, file_path):
    368370        """Load a file from the datastore on activity start."""
    369         logging.debug('JukeBoxAtivity.read_file: %s %s', file_path)
     371        logging.debug('JukeBoxAtivity.read_file: %s', file_path)
    370372        title = self.metadata.get('title', None)
    371373        self._load_file(file_path, title, self._object_id)
    372374
    class JukeboxActivity(activity.Activity): 
    385387            # is another media file:
    386388            gobject.idle_add(self._start, self.uri, title, object_id)
    387389
    388     def can_close(self):
    389         """Activities should override this function if they want to perform
    390         extra checks before actually closing."""
    391         if len(self.playlist) > 1 and \
    392                 self.metadata['mime_type'] != 'audio/x-mpegurl':
    393             # if the activity started with a media file and added more later
    394             # need create a new file with the file list to no destroy
    395             # the first opened file
    396             logging.error('need new file')
    397             self._jobject = datastore.create()
    398             self.title_entry.set_text(_('Jukebox playlist'))
    399             self._jobject.metadata['title'] = _('Jukebox playlist')
    400             self._jobject.metadata['title_set_by_user'] = '1'
    401             description = ''
    402             for uri in self.playlist:
    403                 description += '%s\n' % uri['title']
    404             self._jobject.metadata['description'] = description
    405             self._jobject.metadata['mime_type'] = 'audio/x-mpegurl'
    406             datastore.write(self._jobject)
     390    def _create_playlist_jobject(self):
     391        """Create an object in the Journal to store the playlist.
    407392
    408         return True
     393        This is needed if the activity was not started from a playlist
     394        or from scratch.
     395
     396        """
     397        jobject = datastore.create()
     398        jobject.metadata['mime_type'] = "audio/x-mpegurl"
     399        jobject.metadata['title'] =  _('Jukebox playlist')
     400
     401        temp_path = os.path.join(activity.get_activity_root(),
     402                                 'instance')
     403        if not os.path.exists(temp_path):
     404            os.makedirs(temp_path)
     405
     406        jobject.file_path = tempfile.mkstemp(dir=temp_path)[1]
     407        self._playlist_jobject = jobject
    409408
    410409    def write_file(self, file_path):
    411         if len(self.playlist) > 1:
     410
     411        def write_playlist_to_file(file_path):
     412            """Open the file at file_path and write the playlist.
     413
     414            It is saved in audio/x-mpegurl format.
     415
     416            """
    412417            list_file = open(file_path, 'w')
    413418            for uri in self.playlist:
    414419                list_file.write('#EXTINF: %s\n' % uri['title'])
    415420                list_file.write('%s\n' % uri['url'])
    416421            list_file.close()
    417422
     423        if not self.metadata['mime_type']:
     424            self.metadata['mime_type'] = 'audio/x-mpegurl'
     425
     426        if self.metadata['mime_type'] == 'audio/x-mpegurl':
     427            write_playlist_to_file(file_path)
     428
     429        else:
     430            if self._playlist_jobject is None:
     431                self._create_playlist_jobject()
     432
     433            # Add the playlist to the playlist jobject description.
     434            # This is only done if the activity was not started from a
     435            # playlist or from scratch:
     436            description = ''
     437            for uri in self.playlist:
     438                description += '%s\n' % uri['title']
     439            self._playlist_jobject.metadata['description'] = description
     440
     441            write_playlist_to_file(self._playlist_jobject.file_path)
     442            datastore.write(self._playlist_jobject)
     443
    418444    def _read_m3u_playlist(self, file_path):
    419445        urls = []
    420446        title = ''