homescreenapp/widgetplugins/hsshortcutwidgetplugin/src/hsshortcutwidget.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     1 /*
       
     2 * Copyright (c) 2008 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: Home screen shortcut widget.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QGraphicsSceneMouseEvent>
       
    19 
       
    20 #include <HbStyleLoader>
       
    21 #include <HbFrameItem>
       
    22 #include <HbFrameDrawer>
       
    23 #include <HbIconItem>
       
    24 #include <HbTextItem>
       
    25 #include <HbInstantFeedback>
       
    26 #include <HbTapGesture>
       
    27 
       
    28 #include "hsshortcutwidget.h"
       
    29 #include "hsshortcutservice.h"
       
    30 #include "hsapp_defs.h"
       
    31 #include "caservice.h"
       
    32 #include "canotifier.h"
       
    33 #include "caquery.h"
       
    34 #include "hsconfiguration.h"
       
    35 
       
    36 /*!
       
    37     \class HsShortcutWidget
       
    38     \ingroup group_hsshortcutwidgetplugin
       
    39     \brief Implementation for the homescreen shortcut widget.
       
    40 
       
    41     Shortcut can be defined to launch different applications or applications
       
    42     with parameters, for example browser with certain url.
       
    43 */
       
    44 
       
    45 /*!
       
    46     Constructor.
       
    47 */
       
    48 HsShortcutWidget::HsShortcutWidget(QGraphicsItem *parent, Qt::WindowFlags flags)
       
    49   : HbWidget(parent, flags),
       
    50     mBackground(0), mIcon(0), mText(0),
       
    51     mCaEntryId(-1), mCaEntryRole(ItemEntryRole)
       
    52 {
       
    53     grabGesture(Qt::TapGesture);
       
    54 
       
    55     HbStyleLoader::registerFilePath(":/hsshortcutwidget.widgetml");
       
    56     HbStyleLoader::registerFilePath(":/hsshortcutwidget.css");
       
    57 
       
    58     createPrimitives();
       
    59 }
       
    60 
       
    61 /*!
       
    62     Destructor.
       
    63 */
       
    64 HsShortcutWidget::~HsShortcutWidget()
       
    65 {
       
    66     HbStyleLoader::unregisterFilePath(":/hsshortcutwidget.widgetml");
       
    67     HbStyleLoader::unregisterFilePath(":/hsshortcutwidget.css");
       
    68 }
       
    69 
       
    70 /*!
       
    71     Sets the menu content service id.
       
    72 */
       
    73 void HsShortcutWidget::setCaEntryId(int caEntryId)
       
    74 {
       
    75     mCaEntryId = caEntryId;
       
    76 }
       
    77 
       
    78 /*!
       
    79     Returns the menu content service id.
       
    80 */
       
    81 int HsShortcutWidget::caEntryId() const
       
    82 {
       
    83     return mCaEntryId;
       
    84 }
       
    85 
       
    86 /*!
       
    87     Sets the application's UID this shortcut points to.
       
    88 */
       
    89 void HsShortcutWidget::setUid(const QString &uid)
       
    90 {
       
    91     mUid = uid;
       
    92 }
       
    93 
       
    94 /*!
       
    95     Returns the application's UID this shortcut points to.
       
    96 */
       
    97 QString HsShortcutWidget::uid() const
       
    98 {
       
    99     return mUid;
       
   100 }
       
   101 
       
   102 /*!
       
   103     Returns the text property. This property is needed by css selector.
       
   104 */
       
   105 QString HsShortcutWidget::text() const
       
   106 {
       
   107     if ( mText ) {
       
   108         return mText->text();
       
   109     } else {
       
   110         return QString();
       
   111         }
       
   112 }
       
   113 
       
   114 /*!
       
   115     Sets the text property. This property is needed by css selector.
       
   116 */
       
   117 void HsShortcutWidget::setText(const QString& textItem)
       
   118 {
       
   119     if ( mText ) {
       
   120         mText->setText(textItem);
       
   121     }
       
   122 }
       
   123 
       
   124 /*!
       
   125     Initializes this widget.
       
   126 */
       
   127 void HsShortcutWidget::onInitialize()
       
   128 {
       
   129     QSharedPointer<CaEntry> caEntry;
       
   130 
       
   131     if(-1 == mCaEntryId) {
       
   132         //Shortcut can be pre installed to Homescreen's DB by setting
       
   133         //application UID for its preference. Entry Id is retrieved
       
   134         //from CA by UID.
       
   135         CaQuery query;
       
   136         query.setEntryTypeNames(QStringList(Hs::applicationTypeName));
       
   137         //Convert UID from hex to ten base, because CA t UIDs in ten base.
       
   138         bool ok;
       
   139         int hexBaseInteger = mUid.toInt(&ok, 0); // Qt Assistant: If base is 0, the C language convention is used:
       
   140                                              // If the string begins with "0x", base 16 is used.
       
   141         QString tenBaseString = QString::number(hexBaseInteger);
       
   142         query.setAttribute(Hs::applicationUidEntryKey, tenBaseString);
       
   143         QList< QSharedPointer<CaEntry> > appEntries = CaService::instance()->getEntries(query);
       
   144 
       
   145         //Verify that entry's UID is what we want since we get all application entries
       
   146         //if UID did not match in getEntries() above.
       
   147         if (!appEntries.isEmpty()
       
   148             && appEntries.first()->attribute(Hs::applicationUidEntryKey) == tenBaseString) {
       
   149             caEntry = appEntries.first();
       
   150             mCaEntryId = caEntry->id();
       
   151 			//Save caEntryId to Homescreen database
       
   152             emit setPreferences(QStringList() << QLatin1String("caEntryId"));
       
   153         }
       
   154     } else {
       
   155         caEntry = CaService::instance()->getEntry(mCaEntryId);
       
   156     }
       
   157 
       
   158     if (!caEntry.isNull()) {
       
   159         createCaNotifier();
       
   160         updateContent(*caEntry.data());
       
   161     } else {
       
   162         emit finished();
       
   163     }
       
   164 }
       
   165 
       
   166 /*!
       
   167     Wakes up this widget.
       
   168 */
       
   169 void HsShortcutWidget::onShow()
       
   170 {
       
   171     updateVisibility();
       
   172 }
       
   173 
       
   174 /*!
       
   175     Puts this widget in quiescent state.
       
   176 */
       
   177 void HsShortcutWidget::onHide()
       
   178 {
       
   179 }
       
   180 
       
   181 /*!
       
   182     \internal  
       
   183 */
       
   184 #ifdef COVERAGE_MEASUREMENT
       
   185 #pragma CTC SKIP
       
   186 #endif //COVERAGE_MEASUREMENT
       
   187 void HsShortcutWidget::gestureEvent(QGestureEvent *event)
       
   188 {
       
   189     HbTapGesture *gesture = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
       
   190     if (gesture) {
       
   191         switch (gesture->state()) {
       
   192             case Qt::GestureStarted:
       
   193                 setBackgroundToPressed();
       
   194                 break;            
       
   195             case Qt::GestureCanceled:
       
   196                 setBackgroundToNormal();
       
   197                 break;
       
   198             case Qt::GestureFinished:
       
   199                 setBackgroundToNormal();
       
   200                 if (gesture->tapStyleHint() == HbTapGesture::Tap) {
       
   201                     launch();
       
   202                 }
       
   203                 break;
       
   204             default:
       
   205                 break;
       
   206         }
       
   207     }
       
   208 }
       
   209 #ifdef COVERAGE_MEASUREMENT
       
   210 #pragma CTC ENDSKIP
       
   211 #endif //COVERAGE_MEASUREMENT
       
   212 
       
   213 /*!
       
   214     \internal
       
   215 */
       
   216 void HsShortcutWidget::launch()
       
   217 {
       
   218     HbInstantFeedback::play(HSCONFIGURATION_GET(shortcutWidgetTapFeedbackEffect));
       
   219     
       
   220     if (mCaEntryRole == ItemEntryRole) {
       
   221         CaService::instance()->executeCommand(mCaEntryId);
       
   222     } else {
       
   223         HsShortcutService::instance()->executeCollectionAction(mCaEntryId, mCaEntryTypeName);
       
   224     }
       
   225 }
       
   226 
       
   227 /*!
       
   228     \internal
       
   229 */
       
   230 void HsShortcutWidget::createPrimitives()
       
   231 {
       
   232     // Background
       
   233     if (!mBackground) {
       
   234         HbFrameDrawer *drawer = new HbFrameDrawer(
       
   235             QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
       
   236         mBackground = new HbFrameItem(drawer, this);
       
   237         HbStyle::setItemName(mBackground, QLatin1String("background"));
       
   238     }
       
   239 
       
   240     // Icon
       
   241     if (!mIcon) {
       
   242         mIcon = new HbIconItem(this);
       
   243         HbStyle::setItemName(mIcon, QLatin1String("icon"));
       
   244     }
       
   245 
       
   246     // Text
       
   247     if (HSCONFIGURATION_GET(isShortcutLabelVisible) && !mText ) {
       
   248         mText = new HbTextItem(this);
       
   249         HbStyle::setItemName(mText, QLatin1String("text"));
       
   250     }
       
   251 }
       
   252 
       
   253 /*!
       
   254     \internal
       
   255 */
       
   256 void HsShortcutWidget::updateContent(const CaEntry &caEntry)
       
   257 {
       
   258     mCaEntryRole = caEntry.role();
       
   259     mCaEntryFlags = caEntry.flags();
       
   260     mCaEntryTypeName = caEntry.entryTypeName();
       
   261     mIcon->setIcon(caEntry.makeIcon());
       
   262     if (mText) {
       
   263         if(caEntry.attribute(Hs::entryShortName).length()) {
       
   264             mText->setText(caEntry.attribute(Hs::entryShortName));
       
   265         } else {
       
   266             mText->setText(caEntry.text());
       
   267         }        
       
   268     }
       
   269 }
       
   270 
       
   271 /*!
       
   272     \internal
       
   273 */
       
   274 void HsShortcutWidget::updateVisibility()
       
   275 {
       
   276     setVisible(!mCaEntryFlags.testFlag(MissingEntryFlag));
       
   277 }
       
   278 
       
   279 /*!
       
   280     \internal
       
   281 */
       
   282 void HsShortcutWidget::setBackgroundToNormal()
       
   283 {
       
   284     mBackground->frameDrawer().
       
   285         setFrameGraphicsName(QLatin1String("qtg_fr_hsshortcut_normal"));
       
   286 }
       
   287 
       
   288 /*!
       
   289     \internal
       
   290 */
       
   291 void HsShortcutWidget::setBackgroundToPressed()
       
   292 {
       
   293     mBackground->frameDrawer().
       
   294         setFrameGraphicsName(QLatin1String("qtg_fr_hsitems_pressed"));
       
   295 }
       
   296 
       
   297 /*!
       
   298     \internal
       
   299 */
       
   300 void HsShortcutWidget::createCaNotifier()
       
   301 {
       
   302     CaNotifierFilter filter;
       
   303     filter.setIds(QList<int>() << mCaEntryId);
       
   304 
       
   305     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
       
   306     notifier->setParent(this);
       
   307 
       
   308     connect(notifier,
       
   309         SIGNAL(entryChanged(CaEntry,ChangeType)),
       
   310         SLOT(onEntryChanged(CaEntry,ChangeType)),
       
   311         Qt::QueuedConnection);
       
   312 }
       
   313 
       
   314 /*!
       
   315     \internal
       
   316 */
       
   317 void HsShortcutWidget::onEntryChanged(const CaEntry &caEntry, ChangeType changeType)
       
   318 {
       
   319     updateContent(caEntry);
       
   320     updateVisibility();
       
   321 
       
   322     if ( changeType == RemoveChangeType && !(caEntry.flags() & MissingEntryFlag)) {
       
   323         //genuinely removed
       
   324         emit finished();
       
   325     }
       
   326 }