Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / shellcmdplugin / kshellcmdplugin.cpp
blobdb5c170d3b5e92681c6239e8106b1e2959024339
1 /* This file is part of the KDE project
2 Copyright (C) 2000 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "kshellcmdplugin.h"
20 #include "kshellcmddialog.h"
21 #include <kparts/part.h>
22 #include <kicon.h>
23 #include <kactioncollection.h>
24 #include <kinputdialog.h>
25 #include <kmessagebox.h>
26 #include <kshell.h>
27 #include <kapplication.h>
28 #include <kgenericfactory.h>
29 #include <kauthorized.h>
30 #include <kio/netaccess.h>
32 KShellCmdPlugin::KShellCmdPlugin( QObject* parent, const QStringList & )
33 : KParts::Plugin( parent )
35 if (!KAuthorized::authorizeKAction("shell_access"))
36 return;
38 KAction *action = actionCollection()->addAction("executeshellcommand");
39 action->setIcon(KIcon("system-run"));
40 action->setText(i18n( "&Execute Shell Command..." ));
41 connect(action, SIGNAL(triggered(bool)), SLOT( slotExecuteShellCommand() ));
42 action->setShortcut(Qt::CTRL+Qt::Key_E);
45 void KShellCmdPlugin::slotExecuteShellCommand()
47 KParts::ReadOnlyPart * part = dynamic_cast<KParts::ReadOnlyPart *>(parent());
48 if ( !part )
50 KMessageBox::sorry(0L, i18n("KShellCmdPlugin::slotExecuteShellCommand: Program error, please report a bug."));
51 return;
53 KUrl url = KIO::NetAccess::mostLocalUrl(part->url(),NULL);
54 if ( !url.isLocalFile() )
56 KMessageBox::sorry(part->widget(),i18n("Executing shell commands works only on local directories."));
57 return;
59 QString path;
60 #if 0 // to be ported if still needed
61 if ( part->currentItem() )
63 // Putting the complete path to the selected file isn't really necessary,
64 // since we'll cd to the directory first. But we do need to get the
65 // complete relative path.
66 path = KUrl::relativePath( url.path(),
67 part->currentItem()->url().path() );
69 else
70 #endif
72 path = url.path();
74 bool ok;
75 QString cmd = KInputDialog::getText( i18n("Execute Shell Command"),
76 i18n( "Execute shell command in current directory:" ),
77 KShell::quoteArg( path ), &ok, part->widget() );
78 if ( ok )
80 QString chDir;
81 chDir="cd ";
82 chDir+=KShell::quoteArg(part->url().path());
83 chDir+="; ";
84 chDir+=cmd;
86 KShellCommandDialog *shellCmdDialog=new KShellCommandDialog(i18n("Output from command: \"%1\"", cmd),chDir,part->widget(),true);
87 shellCmdDialog->resize(500,300);
88 shellCmdDialog->executeCommand();
89 delete shellCmdDialog;
93 typedef KGenericFactory<KShellCmdPlugin> KonqShellCmdPluginFactory;
94 K_EXPORT_COMPONENT_FACTORY( konq_shellcmdplugin, KonqShellCmdPluginFactory( "kshellcmdplugin" ) )
96 #include "kshellcmdplugin.moc"