Banshee.Dap.Mtp.MtpSource.LoadFromDevice C# (CSharp) Method

LoadFromDevice() protected method

protected LoadFromDevice ( ) : void
return void
        protected override void LoadFromDevice ()
        {
            track_map = new Dictionary<int, Track> ();
            try {
                List<Track> files = null;
                lock (mtp_device) {
                    files = mtp_device.GetAllTracks (delegate (ulong current, ulong total, IntPtr data) {
                        //user_event.Progress = (double)current / total;
                        // Translators: {0} is the name of the MTP audio device (eg Gabe's Zen Player), {1} is the
                        // track currently being loaded, and {2} is the total # of tracks that will be loaded.
                        SetStatus (String.Format (Catalog.GetString ("Loading {0} - {1} of {2}"), Name, current, total), false);
                        return 0;
                    });
                }

                /*if (user_event.IsCancelRequested) {
                    return;
                }*/

                // Delete any empty albums
                lock (mtp_device) {
                    foreach (Album album in mtp_device.GetAlbums ()) {
                        if (album.Count == 0) {
                            album.Remove ();
                        }
                    }
                }

                int [] source_ids = new int [] { DbId };
                foreach (Track mtp_track in files) {
                    int track_id;
                    if ((track_id = DatabaseTrackInfo.GetTrackIdForUri (MtpTrackInfo.GetPathFromMtpTrack (mtp_track), source_ids)) > 0) {
                        track_map[track_id] = mtp_track;
                    } else {
                        MtpTrackInfo track = new MtpTrackInfo (mtp_device, mtp_track);
                        track.PrimarySource = this;
                        track.Save (false);
                        track_map[track.TrackId] = mtp_track;
                    }
                }

                Hyena.Data.Sqlite.HyenaSqliteCommand insert_cmd = new Hyena.Data.Sqlite.HyenaSqliteCommand (
                    @"INSERT INTO CorePlaylistEntries (PlaylistID, TrackID)
                        SELECT ?, TrackID FROM CoreTracks WHERE PrimarySourceID = ? AND ExternalID = ?");

                lock (mtp_device) {
                    var playlists = mtp_device.GetPlaylists ();
                    if (playlists != null) {
                        foreach (MTP.Playlist playlist in playlists) {
                            PlaylistSource pl_src = new PlaylistSource (playlist.Name, this);
                            pl_src.Save ();
                            // TODO a transaction would make sense here (when the threading issue is fixed)
                            foreach (uint id in playlist.TrackIds) {
                                ServiceManager.DbConnection.Execute (insert_cmd, pl_src.DbId, this.DbId, id);
                            }
                            pl_src.UpdateCounts ();
                            AddChildSource (pl_src);
                        }
                    }
                }

            } catch (Exception e) {
                Log.Exception (e);
            }
            OnTracksAdded ();
        }