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 traceback
19from ..common.PropertyNames import PropertyNames
20from ..common.FileAccess import FileAccess
21from ..common.SystemDialog import SystemDialog
22
23class PathSelection(object):
24
25    class DialogTypes(object):
26        FOLDER = 0
27        FILE = 1
28
29    class TransferMode(object):
30        SAVE = 0
31        LOAD = 1
32
33    def __init__(self, xMSF, CurUnoDialog, TransferMode, DialogType):
34        self.CurUnoDialog = CurUnoDialog
35        self.xMSF = xMSF
36        self.iDialogType = DialogType
37        self.iTransferMode = TransferMode
38        self.sDefaultDirectory = ""
39        self.sDefaultName = ""
40        self.sDefaultFilter = ""
41        self.usedPathPicker = False
42        self.CMDSELECTPATH = 1
43        self.TXTSAVEPATH = 1
44
45    def insert(
46        self, DialogStep, XPos, YPos, Width,
47        CurTabIndex, LabelText, Enabled, TxtHelpURL, BtnHelpURL):
48
49        self.CurUnoDialog.insertControlModel(
50            "com.sun.star.awt.UnoControlFixedTextModel", "lblSaveAs",
51            (PropertyNames.PROPERTY_ENABLED,
52                PropertyNames.PROPERTY_HEIGHT,
53                PropertyNames.PROPERTY_LABEL,
54                PropertyNames.PROPERTY_POSITION_X,
55                PropertyNames.PROPERTY_POSITION_Y,
56                PropertyNames.PROPERTY_STEP,
57                PropertyNames.PROPERTY_TABINDEX,
58                PropertyNames.PROPERTY_WIDTH),
59            (Enabled, 8, LabelText, XPos, YPos, DialogStep,
60                CurTabIndex, Width))
61        self.xSaveTextBox = self.CurUnoDialog.insertTextField(
62            "txtSavePath", "callXPathSelectionListener",
63            (PropertyNames.PROPERTY_ENABLED,
64                PropertyNames.PROPERTY_HEIGHT,
65                PropertyNames.PROPERTY_HELPURL,
66                PropertyNames.PROPERTY_POSITION_X,
67                PropertyNames.PROPERTY_POSITION_Y,
68                PropertyNames.PROPERTY_STEP,
69                PropertyNames.PROPERTY_TABINDEX,
70                PropertyNames.PROPERTY_WIDTH),
71            (Enabled, 12, TxtHelpURL, XPos, YPos + 10, DialogStep,
72                (CurTabIndex + 1), Width - 26), self)
73
74        self.CurUnoDialog.xDialogModel.txtSavePath.Enabled = False
75        self.CurUnoDialog.insertButton("cmdSelectPath", "triggerPathPicker",
76            (PropertyNames.PROPERTY_ENABLED,
77                PropertyNames.PROPERTY_HEIGHT,
78                PropertyNames.PROPERTY_HELPURL,
79                PropertyNames.PROPERTY_LABEL,
80                PropertyNames.PROPERTY_POSITION_X,
81                PropertyNames.PROPERTY_POSITION_Y,
82                PropertyNames.PROPERTY_STEP,
83                PropertyNames.PROPERTY_TABINDEX,
84                PropertyNames.PROPERTY_WIDTH),
85            (Enabled, 14, BtnHelpURL, "...",XPos + Width - 16, YPos + 9,
86                DialogStep, (CurTabIndex + 2), 16), self)
87
88    def addSelectionListener(self, xAction):
89        self.xAction = xAction
90
91    def getSelectedPath(self):
92        return self.xSaveTextBox.Text
93
94    def initializePath(self):
95        try:
96            myFA = FileAccess(self.xMSF)
97            self.xSaveTextBox.setText(
98                myFA.getPath(self.sDefaultDirectory + \
99                    "/" + \
100                    self.sDefaultName, None))
101        except Exception:
102            traceback.print_exc()
103
104    def triggerPathPicker(self):
105        try:
106            if self.iTransferMode == self.TransferMode.SAVE:
107                if self.iDialogType == self.DialogTypes.FOLDER:
108                    #TODO: write code for picking a folder for saving
109                    return
110                elif self.iDialogType == self.DialogTypes.FILE:
111                    self.usedPathPicker = True
112                    myFilePickerDialog = \
113                        SystemDialog.createStoreDialog(self.xMSF)
114                    myFilePickerDialog.callStoreDialog(
115                        self.sDefaultDirectory,
116                        self.sDefaultName, self.sDefaultFilter)
117                    sStorePath = myFilePickerDialog.sStorePath
118                    if sStorePath is not None:
119                        myFA = FileAccess(self.xMSF)
120                        self.xSaveTextBox.Text = myFA.getPath(sStorePath, None)
121                        self.sDefaultDirectory = \
122                            FileAccess.getParentDir(sStorePath)
123                        self.sDefaultName = myFA.getFilename(sStorePath)
124                    return
125            elif iTransferMode == TransferMode.LOAD:
126                if iDialogType == DialogTypes.FOLDER:
127                    #TODO: write code for picking a folder for loading
128                    return
129                elif iDialogType == DialogTypes.FILE:
130                    #TODO: write code for picking a file for loading
131                    return
132        except Exception:
133            traceback.print_exc()
134
135    def callXPathSelectionListener(self):
136            if self.xAction is not None:
137                self.xAction.validatePath()
138