You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openoffice.apache.org by Jürgen Schmidt <jo...@gmail.com> on 2013/01/09 15:40:12 UTC

Re: MIME type of current document

On 12/19/12 3:04 PM, Robert Barbey wrote:
> Hi everyone,
> 
> is it possible to get the kind of document currently opened in Writer via the API? Ideally, this would be a MIME type kind of thing. 
> 

sorry for answering so late but I forget it and stumbled over this now
again. But the bad news is that I don't know it for sure but it seems
that it not possible.

Juergen

Re: MIME type of current document

Posted by Ariel Constenla-Haile <ar...@apache.org>.
On Wed, Jan 09, 2013 at 05:10:25PM +0100, Jürgen Schmidt wrote:
> thanks Ariel, I was looking for DociumentProperties and the deprecated
> DocumentInfo. I've thought about the MediaDescriptor as well but of
> course did not looked in the code in detail but only the API.
> 
> getArgs() a self explaining method for accessing these media
> properties/args , why did I not noticed this ;-)

There is a bug to unify interfaces of the kind XModel and XModel2; while
at it, this method could be change to an interface attribute, and name
it MediaDescriptor, reflecting what it really is.

> Why are these things so complicate? 

I guess this was a rhetorical question ;) It's AOO API.

> I would have expected that all documents have a media type (mime type)
> by default. New documents would get the mime type of the used default
> format as long as they are not saved in a different format.
> 
> Going via the TypeDetection looks not really straight forward. 

See my previous mail correcting the macro; it's even more complicated:
you have to go through the FilterFactory and then through the
TypeDetection.

> The question come into my mind why DocumentInfo is deprecated where
> a MIMEType property is defined?

http://www.mail-archive.com/interface-announce@openoffice.org/msg00432.html

The MIME type is not part of the office:meta, it makes no sense to
include this in the DocumentProperties


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina

Re: MIME type of current document

Posted by Jürgen Schmidt <jo...@gmail.com>.
On 1/9/13 3:59 PM, Ariel Constenla-Haile wrote:
> Hi *
> 
> On Wed, Jan 9, 2013 at 11:40 AM, Jürgen Schmidt <jo...@gmail.com> wrote:
>> On 12/19/12 3:04 PM, Robert Barbey wrote:
>>> Hi everyone,
>>>
>>> is it possible to get the kind of document currently opened in Writer via the API? Ideally, this would be a MIME type kind of thing.
>>>
>>
>> sorry for answering so late but I forget it and stumbled over this now
>> again. But the bad news is that I don't know it for sure but it seems
>> that it not possible.
> 
> The model has a getArgs() method
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html
> 
> that returns a MediaDescriptor, look for "FilterName" in that array
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html#FilterName
> 
> then use the TypeDetection to get the filter information by name, and
> search for "MediaType"
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html
> 
> Sample Basic code:
> 
> REM  *****  BASIC  *****
> Option Explicit
> 
> Sub Main
> 	Dim oDoc as Object
> 	oDoc = ThisComponent
> 	
> 	Dim aArgs()
> 	aArgs = oDoc.getArgs()
> 	
> 	Dim sFilterName$
> 	Dim i%
> 	For i = 0 To UBound(aArgs)
> 		If aArgs(i).Name = "FilterName" Then
> 			sFilterName = aArgs(i).Value
> 			Exit For
> 		End If
> 	Next
> 	
> 	Dim sMime$
> 	Dim oTypeDetection as Object
> 	oTypeDetection = CreateUnoService("com.sun.star.document.TypeDetection")
> 	If oTypeDetection.hasByName(sFilterName) Then
> 		Dim aFilter()
> 		aFilter = oTypeDetection.getByName(sFilterName)
> 		For i = 0 To UBound(aFilter)
> 			If aFilter(i).Name = "MediaType" Then
> 				sMime = aFilter(i).Value
> 				Exit For
> 			End If
> 		Next
> 	End If
> 	
> 	MsgBox "Mime " & sMime
> End Sub
> 

thanks Ariel, I was looking for DociumentProperties and the deprecated
DocumentInfo. I've thought about the MediaDescriptor as well but of
course did not looked in the code in detail but only the API.

getArgs() a self explaining method for accessing these media
properties/args , why did I not noticed this ;-)

Why are these things so complicate? I would have expected that all
documents have a media type (mime type) by default. New documents would
get the mime type of the used default format as long as they are not
saved in a different format.

Going via the TypeDetection looks not really straight forward. The
question come into my mind why DocumentInfo is deprecated where a
MIMEType property is defined?

Juergen

Re: MIME type of current document

