smf/smfservermodule/smfserver/pluginmgr/smfpluginmanager.h
changeset 7 be09cf1f39dd
equal deleted inserted replaced
6:c39a6cfd1fb9 7:be09cf1f39dd
       
     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  * Description:
       
    16  * The Plugin Manager class manages the loading and unloading of plug-ins
       
    17  *
       
    18  */
       
    19 
       
    20 #ifndef SMFPLUGINMANAGER_H_
       
    21 #define SMFPLUGINMANAGER_H_
       
    22 
       
    23 #include <QtSql>
       
    24 #include <smfglobal.h>
       
    25 #include <smfserverglobal.h>
       
    26 #include "smfserver.h"
       
    27 
       
    28 // Forward declaration
       
    29 class SmfPluginManagerUtil;
       
    30 class SmfTransportManagerUtil;
       
    31 class QFileSystemWatcher;
       
    32 class QPluginLoader;
       
    33 class SmfPluginBase;
       
    34 class SmfProvider;
       
    35 
       
    36 /**
       
    37  * Structure that holds information about a plugin that is awaiting a 
       
    38  * response from Transport Manager
       
    39  */
       
    40 struct SmfWaitingPluginInfoStruc
       
    41 	{
       
    42 	/**
       
    43 	 * The server session ID for this request
       
    44 	 */
       
    45 	quint32 iSessionID;
       
    46 	/**
       
    47 	 * The plugin Id who creates this request
       
    48 	 */
       
    49 	QString iPluginId;
       
    50 	
       
    51 	/**
       
    52 	 * The instance of the loaded plugin
       
    53 	 */
       
    54 	SmfPluginBase *iInstance;
       
    55 	
       
    56 	/**
       
    57 	 * The type of operation requested by the plugin
       
    58 	 * @see smfglobal.h
       
    59 	 */
       
    60 	SmfRequestTypeID iOperation;
       
    61 	
       
    62 	/**
       
    63 	 * The input data for this request (if request need to be send again)
       
    64 	 */
       
    65 	QByteArray iInputData;
       
    66 	
       
    67 	/**
       
    68 	 * The list of valid Urls accessible for this plugin
       
    69 	 */
       
    70 	QList<QUrl> iUrlList;
       
    71 
       
    72 	};
       
    73 
       
    74 
       
    75 /**
       
    76  * The Plugin Manager class manages the loading and unloading of plug-ins
       
    77  */
       
    78 class SmfPluginManager : public QObject
       
    79 	{
       
    80 	Q_OBJECT
       
    81 	
       
    82 public:
       
    83 	/**
       
    84 	 * Method to get the instance of SmfPluginManager class
       
    85 	 * @param aServer The Smf server instance
       
    86 	 * @return The instance of SmfPluginManager class
       
    87 	 */
       
    88 	static SmfPluginManager* getInstance ( SmfServer *aServer = NULL );
       
    89 	
       
    90 	/**
       
    91 	 * Debugging only
       
    92 	 */
       
    93 	SmfServer* server ( ) { return m_server; }
       
    94 	
       
    95 	/**
       
    96 	 * Destructor
       
    97 	 */
       
    98 	~SmfPluginManager ( );
       
    99 	
       
   100 public:
       
   101 	/**
       
   102 	 * Method called by Smf server to create a web query.
       
   103 	 * Plugin Manager calls the appropriate web query creation method 
       
   104 	 * using the aOperation and aInputData parameters. Once the web request 
       
   105 	 * is ready, it calls the appropriate methods exposed by the Transport 
       
   106 	 * Manager to send the network request.
       
   107 	 * @param aSessionID The session ID provided by Smf Server
       
   108 	 * @param aPluginID The plugin ID that need to perform this operation
       
   109 	 * @param aOperation The type of operation to be performed
       
   110 	 * @param aInputData The data required to create the web query
       
   111 	 * @return SmfPluginManagerResult The result of the operation
       
   112 	 * @see smfglobal.h
       
   113 	 */
       
   114 	SmfPluginManagerResult createRequest ( const quint32& aSessionID, 
       
   115 			const QString& aPluginID, 
       
   116 			const SmfRequestTypeID& aOperation, 
       
   117 			QByteArray& aInputData );
       
   118 	
       
   119 	/**
       
   120 	 * Method called by Transport Manager when network response 
       
   121 	 * is available
       
   122 	 * @param aTransportResult The result of Transport Operation
       
   123 	 * @param aReply The QNetworkReply instance that requested 
       
   124 	 * this transaction
       
   125 	 * @param aResponse The network response data
       
   126 	 */
       
   127 	void responseAvailable ( const SmfTransportResult &aTransportResult, 
       
   128 			QNetworkReply *aReply,
       
   129 			QByteArray *aResponse );
       
   130 	
       
   131 	/**
       
   132 	 * Method to cancel the service request
       
   133 	 * @param aPluginId The plugin whose current operation 
       
   134 	 * is to be cancelled
       
   135 	 */
       
   136 	bool cancelRequest ( const QString& aPluginId );
       
   137 	
       
   138 	/**
       
   139 	 * Method to get the list of the SmfProvider for all the plugins that implement 
       
   140 	 * the mentioned Interface 
       
   141 	 * @param aInterface The interface for which list of plugins is required 
       
   142 	 * @param aMap The map of pluginID and its corresponding SmfProvider
       
   143 	 */
       
   144 	void getPlugins ( const QString& aInterface, 
       
   145 			QMap<QString,SmfProvider>& aMap );
       
   146 	
       
   147 	/**
       
   148 	 * Method to get the pluginID for the mentioned interface and service provider 
       
   149 	 * @param aInterface The interface implemented by the plugin
       
   150 	 * @param aProv The plugin's service provider
       
   151 	 * @param aPluginId The required pluginID
       
   152 	 */
       
   153 	void getPluginId ( const QString& aInterface, 
       
   154 			const SmfProvider& aProv, QString& aPluginId );
       
   155 	
       
   156 private:
       
   157 	/**
       
   158 	 * Method called to initialize the file watcher watching the file  
       
   159 	 * system for adition/upgradation/removal of plugins
       
   160 	 */
       
   161 	void initializeFileWatcher ( );
       
   162 	
       
   163 	/**
       
   164 	 * Method called to initialize the database holding the plugin 
       
   165 	 * directory sructure information. This is called only once when 
       
   166 	 * the Plugin Manager is instantiated.
       
   167 	 * This method creates and updates iPluginIdPathHash member 
       
   168 	 * of this class
       
   169 	 * @return Retuns true if success else false
       
   170 	 */
       
   171 	bool initializeSmfPluginDataBase ( );
       
   172 	
       
   173 	/**
       
   174 	 * Method to load a plugin using its Plugin Id.
       
   175 	 * @param aPluginId The unique ID of the plugin 
       
   176 	 * @param aLoadResult [out] Output paramater indicating the result 
       
   177 	 * of the loading
       
   178 	 * @return The instance of the loaded plugin if loaded, else NULL
       
   179 	 */
       
   180 	QObject* load ( const QString &aPluginId, 
       
   181 			SmfPluginManagerResult &aLoadResult);
       
   182 	
       
   183 	/**
       
   184 	 * Method to unload a loaded plugin. Returns true if success, else 
       
   185 	 * returns false.
       
   186 	 * @param aPlugin The plugin instance to be unloaded
       
   187 	 * @return Returns true if success, else returns false
       
   188 	 */
       
   189 	bool unload ( SmfPluginBase *aPlugin ); 
       
   190 	
       
   191 	/**
       
   192 	 * Method to unload the list of loaded plugins. Returns true if all are 
       
   193 	 * success, else returns false if any one fails.
       
   194 	 * @param aPluginList The list of instances for all plugins that are 
       
   195 	 * to be unloaded
       
   196 	 * @return Returns true if all are success, else returns false if any 
       
   197 	 * one fails.
       
   198 	 */
       
   199 	bool unload ( const QList<SmfPluginBase*> &aPluginList);
       
   200 	
       
   201 	/**
       
   202 	 * Method that calls the Transport Manager Utility class method to 
       
   203 	 * send the request created by the plugins over the network
       
   204 	 * @param aReqData The request data created by the plugin
       
   205 	 * @param aResult [out] The output parameter indicating the result 
       
   206 	 * of this method
       
   207 	 * @param aUrlList The list of accessible Urls for this plugin
       
   208 	 * @see smfglobal.h
       
   209 	 */
       
   210 	void sendRequest ( SmfPluginRequestData &aReqData, 
       
   211 			SmfPluginManagerResult &aResult,
       
   212 			const QList<QUrl> &aUrlList );
       
   213 	
       
   214 	/**
       
   215 	 * Method that checks if a plugin is authorised to make a request. 
       
   216 	 * This method communicates with Credential and Settings Manager 
       
   217 	 * through Smf server, giving the registration token and getting 
       
   218 	 * the valid url list if available for this plugin.
       
   219 	 * @param aRegToken The registration token given by the plugin
       
   220 	 * @param aUrlList [out] The list of Urls that the plugin can send 
       
   221 	 * request to (to be filled by CSM)
       
   222 	 * @return Returns true if plugin is authorised, else returns false.
       
   223 	 */
       
   224 	bool authorisePlugin( const QString &aRegToken, 
       
   225 			QList<QUrl> &aUrlList );
       
   226 	
       
   227 	/**
       
   228 	 * Method to serialize the result of parsing (which is done by the 
       
   229 	 * plugins) to QByteArray to be sent to Smf server.
       
   230 	 * @param aOperation The type of operation to be performed
       
   231 	 * @param aResult The data to be serialized
       
   232 	 * @param aDataStream Stream to be written
       
   233 	 */
       
   234 	void serializeResult ( const SmfRequestTypeID &aOperation, 
       
   235 			QVariant* aResult,
       
   236 			QDataStream &aDataStream );
       
   237 	
       
   238 private slots:
       
   239 	/**
       
   240 	 * Method for the directoryChanged signal of QFileSystemWatcher.
       
   241 	 * This will update the iPluginHash member and also the Plugin 
       
   242 	 * Information List.
       
   243 	 * @param aPath The path of the directory that has changed
       
   244 	 */
       
   245 	void directoryChanged ( const QString &aPath );
       
   246 
       
   247 private:
       
   248 	/**
       
   249 	 * Constructor with default argument
       
   250 	 * @param aServer The Smf server instance
       
   251 	 */
       
   252 	SmfPluginManager ( SmfServer *aServer );
       
   253 	
       
   254 	/**
       
   255 	 * Copy Constructor
       
   256 	 * @param aOther The reference object
       
   257 	 */
       
   258 	SmfPluginManager ( const SmfPluginManager &aOther );
       
   259 	
       
   260 private:
       
   261 	/**
       
   262 	 * The single instance of SmfPluginManager
       
   263 	 */
       
   264 	static SmfPluginManager* m_myInstance;
       
   265 	
       
   266 	/**
       
   267 	 * Member variable that maps the plugin instances to their 
       
   268 	 * respective QPluginLoader instances (loaders may be multiple)
       
   269 	 */
       
   270 	QHash<SmfPluginBase*, QPluginLoader*> m_pluginLoaderHash;
       
   271 	
       
   272 	/**
       
   273 	 * Member variable that watches the file system for 
       
   274 	 * addition/upgradation/removal of plugins 
       
   275 	 */
       
   276 	QFileSystemWatcher *m_fileWatcher;
       
   277 	
       
   278 	/**
       
   279 	 * Member variable that is an instance of a structure that 
       
   280 	 * holds a loaded plugin information 
       
   281 	 */
       
   282 	SmfWaitingPluginInfoStruc *m_tempStruct;
       
   283 	
       
   284 	/**
       
   285 	 * Member variable that maps a QNetworkReply instance to 
       
   286 	 * its corresponding loaded plugin information structure  
       
   287 	 */
       
   288 	QHash<QNetworkReply*, SmfWaitingPluginInfoStruc*> m_waitingPluginHash;
       
   289 	
       
   290 	/**
       
   291 	 * Member variable that maps a pluginId to its path
       
   292 	 */
       
   293 	QHash<QString, QString> m_pluginIdPathHash;
       
   294 	
       
   295 	/**
       
   296 	 * Member variable that holds the instance of 
       
   297 	 * SmfServer class
       
   298 	 */
       
   299 	SmfServer *m_server;
       
   300 	
       
   301 	/**
       
   302 	 * Member variable that holds the instance of 
       
   303 	 * SmfTransportManagerUtil class
       
   304 	 */
       
   305 	SmfTransportManagerUtil *m_transMngrUtil;
       
   306 
       
   307 	/**
       
   308 	 * Member variable that holds the instance of 
       
   309 	 * SmfPluginManagerUtil class. SmfPluginManager is a friend of SmfPluginManagerUtil.
       
   310 	 */
       
   311 	SmfPluginManagerUtil *m_util;
       
   312 
       
   313 	/**
       
   314 	 * Member variable that holds QSqlDatabase instance.
       
   315 	 */
       
   316 	QSqlDatabase m_pluginDataBase;
       
   317 	
       
   318 	};
       
   319 
       
   320 #endif /* SMFPLUGINMANAGER_H_ */