OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Clipart Installer Construction Set to preload Gallery

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Apr 01, 2005 8:19 am    Post subject: Clipart Installer Construction Set to preload Gallery Reply with quote

Clipart Installer Construction Set
This message provides you the tools to build your own clipart installer which will load the OOo version 2.0 gallery with clipart.

I plan to release this in the form of an office document that contains the macro and all preloaded clipart from the OpenClipArt.org project. I already have a fully working prototype. The office document is about 12 MB. Therefore, I am not posting the url of where to downlaod the document, because I don't want to kill my server. But I wanted to post the code here before I am ready to submit it to OOoMacros.org in the hope that others could study it.

My prototype document (12 MB, that I am NOT posting here) includes ONLY the PNG files from OpenClipArt.org project. It does NOT include any of the TXT or SVG files (except for LICENSE.TXT and README.TXT).

Advantages of this approach
Here are the advantages I see in using this approach to preload clipart into OOo's gallery.

  • User simply downloads a single office document, opens it, reads instructions, and clicks a button to install.
  • It works on OOo on any platform. It is platform neutral because it is a macro document using Basic macro code.
    (Suppose a new OS were invented two years from now. Later, OOo is ported to run on that OS. This clipart installer will run on OOo on an OS that is not even invented today.)
  • Office power users who are unable to write their own macros or even installer software can use this to package their own clipart installers that work on any platform. All the ingredients you need are:

    • OpenOffice.org 2.0
    • Some clipart that is in a format suitable for the gallery
    • A little bit of knowledge of how OOo documents are really just Zip files
    • A good Zip/Unzip utility



How To Use

  1. Create an office document. (i.e. I create a drawing)
  2. Put the following code into the macro library of the document.
  3. Put a button on the office document that starts the code.
  4. Put lots of descriptive text on the office document

    • Your copyright
      (Please note that the following macro code is LGPL and may only be redistributed in compliance with the LGPL.)
    • What terms you are licensing under
    • Your license information
    • Other copyright and license information regarding the source(s) of any built in clipart.
    • Credits
    • How to use (i.e. click this button and wait while your gallery is loaded with clipart)

  5. Unzip the office document.
  6. Add a folder directly at the root level of the zip file named "OpenClipArt.org". You can use any folder name, but then you'll have to change the macro code.
  7. Rezip the folder back into an office document. It is now larger, because it has the original office document and macros, PLUS, your clipart.


When you create a top level folder in the zip file, i.e. the OpenClipArt.org folder, then every subfolder within this folder is going to be added as a separate Gallery theme. So if the OpenClipArt.org folder contains two folders: "animals", "computer", then you will get two new themes in your OOo Gallery...
OpenClipArt:animals
OpenClipArt:computer
All items within the subfolders, recursively including any sub-sub-folders are added to that gallery theme.

Code:
'**********************************************************************
'   Copyright (c) 2005 Danny Brewer
'   d29583@groovegarden.com
'
'   This library is free software; you can redistribute it and/or
'   modify it under the terms of the GNU Lesser General Public
'   License as published by the Free Software Foundation; either
'   version 2.1 of the License, or (at your option) any later version.
'
'   This library is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'   Lesser General Public License for more details.
'
'   You should have received a copy of the GNU Lesser General Public
'   License along with this library; if not, write to the Free Software
'   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'
'   See:  http://www.gnu.org/licenses/lgpl.html
'
'**********************************************************************
'   If you make changes, please append to the change log below.
'
'   Change Log
'      Danny Brewer         Revised 2005-04-01-01
'
'**********************************************************************

Sub Main
   AddOpenClipartGalleries()
End Sub



