Exemple #1
0
        public void Add(string key, LibraryDownload d)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            else if (d == null)
            {
                throw new ArgumentNullException("d");
            }
            else if (monitoring)
            {
                throw new InvalidOperationException("Can't add while monitoring.");
            }

            lock (downloads) {
                if (!downloads.ContainsKey(key))
                {
                    downloads.Add(key, d);
                    d.Finished           += OnDownloadFinished;
                    d.ProcessingComplete += OnDownloadProcessed;
                    Log.DebugFormat("Download added for {0}", key);
                }
            }
        }
        private void LoadPlaylists()
        {
            if (CurrentActivity == null)
            {
                return;
            }
            else if (CurrentActivity.State != ActivityState.Connected)
            {
                Hyena.Log.Debug(String.Format("activity state {0} is invalid.", CurrentActivity.State));
                return;
            }
            else if (CurrentState != State.LoadedMetadata)
            {
                Hyena.Log.Debug(String.Format("state {0} is invalid.", CurrentState));
                return;
            }

            try {
                IMetadataProviderService service = CurrentActivity.GetDBusObject <IMetadataProviderService> (MetadataProviderService.BusName, MetadataProviderService.ObjectPath);
                int [] playlist_ids = service.GetPlaylistIds(LibraryType.Music);

                download_monitor.Reset();

                if (playlist_ids.Length == 0)
                {
                    CurrentState = State.Loaded;
                }
                else
                {
                    foreach (int id in playlist_ids)
                    {
                        string            playlist_path     = service.CreatePlaylistProvider(id).ToString();
                        IPlaylistProvider playlist_provider = CurrentActivity.GetDBusObject <IPlaylistProvider>
                                                                  (PlaylistProvider.BusName, playlist_path);

                        LibraryDownload download = new LibraryDownload();
                        download_monitor.Add(playlist_path, download);
                        //download_monitor.AssociateObject (playlist_path, new ContactPlaylistSource (playlist_provider.GetName (), this));

                        download.ProcessIncomingPayloads(delegate(object sender, object [] o) {
                            OnPlaylistTracksDownloaded(new DownloadedTracksEventArgs(sender as LibraryDownload, o, playlist_provider.GetName()));
                        });

                        playlist_provider.ChunkReady += OnPlaylistChunkReady;
                        playlist_provider.GetChunks(chunk_length);
                    }

                    download_monitor.Start();
                }
            } catch (Exception e) {
                Hyena.Log.Warning(e);
                ResetState();
                OnError(new TubeManagerErrorEventArgs(ErrorReason.ErrorDuringPlaylistLoad));
            }
        }
