// TODO: Весь связанный с UI код вынести в отдельный класс public static void Export(IServiceProvider provider) { var activeMessagesSvc = provider.GetService<IActiveMessagesService>(); var canExportMessages = activeMessagesSvc != null && activeMessagesSvc.ActiveMessages.Any(); var activeForumSvc = provider.GetService<IActiveForumService>(); var canExportForum = activeForumSvc?.ActiveForum != null; var mainWnd = provider.GetRequiredService<IUIShell>().GetMainWindowParent(); using (var emd = new ExportMessageDialog(canExportMessages, canExportForum)) { if (emd.ShowDialog(mainWnd) != DialogResult.OK) return; var uiInfo = new {emd.FileName, emd.ExportMode, emd.ExportFormat, emd.UnreadMessagesOnly}; var noMessages = false; ProgressWorker.Run( provider, false, pi => { pi.SetProgressText(_exportPrepareText); var activeMsgSvc = provider.GetRequiredService<IActiveMessagesService>(); // Prepare msg list IList<IMsg> messages; switch (uiInfo.ExportMode) { case ExportMode.Messages: messages = new List<IMsg>( (!uiInfo.UnreadMessagesOnly ? activeMsgSvc.ActiveMessages : activeMsgSvc.ActiveMessages.Where(msg => !msg.IsRead)) .Cast<IMsg>()); break; case ExportMode.Topics: messages = new List<IMsg>(100); foreach (var msg in activeMsgSvc.ActiveMessages) GetAllChildren((IMsg)msg.Topic, messages, uiInfo.UnreadMessagesOnly); break; case ExportMode.Forum: messages = new List<IMsg>(1000); foreach (IMsg msg in Forums.Instance.ActiveForum.Msgs) GetAllChildren(msg, messages, uiInfo.UnreadMessagesOnly); break; default: throw new ArgumentException("Unknown export mode"); } if (messages.Count == 0) { noMessages = true; return; } ProgressDelegate pd = (count, total) => { pi.SetProgressText(_exportMessageText.FormatWith(count, total)); pi.ReportProgress(total, count); }; using (var fs = new FileStream(uiInfo.FileName, FileMode.Create)) switch (uiInfo.ExportFormat) { case ExportFormat.Text: Export2Text(messages, fs, pd); break; case ExportFormat.HTML: Export2HTML(provider, messages, fs, pd); break; case ExportFormat.MHT: Export2Mht(provider, messages, fs, pd); break; } }); if (noMessages) MessageBox.Show(mainWnd, ExportResources.NoMessages); } }
// TODO: Весь связанный с UI код вынести в отдельный класс public static void Export(IServiceProvider provider) { var activeMessagesSvc = provider.GetService <IActiveMessagesService>(); var canExportMessages = activeMessagesSvc != null && activeMessagesSvc.ActiveMessages.Any(); var activeForumSvc = provider.GetService <IActiveForumService>(); var canExportForum = activeForumSvc != null && activeForumSvc.ActiveForum != null; var mainWnd = provider.GetRequiredService <IUIShell>().GetMainWindowParent(); using (var emd = new ExportMessageDialog(canExportMessages, canExportForum)) { if (emd.ShowDialog(mainWnd) != DialogResult.OK) { return; } var uiInfo = new { emd.FileName, emd.ExportMode, emd.ExportFormat, emd.UnreadMessagesOnly }; var noMessages = false; ProgressWorker.Run( provider, false, pi => { pi.SetProgressText(_exportPrepareText); var activeMsgSvc = provider.GetRequiredService <IActiveMessagesService>(); // Prepare msg list IList <IMsg> messages = null; switch (uiInfo.ExportMode) { case ExportMode.Messages: messages = new List <IMsg>( (!uiInfo.UnreadMessagesOnly ? activeMsgSvc.ActiveMessages : activeMsgSvc.ActiveMessages.Where(msg => !msg.IsRead)) .Cast <IMsg>()); break; case ExportMode.Topics: messages = new List <IMsg>(100); foreach (var msg in activeMsgSvc.ActiveMessages) { GetAllChildren((IMsg)msg.Topic, messages, uiInfo.UnreadMessagesOnly); } break; case ExportMode.Forum: messages = new List <IMsg>(1000); foreach (IMsg msg in Forums.Instance.ActiveForum.Msgs) { GetAllChildren(msg, messages, uiInfo.UnreadMessagesOnly); } break; default: throw new ArgumentException("Unknown export mode"); } if (messages.Count == 0) { noMessages = true; return; } ProgressDelegate pd = (count, total) => { pi.SetProgressText(_exportMessageText.FormatStr(count, total)); pi.ReportProgress(total, count); }; using (var fs = new FileStream(uiInfo.FileName, FileMode.Create)) switch (uiInfo.ExportFormat) { case ExportFormat.Text: Export2Text(messages, fs, pd); break; case ExportFormat.HTML: Export2HTML(provider, messages, fs, pd); break; case ExportFormat.MHT: Export2Mht(provider, messages, fs, pd); break; } }); if (noMessages) { MessageBox.Show(mainWnd, ExportResources.NoMessages); } } }