1#
2# This file is part of the LibreOffice project.
3#
4# This Source Code Form is subject to the terms of the Mozilla Public
5# License, v. 2.0. If a copy of the MPL was not distributed with this
6# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7#
8# This file incorporates work covered by the following license notice:
9#
10#   Licensed to the Apache Software Foundation (ASF) under one or more
11#   contributor license agreements. See the NOTICE file distributed
12#   with this work for additional information regarding copyright
13#   ownership. The ASF licenses this file to you under the Apache
14#   License, Version 2.0 (the "License"); you may not use this file
15#   except in compliance with the License. You may obtain a copy of
16#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17#
18import uno
19import traceback
20from unohelper import systemPathToFileUrl, absolutize
21from ..common.Desktop import Desktop
22from ..common.SystemDialog import SystemDialog
23
24from com.sun.star.awt import WindowDescriptor
25from com.sun.star.awt import Rectangle
26from com.sun.star.awt.WindowClass import TOP
27from com.sun.star.task import ErrorCodeIOException
28
29#Window Constants
30com_sun_star_awt_WindowAttribute_BORDER \
31    = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.BORDER" )
32com_sun_star_awt_WindowAttribute_SIZEABLE \
33    = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.SIZEABLE" )
34com_sun_star_awt_WindowAttribute_MOVEABLE \
35    = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.MOVEABLE" )
36com_sun_star_awt_VclWindowPeerAttribute_CLIPCHILDREN \
37    = uno.getConstantByName(
38        "com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN" )
39
40class OfficeDocument(object):
41    '''Creates a new instance of OfficeDocument '''
42
43    def __init__(self, _xMSF):
44        self.xMSF = _xMSF
45
46    @classmethod
47    def attachEventCall(self, xComponent, EventName, EventType, EventURL):
48        try:
49            oEventProperties = list(range(2))
50            oEventProperties[0] = uno.createUnoStruct(
51                'com.sun.star.beans.PropertyValue')
52            oEventProperties[0].Name = "EventType"
53            oEventProperties[0].Value = EventType
54            # "Service", "StarBasic"
55            oEventProperties[1] = uno.createUnoStruct(
56                'com.sun.star.beans.PropertyValue')
57            oEventProperties[1].Name = "Script" #"URL";
58            oEventProperties[1].Value = EventURL
59            uno.invoke(xComponent.Events, "replaceByName",
60                (EventName, uno.Any("[]com.sun.star.beans.PropertyValue",
61                    tuple(oEventProperties))))
62        except Exception:
63            traceback.print_exc()
64
65    def dispose(self, xMSF, xComponent):
66        try:
67            if xComponent is not None:
68                xFrame = xComponent.CurrentController.Frame
69                if xComponent.isModified():
70                    xComponent.setModified(False)
71
72                Desktop.dispatchURL(xMSF, ".uno:CloseDoc", xFrame)
73
74        except Exception:
75            traceback.print_exc()
76
77    @classmethod
78    def createNewFrame(self, xMSF, listener, FrameName="_blank"):
79        xFrame = None
80        if FrameName.lower() == "WIZARD_LIVE_PREVIEW".lower():
81            xFrame = self.createNewPreviewFrame(xMSF, listener)
82        else:
83            xF = Desktop.getDesktop(xMSF)
84            xFrame = xF.findFrame(FrameName, 0)
85            if listener is not None:
86                xFF = xF.getFrames()
87                xFF.remove(xFrame)
88                xF.addTerminateListener(listener)
89
90        return xFrame
91
92    @classmethod
93    def createNewPreviewFrame(self, xMSF, listener):
94        xToolkit = None
95        try:
96            xToolkit = xMSF.createInstance("com.sun.star.awt.Toolkit")
97        except Exception:
98            # TODO Auto-generated catch block
99            traceback.print_exc()
100
101        #describe the window and its properties
102        aDescriptor = WindowDescriptor()
103        aDescriptor.Type = TOP
104        aDescriptor.WindowServiceName = "window"
105        aDescriptor.ParentIndex = -1
106        aDescriptor.Parent = None
107        aDescriptor.Bounds = Rectangle(10, 10, 640, 480)
108
109        #Set Window Attributes
110        gnDefaultWindowAttributes = \
111            com_sun_star_awt_WindowAttribute_BORDER + \
112            com_sun_star_awt_WindowAttribute_MOVEABLE + \
113            com_sun_star_awt_WindowAttribute_SIZEABLE + \
114            com_sun_star_awt_VclWindowPeerAttribute_CLIPCHILDREN
115
116        aDescriptor.WindowAttributes = gnDefaultWindowAttributes
117        #create a new blank container window
118        xPeer = None
119        try:
120            xPeer = xToolkit.createWindow(aDescriptor)
121        except Exception:
122            traceback.print_exc()
123
124        #define some further properties of the frame window
125        #if it's needed .-)
126        #xPeer->setBackground(...);
127        #create new empty frame and set window on it
128        xFrame = None
129        try:
130            xFrame = xMSF.createInstance("com.sun.star.frame.Frame")
131        except Exception:
132            traceback.print_exc()
133
134        xFrame.initialize(xPeer)
135        #from now this frame is usable ...
136        #and not part of the desktop tree.
137        #You are alone with him .-)
138        if listener is not None:
139            Desktop.getDesktop(xMSF).addTerminateListener(listener)
140
141        return xFrame
142
143    @classmethod
144    def load(self, xInterface, sURL, sFrame, xValues):
145        xComponent = None
146        try:
147            if not sURL.startswith("file://"):
148                sURL = systemPathToFileUrl(sURL)
149            xComponent = xInterface.loadComponentFromURL(
150                sURL, sFrame, 0, tuple(xValues))
151        except Exception:
152            traceback.print_exc()
153
154        return xComponent
155
156    @classmethod
157    def store(self, xMSF, xComponent, StorePath, FilterName):
158        try:
159            if len(FilterName):
160                oStoreProperties = list(range(2))
161                oStoreProperties[0] = uno.createUnoStruct(
162                    'com.sun.star.beans.PropertyValue')
163                oStoreProperties[0].Name = "FilterName"
164                oStoreProperties[0].Value = FilterName
165                oStoreProperties[1] = uno.createUnoStruct(
166                    'com.sun.star.beans.PropertyValue')
167                oStoreProperties[1].Name = "InteractionHandler"
168                oStoreProperties[1].Value = xMSF.createInstance(
169                    "com.sun.star.comp.uui.UUIInteractionHandler")
170            else:
171                oStoreProperties = list(range(0))
172
173            StorePath = systemPathToFileUrl(StorePath)
174            sPath = StorePath[:(StorePath.rfind("/") + 1)]
175            sFile = StorePath[(StorePath.rfind("/") + 1):]
176            xComponent.storeToURL(
177                absolutize(sPath, sFile), tuple(oStoreProperties))
178            return True
179        except ErrorCodeIOException:
180            #Throw this exception when trying to save a file
181            #which is already opened in Libreoffice
182            #TODO: handle it properly
183            return True
184            pass
185        except Exception:
186            traceback.print_exc()
187            return False
188
189    def close(self, xComponent):
190        bState = False
191        if xComponent is not None:
192            try:
193                xComponent.close(True)
194                bState = True
195            except Exception:
196                print ("could not close doc")
197                bState = False
198
199        else:
200            bState = True
201
202        return bState
203
204    def showMessageBox(
205        self, xMSF, windowServiceName, windowAttribute, MessageText):
206
207        return SystemDialog.showMessageBox(
208            xMSF, windowServiceName, windowAttribute, MessageText)
209