Friday, October 30, 2020

Default Git Message as a Pre-Commit Reminder

If you want a reminder when you commit in Git that you need to do something before committing, such as generating the text equivalents of binary files (something I occasionally forget to do if I working on something that doesn't use Project Explorer, which automatically does that for me), do the following:

  • Create a text file anywhere you wish (named, for example, commit.txt) with the default message you want used in Git commits, such as “Remember to DO WHATEVER YOU NEED TO DO".
  • Open a command window in the project folder (the one that has .git as a subdirectory).
  • Type in a command window: git config commit.template “path\commit.txt”, where path is the full path for the text file you created.

Now, whenever you commit, the commit message defaults to “Remember to DO WHATEVER YOU NEED TO DO” (or whatever your message was), which of course you will then overwrite with the real commit message after you do what the reminder tells you to do.

Application Configuration using JSON

After watching Andrew MacNeill's Quasar and JSON: A Full Stack Experience for the DB Developer presentation at Virtual Fox Fest 2020, I was inspired to look at nfJSON, a VFX project by Marco Plaza. This cool project adds JSON support to VFP applications. The thing that tweaked my interest was the ability to convert a JSON string to a VFP object and vice versa with one line of code.

My first thought was using this for configuration settings. Just about every application needs configuration settings: it's better to read settings such as database connection information, file locations, email settings, etc. from a configuration source rather than hard-coding them into the app. I've used the Windows Registry, DBF files, INI, and XML files at various times but all of those require manually coding the reading and writing between the source and the VFP object(s) containing the settings. With nfJSON, it's one line of code.

I created a wrapper class called SFConfiguration. It only has three methods:

  • Load returns an object with properties matching the name/value pairs in the JSON contained in the specified settings file. If the file doesn't exist or is empty (such as the first time the application is run), it calls GetDefaultSettings (described below) to get default settings.
  • Save saves the properties of the specified settings object to the file specified in either the passed file name or the cSettingsFile property if a file name isn't passed.
  • GetDefaultSettings returns JSON for default settings. You can use this one of two ways: subclass SFConfiguration and override GetDefaultSettings to return the desired JSON, or set the oSettings property to a settings object containing the default settings.

Here's an example of using this class to get email settings:

loConfig = createobject('SFConfiguration')
loConfig.cSettingsFile = 'email.json'
loConfig.oSettings     = createobject('EmailSettings')
loSettings = loConfig.Load()
* loSettings contains the email settings for the user;
* if email.json doesn't exist, the settings in the
* EmailSettings class below are used as defaults.
* After the user enters the desired settings in some dialog,
* save them using:
loConfig.Save(loSettings)

define class EmailSettings as Custom
    Email      = 'dhennig@stonefield.com'
    MailServer = 'mail.stonefield.com'
    Port       = 25
    UseSSL     = .F.
    UserName   = 'dhennig'
    Password   = 'mypw'
enddefine

Here's what the settings object looks like:


Here's what the saved JSON looks like:


Here's another example, this time using a subclass of SFConfiguration for the same thing:

loConfig = createobject('SFEmailConfiguration')
loConfig.cSettingsFile = 'email.json'
loSettings = loConfig.Load()
* loSettings contains the email settings for the user;
* if email.json doesn't exist, the settings in the
SFEmailConfiguration class below are used as defaults.

define class SFEmailConfiguration as SFConfiguration
    function GetDefaultSettings
        text to lcSettings noshow
            {
                "email":"dhennig@stonefield.com",
                "mailserver":"mailserver.stonefield.com",
                "password":"mypw",
                "port":25,
                "username":"dhennig",
                "usessl":false
            }
            endtext
            return lcSettings
    endfunc
enddefine

Here's the code for SFConfiguration. It requires nfJSONRead.prg and nfJSONCreate.prg, which you can get from the nfJSON GitHub repository:

define class SFConfiguration as Custom
  cSettingsFile = ''
        && the name and path for the settings file
    cErrorMessage = ''
        && the text of any error that occurs
    oSettings     = ''
        && a settings object

* Load the settings from the file specified in the parameter
* or in This.cSettingsFile and return a settings object. If
* the file doesn't exist
 (such as the first time we're called),
* a settings object containing default
 settings is returned.

    function Load(tcSettingsFile)
        local lcSettingsFile, ;
            lcSettings, ;
            loSettings, ;
            loException as Exception
        try
            lcSettingsFile = evl(tcSettingsFile, This.cSettingsFile)
            if not empty(lcSettingsFile) and file(lcSettingsFile)
                lcSettings = filetostr(lcSettingsFile)
            endif not empty(lcSettingsFile) ...
            if empty(lcSettings)
                lcSettings = This.GetDefaultSettings()
            endif empty(lcSettings)
            loSettings = nfJSONRead(lcSettings)
            This.cErrorMessage = ''
        catch to loException
            This.cErrorMessage = loException.Message
            loSettings = NULL
        endtry
        This.oSettings = loSettings
        return loSettings
    endfunc

* Save the settings in the specified object to the file
* specified in
 the parameter or This.cSettingsFile.

    function Save(toSettings, tcSettingsFile)
        local lcSettingsFile, ;
            lcSettings, ;
            loException as Exception
        lcSettingsFile = evl(tcSettingsFile, This.cSettingsFile)
        if not empty(lcSettingsFile)
            try
                lcSettings = nfJSONCreate(toSettings, .T.)
                strtofile(lcSettings, lcSettingsFile)
                This.cErrorMessage = ''
            catch to loException
                This.cErrorMessage = loException.Message
            endtry
        else
            This.cErrorMessage = 'Settings file not specified.'
        endif not empty(lcSettingsFile)
        return empty(This.cErrorMessage)
    endfunc

* Gets the default set of settings as a JSON string; override
* this in a
 subclass if necessary.

    function GetDefaultSettings
        local lcSettings
        if vartype(This.oSettings) = 'O'
            lcSettings = nfJSONCreate(This.oSettings, .T.)
        else
            lcSettings = '{"text":"some text"}'
        endif vartype(This.oSettings) = 'O'
        return lcSettings
    endfunc
enddefine


Monday, October 26, 2020

Sending Email from VFP Applications: Supporting Newer SMTP Protocols

There are many libraries you can use to send emails from VFP applications. I created my own several years ago: a C# wrapper class using the .NET SMTPClient class, which I call using Rick Strahl's wwDotNetBridge. However, as discussed here, Microsoft no longer recommends using this class because it doesn't support modern protocols (which I can attest to, thanks to several support tickets from customers). As suggested in the article, I've rewritten my wrapper class to use the open source MailKit project.

Here's some code that shows how to use this wrapper class:

local loMail, ;
   llReturn
loMail = newobject('SFMail', 'SFMail.prg')
with loMail
   .cServer      = 'MyMailServer.com'
   .cUser        = 'MyUserName'
   .cPassword    = 'MyPassword'
   .nSMTPPort    = 25
   .cSenderEmail = .cUser
   .cSenderName  = 'My Name'
   .cRecipients  = 'someone@somewhere.com'
   .cSubject     = 'Test email'
   .cBody        = 'This is a test message. ' + ;
      '<strong>This is bold text</strong>. ' + ;
      '<font color="red">This is red text</font>'
   .cAttachments = 'koko.jpg'
   llReturn      = .SendMail()
   if llReturn
      messagebox('Message sent')
   else
      messagebox('Message not sent: ' + .cErrorMessage)
   endif llReturn
endwith

Here's what the received email looks like. Notice the attached image and the formatted text.

The class supports the following:

  • Text or HTML body: simply include HTML tags in the cBody property and SFMail automatically handles it
  • Attachments: cAttachments is a comma-separated list of file names
  • Normal, CC (the cCCRecipients property), and BCC recipients (the cBCCRecipients property): separate multiple email addresses with commas or semi-colons
  • SMTP or MAPI (using Craig Boyd's VFPExMAPI.fll): set lUseMAPI to .T. to use MAPI or .F. (the default) to use SMTP
  • Adjustable timeout (the nTimeout property, which is in seconds)
  • Adjustable security settings (the nSecurityOptions property; see the MailKit documentation for the values). I find the default setting of 1 (Auto) works best for most servers.
  • Diagnostic logging: set cLogFile to the name and pass of a log file; it will contain the dialog between MailKit and the mail server so you can debug any problems.

You can download SFMail and the supporting files from the Technical Papers page of my web site or directly from here.

Tuesday, October 20, 2020

Regretting not registering for Virtual Fox Fest?

 

Virtual Fox Fest continues this week and next on October 21 and 27, 2020!

The posts on social media about the first day of Virtual Fox Fest are very uplifting, the sessions were informative and inspiring, and the camaraderie during the social time after the sessions was a lot of fun.

Does this make you wish you were part of the fun and learning? No worries, you can still register before our next conference day on October 21st, and have access to all the conference materials and to videos of the sessions you missed. Head over to our Registration site.

Friday, October 09, 2020

The Fox Show: Virtual FoxFest 2020

Listen to Rick, Tamar, and I discuss Virtual Fox Fest 2020 with Andrew MacNeill on The FoxShow.

Virtual Fox Fest starts next week. If you haven't registered, there's still time: http://geekgatherings.com/registration. Want to know who else is attending? See https://virtualfoxfest.com/whoscoming.aspx.

Wednesday, October 07, 2020

Virtual Fox Fest 2020--still time to register

Our thanks to all those who responded to our plea not to wait for the last minute. Here's a reminder in case you got busy and forgot...

Virtual Fox Fest starts next week and is held October 15, 21, and 27, 2020! Get registered today! http://geekgatherings.com/Registration

Get Your Geekwear

Order your Virtual Fox Fest gear from https://geekgatherings.logosoftwear.com/. Choose from the options shown or create your own unique Virtual Fox Fest-wear. 

To choose your favorite color instead of ours, click the item you want and on that item's page, click Customize This Product. On the page that opens, click Product & Color and choose the color you want. 

To create custom items, click any item. On the page that appears, click Customize This Product. On the page that appears, click Product & Color and then click Change Product. You can select from all items offered by Logo Sportswear. If you want the VFF logo printed, be sure to start with one of the t-shirts. If you want the logo embroidered, start with anything other than one of the t-shirts. 

Once your VFF gear arrives, post your pictures on social media with the hashtag #VirtualFoxFest, so we can see them.

Only 8 days until we gather virtually via the Internet! We look forward to seeing everyone soon.