Page 1 of 1

Cleanup recent files

PostPosted: Mon Jun 23, 2008 7:12 pm
by hanya
This code removes non-existing files from the history list. The list of the history is stored in the configuration node of /org.openoffice.Office.Common/History. This code also does not affect at the runtime like the changing the size of the history. So, you need to shutdown the OpenOffice.org completely after the code execution. Because the PopupMenuController implementation of the Recent Files menu keeps the list of the history and they would be back if you choose the File menu (not only the File menu but any menu including the Recent Files menu entry).

Code: Select all   Expand viewCollapse view
Sub CleanUpHistory()
Dim oCP As Object
Dim oCUA As Object
Dim aProps(0) As New com.sun.star.beans.PropertyValue

' get configuration of the file history
  oCP = GetProcessServiceManager().createInstanceWithContext( _
    "com.sun.star.configuration.ConfigurationProvider", GetDefaultContext() )
  aProps(0).Name = "nodepath"
  aProps(0).Value = "/org.openoffice.Office.Common/History"
  oCUA = oCP.createInstanceWithArguments( _
    "com.sun.star.configuration.ConfigurationUpdateAccess", aProps )
 
  ' clean up PickList
  CleanUpList( "PickList", "PickListSize", "p", oCUA )
 
  ' clean up List
  CleanUpList( "List", "Size", "h", oCUA )
 
  oCUA.commitChanges()
End Sub

' sListName: PickList or List
' sSizeName: PickListSize or Size
'   sPrefix: p or h
Sub CleanUpList( sListName As String, sSizeName As String, _
    sPrefix As String, oAccess As Object)
 
  Dim oList As Object
  Dim oItem As Object
  Dim nSize As Long
  Dim sName As String
  Dim nCounter As Integer
 
  oList = oAccess.getPropertyValue( sListName )
  nSize = oAccess.getPropertyValue( sSizeName )
 
  nCounter = 0 'existing items
 
  For i = 0 To nSize - 1 step 1
    sName = sPrefix & CStr(i)
    If oList.hasByName( sName ) Then
      oItem = oList.getByName( sName )
      If FileExists( oItem.URL ) Then
       
        CreateAsNewItem( oList, oItem, sPrefix & CStr(nCounter) )
       
        If nCounter <> i Then
          oList.removeByName( sName )
        End If
        nCounter = nCounter + 1
      Else
        oList.removeByName( sName )
      End If
    End If
  Next
End Sub


' make new item and insert/replace it in the sName position
Sub CreateAsNewItem( oList As Object, oItem As Object, sName As String )
  oNewItem = oList.createInstance()
  With oNewItem
    .URL = oItem.URL
    .Filter = oItem.Filter
    .Title = oItem.Title
    .Password = oItem.Password
  End With
  If oList.hasByName( sName ) Then
    oList.replaceByName( sName, oNewItem )
  Else
    oList.insertByName( sName, oNewItem )
  End If
End Sub

Re: Cleanup recent files

PostPosted: Tue Jun 24, 2008 4:55 pm
by hanya
Dummy post to remove from the unanswered list.