29 #include <tdeapplication.h>
30 #include <kstandarddirs.h>
32 #include <tdemessagebox.h>
33 #include <tdeio/job.h>
35 #include <tdeio/netaccess.h>
36 #include <tdeprocess.h>
38 #include <tdelocale.h>
39 #include <tdecmdlineargs.h>
40 #include <tdeaboutdata.h>
41 #include <tdestartupinfo.h>
49 static const char description[] =
50 I18N_NOOP(
"TDEIO Exec - Opens remote files, watches modifications, asks for upload");
52 static TDECmdLineOptions options[] =
54 {
"tempfiles", I18N_NOOP(
"Treat URLs as local files and delete them afterwards"), 0 },
55 {
"suggestedfilename <file name>", I18N_NOOP(
"Suggested file name for the downloaded file"), 0 },
56 {
"+command", I18N_NOOP(
"Command to execute"), 0 },
57 {
"+[URLs]", I18N_NOOP(
"URL(s) or local file(s) used for 'command'"), 0 },
64 TQPtrList<TDEIO::Job>* jobList = 0L;
68 jobList =
new TQPtrList<TDEIO::Job>;
69 jobList->setAutoDelete(
false );
71 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
72 if (args->count() < 1)
73 TDECmdLineArgs::usage(i18n(
"'command' expected.\n"));
75 tempfiles = args->isSet(
"tempfiles");
76 if ( args->isSet(
"suggestedfilename" ) )
77 suggestedFileName = TQString::fromLocal8Bit( args->getOption(
"suggestedfilename" ) );
79 command = args->arg(0);
80 kdDebug() <<
"command=" << command << endl;
82 for (
int i = 1; i < args->count(); i++ )
84 KURL url = args->url(i);
86 url = TDEIO::NetAccess::mostLocalURL( url, 0 );
91 if ( url.isLocalFile() )
94 file.path = url.path();
96 fileList.append(file);
101 if ( !url.isValid() )
102 KMessageBox::error( 0L, i18n(
"The URL %1\nis malformed" ).arg( url.url() ) );
103 else if ( tempfiles )
104 KMessageBox::error( 0L, i18n(
"Remote URL %1\nnot allowed with --tempfiles switch" ).arg( url.url() ) );
108 TQString fileName = TDEIO::encodeFileName( url.fileName() );
109 if ( !suggestedFileName.isEmpty() )
110 fileName = suggestedFileName;
114 TQString tmp = TDEGlobal::dirs()->saveLocation(
"cache",
"krun/" ) +
115 TQString(
"%1.%2.%3").arg(getpid()).arg(jobCounter++).arg(fileName);
119 fileList.append(file);
124 kdDebug() <<
"Copying " << url.prettyURL() <<
" to " << dest << endl;
125 TDEIO::Job *job = TDEIO::file_copy( url, dest );
126 jobList->append( job );
128 connect( job, TQ_SIGNAL( result( TDEIO::Job * ) ), TQ_SLOT( slotResult( TDEIO::Job * ) ) );
136 TQTimer::singleShot( 0,
this, TQ_SLOT( slotRunApp() ) );
142 if ( counter == expectedCounter )
146 void KIOExec::slotResult( TDEIO::Job * job )
148 if (job && job->error())
152 if ( (job->error() != TDEIO::ERR_USER_CANCELED) )
153 KMessageBox::error( 0L, job->errorString() );
155 TQString path =
static_cast<TDEIO::FileCopyJob*
>(job)->destURL().path();
157 TQValueList<fileInfo>::Iterator it = fileList.begin();
158 for(;it != fileList.end(); ++it)
160 if ((*it).path == path)
164 if ( it != fileList.end() )
165 fileList.remove( it );
167 kdDebug() <<
static_cast<TDEIO::FileCopyJob*
>(job)->destURL().path() <<
" not found in list" << endl;
172 if ( counter < expectedCounter )
175 kdDebug() <<
"All files downloaded, will call slotRunApp shortly" << endl;
177 TQTimer::singleShot( 0,
this, TQ_SLOT( slotRunApp() ) );
182 void KIOExec::slotRunApp()
184 if ( fileList.isEmpty() ) {
185 kdDebug() << k_funcinfo <<
"No files downloaded -> exiting" << endl;
189 KService service(
"dummy", command, TQString::null);
193 TQValueList<fileInfo>::Iterator it = fileList.begin();
194 for ( ; it != fileList.end() ; ++it )
196 KDE_struct_stat buff;
197 (*it).time = KDE_stat( TQFile::encodeName((*it).path), &buff ) ? 0 : buff.st_mtime;
199 url.setPath((*it).path);
203 TQStringList params = KRun::processDesktopExec(service, list,
false );
205 kdDebug() <<
"EXEC " << KShell::joinArgs( params ) << endl;
210 id.initId( kapp->startupId());
211 id.setupStartupEnv();
216 proc.start( TDEProcess::Block );
219 TDEStartupInfo::resetStartupEnv();
222 kdDebug() <<
"EXEC done" << endl;
225 it = fileList.begin();
226 for( ;it != fileList.end(); ++it )
228 KDE_struct_stat buff;
229 TQString src = (*it).path;
230 KURL dest = (*it).url;
231 if ( (KDE_stat( TQFile::encodeName(src), &buff ) == 0) &&
232 ((*it).time != buff.st_mtime) )
236 if ( KMessageBox::questionYesNo( 0L,
237 i18n(
"The supposedly temporary file\n%1\nhas been modified.\nDo you still want to delete it?" ).arg(dest.prettyURL()),
238 i18n(
"File Changed" ), KStdGuiItem::del(), i18n(
"Do Not Delete") ) != KMessageBox::Yes )
241 else if ( ! dest.isLocalFile() )
243 if ( KMessageBox::questionYesNo( 0L,
244 i18n(
"The file\n%1\nhas been modified.\nDo you want to upload the changes?" ).arg(dest.prettyURL()),
245 i18n(
"File Changed" ), i18n(
"Upload"), i18n(
"Do Not Upload") ) == KMessageBox::Yes )
247 kdDebug() << TQString(TQString(
"src='%1' dest='%2'").arg(src).arg(dest.url())).ascii() << endl;
249 if ( !TDEIO::NetAccess::upload( src, dest, 0 ) )
251 KMessageBox::error( 0L, TDEIO::NetAccess::lastErrorString() );
258 if ( !dest.isLocalFile() || tempfiles ) {
261 kdDebug() <<
"sleeping..." << endl;
263 kdDebug() <<
"about to delete " << src << endl;
264 unlink( TQFile::encodeName(src) );
272 int main(
int argc,
char **argv )
274 TDEAboutData aboutData(
"tdeioexec", I18N_NOOP(
"KIOExec"),
275 VERSION, description, TDEAboutData::License_GPL,
276 "(c) 1998-2000,2003 The KFM/Konqueror Developers");
277 aboutData.addAuthor(
"David Faure",0,
"faure@kde.org");
278 aboutData.addAuthor(
"Stephan Kulow",0,
"coolo@kde.org");
279 aboutData.addAuthor(
"Bernhard Rosenkraenzer",0,
"bero@arklinux.org");
280 aboutData.addAuthor(
"Waldo Bastian",0,
"bastian@kde.org");
281 aboutData.addAuthor(
"Oswald Buddenhagen",0,
"ossi@kde.org");
283 TDECmdLineArgs::init( argc, argv, &aboutData );
284 TDECmdLineArgs::addCmdLineOptions( options );
290 kdDebug() <<
"Constructor returned..." << endl;