public void Share(string path, string version)
        {
            try
            {
                var context = SynchronizationContext.Current ?? new WindowsFormsSynchronizationContext();
                var user = WorkshareOnline.Instance.Security.Authenticate(context);

                using (GroupsServiceLocator.Instance)
                {
                    GroupsServiceLocator.Instance.Initialize(path, user);
                    var window = new ShareWindow();
                    new System.Windows.Interop.WindowInteropHelper(window).Owner =
                        Process.GetCurrentProcess().MainWindowHandle;
                    window.ShowDialog();
                }

            }
            catch (OperationCanceledException)
            {
                // ignore the user has cancelled
            }
            catch (UnauthorizedAccessException e)
            {
                Logger.LogError(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)
            {
                Logger.LogError(e);

                var message = e.InnerException == null ? e.Message : e.InnerException.Message;

                Workshare.Interop.Messaging.WsMessage.ShowMessage(IntPtr.Zero,
                    "Error sharing to Workshare: \n\n" + message, Workshare.Interop.Messaging.MessageButtons.WsOK,
                    Workshare.Interop.Messaging.MessageBranding.WsDefault,
                    Workshare.Interop.Messaging.MessageType.WsErrorIcon, "",
                    0);
            }
        }
Exemple #2
0
        public void ShareOnline(IOfficeApplication application)
        {
            try
            {
                var applicationState = application.ApplicationReadyState();

                if (applicationState == OfficeApplicationReadyState.Success)
                {
                    var context = SynchronizationContext.Current ?? new WindowsFormsSynchronizationContext();
                    var user = WorkshareOnline.Instance.Security.Authenticate(context);

                    using (var activeDocument = application.ActiveDocument)
                    {
                        using (GroupsServiceLocator.Instance)
                        {
                            GroupsServiceLocator.Instance.Initialize(activeDocument.SaveSnapShot(), user);

                            var window = new ShareWindow();
                            new System.Windows.Interop.WindowInteropHelper(window).Owner =
                                Process.GetCurrentProcess().MainWindowHandle;
                            window.Error += OnShareError;
                            window.Error += new System.EventHandler<ExceptionEventArgs>((object sender, ExceptionEventArgs e) =>
                            {
                                Events.Events.Stream.GetEvent<DocumentSavedOnlineEvent>()
                                    .Publish(new SaveDocumentEventArgs()
                                    {
                                        Document = activeDocument.Instance,
                                        Exception = e.Exception.Flatten().InnerException
                                    });
                            });

                            window.StartSharing += new System.EventHandler((object sender, EventArgs e) =>
                            {
                                Events.Events.Stream.GetEvent<DocumentSharingOnlineEvent>()
                                    .Publish(new SaveDocumentEventArgs()
                                    {
                                        Document = activeDocument.Instance
                                    });
                            });
                            
                            window.EndSharing += new System.EventHandler((object sender, EventArgs e) =>
                            {
                                Events.Events.Stream.GetEvent<DocumentSharedOnlineEvent>()
                                    .Publish(new SaveDocumentEventArgs()
                                    {
                                        Document = activeDocument.Instance,
                                        VersionId = ((FileInfoArgs)e).File.VersionId
                                    });
                            });

                            if (window.ShowDialog().GetValueOrDefault(false))
                            {
                                if (!string.IsNullOrEmpty(window.GroupUuid))
                                {
                                    activeDocument.SetCustomProperty("WorkshareGroupUuid", window.GroupUuid);
                                    activeDocument.SetCustomProperty("WorkshareFileId", window.FileId);
                                    activeDocument.Instance.Save();
                                }
                            }
                        }
                    }
                }
                else Utils.ShowErrorMessage(applicationState);
            }
            catch (Exception e)
            {
                Logger.LogError(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);

                Events.Events.Stream.GetEvent<DocumentSavedOnlineEvent>()
                    .Publish(new SaveDocumentEventArgs()
                    {
                        Exception = e
                    });
            }
        }