smf/smfservermodule/smfclient/client/smfpostprovider_p.cpp
changeset 18 013a02bf2bb0
parent 17 106a4bfcb866
child 19 c412f0526c34
equal deleted inserted replaced
17:106a4bfcb866 18:013a02bf2bb0
     1 /**
       
     2  * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the "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  * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution
       
    11  *
       
    12  * Contributors:
       
    13  * Manasij Roy, Nalina Hariharan
       
    14  * 
       
    15  *
       
    16  */
       
    17 
       
    18 #include "smfpostprovider_p.h"
       
    19 //logging headers
       
    20 #ifdef WRITE_LOG
       
    21 #include <QTextStream>
       
    22 #include <QFile>
       
    23 #endif
       
    24 #ifdef Q_OS_SYMBIAN
       
    25 #include "smfclientsymbian.h"
       
    26 #else
       
    27 #include "SmfClientQt.h"
       
    28 #endif
       
    29 SmfPostProviderPrivate::SmfPostProviderPrivate(SmfPostProvider* postProvider)
       
    30 : m_postProvider(postProvider),m_postList(NULL)
       
    31 	{
       
    32 	//private impl for symbian
       
    33 	#ifdef Q_OS_SYMBIAN
       
    34 	m_SmfClientPrivate = CSmfClientSymbian::NewL(this);
       
    35 	#endif
       
    36 	}
       
    37 SmfPostProviderPrivate::~SmfPostProviderPrivate()
       
    38 	{
       
    39 	if(m_SmfClientPrivate)
       
    40 		{
       
    41 		delete m_SmfClientPrivate;
       
    42 		m_SmfClientPrivate = NULL;
       
    43 		}
       
    44 	}
       
    45 /**
       
    46  * Gets the posts asynchronously. The signal postsAvailable()with SmfPostList is emitted
       
    47  * once the post lists are available
       
    48  * @param user user's contact in this SP, omit for self contact
       
    49  * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
    50  * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE 
       
    51  * @see postsAvailable()
       
    52  */
       
    53 void SmfPostProviderPrivate::posts(SmfContact* user ,int pageNum,int perPage)
       
    54 {
       
    55 	  //We need to pass Opcode and SmfProvider+SmfContact
       
    56 	  // (when user is not NULL) serialized into bytearray 
       
    57 	  
       
    58 	  m_baseProvider = m_postProvider->getProvider();
       
    59 	  //serialize start
       
    60 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
    61 	  write<<*(m_baseProvider);
       
    62 	  m_pageInfoFlag = 1;
       
    63 	  write<<m_pageInfoFlag;
       
    64 	  write<<pageNum;
       
    65 	  write<<perPage;
       
    66 	  //TODO:- use different opcode incase post retreival is for other user
       
    67 	  //serialize SmfContact if post retrieval is for other user
       
    68 
       
    69 	  if(user)
       
    70 		  {
       
    71 		  //m_xtraInfoFlag = 1;
       
    72 		  //write<<m_xtraInfoFlag;
       
    73 		  //now serialize SmfContact 
       
    74 		  m_xtraInfoSerialized.clear();
       
    75 		  QDataStream Xtrawrite(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
    76 		  Xtrawrite<<*(user);
       
    77 		  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
    78 		  write<<m_xtraInfoFlag;
       
    79 		  }
       
    80 	  else
       
    81 		  {
       
    82 		  m_xtraInfoFlag = 0;
       
    83 		  write<<m_xtraInfoFlag;
       
    84 		  }
       
    85 	  //serialize end
       
    86 	  intfName.clear();
       
    87 	  intfName.append(postProviderInterface);
       
    88 	  int maxAllocation = MaxSmfPostSize*perPage;
       
    89 	  //call private impl's send method
       
    90 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactRetrievePosts,maxAllocation,m_xtraInfoSerialized);
       
    91 
       
    92 }
       
    93 	
       
    94 /**
       
    95  * Updates a post to own area, the success of the post can be checked with signal
       
    96  * updatePostFinished() signal
       
    97  * @param postData data to be posted
       
    98  * @param location location data
       
    99  */
       
   100 void SmfPostProviderPrivate::post(SmfPost& postData,SmfLocation& location) 
       
   101 	{
       
   102 	  m_xtraInfoSerialized.clear();
       
   103 	  m_baseProvider = m_postProvider->getProvider();
       
   104 	  //serialize start
       
   105 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   106 	  //SmfProvider
       
   107 	  write<<*(m_baseProvider);
       
   108 	  m_pageInfoFlag = 0;
       
   109 	  write<<m_pageInfoFlag;
       
   110 	  //m_xtraInfoFlag = 1;
       
   111 	  //write<<m_xtraInfoFlag;
       
   112 	  
       
   113 	  QDataStream xtraInfoWrite(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   114 	  //SmfPost
       
   115 	  xtraInfoWrite<<postData;
       
   116 	  //SmfPlace
       
   117 	  xtraInfoWrite<<location;
       
   118 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   119 	  write<<m_xtraInfoFlag;
       
   120 	  //serialize end
       
   121 	  QString intfName(postProviderInterface);
       
   122 	  //we are not mentioning the max size, as we'll receive only bool
       
   123 	  //call private impl's send method
       
   124 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactPost,10,m_xtraInfoSerialized);
       
   125 	}
       
   126 /**
       
   127  * Updates the last post to own area with new data, the success of the post can be checked with signal
       
   128  * updatePostFinished() signal
       
   129  * @param postData edited/new data to be posted
       
   130  * @param location location data
       
   131  */
       
   132 void SmfPostProviderPrivate::updatePost(SmfPost& postData)
       
   133 	{
       
   134 	  m_baseProvider = m_postProvider->getProvider();
       
   135 	  //serialize start
       
   136 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   137 	  //SmfProvider
       
   138 	  write<<*(m_baseProvider);
       
   139 	  m_pageInfoFlag = 0;
       
   140 	  write<<m_pageInfoFlag;
       
   141 	  //m_xtraInfoFlag = 1;
       
   142 	  //write<<m_xtraInfoFlag;
       
   143 	  
       
   144 	  QDataStream xtraInfoWrite(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   145 	  //SmfPost
       
   146 	  xtraInfoWrite<<postData;
       
   147 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   148 	  write<<m_xtraInfoFlag;
       
   149 	  //serialize end
       
   150 	  QString intfName(postProviderInterface);
       
   151 	  //we are not mentioning the max size, as we'll receive only bool
       
   152 	  //call private impl's send method
       
   153 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactUpdatePost,10,m_xtraInfoSerialized);
       
   154 	}
       
   155 
       
   156 /**
       
   157  * Updates a post to a particular Smf contact. the success of the post can be checked with signal
       
   158  * updatePostFinished() signal.
       
   159  * @param postData data to be posted
       
   160  * @param contact contact to which the post is to be directed
       
   161  * @param location location data
       
   162  */
       
   163 void SmfPostProviderPrivate::postDirected(SmfPost& postData,SmfContact& contact,SmfLocation* location)
       
   164 	{
       
   165 
       
   166 	  m_baseProvider = m_postProvider->getProvider();
       
   167 	  //serialize start
       
   168 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   169 	  //SmfProvider
       
   170 	  write<<*(m_baseProvider);
       
   171 	  m_pageInfoFlag = 0;
       
   172 	  write<<m_pageInfoFlag;
       
   173 	  //m_xtraInfoFlag = 1;
       
   174 	  //write<<m_xtraInfoFlag;
       
   175 	  
       
   176 	  QDataStream xtraInfoWrite(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   177 	  //SmfPost
       
   178 	  xtraInfoWrite<<postData;
       
   179 	  //SmfContact
       
   180 	  xtraInfoWrite<<contact;
       
   181 	  //SmfPlace
       
   182 	  xtraInfoWrite<<location;
       
   183 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   184 	  write<<m_xtraInfoFlag;
       
   185 	  //serialize end
       
   186 	  
       
   187 	  QString intfName(postProviderInterface);
       
   188 	  //we are not mentioning the max size, as we'll receive only bool
       
   189 	  //call private impl's send method
       
   190 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactPostDirected,10,m_xtraInfoSerialized);
       
   191 	}
       
   192 
       
   193 /**
       
   194  * Posts appearance info of the user.e.g. appear offline, busy, do-not-disturb
       
   195  * @param appearence user appearance
       
   196  * @see SmfPresenceInfo
       
   197  * @return False on Failure/Not supported 
       
   198  */
       
   199 //TODO:-How to return "supported" value? should it be synchronous?
       
   200 //Currently doing it asynchronously with the assumption of always supported
       
   201 //TODO:- implement some signal completion API
       
   202 bool SmfPostProviderPrivate::postAppearence(SmfAppearenceInfo appearence)
       
   203 	{
       
   204 	  m_baseProvider = m_postProvider->getProvider();
       
   205 	  //serialize start
       
   206 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   207 	  write<<*(m_baseProvider);
       
   208 	  m_pageInfoFlag = 0;
       
   209 	  write<<m_pageInfoFlag;
       
   210 	  //m_xtraInfoFlag = 1;
       
   211 	  //write<<m_xtraInfoFlag;
       
   212 	  QDataStream xtraInfoWrite(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   213 	  xtraInfoWrite<<appearence;
       
   214 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   215 	  write<<m_xtraInfoFlag;
       
   216 	  QString intfName(postProviderInterface);
       
   217 	  //we are not mentioning the max size, as we'll receive only bool
       
   218 	  //call private impl's send method
       
   219 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactPostAppearence,10,m_xtraInfoSerialized);
       
   220 	}
       
   221 /**
       
   222  * Share /a contact's post to user's friends and followers (e.g. retweet in twitter, share on facebook)
       
   223  * emits updatePostFinished() signal when done.
       
   224  * @param postData data to be posted
       
   225  * @param contact contact to which the post belonged
       
   226  * @param bool whether user changed items within the post
       
   227  */
       
   228 void SmfPostProviderPrivate::sharePost(SmfPost& postData,SmfContact& contact,bool edited)
       
   229 	{
       
   230 	  m_baseProvider = m_postProvider->getProvider();
       
   231 	  //serialize start
       
   232 	  QDataStream write(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   233 	  write<<*(m_baseProvider);
       
   234 	  m_pageInfoFlag = 0;
       
   235 	  write<<m_pageInfoFlag;
       
   236 	  //m_xtraInfoFlag = 1;
       
   237 	  //write<<m_xtraInfoFlag;
       
   238 	  
       
   239 	  QDataStream xtraInfoWrite(&m_serializedDataToServer,QIODevice::WriteOnly);
       
   240 	  xtraInfoWrite<<postData;
       
   241 	  xtraInfoWrite<<contact;
       
   242 	  xtraInfoWrite<<edited;
       
   243 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   244 	  write<<m_xtraInfoFlag;
       
   245 	  QString intfName(postProviderInterface);
       
   246 	  //we are not mentioning the max size, as we'll receive only bool
       
   247 	  //call private impl's send method
       
   248 	  m_SmfClientPrivate->sendRequest(m_serializedDataToServer,intfName,SmfContactSharePost,10,m_serializedDataToServer);
       
   249 	}
       
   250 /**
       
   251 * From smfobserver
       
   252 */
       
   253 void SmfPostProviderPrivate::resultsAvailable(QByteArray result,SmfRequestTypeID opcode,SmfError error)
       
   254    {
       
   255 	writeLog("SmfPostProviderPrivate::resultsAvailable");
       
   256 	writeLog("opcode=");
       
   257 	writeLog(QString::number(opcode));
       
   258 	QDataStream reader(&result,QIODevice::ReadOnly);
       
   259 	//Now de-serialize it based on opcode
       
   260 	switch(opcode)
       
   261 		{
       
   262 		case SmfContactRetrievePosts:
       
   263 			{
       
   264 				if(m_postList)
       
   265 					{
       
   266 					delete m_postList;
       
   267 					m_postList = NULL;
       
   268 					}
       
   269 
       
   270 				m_postList = new SmfPostList;
       
   271 				reader>>*(m_postList);
       
   272 				writeLog("postList.count=");
       
   273 				writeLog(QString::number(m_postList->count()));
       
   274 				foreach(SmfPost post,*m_postList)
       
   275 					{
       
   276 					writeLog(post.description());
       
   277 					}
       
   278 				//not incorporating paging now
       
   279 				SmfResultPage page;
       
   280 
       
   281 				emit m_postProvider->postsAvailable(m_postList,error,page);
       
   282 			
       
   283 			}
       
   284 			break;
       
   285 		case SmfContactPost:
       
   286 			{
       
   287 			int errInt;
       
   288 			reader>>errInt;
       
   289 			writeLog("success=");
       
   290 			SmfError success = (SmfError)errInt; 
       
   291 			emit m_postProvider->postFinished(success);
       
   292 			
       
   293 			}
       
   294 			break;
       
   295 		case SmfContactUpdatePost:
       
   296 		case SmfContactPostDirected:
       
   297 		case SmfContactSharePost:
       
   298 			{
       
   299 			int errInt;
       
   300 			reader>>errInt;
       
   301 			writeLog("success=");
       
   302 			SmfError success = (SmfError)errInt; 
       
   303 			emit m_postProvider->postFinished(success);
       
   304 			}
       
   305 		default:
       
   306 			Q_ASSERT_X(1,"SmfPostProviderPrivate::resultsAvailable","default case");
       
   307 		}
       
   308    }
       
   309 void SmfPostProviderPrivate::writeLog(QString log) const
       
   310 	{
       
   311 #ifdef WRITE_LOG
       
   312 	QFile file("c:\\data\\SmfClientLogs.txt");
       
   313     if (!file.open(QIODevice::Append | QIODevice::Text))
       
   314 	         return;
       
   315     QTextStream out(&file);
       
   316     out << log << "\n";
       
   317     file.close();
       
   318 #endif
       
   319 	}