browsercore/appfw/Api/Managers/hsbookmarkpublishclient.cpp
changeset 12 afcd8e6d025b
child 14 6aeb7a756187
equal deleted inserted replaced
11:786160610b4d 12:afcd8e6d025b
       
     1 /*
       
     2 * Copyright (c) 2010 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: Homescreen bookmark publishing client
       
    15 *
       
    16 */
       
    17 
       
    18 #include <qservicefilter.h>
       
    19 #include <qserviceinterfacedescriptor.h>
       
    20 
       
    21 #include <QMetaObject>
       
    22 #include <QMetaMethod>
       
    23 #include <QDir>
       
    24 #include <QUrl>
       
    25 #include <QIcon>
       
    26 #include <QWebSettings>
       
    27 #include <QCryptographicHash>
       
    28 #include <QTime>
       
    29 
       
    30 #include "bedrockprovisioning.h"
       
    31 #include "hsbookmarkpublishclient.h"
       
    32 
       
    33 namespace WRT {
       
    34 
       
    35 /*!
       
    36     \ingroup group_bookmark_publish_client
       
    37     \class HsBookmarkPublishClient
       
    38     \brief Implementation for homescreen bookmark publish client.
       
    39 
       
    40     HsBookmarkClient is derived from QObject and implements 
       
    41     needed functions for the homescreen bookmark publish client. 
       
    42 */
       
    43 
       
    44 /*!
       
    45     Constructor.
       
    46 */
       
    47 HsBookmarkPublishClient::HsBookmarkPublishClient(QObject *parent)
       
    48     : QObject(parent),
       
    49       mWidgetUri("hsbookmarkwidgetplugin")
       
    50 {
       
    51 	  setObjectName("hsBookmarkPublishClient");
       
    52 }
       
    53 
       
    54 /*!
       
    55     Destructor
       
    56 */
       
    57 HsBookmarkPublishClient::~HsBookmarkPublishClient()
       
    58 {
       
    59 }
       
    60 
       
    61 /*!
       
    62     Adds widget \a widgetUri utilizing service interface and invokeMethod call
       
    63 */
       
    64 int HsBookmarkPublishClient::addWidget(QString Title, QString Url)
       
    65 {   
       
    66     // find interface IHomeScreenClient from service hshomescreenclientplugin 
       
    67     QServiceManager manager;
       
    68     QServiceFilter filter("com.nokia.symbian.IHomeScreenClient");
       
    69     filter.setServiceName("hshomescreenclientplugin");
       
    70     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
       
    71     QVariantHash params;
       
    72     QString iconFileName;
       
    73     params["bookmarkTitle"] = Title;
       
    74     params["bookmarkUrl"] = Url;
       
    75     params["faviconPath"] = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("DataBaseDirectory");
       
    76 
       
    77     if(interfaces.isEmpty()) {
       
    78         QServiceManager::Error error = manager.error();
       
    79         return FAILURE;
       
    80     }
       
    81     
       
    82     // Retrieve the favicon and check its exsistance
       
    83     QIcon icon = QWebSettings::iconForUrl(Url);
       
    84     if (!icon.isNull())
       
    85         iconFileName = getIconFileName(Url);
       
    86         
       
    87     params["faviconFileName"] = iconFileName;
       
    88     
       
    89     if (!saveFavicon(Url, iconFileName))
       
    90         return FAILURE;
       
    91     
       
    92     QObject* service = manager.loadInterface(interfaces.first());
       
    93 
       
    94     // access service's addWidget function
       
    95     bool retVal = false;
       
    96     bool ret = QMetaObject::invokeMethod( 
       
    97         service, 
       
    98         "addWidget",
       
    99         Qt::DirectConnection,
       
   100         Q_RETURN_ARG(bool, retVal),
       
   101         Q_ARG(QString,mWidgetUri),
       
   102         Q_ARG(QVariantHash,params));   
       
   103         
       
   104     if(!ret){
       
   105         // invokeMethod returned error
       
   106         return FAILURE;
       
   107     }
       
   108     if(!retVal){
       
   109         // addWidget returned error
       
   110         return FAILURE;
       
   111     }
       
   112     
       
   113     return SUCCESS;
       
   114 }
       
   115 
       
   116 bool HsBookmarkPublishClient::saveFavicon(const QString Url, const QString iconFileName)
       
   117 {
       
   118     QIcon icon;
       
   119     QDir iconDir(BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("DataBaseDirectory"));
       
   120     QUrl url(Url);
       
   121     
       
   122     // Retrieve the favicon and check its exsistance
       
   123     icon = QWebSettings::iconForUrl(url);
       
   124     if (icon.isNull())
       
   125         return false;
       
   126 	
       
   127     QString iconPath = iconDir.filePath(iconFileName);
       
   128     QFile iconFile(iconPath);
       
   129     iconFile.open(QIODevice::WriteOnly);
       
   130     
       
   131     QPixmap pixmap = icon.pixmap(30, 30);
       
   132     pixmap.save(&iconFile, "PNG");
       
   133     
       
   134     iconFile.close();
       
   135     
       
   136     return true;
       
   137 }
       
   138 
       
   139 /*!
       
   140     \internal
       
   141 */
       
   142 QString HsBookmarkPublishClient::getIconFileName(QString url) const
       
   143 {
       
   144     QByteArray hash = QCryptographicHash::hash(url.toAscii(), QCryptographicHash::Md5);
       
   145     QString iconFile = hash.left(16).toHex();    
       
   146     return iconFile + ".png";
       
   147 }
       
   148 
       
   149 }