예제 #1
0
        void FetchButton_Click(object sender, EventArgs eventArgs)
        {
            var fromWeek = scrobbleData == null ? DateTime.MinValue : scrobbleData.LastWeekFetched;

            if (scrobbleData != null && UsernameText.Text != scrobbleData.Account)
            {
                if (MessageBox.Show(this,
                                    string.Format("Username '{0}' doesn't match the Last.fm account whose scrobbles were last fetched ('{1}').\nFetching from account '{0}' will clear currently loaded data and start over from scratch.\n\nContinue?",
                                                  UsernameText.Text, scrobbleData.Account),
                                    "Username mismatch", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
                else
                {
                    scrobbleData = null;
                }
            }

            TaskUtil.PerformForegroundTask(this, fetcher.FetchAsync(UsernameText.Text, fromWeek), result =>
            {
                if (result.Errors.Count() > 0)
                {
                    new ErrorReporter(result.Errors)
                    {
                        AlternateTaskName = "fetching the user's scrobbles.", AlternateItemName = "Weekly chart"
                    }
                }
                .ShowDialog();

                if (scrobbleData == null)
                {
                    scrobbleData = new ScrobbleArchive {
                        Scrobbles = result.Scrobbles, Account = UsernameText.Text
                    };
                    ExportMenuItem.Enabled = true;
                }
                else
                {
                    // merge scrobbles
                    var currentScrobbleSet = new Dictionary <ScrobbledTrack, ScrobbledTrack>(scrobbleData.Scrobbles.Count);
                    foreach (var scrobble in scrobbleData.Scrobbles)
                    {
                        currentScrobbleSet.Add(scrobble, scrobble);
                    }

                    foreach (var fetchedScrobble in result.Scrobbles)
                    {
                        ScrobbledTrack currentScrobble;
                        if (currentScrobbleSet.TryGetValue(fetchedScrobble, out currentScrobble))
                        {
                            currentScrobble.PlayCount += fetchedScrobble.PlayCount;
                            if (currentScrobble.WeekLastPlayed < fetchedScrobble.WeekLastPlayed)
                            {
                                currentScrobble.WeekLastPlayed = fetchedScrobble.WeekLastPlayed;
                            }
                        }
                        else
                        {
                            scrobbleData.Scrobbles.Add(fetchedScrobble);
                        }
                    }
                }
                scrobbleData.LastWeekFetched = result.LastWeekFetched;

                TracksView.DataSource     = new ScrobbledTrackBindingList(scrobbleData.Scrobbles);
                MapToWmpButton.Enabled    = hasWmp;
                MapToITunesButton.Enabled = hasiTunes;
            },