TMS VCL Cloud Pack v3.7 introduces new level of seamlessness to access cloud data

Thursday, September 22, 2016

In TMS VCL Cloud Pack v3.7, new components TAdvmyCloudDataConnection and TAdvmyCloudDataDataSet have been added. With these 2 components, access to structured data on the cloud via the myCloudData service becomes easier than ever.

TAdvmyCloudDataConnection
TAdvmyCloudDataConnection is a non-visual component that manages access to myCloudData. It works as the intermediator for the authentication and authorization to access myCloudData for one or multiple TAdvmyCloudDataDataSet instances. This means that via the TAdvmyCloudDataConnection at least a one-time authentication and authorization is done with myCloudData to obtain the access token and the TAdvmyCloudDataDataSet can then work through TAdvmyCloudDataConnection to use this access token. To let the TAdvmyCloudDataDataSet use the connection is as simple as assigning the TAdvmyCloudDataConnection instance to TAdvmyCloudDataDataSet.Connection, just like we are used to assign a TADOConnection to a TADOTable for example.


TAdvmyCloudDataDataSet
TAdvmyCloudDataDataSet is a wrapper as a dataset of a table in the myCloudData service. When a connection is provided, set either the name of the table via the Table property or the unique ID of the table via the TableID property. When a connection has been made (i.e. access token was obtained), setting the property TAdvmyCloudDataDataSet.Active to true will fetch the table data from the myCloudData table and present it as a TDataSet through a TDataSource to any DB-aware component in VCL or via LiveBindings in FireMonkey applications. Note that this approach works both at run-time and design-time, so we can work with the cloud data at design-time to configure our DB-aware controls connected to it.


Dataset filter
An easy way is provided to perform basic dataset filtering. The TAdvmyCloudDataDataSet has a filter property which is presented as a collection of filter conditions. The filter condition consists of a field name, filter value, a filter condition logical operator and a filter comparison operator. The filter condition can be easily set at design-time where the dataset field can be picked and the values set.



From here we can work with the cloud data in much the same way from our code on the dataset as Delphi developers have been doing all the time with datasets, i.e. insert, delete, update records.

Rich metadata
But with the updated myCloudData service, there is much more than this possible as myCloudData now offers rich metadata for its structured data. This rich metadata can be programmatically accessed, created and updated but also easily configured via the myCloudData web interface. We'll cover this in a follow-up blog focused on how we can benefit from this rich metadata, but here you can already glance over the web interface you can access from your myCloudData account:


You can explore all this with a free myCloudData account. Create a new table via this account and you can use our TAdvmyCloudDataDataSetDemo application included in TMS VCL Cloud Pack to explore this.

Stay tuned for more!

Bruno Fierens


Bookmarks: 

This blog post has received 2 comments. Add a comment.



Weekend fun with Raspberry Pi 2 and TMS Cloud Pack

Sunday, June 07, 2015

Thinking to myself, why go through the hassle of sacrificing a desktop computer or fiddle around with VMs to test something on a Linux machine when I have here several 85mmx56mm Raspberry Pi boards laying around capable of amazing things. For my experiments, I wanted to have an ownCloud available and first thing to do was getting an ownCloud up and running on the Raspberry Pi 2, which turned out to be a piece of cake. To do this, follow these instructions:

Step 1: getting Apache up and running

If Apache is already setup on your device, skip this step, otherwise, from the command line, execute:

$ sudo apt-get install apache2

The Raspberry Pi 2 LED flashes for a short while and when completed, verify it is working by opening a browser on a machine in the network and navigate to the IP address of the Raspberry Pi 2 like http://192.168.1.100. When install was successful, you'll be greeted with an "It works!" page.

Step 2: install PHP and tools

In case you had Apache already configured with PHP 5, you can also skip this step.
To install, execute following commands:

$ sudo apt-get install php5
$ sudo apt-get install php5-gd
$ sudo apt-get install sqlite
$ sudo apt-get install php5-sqlite
$ sudo apt-get install php5-curl

After some more LED flickering, these steps will also be executed and the Apache environment with PHP 5 is ready.

Step 3: install ownCloud

To install ownCloud, start by downloading the latest distribution. At this time, this is v8.0.3 and is downloaded with:

$ sudo wget https://download.owncloud.org/community/owncloud-8.0.3.tar.bz2

Next step is to unpack the ownCloud distribution and install it under Apache.

$ sudo mv owncloud-8.0.3.tar.bz2 /var/www
$ cd /var/www
$ sudo bunzip2 owncloud-8.0.3.tar.bz2
$ sudo tar xf owncloud-8.0.3.tar

