appinstaller/AppinstUi/sisxsifplugin/tsrc/testinstaller/testinstaller.cpp
branchRCL_3
changeset 25 7333d7932ef7
equal deleted inserted replaced
24:5cc91383ab1e 25:7333d7932ef7
       
     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:  Test installer that uses Usif::RSoftwareInstall API.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "testinstaller.h"
       
    19 #include "activerunner.h"
       
    20 #include <hbmainwindow.h>
       
    21 #include <hbview.h>
       
    22 #include <hbtoolbar.h>
       
    23 #include <hbpushbutton.h>
       
    24 #include <hbcheckbox.h>
       
    25 #include <hbcombobox.h>
       
    26 #include <hblabel.h>
       
    27 #include <hbmessagebox.h>
       
    28 #include <QGraphicsLinearLayout>
       
    29 #include <QDir>
       
    30 #include <xqappmgr.h>                       // XQApplicationManager
       
    31 #include <usif/scr/scr.h>                   // RSoftwareComponentRegistry
       
    32 
       
    33 using namespace Usif;
       
    34 
       
    35 #define INSTALLS_PATH_1 "C:\\Data\\Installs\\"
       
    36 #define INSTALLS_PATH_2 "E:\\Installs\\"
       
    37 #define INSTALLS_PATH_3 "F:\\Installs\\"
       
    38 #define INSTALLS_PATH_4 "C:\\"
       
    39 #define INSTALLS_PATH_5 "E:\\"
       
    40 #define INSTALLS_PATH_6 "F:\\"
       
    41 
       
    42 
       
    43 TestInstaller::TestInstaller(int& argc, char* argv[]) : HbApplication(argc, argv),
       
    44     mMainWindow(0), mInstallView(0), mRemoveView(0),
       
    45     mUseSilentInstall(false), mUseSilentUninstall(false), mUseRFileInstall(false), mOcsp(false),
       
    46     mInstallDirectories(0), mInstallableFiles(0), mRemovableApps(0),
       
    47     mCurrentDirPath(), mCurrentFile(), mRunner(0)
       
    48 {
       
    49     mMainWindow = new HbMainWindow();
       
    50 
       
    51     // Install view
       
    52     mInstallView = new HbView();
       
    53     mInstallView->setTitle(tr("Test Installer"));
       
    54 
       
    55     QGraphicsLinearLayout *installLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
    56 
       
    57     HbLabel *installTitle = new HbLabel(tr("Install:"));
       
    58     installLayout->addItem(installTitle);
       
    59 
       
    60     mInstallDirectories = new HbComboBox;
       
    61     mInstallDirectories->setEditable(false);
       
    62     QStringList dirList;
       
    63     getInstallDirs(dirList);
       
    64     mInstallDirectories->setItems(dirList);
       
    65     connect(mInstallDirectories, SIGNAL(currentIndexChanged(int)),
       
    66         this, SLOT(installableDirChanged(int)));
       
    67     installLayout->addItem(mInstallDirectories);
       
    68 
       
    69     mInstallableFiles = new HbComboBox;
       
    70     mInstallableFiles->setEditable(false);
       
    71     connect(mInstallableFiles, SIGNAL(currentIndexChanged(int)),
       
    72             this, SLOT(installableFileChanged(int)));
       
    73     installLayout->addItem(mInstallableFiles);
       
    74 
       
    75     QGraphicsLinearLayout *checkboxesLayout = new QGraphicsLinearLayout(Qt::Horizontal);
       
    76     HbCheckBox *silentInstallCheckBox = new HbCheckBox;
       
    77     silentInstallCheckBox->setText(tr("Silent"));
       
    78     connect(silentInstallCheckBox, SIGNAL(stateChanged(int)),
       
    79             this, SLOT(silentInstallCheckChanged(int)));
       
    80     checkboxesLayout->addItem(silentInstallCheckBox);
       
    81     HbCheckBox *rfileCheckBox = new HbCheckBox;
       
    82     rfileCheckBox->setText(tr("Use RFile"));
       
    83     connect(rfileCheckBox, SIGNAL(stateChanged(int)), this, SLOT(rfileCheckChanged(int)));
       
    84     checkboxesLayout->addItem(rfileCheckBox);
       
    85     HbCheckBox *ocspCheckBox = new HbCheckBox;
       
    86     ocspCheckBox->setText(tr("OCSP"));
       
    87     connect(ocspCheckBox, SIGNAL(stateChanged(int)), this, SLOT(ocspCheckChanged(int)));
       
    88     checkboxesLayout->addItem(ocspCheckBox);
       
    89     installLayout->addItem(checkboxesLayout);
       
    90     installLayout->addStretch();
       
    91 
       
    92     HbPushButton *installNew = new HbPushButton(tr("Install using new API"));
       
    93     installLayout->addItem(installNew);
       
    94     HbPushButton *installOld = new HbPushButton(tr("Install using old API"));
       
    95     installLayout->addItem(installOld);
       
    96     HbPushButton *launchApp = new HbPushButton(tr("Install by opening file"));
       
    97     installLayout->addItem(launchApp);
       
    98     HbPushButton *cancelInstall = new HbPushButton(tr("Cancel installing"));
       
    99     installLayout->addItem(cancelInstall);
       
   100     installLayout->addStretch();
       
   101     connect(installNew, SIGNAL(clicked()), this, SLOT(installUsingNewApi()));
       
   102     connect(installOld, SIGNAL(clicked()), this, SLOT(installUsingOldApi()));
       
   103     connect(launchApp, SIGNAL(clicked()), this, SLOT(installByOpeningFile()));
       
   104     connect(cancelInstall, SIGNAL(clicked()), this, SLOT(cancelInstalling()));
       
   105 
       
   106     HbToolBar *installToolBar = new HbToolBar();
       
   107     installToolBar->addAction(tr("RemoveView"), this, SLOT(removeViewActivated()));
       
   108     installToolBar->addAction(tr("Exit"), this, SLOT(closeApp()));
       
   109     mInstallView->setToolBar(installToolBar);
       
   110 
       
   111     mInstallView->setLayout(installLayout);
       
   112     mMainWindow->addView(mInstallView);
       
   113 
       
   114     // Remove view
       
   115     mRemoveView = new HbView();
       
   116     mRemoveView->setTitle(tr("Test Uninstaller"));
       
   117 
       
   118     QGraphicsLinearLayout *removeLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
   119 
       
   120     HbLabel *uninstallTitle = new HbLabel(tr("Uninstall:"));
       
   121     removeLayout->addItem(uninstallTitle);
       
   122 
       
   123     mRemovableApps = new HbComboBox;
       
   124     mRemovableApps->setEditable(false);
       
   125     removeLayout->addItem(mRemovableApps);
       
   126 
       
   127     HbCheckBox *silentRemoveCheckBox = new HbCheckBox;
       
   128     silentRemoveCheckBox->setText(tr("Silent"));
       
   129     connect(silentRemoveCheckBox, SIGNAL(stateChanged(int)),
       
   130             this, SLOT(silentCheckChanged(int)));
       
   131     removeLayout->addItem(silentRemoveCheckBox);
       
   132     removeLayout->addStretch();
       
   133 
       
   134     HbPushButton *removeNew = new HbPushButton(tr("Remove using new API"));
       
   135     removeLayout->addItem(removeNew);
       
   136     HbPushButton *removeOld = new HbPushButton(tr("Remove using old API"));
       
   137     removeLayout->addItem(removeOld);
       
   138     removeLayout->addStretch();
       
   139     connect(removeNew, SIGNAL(clicked()), this, SLOT(removeUsingNewApi()));
       
   140     connect(removeOld, SIGNAL(clicked()), this, SLOT(removeUsingOldApi()));
       
   141 
       
   142     mRemoveView->setLayout(removeLayout);
       
   143     mMainWindow->addView(mRemoveView);
       
   144 
       
   145     HbToolBar *removeToolBar = new HbToolBar();
       
   146     removeToolBar->addAction(tr("InstallView"), this, SLOT(installViewActivated()));
       
   147     removeToolBar->addAction(tr("Exit"), this, SLOT(closeApp()));
       
   148     mRemoveView->setToolBar(removeToolBar);
       
   149 
       
   150     mMainWindow->setCurrentView(mInstallView);
       
   151     mMainWindow->show();
       
   152 
       
   153     changeDir(mInstallDirectories->currentText());
       
   154     getRemovableApps();
       
   155 }
       
   156 
       
   157 TestInstaller::~TestInstaller()
       
   158 {
       
   159     delete mRunner;
       
   160     delete mInstallView;
       
   161     delete mRemoveView;
       
   162     delete mMainWindow;
       
   163 }
       
   164 
       
   165 void TestInstaller::installViewActivated()
       
   166 {
       
   167     mMainWindow->setCurrentView(mInstallView);
       
   168 }
       
   169 
       
   170 void TestInstaller::removeViewActivated()
       
   171 {
       
   172     mMainWindow->setCurrentView(mRemoveView);
       
   173 }
       
   174 
       
   175 void TestInstaller::silentInstallCheckChanged(int state)
       
   176 {
       
   177     Qt::CheckState s = static_cast<Qt::CheckState>(state);
       
   178     mUseSilentInstall = (s == Qt::Checked);
       
   179 }
       
   180 
       
   181 void TestInstaller::silentRemoveCheckChanged(int state)
       
   182 {
       
   183     Qt::CheckState s = static_cast<Qt::CheckState>(state);
       
   184     mUseSilentUninstall = (s == Qt::Checked);
       
   185 }
       
   186 
       
   187 void TestInstaller::rfileCheckChanged(int state)
       
   188 {
       
   189     Qt::CheckState s = static_cast<Qt::CheckState>(state);
       
   190     mUseRFileInstall = (s == Qt::Checked);
       
   191 }
       
   192 
       
   193 void TestInstaller::ocspCheckChanged(int state)
       
   194 {
       
   195     Qt::CheckState s = static_cast<Qt::CheckState>(state);
       
   196     mOcsp = (s == Qt::Checked);
       
   197 }
       
   198 
       
   199 void TestInstaller::installableDirChanged(int /*index*/)
       
   200 {
       
   201     if (mInstallDirectories) {
       
   202         changeDir(mInstallDirectories->currentText());
       
   203     }
       
   204 }
       
   205 
       
   206 void TestInstaller::installableFileChanged(int /*index*/)
       
   207 {
       
   208     if (mInstallableFiles) {
       
   209         mCurrentFile = mCurrentDirPath;
       
   210         mCurrentFile.append(mInstallableFiles->currentText());
       
   211     }
       
   212 }
       
   213 
       
   214 void TestInstaller::installUsingNewApi()
       
   215 {
       
   216     if (isFileSelected() && createRunner(true)) {
       
   217         doInstall(mCurrentFile);
       
   218     }
       
   219 }
       
   220 
       
   221 void TestInstaller::installUsingOldApi()
       
   222 {
       
   223     if (isFileSelected() && createRunner(false)) {
       
   224         doInstall(mCurrentFile);
       
   225     }
       
   226 }
       
   227 
       
   228 void TestInstaller::installByOpeningFile()
       
   229 {
       
   230     if (mInstallableFiles) {
       
   231         doOpenFile(mCurrentFile);
       
   232     }
       
   233 }
       
   234 
       
   235 void TestInstaller::cancelInstalling()
       
   236 {
       
   237     if (mRunner) {
       
   238         delete mRunner;
       
   239         mRunner = 0;
       
   240         HbMessageBox::warning(tr("Running operation deleted"));
       
   241     } else {
       
   242         HbMessageBox::warning(tr("No operation running"));
       
   243     }
       
   244 }
       
   245 
       
   246 void TestInstaller::removeUsingNewApi()
       
   247 {
       
   248     if (isFileSelected() && createRunner(true)) {
       
   249         removeSelectedUsingNewApi();
       
   250     }
       
   251 }
       
   252 
       
   253 void TestInstaller::removeUsingOldApi()
       
   254 {
       
   255     if (isFileSelected() && createRunner(false)) {
       
   256         removeSelectedUsingOldApi();
       
   257     }
       
   258 }
       
   259 
       
   260 void TestInstaller::handleComplete()
       
   261 {
       
   262     HbMessageBox::information(tr("Completed"));
       
   263 
       
   264     delete mRunner;
       
   265     mRunner = 0;
       
   266 
       
   267     changeDir(mCurrentDirPath);
       
   268     getRemovableApps();
       
   269 }
       
   270 
       
   271 void TestInstaller::handleError(int error)
       
   272 {
       
   273     QString messageText;
       
   274     if (error == KErrCancel) {
       
   275         messageText = tr("Cancelled");
       
   276     } else {
       
   277         messageText = tr("Error %1").arg(error);
       
   278     }
       
   279     HbMessageBox::warning(messageText);
       
   280 
       
   281     delete mRunner;
       
   282     mRunner = 0;
       
   283 }
       
   284 
       
   285 void TestInstaller::closeApp()
       
   286 {
       
   287     qApp->exit();
       
   288 }
       
   289 
       
   290 void TestInstaller::fileOpenOk(const QVariant &/*result*/)
       
   291 {
       
   292     HbMessageBox::information(tr("Open ok"));
       
   293 }
       
   294 
       
   295 void TestInstaller::fileOpenFailed(int errorCode, const QString &errorMsg)
       
   296 {
       
   297     HbMessageBox::warning(tr("Open failed: %1: %2").arg(errorCode).arg(errorMsg));
       
   298 }
       
   299 
       
   300 void TestInstaller::getInstallDirs(QStringList& dirList)
       
   301 {
       
   302     QStringList possibleDirs;
       
   303     possibleDirs << INSTALLS_PATH_1 << INSTALLS_PATH_2 << INSTALLS_PATH_3
       
   304         << INSTALLS_PATH_4 << INSTALLS_PATH_5 << INSTALLS_PATH_6;
       
   305 
       
   306     QListIterator<QString> iter(possibleDirs);
       
   307     while (iter.hasNext()) {
       
   308         QString dirName(iter.next());
       
   309         QDir dir(dirName);
       
   310         if (dir.exists()) {
       
   311             dirList.append(dirName);
       
   312         }
       
   313     }
       
   314 }
       
   315 
       
   316 void TestInstaller::changeDir(const QString& dirPath)
       
   317 {
       
   318     QDir dir(dirPath);
       
   319     if (dir.exists()) {
       
   320         mCurrentDirPath = dirPath;
       
   321         mInstallableFiles->clear();
       
   322 
       
   323         QFileInfoList list = dir.entryInfoList(QDir::Files);
       
   324         QListIterator<QFileInfo> iter(list);
       
   325         while (iter.hasNext()) {
       
   326             const QFileInfo &info(iter.next());
       
   327             mInstallableFiles->addItem(info.fileName());
       
   328         }
       
   329 
       
   330         mCurrentFile.clear();
       
   331         if (mInstallableFiles->count()) {
       
   332             mCurrentFile = mCurrentDirPath;
       
   333             mCurrentFile.append(mInstallableFiles->currentText());
       
   334         }
       
   335     }
       
   336 }
       
   337 
       
   338 void TestInstaller::getRemovableApps()
       
   339 {
       
   340     TRAP_IGNORE(doGetRemovableAppsL());
       
   341 }
       
   342 
       
   343 void TestInstaller::doGetRemovableAppsL()
       
   344 {
       
   345     mRemovableApps->clear();
       
   346     mRemovableComponentIds.clear();
       
   347     mRemovableUids.clear();
       
   348     mRemovableSoftwareTypes.clear();
       
   349 
       
   350     RSoftwareComponentRegistry registry;
       
   351     User::LeaveIfError(registry.Connect());
       
   352     CleanupClosePushL(registry);
       
   353 
       
   354     RArray<TComponentId> componentIdList;
       
   355     CleanupClosePushL( componentIdList );
       
   356     registry.GetComponentIdsL(componentIdList);
       
   357     for (int i = 0; i < componentIdList.Count(); ++i) {
       
   358         TComponentId compId = componentIdList[i];
       
   359         CComponentEntry *compEntry = CComponentEntry::NewLC();
       
   360         if (registry.GetComponentL(compId, *compEntry)) {
       
   361             if (compEntry->IsRemovable()) {
       
   362                 TPtrC compName = compEntry->Name();
       
   363                 QString name = QString::fromUtf16(compName.Ptr(), compName.Length());
       
   364                 mRemovableApps->addItem(name);
       
   365 
       
   366                 mRemovableComponentIds.append(compId);
       
   367 
       
   368                 _LIT(KCompUid, "CompUid");
       
   369                 CPropertyEntry *property = registry.GetComponentPropertyL(compId, KCompUid);
       
   370                 CleanupStack::PushL(property);
       
   371                 CIntPropertyEntry* intProperty = dynamic_cast<CIntPropertyEntry*>(property);
       
   372                 mRemovableUids.append(TUid::Uid(intProperty->IntValue()));
       
   373                 CleanupStack::PopAndDestroy(property);
       
   374 
       
   375                 TPtrC softwareType = compEntry->SoftwareType();
       
   376                 if (softwareType == KSoftwareTypeNative) {
       
   377                     mRemovableSoftwareTypes.append(Native);
       
   378                 } else if (softwareType == KSoftwareTypeJava) {
       
   379                     mRemovableSoftwareTypes.append(Java);
       
   380                 } else {
       
   381                     mRemovableSoftwareTypes.append(Unknown);
       
   382                 }
       
   383             }
       
   384         }
       
   385         CleanupStack::PopAndDestroy(compEntry);
       
   386     }
       
   387 
       
   388     CleanupStack::PopAndDestroy(2, &registry);  // componentIdList, registry
       
   389 }
       
   390 
       
   391 bool TestInstaller::isFileSelected()
       
   392 {
       
   393     if (mCurrentFile.isEmpty()) {
       
   394         HbMessageBox::warning(tr("No files selected"));
       
   395         changeDir(mCurrentDirPath);
       
   396         return false;
       
   397     }
       
   398     return true;
       
   399 }
       
   400 
       
   401 bool TestInstaller::createRunner(bool useSif)
       
   402 {
       
   403     if (!mRunner) {
       
   404         mRunner = new ActiveRunner(useSif);
       
   405         connect(mRunner, SIGNAL(opCompleted()), this, SLOT(handleComplete()));
       
   406         connect(mRunner, SIGNAL(opFailed(int)), this, SLOT(handleError(int)));
       
   407     } else {
       
   408         HbMessageBox::warning(tr("Already running"));
       
   409         return false;
       
   410     }
       
   411     return true;
       
   412 }
       
   413 
       
   414 void TestInstaller::doInstall(const QString &fileName)
       
   415 {
       
   416     if (mRunner) {
       
   417         mRunner->install(fileName, mUseSilentInstall, mUseRFileInstall, mOcsp );
       
   418     }
       
   419 }
       
   420 
       
   421 void TestInstaller::doOpenFile(const QString &fileName)
       
   422 {
       
   423     QFile file(fileName);
       
   424     if (file.exists()) {
       
   425         XQApplicationManager appManager;
       
   426         XQAiwRequest *request = appManager.create(file);
       
   427         if (request) {
       
   428             connect(request, SIGNAL(requestOk(const QVariant &)),
       
   429                 this, SLOT(fileOpenOk(const QVariant &)));
       
   430             connect(request, SIGNAL(requestError(int, const QString &)),
       
   431                 this, SLOT(fileOpenFailed(int, const QString &)));
       
   432             QList<QVariant> args;
       
   433             args << file.fileName();
       
   434             request->setArguments(args);
       
   435             if (request->send()) {
       
   436                 HbMessageBox::information(tr("Opening..."));
       
   437             } else {
       
   438                 HbMessageBox::warning(tr("Cannot open"));
       
   439             }
       
   440             delete request;
       
   441         } else {
       
   442             HbMessageBox::warning(tr("No handler for file '%1'").arg(fileName));
       
   443         }
       
   444     }
       
   445 }
       
   446 
       
   447 void TestInstaller::removeSelectedUsingNewApi()
       
   448 {
       
   449     if (mRemovableApps && mRunner) {
       
   450         int index = mRemovableApps->currentIndex();
       
   451         const TComponentId &compId(mRemovableComponentIds.at(index));
       
   452         mRunner->remove(compId, mUseSilentUninstall);
       
   453     }
       
   454 }
       
   455 
       
   456 void TestInstaller::removeSelectedUsingOldApi()
       
   457 {
       
   458     if (mRemovableApps && mRunner) {
       
   459         int index = mRemovableApps->currentIndex();
       
   460         const TUid &uid(mRemovableUids.at(index));
       
   461 
       
   462         if (mRemovableSoftwareTypes.at(index) == Native) {
       
   463             _LIT8(KSisxMimeType, "x-epoc/x-sisx-app");
       
   464             mRunner->remove(uid, KSisxMimeType, mUseSilentInstall);
       
   465         } else if (mRemovableSoftwareTypes.at(index) == Java) {
       
   466             _LIT8(KJarMIMEType, "application/java-archive");
       
   467             mRunner->remove(uid, KJarMIMEType, mUseSilentInstall);
       
   468         } else {
       
   469             HbMessageBox::warning(tr("Not supported software type"));
       
   470             delete mRunner;
       
   471             mRunner = 0;
       
   472         }
       
   473     }
       
   474 }
       
   475