taskswitcher/testapplications/generator/hellostarter/lunchwidget.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 "lunchwidget.h"
       
    18 #include <hbapplication>
       
    19 #include <QStringList>
       
    20 #include <HbComboBox>
       
    21 #include <HbIcon>
       
    22 #include <hbMainWindow>
       
    23 #include <hbinstance.h>
       
    24 #include "tstasksettings.h"
       
    25 #include <XQConversions>
       
    26 #include <e32base.h>
       
    27 #include <QFile>
       
    28 #include <QTextStream>
       
    29 #include <QTimer>
       
    30 #include <hblineedit.h>
       
    31 #include <HbValidator>
       
    32 
       
    33 #define hbApp qobject_cast<HbApplication*>(qApp)
       
    34 
       
    35 lunchwidget::lunchwidget(QGraphicsItem *parent)
       
    36     : HbWidget(parent), mActionCounter(0)
       
    37 {
       
    38     mFile = new QFile("c:\\data\\helloworldstarter.txt");    
       
    39     mFile->open(QIODevice::Append | QIODevice::Text);
       
    40     mStream = new QTextStream(mFile);
       
    41     
       
    42     mResultLabel = new HbLabel("idle");        
       
    43     mTenLabel= new HbLabel("10x");        
       
    44     mOneLabel= new HbLabel("1x");        
       
    45     mPlusLabel= new HbLabel("+");        
       
    46    
       
    47     
       
    48     mStartButton = new HbPushButton("Start");
       
    49     mCloseButton = new HbPushButton("Close");
       
    50     mAutoButton = new HbPushButton("Auto");
       
    51     
       
    52     
       
    53     mTenCombo = new HbComboBox();
       
    54     mOneCombo = new HbComboBox();
       
    55     for (int i=0; i<10; i++) {
       
    56         QString num;
       
    57         num.setNum(i);
       
    58         mTenCombo->addItem(num);
       
    59         mOneCombo->addItem(num);
       
    60     }
       
    61 
       
    62     mTimesEdid = new HbLineEdit("300");
       
    63     mTimesEdid->setObjectName("TimesEditLine");     
       
    64     mQTimesValidator = new QIntValidator(this); 
       
    65     mQTimesValidator->setRange(0, 1000000);
       
    66     mTimesValidator = new HbValidator(this);
       
    67     mTimesValidator->addField(mQTimesValidator, "300");
       
    68     mTimesEdid->setValidator(mTimesValidator);
       
    69     
       
    70     mTimesLabel = new HbLabel("Times:");
       
    71     mCounterLabel = new HbLabel("Count:");
       
    72     mCounterValueLabel = new HbLabel("");
       
    73     
       
    74     mGridLayout = new QGraphicsGridLayout();
       
    75     mGridLayout->addItem(mResultLabel, 0, 0, 1, 6);
       
    76     mGridLayout->addItem(mStartButton, 1, 0, 1, 2);
       
    77     mGridLayout->addItem(mCloseButton, 1, 3, 1, 2);
       
    78     mGridLayout->addItem(mAutoButton, 1, 5, 1, 2);
       
    79     mGridLayout->addItem(mTenLabel, 2, 0, 1, 2);
       
    80     mGridLayout->addItem(mPlusLabel, 2, 2, 1, 2);
       
    81     mGridLayout->addItem(mOneLabel, 2, 4, 1, 2);    
       
    82     mGridLayout->addItem(mTenCombo, 3, 0, 1, 3);
       
    83     mGridLayout->addItem(mOneCombo, 3, 4, 1, 3);
       
    84     mGridLayout->addItem(mTimesLabel, 4, 0, 1, 2);
       
    85     mGridLayout->addItem(mTimesEdid, 4, 3, 1, 4);
       
    86     mGridLayout->addItem(mCounterLabel, 5, 0, 1, 3);
       
    87     mGridLayout->addItem(mCounterValueLabel, 5, 4, 1, 3);
       
    88     
       
    89     setLayout(mGridLayout);
       
    90     
       
    91     connect( mStartButton, SIGNAL(released()), this, SLOT(start()) );
       
    92     connect( mCloseButton, SIGNAL(released()), this, SLOT(stop()) );
       
    93     connect( mAutoButton, SIGNAL(released()), this, SLOT(automat()) );
       
    94     
       
    95     mTimer = new QTimer(this);
       
    96     connect(mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
       
    97     
       
    98 }
       
    99 
       
   100 lunchwidget::~lunchwidget()
       
   101 {
       
   102     delete mFile;
       
   103     delete mStream;  
       
   104     delete mQTimesValidator;
       
   105     delete mTimesValidator;
       
   106 }
       
   107 
       
   108 void lunchwidget::start() 
       
   109 {
       
   110     int ten = mTenCombo->currentText().toInt();
       
   111     int one = mOneCombo->currentText().toInt();
       
   112     int num = ten*10+one;
       
   113     for (int i=1; i<=num; i++) {        
       
   114         QString application;
       
   115         application = tr("HelloWorldBasic%1").arg(i);
       
   116         mResultLabel->setPlainText(application);
       
   117         HBufC *appName = XQConversions::qStringToS60Desc(application);
       
   118         CleanupStack::PushL(appName);   
       
   119 
       
   120         RProcess process;
       
   121         CleanupClosePushL(process);
       
   122 
       
   123         bool result = false;
       
   124         if (process.Create(*appName, KNullDesC()) == KErrNone) {        
       
   125             // wait until process reports it's ready
       
   126             TRequestStatus stat;
       
   127             process.Rendezvous(stat);
       
   128             process.Resume();
       
   129             User::WaitForRequest(stat);
       
   130             
       
   131             if (stat.Int() == KErrNone) {                
       
   132                 result = true;
       
   133                 User::After(1000);
       
   134             }    
       
   135         }
       
   136         
       
   137         CleanupStack::PopAndDestroy(&process);
       
   138         CleanupStack::PopAndDestroy(appName);
       
   139     }
       
   140 }
       
   141 
       
   142 void lunchwidget::stop()
       
   143 {
       
   144     RProcess application;
       
   145     TFindProcess finder(_L("*hello*"));
       
   146     TFullName fullname;
       
   147      while (finder.Next(fullname) == KErrNone) {
       
   148          TInt err = application.Open(fullname);
       
   149          if (err==KErrNone && (application.FileName().Find(_L("HelloWorldBasic"))>=0 || application.FileName().Find(_L("helloworldbasic"))>=0)) {
       
   150              application.Terminate(KErrCancel);
       
   151              User::After(1000);
       
   152          }
       
   153          application.Close();
       
   154      }
       
   155 }
       
   156 
       
   157 void lunchwidget::timeout()
       
   158 {
       
   159     if(mActionCounter == mTimes) {
       
   160         stop();
       
   161         mTimer->stop();
       
   162         *mStream<<"end\n";
       
   163         mStream->flush();
       
   164         enableControls(true);
       
   165     }
       
   166     if(!mOpened) {
       
   167         start();
       
   168         mOpened = true;
       
   169     }
       
   170     else {
       
   171         stop();
       
   172         mOpened = false;
       
   173         mActionCounter++;
       
   174         *mStream<<mActionCounter<<"\n";
       
   175         mStream->flush();
       
   176         setCounterLabel(mActionCounter);
       
   177     }
       
   178     
       
   179     
       
   180 }
       
   181 
       
   182 void lunchwidget::automat()
       
   183 {
       
   184     mTimes = times();
       
   185     mActionCounter = 0;
       
   186     *mStream<<"auto open\n";
       
   187     mStream->flush();
       
   188     mOpened = false;
       
   189     enableControls(false);
       
   190     mTimer->start(2000);
       
   191 }
       
   192 
       
   193 void lunchwidget::enableControls(bool enable)
       
   194 {    
       
   195     mStartButton->setEnabled(enable);
       
   196     mCloseButton->setEnabled(enable);
       
   197     mAutoButton->setEnabled(enable);
       
   198     mTenCombo->setEnabled(enable);
       
   199     mOneCombo->setEnabled(enable);
       
   200     mTimesEdid->setEnabled(enable);
       
   201 }
       
   202 
       
   203 void lunchwidget::setCounterLabel(int count)
       
   204 {
       
   205     QString num;
       
   206     num.setNum(count);
       
   207     mCounterValueLabel->setPlainText(num);
       
   208 }
       
   209 
       
   210 int lunchwidget::times()
       
   211 {
       
   212     QString ttext = mTimesEdid->text();
       
   213     bool ok;
       
   214     int times = ttext.toInt(&ok);
       
   215     if(!ok) {
       
   216         times = 0;
       
   217     }
       
   218     return times;
       
   219 }