summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/common/SystemDialog.py
blob: a04c0272c96b308114eb2bdaf35feb71ce47ae75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import uno
import traceback
from Configuration import Configuration

from Desktop import Desktop
from Helper import Helper

from com.sun.star.ui.dialogs.TemplateDescription import FILESAVE_AUTOEXTENSION, FILEOPEN_SIMPLE
from com.sun.star.ui.dialogs.ExtendedFilePickerElementIds import CHECKBOX_AUTOEXTENSION
from com.sun.star.awt import WindowDescriptor
from com.sun.star.awt.WindowClass import MODALTOP
from com.sun.star.lang import IllegalArgumentException
from com.sun.star.awt.VclWindowPeerAttribute import OK

class SystemDialog(object):

    '''
    @param xMSF
    @param ServiceName
    @param type  according to com.sun.star.ui.dialogs.TemplateDescription
    '''

    def __init__(self, xMSF, ServiceName, Type):
        try:
            self.xMSF = xMSF
            self.systemDialog = xMSF.createInstance(ServiceName)
            self.xStringSubstitution = self.createStringSubstitution(xMSF)

        except Exception, exception:
            traceback.print_exc()

    @classmethod
    def createStoreDialog(self, xmsf):
        return SystemDialog(
            xmsf, "com.sun.star.ui.dialogs.FilePicker",FILESAVE_AUTOEXTENSION)

    @classmethod
    def createOpenDialog(self, xmsf):
        return SystemDialog(
            xmsf, "com.sun.star.ui.dialogs.FilePicker", FILEOPEN_SIMPLE)

    @classmethod
    def createFolderDialog(self, xmsf):
        return SystemDialog(
            xmsf, "com.sun.star.ui.dialogs.FolderPicker", 0)

    @classmethod
    def createOfficeFolderDialog(self, xmsf):
        return SystemDialog(
            xmsf, "com.sun.star.ui.dialogs.OfficeFolderPicker", 0)

    def subst(self, path):
        try:
            s = self.xStringSubstitution.substituteVariables(path, False)
            return s
        except Exception, ex:
            traceback.print_exc()
            return path

    '''
    @param displayDir
    @param defaultName
    given url to a local path.
    @return
    '''

    def callStoreDialog(self, displayDir, defaultName, sDocuType=None):
        if sDocuType is not None:
            self.addFilterToDialog(defaultName[-3:], sDocuType, True)

        self.sStorePath = None
        try:
            self.systemDialog.setValue(CHECKBOX_AUTOEXTENSION, 0, True)
            self.systemDialog.setDefaultName(defaultName)
            self.systemDialog.setDisplayDirectory(self.subst(displayDir))
            if self.execute(self.systemDialog):
                sPathList = self.systemDialog.getFiles()
                self.sStorePath = sPathList[0]

        except Exception, exception:
            traceback.print_exc()

        return self.sStorePath

    def callFolderDialog(self, title, description, displayDir):
        try:
            self.systemDialog.setDisplayDirectoryxPropertyValue(
                subst(displayDir))
        except IllegalArgumentException, iae:
            traceback.print_exc()
            raise AttributeError(iae.getMessage());

        self.systemDialog.setTitle(title)
        self.systemDialog.setDescription(description)
        if self.execute(self.systemDialog):
            return self.systemDialog.getDirectory()
        else:
            return None

    def execute(self, execDialog):
        return execDialog.execute() == 1

    def callOpenDialog(self, multiSelect, displayDirectory):
        try:
            self.systemDialog.setMultiSelectionMode(multiSelect)
            self.systemDialog.setDisplayDirectory(self.subst(displayDirectory))
            if self.execute(self.systemDialog):
                return self.systemDialog.getFiles()

        except Exception, exception:
            traceback.print_exc()

        return None

    def addFilterToDialog(self, sExtension, filterName, setToDefault):
        try:
            #get the localized filtername
            uiName = self.getFilterUIName(filterName)
            pattern = "*." + sExtension
            #add the filter
            self.addFilter(uiName, pattern, setToDefault)
        except Exception, exception:
            traceback.print_exc()

    def addFilter(self, uiName, pattern, setToDefault):
        try:
            self.systemDialog.appendFilter(uiName, pattern)
            if setToDefault:
                self.systemDialog.setCurrentFilter(uiName)

        except Exception, ex:
            traceback.print_exc()

    '''
    converts the name returned from getFilterUIName_(...) so the
    product name is correct.
    @param filterName
    @return
    '''

    def getFilterUIName(self, filterName):
        prodName = Configuration.getProductName(self.xMSF)
        s = [[self.getFilterUIName_(filterName)]]
        s[0][0] = s[0][0].replace("%productname%", prodName)
        return s[0][0]

    '''
    note the result should go through conversion of the product name.
    @param filterName
    @return the UI localized name of the given filter name.
    '''

    def getFilterUIName_(self, filterName):
        try:
            oFactory = self.xMSF.createInstance(
                "com.sun.star.document.FilterFactory")
            oObject = Helper.getUnoObjectbyName(oFactory, filterName)
            xPropertyValue = list(oObject)
            for i in xPropertyValue:
                if i is not None and i.Name == "UIName":
                    return str(i.Value)

            raise NullPointerException(
                "UIName property not found for Filter " + filterName);
        except Exception, exception:
            traceback.print_exc()
            return None

    @classmethod
    def showErrorBox(self, xMSF, ResName, ResPrefix,
            ResID, AddTag=None, AddString=None):
        ProductName = Configuration.getProductName(xMSF)
        oResource = Resource(xMSF, ResPrefix)
        sErrorMessage = oResource.getResText(ResID)
        sErrorMessage = sErrorMessage.replace( ProductName, "%PRODUCTNAME")
        sErrorMessage = sErrorMessage.replace(str(13), "<BR>")
        if AddTag and AddString:
            sErrorMessage = sErrorMessage.replace( AddString, AddTag)
        return self.showMessageBox(xMSF, "ErrorBox", OK, sErrorMessage)

    '''
    example:
    (xMSF, "ErrorBox", com.sun.star.awt.VclWindowPeerAttribute.OK, "message")

    @param windowServiceName one of the following strings:
    "ErrorBox", "WarningBox", "MessBox", "InfoBox", "QueryBox".
    There are other values possible, look
    under src/toolkit/source/awt/vcltoolkit.cxx
    @param windowAttribute see com.sun.star.awt.VclWindowPeerAttribute
    @return 0 = cancel, 1 = ok, 2 = yes,  3 = no(I'm not sure here)
    other values check for yourself ;-)
    '''
    @classmethod
    def showMessageBox(self, xMSF, windowServiceName, windowAttribute,
            MessageText, peer=None):

        if MessageText is None:
            return 0

        iMessage = 0
        try:
            # If the peer is null we try to get one from the desktop...
            if peer is None:
                xFrame = Desktop.getActiveFrame(xMSF)
                peer = xFrame.getComponentWindow()

            xToolkit = xMSF.createInstance("com.sun.star.awt.Toolkit")
            oDescriptor = WindowDescriptor()
            oDescriptor.WindowServiceName = windowServiceName
            oDescriptor.Parent = peer
            oDescriptor.Type = MODALTOP
            oDescriptor.WindowAttributes = windowAttribute
            xMsgPeer = xToolkit.createWindow(oDescriptor)
            xMsgPeer.MessageText = MessageText
            iMessage = xMsgPeer.execute()
            xMsgPeer.dispose()
        except Exception:
            traceback.print_exc()

        return iMessage

    @classmethod
    def createStringSubstitution(self, xMSF):
        xPathSubst = None
        try:
            xPathSubst = xMSF.createInstance(
                "com.sun.star.util.PathSubstitution")
            return xPathSubst
        except Exception, e:
            traceback.print_exc()
            return None