private void SearchImportsAndBind(bool newActiveViewModel) { var col = GetElements(_currentDocument); if (col.Any()) { if (MainWindow == null) { MainWindow = new DWGImportManagerWindow(); _deleteManyElementsEvent.MainWindow = MainWindow; MainWindow.Loaded += MainWindowOnLoaded; MainWindow.Closed += MainWindow_Closed; _dwgImportManagerVm = new DWGImportManagerVM(_uiApplication, col, _deleteElementEvent, _changeViewEvent, _deleteManyElementsEvent); MainWindow.DataContext = _dwgImportManagerVm; MainWindow.Show(); } else { MainWindow.Activate(); if (newActiveViewModel) { _dwgImportManagerVm = new DWGImportManagerVM(_uiApplication, col, _deleteElementEvent, _changeViewEvent, _deleteManyElementsEvent); MainWindow.DataContext = _dwgImportManagerVm; } } } else { MessageBox.Show(Language.GetItem(LangItem, "msg2")); MainWindow?.Close(); } }
private void BtDeleteArchive_OnClick(object sender, RoutedEventArgs e) { if (File.Exists(_currentFileToUpload)) { if (MessageBox.ShowYesNo(ModPlusAPI.Language.GetItem(LangItem, "msg30"), MessageBoxIcon.Question)) { var wasDel = false; try { File.Delete(_currentFileToUpload); wasDel = true; } catch { MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg31")); } if (wasDel) { BtDeleteArchive.Visibility = Visibility.Collapsed; BtUploadArchive.Visibility = Visibility.Collapsed; BtSeeArchive.Visibility = Visibility.Collapsed; BtMakeArchive.Visibility = Visibility.Visible; } } } }
// upload private async void BtUploadArchive_OnClick(object sender, RoutedEventArgs e) { if (!await ModPlusAPI.Web.Connection.HasAllConnectionAsync(1)) { await this.ShowMessageAsync(ModPlusAPI.Language.GetItem(LangItem, "msg23"), string.Empty); return; } if (File.Exists(_currentFileToUpload)) { ProgressDialogController controller = null; try { var settings = new MetroDialogSettings { AnimateShow = true, AnimateHide = true, DialogTitleFontSize = 20 }; controller = await this.ShowProgressAsync(ModPlusAPI.Language.GetItem(LangItem, "msg24"), string.Empty, false, settings); controller.Minimum = 0; controller.Maximum = 2; using (var client = await ModPlusAPI.Web.ApiClient.CreateClientAsync()) { controller.SetMessage(ModPlusAPI.Language.GetItem(LangItem, "msg25")); controller.SetProgress(1); await client.UploadUserFile(new[] { "DwgForBaseFromUsers" }, _currentFileToUpload, false, true); controller.SetMessage(ModPlusAPI.Language.GetItem(LangItem, "msg26")); controller.SetProgress(2); var emailSettings = await client.GetEmailSettings(); await SendEmailNotification(emailSettings); } await controller.CloseAsync(); await this.ShowMessageAsync( string.Empty, $"{ModPlusAPI.Language.GetItem(LangItem, "msg27")}: {_currentFileToUpload} {ModPlusAPI.Language.GetItem(LangItem, "msg28")}{Environment.NewLine}{ModPlusAPI.Language.GetItem(LangItem, "msg29")}"); } catch (Exception exception) { if (controller != null) { await controller.CloseAsync(); } ExceptionBox.Show(exception); } } else { MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg22")); } }
// open private void BtSeeArchive_OnClick(object sender, RoutedEventArgs e) { if (File.Exists(_currentFileToUpload)) { Process.Start(_currentFileToUpload); } else { MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg22")); } }
// window closed private void BaseUploading_OnClosed(object sender, EventArgs e) { var tmpFolder = Path.Combine(_dwgBaseFolder, "Temp"); try { // check if temp directory exist if (Directory.Exists(tmpFolder)) { Directory.Delete(tmpFolder, true); } } catch { MessageBox.Show( $"{ModPlusAPI.Language.GetItem(LangItem, "msg20")}: {tmpFolder}{Environment.NewLine}{ModPlusAPI.Language.GetItem(LangItem, "msg21")}"); } }
private void BtMakeArchive_OnClick(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(TbFeedback.Text)) { if (!MessageBox.ShowYesNo(ModPlusAPI.Language.GetItem(LangItem, "h75"))) { return; } } var updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar.SetValue); var updatePtDelegate = new UpdateProgressTextDelegate(ProgressText.SetValue); if (!_filesToBind.Any(x => x.Selected)) { MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg15"), MessageBoxIcon.Alert); return; } CreateArchive(); Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, string.Empty); Dispatcher?.Invoke(updatePbDelegate, DispatcherPriority.Background, System.Windows.Controls.Primitives.RangeBase.ValueProperty, 0.0); }
private void CreateArchive() { // Create a new instance of our ProgressBar Delegate that points // to the ProgressBar's SetValue method. var updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar.SetValue); var updatePtDelegate = new UpdateProgressTextDelegate(ProgressText.SetValue); // progress text Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, ModPlusAPI.Language.GetItem(LangItem, "msg16")); // create temp folder var tmpFolder = Path.Combine(_dwgBaseFolder, "Temp"); if (!Directory.Exists(tmpFolder)) { Directory.CreateDirectory(tmpFolder); } // create base file with selected files items ProgressBar.Minimum = 0; ProgressBar.Maximum = _dwgBaseItems.Count; ProgressBar.Value = 0; // Соберем список файлов-источников чтобы проще их сверять var sourceFiles = new List <string>(); foreach (var fileToBind in _filesToBind) { if (!sourceFiles.Contains(fileToBind.SourceFile)) { sourceFiles.Add(fileToBind.SourceFile); } } var baseFileToArchive = new List <DwgBaseItem>(); for (var i = 0; i < _dwgBaseItems.Count; i++) { Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, $"{ModPlusAPI.Language.GetItem(LangItem, "msg17")}: {i}/{_dwgBaseItems.Count}"); Dispatcher?.Invoke(updatePbDelegate, DispatcherPriority.Background, System.Windows.Controls.Primitives.RangeBase.ValueProperty, (double)i); var dwgBaseItem = _dwgBaseItems[i]; if (sourceFiles.Contains(dwgBaseItem.SourceFile)) { if (!baseFileToArchive.Contains(dwgBaseItem)) { baseFileToArchive.Add(dwgBaseItem); } } } // save xml file var xmlToArchive = Path.Combine(tmpFolder, "UserDwgBase.xml"); DwgBaseHelpers.SerializerToXml(baseFileToArchive, xmlToArchive); if (!File.Exists(xmlToArchive)) { MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg18"), MessageBoxIcon.Close); return; } // comment file var commentFile = CreateCommentFile(tmpFolder); // create zip _currentFileToUpload = Path.ChangeExtension(Path.Combine(tmpFolder, Path.GetRandomFileName()), ".zip"); if (File.Exists(_currentFileToUpload)) { File.Delete(_currentFileToUpload); } using (var zip = ZipFile.Open(_currentFileToUpload, ZipArchiveMode.Create)) { // create directories foreach (var fileToBind in _filesToBind.Where(x => x.Selected)) { zip.CreateEntryFromFile( fileToBind.FullFileName, Path.Combine(fileToBind.SubDirectory, fileToBind.FileName)); } // add xml file and delete him zip.CreateEntryFromFile(xmlToArchive, new FileInfo(xmlToArchive).Name); // add comment file if (!string.IsNullOrEmpty(commentFile) && File.Exists(commentFile)) { zip.CreateEntryFromFile(commentFile, new FileInfo(commentFile).Name); } } File.Delete(xmlToArchive); // show buttons BtMakeArchive.Visibility = Visibility.Collapsed; BtSeeArchive.Visibility = Visibility.Visible; BtDeleteArchive.Visibility = Visibility.Visible; BtUploadArchive.Visibility = Visibility.Visible; Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, ModPlusAPI.Language.GetItem(LangItem, "msg19")); }