Exemple #3
0
        private void OnDownloadProcessed(object sender, EventArgs args)
        {
            LibraryDownload download = sender as LibraryDownload;

            if (download != null && downloads.Values.Contains(download))
            {
                if (monitoring && ProcessingFinished())
                {
                    OnAllProcessed(EventArgs.Empty);
                }
            }
        }
        private void OnTubeManagerPlaylistTracksDownloaded(object sender, EventArgs args)
        {
            DownloadedTracksEventArgs track_args = args as DownloadedTracksEventArgs;

            IDictionary <string, object> [] chunk = track_args.tracks as IDictionary <string, object> [];
            if (chunk == null)
            {
                return;
            }

            LibraryDownload d = track_args.download as LibraryDownload;

            ThreadAssist.ProxyToMain(delegate {
                SetStatus(String.Format(AddinManager.CurrentLocalizer.GetString("Loading {0} of {1}"),
                                        CalculateLoadingTracks(chunk.Length, d.TotalExpected),
                                        d.TotalExpected), false);
            });

            ContactPlaylistSource source = null;

            if (playlist_map.ContainsKey(d))
            {
                source = playlist_map[d];
            }
            else
            {
                source = new ContactPlaylistSource(track_args.name, this);
                playlist_map.Add(d, source);
            }

            source.AddTracks(chunk);

            ThreadAssist.ProxyToMain(delegate {
                if (d != null && d.IsFinished)
                {
                    Hyena.Log.DebugFormat("Download complete for {0}", source.Name);
                    AddChildSource(source);
                    playlist_map.Remove(d);
                    HideStatus();
                }
            });
        }
        private void OnPlaylistChunkReady(string object_path, IDictionary <string, object>[] chunk,
                                          long timestamp, int seq_num, int total)
        {
            Hyena.Log.DebugFormat("Playlist Chunk Ready timestamp {0} seq {1} tracks {2} path {3}",
                                  timestamp, seq_num, chunk.Length, object_path);

            LibraryDownload current_download = download_monitor.Get(object_path);

            if (current_download != null)
            {
                if (!current_download.IsStarted)
                {
                    Hyena.Log.Debug("Initializing download");
                    current_download.Timestamp     = timestamp;
                    current_download.TotalExpected = total;
                }

                current_download.UpdateDownload(timestamp, seq_num, chunk.Length, chunk);
            }
        }
        public void Add (string key, LibraryDownload d)
        {
            if (key == null) {
                throw new ArgumentNullException ("key");
            }
            else if (d == null) {
                throw new ArgumentNullException ("d");
            }
            else if (monitoring) {
                throw new InvalidOperationException ("Can't add while monitoring.");
            }

            lock (downloads) {
                if (!downloads.ContainsKey (key)) {
                    downloads.Add (key, d);
                    d.Finished += OnDownloadFinished;
                    d.ProcessingComplete += OnDownloadProcessed;
                    Log.DebugFormat ("Download added for {0}", key);
                }
            }
        }
        private void OnTubeManagerTracksDownloaded(object sender, EventArgs args)
        {
            DownloadedTracksEventArgs track_args = args as DownloadedTracksEventArgs;

            IDictionary <string, object> [] chunk = track_args.tracks as IDictionary <string, object> [];
            if (chunk == null)
            {
                return;
            }

            LibraryDownload d = track_args.download as LibraryDownload;

            ThreadAssist.ProxyToMain(delegate {
                SetStatus(String.Format(AddinManager.CurrentLocalizer.GetString("Loading {0} of {1}"),
                                        CalculateLoadingTracks(chunk.Length, d.TotalExpected),
                                        d.TotalExpected), false);
            });

            HyenaSqliteConnection conn = ServiceManager.DbConnection;

            conn.BeginTransaction();

            for (int i = 0; i < chunk.Length; i++)
            {
                IDictionary <string, object> track         = chunk[i];
                ContactTrackInfo             contact_track = new ContactTrackInfo(track, this);

                // notify once per chunk
                if (i == chunk.Length - 1)
                {
                    conn.CommitTransaction();
                    contact_track.Save(true);
                }
                else
                {
                    contact_track.Save(false);
                }
            }
        }
 public DownloadedTracksEventArgs(LibraryDownload download, object [] tracks, string name)
     : this(download, tracks)
 {
     this.name = name;
 }
 public DownloadedTracksEventArgs(LibraryDownload download, object [] tracks)
 {
     this.download = download;
     this.tracks = tracks;
 }
 public DownloadedTracksEventArgs(LibraryDownload download, object [] tracks, string name) : this(download, tracks)
 {
     this.name = name;
 }
 public DownloadedTracksEventArgs(LibraryDownload download, object [] tracks)
 {
     this.download = download;
     this.tracks   = tracks;
 }
        private void LoadData()
        {
            Hyena.Log.Debug("TubeManager.LoadData ()");

            if (CurrentState >= State.LoadingMetadata)
            {
                return;
            }
            else if (CurrentActivity == null)
            {
                return;
            }
            else if (CurrentActivity.State != ActivityState.Connected)
            {
                Hyena.Log.Debug(String.Format("activity state {0} is invalid.", CurrentActivity.State));
                return;
            }

            IMetadataProviderService service = CurrentActivity.GetDBusObject <IMetadataProviderService> (MetadataProviderService.BusName, MetadataProviderService.ObjectPath);

            if (service == null)
            {
                Hyena.Log.Debug("ContactSource.LoadData found service null");
                return;
            }

            try {
                // call MetadataProviderService.PermissionGranted () asynchronously to prevent blocking the UI
                // when Telepathy tubes are slow
                if (CurrentState <= State.Waiting)
                {
                    Hyena.Log.Debug("Setting to waiting state");
                    CurrentState      = State.Waiting;
                    permission_caller = new GetBoolPropertyCaller(service.PermissionGranted);
                    permission_caller.BeginInvoke(new AsyncCallback(delegate(IAsyncResult result) {
                        if (CurrentState != State.Waiting)
                        {
                            return;
                        }

                        GetBoolPropertyCaller caller = (GetBoolPropertyCaller)result.AsyncState;
                        bool granted = caller.EndInvoke(result);

                        if (granted)
                        {
                            CurrentState = State.PermissionGranted;
                        }
                        else
                        {
                            CurrentState = State.PermissionNotGranted;
                        }

                        LoadData();
                    }), permission_caller);
                }
                else if (CurrentState == State.PermissionGranted)
                {
                    service.DownloadingAllowedChanged += delegate(bool allowed) {
                        IsDownloadingAllowed = allowed;
                    };

                    // determine if downloading is allowed asynchronously
                    downloading_caller = new GetBoolPropertyCaller(service.DownloadsAllowed);
                    downloading_caller.BeginInvoke(new AsyncCallback(delegate(IAsyncResult result) {
                        GetBoolPropertyCaller caller = (GetBoolPropertyCaller)result.AsyncState;
                        IsDownloadingAllowed         = caller.EndInvoke(result);
                    }), downloading_caller);

                    // clean up any residual tracks
                    download_monitor.Reset();
                    CurrentState = State.LoadingMetadata;

                    string            metadata_path    = service.CreateMetadataProvider(LibraryType.Music).ToString();
                    IMetadataProvider library_provider = CurrentActivity.GetDBusObject <IMetadataProvider> (MetadataProvider.BusName, metadata_path);

                    LibraryDownload download = new LibraryDownload();
                    download_monitor.Add(metadata_path, download);

                    download.ProcessIncomingPayloads(delegate(object sender, object [] o) {
                        OnTracksDownloaded(new DownloadedTracksEventArgs(sender as LibraryDownload, o));
                    });

                    library_provider.ChunkReady += OnLibraryChunkReady;
                    library_provider.GetChunks(chunk_length);

                    download_monitor.Start();
                }
                else if (CurrentState == State.PermissionNotGranted)
                {
                    CurrentState           = State.Waiting;
                    service.PermissionSet += OnPermissionSet;
                    service.RequestPermission();
                }
            } catch (Exception e) {
                Hyena.Log.Warning(e);
                ResetState();
                OnError(new TubeManagerErrorEventArgs(ErrorReason.ErrorDuringLoad));
            }
        }