summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/text/TextDocument.py
blob: 93351c6c02f37ef639096fd0afc28e184647503a (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import uno
from common.Desktop import Desktop
from com.sun.star.view.DocumentZoomType import ENTIRE_PAGE
from com.sun.star.beans.PropertyState import DIRECT_VALUE
from common.Helper import Helper
from document.OfficeDocument import OfficeDocument
import traceback
from text.ViewHandler import ViewHandler
from text.TextFieldHandler import TextFieldHandler
from com.sun.star.container import NoSuchElementException
from com.sun.star.lang import WrappedTargetException
from common.Configuration import Configuration
import time
from com.sun.star.util import DateTime

class TextDocument(object):

    def __init__(self, xMSF,listener=None,bShowStatusIndicator=None,
        FrameName=None,_sPreviewURL=None,_moduleIdentifier=None,
        _textDocument=None, xArgs=None):

        self.xMSF = xMSF
        self.xTextDocument = None

        if listener is not None:
            if FrameName is not None:
                '''creates an instance of TextDocument and creates a named frame.
                No document is actually loaded into this frame.'''
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener, FrameName);
                return

            elif _sPreviewURL is not None:
                '''creates an instance of TextDocument by loading a given URL as preview'''
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
                self.xTextDocument = self.loadAsPreview(_sPreviewURL, True)

            elif xArgs is not None:
                '''creates an instance of TextDocument and creates a frame and loads a document'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener);
                self.xTextDocument = OfficeDocument.load(xFrame, URL, "_self", xArgs);
                self.xWindowPeer = xFrame.getComponentWindow()
                self.m_xDocProps = self.xTextDocument.getDocumentProperties();
                CharLocale = Helper.getUnoStructValue( self.xTextDocument, "CharLocale");
                return

            else:
                '''creates an instance of TextDocument from the desktop's current frame'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = self.xDesktop.getActiveFrame()
                self.xTextDocument = self.xFrame.getController().getModel()

        elif _moduleIdentifier is not None:
            try:
                '''create the empty document, and set its module identifier'''
                self.xTextDocument = xMSF.createInstance("com.sun.star.text.TextDocument")
                self.xTextDocument.initNew()
                self.xTextDocument.setIdentifier(_moduleIdentifier.getIdentifier())
                # load the document into a blank frame
                xDesktop = Desktop.getDesktop(xMSF)
                loadArgs = range(1)
                loadArgs[0] = "Model"
                loadArgs[0] = -1
                loadArgs[0] = self.xTextDocument
                loadArgs[0] = DIRECT_VALUE
                xDesktop.loadComponentFromURL("private:object", "_blank", 0, loadArgs)
                # remember some things for later usage
                self.xFrame = self.xTextDocument.getCurrentController().getFrame()
            except Exception, e:
                traceback.print_exc()

        elif _textDocument is not None:
            '''creates an instance of TextDocument from a given XTextDocument'''
            self.xFrame = _textDocument.getCurrentController().getFrame()
            self.xTextDocument = _textDocument
        if bShowStatusIndicator:
            self.showStatusIndicator()
        self.init()

    def init(self):
        self.xWindowPeer = self.xFrame.getComponentWindow()
        self.m_xDocProps = self.xTextDocument.getDocumentProperties()
        self.CharLocale = Helper.getUnoStructValue(self.xTextDocument, "CharLocale")
        self.xText = self.xTextDocument.getText()

    def showStatusIndicator(self):
        self.xProgressBar = self.xFrame.createStatusIndicator()
        self.xProgressBar.start("", 100)
        self.xProgressBar.setValue(5)

    def loadAsPreview(self, sDefaultTemplate, asTemplate):
        loadValues = range(3)
        #      open document in the Preview mode
        loadValues[0] = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
        loadValues[0].Name = "ReadOnly"
        loadValues[0].Value = True
        loadValues[1] = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
        loadValues[1].Name = "AsTemplate"
        if asTemplate:
            loadValues[1].Value = True
        else:
            loadValues[1].Value = False

        loadValues[2] = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
        loadValues[2].Name = "Preview"
        loadValues[2].Value = True
        #set the preview document to non-modified mode in order to avoid the 'do u want to save' box
        if self.xTextDocument is not None:
            try:
                self.xTextDocument.setModified(False)
            except PropertyVetoException, e1:
                traceback.print_exc()
        self.xTextDocument = OfficeDocument.load(self.xFrame, sDefaultTemplate, "_self", loadValues)
        self.DocSize = self.getPageSize()
        myViewHandler = ViewHandler(self.xTextDocument, self.xTextDocument)
        try:
            myViewHandler.setViewSetting("ZoomType", uno.Any("short",ENTIRE_PAGE))
        except Exception, e:
            traceback.print_exc()
        myFieldHandler = TextFieldHandler(self.xMSF, self.xTextDocument)
        myFieldHandler.updateDocInfoFields()
        return self.xTextDocument

    def getPageSize(self):
        try:
            xNameAccess = self.xTextDocument.getStyleFamilies()
            xPageStyleCollection = xNameAccess.getByName("PageStyles")
            xPageStyle = xPageStyleCollection.getByName("First Page")
            return Helper.getUnoPropertyValue(xPageStyle, "Size")
        except Exception, exception:
            traceback.print_exc()
            return None

    #creates an instance of TextDocument and creates a frame and loads a document

    def createTextCursor(self, oCursorContainer):
        xTextCursor = oCursorContainer.createTextCursor()
        return xTextCursor

    # Todo: This method is  unsecure because the last index is not necessarily the last section
    # Todo: This Routine should be  modified, because I cannot rely on the last Table in the document to be the last in the TextTables sequence
    # to make it really safe you must acquire the Tablenames before the insertion and after the insertion of the new Table. By comparing the
    # two sequences of tablenames you can find out the tablename of the last inserted Table
    # Todo: This method is  unsecure because the last index is not necessarily the last section

    def getCharWidth(self, ScaleString):
        iScale = 200
        self.xTextDocument.lockControllers()
        iScaleLen = ScaleString.length()
        xTextCursor = createTextCursor(self.xTextDocument.getText())
        xTextCursor.gotoStart(False)
        com.sun.star.wizards.common.Helper.setUnoPropertyValue(xTextCursor, "PageDescName", "First Page")
        xTextCursor.setString(ScaleString)
        xViewCursor = self.xTextDocument.getCurrentController()
        xTextViewCursor = xViewCursor.getViewCursor()
        xTextViewCursor.gotoStart(False)
        iFirstPos = xTextViewCursor.getPosition().X
        xTextViewCursor.gotoEnd(False)
        iLastPos = xTextViewCursor.getPosition().X
        iScale = (iLastPos - iFirstPos) / iScaleLen
        xTextCursor.gotoStart(False)
        xTextCursor.gotoEnd(True)
        xTextCursor.setString("")
        unlockallControllers()
        return iScale

    def unlockallControllers(self):
        while self.xTextDocument.hasControllersLocked() == True:
            self.xTextDocument.unlockControllers()

    def refresh(self):
        self.xTextDocument.refresh()

    '''
    This method sets the Author of a Wizard-generated template correctly
    and adds a explanatory sentence to the template description.
    @param WizardName The name of the Wizard.
    @param TemplateDescription The old Description which is being appended with another sentence.
    @return void.
    '''

    def setWizardTemplateDocInfo(self, WizardName, TemplateDescription):
        try:
            xNA = Configuration.getConfigurationRoot(self.xMSF, "/org.openoffice.UserProfile/Data", False)
            gn = xNA.getByName("givenname")
            sn = xNA.getByName("sn")
            fullname = str(gn) + " " + str(sn)
            currentDate = DateTime()
            now = time.localtime(time.time())
            currentDate.Day = time.strftime("%d", now)
            currentDate.Year = time.strftime("%Y", now)
            currentDate.Month = time.strftime("%m", now)
            du = Helper.DateUtils(self.xMSF, self.xTextDocument)
            ff = du.getFormat(NumberFormatIndex.DATE_SYS_DDMMYY)
            myDate = du.format(ff, currentDate)
            xDocProps2 = self.xTextDocument.getDocumentProperties()
            xDocProps2.setAuthor(fullname)
            xDocProps2.setModifiedBy(fullname)
            description = xDocProps2.getDescription()
            description = description + " " + TemplateDescription
            description = JavaTools.replaceSubString(description, WizardName, "<wizard_name>")
            description = JavaTools.replaceSubString(description, myDate, "<current_date>")
            xDocProps2.setDescription(description)
        except NoSuchElementException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except WrappedTargetException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except Exception, e:
            # TODO Auto-generated catch block
            traceback.print_exc()

    '''
    removes an arbitrary Object which supports the  'XTextContent' interface
    @param oTextContent
    @return
    '''

    def removeTextContent(self, oTextContent):
        try:
            self.xText.removeTextContent(oxTextContent)
            return True
        except NoSuchElementException, e:
            traceback.print_exc()
            return False

    '''
    Apparently there is no other way to get the
    page count of a text document other than using a cursor and
    making it jump to the last page...
    @param model the document model.
    @return the page count of the document.
    '''

    def getPageCount(self, model):
        xController = model.getCurrentController()
        xPC = xController.getViewCursor()
        xPC.jumpToLastPage()
        return xPC.getPage()

    '''
    Possible Values for "OptionString" are: "LoadCellStyles", "LoadTextStyles", "LoadFrameStyles",
    "LoadPageStyles", "LoadNumberingStyles", "OverwriteStyles"
    '''