[banshee/youtube] [YouTube] Thread-safe UI update



commit 0f9650f4cda7e04511b0c5eb487e3b3d02015fd1
Author: Alexander Kojevnikov <alexander kojevnikov com>
Date:   Fri Mar 5 13:46:43 2010 +1100

    [YouTube] Thread-safe UI update

 .../Banshee.YouTube.Gui/YouTubeTile.cs             |  103 ++++++++++++--------
 .../Banshee.YouTube/Banshee.YouTube/YouTubePane.cs |   74 ++++++++-------
 2 files changed, 100 insertions(+), 77 deletions(-)
---
diff --git a/src/Extensions/Banshee.YouTube/Banshee.YouTube.Gui/YouTubeTile.cs b/src/Extensions/Banshee.YouTube/Banshee.YouTube.Gui/YouTubeTile.cs
index 8b99cd7..8c9aece 100755
--- a/src/Extensions/Banshee.YouTube/Banshee.YouTube.Gui/YouTubeTile.cs
+++ b/src/Extensions/Banshee.YouTube/Banshee.YouTube.Gui/YouTubeTile.cs
@@ -1,10 +1,12 @@
 //
 // YouTubeTile.cs
 //
-// Author:
+// Authors:
 //   Kevin Duffus <KevinDuffus gmail com>
+//   Alexander Kojevnikov <alexander kojevnikov com>
 //
 // Copyright (C) 2009 Kevin Duffus
