Purebasic Preferences Cleaner (All OS)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Purebasic Preferences Cleaner (All OS)

Post by mk-soft »

Since network folders can cause problems when starting Purebasic IDE (timeout).
Here now a cleaner for the purebasic.prefs to remove references to network files.
The Purebasic IDE must be closed.

Update v1.01.3

Code: Select all

;-TOP

#ProgramTitle = "Purebasic Preferences Cleaner (No warranty)"
#ProgramCopyright = "©2022 by mk-soft (License MIT)"
#ProgramVersion = "v1.01.3"
#ProgramAbout = #ProgramTitle + #LF$ + #LF$ + #ProgramCopyright + #LF$ + #ProgramVersion

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainList
  #MainButtonRead
  #MainButtonCleanup
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

; ----

Global IsRead
Global ExplorerPath.s
Global HistorySize
Global ProjectSize
Global OpenedSize
Global OpenedProject.s

Global NewList RecentFiles.s()
Global NewList RecentProject.s()
Global NewList OpenedFiles.s()

; ----

Procedure AddInfo(Text.s)
  AddGadgetItem(#MainList, -1, Text)
EndProcedure

; ----

Procedure.s GetPreferencesPath()
  Protected path.s
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      path = GetHomeDirectory()
      path + ".purebasic/"
  
    CompilerCase #PB_OS_MacOS
      path = GetHomeDirectory()
      path + ".purebasic/"
      
    CompilerCase #PB_OS_Windows
      path = GetUserDirectory(#PB_Directory_ProgramData)
      path + "purebasic/"
      
  CompilerEndSelect
  
  ProcedureReturn path
    
EndProcedure

; ----

Procedure IsNetPath(File.s)
  Protected r1, path.s
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      path = "/Volumes/"
      If Left(File, 9) = path
        r1 = #True
      EndIf
    CompilerCase #PB_OS_Windows
      path = "\\"
      If Left(File, 2) = path
        r1 = #True
      EndIf
    CompilerCase #PB_OS_Linux
      path = "smb-share:"
      If FindString(File, path, 1, #PB_String_NoCase)
        r1 = #True
      EndIf
  CompilerEndSelect
  ProcedureReturn r1
EndProcedure

; ----

Procedure ReadPreferencesFile()
  Protected r1, filename.s, key.s, cnt, temp.s
  
  ExplorerPath = ""
  HistorySize = 0
  OpenedFiles = 0
  ClearList(RecentFiles())
  ClearList(RecentProject())
  ClearList(OpenedFiles())
  
  filename = GetPreferencesPath() + "purebasic.prefs"
  If OpenPreferences(filename)
    AddInfo("Explorer")
    If PreferenceGroup("Explorer")
      ExplorerPath = ReadPreferenceString("path", "")
      If IsNetPath(ExplorerPath)
        AddInfo("! Path: " + ExplorerPath)
      Else
        AddInfo("- Path: " + ExplorerPath)
      EndIf  
    EndIf
    
    AddInfo("RecentFiles")
    If PreferenceGroup("RecentFiles")
      HistorySize = ReadPreferenceInteger("HistorySize", 0)
      AddInfo("- HistorySize: " + HistorySize)
      cnt = 0
      Repeat
        cnt + 1
        key = "RecentFile_" + cnt
        temp = ReadPreferenceString(key, "")
        If temp
          AddElement(RecentFiles())
          RecentFiles() = temp
          If IsNetPath(temp)
            AddInfo("! " + key + ": " + temp)
          Else
            AddInfo("- " + key + ": " + temp)
          EndIf  
        Else
          Break
        EndIf
      ForEver
      cnt = 0
      Repeat
        cnt + 1
        key = "RecentProject_" + cnt
        temp = ReadPreferenceString(key, "")
        If temp
          AddElement(RecentProject())
          RecentProject() = temp
          If IsNetPath(temp)
            AddInfo("! " + key + ": " + temp)
          Else
            AddInfo("- " + key + ": " + temp)
          EndIf  
        Else
          Break
        EndIf
      ForEver
      ProjectSize = ListSize(RecentProject())
    EndIf
    
    AddInfo("OpenedFiles")
    If PreferenceGroup("OpenedFiles")
      OpenedSize = ReadPreferenceInteger("Count", 0)
      AddInfo("- Count: " + OpenedSize)
      cnt = 0
      Repeat
        cnt + 1
        key = "OpenedFile_" + cnt
        temp = ReadPreferenceString(key, "")
        If temp
          AddElement(OpenedFiles())
          OpenedFiles() = temp
          If IsNetPath(temp)
            AddInfo("! " + key + ": " + temp)
          Else
            AddInfo("- " + key + ": " + temp)
          EndIf  
        Else
          Break
        EndIf
      ForEver
      key = "OpenedProject"
      OpenedProject = ReadPreferenceString(key, "")
      If IsNetPath(OpenedProject)
        AddInfo("! " + key + ": " + temp)
      Else
        AddInfo("- " + key + ": " + temp)
      EndIf  
    EndIf
    ClosePreferences()
    r1 = #True
  Else
    AddInfo("Can't open preferences file!")
    r1 = #False
  EndIf
  
  ProcedureReturn r1
  
EndProcedure

; ----

Procedure CleanUpPreferencesFile()
  Protected r1, filename.s, key.s, cnt, temp.s, path.s, len, do
  
  AddInfo("Start CleanUp ...")
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      path = "/Volumes/"
      len = Len(path)
      
    CompilerCase #PB_OS_Windows
      path = #PS$ + #PS$
      len = Len(path)
      
    CompilerCase #PB_OS_Linux
      path = "smb-share:"
      len = 0
      
  CompilerEndSelect
  
  filename = GetPreferencesPath() + "purebasic.prefs"
  If OpenPreferences(filename)
    AddInfo("Explorer")
    If PreferenceGroup("Explorer")
      If IsNetPath(ExplorerPath)
        ExplorerPath = GetHomeDirectory()
        WritePreferenceString("path", ExplorerPath)
        AddInfo("- Update Path: " + ExplorerPath)
      Else
        AddInfo("- No change")
      EndIf
    EndIf
    
    AddInfo("RecentFiles")
    If PreferenceGroup("RecentFiles")
      do = #False
      ForEach RecentFiles()
        If IsNetPath(RecentFiles())
          DeleteElement(RecentFiles())
          do = #True
        EndIf
      Next
      If do
        For cnt = 1 To HistorySize
          key = "RecentFile_" + cnt
          RemovePreferenceKey(key)
        Next
        HistorySize = ListSize(RecentFiles())
        WritePreferenceInteger("HistorySize", HistorySize)
        AddInfo("- Update HistorySize: " + HistorySize)
        
        cnt = 0
        ForEach RecentFiles()
          cnt + 1
          key = "RecentFile_" + cnt
          temp = RecentFiles()
          WritePreferenceString(key, temp)
          AddInfo("- Update " + key + ": " + temp)
        Next
      Else
        AddInfo("- No change recent files")
      EndIf
      
      do = #False
      ForEach RecentProject()
        If IsNetPath(RecentProject())
          DeleteElement(RecentProject())
          do = #True
        EndIf
      Next
      If do
        For cnt = 1 To ProjectSize
          key = "RecentProject_" + cnt
          RemovePreferenceKey(key)
        Next
        cnt = 0
        ForEach RecentProject()
          cnt + 1
          key = "RecentProject_" + cnt
          temp = RecentProject()
          WritePreferenceString(key, temp)
          AddInfo("- Update " + key + ": " + temp)
        Next
      Else
        AddInfo("- No change recent projects")
      EndIf
        
    EndIf
    
    AddInfo("OpenedFiles")
    If PreferenceGroup("OpenedFiles")
      do = #False
      ForEach OpenedFiles()
        If IsNetPath(OpenedFiles())
          DeleteElement(OpenedFiles())
          do = #True
        EndIf
      Next
      If do
        For cnt = 1 To OpenedSize
          key = "OpenedFile_" + cnt
          RemovePreferenceKey(key)
        Next
        OpenedSize = ListSize(OpenedFiles())
        WritePreferenceInteger("Count", OpenedSize)
        AddInfo("- Update Count: " + OpenedSize)
        
        cnt = 0
        ForEach OpenedFiles()
          cnt + 1
          key = "OpenedFile_" + cnt
          temp = OpenedFiles()
          WritePreferenceString(key, temp)
          AddInfo("- Update " + key + ": " + temp)
        Next
      Else
        AddInfo("- No change opened files")
      EndIf
      If IsNetPath(OpenedProject)
        key = "OpenedProject"
        temp = "[Nothing]"
        WritePreferenceString(key, "")
        AddInfo("- Clear OpenedProject " + key + ": " + temp)
      Else
        AddInfo("- No change opened project")
      EndIf
    EndIf
    ClosePreferences()
    r1 = #True
    AddInfo("Finished CleanUp.")
  Else
    AddInfo("Can't open preferences file!")
    r1 = #False
  EndIf
  
  ProcedureReturn r1
  
EndProcedure

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainList, 5, 5, dx -10, dy - 45)
  ResizeGadget(#MainButtonRead, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCleanup, dx - 130, dy - 35, 120, 30)
  EndProcedure

Procedure Main()
  Protected dx, dy
  Protected r1
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
      MenuBar()
    CompilerEndIf
    
    MenuItem(#MainMenuExit, "E&xit")
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ListViewGadget(#MainList, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonRead, 10, dy - 35, 120, 30, "Read")
    ButtonGadget(#MainButtonCleanup, dx - 130, dy - 35, 120, 30, "CleanUp")
    
    ; macOS NSTableView Styles
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      CocoaMessage(0, GadgetID(#MainList), "sizeToFit")
      CocoaMessage(0, GadgetID(#MainList), "setColumnAutoresizingStyle:", 0); NSTableViewNoColumnAutoresizing = 0
    CompilerEndIf
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramAbout, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainButtonRead
              ClearGadgetItems(#MainList)
              IsRead = ReadPreferencesFile()
              
            Case #MainButtonCleanup
              If IsRead
                r1 = MessageRequester("Question", "Start Preferences CleanUp?", #PB_MessageRequester_Warning | #PB_MessageRequester_YesNo)
                If r1 = #PB_MessageRequester_Yes
                  ClearGadgetItems(#MainList)
                  CleanUpPreferencesFile()
                EndIf
              EndIf
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Purebasic Preferences Cleaner (All OS)

Post by Kwai chang caine »

Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply