src/hbcore/utils/hbthetestutility_p.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbthetestwidget_p.h"
       
    27 #include "hbthetestutility_p.h"
       
    28 #include "hbinstance.h"
       
    29 #include "hbnamespace_p.h"
       
    30 
       
    31 #include <QGraphicsWidget>
       
    32 #include <QTextStream>
       
    33 #include <QFile>
       
    34 #include <QDir>
       
    35 #include <QDate>
       
    36 #include <QTimer>
       
    37 #include <QFont>
       
    38 #include <QObject>
       
    39 #include <hbstyle.h>
       
    40 #include <hbaction.h>
       
    41 
       
    42 #include <QDebug> // for qWarning
       
    43 
       
    44 #ifndef Q_OS_SYMBIAN
       
    45 #include <iostream>
       
    46 #endif
       
    47 
       
    48 const QChar KComment('#');
       
    49 const QString KDriveFPath("F:\\data\\log\\");
       
    50 const QString KDriveEPath("E:\\data\\log\\");
       
    51 const QString KDriveCPath("C:\\data\\log\\");
       
    52 const QString KTheConfigureFile("TheTestUtility.txt"); // todo: proper name for configuration file
       
    53 
       
    54 
       
    55 class HbTheTestUtilityPrivate
       
    56 {
       
    57 public:
       
    58     HbTheTestUtilityPrivate(HbMainWindow *mainWindow);
       
    59     bool checkConfigFile();
       
    60     bool doCheckConfigFile(const QString &path);
       
    61     QObject *receiver(const QString &name);
       
    62 public:
       
    63     HbMainWindow *mMainWindow;
       
    64     HbTheTestWidget *mTestWidget;
       
    65     bool useE;
       
    66     bool useF;
       
    67     bool button1used;
       
    68     bool button2used;
       
    69     bool button3used;
       
    70     bool button4used;
       
    71 };
       
    72 
       
    73 HbTheTestUtilityPrivate::HbTheTestUtilityPrivate(HbMainWindow *mainWindow):
       
    74 mMainWindow(mainWindow),
       
    75 mTestWidget(0),
       
    76 useE(false),
       
    77 useF(false),
       
    78 button1used(false),
       
    79 button2used(false),
       
    80 button3used(false),
       
    81 button4used(false)
       
    82 {
       
    83 }
       
    84 
       
    85 /*!
       
    86     Check the existence of configuration file.
       
    87     Test button is only created if configuration file can be found and
       
    88     there are at least one slot configured to any button.
       
    89     \internal
       
    90 */
       
    91 bool HbTheTestUtilityPrivate::checkConfigFile()
       
    92 {
       
    93 #if defined (Q_OS_UNIX)
       
    94     return false; // currently no UNIX support
       
    95 #elif defined (Q_OS_SYMBIAN)
       
    96     bool useC = false;
       
    97     useF = doCheckConfigFile(KDriveFPath);
       
    98     if (!useF) {
       
    99         useE = doCheckConfigFile(KDriveEPath);
       
   100         if (!useE) {
       
   101             useC = doCheckConfigFile(KDriveCPath);
       
   102         }
       
   103     }
       
   104     return (useC || useE || useF);
       
   105 #elif defined (Q_OS_WIN32)
       
   106     return doCheckConfigFile(KDriveCPath);
       
   107 #endif
       
   108 }
       
   109 
       
   110 /*!
       
   111     Called from checkConfigFile()
       
   112     Does the actual configuration file check from given path.
       
   113     \internal
       
   114 */
       
   115 bool HbTheTestUtilityPrivate::doCheckConfigFile(const QString &path)
       
   116 {
       
   117     QString filePath = path;
       
   118     filePath = QDir::toNativeSeparators(filePath);
       
   119 
       
   120     QDir dir(filePath);
       
   121     if (!dir.exists()) {
       
   122         return false;
       
   123     }
       
   124 
       
   125     filePath.append(KTheConfigureFile);
       
   126     QFile file(filePath);
       
   127     if (!file.exists()) {
       
   128         return false;
       
   129     }
       
   130 
       
   131     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
   132 #ifndef Q_OS_SYMBIAN
       
   133         std::cerr << "Error: Cannot read from file " << qPrintable(file.fileName());
       
   134         std::cerr << qPrintable(filePath);
       
   135         std::cerr << qPrintable(file.errorString()) << std::endl;
       
   136 #endif
       
   137         return false;
       
   138     }
       
   139 
       
   140     QTextStream configReader(&file);
       
   141     QString line;
       
   142     do {
       
   143         line = configReader.readLine();
       
   144         if (line.size() > 0) {
       
   145             if (line.at(0).digitValue() == 1) {
       
   146                 button1used = true;
       
   147             } else if (line.at(0).digitValue() == 2) {
       
   148                 button2used = true;
       
   149             } else if (line.at(0).digitValue() == 3) {
       
   150                 button3used = true;
       
   151             } else if (line.at(0).digitValue() == 4) {
       
   152                 button4used = true;
       
   153             }
       
   154         }
       
   155     } while (!line.isNull());
       
   156 
       
   157     if (button1used || button2used || button3used || button4used) {
       
   158         return true;
       
   159     }
       
   160 
       
   161     return false;
       
   162 }
       
   163 
       
   164 
       
   165 
       
   166 /*!
       
   167     Returns first QObject from mainwindow's scene or 0,
       
   168     if no object with given name found.
       
   169     \internal
       
   170 */
       
   171 
       
   172 QObject *HbTheTestUtilityPrivate::receiver(const QString& name)
       
   173 {
       
   174     QGraphicsScene* scene = mMainWindow->scene();
       
   175     QList<QGraphicsItem*> sceneItems = scene->items();
       
   176     foreach (QGraphicsItem* sceneItem, sceneItems ) {
       
   177         if (sceneItem->isWidget()) {
       
   178             QGraphicsWidget *obj = static_cast<QGraphicsWidget *>(sceneItem);
       
   179             // in 4.6 this code could be used
       
   180             //QGraphicsObject *obj = sceneItem->toGraphicsObject();
       
   181             //if (!obj) continue;
       
   182             QString className = QString::fromUtf8(obj->metaObject()->className());
       
   183             if (className == name) {
       
   184                 return qobject_cast<QObject*>(obj);
       
   185             }
       
   186         }
       
   187     }
       
   188 
       
   189     return 0;
       
   190 }
       
   191 
       
   192 
       
   193 /*!
       
   194     \class HbTheTestUtility
       
   195     \internal
       
   196     \proto
       
   197 */
       
   198 
       
   199 HbTheTestUtility::HbTheTestUtility(HbMainWindow *mainWindow)
       
   200 {
       
   201     d = new HbTheTestUtilityPrivate(mainWindow);
       
   202     QTimer::singleShot(3000,this,SLOT(initialize())); // random 3 second delay
       
   203 }
       
   204 
       
   205 void HbTheTestUtility::initialize()
       
   206 {
       
   207     if (d->checkConfigFile()) {
       
   208         d->mTestWidget = new HbTheTestWidget(d->mMainWindow);
       
   209         QObject::connect(d->mTestWidget->button1(), SIGNAL(clicked()),
       
   210             this, SLOT(button1clicked()));
       
   211         QObject::connect(d->mTestWidget->button2(), SIGNAL(clicked()),
       
   212             this, SLOT(button2clicked()));
       
   213         QObject::connect(d->mTestWidget->button3(), SIGNAL(clicked()),
       
   214             this, SLOT(button3clicked()));
       
   215         QObject::connect(d->mTestWidget->button4(), SIGNAL(clicked()),
       
   216             this, SLOT(button4clicked()));
       
   217         d->mTestWidget->setZValue(HbPrivate::PopupZValueRangeEnd+1);
       
   218         // hide buttons, that are not used
       
   219         if (!d->button1used) {
       
   220             d->mTestWidget->button1()->setVisible(false);
       
   221         }
       
   222         if (!d->button2used) {
       
   223             d->mTestWidget->button2()->setVisible(false);
       
   224         }
       
   225         if (!d->button3used) {
       
   226             d->mTestWidget->button3()->setVisible(false);
       
   227         }
       
   228         if (!d->button4used) {
       
   229             d->mTestWidget->button4()->setVisible(false);
       
   230         }
       
   231         d->mTestWidget->setLayoutDirection(Qt::LeftToRight);
       
   232 
       
   233         d->mMainWindow->scene()->addItem(d->mTestWidget);
       
   234     }
       
   235 }
       
   236 
       
   237 void HbTheTestUtility::button1clicked()
       
   238 {
       
   239     invokeSlots(1);
       
   240 }
       
   241 
       
   242 void HbTheTestUtility::button2clicked()
       
   243 {
       
   244     invokeSlots(2);
       
   245 }
       
   246 
       
   247 void HbTheTestUtility::button3clicked()
       
   248 {
       
   249     invokeSlots(3);
       
   250 }
       
   251 
       
   252 void HbTheTestUtility::button4clicked()
       
   253 {
       
   254     invokeSlots(4);
       
   255 }
       
   256 
       
   257 /*!
       
   258     Called, when TheTestButton is clicked.
       
   259     This method parses configuration file and calls all available slots.
       
   260     \internal
       
   261 */
       
   262 void HbTheTestUtility::invokeSlots(const int button)
       
   263 {
       
   264 #if defined (Q_OS_LINUX) || defined(Q_OS_MAC)
       
   265     QString filePath(QDir::tempPath());
       
   266 #else
       
   267     QString filePath;
       
   268     if (d->useF) {
       
   269         filePath = "F:\\data\\log\\";
       
   270     } else if (d->useE) {
       
   271         filePath = "E:\\data\\log\\";
       
   272     } else {
       
   273         filePath = "C:\\data\\log\\";
       
   274     }
       
   275 #endif
       
   276 
       
   277     filePath = QDir::toNativeSeparators(filePath);
       
   278 
       
   279     filePath.append(KTheConfigureFile);
       
   280     QFile file(filePath);
       
   281     if (!file.exists()) {
       
   282         return;
       
   283     }
       
   284 
       
   285     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
   286 #ifndef Q_OS_SYMBIAN
       
   287         std::cerr << "Error: Cannot read from file " << qPrintable(file.fileName());
       
   288         std::cerr << qPrintable(filePath);
       
   289         std::cerr << qPrintable(file.errorString()) << std::endl;
       
   290 #endif
       
   291         return;
       
   292     }
       
   293 
       
   294     QTextStream configReader(&file);
       
   295     QString line;
       
   296     do {
       
   297         line = configReader.readLine();
       
   298         if (line.isEmpty()) {
       
   299             continue;
       
   300         }
       
   301         // line not empty -> at least one character found
       
   302         if (line.at(0) == KComment) {
       
   303             continue;
       
   304         }
       
   305         if (line.at(0).digitValue() == button) {
       
   306             QString object = line.section(" ", 1, 1);
       
   307             QString method = line.section(" ", 2, 2);
       
   308             QObject *temp = d->receiver(object);
       
   309             if (temp) {
       
   310                 QMetaObject::invokeMethod(temp, method.toLatin1(), Qt::DirectConnection);
       
   311             }
       
   312         }
       
   313     } while (!line.isNull());
       
   314 }
       
   315 
       
   316 
       
   317 HbTheTestUtility::~HbTheTestUtility()
       
   318 {
       
   319     delete d;
       
   320 }
       
   321