bookmarksengine/bookmarksclient/src/bookmarkclient.cpp
changeset 0 fa475d6462b2
child 2 016bf4557e2f
equal deleted inserted replaced
-1:000000000000 0:fa475d6462b2
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include<QString>
       
    20 #include<bookmarkscommonengine.h>
       
    21 #include<bookmarkclient.h>
       
    22 #include <serviceipc.h>
       
    23 #include<serviceipcclient.h>
       
    24 #include<QDebug>
       
    25 
       
    26 class BookmarksClientSidePrivate
       
    27     {
       
    28 BOOKMARKSCLIENT_PUBLIC(BookmarksClientSide)
       
    29   
       
    30 public:
       
    31     ~BookmarksClientSidePrivate();
       
    32     QString m_clientName;
       
    33     int m_clientuid;
       
    34     bool m_isConnected;
       
    35     int m_error;
       
    36     WRT::ServiceFwIPC* m_IpcSession;
       
    37     int dispatchServerMessagestoClient(int msgType, QString title);
       
    38     };
       
    39 
       
    40 /**==============================================================
       
    41  * Description: Constructor of BookmarksClientSidePrivate
       
    42  * Author: Chandrashekar.V
       
    43  * EmpId:  10289207
       
    44  ================================================================*/
       
    45 BookmarksClientSidePrivate::~BookmarksClientSidePrivate()
       
    46     {
       
    47     if (m_IpcSession)
       
    48         {
       
    49         m_IpcSession->disconnect();
       
    50         delete m_IpcSession;
       
    51         m_IpcSession = NULL;
       
    52         }
       
    53 
       
    54     }
       
    55 
       
    56 /**==============================================================
       
    57  * Description: Constructor of BookmarksClientSide
       
    58  * Author: Chandrashekar.V
       
    59  * EmpId:  10289207
       
    60  ================================================================*/
       
    61 BookmarksClientSide::BookmarksClientSide()
       
    62     {
       
    63     BOOKMARKSCLIENT_INITIALIZE(BookmarksClientSide);
       
    64 
       
    65     priv->m_IpcSession = new WRT::ServiceFwIPC(this);
       
    66 
       
    67     }
       
    68 
       
    69 /**==============================================================
       
    70  * Description: Connects to the bookmark server
       
    71  * Author: Chandrashekar.V
       
    72  * EmpId:  10289207
       
    73  ================================================================*/
       
    74 int BookmarksClientSide::connectToServer()
       
    75     {
       
    76     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
    77     bool connected = false;
       
    78     if (priv->m_IpcSession)
       
    79         {
       
    80         int retry(5);
       
    81         for (;;)
       
    82             {
       
    83             connected = priv->m_IpcSession->connect(BOOKMARKSENGINESERVER);
       
    84             if (connected)
       
    85                 {
       
    86                 break;
       
    87                 }
       
    88             else
       
    89                 {
       
    90                 if (!priv->m_IpcSession->startServer(BOOKMARKSENGINESERVER,
       
    91                 BOOKMARKSENGINESERVEREXE))
       
    92                     { // start server failed
       
    93                         qDebug() << "Failed to Start the BOOKMARK Server.";
       
    94                         break;
       
    95                         }
       
    96 
       
    97                     }
       
    98                 if (0 == --retry)
       
    99                     {
       
   100                     break;
       
   101                     }
       
   102                 }
       
   103             }
       
   104         if (connected)
       
   105             {
       
   106 
       
   107             connect(priv->m_IpcSession, SIGNAL(readyRead()), this,
       
   108                     SLOT(handleServerResponse()));
       
   109             return ErrNone;
       
   110             }
       
   111         else
       
   112             {
       
   113             return ErrGeneral;
       
   114             }
       
   115         }
       
   116 
       
   117 /**==============================================================
       
   118 * Description: Disconnects the client
       
   119 * Author: Chandrashekar.V
       
   120 * EmpId:  10289207
       
   121 ================================================================*/
       
   122 void BookmarksClientSide::closeServer()
       
   123     {
       
   124     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   125     if (priv->m_IpcSession)
       
   126         {
       
   127         priv->m_IpcSession->disconnect();
       
   128         }
       
   129     }
       
   130 
       
   131 /**==============================================================
       
   132  * Description: Destrcutor of the BookmarksClientSide
       
   133  * Author: Chandrashekar.V
       
   134  * EmpId:  10289207
       
   135  ================================================================*/
       
   136 BookmarksClientSide::~BookmarksClientSide()
       
   137     {
       
   138     }
       
   139 
       
   140 /**==============================================================
       
   141  * Description: Sends add bookmark request to the server
       
   142  * Author: Chandrashekar.V
       
   143  * EmpId:  10289207
       
   144  ================================================================*/
       
   145 int BookmarksClientSide::AddBookmark(
       
   146         BookmarkLeaf* BookmarkContent)
       
   147     {
       
   148     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   149     QString data;
       
   150     bool result = false;
       
   151     data.append(BookmarkContent->getTitle());
       
   152     data.append(BOOKMARKSENGINESEPARATOR);
       
   153     data.append(BookmarkContent->getUrl());
       
   154     data.append(BOOKMARKSENGINESEPARATOR);
       
   155     data.append(BookmarkContent->getDate().toString("dd.MM.yyyy"));
       
   156     data.append(BOOKMARKSENGINESEPARATOR);
       
   157     data.append(BookmarkContent->getTag());
       
   158     QString requestType;
       
   159     requestType.append(QString::number(EAddBookmark));
       
   160     if (priv->m_IpcSession)
       
   161         {
       
   162         result = priv->m_IpcSession->sendSync(requestType, data.toAscii());
       
   163         }
       
   164 
       
   165     if (result == true)
       
   166         {
       
   167         return ErrNone;
       
   168         }
       
   169     else
       
   170         {
       
   171         return ErrGeneral;
       
   172         }
       
   173     }
       
   174 
       
   175 /**==============================================================
       
   176  * Description: Sends add fetchbookmark request to the server
       
   177  * Author: Chandrashekar.V
       
   178  * EmpId:  10289207
       
   179  ================================================================*/
       
   180 BookmarkLeaf* BookmarksClientSide::FetchBookmark(
       
   181         QString title)
       
   182     {
       
   183     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   184     BookmarkLeaf *node = NULL;
       
   185     QString data;
       
   186     data.append(title);
       
   187     QString requestType;
       
   188     requestType.append(QString::number(EFetchBookmark));
       
   189     if (priv->m_IpcSession)
       
   190         {
       
   191         priv->m_IpcSession->sendSync(requestType, data.toAscii());
       
   192         QByteArray msg = priv->m_IpcSession->readAll();
       
   193         QString serverMsg;
       
   194         serverMsg = msg.data();
       
   195         QStringList list = serverMsg.split(BOOKMARKSENGINESEPARATOR);
       
   196 
       
   197         QString title = list[0];
       
   198         QString url = list[1];
       
   199         QString strdate = list[2];
       
   200         QString tags = list[3];
       
   201         QDate adate = QDate::fromString(strdate, "dd.MM.yyyy");
       
   202 
       
   203         node = new BookmarkLeaf();
       
   204         node->setTitle(title);
       
   205         node->setUrl(url);
       
   206         node->setDate(adate);
       
   207         node->setTag(tags);
       
   208         }
       
   209     return node;
       
   210     }
       
   211 
       
   212 /**==============================================================
       
   213  * Description: Sends add deletebookmark request to the server
       
   214  * Author: Chandrashekar.V
       
   215  * EmpId:  10289207
       
   216  ================================================================*/
       
   217 int BookmarksClientSide::DeleteBookmark(
       
   218         QString title)
       
   219     {
       
   220     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   221     QString data;
       
   222     bool result = false;
       
   223     data.append(title);
       
   224     QString requestType;
       
   225     requestType.append(QString::number(EDeleteBookmark));
       
   226     if (priv->m_IpcSession)
       
   227         {
       
   228         result = priv->m_IpcSession->sendSync(requestType, data.toAscii());
       
   229         }
       
   230     if (result == true)
       
   231         {
       
   232         return ErrNone;
       
   233         }
       
   234     else
       
   235         {
       
   236         return ErrGeneral;
       
   237         }
       
   238 
       
   239     }
       
   240 
       
   241 /**==============================================================
       
   242  * Description: gets session Id of the client
       
   243  * Author: Chandrashekar.V
       
   244  * EmpId:  10289207
       
   245  ================================================================*/
       
   246 int BookmarksClientSide::getSessionId(
       
   247         int& BookmarkId)
       
   248     {
       
   249     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   250     bool result = false;
       
   251     if (priv->m_IpcSession)
       
   252         {
       
   253         result = priv->m_IpcSession->getSessionId(BookmarkId);
       
   254         }
       
   255     if (result == true)
       
   256         {
       
   257         return ErrNone;
       
   258         }
       
   259     else
       
   260         {
       
   261         return ErrGeneral;
       
   262         }
       
   263     }
       
   264 
       
   265 /**==============================================================
       
   266  * Description: Sends add fetchAllbookmark request to the server
       
   267  * Author: Chandrashekar.V
       
   268  * EmpId:  10289207
       
   269  ================================================================*/
       
   270 QList<BookmarkLeaf*> BookmarksClientSide::FetchAllBookmarks()
       
   271     {
       
   272     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   273     QList<BookmarkLeaf*> nodeslist;
       
   274     QString data;
       
   275     data.append(NULL);
       
   276     QString requestType;
       
   277     requestType.append(QString::number(EFetchAllBookmark));
       
   278 
       
   279     if (priv->m_IpcSession)
       
   280         {
       
   281         priv->m_IpcSession->sendSync(requestType, data.toAscii());
       
   282         QByteArray msg = priv->m_IpcSession->readAll();
       
   283         QString serverMsg;
       
   284         serverMsg = msg.data();
       
   285         QStringList list = serverMsg.split(BOOKMARKSENGINESEPARATOR);
       
   286         int count = list.count() / 4;
       
   287         int i = 0;
       
   288         while (count--)
       
   289             {
       
   290             QString title = list[i++];
       
   291             QString url = list[i++];
       
   292             QString strdate = list[i++];
       
   293             QDate adate = QDate::fromString(strdate, "dd.MM.yyyy");
       
   294             QString tags = list[i++];
       
   295             BookmarkLeaf* node = new BookmarkLeaf();
       
   296             node->setTitle(title);
       
   297             node->setUrl(url);
       
   298             node->setDate(adate);
       
   299             node->setTag(tags);
       
   300 
       
   301             nodeslist.append(node);
       
   302             }
       
   303         }
       
   304     return nodeslist;
       
   305 
       
   306     }
       
   307 
       
   308 /**==============================================================
       
   309  * Description: handles server responses
       
   310  * Author: Chandrashekar.V
       
   311  * EmpId:  10289207
       
   312  ================================================================*/
       
   313 void BookmarksClientSide::handleServerResponse()
       
   314     {
       
   315     BOOKMARKSCLIENT_PRIVATEPTR(BookmarksClientSide);
       
   316     qDebug() << " handling server response" << '\n';
       
   317     QByteArray msg;
       
   318     msg = priv->m_IpcSession->readAll();
       
   319     QString serverMsg;
       
   320     serverMsg = msg.data();
       
   321     qDebug() << " Message is " << serverMsg << '\n';
       
   322     QStringList list1 = serverMsg.split(BOOKMARKSENGINESEPARATOR);
       
   323     int msgType = list1[0].toLocal8Bit().toInt();
       
   324     QString title = list1[1];
       
   325     int res=priv->dispatchServerMessagestoClient(msgType, title);
       
   326 
       
   327     }
       
   328 
       
   329 /**==============================================================
       
   330  * Description: Sends server response notifications to the UI
       
   331  * Author: Chandrashekar.V
       
   332  * EmpId:  10289207
       
   333  ================================================================*/
       
   334 int BookmarksClientSidePrivate::dispatchServerMessagestoClient(int msgType,
       
   335         QString title)
       
   336     {
       
   337     BOOKMARKSCLIENT_PUBLICPTR(BookmarksClientSide);
       
   338     switch (msgType)
       
   339         {
       
   340         case EServerMsgBookmarkDeleted:
       
   341             emit pub->BookmarkDeleted(title);
       
   342             break;
       
   343         case EServerMsgBookmarkAdded:
       
   344             emit pub->BookmarkAdded(title);
       
   345             break;
       
   346         default:
       
   347             break;
       
   348         }
       
   349     return ErrNone;
       
   350     }
       
   351