diff --git a/kioclient/CMakeLists.txt b/kioclient/CMakeLists.txt new file mode 100644 index 0000000..2f47d7a --- /dev/null +++ b/kioclient/CMakeLists.txt @@ -0,0 +1,9 @@ +set(kioclient_SRCS kioclient.cpp ) + +kde4_automoc(${kioclient_SRCS}) + +kde4_add_executable(kioclient ${kioclient_SRCS}) + +target_link_libraries(kioclient ${KDE4_KIO_LIBS}) + +install(TARGETS kioclient DESTINATION bin) diff --git a/kioclient/kioclient.cpp b/kioclient/kioclient.cpp new file mode 100644 index 0000000..db01323 --- /dev/null +++ b/kioclient/kioclient.cpp @@ -0,0 +1,277 @@ +/* This file is part of the KDE project + Copyright (C) 1999-2006 David Faure + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kioclient.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char appName[] = "kioclient"; +static const char programName[] = I18N_NOOP("KIO Client"); +static const char description[] = I18N_NOOP("Command-line tool for network-transparent operations"); +static const char version[] = "2.0"; + +bool ClientApp::m_ok = true; +bool s_interactive = true; + +static const KCmdLineOptions options[] = +{ + { "noninteractive", I18N_NOOP("Non interactive use: no message boxes"), 0}, + { "commands", I18N_NOOP("Show available commands"), 0}, + { "+command", I18N_NOOP("Command (see --commands)"), 0}, + { "+[URL(s)]", I18N_NOOP("Arguments for command"), 0}, + KCmdLineLastOption +}; + +int main( int argc, char **argv ) +{ + KCmdLineArgs::init(argc, argv, appName, programName, description, version, false); + + KCmdLineArgs::addCmdLineOptions( options ); + KCmdLineArgs::addTempFileOption(); + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + + if ( args->isSet("commands") ) + { + KCmdLineArgs::enable_i18n(); + puts(i18n("\nSyntax:\n").toLocal8Bit()); + puts(i18n(" kioclient openProperties 'url'\n" + " # Opens a properties menu\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec 'url' ['binding']\n" + " # Tries to execute 'url'. 'url' may be a usual\n" + " # URL, this URL will be opened. You may omit\n" + " # 'binding'. In this case the default binding\n").toLocal8Bit()); + puts(i18n(" # is tried. Of course URL may be the URL of a\n" + " # document, or it may be a *.desktop file.\n").toLocal8Bit()); + puts(i18n(" # This way you could for example mount a device\n" + " # by passing 'Mount default' as binding to \n" + " # 'cdrom.desktop'\n\n").toLocal8Bit()); + puts(i18n(" kioclient move 'src' 'dest'\n" + " # Moves the URL 'src' to 'dest'.\n" + " # 'src' may be a list of URLs.\n").toLocal8Bit()); + //puts(i18n(" # 'dest' may be \"trash:/\" to move the files\n" + // " # in the trash bin.\n\n").toLocal8Bit()); + puts(i18n(" kioclient download ['src']\n" + " # Copies the URL 'src' to a user specified location'.\n" + " # 'src' may be a list of URLs, if not present then\n" + " # a URL will be requested.\n\n").toLocal8Bit()); + puts(i18n(" kioclient copy 'src' 'dest'\n" + " # Copies the URL 'src' to 'dest'.\n" + " # 'src' may be a list of URLs.\n\n").toLocal8Bit()); + + puts(i18n("*** Examples:\n" + " kioclient exec file:/root/Desktop/cdrom.desktop \"Mount default\"\n" + " // Mounts the CD-ROM\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec file:/home/weis/data/test.html\n" + " // Opens the file with default binding\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec file:/home/weis/data/test.html Netscape\n" + " // Opens the file with netscape\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec ftp://localhost/\n" + " // Opens new window with URL\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec file:/root/Desktop/emacs.desktop\n" + " // Starts emacs\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec file:/root/Desktop/cdrom.desktop\n" + " // Opens the CD-ROM's mount directory\n\n").toLocal8Bit()); + puts(i18n(" kioclient exec .\n" + " // Opens the current directory. Very convenient.\n\n").toLocal8Bit()); + return 0; + } + + return ClientApp::doIt() ? 0 /*no error*/ : 1 /*error*/; +} + +void ClientApp::delayedQuit() +{ + // Quit in 2 seconds. This leaves time for KRun to pop up + // "app not found" in KProcessRunner, if that was the case. + QTimer::singleShot( 2000, this, SLOT(deref()) ); +} + +static void checkArgumentCount(int count, int min, int max) +{ + if (count < min) + { + fputs( i18n("Syntax Error: Not enough arguments\n").toLocal8Bit(), stderr ); + ::exit(1); + } + if (max && (count > max)) + { + fputs( i18n("Syntax Error: Too many arguments\n").toLocal8Bit(), stderr ); + ::exit(1); + } +} + +bool ClientApp::doIt() +{ + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + int argc = args->count(); + checkArgumentCount(argc, 1, 0); + + if ( !args->isSet( "ninteractive" ) ) { + s_interactive = false; + } + QByteArray command = args->arg(0); + + kDebug() << "Creating ClientApp" << endl; + int fake_argc = 0; + char** fake_argv = 0; + ClientApp app( fake_argc, fake_argv ); + KInstance instance( "kioclient" ); // needed by KIO's internal use of KConfig + app.setApplicationName(instance.instanceName()); + + // KIO needs dbus (for uiserver communication) + extern void qDBusBindToApplication(); + qDBusBindToApplication(); + if (!QDBus::sessionBus().isConnected()) + kFatal(101) << "Session bus not found" << endl; + + + if ( command == "openProperties" ) + { + checkArgumentCount(argc, 2, 2); + KPropertiesDialog * p = new KPropertiesDialog( args->url(1) ); + QObject::connect( p, SIGNAL( destroyed() ), &app, SLOT( quit() )); + QObject::connect( p, SIGNAL( canceled() ), &app, SLOT( slotDialogCanceled() )); + app.exec(); + return m_ok; + } + else if ( command == "exec" ) + { + checkArgumentCount(argc, 2, 3); + if ( argc == 2 ) + { + KRun * run = new KRun( args->url(1), 0L); + QObject::connect( run, SIGNAL( finished() ), &app, SLOT( delayedQuit() )); + QObject::connect( run, SIGNAL( error() ), &app, SLOT( delayedQuit() )); + app.exec(); + return !run->hasError(); + } + else if ( argc == 3 ) + { + KUrl::List urls; + urls.append( args->url(1) ); + const KService::List offers = KMimeTypeTrader::self()->query( QString::fromLocal8Bit( args->arg( 2 ) ), + QLatin1String( "Application" ) ); + if (offers.isEmpty()) return 1; + KService::Ptr serv = offers.first(); + return KRun::run( *serv, urls, 0 ); + } + } + else if ( command == "move" ) + { + checkArgumentCount(argc, 2, 0); + KUrl::List srcLst; + for ( int i = 1; i <= argc - 2; i++ ) + srcLst.append( args->url(i) ); + + KIO::Job * job = KIO::move( srcLst, args->url(argc - 1) ); + if ( !s_interactive ) + job->setUiDelegate( 0 ); + connect( job, SIGNAL( result( KJob * ) ), &app, SLOT( slotResult( KJob * ) ) ); + app.exec(); + return m_ok; + } + else if ( command == "download" ) + { + checkArgumentCount(argc, 0, 0); + KUrl::List srcLst; + if (argc == 1) { + while(true) { + KUrl src = KUrlRequesterDlg::getURL(); + if (!src.isEmpty()) { + if (!src.isValid()) { + KMessageBox::error(0, i18n("Unable to download from an invalid URL.")); + continue; + } + srcLst.append(src); + } + break; + } + } else { + for ( int i = 1; i <= argc - 1; i++ ) + srcLst.append( args->url(i) ); + } + if (srcLst.count() == 0) + return m_ok; + QString dst = + KFileDialog::getSaveFileName( (argc<2) ? (QString::null) : (args->url(1).fileName()) ); + if (dst.isEmpty()) // canceled + return m_ok; // AK - really okay? + KUrl dsturl; + dsturl.setPath( dst ); + KIO::Job * job = KIO::copy( srcLst, dsturl ); + if ( !s_interactive ) + job->setUiDelegate( 0 ); + connect( job, SIGNAL( result( KJob * ) ), &app, SLOT( slotResult( KJob * ) ) ); + app.exec(); + return m_ok; + } + else if ( command == "copy" ) + { + checkArgumentCount(argc, 2, 0); + KUrl::List srcLst; + for ( int i = 1; i <= argc - 2; i++ ) + srcLst.append( args->url(i) ); + + KIO::Job * job = KIO::copy( srcLst, args->url(argc - 1) ); + if ( !s_interactive ) + job->setUiDelegate( 0 ); + connect( job, SIGNAL( result( KJob * ) ), &app, SLOT( slotResult( KJob * ) ) ); + app.exec(); + return m_ok; + } + else + { + fprintf( stderr, "%s", i18n("Syntax Error: Unknown command '%1'\n", QString::fromLocal8Bit(command)).toLocal8Bit().data() ); + return false; + } + return true; +} + +void ClientApp::slotResult( KJob * job ) +{ + if (job->error() && s_interactive) + static_cast(job)->ui()->showErrorMessage(); + m_ok = !job->error(); + quit(); +} + +void ClientApp::slotDialogCanceled() +{ + m_ok = false; + quit(); +} + +#include "kioclient.moc" diff --git a/kioclient/kioclient.h b/kioclient/kioclient.h new file mode 100644 index 0000000..dacf4c5 --- /dev/null +++ b/kioclient/kioclient.h @@ -0,0 +1,44 @@ +/* This file is part of the KDE project + Copyright (C) 1999-2006 David Faure + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef __kioclient_h +#define __kioclient_h + +#include +class KJob; + +class ClientApp : public QApplication +{ + Q_OBJECT +public: + ClientApp(int &argc, char **argv ) : QApplication( argc, argv ) {} + + /** Parse command-line arguments and "do it" */ + static bool doIt(); + +protected Q_SLOTS: + void slotResult( KJob * ); + void delayedQuit(); + void slotDialogCanceled(); + +private: + static bool m_ok; + +}; + +#endif