User Tools

Site Tools


k19:k19.1:k19.1.3:k19.1.3.1:start

19.1.3.1 Example 3 - Profile Manager GB2

With Gambas 3, profiles can be managed in a configuration file in a convenient way because the Settings class has been extended by the Keys property. In order to be able to work with profiles under Gambas 2, this class was rewritten for Gambas 2 as SettingsP. The rewrite was successful - except for the fact that no umlauts and no ß may be used in the profile names. The SettingsP class requires the _Settings_Keys class and the Main module. All three files are made available to you in programme example 3. Therefore, you need to include the following three files in your Gambas2 project:

Klassen und Modul

Figure 19.1.3.1.1: Module and classes for working with profiles under Gambas 2.

The Settings.Clear(Section) method was also used to clear existing profiles.

MethodDescription
Settings.Clear()This method deletes the Settings object and frees the occupied memory.
Settings.Clear(Section)This method clears only the specified section in the configuration file.

Table: 19.1.3.1 Description of Methods of the Settings Class

With the Profile Manager programme you can

  • create new profiles,
  • modify existing profiles,
  • save new or changed profiles in the configuration file or
  • delete existing profiles in the configuration file.

Profil-Manager Gambas 2

Figure 19.1.3.1.2: Profile Manager - Gambas 2

The entries for the profile name, the server, the user as well as the password are necessary, while the entries for the local (start) directory and the remote directory are optional.

Since not every section in the configuration file is assigned to a profile, the two characters P_ are prefixed to the real profiles as identifiers. There is only one key-value pair in the [LastProfile] section: LastProfilName=“Online book”, which contains the information about the last profile used, as you can see from the excerpt from the configuration file used:

# Profile für einen FTP-Client
[LastProfil]
LastProfilName="Online-Buch"

[P_Online-Buch]
FTPServerName="www.gambas-buch.de"
FTPUserName="hatux"
FTPUserPassword="vdo#bs12"
FTPInitialDirLocal="Online-Buch"
FTPInitialDirRemote="dokuwiki"

[P_Gymnasium]
FTPServerName="www.gymnasium.de"
FTPUserName="w00xyz"
FTPUserPassword="f1#skkk"
FTPInitialDirLocal="Bild&Ton"
FTPInitialDirRemote="Projekttage"
...

Here is the full source code for the Profile Manager programme:

' Gambas class file ---> Gambas 2
 
PUBLIC pSettings AS SettingsP
PUBLIC aProfilMatrix AS NEW String[]
 
PUBLIC SUB Form_Open()
  FProfilManager.Center
  FProfilManager.Border = 1
  aProfilMatrix.Clear
  pSettings = NEW SettingsP(Application.Path &/ "Profils/profils.conf", "Profile für einen FTP-Client")
  cmbProfilName.SetFocus
  txtFTPUserPassword.Password = TRUE
  cmbProfilName.ReadOnly = TRUE
 
  IF pSettings["LastProfil/LastProfilName"] = NULL THEN
     Message.Info("Es existiert kein Profil für das Client-Programm!") ' optional
     cmbProfilName.ReadOnly = FALSE
  ELSE
     LoadSettings(pSettings["LastProfil/LastProfilName"])
  ENDIF
 
END ' Form_Open()
 
PUBLIC SUB LoadSettings(OPTIONAL sProfilName AS String)
  DIM sSection AS String
 
  cmbProfilName.Clear()
  aProfilMatrix.Clear
  pSettings.Reload()
 
  FOR EACH sSection IN pSettings.Keys ' Profile (beginnen mit P_) auslesen und in Matrix speichern
      IF Left(sSection, 2) = "P_" THEN
         aProfilMatrix.Add(Mid(sSection, 3))
      ENDIF
  NEXT
 
  IF sProfilName THEN
     aProfilMatrix.Remove(aProfilMatrix.Find(sProfilName)) ' Das Profil suchen und in der Matrix löschen
     aProfilMatrix.Add(sProfilName, 0)  ' Das Profil an die 1. Stelle in der Matrix setzen
     GetProfilValues(sProfilName) ' Die Profil-Daten zum Profil anzeigen
  ELSE
     GetProfilValues(aProfilMatrix[0])
  ENDIF
 
  cmbProfilName.List = aProfilMatrix
