TasCommand* TasServerCommand::getCommandParameters(TasCommandModel& commandModel, const QString& commandName)
{
    TasCommand* command = 0;
    TasTarget* target = commandModel.findTarget(APPLICATION_TARGET);
    if(target){
        command = target->findCommand(commandName);
    }
    return command;
}
/*!
  Searches for a tasrget with given id. Returns null if not found.
*/
TasTarget* TasCommandModel::findTarget(const QString& id)
{
    TasTarget* match = 0;
    QListIterator<TasTarget*> i(targetList());
    while (i.hasNext()){
        TasTarget* commandTarget = i.next();
        if( commandTarget->id() == id){
            match = commandTarget;
            break;
        }
    }
    return match;
}
void ObjectService::performObjectService(TasCommandModel& model, TasResponse& response)
{
    TasLogger::logger()->debug(QString("TasCommander::performObjectService entry"));
    QListIterator<TasTarget*> i(model.targetList());
    QString errorString;
    while (i.hasNext()){
        TasTarget* commandTarget = i.next();
        QString targetId = commandTarget->id();
        QString targetType = commandTarget->type();
        QObject* target = 0;

        if(targetType == TYPE_GRAPHICS_VIEW){
            QGraphicsItem* item = findGraphicsItem(targetId);
            //target = TestabilityUtils::castToGraphicsWidget(item);
            target = item->toGraphicsObject();
        }
        else if(targetType == TYPE_STANDARD_VIEW){
            target = findWidget(targetId);

            //If Type is Standard View check QObjects in cache too.
            if(!target) {
              target = findObject(targetId);
            }
        }
        else if(targetType == TYPE_QSCENEGRAPH){
            target = findQuickItem(targetId);
        }
        else if(targetType == TYPE_APPLICATION_VIEW ){
            target = qApp;
        }
        // TODO: add support
        //else if(targetType == TYPE_ACTION_VIEW = "Action";
        //else if(targetType == TYPE_LAYOUT = "Layout";
        //else if(targetType == TYPE_LAYOUT_ITEM = "LayoutItem";
        //else if(targetType == TYPE_WEB = "Web";
        //else if(targetType == TYPE_QWEB = "QWeb";
        else {
            errorString.append(targetType + " target type not supported. ");
        }

        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService %1 %2 found: %3")
                    .arg(targetType).arg(targetId).arg(target!=NULL));

        if (target) {
            QListIterator<TasCommand*> j(commandTarget->commandList());
            while (j.hasNext()){
                TasCommand* command = j.next();
                if(command->name() == "SetAttribute"){
                    doSetAttribute(command, target, errorString);
                }
                else if (command->name() == "CallMethod"){
                    response.setData(doCallMethod(command, target, errorString));
                }
                else {
                    TasLogger::logger()->debug(
                                QString("TasCommander::performObjectService %1 %2: unsupported %3")
                                .arg(targetType).arg(targetId).arg(command->name()));
                }
            }
        }
    }

    if(!errorString.isEmpty()){
        response.setErrorMessage(errorString);
        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService errors: %1")
                    .arg(errorString));
    }
    else {
        TasLogger::logger()->debug(
                    QString("TasCommander::performObjectService, no errors."));
    }
}
TasTarget::TasTarget(const TasTarget& other)
{
    mTargetObject = 0;
    mElement = other.domElement().cloneNode(true).toElement();
    initialize();
}
void ScreenshotService::getScreenshot(TasCommandModel& model, TasResponse& response)
{
    QListIterator<TasTarget*> i(model.targetList());
    QString errorMsg = PARSE_ERROR;
    QImage screenshot;
    QString pictureFormat = "PNG";
    while (i.hasNext()) {
        TasTarget* commandTarget = i.next();
        QString targetId = commandTarget->id();
        QString targetType = commandTarget->type();
        TasCommand* command = commandTarget->findCommand("Screenshot");

        // are required for command completion
        if (targetId.isEmpty() || targetType.isEmpty() || !command) {
            continue;
        }

        if (!command->parameter("format").isEmpty()) {
            pictureFormat = command->parameter("format");
        }

        if (!isFormatSupported(pictureFormat)) {
            errorMsg = "Given format " + pictureFormat + "is not supported. Supported formats are: PNG, JPEG and BMP.";
            break;
        }

        bool draw = (command->parameter("draw") == "true");

        QWidget* widget = 0;
        QQuickWindow* qtQuickWindow = 0;
        WId winId = 0;
        QRect rect(0,0,-1,-1);

        errorMsg = "Taking screenshot failed!";

        if (targetType == TYPE_GRAPHICS_VIEW) {
            //TasLogger::logger()->debug("TYPE_GRAPHICS_VIEW Target id:" + targetId);
            QGraphicsItem* item = findGraphicsItem(targetId);

            if (item) {
                QGraphicsView* view = getViewForItem(item);
                if(view) {
                    ItemLocationDetails locationDetails = TestabilityUtils::getItemLocationDetails(item);
                    rect = QRect(locationDetails.windowPoint.x(),
                                 locationDetails.windowPoint.y(),
                                 locationDetails.width,
                                 locationDetails.height);

                    if (draw) {
                        widget = view->window();
                    } else {
                        winId = view->window()->winId();
                    }
                } else {
                    errorMsg = "Could not find a GraphicsView for the GraphicsItem!";
                }
            } else {
                errorMsg = "Could not find the GraphicsItem!";
            }
        } else if (targetType == TYPE_STANDARD_VIEW) {
            //TasLogger::logger()->debug("TYPE_STANDARD_VIEW about to find widget Target id:" + targetId);
            widget = findWidget(targetId);

            if (widget) {
                if ((widget->isWindow() && !draw) || widget->inherits("QDesktopWidget")) {
                    winId = widget->winId();
                    widget = 0;
                } else if (!draw) {
                    QPoint point = widget->mapToGlobal(QPoint(0,0));
                    QPoint windowPoint = widget->window()->mapFromGlobal(point);

                    rect = QRect(windowPoint.x(),
                                 windowPoint.y(),
                                 widget->rect().width(),
                                 widget->rect().width());
                    winId = widget->window()->winId();
                    widget = 0;
                }
            } else {
                TasLogger::logger()->debug("ScreenshotService::executeService application has no visible ui!");
                errorMsg = "Application has no visible ui!";
            }
        } else if (targetType == TYPE_QSCENEGRAPH) {
            QQuickItem* item = TestabilityUtils::findQuickItem(targetId);

            if (item) {
                QPointF offset = item->mapToScene(QPointF(0,0));
                rect = QRect(-offset.x(), -offset.y(), item->width(), item->height());
                qtQuickWindow = item->window();
            }
        } else {
            //TasLogger::logger()->debug("TYPE_APPLICATION_VIEW about to find application window Target id:" + targetId);
            widget = getApplicationWidget();

            if (!widget) {
                QWindow *window = getApplicationWindow();
                //in case no window false, return the desktop
                qtQuickWindow = qobject_cast<QQuickWindow *>(window);

                if (!window) {
                    widget = qApp->desktop();
                }
            }
        }

        if (widget) {
            screenshot = widget->grab(rect).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(widget));
            }
        } else if (qtQuickWindow) {
            screenshot = qtQuickWindow->grabWindow();
            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", objectId(qtQuickWindow));
            }
        } else if (winId) {
            screenshot = QPixmap::grabWindow(winId, rect.x(), rect.y(), rect.width(), rect.height()).toImage();

            if (!screenshot.isNull()) {
                screenshot.setText("tas_id", QString::number(winId));
            }
        }

        break;
    }

    if (!screenshot.isNull()) {
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        screenshot.save(&buffer, pictureFormat.toLatin1());
        response.setData(bytes);
        buffer.close();
    } else {
        response.setErrorMessage(errorMsg);
    }

}
Пример #6
0
/*!
  Passes service directed to plugins on to the correct plugin.
*/
void InfoLogger::performLogService(TasCommandModel& model, TasResponse& response)
{
    TasTarget* target = model.findTarget(APPLICATION_TARGET);
    if(!mTimer.isActive() && model.interval() > 100){
        mTimer.setInterval(model.interval());
    }
    if(target){
        TasCommand* command = target->findCommand(CPU);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, CPU, fileName)){
                    response.setErrorMessage("File path must be defined for cpu logging!");
                }
                else{
                    mLastCpuTime = mDeviceUtils->currentProcessCpuTime();
                    mInterval.start();
                    mState |= CpuLogging;     
                    if(mCpu){
                        delete mCpu;
                        mCpu = 0;
                    }
                    mCpu = openFile(fileName, command);
                }
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadCpuData(response, command);
            }
        }
        command = target->findCommand(MEM);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, MEM, fileName)){
                    response.setErrorMessage("File path must be defined for mem logging!");
                }
                else{
                    mState |= MemLogging;
                    if(mMem){
                        delete mMem;
                        mMem = 0;
                    }
                    mMem = openFile(fileName, command);
                }
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadMemData(response, command);
            }

        }
        command = target->findCommand(GPU);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, GPU, fileName)){
                    response.setErrorMessage("File path must be defined for mem logging!");
                }
                else{
                    mState |= GpuLogging;
                    if(mGpu){
                        delete mGpu;
                        mGpu = 0;
                    }
                    mGpu = openFile(fileName, command);
                }

            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadGpuData(response, command);
            }

        }
        command = target->findCommand(PWR);
        if(command){
            if(command->parameter(ACTION) == "start"){
                QString fileName;
                if(!makeFileName(command, PWR, fileName)){
                    response.setErrorMessage("File path must be defined for pwr logging!");
                }
                else{
                    mState |= PwrLogging;
                    if(mPwr){
                        delete mPwr;
                        mPwr = 0;
                    }
                    mPwr = openFile(fileName, command);
                }
                //to launch measuring if not running
                mDeviceUtils->pwrDetails();
            }
            else if(command->parameter(ACTION) == "stop" || command->parameter(ACTION) == "load"){
                loadPwrData(response, command);
                if(command->parameter(ACTION) == "stop" ){
                    mDeviceUtils->stopPwrData();
                }
            }

        }
        //determines that does the interval need to be started or stopped
        checkLoggerState();
    }
}