Ejemplo n.º 1
0
 private T RetryingPolicyAction <T>(Func <T> func, int step = 1)
 {
     logger.Debug($"RetryingPolicyAction : tentative {step}/{_retry_tentative} in progress...");
     if (step >= _retry_tentative)
     {
         logger.Error("BiblosDS.Library.Common.Converter.OpenOffice.OpenOfficeToPdfConverter.RetryingPolicyAction: retry policy expired maximum tentatives");
         throw new Exception("OpenOfficeToPdfConverter retry policy expired maximum tentatives");
     }
     try
     {
         return(func());
     }
     catch (Exception ex)
     {
         if (ex is unoidl.com.sun.star.uno.RuntimeException || ex is unoidl.com.sun.star.io.IOException)
         {
             logger.Warn($"SafeActionWithRetryPolicy : tentative {step}/{_retry_tentative} failed. Killing OpenOffice process before retry.", ex);
             localContext = null;
             KillOpenOfficeProcesses();
             return(RetryingPolicyAction(func, ++step));
         }
         logger.Error($"RetryingPolicyAction: exception type not manageable.", ex);
         throw ex;
     }
 }
Ejemplo n.º 2
0
 public Component(ucss.uno.XComponentContext ctx, uno.Any[] args)
 {
     m_args    = new Any[args.Length + 1];
     m_args[0] = new Any(typeof(ucss.uno.XComponentContext), ctx);
     for (int i = 0; i < args.Length; i++)
     {
         m_args[i + 1] = args[i];
     }
 }
Ejemplo n.º 3
0
        void PerformLoad()
        {
            // Start LibreOffice and load file
            unoidl.com.sun.star.uno.XComponentContext     localContext        = uno.util.Bootstrap.bootstrap();
            unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
            desktop = (XDesktop)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
            var componentLoader = (XComponentLoader)desktop;

            component = componentLoader.loadComponentFromURL(CreateFileUrl(file.FullName), "_blank", 0, new PropertyValue[] { });

            // TODO: while/before loading, set the viewsettings ViewId:=view1, PageKind:=0 to reset to the default view
            // (instead of notes or outline view). Unfortunately these settings aren't accesible in the API ...

            // Get the main window's handle and hide the window
            document = (XModel)component;
            XWindow window = document.getCurrentController().getFrame().getContainerWindow();

            window.setVisible(false);
            XSystemDependentWindowPeer xWindowPeer = (XSystemDependentWindowPeer)(window);

            mainHandle = new IntPtr((int)xWindowPeer.getWindowHandle(new byte[] { }, SystemDependent.SYSTEM_WIN32).Value);
            //ShowWindow(mainHandle, 0);

            presentation = (XPresentation2)((XPresentationSupplier)component).getPresentation();

            CreateThumbnails();

            listener.SlideTransitionStarted += (sender, args) =>
            {
                OnSlideIndexChanged();
            };

            Start();
            controller.gotoSlideIndex(0);

            LoadPreviewProvider();

            base.OnLoaded(true);
        }
Ejemplo n.º 4
0
 public static XMultiServiceFactory Connect()
 {
     unoidl.com.sun.star.uno.XComponentContext m_xContext = null;
     try
     {
         m_xContext = uno.util.Bootstrap.bootstrap();
         System.Console.WriteLine("OO: bootstrap finished with success");
     }
     catch (System.Exception exp)
     {
         System.Console.WriteLine("OO: Exception during bootstrap <" +
                                  exp.Message + ">");
         System.Console.WriteLine("OO: Source" + exp.Source);
         if (exp.InnerException != null)
         {
             System.Console.WriteLine("OO: Inner Message" +
                                      exp.InnerException.Message);
             System.Console.WriteLine("OO: Inner Source" +
                                      exp.InnerException.Source);
         }
     }
     return((m_xContext != null) ?
            (XMultiServiceFactory)m_xContext.getServiceManager() : null);
 }
Ejemplo n.º 5
0
 public Component(ucss.uno.XComponentContext ctx)
 {
     m_args = new Any[] { new Any(typeof(ucss.uno.XComponentContext), ctx) };
     m_A2   = 0;
     m_A4   = 0;
 }
