private void bw_DoWork(object sender, DoWorkEventArgs e) { while (_mSongs.Count != 0) { if (_lyricsController == null) { return; } if (bwOnlineSearch.CancellationPending) { return; } if (_mNoOfCurrentlySearches < MNoOfSearchesAllowed && _lyricsController.StopSearches == false) { _mNoOfCurrentlySearches += 1; var lyricID = _mSongs.Dequeue(); var artist = lyricID[0]; var title = lyricID[1]; var rowNumberInListView = int.Parse(lyricID[2]); bwOnlineSearch.ReportProgress(rowNumberInListView); _lyricsController.AddNewLyricSearch(artist, title, MediaPortalUtil.GetStrippedPrefixArtist(artist, _mStrippedPrefixStrings)); } Thread.Sleep(200); } }
private void FetchLyric(string artist, string title, bool automaticUpdate) { LockGui(); tbLyrics.Text = ""; lvSearchResults.Items.Clear(); _counter = 0; _sitesToSearch.Clear(); foreach (var site in singleRunSitesList.Items.Cast <object>().Where(site => singleRunSitesList.CheckedItems.Contains(site))) { _sitesToSearch.Add((string)site); } // If automaticUpdate is set then return after the first positive search _mEventStopThread = new ManualResetEvent(false); _lyricsController = new LyricsController(this, _mEventStopThread, _sitesToSearch.ToArray(), true, automaticUpdate, _mFind, _mReplace); ThreadStart job = delegate { _lyricsController.Run(); }; _mLyricControllerThread = new Thread(job); _mLyricControllerThread.Name = "lyricSearch Thread"; // looks nice in Output window _mLyricControllerThread.Start(); _lyricsController.AddNewLyricSearch(artist, title, MediaPortalUtil.GetStrippedPrefixArtist(artist, _mStrippedPrefixStrings)); }
public FindLyric(LyricsLibrary parent, string artist, string title, bool markedDatabase, int treeArtistIndex, int treeTitleIndex) { InitializeComponent(); Text = String.Format("Find a lyric for {0} - {1}", artist, title); _parent = parent; _markedDatabase = markedDatabase; _treeArtistIndex = treeArtistIndex; _treeTitleIndex = treeTitleIndex; // initialize delegates MDelegateStringUpdate = UpdateStringMethod; MDelegateStatusUpdate = UpdateStatusMethod; MDelegateLyricFound = LyricFoundMethod; MDelegateLyricNotFound = LyricNotFoundMethod; MDelegateThreadFinished = ThreadFinishedMethod; MDelegateThreadException = ThreadExceptionMethod; // initialize events _mEventStopThread = new ManualResetEvent(false); tbArtist.Text = artist; tbTitle.Text = title; _originalArtist = artist; _originalTitle = title; var lyricsSitesNames = LyricsSiteFactory.LyricsSitesNames(); singleRunSitesList.Items.Clear(); foreach (var site in lyricsSitesNames) { singleRunSitesList.Items.Add(site, SettingManager.GetParamAsBool(SettingManager.SitePrefix + site, false)); } _mAutomaticFetch = SettingManager.GetParamAsBool(SettingManager.AutomaticFetch, true); _mAutomaticUpdate = SettingManager.GetParamAsBool(SettingManager.AutomaticUpdateWhenFirstFound, false); _mMoveLyricFromMarkedDatabase = SettingManager.GetParamAsBool(SettingManager.MoveLyricFromMarkedDatabase, true); _mAutomaticWriteToMusicTag = SettingManager.GetParamAsBool(SettingManager.AutomaticWriteToMusicTag, true); _mFind = SettingManager.GetParamAsString(SettingManager.Find, ""); _mReplace = SettingManager.GetParamAsString(SettingManager.Replace, ""); _mStrippedPrefixStrings = MediaPortalUtil.GetStrippedPrefixStringArray(); BeginSearchIfPossible(artist, title); ShowDialog(); }
private void SearchOnline() { // After the tag search is completed, we now recalculate the number of lyrics to search for during online search _mNoOfSearchesToComplete -= _mNoOfSearchesCompleted; _mNoOfSearchesCompleted = 0; _mNoOfCurrentlySearches = 0; if (bwOnlineSearch.IsBusy) { Thread.Sleep(2000); } var lyricsSitesNames = LyricsSiteFactory.LyricsSitesNames(); _mFind = SettingManager.GetParamAsString(SettingManager.Find, ""); _mReplace = SettingManager.GetParamAsString(SettingManager.Replace, ""); _mStrippedPrefixStrings = MediaPortalUtil.GetStrippedPrefixStringArray(); _mEventStopThread = new ManualResetEvent(false); // If automaticUpdate is set then return after the first positive search _lyricsController = new LyricsController(this, _mEventStopThread, lyricsSitesNames.Where(site => SettingManager.GetParamAsBool(SettingManager.SitePrefix + site, false)).ToArray(), false, false, _mFind, _mReplace); _lyricsController.NoOfLyricsToSearch = _mNoOfSearchesToComplete; ThreadStart job = delegate { _lyricsController.Run(); }; _mLyricControllerThread = new Thread(job); _mLyricControllerThread.Start(); _lyricsController.StopSearches = false; bwOnlineSearch.RunWorkerAsync(); }
private void bgWorkerSearch_DoWork(object sender, DoWorkEventArgs e) { #region 1. Sorting song _lyricConfigInfosQueue = new Queue(); _mMusicDatabase = MusicDatabase.Instance; if (_mSearchOnlyMarkedSongs == false) { //System.IO.Directory.SetCurrentDirectory(@"C:\Program Files\Team MediaPortal\MediaPortal"); //string test = System.IO.Directory.GetCurrentDirectory(); _mMusicDatabase.GetAllArtists(ref _artists); var canStartSearch = false; foreach (var artist in _artists) { // If the user has cancelled the search => end this if (_stopCollectingOfTitles) { bgWorkerSearch.CancelAsync(); return; } // Reached the limit if (canStartSearch) { break; } var currentArtist = (string)artist; _mMusicDatabase.GetSongsByArtist(currentArtist, ref _songs); foreach (var song in _songs) { if (canStartSearch) { break; } /* Don't include song if one of the following is true * 1. The artist is unknown or empty * 2. The title is empty * 3. Various artister should not be considered and the artist is "various artist" * 4. Song with a lyric in the tag should not be considered, but instead include the file to the database right away */ MusicTag tag; if (song.Artist.Equals("unknown") || string.IsNullOrEmpty(song.Artist) || string.IsNullOrEmpty(song.Title) || (_mDisregardVariousArtist && (song.Artist.ToLower().Equals("various artists")))) { } else if ((_mDisregardSongWithLyricInTag && ((tag = TagReader.ReadTag(song.FileName)) != null) && tag.Lyrics.Length > 0)) { _mSongsWithLyric += 1; var capArtist = LyricUtil.CapatalizeString(tag.Artist); var capTitle = LyricUtil.CapatalizeString(tag.Title); if ( DatabaseUtil.IsSongInLyricsDatabase(MyLyricsUtils.LyricsDB, capArtist, capTitle). Equals(DatabaseUtil.LyricNotFound)) { MyLyricsUtils.LyricsDB.Add(DatabaseUtil.CorrectKeyFormat(capArtist, capTitle), new LyricsItem(capArtist, capTitle, tag.Lyrics, "music tag")); } if ( DatabaseUtil.IsSongInLyricsMarkedDatabase(MyLyricsUtils.LyricsMarkedDB, capArtist, capTitle) .Equals(DatabaseUtil.LyricMarked)) { MyLyricsUtils.LyricsMarkedDB.Remove(DatabaseUtil.CorrectKeyFormat(capArtist, capTitle)); } } else { var status = DatabaseUtil.IsSongInLyricsDatabase(MyLyricsUtils.LyricsDB, song.Artist, song.Title); if (!_mDisregardKnownLyric && status.Equals(DatabaseUtil.LyricFound) || (!_mDisregardMarkedLyric && ((DatabaseUtil.IsSongInLyricsMarkedDatabase(MyLyricsUtils.LyricsMarkedDB, song.Artist, song.Title).Equals( DatabaseUtil.LyricMarked)) || status.Equals(DatabaseUtil.LyricMarked))) || (status.Equals(DatabaseUtil.LyricNotFound) && !DatabaseUtil.IsSongInLyricsMarkedDatabase(MyLyricsUtils.LyricsMarkedDB, song.Artist, song.Title).Equals( DatabaseUtil.LyricMarked))) { if (++_mSongsNotKnown > _mLimit) { bgWorkerSearch.ReportProgress(0); canStartSearch = true; continue; } var lyricId = new[] { song.Artist, song.Title }; _lyricConfigInfosQueue.Enqueue(lyricId); _mSongsToSearch = _lyricConfigInfosQueue.Count; } else if (status.Equals(DatabaseUtil.LyricFound)) { _mSongsWithLyric += 1; } else //if (status.Equals(MyLyricsUtil.LYRIC_MARKED)) { _mSongsWithMark += 1; } } bgWorkerSearch.ReportProgress(-1); } } } else { foreach (var kvp in MyLyricsUtils.LyricsMarkedDB) { if (++_mSongsNotKnown > _mLimit) { break; } var lyricId = new[] { kvp.Value.Artist, kvp.Value.Title }; _lyricConfigInfosQueue.Enqueue(lyricId); _mSongsToSearch = _lyricConfigInfosQueue.Count; bgWorkerSearch.ReportProgress(-1); } } bgWorkerSearch.ReportProgress(0); #endregion #region 2. Search music tags for lyrics // only if user wants to read from music tag and the music tags already aren't disregarded in the search if (_mAutomaticReadFromToMusicTag && !_mDisregardSongWithLyricInTag) { var mSongsToSearchOnline = new Queue(); foreach (string[] song in _lyricConfigInfosQueue) { if (!LyricFoundInMusicTag(song[0], song[1])) { mSongsToSearchOnline.Enqueue(new[] { song[0], song[1] }); } if (_stopCollectingOfTitles) { bgWorkerSearch.CancelAsync(); return; } } _lyricConfigInfosQueue = mSongsToSearchOnline; } #endregion #region 3. Searching for lyrics // create worker thread instance if (_lyricConfigInfosQueue.Count > 0) { _mFind = SettingManager.GetParamAsString(SettingManager.Find, ""); _mReplace = SettingManager.GetParamAsString(SettingManager.Replace, ""); _mEventStopThread = new ManualResetEvent(false); _lyricsController = new LyricsController(this, _mEventStopThread, _sitesToSearchArray, false, false, _mFind, _mReplace); _lyricsController.NoOfLyricsToSearch = _lyricConfigInfosQueue.Count; ThreadStart runLyricController = delegate { _lyricsController.Run(); }; _mLyricControllerThread = new Thread(runLyricController); _mLyricControllerThread.Start(); _lyricsController.StopSearches = false; while (_lyricConfigInfosQueue.Count != 0) { // If the user has cancelled the search => end this if (_stopCollectingOfTitles && _lyricsController != null) { bgWorkerSearch.CancelAsync(); return; } if (_lyricsController == null) { return; } if (_lyricsController.NoOfCurrentSearches < NumberOfCurrentSearchesAllowed && _lyricsController.StopSearches == false) { var lyricID = (string[])_lyricConfigInfosQueue.Dequeue(); //TODO: if there is a lyric in the music tag of the file, then include this in the db and don't search online var artist = lyricID[0]; var title = lyricID[1]; BatchLogger.Info("New!: Looking for {0} - {1}.", artist, title); _lyricsController.AddNewLyricSearch(artist, title, MediaPortalUtil.GetStrippedPrefixArtist(artist, _mStrippedPrefixStrings)); } Thread.Sleep(100); } } else { ThreadFinished = new[] { "", "", "No titles left for online search", "" }; } #endregion }
private void GetSettingsFromConfigurationXml() { #region Get settings from in configuration file try { var sitesMode = SettingManager.GetParamAsString(SettingManager.SitesMode, rdLyricsMode.Tag as string); if (sitesMode.Equals(rdLyricsMode.Tag)) { rdLyricsMode.Checked = true; } else if (sitesMode.Equals(rbUserSelectMode.Tag)) { rbUserSelectMode.Checked = true; for (var index = 0; index < sitesList.Items.Count; index++) { var active = SettingManager.GetParamAsBool(SettingManager.SitePrefix + (sitesList.Items[index]), true); sitesList.SetItemChecked(index, active); } } else { rbLrcMode.Checked = true; } rdTrackBar_CheckedChanged(null, null); Setup.GetInstance().UpdateActiveSitesInSetup(sitesList); tbLimit.Text = SettingManager.GetParamAsString(SettingManager.Limit, _mTotalTitles.ToString(CultureInfo.InvariantCulture)); tbPluginName.Text = SettingManager.GetParamAsString(SettingManager.PluginsName, SettingManager.DefaultPluginName); cbAutoFetch.Checked = SettingManager.GetParamAsBool(SettingManager.AutomaticFetch, false); cbAutomaticUpdate.Checked = SettingManager.GetParamAsBool(SettingManager.AutomaticUpdateWhenFirstFound, false); cbMoveSongFrom.Checked = SettingManager.GetParamAsBool(SettingManager.MoveLyricFromMarkedDatabase, false); _mAutomaticWriteToMusicTag = SettingManager.GetParamAsBool(SettingManager.AutomaticWriteToMusicTag, false); cbMusicTagWrite.Checked = _mAutomaticWriteToMusicTag; _mAutomaticReadFromToMusicTag = SettingManager.GetParamAsBool(SettingManager.AutomaticReadFromMusicTag, false); cbMusicTagAlwaysCheck.Checked = _mAutomaticReadFromToMusicTag; cbUseAutoScrollAsDefault.Checked = SettingManager.GetParamAsBool(SettingManager.UseAutoscroll, false); tbLrcTaggingOffset.Text = SettingManager.GetParamAsString(SettingManager.LrcTaggingOffset, SettingManager.DefaultLrcTaggingOffset); tbLrcTaggingName.Text = SettingManager.GetParamAsString(SettingManager.LrcTaggingName, ""); _mGuidString = SettingManager.GetParamAsString(SettingManager.Guid, ""); cbUploadLrcAutomatically.Checked = SettingManager.GetParamAsBool(SettingManager.UploadLrcToLrcFinder, false); cbAlwaysAskForUploadToLrcFinder.Checked = SettingManager.GetParamAsBool(SettingManager.AlwaysAskUploadLrcToLrcFinder, false); lbSongsLimitNote.Text = ("(You have currently " + _mTotalTitles + " titles in your music database)"); trackBar.Value = SettingManager.GetParamAsInt(SettingManager.DefaultSitesModeValue, 2); // Update the search sites according to trackbar if (rdLyricsMode.Checked) { trackBar_Scroll(null, null); } comboBoxLanguages.SelectedItem = SettingManager.GetParamAsString(SettingManager.TranslationLanguage, SettingManager.DefaultTranslationLanguage); _mFind = SettingManager.GetParamAsString(SettingManager.Find, ""); _mReplace = SettingManager.GetParamAsString(SettingManager.Replace, ""); if (_mFind != "") { var findArray = _mFind.Split(','); var replaceArray = _mReplace.Split(','); var valueIndex = 0; dbGridView.Rows.Clear(); foreach (var findValue in findArray) { if (findValue != "") { dbGridView.Rows.Insert(valueIndex, 1); var row = dbGridView.Rows[valueIndex]; row.Cells[0].Value = findValue; row.Cells[1].Value = replaceArray[valueIndex]; valueIndex++; } } } if (string.IsNullOrEmpty(_mGuidString)) { MGuid = Guid.NewGuid(); _mGuidString = MGuid.ToString("P"); SettingManager.SetParam(SettingManager.Guid, _mGuidString); } else { MGuid = new Guid(_mGuidString); } } catch (Exception ex) { MessageBox.Show("Something has gone wrong when reading Mediaportal.xml (" + ex.Message + ")"); } _mStrippedPrefixStrings = MediaPortalUtil.GetStrippedPrefixStringArray(); #endregion }