Monday, April 08, 2013Google Chrome has the very interesting feature to be able to store its settings via your Google account. This means that when you install Chrome on a different machine and associate it with your Google account, it will automatically "inherit" all settings of your other configs. Not only Google Chrome does this but increasingly Windows desktop applications and tools use online storage to offer the convenience of having identical configurations on multiple machines. Another excellent example is the Google Chrome extension Speed Dial 2 that can synchronize its settings among machines this way.
Now, nothing prevents us from doing the same for a Delphi application and with the TMS Cloud Pack, it becomes very simple to use a cloud storage service such as DropBox to allow the user to persist his settings in a DropBox folder and have these settings synchronised between different machines this way.
We have created a very rudimentary example to demonstrate the concept. The settings from the sample application are simply the contents of a listbox where items can be added or removed via the application. We save the settings as a simple text file and load this at application startup time from a DropBox account and save it back to DropBox when the application closes.
All we need to do is drop an instance of TAdvDropBox from the TMS Cloud Pack on the form, set the DropBox application key and secret (that can be obtained for free after registering with DropBox) load the access tokens and when the access tokens do not yet exist, get an access token for DropBox via an authentication/authorization step and call one line of code to download the settings file.
When the application closes, we simply upload the settings again with one call.
When we save the access token (here for reasons of simplicity of the demo in an INI file), this one time authentication/authorization with the DropBox account by the user is sufficient (which is similar for Google Chrome settings synchronisation too by the way)
The code with information in comments for application startup becomes:
When the application is first used, there is no access token to download a file from the users DropBox account and in this condition, first the DropBox authentication login screen is shown:
procedure TForm1.FormCreate(Sender: TObject); var acc: boolean; begin Dirty := false; // set the DropBox application key & secret here that is provided by DropBox // for free when registering via: https://www.dropbox.com/developers/apps AdvDropBox1.App.Key := DropBoxAppkey; AdvDropBox1.App.Secret := DropBoxAppSecret; // Use simple INI file storage for the access token that DropBox will give AdvDropBox1.PersistTokens.Location := plIniFile; AdvDropBox1.PersistTokens.Key := '.sync.ini'; AdvDropBox1.PersistTokens.Section := 'DropBox'; if AdvDropBox1.App.Key <> '' then begin // Try to load an access token if it was already retrieved earlier AdvDropBox1.LoadTokens; // Test if the token is working acc := AdvDropBox1.TestTokens; if not acc then // If the token was not working try to refresh it acc := AdvDropBox1.RefreshAccess; if not acc then // No token was found or existing token is not valid, so authenticate/authorize via DropBox AdvDropBox1.DoAuth else // Download the settings from DropBox and apply LoadSettings; end; end;
followed by the authorization screen:
When the access token is obtained the first time, the TAdvDropBox component triggers OnReceivedAccessToken from where the token is first saved and then the settings downloaded and applied:
procedure TForm1.AdvDropBox1ReceivedAccessToken(Sender: TObject); begin AdvDropBox1.SaveTokens; LoadSettings; end;
When the application closes, we can simply save the settings in the Form's OnClose event via:
procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction); begin listbox1.Items.SaveToFile(GetSettingsFileName); if Dirty and AdvDropBox1.TestTokens then AdvDropBox1.Upload(nil,GetSettingsFileName); end;
The full source of the sample can be download here. With the TMS Cloud Pack offering similar access also to Microsoft Skydrive and Google Drive, it becomes very easy to change to the cloud storage of your preference by swapping to the component TAdvGDrive or TAdvSkyDrive.
This blog post has received 3 comments.
Previous | Next | Index