• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeioexec
 

tdeio/tdeioexec

  • tdeio
  • tdeioexec
main.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3 Copyright (C) 2000 David Faure <faure@kde.org>
4 Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include <config.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <sys/stat.h>
26
27#include <tqfile.h>
28
29#include <tdeapplication.h>
30#include <tdestandarddirs.h>
31#include <kdebug.h>
32#include <tdemessagebox.h>
33#include <tdeio/job.h>
34#include <krun.h>
35#include <tdeio/netaccess.h>
36#include <tdeprocess.h>
37#include <kservice.h>
38#include <tdelocale.h>
39#include <tdecmdlineargs.h>
40#include <tdeaboutdata.h>
41#include <tdestartupinfo.h>
42#include <kshell.h>
43#include <kde_file.h>
44
45
46#include "main.h"
47
48
49static const char description[] =
50 I18N_NOOP("TDEIO Exec - Opens remote files, watches modifications, asks for upload");
51
52static TDECmdLineOptions options[] =
53{
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 },
58 TDECmdLineLastOption
59};
60
61
62int jobCounter = 0;
63
64TQPtrList<TDEIO::Job>* jobList = 0L;
65
66KIOExec::KIOExec()
67{
68 jobList = new TQPtrList<TDEIO::Job>;
69 jobList->setAutoDelete( false ); // jobs autodelete themselves
70
71 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
72 if (args->count() < 1)
73 TDECmdLineArgs::usage(i18n("'command' expected.\n"));
74
75 tempfiles = args->isSet("tempfiles");
76 if ( args->isSet( "suggestedfilename" ) )
77 suggestedFileName = TQString::fromLocal8Bit( args->getOption( "suggestedfilename" ) );
78 expectedCounter = 0;
79 command = args->arg(0);
80 kdDebug() << "command=" << command << endl;
81
82 for ( int i = 1; i < args->count(); i++ )
83 {
84 KURL url = args->url(i);
85 // we need to map system:/ etc to make sure we get this right
86 url = TDEIO::NetAccess::mostLocalURL( url, 0 );
87
88 //kdDebug() << "url=" << url.url() << " filename=" << url.fileName() << endl;
89 // A local file, not an URL ?
90 // => It is not encoded and not shell escaped, too.
91 if ( url.isLocalFile() )
92 {
93 fileInfo file;
94 file.path = url.path();
95 file.url = url;
96 fileList.append(file);
97 }
98 // It is an URL
99 else
100 {
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() ) );
105 else
106 // We must fetch the file
107 {
108 TQString fileName = TDEIO::encodeFileName( url.fileName() );
109 if ( !suggestedFileName.isEmpty() )
110 fileName = suggestedFileName;
111 // Build the destination filename, in ~/.trinity/cache-*/krun/
112 // Unlike KDE-1.1, we put the filename at the end so that the extension is kept
113 // (Some programs rely on it)
114 TQString tmp = TDEGlobal::dirs()->saveLocation( "cache", "krun/" ) +
115 TQString("%1.%2.%3").arg(getpid()).arg(jobCounter++).arg(fileName);
116 fileInfo file;
117 file.path = tmp;
118 file.url = url;
119 fileList.append(file);
120
121 expectedCounter++;
122 KURL dest;
123 dest.setPath( tmp );
124 kdDebug() << "Copying " << url.prettyURL() << " to " << dest << endl;
125 TDEIO::Job *job = TDEIO::file_copy( url, dest );
126 jobList->append( job );
127
128 connect( job, TQ_SIGNAL( result( TDEIO::Job * ) ), TQ_SLOT( slotResult( TDEIO::Job * ) ) );
129 }
130 }
131 }
132 args->clear();
133
134 if ( tempfiles ) {
135 // #113991
136 TQTimer::singleShot( 0, this, TQ_SLOT( slotRunApp() ) );
137 //slotRunApp(); // does not return
138 return;
139 }
140
141 counter = 0;
142 if ( counter == expectedCounter )
143 slotResult( 0L );
144}
145
146void KIOExec::slotResult( TDEIO::Job * job )
147{
148 if (job && job->error())
149 {
150 // That error dialog would be queued, i.e. not immediate...
151 //job->showErrorDialog();
152 if ( (job->error() != TDEIO::ERR_USER_CANCELED) )
153 KMessageBox::error( 0L, job->errorString() );
154
155 TQString path = static_cast<TDEIO::FileCopyJob*>(job)->destURL().path();
156
157 TQValueList<fileInfo>::Iterator it = fileList.begin();
158 for(;it != fileList.end(); ++it)
159 {
160 if ((*it).path == path)
161 break;
162 }
163
164 if ( it != fileList.end() )
165 fileList.remove( it );
166 else
167 kdDebug() << static_cast<TDEIO::FileCopyJob*>(job)->destURL().path() << " not found in list" << endl;
168 }
169
170 counter++;
171
172 if ( counter < expectedCounter )
173 return;
174
175 kdDebug() << "All files downloaded, will call slotRunApp shortly" << endl;
176 // We know we can run the app now - but let's finish the job properly first.
177 TQTimer::singleShot( 0, this, TQ_SLOT( slotRunApp() ) );
178
179 jobList->clear();
180}
181
182void KIOExec::slotRunApp()
183{
184 if ( fileList.isEmpty() ) {
185 kdDebug() << k_funcinfo << "No files downloaded -> exiting" << endl;
186 exit(1);
187 }
188
189 KService service("dummy", command, TQString::null);
190
191 KURL::List list;
192 // Store modification times
193 TQValueList<fileInfo>::Iterator it = fileList.begin();
194 for ( ; it != fileList.end() ; ++it )
195 {
196 KDE_struct_stat buff;
197 (*it).time = KDE_stat( TQFile::encodeName((*it).path), &buff ) ? 0 : buff.st_mtime;
198 KURL url;
199 url.setPath((*it).path);
200 list << url;
201 }
202
203 TQStringList params = KRun::processDesktopExec(service, list, false /*no shell*/);
204
205 kdDebug() << "EXEC " << KShell::joinArgs( params ) << endl;
206
207#ifdef TQ_WS_X11
208 // propagate the startup indentification to the started process
209 TDEStartupInfoId id;
210 id.initId( tdeApp->startupId());
211 id.setupStartupEnv();
212#endif
213
214 TDEProcess proc;
215 proc << params;
216 proc.start( TDEProcess::Block );
217
218#ifdef TQ_WS_X11
219 TDEStartupInfo::resetStartupEnv();
220#endif
221
222 kdDebug() << "EXEC done" << endl;
223
224 // Test whether one of the files changed
225 it = fileList.begin();
226 for( ;it != fileList.end(); ++it )
227 {
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) )
233 {
234 if ( tempfiles )
235 {
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 )
239 continue; // don't delete the temp file
240 }
241 else if ( ! dest.isLocalFile() ) // no upload when it's already a local file
242 {
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 )
246 {
247 kdDebug() << TQString(TQString("src='%1' dest='%2'").arg(src).arg(dest.url())).ascii() << endl;
248 // Do it the synchronous way.
249 if ( !TDEIO::NetAccess::upload( src, dest, 0 ) )
250 {
251 KMessageBox::error( 0L, TDEIO::NetAccess::lastErrorString() );
252 continue; // don't delete the temp file
253 }
254 }
255 }
256 }
257
258 if ( !dest.isLocalFile() || tempfiles ) {
259 // Wait for a reasonable time so that even if the application forks on startup (like OOo or amarok)
260 // it will have time to start up and read the file before it gets deleted. #130709.
261 kdDebug() << "sleeping..." << endl;
262 sleep(180); // 3 mn
263 kdDebug() << "about to delete " << src << endl;
264 unlink( TQFile::encodeName(src) );
265 }
266 }
267
268 //tdeApp->quit(); not efficient enough
269 exit(0);
270}
271
272int main( int argc, char **argv )
273{
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");
282
283 TDECmdLineArgs::init( argc, argv, &aboutData );
284 TDECmdLineArgs::addCmdLineOptions( options );
285
286 TDEApplication app;
287
288 KIOExec exec;
289
290 kdDebug() << "Constructor returned..." << endl;
291 return app.exec();
292}
293
294#include "main.moc"

tdeio/tdeioexec

Skip menu "tdeio/tdeioexec"
  • Main Page
  • File List

tdeio/tdeioexec

Skip menu "tdeio/tdeioexec"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeioexec by doxygen 1.9.4
This website is maintained by Timothy Pearson.