[Home] [Overview] [Developer] [Manual]


Developing Google Desktop Sidebar plug-in using Delphi

Notes: Google Desktop's developer manual now recommends using Javascript to develop Google Desktop plug-in, though COM interfaces are still supported. Thus I just rewrote Open Contacts Google Desktop Sidebar using Delphi. (updated 2005-05-18)

A few months ago, I decided to develop a Google Desktop sidebar plug-in in order to provide quick search functions of Open Contacts through the sidebar. I studied the SDK for a few hours, and I preferred using Delphi to do the coding. I found few clues in the Web and the newsgroups. Eventually I had to figured out how to do it. Finally things got done, and I would share some experiences with you Delphi guys.

Though I had done a lot low level C/C++ programming on various platforms 10 years ago, I was mostly engaged in developing business applications for Windows using Delphi in the last 10 years. I rarely care much about low level programming of system house keeping. If I had to, I would wrap them into higher level of interfaces, then forgot the technical details, and focus on business logics.

Telling you this, just try to make you not to laugh at me who does not always use conventional terms describing the technical details.

Background

Google had provided a set of SDK and documentations at

http://desktop.google.com/developer.html

There are pretty comprehensive Developer Guides, tutorials, and examples for developers using Java, VB and C#. Somehow the documents are not so friendly to Delphi developers.

When I read the examples and documents a few months ago, I just wonder: do I really need to deal with these low level API and utilities for handling COM?

Finally I figured out those really important chapters for Delphi developers and which contents insignificant.

At http://desktop.google.com/developer.html, there is such statement

Developers can program in:
Visual Studio .NET
Java
Perl
Python
any language that
supports COM and XML
JavaScript and
VBScript
(Sidebar only)

Uhhh. Delphi was not worthy to mention here, sound like Delphi is one of the insignificant languages of "any language that supports COM and XML".

Now, I can have a conclusion: using Delphi is much straightforward, you can hook your plug-in to Google Desktop sidebar with just a few simple steps, without using those low level API and utilities.

Where to start

The plug-in is a COM object to GD, with implementation of some interfaces defined by Google. A sidebar plug-in is an ActiveX control, more exactly a TActiveForm object of Delphi.

Google Desktop itself provides a set of COM objects for various purposes. To hook the plug-in to Google Desktop, we need to tell Google Desktop about the existence of the plug-in through one of the COM objects of Google Desktop. The Google Desktop could display the TActiveForm object in the sidebar panel.

Let's start

1. Create an ActiveForm project

2. Import type libraries of Google Desktop

Generally you only need GoogleDesktop Display API Type Library, and Google Desktop Search API Type Library. As I don't like to install them to make my Delphi component palates more crowded, I would just press the "Create Unit" button.

Apparently you only need to do this step once, even if you are going to develop many sidebar projects.

3. When registering the plug-in, tell Google Desktop about the plug-in

When you created the project, Delphi IDE created some codes to register the in-process COM object in the main program:

exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
 

By default, they are implemented in unit ComServ. We need to override them. I created a unit with the following implementation:

function DllGetClassObject(const CLSID, IID: TGUID; var Obj): HResult; stdcall;
begin
  Result := ComServ.DllGetClassObject(clsid, iid, obj);
end;

function DllCanUnloadNow: HResult; stdcall;
begin
  Result :=  ComServ.DllCanUnloadNow
end;

function DllRegisterServer: HResult; stdcall;
var
  rr : TGoogleDesktopRegistrar ;
  rg : IGoogleDesktopRegisterDisplayPlugin;
  a : Variant;
  reg : TRegistry;
