How to disable "Save As" menu option from a C# console app?

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Gudra
Posts: 3
Joined: Thu Apr 05, 2018 10:56 am

How to disable "Save As" menu option from a C# console app?

Post by Gudra »

So, I want to disable a single menu option from Office when I launch a new document. Most of what I've read redirect me to this, which is a monstruous Java example I'm not sure I understand fully (I'm new with the API). I tried to convert most of the code there, but I don't know how to convert this line

Code: Select all

XComponentContext xLocalContext =
              com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
Also I have a problem with some objects and uno.Any. The converted code, not working, I have right now would be:

Code: Select all

        private void disableCommands()
        {
            string[] aCommandURLTestSet = { "Open", "About", "SelectAll", "Quit", "SaveAs" };

            // Set the root path for our configuration access
            PropertyValue[] lParams = new PropertyValue[1];
            lParams[0] = new PropertyValue();
            lParams[0].Name = "nodepath";
            lParams[0].Value = new uno.Any("/org.openoffice.Office.Commands/Execute/Disabled");
            XComponentContext xRemoteContext = null;
            XMultiComponentFactory xRemoteServiceManager = null;
            XURLTransformer xTransformer = null;
            //      // Create configuration update access to have write access to the configuration
            // connect
            XComponentContext xLocalContext = createInitialComponentContext(null); //this wont work obviously
            XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
            Object urlResolver = xLocalServiceManager.createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", xLocalContext);
            XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver)urlResolver;
            Object initialObject = xUnoUrlResolver.resolve(
            "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager");
            XPropertySet xPropertySet = (XPropertySet)initialObject;
            Object context = xPropertySet.getPropertyValue("DefaultContext");
            xRemoteContext = (XComponentContext)context;
            xRemoteServiceManager = xRemoteContext.getServiceManager();
            Object transformer = xRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.util.URLTransformer", xRemoteContext);
            xTransformer = (XURLTransformer)transformer;

            Object configProvider = xRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.configuration.ConfigurationProvider", xRemoteContext);
            XMultiServiceFactory xConfigProvider = (XMultiServiceFactory)configProvider;
            Object xAccess = xConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", lParams);

            XSingleServiceFactory xSetElementFactory =
            (XSingleServiceFactory)xAccess;

            XNameContainer xNameContainer = (XNameContainer)xAccess;

            if (xSetElementFactory != null && xNameContainer != null)
            {
                Object[] aArgs = { };

                for (int i = 0; i < aCommandURLTestSet.Length; i++)
                {
                    // Create the nodes with the XSingleServiceFactory of the configuration
                    Object xNewElement = xSetElementFactory.createInstanceWithArguments(new uno.Any(aArgs)); //can't use aArgs as an argument for uno.Any()
                    if (xNewElement != null)
                    {
                        // We have a new node. To set the properties of the node we need
                        // the XPropertySet interface.
                        XPropertySet xPropertySet1 = (XPropertySet)xNewElement;

                        if (xPropertySet1 != null)
                        {
                            // Create a unique node name.
                            String aCmdNodeName = "Command-";
                            aCmdNodeName += i;

                            // Insert the node into the Disabled set
                            xPropertySet.setPropertyValue("Command", new uno.Any(aCommandURLTestSet[i]));
                            xNameContainer.insertByName(aCmdNodeName, new uno.Any(xNewElement)); //can't use aArgs as an argument for uno.Any()
                        }
                    }
                }

                // Commit our changes
                XChangesBatch xFlush = (XChangesBatch)xAccess;
                xFlush.commitChanges();
            }
        }
I'm not sure if this is the way to go with this problem, to be quite honest. I'm totally lost.
User avatar
Zizi64
Volunteer
Posts: 11418
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to disable "Save As" menu option from a C# console a

Post by Zizi64 »

I want to disable a single menu option from Office when I launch a new document.
Do you want to hide it only from the menu an from the toolbar and from the hotkeys, or do you want really disable it?

My first tip:
Just create a template file (manually), with unvisible "Save as..." menu item, and without "Save as..." toolbar icon by usage the Customize feature. Then use that template in your program (load it instead of a new, empty document).

And some questions:
Why you want to do it?
What about the normal Save function and the simple "copy + rename" feature of the operating system? Everybody can achieve a virtual "Save as..." by these functions... And everybody can switch ON the invisible functions...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply