XDataField_ListSingle(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        QLabel *label = new QLabel(labelText(), parent);
        grid->addWidget(label, row, 0);

        combo = new QComboBox(parent);
        grid->addWidget(combo, row, 1);
        combo->setInsertionPolicy(QComboBox::NoInsertion);

        QString sel;
        if ( !f.value().isEmpty() )
            sel = f.value().first();

        XData::Field::OptionList opts = f.options();
        XData::Field::OptionList::Iterator it = opts.begin();
        for ( ; it != opts.end(); ++it) {
            QString lbl = (*it).label;
            if ( lbl.isEmpty() )
                lbl = (*it).value;

            combo->insertItem(lbl);
            if ( (*it).value == sel )
                combo->setCurrentText( lbl );
        }

        QLabel *req = new QLabel(reqText(), parent);
        grid->addWidget(req, row, 2);

        if ( !f.desc().isEmpty() ) {
            label->setToolTip(f.desc());
            combo->setToolTip(f.desc());
            req->setToolTip(f.desc());
        }
    }
    XDataField_Boolean(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        bool checked = false;
        if ( f.value().count() ) {
            QString s = f.value().first();
            if ( s == "1" || s == "true" || s == "yes" )
                checked = true;
        }

        QLabel *label = new QLabel(labelText(), parent);
        grid->addWidget(label, row, 0);

        check = new QCheckBox(parent);
        check->setChecked(checked);
        grid->addWidget(check, row, 1);

        QLabel *req = new QLabel(reqText(), parent);
        grid->addWidget(req, row, 2);

        if ( !f.desc().isEmpty() ) {
            label->setToolTip(f.desc());
            check->setToolTip(f.desc());
            req->setToolTip(f.desc());
        }
    }
    XDataField_TextMulti(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        QLabel *label = new QLabel(labelText(), parent);
        grid->addWidget(label, row, 0);

        edit = new QTextEdit(parent);
        grid->addWidget(edit, row, 1);

        QString text;
        QStringList val = f.value();
        QStringList::Iterator it = val.begin();
        for ( ; it != val.end(); ++it) {
            if ( !text.isEmpty() )
                text += "\n";
            text += *it;
        }
        edit->setText(text);

        QLabel *req = new QLabel(reqText(), parent);
        grid->addWidget(req, row, 2);

        if ( !f.desc().isEmpty() ) {
            label->setToolTip(f.desc());
            edit->setToolTip(f.desc());
            req->setToolTip(f.desc());
        }
    }
    XDataField_ListMulti(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        QLabel *label = new QLabel(labelText(), parent);
        grid->addWidget(label, row, 0);

        list = new QListWidget(parent);
        grid->addWidget(list, row, 1);
        list->setSelectionMode(QAbstractItemView::MultiSelection);

        XData::Field::OptionList opts = f.options();
        XData::Field::OptionList::Iterator it = opts.begin();
        for ( ; it != opts.end(); ++it) {
            QString lbl = (*it).label;
            if ( lbl.isEmpty() )
                lbl = (*it).value;

            QListWidgetItem* item = new QListWidgetItem(lbl,list);

            QStringList val = f.value();
            QStringList::Iterator sit = val.begin();
            for ( ; sit != val.end(); ++sit)
                if ( (*it).label == *sit || (*it).value == *sit )
                    list->setItemSelected(item, true);
        }

        QLabel *req = new QLabel(reqText(), parent);
        grid->addWidget(req, row, 2);

        if ( !f.desc().isEmpty() ) {
            label->setToolTip(f.desc());
            list->setToolTip(f.desc());
            req->setToolTip(f.desc());
        }
    }
    XDataField_TextSingle(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        QString text;
        if ( f.value().count() )
            text = f.value().first();

        QLabel *label = new QLabel(labelText(), parent);
        grid->addWidget(label, row, 0);

        edit = new QLineEdit(parent);
        edit->setText(text);
        grid->addWidget(edit, row, 1);

        QLabel *req = new QLabel(reqText(), parent);
        grid->addWidget(req, row, 2);

        if ( !f.desc().isEmpty() ) {
            label->setToolTip(f.desc());
            edit->setToolTip(f.desc());
            req->setToolTip(f.desc());
        }
    }
    XDataField_Fixed(XData::Field f, QGridLayout *grid, int row, QWidget *parent)
        : XDataField(f)
    {
        QString text;
        QStringList val = f.value();
        QStringList::Iterator it = val.begin();
        for ( ; it != val.end(); ++it) {
            if ( !text.isEmpty() )
                text += "<br>";
            text += *it;
        }

        QLabel *fixed = new QLabel("<qt>" + text + "</qt>", parent);
        grid->addMultiCellWidget(fixed, row, row, 0, 2);

        if ( !f.desc().isEmpty() ) {
            fixed->setToolTip(f.desc());
        }
    }
Example #7
0
void HttpFileUpload::start()
{
    if (d->state != State::None) // Attempt to start twice?
        return;

    setState(State::GettingSlot);

    d->result.statusCode = HttpFileUpload::ErrorCode::NoError;
    static QList<QSet<QString>> featureOptions;
    if (featureOptions.isEmpty()) {
        featureOptions << (QSet<QString>() << xmlns_v0_2_5) << (QSet<QString>() << xmlns_v0_3_1);
    }
    d->client->serverInfoManager()->queryServiceInfo(
                QLatin1String("store"), QLatin1String("file"),
                featureOptions, QRegExp("^(upload|http|stor|file|dis|drive).*"), ServerInfoManager::SQ_CheckAllOnNoMatch,
                [this](const QList<DiscoItem> &items)
    {
        d->httpHosts.clear();
        for (const auto &item: items) {
            const QStringList &l = item.features().list();
            XEP0363::version ver = XEP0363::vUnknown;
            QString xmlns;
            quint64 sizeLimit = 0;
            if (l.contains(xmlns_v0_3_1)) {
                ver = XEP0363::v0_3_1;
                xmlns = xmlns_v0_3_1;
            } else if (l.contains(xmlns_v0_2_5)) {
                ver = XEP0363::v0_2_5;
                xmlns = xmlns_v0_2_5;
            }
            if (ver != XEP0363::vUnknown) {
                QList<std::pair<HttpHost,int>> hosts;
                const XData::Field field = item.registeredExtension(xmlns).getField(QLatin1String("max-file-size"));
                if (field.isValid() && field.type() == XData::Field::Field_TextSingle)
                    sizeLimit = field.value().at(0).toULongLong();
                HttpHost host;
                host.ver = ver;
                host.jid = item.jid();
                host.sizeLimit = sizeLimit;
                QVariant metaProps(d->client->serverInfoManager()->serviceMeta(host.jid, "httpprops"));
                if (metaProps.isValid()) {
                    host.props = HostProps(metaProps.value<int>());
                } else {
                    host.props = SecureGet | SecurePut;
                    if (ver == XEP0363::v0_3_1)
                        host.props |= NewestVer;
                }
                int value = 0;
                if (host.props & SecureGet) value += 5;
                if (host.props & SecurePut) value += 5;
                if (host.props & NewestVer) value += 3;
                if (host.props & Failure) value -= 15;
                if (!sizeLimit || d->fileSize < sizeLimit)
                    hosts.append({host,value});

                // no sorting in preference order. most preferred go first
                std::sort(hosts.begin(), hosts.end(), [](const auto &a, const auto &b){
                    return a.second > b.second;
                });
                for (auto &hp: hosts) {
                    d->httpHosts.append(hp.first);
                }
            }
        }
        //d->currentHost = d->httpHosts.begin();
        if (d->httpHosts.isEmpty()) { // if empty as the last resort check all services
            d->result.statusCode   = HttpFileUpload::ErrorCode::NoUploadService;
            d->result.statusString = "No suitable http upload services were found";
            done(State::Error);
        } else {
            tryNextServer();
        }
    });
}