Esempio n. 1
0
        internal void Sync(bool force)
        {
            if (Enabled)
            {
                ThreadAssist.AssertNotInMainThread();

                CalculateSync();

                if (!force && to_remove.Count > MAX_NOWARN_TRACKS_REMOVAL)
                {
                    throw new PossibleUserErrorException(to_remove.Count);
                }

                Hyena.Log.Debug("Starting DAP sync");

                sync.Dap.DeleteAllTracks(to_remove);
                sync.Dap.AddAllTracks(to_add);

                if (library.SupportsPlaylists && sync.Dap.SupportsPlaylists)
                {
                    SnapshotPlaylists();
                }

                Hyena.Log.Debug("Ending DAP sync");

                CalculateSync();
                sync.OnUpdated();
            }
        }
Esempio n. 2
0
        internal void Sync(bool force)
        {
            if (Enabled)
            {
                ThreadAssist.AssertNotInMainThread();

                CalculateSync();

                if (!force && to_remove.Count > MAX_NOWARN_TRACKS_REMOVAL)
                {
                    throw new PossibleUserErrorException(to_remove.Count);
                }

                Hyena.Log.Debug("Starting DAP sync");

                sync.Dap.DeleteAllTracks(to_remove);
                sync.Dap.AddAllTracks(to_add);

                if (library.SupportsPlaylists && sync.Dap.SupportsPlaylists)
                {
                    // Now create the playlists, taking snapshots of smart playlists and saving them
                    // as normal playlists
                    IList <AbstractPlaylistSource> playlists = GetSyncPlaylists();
                    foreach (AbstractPlaylistSource from in playlists)
                    {
                        if (from.Count == 0)
                        {
                            continue;
                        }
                        var to = new SyncPlaylist(from.Name, sync.Dap, this);
                        to.Save();

                        ServiceManager.DbConnection.Execute(
                            String.Format(
                                @"INSERT INTO CorePlaylistEntries (PlaylistID, TrackID)
                                    SELECT ?, TrackID FROM CoreTracks WHERE PrimarySourceID = ? AND MetadataHash IN
                                        (SELECT MetadataHash FROM {0} WHERE {1})",
                                from.DatabaseTrackModel.ConditionFromFragment, from.DatabaseTrackModel.Condition),
                            to.DbId, sync.Dap.DbId
                            );
                        to.UpdateCounts();

                        if (to.Count == 0)
                        {
                            // If it's empty, don't leave it on the device
                            to.Unmap();
                        }
                        else
                        {
                            sync.Dap.AddChildSource(to);
                        }
                    }
                }

                Hyena.Log.Debug("Ending DAP sync");

                CalculateSync();
                sync.OnUpdated();
            }
        }
Esempio n. 3
0
        public bool Parse(SafeUri uri)
        {
            ThreadAssist.AssertNotInMainThread();
            lock (this) {
                elements = null;
                HttpWebResponse response       = null;
                Stream          stream         = null;
                Stream          web_stream     = null;
                bool            partial_read   = false;
                long            saved_position = 0;

                if (uri.Scheme == "file")
                {
                    stream = Banshee.IO.File.OpenRead(uri);
                }
                else if (uri.Scheme == "http")
                {
                    response   = Download(uri, HTTP_REQUEST_RETRIES);
                    web_stream = response.GetResponseStream();
                    try {
                        stream = new MemoryStream();

                        byte [] buffer = new byte[4096];
                        int     read;

                        // If we haven't read the whole stream and if
                        // it turns out to be a playlist, we'll read the rest
                        read = web_stream.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, read);
                        if ((read = web_stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            partial_read = true;
                            stream.Write(buffer, 0, read);
                            saved_position = stream.Position;
                        }

                        stream.Position = 0;
                    } finally {
                        if (!partial_read)
                        {
                            web_stream.Close();
                            response.Close();
                        }
                    }
                }
                else
                {
                    Hyena.Log.DebugFormat("Not able to parse playlist at {0}", uri);
                    return(false);
                }

                PlaylistFormatDescription matching_format = null;

                foreach (PlaylistFormatDescription format in playlist_formats)
                {
                    stream.Position = 0;

                    if (format.MagicHandler(new StreamReader(stream)))
                    {
                        matching_format = format;
                        break;
                    }
                }

                if (matching_format == null)
                {
                    if (partial_read)
                    {
                        web_stream.Close();
                        response.Close();
                    }
                    return(false);
                }

                if (partial_read)
                {
                    try {
                        stream.Position = saved_position;
                        Banshee.IO.StreamAssist.Save(web_stream, stream, false);
                    } finally {
                        web_stream.Close();
                        response.Close();
                    }
                }
                stream.Position = 0;
                IPlaylistFormat playlist = (IPlaylistFormat)Activator.CreateInstance(matching_format.Type);
                playlist.BaseUri = BaseUri;
                playlist.Load(stream, false);
                stream.Dispose();

                elements = playlist.Elements;
                Title    = playlist.Title ?? Path.GetFileNameWithoutExtension(uri.LocalPath);
                return(true);
            }
        }