+// Copyright (C) 2010 Alexander Kojevnikov
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -51,34 +53,51 @@ namespace Banshee.YouTube.Gui
 {
     public class YouTubeTile : VideoStreamTile
     {
-        public YouTubeTile (Video video_entry)
+        public YouTubeTile (YouTubeTileData data)
         {
-            Info = video_entry;
+            BansheePlaybackUri = data.BansheePlaybackUri;
+            BrowserPlaybackUri = data.BrowserPlaybackUri;
+            Title = data.Title;
+            Uploader = data.Uploader;
+            RatingValue = data.RatingValue;
+
+            if (data.Thumbnail != null) {
+                Pixbuf = new Gdk.Pixbuf (data.Thumbnail);
+            } else {
+                Pixbuf = Banshee.Gui.IconThemeUtils.LoadIcon ("generic-artist", 48);
+            }
         }
+    }
 
-        private static string GetTParam (string yt_video_uri)
+    public class YouTubeTileData
+    {
+        public string BansheePlaybackUri { get; private set; }
+        public string BrowserPlaybackUri { get; private set; }
+        public string Title { get; private set; }
+        public string Uploader { get; private set; }
+        public int RatingValue { get; private set; }
+        public string Thumbnail { get; private set; }
+
+        public YouTubeTileData (Video video)
         {
-            string t_param;
-            DataFetch df = new DataFetch ();
-            string watch_page_contents = df.GetWatchPageContents (yt_video_uri);
-
-            if (String.IsNullOrEmpty (watch_page_contents)) {
-                return null;
+            BansheePlaybackUri = GetPlaybackUri (video);
+            BrowserPlaybackUri = video.WatchPage.AbsoluteUri;
+            Title = video.Title;
+            Uploader = video.Uploader;
+
+            try {
+                RatingValue = (int) Math.Round (video.RatingAverage);
+            } catch (Exception e) {
+                Log.DebugException (e);
             }
 
-            Regex regex = new Regex ("'SWF_ARGS'.*\"t\": \"([^\"]+)\"");
-            Match match = regex.Match (watch_page_contents);
-
-            if (!match.Success) {
-                return null;
+            try {
+                DataFetch df = new DataFetch ();
+                Thumbnail = df.DownloadContent (video.Thumbnails[0].Url, CacheDuration.Normal);
+            } catch (Exception e) {
+                Log.DebugException (e);
+                Thumbnail = null;
             }
-
-            t_param = Regex.Unescape (match.Result ("$1"));
-            if (t_param == null) {
-                t_param = match.Result ("$1");
-            }
-
-            return t_param;
         }
 
         private static string GetPlaybackUri (Video yt_video)
@@ -117,29 +136,29 @@ namespace Banshee.YouTube.Gui
             return playback_uri;
         }
 
-        public Video Info {
-            set {
-                BansheePlaybackUri = GetPlaybackUri (value);
-                BrowserPlaybackUri = value.WatchPage.AbsoluteUri;
-                Title = value.Title;
-                Uploader = value.Uploader;
-
-                try {
-                    RatingValue = (int) Math.Round (value.RatingAverage);
-                } catch (Exception e) {
-                    Log.DebugException (e);
-                }
+        private static string GetTParam (string yt_video_uri)
+        {
+            string t_param;
+            DataFetch df = new DataFetch ();
+            string watch_page_contents = df.GetWatchPageContents (yt_video_uri);
 
-                try {
-                    DataFetch df = new DataFetch ();
-                    string thumbnail = df.DownloadContent (value.Thumbnails[0].Url, CacheDuration.Normal);
+            if (String.IsNullOrEmpty (watch_page_contents)) {
+                return null;
+            }
 
-                    Pixbuf = new Gdk.Pixbuf (thumbnail);
-                } catch (Exception e) {
-                    Log.DebugException (e);
-                    Pixbuf = Banshee.Gui.IconThemeUtils.LoadIcon ("generic-artist", 48);
-                }
+            Regex regex = new Regex ("'SWF_ARGS'.*\"t\": \"([^\"]+)\"");
+            Match match = regex.Match (watch_page_contents);
+
+            if (!match.Success) {
+                return null;
             }
+
+            t_param = Regex.Unescape (match.Result ("$1"));
+            if (t_param == null) {
+                t_param = match.Result ("$1");
+            }
+
+            return t_param;
         }
     }
 }
diff --git a/src/Extensions/Banshee.YouTube/Banshee.YouTube/YouTubePane.cs b/src/Extensions/Banshee.YouTube/Banshee.YouTube/YouTubePane.cs
index 9a39c57..9ccf4a1 100644
--- a/src/Extensions/Banshee.YouTube/Banshee.YouTube/YouTubePane.cs
+++ b/src/Extensions/Banshee.YouTube/Banshee.YouTube/YouTubePane.cs
@@ -1,10 +1,12 @@
 //
 // YouTubePane.cs
 //
-// Author:
+// Authors:
 //   Kevin Duffus <KevinDuffus gmail com>
+//   Alexander Kojevnikov <alexander kojevnikov com>
 //
 // Copyright (C) 2009 Kevin Duffus
+// Copyright (C) 2010 Alexander Kojevnikov
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -39,6 +41,7 @@ using Google.GData.YouTube;
 using Google.YouTube;
 
 using Hyena;
+using Hyena.Jobs;
 using Hyena.Widgets;
 
 using Banshee.Gui;
@@ -57,8 +60,8 @@ namespace Banshee.YouTube
         private Gtk.ScrolledWindow results_sw;
         private TileView results_tv;
         private Label no_results_label = new Label (Catalog.GetString ("No videos found"));
-        private Hyena.Jobs.Scheduler scheduler = new Hyena.Jobs.Scheduler ();
-        private Hyena.Jobs.Job refresh_videos_jobs;
+        private Scheduler scheduler = new Scheduler ();
+        private Job refresh_videos_jobs;
 
         private int max_results_display = 12;
         private bool showing_results = true;
@@ -163,7 +166,7 @@ namespace Banshee.YouTube
             results_tv.ModifyBg (StateType.Normal, Style.Base (StateType.Normal));
         }
 
-        private class RefreshVideosJob : Hyena.Jobs.Job
+        private class RefreshVideosJob : SimpleAsyncJob
         {
             private YouTubePane yt_pane;
             private string yt_query_val;
@@ -174,7 +177,7 @@ namespace Banshee.YouTube
                 this.yt_query_val = query_val;
             }
 
-            protected override void RunJob ()
+            protected override void Run ()
             {
                 DataCore yt_data = new DataCore();
                 bool init_request = yt_data.InitYouTubeRequest();
@@ -191,45 +194,46 @@ namespace Banshee.YouTube
         private void UpdateForQuery (Feed<Video> video_feed)
         {
             int result_display_count = 0;
-            List<YouTubeTile> tiles = new List<YouTubeTile> ();
+            var tiles = new List<YouTubeTileData> ();
+            bool cleanup;
+
+            if (video_feed.TotalResults > 0) {
+                cleanup = !showing_results;
+
+                foreach (Video entry in video_feed.Entries) {
+                    // Don't include videos that are not live
+                    if (entry.IsDraft) {
+                        continue;
+                    } else if (result_display_count++ < max_results_display) {
+                        tiles.Add (new YouTubeTileData (entry));
+                    }
+                }
+
+                showing_results = true;
+            } else {
+                Log.Debug ("YouTube: No videos found");
+                cleanup = showing_results;
+                showing_results = false;
+            }
 
-            results_tv.ClearWidgets ();
+            ThreadAssist.BlockingProxyToMain (delegate {
+                results_tv.ClearWidgets ();
 
-            ThreadAssist.Spawn (delegate {
-                if (video_feed.TotalResults > 0) {
-                    if (!showing_results) {
+                if (showing_results) {
+                    if (cleanup) {
                         Remove (no_results_label);
                         Add (results_sw);
                         ShowAll ();
                     }
 
-                    foreach (Video entry in video_feed.Entries) {
-                        // Don't include videos that are not live
-                        if (entry.IsDraft) {
-                            continue;
-                        } else if (result_display_count++ < max_results_display) {
-                            tiles.Add (new YouTubeTile (entry));
-                        }
+                    foreach (YouTubeTileData tile in tiles) {
+                        results_tv.AddWidget (new YouTubeTile (tile));
                     }
-
                     results_tv.ShowAll ();
-                    showing_results = true;
-                } else {
-                    Log.Debug ("YouTube: No videos found");
-                    if (showing_results) {
-                        Remove (results_sw);
-                        Add (no_results_label);
-                        showing_results = false;
-                        ShowAll ();
-                    }
-                }
-
-                if (showing_results) {
-                    ThreadAssist.ProxyToMain (delegate {
-                        foreach (YouTubeTile tile in tiles) {
-                            results_tv.AddWidget (tile);
-                        }
-                    });
+                } else if (cleanup) {
+                    Remove (results_sw);
+                    Add (no_results_label);
+                    ShowAll ();
                 }
 
                 ready = true;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]