appinstaller/AppinstUi/sifuidevicedialogplugin/src/sifuidialogcontentwidget.cpp
changeset 25 98b66e4fb0be
child 29 26b6f0522fd8
equal deleted inserted replaced
24:84a16765cd86 25:98b66e4fb0be
       
     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: Software install notification content widget.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "sifuidialogcontentwidget.h"
       
    19 #include <QGraphicsLinearLayout>
       
    20 #include <hbstackedwidget.h>
       
    21 #include <hblabel.h>
       
    22 #include <hbpushbutton.h>
       
    23 #include <hbcombobox.h>
       
    24 #include <hbprogressbar.h>
       
    25 #include <QPixmap>
       
    26 #include <QDir>
       
    27 #include <QFileInfoList>
       
    28 #if defined(Q_OS_SYMBIAN)
       
    29 #include <fbs.h>                            // CFbsBitmap
       
    30 #endif  // Q_OS_SYMBIAN
       
    31 
       
    32 // See definitions in sifuidevicedialogplugin.qrc
       
    33 const char KSifUiDialogIconAppDefault[] = ":/default_app_icon.svg";
       
    34 
       
    35 const int KAppNameIndex = 0;
       
    36 const int KAppSizeIndex = 1;
       
    37 
       
    38 
       
    39 // ======== LOCAL FUNCTIONS ========
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // fbsBitmap()
       
    43 // ----------------------------------------------------------------------------
       
    44 //
       
    45 CFbsBitmap *fbsBitmap(int handle)
       
    46 {
       
    47     CFbsBitmap *bitmap = 0;
       
    48 #if defined(Q_OS_SYMBIAN)
       
    49     bitmap = new CFbsBitmap;
       
    50     if (bitmap) {
       
    51         if (!bitmap->Duplicate(handle)) {
       
    52             delete bitmap;
       
    53             bitmap = 0;
       
    54         }
       
    55     }
       
    56 #endif // Q_OS_SYMBIAN
       
    57     return bitmap;
       
    58 }
       
    59 
       
    60 
       
    61 // ======== MEMBER FUNCTIONS ========
       
    62 
       
    63 // ----------------------------------------------------------------------------
       
    64 // SifUiDialogContentWidget::SifUiDialogContentWidget()
       
    65 // ----------------------------------------------------------------------------
       
    66 //
       
    67 SifUiDialogContentWidget::SifUiDialogContentWidget(QGraphicsItem *parent,
       
    68         Qt::WindowFlags flags) : HbWidget(parent, flags),
       
    69         mAppIcon(0), mAppName(0), mAppSize(0),
       
    70         mMainLayout(0), mAppDetailsLayout(0), mStackedWidget(0),
       
    71         mMemorySelection(0), mProgressBar(0), mErrorText(0),
       
    72         mBitmap(0), mMask(0)
       
    73 {
       
    74 }
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // SifUiDialogContentWidget::~SifUiDialogContentWidget()
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 SifUiDialogContentWidget::~SifUiDialogContentWidget()
       
    81 {
       
    82     delete mBitmap;
       
    83     delete mMask;
       
    84 }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // SifUiDialogContentWidget::constructFromParameters()
       
    88 // ----------------------------------------------------------------------------
       
    89 //
       
    90 void SifUiDialogContentWidget::constructFromParameters(const QVariantMap &parameters)
       
    91 {
       
    92     Q_ASSERT(mMainLayout == 0);
       
    93     mMainLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
    94 
       
    95     // Application icon
       
    96     QGraphicsLinearLayout *iconAndAppLayout = new QGraphicsLinearLayout(Qt::Horizontal);
       
    97     Q_ASSERT(mAppIcon == 0);
       
    98     mAppIcon = new HbLabel;
       
    99     updateAppIcon(parameters);
       
   100     iconAndAppLayout->addItem(mAppIcon);
       
   101 
       
   102     // Application name and version
       
   103     Q_ASSERT(mAppDetailsLayout == 0);
       
   104     mAppDetailsLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   105     Q_ASSERT(mAppName == 0);
       
   106     mAppName = new HbLabel(applicationName(parameters));
       
   107     mAppDetailsLayout->addItem(mAppName);
       
   108 
       
   109     // Application size
       
   110     Q_ASSERT(mAppSize == 0);
       
   111     updateAppSize(parameters);
       
   112 
       
   113     // Other application details
       
   114     if (parameters.contains(KSifUiApplicationDetails)) {
       
   115         addDetails(parameters.value(KSifUiApplicationDetails).toStringList());
       
   116     }
       
   117     iconAndAppLayout->addItem(mAppDetailsLayout);
       
   118     mMainLayout->addItem(iconAndAppLayout);
       
   119 
       
   120     // Memory selection, progress bar, and error text within a stacked widget
       
   121     Q_ASSERT(mStackedWidget == 0);
       
   122     mStackedWidget = new HbStackedWidget;
       
   123 
       
   124     Q_ASSERT(mMemorySelection == 0);
       
   125     mMemorySelection = new HbComboBox;
       
   126     connect(mMemorySelection, SIGNAL(currentIndexChanged(const QString &)),
       
   127             this, SIGNAL(memorySelectionChanged(const QString &)));
       
   128     mStackedWidget->addWidget(mMemorySelection);
       
   129 
       
   130     Q_ASSERT(mProgressBar == 0);
       
   131     mProgressBar = new HbProgressBar;
       
   132     mProgressBar->setRange(0,0);    // busy indicator by default
       
   133     mStackedWidget->addWidget(mProgressBar);
       
   134 
       
   135     Q_ASSERT(mErrorText == 0);
       
   136     mErrorText = new HbLabel;
       
   137     mStackedWidget->addWidget(mErrorText);
       
   138 
       
   139     mMainLayout->addItem(mStackedWidget);
       
   140     updateMemorySelection(parameters);
       
   141     updateProgressBar(parameters);
       
   142     updateErrorText(parameters);
       
   143 
       
   144     setLayout(mMainLayout);
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // SifUiDialogContentWidget::updateFromParameters()
       
   149 // ----------------------------------------------------------------------------
       
   150 //
       
   151 void SifUiDialogContentWidget::updateFromParameters(const QVariantMap &parameters)
       
   152 {
       
   153     // Application icon
       
   154     updateAppIcon(parameters);
       
   155 
       
   156     // Application name and version
       
   157     if (parameters.contains(KSifUiApplicationName)) {
       
   158         QString appNameStr = applicationName(parameters);
       
   159         if (mAppName) {
       
   160             if (mAppName->plainText() != appNameStr) {
       
   161                 mAppName->setPlainText(appNameStr);
       
   162             }
       
   163         } else {
       
   164             HbLabel *appName = new HbLabel(appNameStr);
       
   165             mAppDetailsLayout->insertItem(KAppNameIndex, appName);
       
   166             mAppName = appName;
       
   167         }
       
   168     }
       
   169 
       
   170     // Application size
       
   171     updateAppSize(parameters);
       
   172 
       
   173     // Other application details
       
   174     if (parameters.contains(KSifUiApplicationDetails)) {
       
   175         removeDetails();
       
   176         addDetails(parameters.value(KSifUiApplicationDetails).toStringList());
       
   177     }
       
   178 
       
   179     // Stacked widgets: memory selection, progress bar and error text
       
   180     updateMemorySelection(parameters);
       
   181     updateProgressBar(parameters);
       
   182     updateErrorText(parameters);
       
   183 }
       
   184 
       
   185 // ----------------------------------------------------------------------------
       
   186 // SifUiDialogContentWidget::changeType()
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 void SifUiDialogContentWidget::changeType(SifUiDeviceDialogType type)
       
   190 {
       
   191     switch (type) {
       
   192         case SifUiConfirmationQuery:
       
   193             mStackedWidget->setCurrentWidget(mMemorySelection);
       
   194             break;
       
   195         case SifUiProgressNote:
       
   196             mStackedWidget->setCurrentWidget(mProgressBar);
       
   197             break;
       
   198         case SifUiCompleteNote:
       
   199             // TODO: remove mStackedWidget?
       
   200             break;
       
   201         case SifUiErrorNote:
       
   202             mStackedWidget->setCurrentWidget(mErrorText);
       
   203             break;
       
   204         default:
       
   205             break;
       
   206     }
       
   207 }
       
   208 
       
   209 // ----------------------------------------------------------------------------
       
   210 // SifUiDialogContentWidget::applicationName()
       
   211 // ----------------------------------------------------------------------------
       
   212 //
       
   213 QString SifUiDialogContentWidget::applicationName() const
       
   214 {
       
   215     if (mAppName) {
       
   216         return mAppName->plainText();
       
   217     }
       
   218     return QString();
       
   219 }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // SifUiDialogContentWidget::isDefaultIconUsed()
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 bool SifUiDialogContentWidget::isDefaultIconUsed() const
       
   226 {
       
   227     return (mBitmap != 0 && mMask != 0);
       
   228 }
       
   229 
       
   230 // ----------------------------------------------------------------------------
       
   231 // SifUiDialogContentWidget::iconHandles()
       
   232 // ----------------------------------------------------------------------------
       
   233 //
       
   234 void SifUiDialogContentWidget::iconHandles(int &iconHandle, int &maskHandle) const
       
   235 {
       
   236     if (mBitmap && mMask) {
       
   237         iconHandle = mBitmap->Handle();
       
   238         maskHandle = mMask->Handle();
       
   239     }
       
   240 }
       
   241 
       
   242 // ----------------------------------------------------------------------------
       
   243 // SifUiDialogContentWidget::progressInfo()
       
   244 // ----------------------------------------------------------------------------
       
   245 //
       
   246 void SifUiDialogContentWidget::progressInfo(int &finalValue, int &currentValue) const
       
   247 {
       
   248     if (mProgressBar) {
       
   249         finalValue = mProgressBar->maximum();
       
   250         currentValue = mProgressBar->progressValue();
       
   251     }
       
   252 }
       
   253 
       
   254 // ----------------------------------------------------------------------------
       
   255 // SifUiDialogContentWidget::applicationName()
       
   256 // ----------------------------------------------------------------------------
       
   257 //
       
   258 QString SifUiDialogContentWidget::applicationName(const QVariantMap &parameters)
       
   259 {
       
   260     QString appName = "";
       
   261     if (parameters.contains(KSifUiApplicationName)) {
       
   262         QString nameParam = parameters.value(KSifUiApplicationName).toString();
       
   263         if (parameters.contains(KSifUiApplicationVersion)) {
       
   264             QString versionParam = parameters.value(KSifUiApplicationVersion).toString();
       
   265             //: Template for application name and version in SW install confirmation query.
       
   266             //: %1 is the application name and %2 is the version number.
       
   267             //: Version number consist of major, minor, and build numbers.
       
   268             //: For example: "Chess (v 1.01(123))".
       
   269             // TODO: enable when translations ready
       
   270             //appName = hbTrId("txt_sisxui_install_appname_version").arg(nameParam, versionParam);
       
   271             appName = tr("%1 (v %2)").arg(nameParam, versionParam);
       
   272         } else {
       
   273             appName = nameParam;
       
   274         }
       
   275     }
       
   276     return appName;
       
   277 }
       
   278 
       
   279 // ----------------------------------------------------------------------------
       
   280 // SifUiDialogContentWidget::applicationSize()
       
   281 // ----------------------------------------------------------------------------
       
   282 //
       
   283 QString SifUiDialogContentWidget::applicationSize(const QVariantMap &parameters)
       
   284 {
       
   285     QString appSize = "";
       
   286     if (parameters.contains(KSifUiApplicationSize)) {
       
   287         uint size = parameters.value(KSifUiApplicationSize).toUInt();
       
   288         if (size > 0) {
       
   289             if (size > KMega) {
       
   290                 //: Application size in SW install confirmation query, %1 is in megabytes
       
   291                 // TODO: enable when translations ready
       
   292                 //appSize = hbTrId("txt_sisxui_install_appsize_mb").arg(size/KMega);
       
   293                 appSize = tr("Size: %1 MB").arg(size/KMega);
       
   294             } else if(size > KKilo) {
       
   295                 //: Application size in SW install confirmation query, %1 is in kilobytes
       
   296                 // TODO: enable when translations ready
       
   297                 //appSize = hbTrId("txt_sisxui_install_appsize_kb").arg(size/KKilo);
       
   298                 appSize = tr("Size: %1 kB").arg(size/KKilo);
       
   299             } else {
       
   300                 //: Application size in SW install confirmation query, %1 is in bytes
       
   301                 // TODO: enable when translations ready
       
   302                 //appSize = hbTrId("txt_sisxui_install_appsize_b").arg(size);
       
   303                 appSize = tr("Size: %1 B").arg(size);
       
   304             }
       
   305         }
       
   306     }
       
   307     return appSize;
       
   308 }
       
   309 
       
   310 // ----------------------------------------------------------------------------
       
   311 // SifUiDialogContentWidget::removeDetails()
       
   312 // ----------------------------------------------------------------------------
       
   313 //
       
   314 void SifUiDialogContentWidget::removeDetails()
       
   315 {
       
   316     int firstDetailsItemIndex = ( mAppName ? 1 : 0 ) + ( mAppSize ? 1 : 0 );
       
   317     QGraphicsLayoutItem *item = 0;
       
   318     for (int i = 0; i < mAppDetailsLayout->count(); ++i) {
       
   319         if (i >= firstDetailsItemIndex) {
       
   320             item = mAppDetailsLayout->itemAt(i);
       
   321             mAppDetailsLayout->removeAt(i);
       
   322             delete item;
       
   323             item = 0;
       
   324         }
       
   325     }
       
   326 }
       
   327 
       
   328 // ----------------------------------------------------------------------------
       
   329 // SifUiDialogContentWidget::addDetails()
       
   330 // ----------------------------------------------------------------------------
       
   331 //
       
   332 void SifUiDialogContentWidget::addDetails(const QStringList &detailList)
       
   333 {
       
   334     QStringListIterator detailsIter(detailList);
       
   335     while (detailsIter.hasNext())
       
   336         {
       
   337         addDetail(detailsIter.next());
       
   338         }
       
   339 }
       
   340 
       
   341 // ----------------------------------------------------------------------------
       
   342 // SifUiDialogContentWidget::addDetail()
       
   343 // ----------------------------------------------------------------------------
       
   344 //
       
   345 void SifUiDialogContentWidget::addDetail(const QString &detailText)
       
   346 {
       
   347     Q_ASSERT(mAppDetailsLayout != 0);
       
   348     HbLabel *detailItem = new HbLabel(detailText);
       
   349     mAppDetailsLayout->addItem(detailItem);
       
   350 }
       
   351 
       
   352 // ----------------------------------------------------------------------------
       
   353 // SifUiDialogContentWidget::updateAppIcon()
       
   354 // ----------------------------------------------------------------------------
       
   355 //
       
   356 void SifUiDialogContentWidget::updateAppIcon(const QVariantMap &parameters)
       
   357 {
       
   358     Q_ASSERT(mAppIcon != 0);
       
   359     if (parameters.contains(KSifUiApplicationIconHandle) &&
       
   360         parameters.contains(KSifUiApplicationIconMaskHandle)) {
       
   361         int iconHandle = parameters.value(KSifUiApplicationIconHandle).toInt();
       
   362         int maskHandle = parameters.value(KSifUiApplicationIconMaskHandle).toInt();
       
   363         QPixmap pixmap;
       
   364         delete mBitmap;
       
   365         mBitmap = fbsBitmap(iconHandle);
       
   366         delete mMask;
       
   367         mMask = fbsBitmap(maskHandle);
       
   368         if (mBitmap && mMask) {
       
   369             pixmap = QPixmap::fromSymbianCFbsBitmap(mBitmap);
       
   370             pixmap.setAlphaChannel(QPixmap::fromSymbianCFbsBitmap(mMask));
       
   371         }
       
   372         mAppIcon->setIcon(HbIcon(pixmap));
       
   373     } else {
       
   374         if (mAppIcon->icon().isNull()) {
       
   375             mAppIcon->setIcon(HbIcon(KSifUiDialogIconAppDefault));
       
   376         }
       
   377     }
       
   378 }
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // SifUiDialogContentWidget::updateAppSize()
       
   382 // ----------------------------------------------------------------------------
       
   383 //
       
   384 void SifUiDialogContentWidget::updateAppSize(const QVariantMap &parameters)
       
   385 {
       
   386     if (parameters.contains(KSifUiApplicationSize)) {
       
   387         QString appSizeStr = applicationSize(parameters);
       
   388         if (appSizeStr.length() > 0) {
       
   389             if (mAppSize) {
       
   390                 if (mAppSize->plainText() != appSizeStr) {
       
   391                     mAppSize->setPlainText(appSizeStr);
       
   392                 }
       
   393             } else {
       
   394                 HbLabel *appSize = new HbLabel(appSizeStr);
       
   395                 mAppDetailsLayout->insertItem(KAppSizeIndex, appSize);
       
   396                 mAppSize = appSize;
       
   397             }
       
   398         }
       
   399     }
       
   400 }
       
   401 
       
   402 // ----------------------------------------------------------------------------
       
   403 // SifUiDialogContentWidget::updateMemorySelection()
       
   404 // ----------------------------------------------------------------------------
       
   405 //
       
   406 void SifUiDialogContentWidget::updateMemorySelection(const QVariantMap &parameters)
       
   407 {
       
   408     if (parameters.contains(KSifUiMemorySelection)) {
       
   409 
       
   410         // TODO: get user visible drives, icons for them, and free sizes
       
   411         // it might be better to get this list from Symbian-side via SifUi API
       
   412         QStringList driveList;
       
   413         QFileInfoList driveInfoList = QDir::drives();
       
   414         foreach(const QFileInfo &driveInfo, driveInfoList) {
       
   415             const QChar driveName = driveInfo.absolutePath()[0];
       
   416             switch (driveName.toAscii()) {
       
   417                 case 'C':
       
   418                     driveList << "C: Phone memory";
       
   419                     break;
       
   420                 case 'E':
       
   421                     driveList << "E: Mass memory";
       
   422                     break;
       
   423                 case 'F':
       
   424                     driveList << "F: Memory card";
       
   425                     break;
       
   426                 default:
       
   427                     break;
       
   428             }
       
   429         }
       
   430 
       
   431         mMemorySelection->setItems(driveList);
       
   432         mStackedWidget->setCurrentWidget(mMemorySelection);
       
   433 
       
   434         // TODO: set selected item, read the default from cenrep
       
   435     }
       
   436 }
       
   437 
       
   438 // ----------------------------------------------------------------------------
       
   439 // SifUiDialogContentWidget::updateProgressBar()
       
   440 // ----------------------------------------------------------------------------
       
   441 //
       
   442 void SifUiDialogContentWidget::updateProgressBar(const QVariantMap &parameters)
       
   443 {
       
   444     bool progressBarChanged = false;
       
   445     if (parameters.contains(KSifUiProgressNoteFinalValue)) {
       
   446         mProgressBar->setMaximum(parameters.value(KSifUiProgressNoteFinalValue).toInt());
       
   447         progressBarChanged = true;
       
   448     }
       
   449     if (parameters.contains(KSifUiProgressNoteValue)) {
       
   450         int newValue = mProgressBar->progressValue();
       
   451         newValue += parameters.value(KSifUiProgressNoteValue).toInt();
       
   452         mProgressBar->setProgressValue(newValue);
       
   453         progressBarChanged = true;
       
   454     }
       
   455     if (progressBarChanged) {
       
   456         mStackedWidget->setCurrentWidget(mProgressBar);
       
   457     }
       
   458 }
       
   459 
       
   460 // ----------------------------------------------------------------------------
       
   461 // SifUiDialogContentWidget::updateErrorText()
       
   462 // ----------------------------------------------------------------------------
       
   463 //
       
   464 void SifUiDialogContentWidget::updateErrorText(const QVariantMap &parameters)
       
   465 {
       
   466     if (parameters.contains(KSifUiErrorCode)) {
       
   467         // TODO: proper error texts
       
   468         QString errorText = tr("Error %1").arg(parameters.value(KSifUiErrorCode).toInt());
       
   469         mErrorText->setPlainText(errorText);
       
   470         mStackedWidget->setCurrentWidget(mErrorText);
       
   471     }
       
   472 }
       
   473 
       
   474 
       
   475