コード例 #1
0
ファイル: CollectionService.cs プロジェクト: kbalint/Dopamine
        public async Task <ExportPlaylistsResult> ExportPlaylistAsync(Playlist playlist, string destinationDirectory, string iPlaylistName, bool generateUniqueName)
        {
            ExportPlaylistsResult result = ExportPlaylistsResult.Success;

            List <TrackInfo> tracks = await Utils.OrderTracksAsync(await this.trackRepository.GetTracksAsync(playlist.ToList()), TrackOrder.ByFileName);

            await Task.Run(() => {
                try
                {
                    string playlistFileNameWithoutPathAndExtension = FileOperations.SanitizeFilename(iPlaylistName);
                    string playlistFileFullPath = Path.Combine(destinationDirectory, string.Concat(playlistFileNameWithoutPathAndExtension, FileFormats.M3U));

                    if (generateUniqueName)
                    {
                        // Make sure the file we try to create doesn't exist yet
                        while (System.IO.File.Exists(playlistFileFullPath))
                        {
                            playlistFileNameWithoutPathAndExtension = playlistFileNameWithoutPathAndExtension + " (1)";
                            playlistFileFullPath = Path.Combine(destinationDirectory, string.Concat(playlistFileNameWithoutPathAndExtension, FileFormats.M3U));
                        }
                    }

                    // Write all the paths to the file
                    using (StreamWriter file = new StreamWriter(playlistFileFullPath))
                    {
                        foreach (TrackInfo t in tracks)
                        {
                            string audioFileNameWithoutPath = Path.GetFileName(t.Path);

                            // If the audio file is in the same directory as the playlist file,
                            // don't save the full path in the playlist file.
                            if (System.IO.File.Exists(Path.Combine(destinationDirectory, audioFileNameWithoutPath)))
                            {
                                file.WriteLine(audioFileNameWithoutPath);
                            }
                            else
                            {
                                file.WriteLine(t.Path);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    result = ExportPlaylistsResult.Error;
                    LogClient.Instance.Logger.Error("Error while exporting Playlist '{0}'. Exception: {1}", playlist, ex.Message);
                }
            });

            return(result);
        }
コード例 #2
0
ファイル: CollectionService.cs プロジェクト: kbalint/Dopamine
        public async Task <ExportPlaylistsResult> ExportPlaylistsAsync(IList <Playlist> playlists, string destinationDirectory)
        {
            ExportPlaylistsResult result = ExportPlaylistsResult.Success;

            foreach (Playlist pl in playlists)
            {
                ExportPlaylistsResult tempResult = await this.ExportPlaylistAsync(pl, destinationDirectory, pl.PlaylistName, true);

                // If at least 1 export failed, return an error
                if (tempResult == ExportPlaylistsResult.Error)
                {
                    result = tempResult;
                }
            }

            return(result);
        }
コード例 #3
0
        private async Task SaveSelectedPlaylistsAsync()
        {
            if (this.SelectedPlaylists != null && this.SelectedPlaylists.Count > 0)
            {
                if (this.SelectedPlaylists.Count > 1)
                {
                    // Save all the selected playlists
                    // -------------------------------
                    var dlg = new WPFFolderBrowserDialog();

                    if ((bool)dlg.ShowDialog())
                    {
                        try
                        {
                            ExportPlaylistsResult result = await this.collectionService.ExportPlaylistsAsync(this.SelectedPlaylists, dlg.FileName);

                            if (result == ExportPlaylistsResult.Error)
                            {
                                this.dialogService.ShowNotification(
                                    0xe711,
                                    16,
                                    ResourceUtils.GetStringResource("Language_Error"),
                                    ResourceUtils.GetStringResource("Language_Error_Saving_Playlists"),
                                    ResourceUtils.GetStringResource("Language_Ok"),
                                    true,
                                    ResourceUtils.GetStringResource("Language_Log_File"));
                            }
                        }
                        catch (Exception ex)
                        {
                            LogClient.Instance.Logger.Error("Exception: {0}", ex.Message);

                            this.dialogService.ShowNotification(
                                0xe711,
                                16,
                                ResourceUtils.GetStringResource("Language_Error"),
                                ResourceUtils.GetStringResource("Language_Error_Saving_Playlists"),
                                ResourceUtils.GetStringResource("Language_Ok"),
                                true,
                                ResourceUtils.GetStringResource("Language_Log_File"));
                        }
                    }
                }
                else if (this.SelectedPlaylists.Count == 1)
                {
                    // Save 1 playlist
                    // ---------------
                    var dlg = new Microsoft.Win32.SaveFileDialog();
                    dlg.FileName   = FileOperations.SanitizeFilename(this.SelectedPlaylists[0].PlaylistName);
                    dlg.DefaultExt = FileFormats.M3U;
                    dlg.Filter     = string.Concat(ResourceUtils.GetStringResource("Language_Playlists"), " (", FileFormats.M3U, ")|*", FileFormats.M3U);

                    if ((bool)dlg.ShowDialog())
                    {
                        try
                        {
                            ExportPlaylistsResult result = await this.collectionService.ExportPlaylistAsync(this.SelectedPlaylists[0], dlg.FileName, false);

                            if (result == ExportPlaylistsResult.Error)
                            {
                                this.dialogService.ShowNotification(
                                    0xe711,
                                    16,
                                    ResourceUtils.GetStringResource("Language_Error"),
                                    ResourceUtils.GetStringResource("Language_Error_Saving_Playlist"),
                                    ResourceUtils.GetStringResource("Language_Ok"),
                                    true,
                                    ResourceUtils.GetStringResource("Language_Log_File"));
                            }
                        }
                        catch (Exception ex)
                        {
                            LogClient.Instance.Logger.Error("Exception: {0}", ex.Message);

                            this.dialogService.ShowNotification(0xe711, 16, ResourceUtils.GetStringResource("Language_Error"), ResourceUtils.GetStringResource("Language_Error_Saving_Playlist"), ResourceUtils.GetStringResource("Language_Ok"), true, ResourceUtils.GetStringResource("Language_Log_File"));
                        }
                    }
                }
                else
                {
                    // Should not happen
                }
            }
        }