Step 4: setting up ownCloud

To finalize, the data folder for ownCloud must be created and an admin account added.
The data folder is created with following steps:

$ sudo mkdir /var/www/owncloud/data
$ sudo chown www-data:www-data /var/www/owncloud/data
$ sudo chmod 750 /var/www/owncloud/data

At this time it is more convenient to continue the setup from a browser. Either start the graphical shell on the Raspberry Pi 2 or connect from a browser on an external machine and navigate to http://localhost/owncloud or from external machine http://192.168.1.100/owncloud/
The first screen that appears is to create an admin account, so add the credentials for an admin account on this page. With this account added, owncloud is up and running and ready for use.

Step 5: Connecting from Delphi to ownCloud

Now Delphi kicks in and with a little help from the TMS Cloud Pack, let's start using ownCloud from a Delphi app.

Start your IDE, make sure TMS Cloud Pack is installed and drop the component TAdvCalDAV on the form. Configure the TAdvCalDAV component to access your ownCloud. Verify the ownCloud primary CalDAV address by opening the calendar app within ownCloud via: http://192.168.1.100/owncloud/index.php/apps/calendar/ and in the bottom left corner, click on settings where this primary address is shown. By default, this should be:

http://192.168.1.100/owncloud/remote.php/caldav/

So, now we can configure the TAdvCalDAV component to connect to ownCloud on Raspberry Pi 2:

try
  AdvCalDav1.URL := 'http://192.168.1.100/owncloud/remote.php/caldav/';
  AdvCalDav1.Username := 'tms';
  AdvCalDav1.Password := 'tmsrocks!';
  AdvCalDav1.Active := true;
except
   Exit;
end;

After a successful connect, the calendars and their events available on ownCloud can be retrieved, here by filling the info in a listview:

var
  i: integer;
  cdi: TCalDavItem;
  li: TListItem;
begin
  // get events
  for i := 0 to AdvCalDav1.Items.Count - 1 do
  begin
    cdi := AdvCalDav1.Items[i];

    if cdi.vCalendar.vEvents.Count > 0 then
    begin
      li := ListView1.Items.Add;
      li.Caption := cdi.vCalendar.vEvents[0].Summary;
      li.SubItems.Add(FormatDateTime('dd/mm/yyyy hh:nn',cdi.vCalendar.vEvents[0].DTStart));
      li.SubItems.Add(FormatDateTime('dd/mm/yyyy hh:nn',cdi.vCalendar.vEvents[0].DTEnd));
      li.SubItems.Add(cdi.vCalendar.vEvents[0].Location);
      li.SubItems.Add(cdi.vCalendar.vEvents[0].Description.Text);
      li.Data := cdi;     
    end;
  end;
end;

Adding a new calendar item is equally easy:

var
  cdi: TCalDavItem;
  li: TListItem;
begin
  // set the calendar for the event
  cdi := AdvCalDav1.Items.Insert('Personal');  // add item to the "Personal" calendar
  cdi.vCalendar.vEvents.Add;
  cdi.vCalendar.vEvents[0].Summary := 'Schloss Dyck Classic Days';
  cdi.vCalendar.vEvents[0].Location := 'Jüchen, Deutschland';
  cdi.vCalendar.vEvents[0].Description.Text := 'Automobile Kulturgeschichte auf der Museums-Insel';
  cdi.vCalendar.vEvents[0].DTStart := EncodeDate(2015,7,31);
  cdi.vCalendar.vEvents[0].DTEnd := EncodeDate(2015,8;2);
  cdi.Post;
end;

or modifying an existing item

var
  cdi: TCalDavItem;
  li: TListItem;
begin
  cdi := ListView1.Selected.Data;
  cdi.vCalendar.vEvents[0].Summary := 'Schloss Dyck Classic Days 2015';
  cdi.Update;
end;

Similar to accessing the calendars of ownCloud, you can also access the contacts with the TAdvCardDAV component in a very similar way. And this rounded up my little weekend fun experiment. With so much ubiquitous computing power around and wonderful technology, aren't we living in very exciting times?

Bruno Fierens


Bookmarks: 

This blog post has received 2 comments. Add a comment.



Chrome-style application setting persistence/synchronisation with DropBox

Monday, April 08, 2013

Google 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:
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;
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:


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.

Bruno Fierens


Bookmarks: 

This blog post has received 3 comments. Add a comment.




Previous  |  Next  |  Index