smf/smfservermodule/smfclient/client/smfcontactfetcher_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 #include "smfcontactfetcher_p.h"
       
    16 #ifdef WRITE_LOG
       
    17 #include <QFile>
       
    18 #include <QTextstream>
       
    19 #endif
       
    20 #ifdef Q_OS_SYMBIAN
       
    21 #include "SmfClientSymbian.h"
       
    22 #else
       
    23 #include "SmfClientQt.h"
       
    24 #endif
       
    25  /**
       
    26    * Constructs the SmfContactFetcher.
       
    27    * @param parent base provider info
       
    28    * @param contact Used for searching friends of the given contact
       
    29    * Seeing as this is a plug-in implementation, these will realistically
       
    30    * be generated by SMF factory of some kind
       
    31    */
       
    32   SmfContactFetcherPrivate::SmfContactFetcherPrivate(SmfContactFetcher* contactFetcher)
       
    33   : m_contactFetcher(contactFetcher),m_contactList(0)
       
    34 	  {
       
    35 		//private impl for symbian
       
    36 		#ifdef Q_OS_SYMBIAN
       
    37 		m_SmfClientPrivate = CSmfClientSymbian::NewL(this);
       
    38 		#endif
       
    39 	  }
       
    40   SmfContactFetcherPrivate::~SmfContactFetcherPrivate()
       
    41 	  {
       
    42 	  if(m_SmfClientPrivate)
       
    43 		  {
       
    44 		  delete m_SmfClientPrivate;
       
    45 		  m_SmfClientPrivate = NULL;
       
    46 		  }
       
    47 	  }
       
    48 
       
    49   /**
       
    50    * Get the friend listing asynchronously. The friendsListAvailable() signal
       
    51    * is emitted with SmfContactList once data is arrived.
       
    52    * When the list is big user can specify the page number and per page item data.
       
    53    * If not supplied by the user default values are used.
       
    54    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
    55    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
    56    */
       
    57   bool  SmfContactFetcherPrivate::friends(int pageNum,int perPage)
       
    58 	  {
       
    59 	  m_providerSerialized.clear();
       
    60 	  //We need to pass Opcode and SmfProvider serialized into bytearray 
       
    61 	  m_xtraInfoFlag = 0;
       
    62 	  m_pageInfoFlag = 1;
       
    63 	  m_baseProvider = m_contactFetcher->getProvider();
       
    64 	  //serialize start
       
    65 	  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
    66 	  write<<*(m_baseProvider);
       
    67 	  write<<m_pageInfoFlag;
       
    68 	  write<<pageNum;
       
    69 	  write<<perPage;
       
    70 	  write<<m_xtraInfoFlag;
       
    71 	  //serialize end
       
    72 	  QString intfName(contactFetcherInterface);
       
    73 	  int maxAllocation = MaxSmfContactSize*perPage;
       
    74 	  //call private impl's send method
       
    75 	  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactGetFriends,maxAllocation,QByteArray());
       
    76 	  }
       
    77 
       
    78   /**
       
    79    * Get the list of followers asynchronously. The followersListAvailable() signal
       
    80    * is emitted with SmfContactList once data is arrived. Please note that some
       
    81    * service may not support followers/fans - FALSE is returned if not supported.
       
    82    * When the list is big user can specify the page number and per page item data.
       
    83    * If not supplied by the user default values are used.
       
    84    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
    85    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
    86    */
       
    87   bool SmfContactFetcherPrivate::followers(int pageNum,int perPage)
       
    88 	  {
       
    89 	  //We need to pass Opcode and SmfProvider serialized into bytearray 
       
    90 	  
       
    91 	  m_baseProvider = m_contactFetcher->getProvider();
       
    92 	  //serialize start
       
    93 	  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
    94 	  write<<*(m_baseProvider);
       
    95 	  m_pageInfoFlag = 1;
       
    96 	  write<<m_pageInfoFlag;
       
    97 	  write<<pageNum;
       
    98 	  write<<perPage;
       
    99 	  m_xtraInfoFlag = 0;
       
   100 	  write<<m_xtraInfoFlag;
       
   101 	  //serialize end
       
   102 	  
       
   103 	  
       
   104 	  QString intfName(contactFetcherInterface);
       
   105 	  int maxAllocation = MaxSmfContactSize*perPage;
       
   106 	  //call private impl's send method
       
   107 	  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactGetFollowers,maxAllocation);
       
   108 	  }
       
   109   /**
       
   110    * Searches for a contact The searchContactFinished() signal
       
   111    * is emitted with SmfContactList once data is arrived.
       
   112    * When the list is big user can specify the page number and per page item data.
       
   113    * If not supplied by the user default values are used.
       
   114    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
   115    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
   116    */
       
   117   void  SmfContactFetcherPrivate::search(SmfContact* contact,int pageNum,int perPage) 
       
   118 	  {
       
   119 	  //We need to pass Opcode and SmfProvider+SmfContact serialized into bytearray 
       
   120 	  
       
   121 	  m_baseProvider = m_contactFetcher->getProvider();
       
   122 	  m_xtraInfoSerialized.clear();
       
   123 	  m_contact = contact;
       
   124 	  //serialize start
       
   125 	  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
   126 	  write<<*(m_baseProvider);
       
   127 	  m_pageInfoFlag = 1;
       
   128 	  write<<m_pageInfoFlag;
       
   129 	  write<<pageNum;
       
   130 	  write<<pageNum;
       
   131 
       
   132 	  
       
   133 	  //serialize xtra info
       
   134 	  QDataStream writeXtra(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   135 	  writeXtra<<*(m_contact);
       
   136 	  
       
   137 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   138 	  write<<m_xtraInfoFlag;
       
   139 	  
       
   140 	  QString intfName(contactFetcherInterface);
       
   141 	  int maxAllocation = MaxSmfContactSize*perPage;
       
   142 	  //call private impl's send method
       
   143 	  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactSearch,maxAllocation,m_xtraInfoSerialized);
       
   144 	  }
       
   145 
       
   146   /**
       
   147    * Searches for a contacts (friends) who are near the user.
       
   148    * Signal searchNearFinished() is emitted with SmfContactList once data is arrived.
       
   149    * Proximity defines accuracy level
       
   150    * When the list is big user can specify the page number and per page item data.
       
   151    * If not supplied by the user default values are used.
       
   152    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
   153    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
   154    */
       
   155    bool  SmfContactFetcherPrivate::searchNear(SmfLocation* location,SmfLocationSearchBoundary proximity,int pageNum,int perPage) 
       
   156 	   {
       
   157 		  m_baseProvider = m_contactFetcher->getProvider();
       
   158 		  m_xtraInfoSerialized.clear();
       
   159 		  //serialize start
       
   160 		  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
   161 		  write<<*(m_baseProvider);
       
   162 		  m_pageInfoFlag = 1;
       
   163 		  write<<m_pageInfoFlag;
       
   164 		  write<<pageNum;
       
   165 		  write<<pageNum;
       
   166 		  //m_xtraInfoFlag = 1;
       
   167 		  //write<<m_xtraInfoFlag;
       
   168 		  
       
   169 		  //serialize xtra info
       
   170 		  QDataStream writeXtra(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   171 		  writeXtra<<*(location);
       
   172 		  writeXtra<<proximity;
       
   173 		  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   174 		  write<<m_xtraInfoFlag;
       
   175 		  QString intfName(contactFetcherInterface);
       
   176 		  int maxAllocation = MaxSmfContactSize*perPage;
       
   177 		  //call private impl's send method
       
   178 		  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactSearchNear,maxAllocation,m_xtraInfoSerialized);
       
   179 	   }
       
   180 
       
   181 
       
   182   /**
       
   183    * Get the list of groups. The groupListAvailable() signal
       
   184    * is emitted with SmfGroupList once data is arrived. False might be returned
       
   185    * if this service doesn't support any mode of grouping (very rare).
       
   186    * When the list is big user can specify the page number and per page item data.
       
   187    * If not supplied by the user default values are used.
       
   188    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
   189    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
   190    */
       
   191    bool SmfContactFetcherPrivate::groups(int pageNum,int perPage) 
       
   192 	   {
       
   193 		  //We need to pass Opcode and SmfProvider serialized into bytearray 
       
   194 		  
       
   195 		  m_baseProvider = m_contactFetcher->getProvider();
       
   196 		  //serialize start
       
   197 		  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
   198 		  write<<*(m_baseProvider);
       
   199 		  m_pageInfoFlag = 1;
       
   200 		  write<<m_pageInfoFlag;
       
   201 		  write<<pageNum;
       
   202 		  write<<perPage;
       
   203 		  m_xtraInfoFlag = 0;
       
   204 		  write<<m_xtraInfoFlag;
       
   205 		  //serialize end
       
   206 		  
       
   207 		  
       
   208 		  QString intfName(contactFetcherInterface);
       
   209 		  int maxAllocation = MaxSmfGroupSize*perPage;
       
   210 		  //call private impl's send method
       
   211 		  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactGetGroups,maxAllocation);
       
   212 	   }
       
   213 
       
   214   /**
       
   215    * Searches for Smf Contacts in an Smf group.
       
   216    * When the list is big user can specify the page number and per page item data.
       
   217    * If not supplied by the user default values are used.
       
   218    * @param pageNum Page number to download, SMF_FIRST_PAGE denotes fresh query.
       
   219    * @param perPage Item per page, default is SMF_ITEMS_PER_PAGE
       
   220    * @param  group The group to be searcged in
       
   221    */
       
   222 bool  SmfContactFetcherPrivate::searchInGroup(SmfGroup group,int pageNum,int perPage) 
       
   223    {
       
   224 	  //We need to pass Opcode and SmfProvider+SmfGroup serialized into bytearray 
       
   225 	  
       
   226 	  m_baseProvider = m_contactFetcher->getProvider();
       
   227 	  m_grp = group;
       
   228 	  //serialize start
       
   229 	  QDataStream write(&m_providerSerialized,QIODevice::WriteOnly);
       
   230 	  write<<*(m_baseProvider);
       
   231 	  m_pageInfoFlag = 1;
       
   232 	  write<<m_pageInfoFlag;
       
   233 	  write<<pageNum;
       
   234 	  write<<perPage;
       
   235 	  //m_xtraInfoFlag = 1;
       
   236 	  //write<<m_xtraInfoFlag;
       
   237 	  
       
   238 	  //serialize xtra info
       
   239 	  QDataStream writeXtra(&m_xtraInfoSerialized,QIODevice::WriteOnly);
       
   240 	  writeXtra<<m_grp;
       
   241 	  m_xtraInfoFlag = m_xtraInfoSerialized.size();
       
   242 	  write<<m_xtraInfoFlag;
       
   243 	  //serialize end
       
   244 	  
       
   245 	  
       
   246 	  QString intfName(contactFetcherInterface);
       
   247 	  int maxAllocation = MaxSmfContactSize*perPage;
       
   248 	  //call private impl's send method
       
   249 	  m_SmfClientPrivate->sendRequest(m_providerSerialized,intfName,SmfContactSearchInGroup,maxAllocation);
       
   250 	  return 0;
       
   251    }
       
   252 void SmfContactFetcherPrivate::resultsAvailable(QByteArray result,SmfRequestTypeID opcode,SmfError error)
       
   253    	{
       
   254 	writeLog("SmfContactFetcherPrivate::resultsAvailable");
       
   255 	
       
   256    	QDataStream reader(&result,QIODevice::ReadOnly);
       
   257    	
       
   258    	//Now de-serialize it based on opcode
       
   259    	switch(opcode)
       
   260    		{
       
   261    		case SmfContactGetFriends:
       
   262    			{
       
   263 
       
   264    			if(m_contactList)
       
   265    				{
       
   266 				delete m_contactList;
       
   267 				m_contactList = NULL;
       
   268    				}
       
   269    			m_contactList = new SmfContactList;
       
   270    			m_frndist.clear();
       
   271    			reader>>m_frndist;
       
   272    			writeLog("m_frndist.count=");
       
   273    			writeLog(QString::number(m_frndist.count()));
       
   274    			//TODO:-After consulting with PM owner decide page serialization
       
   275    			SmfResultPage page;
       
   276 
       
   277    			emit m_contactFetcher->friendsListAvailable(&m_frndist,error,page);
       
   278    			
       
   279    			}
       
   280    			break;
       
   281    		case SmfContactGetFollowers:
       
   282    			{
       
   283    			if(m_contactList)
       
   284    				{
       
   285 				delete m_contactList;
       
   286 				m_contactList = NULL;
       
   287    				}
       
   288    			m_contactList = new SmfContactList;
       
   289    			reader>>*(m_contactList);
       
   290    			//TODO:-After consulting with PM owner decide page serialization
       
   291    			SmfResultPage page;
       
   292 
       
   293    			emit m_contactFetcher->followersListAvailable(m_contactList,error,page);
       
   294 			
       
   295    			}
       
   296    			break;
       
   297    		case SmfContactGetGroups:
       
   298    			{
       
   299    			writeLog("Before m_grpList.clear=");
       
   300    			m_grpList.clear();
       
   301    			writeLog("Before reader>>m_grpList=");
       
   302    			reader>>m_grpList ;
       
   303    			/** @TODO:-After consulting with PM owner decide page serialization */
       
   304    			SmfResultPage page;
       
   305    			writeLog("m_grpList.count=");
       
   306    			writeLog(QString::number(m_grpList.count()));
       
   307    			emit m_contactFetcher->groupListAvailable(&m_grpList,error,page);
       
   308    			
       
   309    			}
       
   310    			break;
       
   311    		case SmfContactSearch:
       
   312    			{
       
   313    			if(m_contactList)
       
   314    				{
       
   315 				delete m_contactList;
       
   316 				m_contactList = NULL;
       
   317    				}
       
   318    			m_contactList = new SmfContactList;
       
   319    			reader>>*(m_contactList);
       
   320    			/** @TODO:-After consulting with PM owner decide page serialization */
       
   321    			SmfResultPage page;
       
   322    			//searchContactFinished
       
   323    			emit m_contactFetcher->searchContactFinished(m_contactList,error,page);
       
   324    			}
       
   325    			break;
       
   326    		case SmfContactSearchNear:
       
   327    			{
       
   328    			if(m_contactList)
       
   329    				{
       
   330 				delete m_contactList;
       
   331 				m_contactList = NULL;
       
   332    				}
       
   333    			m_contactList = new SmfContactList;
       
   334    			reader>>*(m_contactList);
       
   335    			/** @TODO:-After consulting with PM owner decide page serialization*/
       
   336    			SmfResultPage page;
       
   337    			//searchContactFinished
       
   338    			emit m_contactFetcher->searchNearFinished(m_contactList,error,page);
       
   339    			}
       
   340    			break;
       
   341    		case SmfContactSearchInGroup:
       
   342    			{
       
   343    			if(m_contactList)
       
   344    				{
       
   345 				delete m_contactList;
       
   346 				m_contactList = NULL;
       
   347    				}
       
   348    			m_contactList = new SmfContactList;
       
   349    			reader>>*(m_contactList);
       
   350    			//TODO:-After consulting with PM owner decide page serialization
       
   351    			SmfResultPage page;
       
   352    			//searchContactFinished
       
   353    			emit m_contactFetcher->searchInGroupFinished(m_contactList,error,page);
       
   354    			}
       
   355    			break;
       
   356    		default:
       
   357    			writeLog("!!!!!!!!!!default!!!!!!!!!!!!!!");
       
   358    		}
       
   359    	
       
   360    	}
       
   361 void SmfContactFetcherPrivate::writeLog(QString log) const
       
   362 	{
       
   363 #ifdef WRITE_LOG
       
   364 	QFile file("c:\\data\\SmfClientLogs.txt");
       
   365     if (!file.open(QIODevice::Append | QIODevice::Text))
       
   366 	         return;
       
   367     QTextStream out(&file);
       
   368     out << log << "\n";
       
   369     file.close();
       
   370 #endif
       
   371 	}
       
   372 QDataStream &operator<<( QDataStream &aDataStream, 
       
   373 		const SmfError &err )
       
   374 	{
       
   375 	quint32 errInt = (quint32)err;
       
   376 	aDataStream<<errInt;
       
   377 	return aDataStream;
       
   378 	}
       
   379 
       
   380 
       
   381 QDataStream &operator>>( QDataStream &aDataStream, 
       
   382 		SmfError &err )
       
   383 	{
       
   384 	quint32 errInt;
       
   385 	aDataStream>>errInt;
       
   386 	err = (SmfError)errInt;
       
   387 	return aDataStream;
       
   388 	}