Sub AddOpenClipartGalleries()
   
   ' Get access to the gallery.
   oGalleryThemes = createUnoService( "com.sun.star.gallery.GalleryThemeProvider" )
   '
   ' The methods you can then call on oGalleryThemes are...
   '   oGalleryTheme = oGalleryThemes.insertNewByName( cThemeName As String )
   '   oGalleryThemes.removeByName( cThemeName As String )
   '   oGalleryTheme = oGalleryThemes.getByName( cThemeName As String )
   '   aNames = oGalleryThemes.getElementNames() ' gets an array of the theme names
   '   If oGalleryThemes.hasByName( cThemeName As String ) ' test for a theme name
   '
   ' Once you have a gallery theme in oGalleryTheme, from one of the above examples
   '  you can call...
   '   oGalleryTheme.getName() ' get the name of this Gallery theme
   '   oGalleryTheme.update() ' regenerate thumbnails of items in this gallery
   '   nIndex = oGalleryTheme.insertURLByIndex( cUrl As String, nIndex As Long ) ' Add item to gallery
   '   nIndex = oGalleryTheme.insertGraphicByIndex( com.sun.star.graphic.XGraphic, nIndex As Long )
   '   nIndex = oGalleryTheme.insertDrawingByIndex( com.sun.star.lang.XComponent, nIndex As Long )
   '   oGalleryTheme.removeByIndex( nIndex As Long )
   '   nNumItems = oGalleryTheme.getCount()
   '   oGalleryItem = oGalleryTheme.getByIndex( nIndex As Long ) ' get an item from this gallery theme

   
   ' The Zip file is this actual office document.
   ' The stuff we want to add to the gallery must have been manually added
   '  into the zip file of THIS office document.
   cPackageUrl = ThisComponent.getURL()
   ' Use this zip file instead for debugging.
   'cPackageUrl = ConvertToURL( "C:\Documents and Settings\dbrewer\Desktop\Pictures.zip" )
   
   ' Now access the zip file.
   oPackage = createUnoService( "com.sun.star.packages.Package" )
   oPackage.initialize( Array( cPackageUrl ) )
   
   ' This is the name of the top level folder within this macro documet
   '  that contains all of the clipart that we want to load into the gallery.
   cGraphicsFolder = "OpenClipArt.org"
   oGraphicsFolder = oPackage.getByHierarchicalName( cGraphicsFolder )

   ' Create the prefix for a package url.
   ' A package url is a url that completely specifies some file INSIDE of a Zip file.
   ' This forms the portion of the url leading up to the root of the Zip file.
   cPkgPrefixUrl =  "vnd.sun.star.pkg://" + EscapeUrlChars( cPackageUrl ) + "/" + cGraphicsFolder + "/"

            
   ' Get an array of the names of the items in the top level folder.
     aThemeFolderNames = oGraphicsFolder.getElementNames()
     ' Loop for each name.
     For nTheme = 0 To UBound( aThemeFolderNames )
        cThemeFolderName = aThemeFolderNames( nTheme ) ' get name from array
        
        ' Get the object representing the item (a file or folder).
        ' i.e. a com.sun.star.package.PackageFolder or a com.sun.star.package.PackageStream
        ' Only a PackageFolder has the XNameContainer interface.
        oThemeFolderItem = oGraphicsFolder.getByName( cThemeFolderName )
        
        ' Check if the item implements the XServiceInfo interface.
        If HasUnoInterfaces( oThemeFolderItem, "com.sun.star.lang.XServiceInfo" ) Then
           ' Since we know it supports XServiceInfo, we now know it is safe to call supportsService.
           ' Check to see if it is a "folder" item.
           If oThemeFolderItem.supportsService( "com.sun.star.packages.PackageFolder" ) Then
              ' It is a folder.  So create a special theme for it.
              
            ' Get the gallery theme, or create it.
            cThemeName =  "OpenClipArt:" + cThemeFolderName
            If oGalleryThemes.hasByName( cThemeName ) Then
               oGalleryTheme = oGalleryThemes.getByName( cThemeName )
            Else
               oGalleryTheme = oGalleryThemes.insertNewByName( cThemeName )
            EndIf
            
            ' Now add all the items in this folder, recursively, to the theme we just created.
            AddItemsToTheme( oGalleryTheme, oThemeFolderItem, cPkgPrefixUrl+cThemeFolderName+"/" )
           EndIf
        EndIf
     Next
End Sub


Sub AddItemsToTheme( oGalleryTheme, oThemeFolderItem, cPkgPrefixUrl )
   nNumItemsInGalleryTheme = oGalleryTheme.getCount()
   
   ' Now get an array of names of items for this theme.
   aThemeItemNames = oThemeFolderItem.getElementNames()
   ' Loop for each name.
   For nItem = 0 To UBound( aThemeItemNames )
      cGraphicItemName = aThemeItemNames( nItem ) ' get name from array
      
        ' Get the object representing the item (a file or folder).
        ' i.e. a com.sun.star.package.PackageFolder or a com.sun.star.package.PackageStream
        ' Only a PackageFolder has the XNameContainer interface.
      oGraphicItem = oThemeFolderItem.getByName( cGraphicItemName )
        
        ' Check if the item implements the XServiceInfo interface.
        If HasUnoInterfaces( oGraphicItem, "com.sun.star.lang.XServiceInfo" ) Then
           ' Since we know it supports XServiceInfo, we now know it is safe to call supportsService.
           ' Check to see if it is a "folder" item.
           If oGraphicItem.supportsService( "com.sun.star.packages.PackageFolder" ) Then
              ' It is a folder.
              ' Add the folder's items to the theme.
              AddItemsToTheme( oGalleryTheme, oGraphicItem, cPkgPrefixUrl+cGraphicItemName+"/" )
              
              ' Refresh our count of how many items are now in the gallery theme.
              nNumItemsInGalleryTheme = oGalleryTheme.getCount()
           Else
              ' It is a file.
              cTheUrl = cPkgPrefixUrl + cGraphicItemName
              ' Add it to the gallery.
              oGalleryTheme.insertURLByIndex( cTheUrl, nNumItemsInGalleryTheme )
              nNumItemsInGalleryTheme = nNumItemsInGalleryTheme + 1
           EndIf
        EndIf
   Next
