1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/***************************************************************************
plugin_kateinsertcommand.cpp - description
-------------------
begin : THU Apr 19 2001
copyright : (C) 2001 by Anders Lund
email : anders@alweb.dk
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "plugin_kateinsertcommand.h"
#include "plugin_kateinsertcommand.moc"
#include <qcheckbox.h>
#include <qbuttongroup.h>
#include <qdir.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qradiobutton.h>
#include <qspinbox.h>
#include <qstringlist.h>
#include <qwhatsthis.h>
#include <qwidget.h>
#include <kaction.h>
#include <kanimwidget.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kstddirs.h>
extern "C"
{
void* init_libkateinsertcommandplugin()
{
KGlobal::locale()->insertCatalogue("kateinsertcommand");
return new KatePluginFactory;
}
}
KatePluginFactory::KatePluginFactory()
{
s_instance = new KInstance( "kate" );
}
KatePluginFactory::~KatePluginFactory()
{
delete s_instance;
}
QObject* KatePluginFactory::createObject( QObject* parent, const char* name, const char*, const QStringList & )
{
return new PluginKateInsertCommand( parent, name );
}
KInstance* KatePluginFactory::s_instance = 0L;
PluginKateInsertCommand::PluginKateInsertCommand( QObject* parent, const char* name )
: Kate::Plugin ( parent, name ),
kv ( 0 ),
sh ( 0 )
{
config = new KConfig("kateinsertcommandpluginrc");
cmdhist = config->readListEntry("Command History");
wdlg = 0;
workingdir = QDir::currentDirPath();
}
PluginKateInsertCommand::~PluginKateInsertCommand()
{
// write config file
config->writeEntry("Command History", cmdhist);
config->writeEntry("Dialog Settings", dialogSettings);
config->sync();
}
Kate::PluginView *PluginKateInsertCommand::createView (Kate::MainWindow *win)
{
Kate::PluginView *view = new Kate::PluginView (this, win);
(void) new KAction ( i18n("Insert Command..."), "", 0, this,
SLOT( slotInsertCommand() ), view->actionCollection(),
"edit_insert_command" );
view->setXML( "plugins/kateinsertcommand/ui.rc" );
return view;
}
void PluginKateInsertCommand::slotInsertCommand()
{
if ( sh && sh->isRunning() ) {
KMessageBox::sorry (0, i18n("Sorry, a process is currently beeing executed:("), i18n("Oops!"));
return;
}
kv = myApp->getViewManager()->getActiveView();
QString dir = workingdir;
QString docdir;
KURL docurl = kv->getDoc()->url();
if (docurl.isLocalFile())
docdir = docurl.directory();
QString lwd( config->readEntry("Last WD") );
switch ( (int)config->readNumEntry("Start In", 0) )
{
case 1:
if ( ! docdir.isEmpty() ) dir = docdir;
break;
case 2:
if ( ! lwd.isEmpty() ) dir = lwd;
break;
default:
break;
}
dialogSettings = config->readNumEntry("Dialog Settings", 0);
CmdPrompt *d = new CmdPrompt((QWidget*)kv, 0, cmdhist, dir, docdir, dialogSettings);
if ( d->exec() && ! d->command().isEmpty() ) {
if ( ! sh ) {
sh = new KShellProcess;
connect ( sh, SIGNAL(receivedStdout(KProcess*, char*, int)),
this, SLOT(slotReceivedStdout(KProcess*, char*, int)) );
connect ( sh, SIGNAL(receivedStderr(KProcess*, char*, int)),
this, SLOT(slotReceivedStderr(KProcess*, char*, int)) );
connect ( sh, SIGNAL(processExited(KProcess*)),
this, SLOT(slotProcessExited(KProcess*)) ) ;
}
sh->clearArguments();
bInsStdErr = d->insertStdErr();
if ( d->printCmd() ) {
if ( ! d->wd().isEmpty() )
kv->insertText( d->wd() + ": ");
kv->insertText( d->command()+":\n" );
}
if ( ! d->wd().isEmpty() ) {
*sh << "cd" << d->wd() << "&&";
config->writeEntry("Last WD", d->wd());
}
*sh << d->command().latin1();
sh->start( KProcess::NotifyOnExit, KProcess::All );
// add command to history
if ( cmdhist.contains( d->command() ) ) {
cmdhist.remove( d->command() );
}
cmdhist.prepend( d->command() );
int cmdhistlen = config->readNumEntry("Command History Length", 20);
while ( cmdhist.count() > cmdhistlen )
cmdhist.remove( cmdhist.last() );
// save dialog state
dialogSettings = 0;
if ( d->insertStdErr() )
dialogSettings += 1;
if ( d->printCmd() )
dialogSettings += 2;
cmd = d->command();
delete d;
// If process is still running, display a dialog to cancel...
slotShowWaitDlg();
config->writeEntry("Dialog Settings", dialogSettings);
config->sync();
}
}
void PluginKateInsertCommand::slotAbort()
{
if ( sh->isRunning() )
if (! sh->kill() )
KMessageBox::sorry(0, i18n("Could not kill command :o(("), i18n("Oops!"));
}
void PluginKateInsertCommand::slotShowWaitDlg()
{
if ( sh->isRunning() ) {
wdlg = new WaitDlg( (QWidget*)kv, QString( i18n("Executing command:") + "\n" + cmd + "\n\n" + i18n("Press Cancel to abort") ) );
connect(wdlg, SIGNAL(cancelClicked()), this, SLOT(slotAbort()) );
}
if ( sh->isRunning() ) // we may have finished while creating the dialog.
wdlg->show();
else if (wdlg) { // process may have exited before the WaitDlg constructor returned.
delete wdlg;
wdlg = 0;
}
}
void PluginKateInsertCommand::slotReceivedStdout( KProcess* p, char* text, int len )
{
QString t = QString(text);
t.truncate(len);
kv->insertText( t );
}
void PluginKateInsertCommand::slotReceivedStderr( KProcess* p, char* text, int len )
{
if ( bInsStdErr )
slotReceivedStdout( p, text, len );
}
void PluginKateInsertCommand::slotProcessExited( KProcess* p )
{
if (wdlg) {
wdlg->hide();
delete wdlg;
wdlg = 0;
}
if ( ! p->normalExit() )
KMessageBox::sorry(0, QString(i18n("Command exited with status %1")).arg( p->exitStatus()), i18n("Oops!"));
kv->setFocus();
}
Kate::PluginConfigPage* PluginKateInsertCommand::createConfigPage (QWidget *w)
{
InsertCommandConfigPage* p = new InsertCommandConfigPage(this, w);
initConfigPage( p );
connect( p, SIGNAL(configPageApplyRequest(InsertCommandConfigPage*)), this, SLOT(applyConfig(InsertCommandConfigPage*)) );
return (Kate::PluginConfigPage*)p;
}
void PluginKateInsertCommand::initConfigPage( InsertCommandConfigPage *p )
{
p->sb_cmdhistlen->setValue( config->readNumEntry("Command History Length", 20) );
p->rg_startin->setButton( config->readNumEntry("Start In", 0) );
}
void PluginKateInsertCommand::applyConfig( InsertCommandConfigPage *p )
{
config->writeEntry( "Command History Length", p->sb_cmdhistlen->value() );
// truncate the cmd hist if nessecary?
config->writeEntry( "Start In", p->rg_startin->id(p->rg_startin->selected()) );
config->sync();
}
/////////////////////////////////////////////////////////////////////
// CmdPrompt implementation
// This is a simple dialog to retrieve a command and decide if
// stdErr should be included in the text inserted.
CmdPrompt::CmdPrompt(QWidget* parent,
const char* name,
const QStringList& cmdhist,
const QString& dir,
const QString& docdir,
int settings)
: KDialogBase (parent, name, true, i18n("Insert Command"), Ok|Cancel, Ok, true)
{
QWidget *page = new QWidget( this );
setMainWidget(page);
QVBoxLayout *lo = new QVBoxLayout( page, 0, spacingHint() );
QLabel *l = new QLabel( i18n("Enter &Command"), page );
lo->addWidget( l );
cmb_cmd = new KHistoryCombo(true, page);
cmb_cmd->setHistoryItems(cmdhist, true);
cmb_cmd->setCurrentItem(0);
cmb_cmd->lineEdit()->setSelection(0, cmb_cmd->currentText().length());
l->setBuddy(cmb_cmd);
cmb_cmd->setFocus();
lo->addWidget(cmb_cmd);
QLabel *lwd = new QLabel( i18n("Choose &Working Directory"), page );
lo->addWidget( lwd );
wdreq = new KURLRequester( page );
if ( ! dir.isEmpty() )
wdreq->setURL( dir );
wdreq->fileDialog()->setMode( static_cast<KFile::Mode>(KFile::Directory|KFile::LocalOnly|KFile::ExistingOnly) );
lwd->setBuddy( wdreq );
lo->addWidget( wdreq );
kdDebug()<<"settings: "<<settings<<endl;
cb_insStdErr = new QCheckBox( i18n("Insert Std&Err messages"), page );
cb_insStdErr->setChecked(settings & 1);
lo->addWidget( cb_insStdErr );
cb_printCmd = new QCheckBox( i18n("&Print Command Name"), page );
cb_printCmd->setChecked(settings & 2);
lo->addWidget( cb_printCmd );
QWhatsThis::add( cmb_cmd, i18n("Enter the shell command which' output you want inserted into your document. Feel free to use a pipe or two if you're in that mood;-)") );
QWhatsThis::add( wdreq, i18n("Sets the working directory of command. The command executed is 'cd <dir> && <command>'") );
QWhatsThis::add( cb_insStdErr, i18n("Check this if you want error output from <command> inserted too.\nSome commands like locate prints everything to STDERR") );
QWhatsThis::add( cb_printCmd, i18n("If you check this I will print the command string followed by a newline before the output.") );
}
CmdPrompt::~CmdPrompt() {}
/////////////////////////////////////////////////////////////////////
// WaitDlg implementation
// This is a dialog that is displayed while a command is running,
// with a cancel button to allow the user to kill the command
WaitDlg::WaitDlg(QWidget* parent, const QString& text, const QString& title)
: KDialogBase( parent, "wait dialog", true, title, Cancel, Cancel, true )
{
QWidget *page = new QWidget( this );
setMainWidget( page );
QHBoxLayout *lo = new QHBoxLayout( page, 0, spacingHint() );
KAnimWidget *aw = new KAnimWidget( KGlobal::iconLoader()->loadAnimated("kde", 0, 48), 48, page );
lo->addWidget(aw);
QLabel *l = new QLabel( text, page );
lo->addWidget( l );
aw->start();
}
WaitDlg::~WaitDlg()
{
}
/////////////////////////////////////////////////////////////////////
// InsertCommandConfigPage implementation
// This is the config page for this plugin.
InsertCommandConfigPage::InsertCommandConfigPage(QObject* parent, QWidget *parentWidget)
: Kate::PluginConfigPage( parent, parentWidget )
{
QVBoxLayout* lo = new QVBoxLayout( this );
lo->setSpacing(KDialogBase::spacingHint());
// command history length
QHBox *hb1 = new QHBox( this );
hb1->setSpacing(KDialogBase::spacingHint());
(void) new QLabel( i18n("Remember"), hb1 );
sb_cmdhistlen = new QSpinBox( hb1 );
QLabel *l1 = new QLabel( sb_cmdhistlen, i18n("&Commands"), hb1);
hb1->setStretchFactor(l1, 1);
lo->addWidget( hb1 );
// dir history length
// initial dir choice
rg_startin = new QButtonGroup( 1, Qt::Horizontal, i18n("Start In"), this );
rg_startin->setRadioButtonExclusive( true );
(void) new QRadioButton( i18n("Application &Working Directory"), rg_startin);
(void) new QRadioButton( i18n("&Document Directory"), rg_startin);
(void) new QRadioButton( i18n("&Latest Used Working Directory"), rg_startin);
lo->addWidget( rg_startin );
// other?
lo->addStretch(1); // look nice
// Be helpfull!
QWhatsThis::add( sb_cmdhistlen, i18n("Sets the number of commands to remember. The command history is saved over sessions.") );
QWhatsThis::add( rg_startin, i18n("<qt><p>Decides what is suggested as <em>working directory</em> for the command.</p><p><strong>Application Working Directory (default):</strong> The directory from which you launched the application hosting the plugin, usually your home directory.</p><p><strong>Document Directory:</strong> The directory of the document. Used only for local documents.<p><strong>Latest Working Directory:</strong> The directory used last time you used this plugin.</p></qt>") );
}
void InsertCommandConfigPage::applyConfig()
{
emit configPageApplyRequest( this );
}
|