void KDesktopFileTest::testActionGroup() { KTemporaryFile file; file.setPrefix("test1"); QVERIFY( file.open() ); const QString fileName = file.fileName(); QTextStream ts( &file ); ts << "[Desktop Entry]\n" "Actions=encrypt;\n" "[Desktop Action encrypt]\n" "Name=Encrypt file\n" "\n"; file.close(); QVERIFY(QFile::exists(fileName)); KDesktopFile df(fileName); QCOMPARE(df.readType(), QString()); QCOMPARE(df.fileName(), fileName); QCOMPARE(df.readActions(), QStringList() << "encrypt"); QCOMPARE(df.hasActionGroup("encrypt"), true); QCOMPARE(df.hasActionGroup("doesnotexist"), false); KConfigGroup cg = df.actionGroup("encrypt"); QVERIFY(cg.hasKey("Name")); QCOMPARE(cg.readEntry("Name"), QString("Encrypt file")); }
KJotsBook* KnowItImporter::importFromUrl( KUrl url ) { KJotsBook *book = new KJotsBook(); buildNoteTree(url); // foreach () // kDebug(); buildDomDocument(); KTemporaryFile file; file.setPrefix( KStandardDirs::locateLocal( "data", "kjots/" ) ); file.setSuffix( ".book" ); file.setAutoRemove( false ); if ( file.open() ) { file.write( "<?xml version='1.0' encoding='UTF-8' ?>\n<!DOCTYPE KJots>\n<KJots>\n" ); file.write( m_domDoc.toByteArray() ); file.write( "</KJots>\n" ); kDebug() << file.fileName(); QString newFileName = file.fileName(); file.close(); book->openBook( newFileName ); } return book; }
void KTNEFMain::slotShowMessageText() { if ( !mParser->message() ) { return; } QString rtf = mParser->message()->rtfString(); if ( !rtf.isEmpty() ) { KTemporaryFile *tmpFile = new KTemporaryFile(); tmpFile->setPrefix( KGlobal::dirs()->localkdedir() + "/share/apps/ktnef/tmp/" ); tmpFile->setSuffix( QLatin1String( ".rtf" ) ); tmpFile->open(); tmpFile->setPermissions( QFile::ReadUser ); tmpFile->write( rtf.toLocal8Bit() ); tmpFile->close(); KRun::runUrl( KUrl( tmpFile->fileName() ), "text/rtf", this, true ); delete tmpFile; } else { KMessageBox::error( this, i18nc( "@info", "The message does not contain any Rich Text data." ) ); } }
bool SSMLConvert::transform(const QString &text, const QString &xsltFilename) { m_xsltFilename = xsltFilename; /// Write @param text to a temporary file. KTemporaryFile inFile; inFile.setPrefix(QLatin1String( "kttsd-" )); inFile.setSuffix(QLatin1String( ".ssml" )); inFile.setAutoRemove(false); inFile.open(); m_inFilename = inFile.fileName(); QTextStream wstream (&inFile); // TODO: Is encoding an issue here? // TODO: It would be nice if we detected whether the XML is properly formed // with the required xml processing instruction and encoding attribute. If // not wrap it in such. But maybe this should be handled by SpeechData::setText()? wstream << text; inFile.flush(); // Get a temporary output file name. KTemporaryFile outFile; outFile.setPrefix(QLatin1String( "kttsd-" )); outFile.setSuffix(QLatin1String( ".output" )); outFile.setAutoRemove(false); outFile.open(); m_outFilename = outFile.fileName(); /// Spawn an xsltproc process to apply our stylesheet to our SSML file. QStringList args; m_xsltProc = new QProcess; args << QLatin1String( "-o" ) << m_outFilename << QLatin1String( "--novalid" ) << m_xsltFilename << m_inFilename; // Warning: This won't compile under KDE 3.2. See FreeTTS::argsToStringList(). // kDebug() << "SSMLConvert::transform: executing command: " << // m_xsltProc->args() << endl; connect(m_xsltProc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessExited())); if (!m_xsltProc->execute(QLatin1String("xsltproc"), args)) { kDebug() << "SSMLConvert::transform: Error starting xsltproc"; return false; } m_state = tsTransforming; return true; }
bool downloadRemoteUrls(const QString& collectionName, const KUrl::List& _list, RemoteUrlHash* hash) { Q_ASSERT(hash); KUrl::List list; Q_FOREACH(const KUrl& url, _list) { if (!url.isLocalFile()) { list << url; } } if (list.count() == 0) { return true; } logInfo( i18n("Downloading remote files for \"%1\"", collectionName) ); mProgressDialog->progressWidget()->setTotal(list.count()); int count = 0; Q_FOREACH(const KUrl& url, list) { if (mProgressDialog->isHidden()) { return false; } KTemporaryFile* tempFile = new KTemporaryFile; // Ensure the tempFile gets deleted when mProgressDialog is closed tempFile->setParent(mProgressDialog); tempFile->setPrefix("htmlexport-"); if (!tempFile->open()) { delete tempFile; logError(i18n("Could not open temporary file")); return false; } const QString tempPath = KStandardDirs::locate("tmp", tempFile->fileName()); KIO::Job* job = KIO::file_copy(url, KUrl::fromPath(tempPath), -1 /* permissions */, KIO::Overwrite); if (KIO::NetAccess::synchronousRun(job, mProgressDialog)) { hash->insert(url, tempFile->fileName()); } else { logWarning(i18n("Could not download %1", url.prettyUrl())); hash->insert(url, QString()); } ++count; mProgressDialog->progressWidget()->setProgress(count); } return true; }
bool SocketConnectionBackend::listenForRemote() { Q_ASSERT(state == Idle); Q_ASSERT(!socket); Q_ASSERT(!localServer); // !tcpServer as well if (mode == LocalSocketMode) { QString prefix = KStandardDirs::locateLocal("socket", KGlobal::mainComponent().componentName()); KTemporaryFile *socketfile = new KTemporaryFile(); socketfile->setPrefix(prefix); socketfile->setSuffix(QLatin1String(".slave-socket")); if (!socketfile->open()) { errorString = i18n("Unable to create io-slave: %1", strerror(errno)); delete socketfile; return false; } QString sockname = socketfile->fileName(); KUrl addressUrl(sockname); addressUrl.setProtocol("local"); address = addressUrl.url(); delete socketfile; // can't bind if there is such a file localServer = new KLocalSocketServer(this); if (!localServer->listen(sockname, KLocalSocket::UnixSocket)) { errorString = localServer->errorString(); delete localServer; localServer = 0; return false; } connect(localServer, SIGNAL(newConnection()), SIGNAL(newConnection())); } else { tcpServer = new QTcpServer(this); tcpServer->listen(QHostAddress::LocalHost); if (!tcpServer->isListening()) { errorString = tcpServer->errorString(); delete tcpServer; tcpServer = 0; return false; } address = "tcp://127.0.0.1:" + QString::number(tcpServer->serverPort()); connect(tcpServer, SIGNAL(newConnection()), SIGNAL(newConnection())); } state = Listening; return true; }
void KDesktopFileTest::testUnsuccessfulTryExec() { KTemporaryFile file; file.setPrefix("test1"); QVERIFY( file.open() ); const QString fileName = file.fileName(); QTextStream ts( &file ); ts << "[Desktop Entry]\n" "TryExec=/does/not/exist\n" "\n"; file.close(); QVERIFY(QFile::exists(fileName)); KDesktopFile df(fileName); QCOMPARE(df.tryExec(), false); }
static void storeThumbnailToDiskCache(const QString& path, const QImage& image) { LOG(path); KTemporaryFile tmp; tmp.setPrefix(path + ".gwenview.tmp"); tmp.setSuffix(".png"); if (!tmp.open()) { kWarning() << "Could not create a temporary file."; return; } if (!image.save(tmp.fileName(), "png")) { kWarning() << "Could not save thumbnail"; return; } KDE_rename(QFile::encodeName(tmp.fileName()), QFile::encodeName(path)); }
void KSaveFileTest::test_ksavefile() { QString targetFile; { //This will be the file we eventually write to. Yes, I know you //should never remove the temporaryfile and then expect the filename //to continue to be unique, but this is a test for crying out loud. :) KTemporaryFile file; file.setPrefix("ksavefiletest"); QVERIFY( file.open() ); targetFile = file.fileName(); } { //Test basic functionality KSaveFile saveFile; saveFile.setFileName(targetFile); QVERIFY( saveFile.open() ); QVERIFY( !QFile::exists(targetFile) ); QTextStream ts ( &saveFile ); ts << "This is test data one.\n"; ts.flush(); QCOMPARE( saveFile.error(), QFile::NoError ); QVERIFY( !QFile::exists(targetFile) ); QVERIFY( saveFile.finalize() ); QVERIFY( QFile::exists(targetFile) ); QFile::remove(targetFile); QVERIFY( !QFile::exists(targetFile) ); } { //Make sure destructor does what it is supposed to do. { KSaveFile saveFile; saveFile.setFileName(targetFile); QVERIFY( saveFile.open() ); QVERIFY( !QFile::exists(targetFile) ); } QVERIFY( QFile::exists(targetFile) ); QFile::remove(targetFile); QVERIFY( !QFile::exists(targetFile) ); } { //Test some error conditions KSaveFile saveFile; QVERIFY( !saveFile.open() ); //no filename saveFile.setFileName(targetFile); QVERIFY( saveFile.open() ); QVERIFY( !QFile::exists(targetFile) ); QVERIFY( !saveFile.open() ); //already open QVERIFY( saveFile.finalize() ); QVERIFY( QFile::exists(targetFile) ); QVERIFY( !saveFile.finalize() ); //already finalized QFile::remove(targetFile); QVERIFY( !QFile::exists(targetFile) ); } { //Do it again, aborting this time KSaveFile saveFile ( targetFile ); QVERIFY( saveFile.open() ); QVERIFY( !QFile::exists(targetFile) ); QTextStream ts ( &saveFile ); ts << "This is test data two.\n"; ts.flush(); QCOMPARE( saveFile.error(), QFile::NoError ); QVERIFY( !QFile::exists(targetFile) ); saveFile.abort(); QVERIFY( !QFile::exists(targetFile) ); } QFile file ( targetFile ); QVERIFY( file.open(QIODevice::WriteOnly | QIODevice::Text) ); QVERIFY( file.setPermissions( file.permissions() | QFile::ExeUser ) ); file.close(); { //Test how it works when the file already exists //Also check for special permissions KSaveFile saveFile ( targetFile ); QVERIFY( saveFile.open() ); QVERIFY( QFile::exists(targetFile) ); QFileInfo fi ( targetFile ); #ifndef Q_WS_WIN // Windows: qt_ntfs_permission_lookup is not set by default in // qfsfileengine_win.cpp, could change in future Qt versions. QVERIFY( fi.permission( QFile::ExeUser ) ); #endif QVERIFY( fi.size() == 0 ); QTextStream ts ( &saveFile ); ts << "This is test data three.\n"; ts.flush(); fi.refresh(); QVERIFY( fi.size() == 0 ); QVERIFY( saveFile.finalize() ); fi.refresh(); QVERIFY( fi.size() != 0 ); #ifndef Q_WS_WIN QVERIFY( fi.permission( QFile::ExeUser ) ); #endif QFile::remove(targetFile); } { QFileInfo fi ( targetFile ); targetFile = fi.fileName(); QDir::setCurrent(fi.path()); //one more time, this time with relative filenames KSaveFile saveFile ( targetFile ); QVERIFY( saveFile.open() ); QVERIFY( !QFile::exists(targetFile) ); QTextStream ts ( &saveFile ); ts << "This is test data four.\n"; ts.flush(); QCOMPARE( saveFile.error(), QFile::NoError ); QVERIFY( !QFile::exists(targetFile) ); QVERIFY( saveFile.finalize() ); QVERIFY( QFile::exists(targetFile) ); QFile::remove(targetFile); } }