homescreenapp/hsdomainmodel/src/hswidgetcomponent.cpp
changeset 39 4e8ebe173323
child 46 23b5d6a29cce
equal deleted inserted replaced
36:cdae8c6c3876 39:4e8ebe173323
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QCoreApplication>
       
    19 #include <QDir>
       
    20 
       
    21 #include "hswidgetcomponentregistry.h"
       
    22 #include "hswidgetcomponent.h"
       
    23 #include "hsapp_defs.h"
       
    24 #include "caservice.h"
       
    25 #include "caquery.h"
       
    26 #include "canotifier.h"
       
    27 
       
    28 /*!
       
    29 
       
    30 */
       
    31 HsWidgetComponent::~HsWidgetComponent()
       
    32 {
       
    33 }
       
    34 
       
    35 /*!
       
    36 
       
    37 */
       
    38 QString HsWidgetComponent::rootPath() const
       
    39 {
       
    40     return mRootPath;
       
    41 }
       
    42 
       
    43 /*!
       
    44 
       
    45 */
       
    46 QString HsWidgetComponent::uri() const
       
    47 {
       
    48     return mUri;
       
    49 }
       
    50 
       
    51 /*!
       
    52 
       
    53 */
       
    54 HsWidgetComponent::HsWidgetComponent(const QString &uri, QObject *parent)
       
    55   : QObject(parent),
       
    56     mUri(uri)
       
    57 {
       
    58     listenChangeEvents();
       
    59 	resolveRootPath();
       
    60 	installTranslator();
       
    61 }
       
    62 
       
    63 /*!
       
    64 
       
    65 */
       
    66 void HsWidgetComponent::listenChangeEvents()
       
    67 {
       
    68 	CaQuery query;
       
    69     query.setEntryTypeNames(QStringList(widgetTypeName()));
       
    70 	CaNotifierFilter filter(query);  
       
    71     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
       
    72     notifier->setParent(this);
       
    73     connect(notifier,
       
    74             SIGNAL(entryChanged(CaEntry,ChangeType)),
       
    75             SLOT(onEntryChanged(CaEntry,ChangeType)));
       
    76 }
       
    77 
       
    78 /*!
       
    79 
       
    80 */
       
    81 void HsWidgetComponent::resolveRootPath()
       
    82 {
       
    83 	CaQuery query;
       
    84     query.setEntryTypeNames(QStringList(widgetTypeName()));
       
    85 	query.setAttribute(widgetUriAttributeName(), mUri);
       
    86 	QList< QSharedPointer<CaEntry> > widgetEntries = CaService::instance()->getEntries(query);
       
    87 	
       
    88     if (widgetEntries.isEmpty()) {
       
    89 		return;
       
    90 	}
       
    91     QSharedPointer<CaEntry> entry = widgetEntries.first();
       
    92     mRootPath = entry->attribute(widgetPathAttributeName());
       
    93 }
       
    94 
       
    95 /*!
       
    96 
       
    97 */
       
    98 void HsWidgetComponent::installTranslator()
       
    99 {
       
   100     QStringList possiblePaths;
       
   101 #ifdef Q_OS_WIN32
       
   102     possiblePaths << "resource/qt/translations";
       
   103 #else
       
   104     
       
   105     QDir currentDir(mRootPath);
       
   106     if (!currentDir.exists() || mRootPath.isEmpty()) {
       
   107         return;
       
   108     }
       
   109     while (currentDir.cdUp()) {}
       
   110     QString drive = currentDir.path();
       
   111     drive.append("resource/qt/translations");
       
   112     possiblePaths << drive;
       
   113     // if it is not in installed,then check z drive
       
   114     possiblePaths << "z:/resource/qt/translations";
       
   115 #endif
       
   116     
       
   117     for(int i=0; i<possiblePaths.count(); ++i) {
       
   118         QString candidate = QDir::toNativeSeparators(possiblePaths.at(i));   
       
   119         if (QDir(candidate).exists()) {
       
   120             QString fileName(mUri);
       
   121 		    fileName.append("_");
       
   122 		    fileName.append(QLocale::system().name());
       
   123             if (mTranslator.load(fileName, candidate)) {
       
   124                 QCoreApplication::installTranslator(&mTranslator);
       
   125 		        break;
       
   126             }
       
   127 	    }
       
   128     }
       
   129 }
       
   130 
       
   131 /*!
       
   132 
       
   133 */
       
   134 void HsWidgetComponent::uninstallTranslator()
       
   135 {
       
   136     QCoreApplication::removeTranslator(&mTranslator);
       
   137 }
       
   138 
       
   139 /*!
       
   140     This slot reacts to \a entry change that is described with 
       
   141     \a changeType. On remove change type, aboutToUninstall() signal is
       
   142     emitted.
       
   143 */
       
   144 void HsWidgetComponent::onEntryChanged(const CaEntry &entry, ChangeType changeType)
       
   145 {
       
   146     if (changeType == RemoveChangeType) {
       
   147         QString uri = entry.attribute(widgetUriAttributeName());
       
   148         if (uri == mUri) {
       
   149            	uninstallTranslator();
       
   150 			emit aboutToUninstall();
       
   151         }
       
   152     }
       
   153 }