Ejemplo n.º 1
0
void DataBlockFile::prepareToRead()
{
//    std::cerr << "DataBlockFile[" << m_fileName << "]: prepareToRead" << std::endl;
    if (!m_file.isReadable()) {
        m_file.close();
        m_file.open(QIODevice::ReadOnly);
        Q_ASSERT(m_file.isReadable());
    }
}
Ejemplo n.º 2
0
/** \fn copy(QFile&,QFile&,uint)
 *  \brief Copies src file to dst file.
 *
 *   If the dst file is open, it must be open for writing.
 *   If the src file is open, if must be open for reading.
 *
 *   The files will be in the same open or close state after
 *   this function runs as they were prior to this function being called.
 *
 *   This function does not care if the files are actual files.
 *   For compatibility with pipes and socket streams the file location
 *   will not be reset to 0 at the end of this function. If the function
 *   is successful the file pointers will be at the end of the copied
 *   data.
 *
 *  \param dst Destination QFile
 *  \param src Source QFile
 *  \param block_size Optional block size in bytes, must be at least 1024,
 *                    otherwise the default of 16 KB will be used.
 *  \return bytes copied on success, -1 on failure.
 */
long long copy(QFile &dst, QFile &src, uint block_size)
{
    uint buflen = (block_size < 1024) ? (16 * 1024) : block_size;
    char *buf = new char[buflen];
    bool odst = false, osrc = false;

    if (!buf)
        return -1LL;

    if (!dst.isWritable() && !dst.isOpen())
        odst = dst.open(QIODevice::Unbuffered|QIODevice::WriteOnly|QIODevice::Truncate);

    if (!src.isReadable() && !src.isOpen())
        osrc = src.open(QIODevice::Unbuffered|QIODevice::ReadOnly);

    bool ok = dst.isWritable() && src.isReadable();
    long long total_bytes = 0LL;
    while (ok)
    {
        long long rlen, wlen, off = 0;
        rlen = src.read(buf, buflen);
        if (rlen<0)
        {
            VERBOSE(VB_IMPORTANT, "util.cpp:copy: read error");
            ok = false;
            break;
        }
        if (rlen==0)
            break;

        total_bytes += (long long) rlen;

        while ((rlen-off>0) && ok)
        {
            wlen = dst.write(buf + off, rlen - off);
            if (wlen>=0)
                off+= wlen;
            if (wlen<0)
            {
                VERBOSE(VB_IMPORTANT, "util.cpp:copy: write error");
                ok = false;
            }
        }
    }
    delete[] buf;

    if (odst)
        dst.close();

    if (osrc)
        src.close();

    return (ok) ? total_bytes : -1LL;
}
Ejemplo n.º 3
0
bool Storage::getUserLimits (const QString& user, QVariantMap& target)
{
    QString val;
    QFile file;
    QString filename = QString (CLEPSYDRA_WORKING_FOLDER).append(CLEPSYDRA_JSON_USERDATA_FILENAME);
    file.setFileName(filename);
    if ( !file.isReadable() ) {
        //
        return false;
    }
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)==true) {
        val = file.readAll();
        file.close();
    } else {
        qDebug () << filename << "json file not found";
        return false;
    }

    QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
    if (d.isEmpty()) {
        qDebug () << "Not valid document.";
        return false;
    }

    QJsonObject obj = d.object();
    QJsonValue mapArray = obj.value(user);
    if (mapArray.isUndefined()) {
        qDebug () << "User not found from limits.";
        return false;
    }

    QVariantMap dataC = mapArray.toObject().toVariantMap();
    target = dataC.take(user).toMap();
    return true;
}
Ejemplo n.º 4
0
    bool StreamMail::LoadFile(const QString file) {
        ////qFatal(" unable to read ... ");
        bool filled = false;
        if (clear()) {

            QFile *f = new QFile(file);

            if (f->exists()) {
                /////
                if (f->open(QIODevice::ReadOnly)) {
                    //// read line by line 
                    if (f->isReadable()) {
                        linenr = -1;
                        while (!f->atEnd()) {
                            linenr++;
                            QByteArray crk = f->read(76);
                            d->write(crk);
                        }
                        f->close();
                        d->write(QByteArray("\n--\n--\n--\n--"));
                    }
                } else {
                    qDebug() << "######## file errors:" << f->errorString() << "\n";
                }
            }
        }

        if (d->bytesAvailable() > 23) {
            filled = true;
        }
        d->seek(0);
        return filled;
    }