Ejemplo n.º 6
0
        public byte[] Convert(byte[] fileSource, string fileExtension, string extReq, AttachConversionMode mode)
        {
            logger.DebugFormat("Convert {0} to {1}", fileExtension, extReq);
            string fileName = Path.GetTempFileName();

            try
            {
                return(RetryingPolicyAction <byte[]>(() => {
                    File.WriteAllBytes(fileName + fileExtension, fileSource);
                    if (localContext == null)
                    {
                        localContext = uno.util.Bootstrap.bootstrap();
                    }

                    unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
                    XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");

                    PropertyValue[] propertyValue = new PropertyValue[2];
                    PropertyValue aProperty = new PropertyValue();
                    aProperty.Name = "Hidden";
                    aProperty.Value = new uno.Any(true);
                    propertyValue[0] = aProperty;

                    propertyValue[1] = new PropertyValue();
                    propertyValue[1].Name = "IsSkipEmptyPages";
                    propertyValue[1].Value = new uno.Any(true);

                    XComponent xComponent = null;
                    bool toConvert = false;
                    string fileConvertedName;
                    try
                    {
                        string fileToConvertName = PathConverter(fileName + fileExtension);
                        fileConvertedName = PathConverter(fileName + "." + extReq);
                        logger.DebugFormat("Url to comvert: {0} - {1}", fileToConvertName, fileConvertedName);
                        xComponent = componentLoader.loadComponentFromURL(
                            fileToConvertName,
                            "_blank",
                            0,
                            propertyValue
                            );
                        if (xComponent == null)
                        {
                            throw new DocumentNotConvertible_Exception();
                        }

                        logger.Debug("Verify if is to process...");
                        if (fileExtension.Contains("doc") && (UseRedirectMethod(xComponent)))
                        {
                            logger.Debug("No conversion to do...");
                            toConvert = true;
                        }
                        else
                        {
                            object xls = null;

                            if (xComponent is XSpreadsheetDocument)
                            {
                                xls = xComponent as XSpreadsheetDocument;
                            }
                            else if (xComponent is XTextDocument)
                            {
                                xls = xComponent as XTextDocument;
                            }

                            if (xls != null)
                            {
                                //Se i nomi degli sheets sono fra quelli non convertibili, usare il converter Office.
                                if (xls is XSpreadsheetDocument)
                                {
                                    var sheetNames = (xls as XSpreadsheetDocument)
                                                     .getSheets()
                                                     .getElementNames()
                                                     .ToArray();

                                    for (int i = 0; i < sheetNames.Length; i++)
                                    {
                                        sheetNames[i] = (sheetNames[i] ?? string.Empty).ToUpper();
                                    }

                                    var namesSet = (ConfigurationManager.AppSettings["RedirectSheetNames"] ?? string.Empty)
                                                   .Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                                   .OrderByDescending(x => x.Length);

                                    var founded = false;

                                    foreach (var set in namesSet)
                                    {
                                        var names = set
                                                    .Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                                        if (names.Length <= sheetNames.Length)
                                        {
                                            founded = true;
                                            foreach (var n in names)
                                            {
                                                if (!sheetNames.Contains(n.ToUpper()))
                                                {
                                                    founded = false;
                                                    break;
                                                }
                                            }

                                            if (founded)
                                            {
                                                logger.Debug("No conversion to do...");
                                                return null;
                                            }
                                        }
                                    }
                                }

                                //var xls = xComponent as XSpreadsheetDocument;
                                var styles = (xls as XStyleFamiliesSupplier).getStyleFamilies();
                                var pageStyles = styles.getByName("PageStyles").Value as XNameContainer;

                                bool scaleToPages = false;

                                var configured = ConfigurationManager.AppSettings["ScaleToPages"];
                                try
                                {
                                    if (!string.IsNullOrEmpty(configured))
                                    {
                                        var dummy = bool.Parse(configured);
                                        scaleToPages = dummy;
                                    }
                                }
                                catch { logger.ErrorFormat("Invalid value for configured parameter \"{0}\". Current value is \"{1}\".", "ScaleToPages", configured); }

                                var configValue = ConfigurationManager.AppSettings["StampaConforme.ForcePortrait"];
                                var forcePortrait = !string.IsNullOrEmpty(configValue) && configValue.Equals("true", StringComparison.InvariantCultureIgnoreCase);
                                logger.Info("StampaConforme.ForcePortrait: " + forcePortrait.ToString());

                                foreach (var nome in pageStyles.getElementNames())
                                {
                                    var props = pageStyles.getByName(nome).Value as XPropertySet;
                                    if (forcePortrait)
                                    {
                                        var isLandscape = (bool)props.getPropertyValue("IsLandscape").Value;
                                        if (isLandscape)
                                        {
                                            var size = props.getPropertyValue("Size").Value as Size;
                                            var w = size.Width;
                                            size.Width = size.Height;
                                            size.Height = w;
                                            props.setPropertyValue("Size", new uno.Any(typeof(Size), size));
                                            props.setPropertyValue("IsLandscape", new uno.Any(false));
                                        }
                                    }

                                    if (scaleToPages && props.getPropertySetInfo().hasPropertyByName("ScaleToPages"))
                                    {
                                        props.setPropertyValue("ScaleToPages", new uno.Any((short)1));
                                    }
                                }
                            }

                            PropertyValue[] saveProps = new PropertyValue[2];
                            saveProps[0] = new PropertyValue();
                            saveProps[0].Name = "FilterName";
                            saveProps[0].Value = new uno.Any("writer_pdf_Export");

                            saveProps[1] = new PropertyValue();
                            saveProps[1].Name = "IsSkipEmptyPages";
                            saveProps[1].Value = new uno.Any(true);
                            logger.Debug("loadComponentFromURL end.");

                            ((XStorable)xComponent).storeToURL(fileConvertedName, saveProps);
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    finally
                    {
                        if (xComponent != null)
                        {
                            xComponent.dispose();
                        }
                    }
                    logger.DebugFormat("Conversione END - ToConvert: {0}", toConvert);
                    if (toConvert)
                    {
                        return null;
                    }
                    else
                    {
                        return File.ReadAllBytes(fileName + "." + extReq);
                    }
                }));
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
            finally
            {
                try
                {
                    File.Delete(fileName + fileExtension);
                    File.Delete(fileName + extReq);
                    File.Delete(fileName);
                }
                catch { }
            }
        }