activityfw/testapplications/newtestappset/acttestletter/viewmanager.cpp
changeset 117 c63ee96dbe5f
equal deleted inserted replaced
115:3ab5c078b490 117:c63ee96dbe5f
       
     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 #include <qservicemanager.h>
       
    18 #include <HbInstance>
       
    19 #include <QEventLoop>
       
    20 #include <QStringList>
       
    21 #include <QCoreApplication>
       
    22 #include <hbapplication.h>
       
    23 #include "viewmanager.h"
       
    24 #include "boldwidget.h"
       
    25 #include "italicwidget.h"
       
    26 #include "underlinewidget.h"
       
    27 #include "letterwidget.h"
       
    28 #include "afstorageglobals.h"
       
    29 
       
    30 QTM_USE_NAMESPACE
       
    31 
       
    32 viewmanager::viewmanager(HbApplication *hbapp):
       
    33     HbMainWindow(),  mHbApp(hbapp)
       
    34 {
       
    35     QServiceManager serviceManager;
       
    36     mActivityStorage = serviceManager.loadInterface("com.nokia.qt.activities.Storage");
       
    37     if (!mActivityStorage) {
       
    38         qFatal("load Storage plugin failed");
       
    39     }
       
    40 
       
    41     mActivaction = serviceManager.loadInterface("com.nokia.qt.activities.Activation");
       
    42     if (!mActivaction) {
       
    43         qFatal("load Activation plugin failed");
       
    44     }
       
    45     
       
    46     qRegisterMetaType<Af::ActivationReason>("Af::ActivationReason");
       
    47     connect(mActivaction, SIGNAL(activated(Af::ActivationReason, QString, QVariantHash)), this, SLOT(loadActivityData(Af::ActivationReason, QString, QVariantHash)));
       
    48     
       
    49 
       
    50     mBoldWidget = new boldwidget();
       
    51     mItalicWidget = new italicwidget();
       
    52     mUnderlineWidget = new underlinewidget();
       
    53     mBoldView = addView(mBoldWidget);
       
    54     mBoldView->setTitle("Bold");
       
    55     mItalicView = addView(mItalicWidget);
       
    56     mItalicView->setTitle("Italic");
       
    57     mUnderlineView = addView(mUnderlineWidget);
       
    58     mUnderlineView->setTitle("Underline");
       
    59     toitalic();
       
    60     connect(mBoldWidget, SIGNAL(toitalic()), this, SLOT(toitalic()));
       
    61     connect(mBoldWidget, SIGNAL(tounderline()), this, SLOT(tounderline()));
       
    62 
       
    63     connect(mItalicWidget, SIGNAL(tounderline()), this, SLOT(tounderline()));
       
    64     connect(mItalicWidget, SIGNAL(tobold()), this, SLOT(tobold()));
       
    65 
       
    66     connect(mUnderlineWidget, SIGNAL(tobold()), this, SLOT(tobold()));
       
    67     connect(mUnderlineWidget, SIGNAL(toitalic()), this, SLOT(toitalic()));
       
    68 
       
    69     connect(mBoldWidget, SIGNAL(save()), this, SLOT(save()));
       
    70     connect(mItalicWidget, SIGNAL(save()), this, SLOT(save()));
       
    71     connect(mUnderlineWidget, SIGNAL(save()), this, SLOT(save()));
       
    72     connect(mBoldWidget, SIGNAL(del()), this, SLOT(del()));
       
    73     connect(mItalicWidget, SIGNAL(del()), this, SLOT(del()));
       
    74     connect(mUnderlineWidget, SIGNAL(del()), this, SLOT(del()));
       
    75 
       
    76     connect(this, SIGNAL(statusChanged(bool)), this, SLOT(newStatus(bool)));
       
    77     
       
    78     //startup case    
       
    79     Af::ActivationReason reason;
       
    80     bool ok = QMetaObject::invokeMethod(mActivaction, "reason", Q_RETURN_ARG(Af::ActivationReason, reason));
       
    81     if (!ok) {
       
    82         emit statusChanged(false);
       
    83     }
       
    84     QString actName;
       
    85     QVariantHash parameters;
       
    86     if (reason == Af::ActivationReasonActivity) {
       
    87         ok = QMetaObject::invokeMethod(mActivaction, "name", Q_RETURN_ARG(QString, actName));
       
    88         if (!ok) {
       
    89             emit statusChanged(false);
       
    90         }
       
    91         ok = QMetaObject::invokeMethod(mActivaction, "parameters", Q_RETURN_ARG(QVariantHash, parameters));    
       
    92         if (!ok) {
       
    93             emit statusChanged(false);
       
    94         }
       
    95         
       
    96         QVariant data;
       
    97         ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, actName));
       
    98         if (!ok) {
       
    99             emit statusChanged(false);            
       
   100         }
       
   101         
       
   102         bool background = false;
       
   103         if (parameters.contains(Af::KActivityUriBackgroundKey)) {
       
   104             background = true;
       
   105         }
       
   106         loadActivityData(data, background, true);
       
   107     }
       
   108     //startup case end
       
   109 }
       
   110 
       
   111 viewmanager::~viewmanager()
       
   112 {
       
   113 }
       
   114 
       
   115 void viewmanager::toitalic()
       
   116 {
       
   117     setCurrentView(mItalicView);
       
   118     mStatename = "italic";
       
   119 }
       
   120 
       
   121 void viewmanager::tobold()
       
   122 {
       
   123     setCurrentView(mBoldView);
       
   124     mStatename = "bold";
       
   125 }
       
   126 
       
   127 void viewmanager::tounderline()
       
   128 {
       
   129     setCurrentView(mUnderlineView);
       
   130     mStatename = "underline";
       
   131 }
       
   132 
       
   133 
       
   134 void viewmanager::save()
       
   135 {
       
   136     QString color;
       
   137     QString visible;
       
   138     QString persistent;
       
   139     QString name;
       
   140     bool delBefore;
       
   141     letterwidget* widget = currentWidget();
       
   142     color = widget->color();
       
   143     visible = widget->visibility();
       
   144     persistent = widget->persistent();
       
   145     name = widget->name();
       
   146     delBefore = widget->delBeforeSave();
       
   147 
       
   148     QStringList list;
       
   149     list.append(mStatename);
       
   150     list.append(color);
       
   151     mSaveVariant.clear();
       
   152     mSaveVariant = list;
       
   153 
       
   154     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   155     QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
       
   156     QVariantHash metadata;
       
   157     metadata.insert(ActivityScreenshotKeyword, screenshot);
       
   158     if ( visible == "true" )
       
   159         {
       
   160         metadata.insert(ActivityVisibility, true);    
       
   161         }
       
   162     else if ( visible == "false" )
       
   163         {
       
   164         metadata.insert(ActivityVisibility, false);    
       
   165         }
       
   166     
       
   167     if ( persistent == "true" )
       
   168         {
       
   169         metadata.insert(ActivityPersistence, true);    
       
   170         }
       
   171     else if ( persistent == "false" )
       
   172         {
       
   173         metadata.insert(ActivityPersistence, false);    
       
   174         }
       
   175     
       
   176     if ( name != "none" )
       
   177         {
       
   178         metadata.insert(ActivityApplicationName, name);    
       
   179         }
       
   180     
       
   181     bool retok, ok;
       
   182     if (delBefore) {
       
   183         ok = QMetaObject::invokeMethod(mActivityStorage, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename));
       
   184         if (!ok) {
       
   185             emit statusChanged(false);
       
   186             return;
       
   187         }
       
   188     }
       
   189 
       
   190     ok = QMetaObject::invokeMethod(mActivityStorage, "saveActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename), Q_ARG(QVariant, mSaveVariant), Q_ARG(QVariantHash, metadata));
       
   191     if (!ok || !retok) {
       
   192         emit statusChanged(false);
       
   193         return;
       
   194     }
       
   195     emit statusChanged(true);
       
   196 }
       
   197 void viewmanager::del()
       
   198 {
       
   199     bool retok;
       
   200     bool ok = QMetaObject::invokeMethod(mActivityStorage, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename));
       
   201     if (!ok || !retok) {
       
   202         emit statusChanged(false);
       
   203     }
       
   204     emit statusChanged(true);
       
   205 }
       
   206 
       
   207 void viewmanager::loadActivityData(Af::ActivationReason reason, const QString &name, QVariantHash parameter)
       
   208 {
       
   209     QVariant data;
       
   210     bool ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
       
   211     if (!ok) {
       
   212         emit statusChanged(false);
       
   213         return;
       
   214     }
       
   215     bool background = false;
       
   216     if (parameter.contains(Af::KActivityUriBackgroundKey)) {
       
   217         background = true;
       
   218     }
       
   219     loadActivityData(data, background, false);
       
   220 }
       
   221 
       
   222 void viewmanager::loadActivityData(const QVariant &data, bool background, bool startup)
       
   223 {
       
   224     HbView *oldView = currentView();
       
   225 
       
   226     QStringList list = data.toStringList();
       
   227     QString statename = list.at(0);
       
   228     QString color = list.at(1);
       
   229     if (statename == "bold") {
       
   230         mBoldWidget->setcolor(color);
       
   231         mBoldWidget->setBackgroundFlag(background);
       
   232         tobold();
       
   233         mBoldWidget->update();
       
   234     } else if (statename == "italic") {
       
   235         mItalicWidget->setcolor(color);
       
   236         mItalicWidget->setBackgroundFlag(background);
       
   237         toitalic();
       
   238         mItalicWidget->update();
       
   239     } else if (statename == "underline") {
       
   240         mUnderlineWidget->setcolor(color);
       
   241         mUnderlineWidget->setBackgroundFlag(background);
       
   242         tounderline();
       
   243         mUnderlineWidget->update();
       
   244     } else {
       
   245         statusChanged(false);
       
   246     }
       
   247     
       
   248     if (currentView() != oldView && !startup) {
       
   249         QEventLoop loop;
       
   250         loop.connect(this, SIGNAL(viewReady()), SLOT(quit()));
       
   251         loop.exec();
       
   252     }
       
   253 }
       
   254 
       
   255 letterwidget* viewmanager::currentWidget()
       
   256 {
       
   257 	HbView* view = currentView();
       
   258 	QGraphicsWidget* gwidget = view->widget();
       
   259 	letterwidget* lwidget = qobject_cast<letterwidget*>(gwidget);
       
   260 }
       
   261 
       
   262 void viewmanager::newStatus(bool status)
       
   263 {
       
   264     letterwidget* cw = currentWidget();
       
   265     if(cw) {
       
   266         cw->setStatus(status);
       
   267     }
       
   268 }