Ejemplo n.º 5
0
QByteArray FoxGroundDocument::loadbinfile(const QString file) {
    QFile *f = new QFile(file);
    QByteArray x;
    if (f->exists()) {
        if (f->open(QIODevice::ReadOnly)) {
            if (f->isReadable()) {
                x = f->readAll();
            }
        }
    }
    f->close();
    return x;
}
Ejemplo n.º 6
0
void FoxGroundDocument::_loader() {
    d->write(QByteArray());
    if (d->bytesAvailable() == 0) {
        QFile *f = new QFile(document_file);
        if (f->exists()) {
            if (f->open(QIODevice::ReadOnly)) {
                if (f->isReadable()) {
                    d->write(f->readAll());
                    f->close();
                }
            }
        }
    }
}
Ejemplo n.º 7
0
void ut_load::testIODevice()
{
    QuillImageFilter *filter =
        QuillImageFilterFactory::createImageFilter("org.maemo.load");
    QFile *fileIO = new QFile("/usr/share/quillimagefilter-tests/images/16_color_palette.png");
    QVERIFY(fileIO->open(QIODevice::ReadOnly));
    QCOMPARE(fileIO->isOpen(),true);
    QCOMPARE(fileIO->isReadable(),true);
    QVERIFY(fileIO);
    filter->setIODevice(fileIO);
    QCOMPARE(fileIO, filter->iODevice());
    QImage image("/usr/share/quillimagefilter-tests/images/16_color_palette.png");
    QImage loadedImage1 = filter->apply(image);
    QVERIFY(Unittests::compareImage(loadedImage1, image));
    delete fileIO;
    delete filter;
}
Ejemplo n.º 8
0
QFile *FileExplorer::openFileForUpload() {
  QString fileName = QFileDialog::getOpenFileName(
      this->baseWidget, tr("Select file"), QDir::currentPath()
  );

  if (! fileName.isEmpty()){
    QFile *file = new QFile(fileName);

    if (! file->exists()) {
      QMessageBox::information(
          this->baseWidget,tr("File explorer"), 
          tr("Cannot load %1.").arg(fileName)
      );
      return NULL;
    }

    QFileInfo fInfo(*file);
    uint byteCount = file->bytesAvailable();

    if (! fInfo.isFile()) {
        QMessageBox::information(
           this->baseWidget, tr("Uploader"), 
	   tr("%1. is not a regular file").arg(fileName)
        );
    } else if (! file->isReadable()) {
        QMessageBox::information(
           this->baseWidget, tr("Uploader"), 
           tr("No read permissions for %1.").arg(fileName)
        );
    } else if (! byteCount) {
        QMessageBox::information(
           this->baseWidget, tr("Uploader"), 
           tr("Attempting to upload an empty file")
        );
    } else { // Good for return
        return file;
    }

    // By this point, release memory
    delete file;
  }

  return NULL;
}
Ejemplo n.º 9
0
std::ifstream QFileToifstream(QFile & file) {
    Q_ASSERT(file.isReadable());
    return std::ifstream(::_fdopen(file.handle(), "r"));
}
Ejemplo n.º 10
0
void WPUtils::uploadFile(QString furl)
{
	QFile* file = new QFile(furl);



	if ( file->exists() )
		qDebug() << "file founded.";
	else qDebug() << "file not found.";

	if ( file->isReadable() )
		qDebug() << "readable";
	else qDebug() << "unreadable";

	file->open(QIODevice::ReadOnly);

	QFileInfo fileInfo(file->fileName());
	QString nome(fileInfo.fileName());

	_xml.clear();
	QXmlStreamWriter sw(&_xml);

	sw.setAutoFormatting(true);
	sw.writeStartDocument();

	sw.writeStartElement("methodCall");
	sw.writeTextElement("methodName", "wp.uploadFile");
	sw.writeStartElement("params");
	sw.writeStartElement("param");
	sw.writeCharacters("blog_id");
	sw.writeTextElement("value", _blogid);
	sw.writeEndElement();
	sw.writeStartElement("param");
	sw.writeCharacters("username");
	sw.writeTextElement("value", _username);
	sw.writeEndElement();
	sw.writeStartElement("param");
	sw.writeCharacters("password");
	sw.writeTextElement("value", _password);
	sw.writeEndElement();
	sw.writeStartElement("struct");
	sw.writeStartElement("member");
	sw.writeTextElement("name", "name");
	sw.writeTextElement("value", nome);
	sw.writeEndElement();
	sw.writeStartElement("member");
	sw.writeTextElement("name", "type");
	QImageReader reader(furl);

	sw.writeTextElement("value", "image/"+reader.format());
	sw.writeEndElement();
	sw.writeStartElement("member");
	sw.writeTextElement("name", "bits");
	sw.writeStartElement("value");
	sw.writeStartElement("base64");
	sw.writeCharacters(file->readAll().toBase64());
	sw.writeEndElement();
	sw.writeEndElement();
	sw.writeEndElement();
	sw.writeStartElement("member");
	sw.writeTextElement("name", "overwrite");
	sw.writeTextElement("value", "true");
	sw.writeEndElement();
	sw.writeEndElement();
	sw.writeEndElement();
	sw.writeEndDocument();

	QNetworkAccessManager *manager = new QNetworkAccessManager();

	QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

	QUrl url;

	url.setUrl(_endpoint);
	QNetworkRequest request(url);
	int cmd = 0;
	request.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 1), QVariant((int) cmd)); /* not sure */
	request.setRawHeader("User-Agent", "wp-bb10/0.0.1");
	request.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml");


	manager->post(request,_xml);
}