END
 
PRIVATE SUB SetProfil(sProfilName AS String)
  SetProfilValues(sProfilName)
  LoadSettings(sProfilName)
  cmbProfilName.ReadOnly = FALSE
END
 
PRIVATE SUB Reset()
  cmbProfilName.Clear()
  cmbProfilName.ReadOnly = FALSE
  txtFTPServerName.Clear()
  txtFTPUserName.Clear()
  txtFTPUserPassword.Clear()
  txtInitialDirLocal.Clear()
  txtInitialDirRemote.Clear()
  txtFTPUserPassword.Password = FALSE
END
 
PRIVATE SUB ProfilClear(sProfilName AS String)
  IF Message.Question("Soll das Profil " & sProfilname & " gelöscht werden?", "Ja", "Nein") = 1 THEN
     pSettings.Clear("P_" & sProfilName)
     pSettings.Save()
  ELSE
     RETURN
  ENDIF
  LoadSettings()
END
 
PUBLIC SUB GetProfilValues(sProfilName AS String)
  sProfilName = "P_" & sProfilName
  txtFTPServerName.Text = pSettings[sProfilName & "/FTPServerName"]
  txtFTPUserName.Text = pSettings[sProfilName & "/FTPUserName"]
  txtFTPUserPassword.Text = pSettings[sProfilName & "/FTPUserPassword"]
  txtInitialDirLocal.Text = pSettings[sProfilName & "/FTPInitialDirLocal"]
  txtInitialDirRemote.Text = pSettings[sProfilName & "/FTPInitialDirRemote"]
END
 
PUBLIC SUB SetProfilValues(sProfilName AS String)
  sProfilName = "P_" & sProfilName
  pSettings[sProfilName & "/FTPServerName"] = txtFTPServerName.Text
  pSettings[sProfilName & "/FTPUserName"] = txtFTPUserName.Text
  pSettings[sProfilName & "/FTPUserPassword"] = txtFTPUserPassword.Text
  pSettings[sProfilName & "/FTPInitialDirLocal"] = txtInitialDirLocal.Text
  pSettings[sProfilName & "/FTPInitialDirRemote"] = txtInitialDirRemote.Text
  pSettings.Save()
END
 
PUBLIC SUB Form_Close()
  pSettings["LastProfil/LastProfilName"] = cmbProfilName.Text
' Auf den Save-Befehl kann verzichtet werden. Er wird automatisch ausgeführt beim Schließen des Programms.
  pSettings.Save()
END
 
PUBLIC SUB cmbProfilName_Click()
  LoadSettings(cmbProfilName.Current.Text)
END
 
PUBLIC SUB btnProfilClear_Click()
  ProfilClear(cmbProfilName.Text)
END
 
PUBLIC SUB btnProfilCreate_Click()
  IF Message.Question("Soll ein neues Profil angelegt werden?", "Ja", "Nein") = 1 THEN
     Reset()
  ELSE
     RETURN
  ENDIF
END
 
PUBLIC SUB btnProfilSave_Click()
  IF Message.Question("Das neue oder geänderte Profil <br><b>" & cmbProfilName.Text & "</b><br>speichern?", "Ja", "Nein") = 1 THEN
     IF txtFTPServerName.Text <> "" AND txtFTPUserName.Text <> "" AND \
        txtFTPUserPassword.Text <> "" AND cmbProfilName.Text <> "" THEN
        SetProfil(cmbProfilName.Text)
        cmbProfilName.ReadOnly = TRUE
        txtFTPUserPassword.Password = TRUE
     ELSE
        Message.Error("Die Profil-Daten sind nicht vollständig oder fehlerhaft!")
        RETURN
     ENDIF
  ELSE
     RETURN
  ENDIF
END
 
PUBLIC SUB btnManagerClose_Click()
  FProfilManager.Close()
END
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k19/k19.1/k19.1.3/k19.1.3.1/start.txt · Last modified: 13.10.2023 by emma

Page Tools