/// <summary> /// Gets a song file plugin by index /// </summary> /// <param name="index"></param> /// <exception cref="NotImplementedException">Thrown if the selected plugin has no implementation</exception> /// <returns></returns> private static ISongFilePlugin CreateByTypeIndex(int index) { if (index >= 0 && index < SongFilePluginFactory.GetWriterPlugins().Count) { return(SongFilePluginFactory.GetWriterPlugins().ToArray()[index]); } throw new NotImplementedException(); }
/// <summary> /// Opens a new song in a song editor child window /// </summary> /// <param name="fileName"></param> /// <param name="plugin"></param> public void OpenSong(string fileName, ISongFilePlugin plugin = null) { for (int i = 0; i < MdiChildren.Count(); i++) { EditorChildMetaData md = (EditorChildMetaData)MdiChildren[i].Tag; if (!string.IsNullOrEmpty(md.Filename) && String.Compare( Path.GetFullPath(md.Filename).TrimEnd('\\'), Path.GetFullPath(fileName).TrimEnd('\\'), StringComparison.InvariantCultureIgnoreCase) == 0) { MdiChildren[i].Show(); MdiChildren[i].Focus(); return; } } try { if (plugin == null) { plugin = SongFilePluginFactory.Create(fileName); } var sng = plugin.Load(fileName); SongTemplateMapper stm = new SongTemplateMapper(_settings); // Set default formattig if none exists if (sng.Formatting == null) { stm.ApplyFormattingFromSettings(sng); } // Set default background if none is set foreach (var p in sng.Parts) { foreach (var s in p.Slides) { if (s.Background == null) { s.Background = stm.GetDefaultBackground(); } } } CreateSongEditorChildForm(sng, fileName, false); } catch (NotImplementedException) { MessageBox.Show(StringResources.SongFormatNotSupported, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception e) { MessageBox.Show(StringResources.SongFileHasErrors + @" (" + e.Message + @")!", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Returns the filter string used in save file dialogs /// </summary> /// <returns></returns> private static string GetSaveFileBoxFilter() { String fltr = String.Empty; foreach (ISongFilePlugin t in SongFilePluginFactory.GetWriterPlugins()) { if (fltr != string.Empty) { fltr += "|"; } fltr += t.GetFileTypeDescription() + " (*" + t.GetFileExtension() + ")|*" + t.GetFileExtension(); } return(fltr); }
/// <summary> /// Reloads all songs from the song direcory /// specified in the application settings /// </summary> public void Reload() { // Find song files var songPaths = new List <string>(); foreach (var ext in SongFilePluginFactory.SupportedExtensions) { var songFilePaths = Directory.GetFiles(SongDirPath, "*" + ext, SearchOption.AllDirectories); songPaths.AddRange(songFilePaths); } var cnt = songPaths.Count; // Load songs into list var i = 0; SongList = new Dictionary <string, SongItem>(); foreach (var path in songPaths) { try { var plugin = SongFilePluginFactory.Create(path); var song = plugin.Load(path); var si = new SongItem { Plugin = plugin, Filename = path, Song = song, SearchText = SongSearchUtil.GetSearchableSongText(song) }; SongList.Add(path, si); if (i % 25 == 0) { var e = new SongLoadEventArgs(i, cnt); if (SongLoaded != null) { SongLoaded(e); } } i++; } catch (Exception e) { log.Error(@"Unable to load song file " + path + @" (" + e.Message + @")"); log.Error(e.StackTrace); } } }
/// <summary> /// Saves a song /// </summary> /// <param name="sng">Song to be saved</param> /// <param name="fileName">Target file name</param> private void SaveSong(Song sng, String fileName) { // Load plugin based on the song filename ISongFilePlugin plugin = SongFilePluginFactory.Create(fileName); // Save song using plugin plugin.Save(sng, fileName); // Set status SetStatus(String.Format(StringResources.SongSavedAs, fileName)); // Inform others by firing a SongSaved event if (SongSaved != null) { SongSavedEventArgs p = new SongSavedEventArgs(sng, fileName); SongSaved(this, p); } }
/// <summary> /// Get song file plugins which can open files /// </summary> /// <returns></returns> private static List <ISongFilePlugin> GetOpenFilePlugins() { return(SongFilePluginFactory.GetPlugins().Where(t => t.IsWritingSupported()).ToList()); }
private void buttonImport_Click(object sender, EventArgs e) { SongTemplateMapper stm = new SongTemplateMapper(_settings); List <String> filesToOpen = new List <string>(); int cnt = 0; for (int x = 0; x < listViewSongs.Items.Count; x++) { var item = listViewSongs.Items[x]; if (item.Checked) { Song sng = ((Song)listViewSongs.Items[x].Tag); // Apply formatting stm.ApplyFormattingFromSettings(sng); // Apply default background foreach (var p in sng.Parts) { foreach (var s in p.Slides) { if (s.Background == null) { s.Background = stm.GetDefaultBackground(); } } } // TODO: Allow selection of plugin ISongFilePlugin filePlugin = SongFilePluginFactory.Create(SongFilePluginFactory.GetWriterPlugins().First().GetType()); string fileName = _settings.DataDirectory + Path.DirectorySeparatorChar + _settings.SongDir + Path.DirectorySeparatorChar + sng.Title + filePlugin.GetFileExtension(); if ((File.Exists(fileName) && (MessageBox.Show( string.Format(StringResources.SongExistsAlready, ((Song)listViewSongs.Items[x].Tag).Title) + @" " + StringResources.Overwrite + @"?", StringResources.SongImporter, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)) || !File.Exists(fileName)) { // TODO Exception handling filePlugin.Save(sng, fileName); filesToOpen.Add(fileName); cnt++; } } } if (cnt > 0) { MessageBox.Show(string.Format(StringResources.SongsImported, cnt), StringResources.SongImporter, MessageBoxButtons.OK, MessageBoxIcon.Information); if (checkBoxUseEditor.Checked) { foreach (var f in filesToOpen) { OpenInEditor.Add(f); } } DialogResult = DialogResult.OK; } Close(); }
public void ImportDialog(IWin32Window owner) { string defaultDirectory = !string.IsNullOrEmpty(_settings.CurrentSongImporterDirectory) && Directory.Exists(_settings.CurrentSongImporterDirectory) ? _settings.CurrentSongImporterDirectory : _settings.DataDirectory; List <ISongFilePlugin> plugins = SongFilePluginFactory.GetImportPlugins(); List <string> filters = new List <string>(); foreach (ISongFilePlugin plugin in plugins) { filters.Add(string.Format("{0} (*{1})|*{1}", plugin.GetFileTypeDescription(), plugin.GetFileExtension())); } //var plugin = new SongSelectFilePlugin(); var dlg = new OpenFileDialog() { AddExtension = true, CheckPathExists = true, CheckFileExists = true, Filter = string.Join("|", filters.ToArray()), InitialDirectory = defaultDirectory, Title = StringResources.SongImporter, Multiselect = true }; if (dlg.ShowDialog(owner) == DialogResult.OK) { var plugin = plugins[dlg.FilterIndex - 1]; SongTemplateMapper stm = new SongTemplateMapper(_settings); // Save selected directory if (dlg.FileNames.Length > 0) { _settings.CurrentSongImporterDirectory = Path.GetDirectoryName(dlg.FileNames[0]); } int i = 0; foreach (var selectedFileName in dlg.FileNames) { // Load song var sng = plugin.Load(selectedFileName); // Apply formatting stm.ApplyFormattingFromSettings(sng); // Apply default background foreach (var p in sng.Parts) { foreach (var s in p.Slides) { if (s.Background == null) { s.Background = stm.GetDefaultBackground(); } } } // Initialize writer ISongFilePlugin filePlugin = SongFilePluginFactory.Create(SongFilePluginFactory.GetWriterPlugins().First().GetType()); // Define target file name string fileName = _settings.DataDirectory + Path.DirectorySeparatorChar + _settings.SongDir + Path.DirectorySeparatorChar + sng.Title + filePlugin.GetFileExtension(); // Check if already exists if ((File.Exists(fileName) && (MessageBox.Show( string.Format(StringResources.SongExistsAlready, sng.Title) + @" " + StringResources.Overwrite + @"?", StringResources.SongImporter, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)) || !File.Exists(fileName)) { // TODO Exception handling filePlugin.Save(sng, fileName); i++; // Inform others by firing a SongSaved event if (SongSaved != null) { SongSavedEventArgs p = new SongSavedEventArgs(sng, selectedFileName); SongSaved(this, p); } } } MessageBox.Show(string.Format(StringResources.nSongsHaveBeenImported, i), StringResources.SongImporter, MessageBoxButtons.OK, MessageBoxIcon.Information); } }