End Sub



'############################################################
'   The following ripped off from Danny's Library UtilString module.
'############################################################



' Given a url, escape whatever characters are in the cEscapeSet.
' By default: cEscapeSet is the set of
'  1. space
'  2. forward-slash
' For example, if cUrl were:  http://cnn.com
'  and eEscapeSet were the default, then this would return...
'     http:%2F%2Fcnn.com
'  where the forward slashes were escaped into %2F.
Function EscapeUrlChars( ByVal cUrl As String, Optional cEscapeSet ) As String
   If IsMissing( cEscapeSet ) Then
      cEscapeSet = " /"
   EndIf
   
   For i = 1 To Len( cEscapeSet )
      c = Mid( cEscapeSet, i, 1 )
      cEscaped = "%" & IntValueToHexDigits( Asc(c), 2 )
      
      cUrl = StrSubstitute( cUrl, c, cEscaped )
   Next
   
   EscapeUrlChars() = cUrl
End Function



' The original string, cString, is modified by this function and then returned.
' All occurences of cFindStr are replaced by cReplaceStr.
' cFindStr and cReplaceStr do not need to be the same length.
Function StrSubstitute( ByVal cString As String,_
         ByVal cFindStr As String, ByVal cReplaceStr As String ) As String
   cResult = ""
   If Len( cFindStr ) > 0 Then
      Do
         nPos = Instr( 1, cString, cFindStr, 0 )
         If nPos > 0 Then
            cResult = cResult & Left( cString, nPos-1 )
            cResult = cResult & cReplaceStr
            cString = Mid( cString, nPos+Len(cFindStr) )
         Else
            ' Append the rest of the original string.
            cResult = cResult & cString
         EndIf
      Loop Until nPos = 0
   EndIf
   StrSubstitute() = cResult
End Function



'############################################################
'   Hex utilities.
'   (Ripped off from Danny's Library UtilBasic module.)
'############################################################


'----------
' Pass in a string of Hex digits, and this returns their decimal value.
' Example of how to call...
'
'   Print HexDigitsToIntValue( "FFFF" ), "Expected: 65535"
'   Print HexDigitsToIntValue( "100" ), "Expected: 256"
'   Print HexDigitsToIntValue( "FE" ), "Expected: 254"
'   Print HexDigitsToIntValue( "FF" ), "Expected: 255"
'   Print HexDigitsToIntValue( "7F" ), "Expected: 127"
'
Function HexDigitsToIntValue( ByVal cHexDigits As String ) As Long
   nValue = 0
   nNumHexDigits = Len( cHexDigits )
   For i = 1 To nNumHexDigits
      c = Mid( cHexDigits, i, 1 )
      nValue = (nValue * 16) + _ValueOfHexDigit( c )
   Next
   HexDigitsToIntValue() = nValue
End Function

' Pass in a single hex digit, and this returns it's value from 0-15.
Function _ValueOfHexDigit( ByVal cHexDigit As String ) As Long
   cHexDigit = UCase( cHexDigit )
   nPos = Instr( 1, "0123456789ABCDEF", cHexDigit, 0 )
   If nPos > 0 Then
      nPos = nPos - 1
   EndIf
   _ValueOfHexDigit() = nPos
End Function



