public int GetFileFormatIndex(string path, string sFormats) { if (System.Windows.Application.Current == null) new System.Windows.Application { ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown }; var context = /*SynchronizationContext.Current ??*/ new WindowsFormsSynchronizationContext(); string fileFormatByDefault = "Portable Document Format (*.pdf)"; try { WorkshareOnline.Instance.Security.Authenticate(context); var fileFormats = ParseFormats(sFormats); var window = new FileSaveWindow(path, fileFormats); if (fileFormats.Contains(fileFormatByDefault)) { window.SelectedFileFormatIndex = fileFormats.FindIndex(x => x.Equals(fileFormatByDefault, StringComparison.InvariantCultureIgnoreCase)); } new System.Windows.Interop.WindowInteropHelper(window).Owner = Process.GetCurrentProcess().MainWindowHandle; if (window.ShowDialog() == true) { platformFolder = (IPlatformFolder)window.SelectedEntity; fileName = window.FileName; return window.SelectedFileFormatIndex; } } catch (OperationCanceledException) { // ignore the user has cancelled } catch (UnauthorizedAccessException e) { Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero, e.Message, Workshare.Interop.Messaging.MessageButtons.WsOK, Workshare.Interop.Messaging.MessageBranding.WsDefault, Workshare.Interop.Messaging.MessageType.WsErrorIcon, "", 0); } catch (Exception e) { var message = e.InnerException == null ? e.Message : e.InnerException.Message; Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero, "Error saving to Workshare: \n\n" + message, Workshare.Interop.Messaging.MessageButtons.WsOK, Workshare.Interop.Messaging.MessageBranding.WsDefault, Workshare.Interop.Messaging.MessageType.WsErrorIcon, "", 0); } return -1; }
public async void SaveOnline(IOfficeApplication application) { if (!CanExcecuteEvent.WaitOne(TimeSpan.FromSeconds(1))) { Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero, "Other operation still in progress", Workshare.Interop.Messaging.MessageButtons.WsOK, Workshare.Interop.Messaging.MessageBranding.WsDefault, Workshare.Interop.Messaging.MessageType.WsInfoIcon, "", 0); return; } try { if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext()); } var context = SynchronizationContext.Current; WorkshareOnline.Instance.Security.Authenticate(context); var activeDocument = application.ActiveDocument; try { var path = activeDocument.SaveSnapShot(); var versionId = IsFileAlreadySaved(activeDocument.Instance); if (versionId <= 0) { var window = new FileSaveWindow(path, GetSupportedFileFormats()); new System.Windows.Interop.WindowInteropHelper(window).Owner = Process.GetCurrentProcess().MainWindowHandle; Debug.Assert(File.Exists(path), "file was not created"); if (!File.Exists(path)) throw new Exception("Cannot save snapshot of file"); if (window.ShowDialog() == true) { path = ConvertToFileFormat(application, activeDocument.DefaultExtension, path, window.FileName, window.SelectedFileFormatIndex); var platformFolder = window.SelectedEntity as IPlatformFolder; if (platformFolder != null) { Events.Events.Stream.GetEvent<DocumentSavingOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance }); try { var file = await Task.Factory.StartNew(() => { var breadCrumb = PlatformUtils.GetBreadCrumb(platformFolder); MostRecentlyUsedFolders.Add(platformFolder.Url, breadCrumb); IPlatformFileTransfer fileTransfer = WorkshareOnline.Instance.FileTransfer; Debug.Assert(File.Exists(path), "file was not existes before upload"); return fileTransfer.Upload(path, Path.GetFileNameWithoutExtension(path), platformFolder); }); Events.Events.Stream.GetEvent<DocumentSavedOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance, VersionId = file.VersionId, File = file }); } catch (Exception ex) { Logger.LogError(ex); Events.Events.Stream.GetEvent<DocumentSavedOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance, Exception = ex }); } } } } else { try { Events.Events.Stream.GetEvent<DocumentNewVersionSavingOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance }); var uploadRes = await Task.Factory.StartNew(() => { IPlatformVersion versionInfo = WorkshareOnline.Instance.Files.GetVersion(versionId); var res = WorkshareOnline.Instance.FileTransfer.UploadNewVersion2(path, versionInfo.FileId); return res; }); Events.Events.Stream.GetEvent<DocumentNewVersionSavedOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance, VersionId = (int) uploadRes.Item1.Id, File = uploadRes.Item2, Version = uploadRes.Item1 }); } catch (Exception ex) { Logger.LogError(ex); Events.Events.Stream.GetEvent<DocumentNewVersionSavedOnlineEvent>() .Publish(new SaveDocumentEventArgs() { Document = activeDocument.Instance, Exception = ex, }); } } } finally { activeDocument.Dispose(); _lcfm.Dispose(); } } catch (OperationCanceledException) { // ignore the user has cancelled } catch (UnauthorizedAccessException e) { Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero, e.Message, Workshare.Interop.Messaging.MessageButtons.WsOK, Workshare.Interop.Messaging.MessageBranding.WsDefault, Workshare.Interop.Messaging.MessageType.WsErrorIcon, "", 0); Logger.LogError(e); } catch (Exception e) { var message = e.InnerException == null ? e.Message : e.InnerException.Message; Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero, "Error saving to Workshare: \n\n" + message, Workshare.Interop.Messaging.MessageButtons.WsOK, Workshare.Interop.Messaging.MessageBranding.WsDefault, Workshare.Interop.Messaging.MessageType.WsErrorIcon, "", 0); Logger.LogError(e); } finally { CanExcecuteEvent.Set(); } }