begin
  Result :=  ComServ.DllRegisterServer;

  if Result = S_OK then
  begin
    try
    rr := TGoogleDesktopRegistrar.Create(nil);

    a := varArrayCreate([0, 5], varVariant);
    a[0] := 'Title';  a[1] := 'Open Contacts for Google Desktop';
    a[2] := 'Description';  a[3] := 'Lookup contacts through name, phone or Email.';
    a[4] := 'Icon';  a[5] := '';       // icon is not used for sidebar
    //: Google desktop will show above message when registering the plugin

    rr.StartComponentRegistration(GuidToString(Class_OpenContactsGT), a  );
    rg := rr.GetRegistrationInterface('GoogleDesktop.DisplayPluginRegistration') as IGoogleDesktopRegisterDisplayPlugin;
    rg.RegisterPlugin(GuidToString(Class_OpenContactsGT), true);
//    WriteLogFile( 'GetRegistrationInterface');
    rr.FinishComponentRegistration;

    reg := TRegistry.Create;
    reg.RootKey :=  HKEY_CLASSES_ROOT;
    reg.OpenKey('\CLSID\'+GuidToString(CLASS_OpenContactsGT)+'\Description', true);
    reg.WriteString('', 'Look up contacts through name, phone or Email OK.');
    reg.OpenKey('\CLSID\'+GuidToString(CLASS_OpenContactsGT)+'\Settings', true);
    reg.WriteInteger('SearchType', 0);
    finally
    rr.Free;
    FreeAndNil(reg);
    end;
  end;
end;

function DllUnregisterServer: HResult; stdcall;
var
  rr : TGoogleDesktopRegistrar ;
begin
  Result := ComServ.DllUnregisterServer;
  if Result = S_OK then
  begin
    try
    rr := TGoogleDesktopRegistrar.Create(nil);
    rr.UnregisterComponent(GuidToString(Class_OpenContactsGT));
    finally
    rr.Free;
    end;
  end;

end;

In TGoogleDesktopRegistrar  you will use the TGoogleDesktopRegistar object to tell Google Desktop about the existence of the plug-in.

Variable "a" is a buffer to store info being shown during the registration. Such array structure is needed. The structure looks like much C style. Yes, not surprisingly in Google Inc. most programmers are C guru.

We might need to write something to the registry of the plug-in COM object. Subkey Description is for showing description in the "Panels & Alerts" window. However, in Google Desktop 4.2006.0509.1244-en-pb, this window disappeared, while Google Desktop 4.2006.306.1208-en still has this window. Somehow Google Desktop had become more sticky to the Internet, and less friendly to the desktop without internet connection.

The subkey Settings is just an application specific stuff I used for the plug-in I developed, don't worry about that.

When you uninstall the plug-in, Windows will call DllUnregisterServer. Just let TGoogleDesktopRegistar tell Google Desktop to unhook the plug-in.

4. Build and register

Build the project, and register the object through "RegSvr32.exe". The message defined in variable "a" will appear. If Google Desktop sidebar is on, the plug-in will appear on the top of the sidebar panel right away.

You see, very easy, just  4 simple steps with a few function calls, you hook the ActiveForm object to Google Desktop.

5. What else

How about the title of the sidebar plug-in? It is the display name of the object.  You may define it through the Type Library window. Get it from [Main Menu -> View -> Type Library].

The Help String will be the title of the sidebar plug-in. And this string is actually stored in the default key value of the registry of the COM object under HKEY_CLASSES_ROOT\CLSID\...

How about the About box? Delphi IDE had created the hook for you. IOpenContactsGT defines a function AboutBox. Apparently Google Desktop will talk to this interface and launch the About Box window through the implementation of the ActiveForm object.

How about the Options window? Yeah, I haven't yet think about it. As I am not going to add too many functions to this tiny window of sidebar, I would try to make things simple and smart, rather than give the end users too many options of unnecessary customization.

Nevertheless, I am going to talk more about closer interactions with the Google Desktop API in another article.

Hopefully, this article is good enough for you to build your first sidebar plug-in using your beloved Delphi.

Please find example codes, you may download the source codes of Google Desktop Open Contacts plug-in, or go to Australian Delphi User Groups website

http://www.adug.org.au/meetings/syd/past_meetings.htm

 to download the materials of the meeting in May 2006.