Promoting QtFlickr API

Couple days ago I found QtFlickr API written by Evgeni Gordejev. You can check it out from http://www.qtflickr.com. As the name QtFlickr implies, it’s a Qt API for using Flickr API. It provides a simple interface for request creation, response handling and photo uploading. It’s a very small layer, only 5  classes and two structs. Like Qt, the QtFlickr can be run in the following platforms: Windows, Linux, Mac OS X, Symbian and Maemo.

The main class QtFlickr basically provides a nice wrapper class with convenience methods. See the interface below:

QtFlickr( const QString &apiKey,
   const QString &apiSecret,
   QObject *parent = 0 );

~QtFlickr();

void setToken ( const QString &token );
QUrl authorizationUrl ( const QString &frob,
   const QString &perms = "write" );

int get ( const Method &method,
   const Request &request = Request(),
   void* userData = 0 );

int post ( const Method &method,
   const Request &request = Request(),
   void* userData = 0 );

int upload ( const Photo &photo,
   const Request &request = Request(),
   void* userData = 0 );

signals:

void requestFinished ( int reqId,
   Response data,
   ErrorData err,
   void* userData );

void uploadProgress ( int percent );

The Usage of QtFlickr

Usually a Flickr client needs to be authenticated with the Flickr. If you’re a developer you can create a public and private keys for your application in the Flickr. These keys are used by your application and the actual authentication is done using a web browser. Then what you do in your application, is that you store your received token from the Flickr and that token will be used by all the methods that requires authentication, but it’s handled under the hood by QtFlickr. I guess there are other ways to do authentication also, but check the example below how to do that via web browser. From here you can find more information about the Flickr authentication.

MyApp::MyApp(QObject * parent ) {
   qtFlickr = new QtFlickr ( "Your Public Key","Your Private Key", this );

   // connecting listner to the requestFinished() signal
   connect(qtFlickr,SIGNAL(requestFinished(int,Response, ErrorData,void*)),
                 this,SLOT(requestFinished (int,Response,ErrorData, void*)));

  // A place to store the token
  QSettings settings("QtFlickr","Authorization");
  QString token = settings.value("token").toString();

   if(!token.isEmpty()){
     // This app has been already authenticated.
     qtFlickr->setToken(token);
   }else{
     // Authenticate
     Method method("flickr.auth.getFrob");
     Request request("frob");
     // Store request id for identification in a slot that is called later
     requestId[GetFrob] = qtFlickr->get(method,request);
   }
   ...

void MyApp::requestFinished ( int reqId, Response data, ErrorData err, void* userData )
{

   if ( reqId == requestId[GetFrob]){
      QString frob = data.tagValue("frob");
      QUrl authUrl = qtFlickr->authorizationUrl(frob);
      // Open WebBrowser to show the Flickr's authentication page
      QDesktopServices::openUrl ( authUrl );

      // Show a message box to make sure that user has confirmed
      // the authentication
      QMessageBox msgBox;
      msgBox.setText("Press Ok when you have completed authorization");
      int result = msgBox.exec();
      if( result == QMessageBox::Ok){
         Method method("flickr.auth.getToken");
         method.addArgument( "frob", frob );
         Request request("token");
         request.addTag("user","username,fullname");
         requestId[GetToken] = qtFlickr->get(method, request);
       }
      }
      else
      if ( reqId == requestId[GetToken] ){
        QString token = data.tagValue("token");
        QString username = data.attrValue("user", "username");
        QString fullname = data.attrValue("user", "fullname");

        qtFlickr->setToken(token);

        QSettings settings("QtFlickr","Authorization");
        settings.setValue("username",username);
        settings.setValue("token",token);

        qDebug() << "Token received:" << token << username << fullname;
        //Now you can call authorized calls with "write" permission
    }
    ....

After this, you are free to call what ever Flickr API methods you want and you will have  a write permission to your Flickr account. The example above was just a small example how you can use QtFlick quite easily with small effort. The example code was almost directly taken from the QtFlickr documentation. There are also other examples of how to use this API for example getting user information or showing images from a Flickr. If you’re interested in about using Flickr from Qt app, I guess it’s worth to check this API

Summa Summarum

I’ve had an idea of writing an Qt API for Flickr sometime ago, but I’ve never really had time to do that. I’m glad that Evgeni Gordejev had time for that. The QtFlickr API is simple and easy to use and AFAIK  you can basically use the each feature of Flickr API. (Correct me if I’m wrong, I don’t know Flickr API that well.) The QtFlickr provides really an easy way to develop client application for Flickr and it has a good documentation. I also think that Qt’s signal and slot mechanism is a great way to handle asynchronous responses from the any online service. I sure hope that this API will be taken in use by many developers.

What comes to the code, it looks quite good, although I would like to see classes to use private pointer mechanism in a similar way that they are used in Qt. That makes the public header more clean instead of having bunch of private members visible there. This is just a matter of taste, but it could help to prevent e.g. ABI breaks in the future if this API has been widely deployed. Another thing is the usage of friend class mechanism. Basically the QtFlickr class is a friend class for Response, Request, Method and for Photo classes. This is also a matter of taste, but all the bells and whistles start to ring in my head when I see friend mechanism to be used in this widely. After all, the implementation is simple and I don’t have  much to complain about it. Looks good in general.

Thanks for reading.

Tags: , , , ,

One Response to “Promoting QtFlickr API”

  1. joomla templates nulled…

    [...]Promoting QtFlickr API « Zchydem's Blog[...]…

Leave a Reply

You must be logged in to post a comment.