bool ReadOnlyPart::openURL( const KURL &url ) { if ( url.isMalformed() || !closeURL() ) return false; m_url = url; emit setWindowCaption( m_url.prettyURL() ); if ( m_url.isLocalFile() && m_url.query().isEmpty() ) { emit started( 0 ); m_file = m_url.path(); bool ret = openFile(); if ( ret ) emit completed(); return ret; } m_job = KIO::get( m_url, false, false ); m_file = m_url.fileName(); if ( m_url.isLocalFile() && m_url.query().startsWith( "??" ) ) { m_file = m_url.query().mid( 2 ); int sl = m_file.findRev( '/' ); if ( sl >= 0 ) m_file.remove( 0, sl + 1 ); } int extensionPos = m_file.findRev( '.' ); if ( extensionPos > 0 ) m_file.remove( 0, extensionPos ); else m_file = ""; // HACK: the m_job is assumed to be unique // we also assume sizeof(m_job) == sizeof(ulong) m_file.prepend( QString::number( (ulong)m_job, 36 ) ); m_file.prepend( QString::fromLatin1( "/tmp/.konqe-part-%1" ) ); QCString fn; int fd = -1; for ( int i = 0; ( fd == -1 ) && ( i < TMP_MAX ); i++ ) { fn = QFile::encodeName( m_file.arg( (ulong)::random(), 0, 36 ) ); fd = ::open( fn.data(), O_RDWR | O_CREAT | O_EXCL, 0600 ); } if ( fd == -1 ) { abortLoad(); emit canceled( i18n( "File I/O Error" ) ); return false; } m_tempFile.setName( QFile::decodeName( fn ) ); m_tempFile.open( IO_ReadWrite, fd ); m_file = ""; // Don't allow accesses while downloading connect( m_job, SIGNAL( destroyed() ), SLOT( slotJobDestroyed () ) ); connect( m_job, SIGNAL( result( KIO::Job * ) ), SLOT( slotJobFinished ( KIO::Job * ) ) ); connect( m_job, SIGNAL( data( KIO::Job*, const QByteArray &) ), SLOT( slotData( KIO::Job*, const QByteArray & ) ) ); emit started( m_job ); return true; }
bool KMiniEdit::saveFile(KURL newurl) { if (newurl.isEmpty()) newurl = KFileDialog::getSaveURL(); if (newurl.isEmpty()) return false; if (newurl.isMalformed()) { QString text = i18n("<b>THe URL %1 is not correct!</b>"); KMessageBox::sorry(this, text.arg(newurl.prettyURL())); return false; } if (newurl.isLocalFile()) { QFile file(newurl.path()); file.open(IO_WriteOnly); saveToLocalFile(&file); } else { KTempFile tempfile; saveToLocalFile(tempfile.file()); if (!KIO::NetAccess::upload(tempfile.name(), newurl)) { QString text = i18n("<b>Error uploading %1!</b>"); KMessageBox::sorry(this, text.arg(newurl.prettyURL())); tempfile.unlink(); return false; } tempfile.unlink(); } url = newurl; addURLtoRecent(); resetEdited(); return true; }
bool ReadWritePart::saveAs( const KURL & kurl ) { if ( kurl.isMalformed() || !kurl.isLocalFile() || !kurl.query().isEmpty() ) { KMessageBox::sorry( widget(), i18n( "Can save only to local files." ) ); return false; } if ( QFile::exists( kurl.path() ) && KMessageBox::warningYesNo( widget(), i18n( "Overwrite existing file ?" ) ) != KMessageBox::Yes ) return false; ReadOnlyPart::closeURL(); m_url = kurl; m_file = m_url.path(); emit setWindowCaption( m_url.prettyURL() ); return save(); }
void KDir::setPath(const char *url) { QString ts = url; if (ts.right(1) != "/") ts += "/"; KURL tmp = ts.data(); isBlocking = true; if (tmp.isLocalFile()) { // we can check, if the file is there struct stat buf; QString ts = tmp.path(); int ret = stat(ts.data(), &buf); readable = (ret == 0); if (readable) { // further checks DIR *test; test = opendir(ts); // we do it just to test here readable = (test != 0); if (test) closedir(test); } } else { readable = true; // what else can we say? isBlocking = false; } if (!tmp.isMalformed()) myLocation= tmp.url(); else warning("Malformed url %s\n", url); root = (strcmp(myLocation.path(),"/") == 0); if (!readable) return; // nothing more we can do here if (myOpendir) { closedir(myOpendir); myOpendir = 0; } myDirtyFlag= true; myFilteredDirtyFlag= true; }
bool KMiniEdit::loadFile(KURL newurl) { if (newurl.isMalformed()) { QString text = i18n("<b>The URL %1 is not correct!</b>"); KMessageBox::sorry(this, text.arg(newurl.prettyURL())); return false; } QString filename; if (newurl.isLocalFile()) filename = newurl.path(); else { if (!KIO::NetAccess::download(newurl, filename)) { QString text = i18n("<b>Error downloading %1!</b>"); KMessageBox::sorry(this, text.arg(newurl.prettyURL())); return false; } } QFile file(filename); file.open(IO_ReadOnly); QTextStream stream(&file); //stream.setEncoding(QTextStream::Unicode); edit->setText(stream.read()); file.close(); KIO::NetAccess::removeTempFile(filename); url = newurl; addURLtoRecent(); resetEdited(); return true; }