'----------
' Pass in an integer value, and this returns a string of hex digits.
' The 2nd optional parameter allows you to specify how wide of a string
'  of hex digits to return.
' Example of how to call...
'
'   Print IntValueToHexDigits( 65535 ), "Expected: FFFF"
'   Print IntValueToHexDigits( 256 ), "Expected: 100"
'   Print IntValueToHexDigits( 256, 6 ), "Expected: 000100"  ' note 2nd parameter!
'   Print IntValueToHexDigits( 254 ), "Expected: FE"
'   Print IntValueToHexDigits( 255 ), "Expected: FF"
'
Function IntValueToHexDigits( ByVal nValue As Long, Optional nHexWidth ) As String
   cHexDigits = ""
   Do
      nOneDigitValue = nValue Mod 16
      nValue = Int( nValue / 16 )
      cHexDigits = _ValueToHexDigit( nOneDigitValue ) + cHexDigits
   Loop While nValue > 0
   
   ' Do we need to left zero fill?
   If Not IsMissing( nHexWidth ) Then
      Do While Len( cHexDigits ) < nHexWidth
         cHexDigits = "0" + cHexDigits
      Loop
   EndIf
   
   IntValueToHexDigits = cHexDigits
End Function

' Pass in a value from 0-15, and out comes an uppercase hex digit.
Function _ValueToHexDigit( ByVal nValue As Long )
   IF nValue < 0 Then
      _ValueToHexDigit() = "0"
   ElseIf nValue < 10 Then
      _ValueToHexDigit() = CHR(ASC("0")+nValue)
   ElseIf nValue < 16 Then
      _ValueToHexDigit() = CHR(ASC("A")+nValue-10)
   Else
      _ValueToHexDigit() = "0"
   EndIf
End Function


'----------
' Pass in a string, and this returns a string twice as long of hex digits.
' Each character in the string is converted into a pair of hex digits.
'
Function StringToHexDigits( ByVal cString As String ) As String
   cResult = ""
   nLen = Len( cString )
   For i = 1 To nLen
      c = Mid( cString, i, 1 )
      cResult = cResult + IntValueToHexDigits( Asc( c ) )
   Next
   StringToHexDigits() = cResult
End Function


' This is the oppoisite of StringToHexDigits().
Function HexDigitsToString( ByVal cHexString As String ) As String
   cResult = ""
   nLen = Int( Len( cHexString ) / 2 )
   For i = 1 To nLen
      cHexPair = Mid( cHexString, (i-1)*2+1, 2 )
      cResult = cResult + Chr( HexDigitsToIntValue( cHexPair ) )
   Next
   HexDigitsToString() = cResult
End Function


TO DO:

  • Release this on OOoMacros.org as a complete working end-user macro document.
  • Add a button and descriptive text onto the front of the document.
  • Add copyright notices, licenses, credits, etc.
  • Add instructions.
  • Add a dialog box that shows the progress of the installation. Since it takes several minutes, be sure that the user is aware of the progress.
  • Be sure to have a "Cancel" button so that the user can cancel during installation. This is important because installation takes several minutes.


When I do submit a finished product to OOoMacros.org, I will post an update to this thread. If you are interested in packaging your own clipart installers, then you will want to download my finished product. It will have a nice dialog box with progress indication, and the ability to cancel during installation.

As always, I know I'll get flamed for having more lines of comments than actual code, so apologies in advance.



Cross references

Early pre-development discussion of this macro
Advanced Clipart 2.0 Installer (using OOo 2.0 macros)
http://www.oooforum.org/forum/viewtopic.phtml?t=18505

Advanced Clipart Galery for OpenOffice
http://www.oooforum.org/forum/viewtopic.phtml?t=18046
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Apr 01, 2005 9:31 am    Post subject: Reply with quote

Again many code lines to study.
Thank you Danny
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Apr 02, 2005 12:14 pm    Post subject: Reply with quote

Over here....
http://www.oooforum.org/forum/viewtopic.phtml?p=73041#73041
Fenix*NBK* wrote:
One problem I already found in the BETA-1 - clipart after some usage dissappears from the OOo 2 galleries.

Maybe we should *copy* the graphics into user's location of OOo, instead of making *pointers* into the zip structure ?
And if we are running as admin / root maybe even copy that to shared folder of OOo...

Ohhh... good catch!

I think I have a fix for that.

See the lines that say....
Code:
            ' Now add all the items in this folder, recursively, to the theme we just created.
            AddItemsToTheme( oGalleryTheme, oThemeFolderItem, cPkgPrefixUrl+cThemeFolderName+"/" )


replace with
Code:
            ' Now add all the items in this folder, recursively, to the theme we just created.
            AddItemsToTheme( oGalleryTheme, oThemeFolderItem, cPkgPrefixUrl+cThemeFolderName+"/" )
            
            oGalleryTheme.update()

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group