//! initializes this editor with \a add value void KexiBlobTableEdit::setValueInternal(const QVariant& add, bool removeOld) { if (!d->setValueInternalEnabled) return; if (removeOld) d->value = add.toByteArray(); else //do not add "m_origValue" to "add" as this is QByteArray d->value = m_origValue.toByteArray(); #if 0 //todo? QByteArray val = m_origValue.toByteArray(); kDebug() << "KexiBlobTableEdit: Size of BLOB: " << val.size(); m_tempFile = new KTemporaryFile(); m_tempFile->open(); kDebug() << "KexiBlobTableEdit: Creating temporary file: " << m_tempFile->fileName(); QDataStream stream(m_tempFile); stream->writeRawBytes(val.data(), val.size()); delete m_tempFile; m_tempFile = 0; KMimeMagicResult* mmr = KMimeMagic::self()->findFileType(m_tempFile->fileName()); kDebug() << "KexiBlobTableEdit: Mimetype = " << mmr->mimeType(); setViewWidget(new QWidget(this)); #endif }
Parser::ImagePixmap* Parser::getPair(const QString &filenameOld) const { // is it in there? ImagePixmap *pair; { pair=mImageCache.find(filenameOld); if (pair) return pair; } QString filename=fileItem(filenameOld); QImage image; // Determine file-format trough mimetype (no stupid .ext test) KMimeMagicResult * result = KMimeMagic::self()->findFileType( filename ); if ( result->mimeType() == "image/png" ) { // image = NoatunApp::readPNG(filenameNoCase(filename)); QImageIO iio; iio.setFileName( filenameNoCase(filename) ); // forget about gamma-value, fix for broken PNGs iio.setGamma( 0.00000001 ); if ( iio.read() ) { image = iio.image(); image.setAlphaBuffer(false); // we don't want/support alpha-channels } else { kdDebug(66666) << "Could not load file: " << filename.latin1() << endl; } } else { image = QImage(filenameNoCase(filename)); } //add to the cache QPixmap pixmap; pixmap.convertFromImage(image, QPixmap::AutoColor|QPixmap::ThresholdDither|QPixmap::AvoidDither); pair = new Parser::ImagePixmap; pair->mImage = image; pair->mPixmap = pixmap; mImageCache.insert(filenameOld, pair); return pair; }
void KJFilename::mouseRelease(const QPoint &, bool in) { if (!in) // only do something if users is still inside the button return; if ( !napp->player()->current() ) return; KURL dirURL = napp->player()->current().url().upURL(); KMimeMagicResult *result = KMimeMagic::self()->findFileType( dirURL.path() ); // TODO: Maybe test for protocol type? // if ( napp->player()->current().url().protocol() == "file" ) if ( result->isValid() ) KRun::runURL ( dirURL, result->mimeType() ); }
void KFMJob::testMimeType( const char *_text, int _len ) { bCheckedMimeType = TRUE; KMimeMagicResult *result = KMimeType::findBufferType( _text, _len ); if ( strcmp( "text/html", result->getContent() ) == 0 ) { isHTML = TRUE; emit mimeType( "text/html" ); emit data( _text, _len ); } else if ( result->getAccuracy() > 0 ) { emit mimeType( result->getContent() ); } else { // debugT("UNKNOWN TYPE\n"); // Dont know the mime type emit mimeType( 0L ); } }
/* ---------------------------------------------------------------------------------- */ void ChmProtocol::get( const KURL& url ) { /** When :catalog is appended to the end, a plain-text representation of the catalog * is given out where each entry consists of four lines, an integer representing the parent * of the node, an integer representing a node's ID, the Title of the Node, and it's hyperlink. * When :contents is appended, all contained htm- and html-files will be printed, each in a line. */ kdDebug() << "kio_chm::get(const KURL& url) " << url.path() << endl; bool catalog = false; bool contents = false; QString bigpath = url.path(); if(bigpath.endsWith(":catalog")) { catalog = true; int len = QString(":catalog").length(); bigpath.remove(bigpath.length() - len, len); ///strip :catalog from the end } if(bigpath.endsWith(":contents")) { contents = true; int len = QString(":contents").length(); bigpath.remove(bigpath.length() - len, len); ///strip :catalog from the end } QString path; if ( !checkNewFile( bigpath, path ) ) { error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() ); return; } if (m_dirMap.find(path) == m_dirMap.end()) { error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() ); return; } QByteArray theData; //init.. //added by lucida [email protected] QString fname = QString(); QString chmpath = QString(); KTempFile f("",".html"); fname = f.name(); QTextStream *t = f.textStream(); QString firstPage = QString(""); QString m_strIndex = QString(""); QString tmpstr = QString(""); bool m_bIndex = 0; if(contents) { QString output; KURL u = url; ChmDirectoryMap::Iterator it; for ( it = m_dirMap.begin(); it != m_dirMap.end(); ++it) { u.setPath(bigpath); u.addPath(it.key()); output += u.prettyURL() + "\n"; } data(output.local8Bit()); processedSize(output.length()); finished(); return; } //try get some page to display, if the chm missing index ChmDirectoryMap::Iterator it; for ( it = m_dirMap.begin(); it != m_dirMap.end(); ++it) { tmpstr.sprintf("%s", it.key().latin1()); if ((m_strIndex == "") && (tmpstr.endsWith(".htm") || tmpstr.endsWith(".html"))) m_strIndex = tmpstr; if ((tmpstr == "/index.htm") || (tmpstr == "/index.html")) { m_strIndex = tmpstr; break; } } m_strIndex.remove(0,1); if (path == "/" || catalog) { bool htmlOutput = !catalog; int offset = m_dirMap["/@contents"].offset; int length = m_dirMap["/@contents"].length; theData.setRawData(&m_contents[offset], length); QString s(theData); QString output; QRegExp object("<OBJECT type=\"text/sitemap\">(.*)</OBJECT>", false); object.setMinimal(true); QRegExp nameParam("<param name=\"Name\" value=\"(.*)\">", false); nameParam.setMinimal(true); QRegExp localParam("<param name=\"Local\" value=\"(.*)\">", false); localParam.setMinimal(true); QRegExp mergeParam("<param name=\"Merge\" value=\"(.*)\">", false); mergeParam.setMinimal(true); std::stack<int> parents; int counter = 1; int current = 0; int old = 0, pos = 0; parents.push(0); while ((pos = s.find(object, pos)) != -1) { if(htmlOutput) output += s.mid(old, pos - old); if(catalog) { QRegExp ex("<UL>|</UL>", false); ex.setMinimal(true); QString ms = s.mid(old, pos - old); int pos = 0; while( (pos = ms.find(ex, pos)) != -1) { if(ms.mid(pos, 4) == "<UL>") { parents.push(current); } else { if(parents.empty()) { } else { current = parents.top(); parents.pop(); } } pos++; } } pos += object.matchedLength(); old = pos; QString obj = object.cap(1); QString name, local; if (obj.find(nameParam) != -1) { name = nameParam.cap(1); if (obj.find(localParam) != -1) { local = localParam.cap(1); //output += "<a href=\"" + local + "\">" + name + "</a>"; //added by lucida [email protected] if (local != "" && local != "/") { if(!catalog) { output += "<a target=\"browse\" href=\"" + url.url() + local + "\">" + name + "</a>"; } else { current = counter; ++counter; KURL u = url; u.setPath(bigpath + local); QString str; output += str.sprintf("%i\n%i\n", parents.top(), current); output += name + "\n" + u.prettyURL() + "\n"; } m_bIndex = 1; if (firstPage == "") firstPage = url.url()+QString::fromLocal8Bit(local.latin1()); } else if(htmlOutput) output += name; } else { if(htmlOutput) output += name; } } if (obj.find(mergeParam) != -1 && htmlOutput) { QString link = mergeParam.cap(1); QString href = link.left(link.find("::")); QString path = m_chmFile.left(m_chmFile.findRev("/") + 1); //output += " (<a href=\"" + path + href + "\">link</a>)"; m_bIndex = 1; output += " (<a target=\"browse\" href=\"" + url.url() + path + href + "\">link</a>)"; if (firstPage == "") firstPage = url.url()+QString::fromLocal8Bit(local.latin1()); } } if(htmlOutput) output += s.mid(old); //set left pane //added by lucida, [email protected] QString lframe = QString("</HEAD><FRAMESET COLS=\"25%,*\">\n"); lframe += "<FRAME NAME=\"index\" src=\"file:"+ fname+"\"" + " marginwidth=\"0\"></FRAME>\n"; if (!m_bIndex) { lframe = "</HEAD><FRAMESET>"; firstPage = url.url() + QString::fromLocal8Bit(m_strIndex.latin1()); } theData.resetRawData(&m_contents[offset], length); //KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( output, path ); //kdDebug() << "Emitting mimetype " << result->mimeType() << endl; //mimeType( result->mimeType() ); /* QCString output1 = (QCString)(output.latin1()); data(output1); processedSize(output1.length());*/ //construct the frame //added by lucida [email protected] QString framestr = QString("<HTML><HEAD>\n"); framestr += lframe; framestr += "<FRAME NAME=\"browse\" src=\"" + firstPage + "\">\n"; framestr += "</FRAME>\n"; framestr += "</FRAMESET></HTML>"; //write index file //added by lucida [email protected] *t << QString::fromLocal8Bit(output.latin1()) << endl; if(catalog) { data(output.local8Bit()); processedSize(output.length()); } else { data(framestr.local8Bit()); processedSize(framestr.length()); } } else { int offset = m_dirMap[path].offset; int length = m_dirMap[path].length; totalSize(length); theData.setRawData(&m_contents[offset], length); KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( theData, path ); kdDebug() << "Emitting mimetype " << result->mimeType() << endl; mimeType( result->mimeType() ); data(theData); theData.resetRawData(&m_contents[offset], length); processedSize(length); } finished(); }
void PakProtocol::get(const KURL &url) { kdDebug(PAK_DEBUG_ID) << "ArchiveProtocol::get " << url << endl; QString path; KIO::Error errorNum; if ( !checkNewFile( url, path, errorNum ) ) { if ( errorNum == KIO::ERR_CANNOT_OPEN_FOR_READING ) { // If we cannot open, it might be a problem with the archive header (e.g. unsupported format) // Therefore give a more specific error message error( KIO::ERR_SLAVE_DEFINED, i18n( "Could not open the file, probably due to an unsupported file format.\n%1") .arg( url.prettyURL() ) ); return; } else { // We have any other error error( errorNum, url.prettyURL() ); return; } } kdDebug(PAK_DEBUG_ID) << "Continue getting" << endl; path = QString::fromLocal8Bit(remoteEncoding()->encode(path)); kdDebug(PAK_DEBUG_ID) << "Path > " << path << endl; const KArchiveDirectory* root = _pakFile->directory(); const KArchiveEntry* archiveEntry = root->entry( path ); kdDebug(PAK_DEBUG_ID) << "Check if no archiveEntry > " << archiveEntry << endl; if ( !archiveEntry ) { error( KIO::ERR_DOES_NOT_EXIST, url.prettyURL() ); return; } kdDebug(PAK_DEBUG_ID) << "archiveEntry::name > " << archiveEntry->name() << endl; if ( archiveEntry->isDirectory() ) { error( KIO::ERR_IS_DIRECTORY, url.prettyURL() ); return; } const KArchiveFile* archiveFileEntry = static_cast<const KArchiveFile *>(archiveEntry); if ( !archiveEntry->symlink().isEmpty() ) { kdDebug(7109) << "Redirection to " << archiveEntry->symlink() << endl; KURL realURL; if (archiveEntry->symlink().startsWith("/")) { // absolute path realURL.setPath(archiveEntry->symlink() ); // goes out of tar:/, back into file: } else { realURL = KURL( url, archiveEntry->symlink() ); } kdDebug(7109) << "realURL= " << realURL << endl; redirection( realURL ); finished(); return; } //kdDebug(7109) << "Preparing to get the archive data" << endl; /* * The easy way would be to get the data by calling archiveFileEntry->data() * However this has drawbacks: * - the complete file must be read into the memory * - errors are skipped, resulting in an empty file */ QIODevice* io = 0; // Getting the device is hard, as archiveFileEntry->device() is not virtual! if ( url.protocol() == "pak" ) { io = archiveFileEntry->device(); } else { // Wrong protocol? Why was this not catched by checkNewFile? kdWarning(7109) << "Protocol " << url.protocol() << " not supported by this IOSlave; " << k_funcinfo << endl; error( KIO::ERR_UNSUPPORTED_PROTOCOL, url.protocol() ); return; } if (!io) { error( KIO::ERR_SLAVE_DEFINED, i18n( "The archive file could not be opened, perhaps because the format is unsupported.\n%1" ) .arg( url.prettyURL() ) ); return; } if ( !io->open( IO_ReadOnly ) ) { error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.prettyURL() ); return; } totalSize( archiveFileEntry->size() ); // Size of a QIODevice read. It must be large enough so that the mime type check will not fail const int maxSize = 0x100000; // 1MB int bufferSize = kMin( maxSize, archiveFileEntry->size() ); QByteArray buffer ( bufferSize ); if ( buffer.isEmpty() && bufferSize > 0 ) { // Something went wrong error( KIO::ERR_OUT_OF_MEMORY, url.prettyURL() ); return; } bool firstRead = true; // How much file do we still have to process? int fileSize = archiveFileEntry->size(); KIO::filesize_t processed = 0; while ( !io->atEnd() && fileSize > 0 ) { if ( !firstRead ) { bufferSize = kMin( maxSize, fileSize ); buffer.resize( bufferSize, QGArray::SpeedOptim ); } const Q_LONG read = io->readBlock( buffer.data(), buffer.size() ); // Avoid to use bufferSize here, in case something went wrong. if ( read != bufferSize ) { kdWarning(7109) << "Read " << read << " bytes but expected " << bufferSize << endl; error( KIO::ERR_COULD_NOT_READ, url.prettyURL() ); return; } if ( firstRead ) { // We use the magic one the first data read // (As magic detection is about fixed positions, we can be sure that it is enough data.) KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( buffer, path ); kdDebug(7109) << "Emitting mimetype " << result->mimeType() << endl; mimeType( result->mimeType() ); firstRead = false; } data( buffer ); processed += read; processedSize( processed ); fileSize -= bufferSize; } io->close(); delete io; data( QByteArray() ); finished(); /*kdDebug(PAK_DEBUG_ID) << "Entering get()" << endl; mimetype("text/plain"); QCString str("Hello Pak World!!"); data(str); finished(); kdDebug(PAK_DEBUG_ID) << "Exiting get()" << endl;*/ }