searchui/onlinehandler/src/onlinehandler.cpp
changeset 12 993ab30e92fc
child 26 367228f82b66
equal deleted inserted replaced
11:773be20e0a25 12:993ab30e92fc
       
     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:  Online Handler.
       
    15  *
       
    16  */
       
    17 #include "onlinehandler.h"
       
    18 #include "searchutility.h"
       
    19 #include "onlinesearchplugin.h"  // after export should be included as systeminc
       
    20 #include "onlinesearchpluginfactory.h"  // after export should be included as systeminc
       
    21 #include <QDebug>
       
    22 #include <QPluginLoader>
       
    23 #ifdef Q_OS_SYMBIAN
       
    24 #include <QLibraryInfo>
       
    25 #else
       
    26 #include <QCoreApplication>
       
    27 #endif
       
    28 #include <QDir>
       
    29 #include <hbmainwindow.h>
       
    30 #include <hbview.h>
       
    31 #include <hbinstance.h>
       
    32 #include <hbicon.h>
       
    33 #include <serviceprovider.h>
       
    34 #include <isengine.h>
       
    35 
       
    36 #ifdef WIN32
       
    37 #define PLUGINFILE_SUFFIX "dll"
       
    38 #else
       
    39 #define PLUGINFILE_SUFFIX "qtplugin"
       
    40 #endif
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // OnlineHandler::OnlineHandler
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 OnlineHandler::OnlineHandler(QObject* parent) :
       
    47     QObject(parent), mOnlinePlugin(NULL), mEng(NULL), mCurrentProvider(NULL)
       
    48     {
       
    49     }
       
    50 // ---------------------------------------------------------------------------
       
    51 // OnlineHandler::~OnlineHandler
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 OnlineHandler::~OnlineHandler()
       
    55     {
       
    56     if (mOnlinePlugin)
       
    57         delete mOnlinePlugin;
       
    58     if (mEng)
       
    59         delete mEng;
       
    60     if (mCurrentProvider)
       
    61         delete mCurrentProvider;
       
    62     }
       
    63 void OnlineHandler::setDefaultProvider(const int id)
       
    64     {
       
    65     if (mEng)
       
    66         mEng->SetSelectedProvider(id);
       
    67     }
       
    68 // ---------------------------------------------------------------------------
       
    69 // OnlineHandler::readSettings
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void OnlineHandler::readSettings()
       
    73     {
       
    74     if (!mEng)
       
    75         mEng = new IsEngine();
       
    76     mAvailableProviders.clear();
       
    77     mEng->Providers(mAvailableProviders);
       
    78     for (int i = 0; i < mAvailableProviders.count(); i++)
       
    79         {
       
    80         emit providerDetails(mAvailableProviders.at(i).Name(),
       
    81                 mAvailableProviders.at(i).Icon(),
       
    82                 mAvailableProviders.at(i).Id());
       
    83         }
       
    84     mCurrentProvider = mEng->SelectedProvider(); // default provider
       
    85     if (mCurrentProvider)
       
    86         {
       
    87         mDefaultPluginName = mCurrentProvider->Name();
       
    88         emit defaultProvider(mCurrentProvider->Id());
       
    89         }
       
    90     else
       
    91         {
       
    92         mDefaultPluginName = QString();
       
    93         }
       
    94     }
       
    95 // ---------------------------------------------------------------------------
       
    96 // OnlineHandler::writeSettings
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void OnlineHandler::writeSettings(const int providerId)
       
   100     {
       
   101     if (mEng && mEng->SetSelectedProvider(providerId) && mCurrentProvider)
       
   102         {
       
   103         mCurrentProvider = mEng->SelectedProvider(); // default provider
       
   104         emit defaultProvider(mCurrentProvider->Id());
       
   105         }
       
   106     return;
       
   107     }
       
   108 // ---------------------------------------------------------------------------
       
   109 // OnlineHandler::initializePlugin
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void OnlineHandler::initializePlugin()
       
   113     {
       
   114     /* Below assumes, only default plugins are handled now. If user selects different plugin some more logic needed here. */
       
   115 
       
   116     if (mOnlinePlugin)
       
   117         {
       
   118         delete mOnlinePlugin;
       
   119         mOnlinePlugin = NULL;
       
   120         }
       
   121     mOnlinePlugin = loadSearchPlugin(mDefaultPluginName);
       
   122     if (mOnlinePlugin)
       
   123         {
       
   124         connect(mOnlinePlugin, SIGNAL(pluginInitialized(bool)), this,
       
   125                 SLOT(isPluginIntialized(bool)));
       
   126 
       
   127         connect(mOnlinePlugin, SIGNAL(shutdownCompleted(bool)), this,
       
   128                 SLOT(isShutdownCompleted(bool)));
       
   129 
       
   130         connect(mOnlinePlugin, SIGNAL(handleBackEvent()), this,
       
   131                 SLOT(backEvent()));
       
   132 
       
   133         mOnlinePlugin->initializePlugin();
       
   134         }
       
   135     }
       
   136 void OnlineHandler::loadISPlugin(int id, QString query)
       
   137     {
       
   138     if (!mEng)
       
   139         mEng = new IsEngine();
       
   140     mLoadSuggestion = true;
       
   141     mLoadSuggestionLink = query;
       
   142     QList<ServiceProvider> iAvailableProviders;
       
   143     mEng->Providers(iAvailableProviders);
       
   144     for (int i = 0; i < iAvailableProviders.count(); i++)
       
   145         {
       
   146         if (iAvailableProviders.at(i).Id() == id)
       
   147             {
       
   148             mDefaultPluginName = iAvailableProviders.at(i).Name();
       
   149             break;
       
   150             }
       
   151         }
       
   152     initializePlugin();
       
   153     }
       
   154 // ---------------------------------------------------------------------------
       
   155 // OnlineHandler::isPluginIntialized
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void OnlineHandler::isPluginIntialized(bool success)
       
   159     {
       
   160     if (mLoadSuggestion)
       
   161         {
       
   162         activatePlugin(mLoadSuggestionLink);
       
   163         mLoadSuggestion = false;
       
   164         }
       
   165     else
       
   166         {
       
   167         emit pluginIntialized(success);
       
   168         }
       
   169     }
       
   170 // ---------------------------------------------------------------------------
       
   171 // OnlineHandler::isShutdownCompleted
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void OnlineHandler::isShutdownCompleted(bool success)
       
   175     {
       
   176     mOnlinePlugin->deleteLater();
       
   177     emit pluginShutdown(success);
       
   178     }
       
   179 // ---------------------------------------------------------------------------
       
   180 // OnlineHandler::backEvent
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void OnlineHandler::backEvent()
       
   184     {
       
   185     emit backEventTriggered();
       
   186     }
       
   187 // ---------------------------------------------------------------------------
       
   188 // OnlineHandler::activatePlugin
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 bool OnlineHandler::activatePlugin()
       
   192     {
       
   193     if (mOnlinePlugin)
       
   194         mOnlinePlugin->activatePlugin();
       
   195     return true;
       
   196     }
       
   197 // ---------------------------------------------------------------------------
       
   198 // OnlineHandler::activatePlugin
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 bool OnlineHandler::activatePlugin(const QString &str)
       
   202     {
       
   203     if (mOnlinePlugin)
       
   204         mOnlinePlugin->activatePlugin(str);
       
   205     return true;
       
   206     }
       
   207 // ---------------------------------------------------------------------------
       
   208 // OnlineHandler::unloadPlugin
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void OnlineHandler::unloadPlugin()
       
   212     {
       
   213     if (mOnlinePlugin)
       
   214         {
       
   215         disconnect(mOnlinePlugin, SIGNAL(pluginInitialized(bool)), this,
       
   216                 SLOT(isPluginIntialized(bool)));
       
   217 
       
   218         disconnect(mOnlinePlugin, SIGNAL(shutdownCompleted(bool)), this,
       
   219                 SLOT(isShutdownCompleted(bool)));
       
   220 
       
   221         disconnect(mOnlinePlugin, SIGNAL(handleBackEvent()), this,
       
   222                 SLOT(backEvent()));
       
   223 
       
   224         mOnlinePlugin->deactivatePlugin();
       
   225         }
       
   226     }
       
   227 // ---------------------------------------------------------------------------
       
   228 // OnlineHandler::unloadPluginandExit
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void OnlineHandler::unloadPluginandExit()
       
   232     {
       
   233     if (mOnlinePlugin)
       
   234         {
       
   235         disconnect(mOnlinePlugin, SIGNAL(pluginInitialized(bool)), this,
       
   236                 SLOT(isPluginIntialized(bool)));
       
   237 
       
   238         disconnect(mOnlinePlugin, SIGNAL(shutdownCompleted(bool)), this,
       
   239                 SLOT(isShutdownCompleted(bool)));
       
   240 
       
   241         disconnect(mOnlinePlugin, SIGNAL(handleBackEvent()), this,
       
   242                 SLOT(backEvent()));
       
   243 
       
   244         mOnlinePlugin->deactivatePlugin();
       
   245         mOnlinePlugin->shutdownPlugin();
       
   246         }
       
   247     }
       
   248 // ---------------------------------------------------------------------------
       
   249 // OnlineHandler::loadSearchPlugin
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 OnlineSearchPlugin *OnlineHandler::loadSearchPlugin(const QString &pluginFile)
       
   253     {
       
   254     //   CpLogger logger;
       
   255     //   logger << "loading plugin:" << pluginFile << "\r\n";
       
   256 
       
   257     QFileInfo fileInfo(pluginFile);
       
   258 
       
   259     if (!fileInfo.isAbsolute())
       
   260         {
       
   261         QString fileName = fileInfo.fileName();
       
   262         if (fileInfo.suffix().compare(PLUGINFILE_SUFFIX, Qt::CaseInsensitive))
       
   263             {
       
   264             fileName = fileInfo.baseName() + '.' + PLUGINFILE_SUFFIX;
       
   265             }
       
   266 
       
   267         QStringList pluginDirs = SearchUtility::pluginDirs();
       
   268         foreach(const QString &pluginDir,pluginDirs)
       
   269                 {
       
   270                 fileInfo.setFile(pluginDir + fileName);
       
   271                 if (fileInfo.exists() && QLibrary::isLibrary(
       
   272                         fileInfo.absoluteFilePath()))
       
   273                     {
       
   274                     break;
       
   275                     }
       
   276                 }
       
   277         }
       
   278 
       
   279     QPluginLoader loader(fileInfo.absoluteFilePath());
       
   280     OnlineSearchPluginFactory *pluginFactory = qobject_cast<
       
   281             OnlineSearchPluginFactory*> (loader.instance());
       
   282     OnlineSearchPlugin *plugin = pluginFactory->createPlugin();
       
   283     /*
       
   284      * why unload?
       
   285      if (!plugin) {
       
   286      loader.unload();
       
   287      }
       
   288      */
       
   289     //   logger << (plugin ? "load plugin succeed." : "load plugin failed.") << "\r\n";    
       
   290     return plugin;
       
   291     }
       
   292