Posted by Ariel Constenla-Haile <ar...@apache.org>.
On Wed, Jan 09, 2013 at 11:59:09AM -0300, Ariel Constenla-Haile wrote:
> Hi *
> 
> On Wed, Jan 9, 2013 at 11:40 AM, Jürgen Schmidt <jo...@gmail.com> wrote:
> > On 12/19/12 3:04 PM, Robert Barbey wrote:
> >> Hi everyone,
> >>
> >> is it possible to get the kind of document currently opened in Writer via the API? Ideally, this would be a MIME type kind of thing.
> >>
> >
> > sorry for answering so late but I forget it and stumbled over this now
> > again. But the bad news is that I don't know it for sure but it seems
> > that it not possible.
> 
> The model has a getArgs() method
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html
> 
> that returns a MediaDescriptor, look for "FilterName" in that array
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html#FilterName

Things are more complicated, that documentation should direct to the
FilterFactory server, not the TypeDetection.


> then use the TypeDetection to get the filter information by name, and
> search for "MediaType"
> http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html

Use the "FilterName" found in getArgs() to get the filter by name in the
FilterFactory service.

From the filter, get the "Type" property, and use it to get the type by
name in the TypeDetection service.

From the type, get the "MediaType" property. The previous version of the
marco didn't work with a .doc, the following should:



REM  *****  BASIC  *****
Option Explicit

Sub Main
    Dim oDoc as Object
    oDoc = ThisComponent

    Dim aArgs()
    aArgs = oDoc.getArgs()

    Dim sFilterName$
    SearchPropArray( aArgs, "FilterName", sFilterName)

    Dim sMime$
    GetMimeFromFilterName( sFilterName, sMime)
End Sub


Sub GetMimeFromFilterName(ByVal sFilterName$, sMime$)
    Dim oTypeDetection as Object
    Dim oFilterFactory as Object
    Dim aFilter(), aType()
    Dim sType$
    Dim i%

    oTypeDetection = CreateUnoService(_
        "com.sun.star.document.TypeDetection")
    oFilterFactory = CreateUnoService(_
        "com.sun.star.document.FilterFactory")

    If NOT oFilterFactory.hasByName(sFilterName) Then
        Exit Sub
    End If

    aFilter = oFilterFactory.getByName(sFilterName)
    SearchPropArray(aFilter, "Type", sType)


    If NOT oTypeDetection.hasByName(sType) Then
        Exit Sub
    End If

    aType = oTypeDetection.getByName(sType)
    SearchPropArray(aType, "MediaType", sMime)

    MsgBox sMime
End Sub


Sub SearchPropArray(aArray, sName$, aValue)
    Dim i%
    For i = 0 To UBound(aArray)
        If aArray(i).Name = sName Then
            aValue = aArray(i).Value
            Exit For
        End If
    Next
End Sub

-- 
Ariel Constenla-Haile
La Plata, Argentina

Re: MIME type of current document

Posted by Ariel Constenla-Haile <ar...@apache.org>.
Hi *

On Wed, Jan 9, 2013 at 11:40 AM, Jürgen Schmidt <jo...@gmail.com> wrote:
> On 12/19/12 3:04 PM, Robert Barbey wrote:
>> Hi everyone,
>>
>> is it possible to get the kind of document currently opened in Writer via the API? Ideally, this would be a MIME type kind of thing.
>>
>
> sorry for answering so late but I forget it and stumbled over this now
> again. But the bad news is that I don't know it for sure but it seems
> that it not possible.

The model has a getArgs() method
http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html

that returns a MediaDescriptor, look for "FilterName" in that array
http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html#FilterName

then use the TypeDetection to get the filter information by name, and
search for "MediaType"
http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/TypeDetection.html

Sample Basic code:

REM  *****  BASIC  *****
Option Explicit

Sub Main
	Dim oDoc as Object
	oDoc = ThisComponent
	
	Dim aArgs()
	aArgs = oDoc.getArgs()
	
	Dim sFilterName$
	Dim i%
	For i = 0 To UBound(aArgs)
		If aArgs(i).Name = "FilterName" Then
			sFilterName = aArgs(i).Value
			Exit For
		End If
	Next
	
	Dim sMime$
	Dim oTypeDetection as Object
	oTypeDetection = CreateUnoService("com.sun.star.document.TypeDetection")
	If oTypeDetection.hasByName(sFilterName) Then
		Dim aFilter()
		aFilter = oTypeDetection.getByName(sFilterName)
		For i = 0 To UBound(aFilter)
			If aFilter(i).Name = "MediaType" Then
				sMime = aFilter(i).Value
				Exit For
			End If
		Next
	End If
	
	MsgBox "Mime " & sMime
End Sub


Regards