示例#1
0
void Asterisk::onSocketReadyRead()
{
//    qDebug("<ready-read>");

    QVariantHash headers;

    while (socket.canReadLine()) {
        QByteArray line = socket.readLine();

//        qDebug() << "Line:" << line;

        if (line != "\r\n") {
            QStringList header = QString(line.trimmed()).split(':');

            headers.insertMulti(header[0], decodeValue(header[1].trimmed()));
        } else {
            if (headers.contains("Response"))
                responses.insert(headers.take("ActionID").toString(), headers);
            else if (headers.contains("Event"))
                emit eventReceived(headers.take("Event").toString(), headers);

            headers.clear();
        }
    }

//    qDebug("</ready-read>");
}
bool PageAddPrinter::finishClicked()
{
    bool ret = false;
    QVariantHash args = values();

    // Check if it's a printer or a class that we are adding
    bool isClass = !args.take(ADDING_PRINTER).toBool();
    QString destName = args[KCUPS_PRINTER_NAME].toString();
    QString filename = args.take(FILENAME).toString();

    KCupsRequest *request = new KCupsRequest;
    if (isClass) {
        args[KCUPS_PRINTER_IS_ACCEPTING_JOBS] = true;
        args[KCUPS_PRINTER_STATE] = IPP_PRINTER_IDLE;
        request->addOrModifyClass(destName, args);
    } else {
        request->addOrModifyPrinter(destName, args, filename);
    }

    request->waitTillFinished();
    if (request->hasError()) {
        kDebug() << request->error() << request->errorMsg();
        QString message;
        if (isClass) {
            message = i18nc("@info", "Failed to add class: '%1'", request->errorMsg());
        } else {
            message = i18nc("@info", "Failed to configure printer: '%1'", request->errorMsg());
        }
        ui->messageWidget->setText(message);
        ui->messageWidget->animatedShow();
    } else {
        ret = true;
    }
    request->deleteLater();

    return ret;
}
ReturnArguments KCupsConnection::request(ipp_op_e operation,
                                         const char *resource,
                                         const QVariantHash &reqValues,
                                         bool needResponse)
{
    ReturnArguments ret;

    if (!readyToStart()) {
        return ret; // This is not intended to be used in the gui thread
    }

    ipp_t *response = NULL;
    bool needDestName = false;
    int group_tag = IPP_TAG_PRINTER;
    do {
        ipp_t *request;
        bool isClass = false;
        QString filename;
        QVariantHash values = reqValues;

        ippDelete(response);

        if (values.contains(QLatin1String("printer-is-class"))) {
            isClass = values.take(QLatin1String("printer-is-class")).toBool();
        }
        if (values.contains(QLatin1String("need-dest-name"))) {
            needDestName = values.take(QLatin1String("need-dest-name")).toBool();
        }
        if (values.contains(QLatin1String("group-tag-qt"))) {
            group_tag = values.take(QLatin1String("group-tag-qt")).toInt();
        }

        if (values.contains(QLatin1String("filename"))) {
            filename = values.take(QLatin1String("filename")).toString();
        }

        // Lets create the request
        if (values.contains(QLatin1String(KCUPS_PRINTER_NAME))) {
            request = ippNewDefaultRequest(values.take(QLatin1String(KCUPS_PRINTER_NAME)).toString(),
                                           isClass,
                                           operation);
        } else {
            request = ippNewRequest(operation);
        }

        // send our user name on the request too
        ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
                     "requesting-user-name", NULL, cupsUser());

        // Add the requested values to the request
        requestAddValues(request, values);

        // Do the request
        // do the request deleting the response
        if (filename.isEmpty()) {
            response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, resource);
        } else {
            response = cupsDoFileRequest(CUPS_HTTP_DEFAULT, request, resource, filename.toUtf8());
        }
    } while (retry(resource));

    if (response != NULL && needResponse) {
        ret = parseIPPVars(response, group_tag, needDestName);
    }
    ippDelete(response);

    return ret;
}