Esempio n. 4
0
        public bool Parse(SafeUri uri)
        {
            ThreadAssist.AssertNotInMainThread();
            lock (this) {
                elements = null;
                HttpWebResponse response       = null;
                Stream          stream         = null;
                Stream          web_stream     = null;
                bool            partial_read   = false;
                long            saved_position = 0;

                if (uri.Scheme == "file")
                {
                    stream = Banshee.IO.File.OpenRead(uri);
                }
                else if (uri.Scheme == "http")
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.AbsoluteUri);
                    request.UserAgent         = Banshee.Web.Browser.UserAgent;
                    request.KeepAlive         = false;
                    request.Timeout           = 5 * 1000;
                    request.AllowAutoRedirect = true;

                    // Parse out and set credentials, if any
                    string user_info = new Uri(uri.AbsoluteUri).UserInfo;
                    if (!String.IsNullOrEmpty(user_info))
                    {
                        string username = String.Empty;
                        string password = String.Empty;
                        int    cIndex   = user_info.IndexOf(":");
                        if (cIndex != -1)
                        {
                            username = user_info.Substring(0, cIndex);
                            password = user_info.Substring(cIndex + 1);
                        }
                        else
                        {
                            username = user_info;
                        }
                        request.Credentials = new NetworkCredential(username, password);
                    }

                    response   = (HttpWebResponse)request.GetResponse();
                    web_stream = response.GetResponseStream();

                    try {
                        stream = new MemoryStream();

                        byte [] buffer = new byte[4096];
                        int     read;

                        // If we haven't read the whole stream and if
                        // it turns out to be a playlist, we'll read the rest
                        read = web_stream.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, read);
                        if ((read = web_stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            partial_read = true;
                            stream.Write(buffer, 0, read);
                            saved_position = stream.Position;
                        }

                        stream.Position = 0;
                    } finally {
                        if (!partial_read)
                        {
                            web_stream.Close();
                            response.Close();
                        }
                    }
                }
                else
                {
                    Hyena.Log.DebugFormat("Not able to parse playlist at {0}", uri);
                    return(false);
                }

                PlaylistFormatDescription matching_format = null;

                foreach (PlaylistFormatDescription format in playlist_formats)
                {
                    stream.Position = 0;

                    StreamReader reader = new StreamReader(stream);
                    if (format.MagicHandler(reader))
                    {
                        matching_format = format;
                        break;
                    }
                }

                if (matching_format == null)
                {
                    return(false);
                }

                if (partial_read)
                {
                    try {
                        stream.Position = saved_position;
                        Banshee.IO.StreamAssist.Save(web_stream, stream, false);
                    } finally {
                        web_stream.Close();
                        response.Close();
                    }
                }
                stream.Position = 0;
                IPlaylistFormat playlist = (IPlaylistFormat)Activator.CreateInstance(matching_format.Type);
                playlist.BaseUri = BaseUri;
                playlist.Load(stream, false);
                stream.Dispose();

                elements = playlist.Elements;
                Title    = playlist.Title ?? Path.GetFileNameWithoutExtension(uri.LocalPath);
                return(true);
            }
        }