• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • app
kateexternaltools.cpp
1/*
2 This file is part of the Kate text editor of the KDE project.
3
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.
7
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.
12
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.
17
18 ---
19 Copyright (C) 2004, Anders Lund <anders@alweb.dk>
20*/
21// TODO
22// Icons
23// Direct shortcut setting
24//BEGIN Includes
25#include "kateexternaltools.h"
26#include "kateexternaltools.moc"
27#include "katedocmanager.h"
28#include "kateviewmanager.h"
29#include "kateapp.h"
30
31#include "katemainwindow.h"
32
33#include <kate/view.h>
34#include <kate/document.h>
35
36#include <tdelistbox.h>
37#include <tdelocale.h>
38#include <kiconloader.h>
39#include <tdemessagebox.h>
40#include <kmimetypechooser.h>
41#include <tdeconfig.h>
42#include <krun.h>
43#include <kicondialog.h>
44#include <tdepopupmenu.h>
45#include <kdebug.h>
46
47#include <tqbitmap.h>
48#include <tqcombobox.h>
49#include <tqfile.h>
50#include <tqpushbutton.h>
51#include <tqlineedit.h>
52#include <tqlayout.h>
53#include <tqlabel.h>
54#include <tqlistbox.h>
55#include <tqmap.h>
56#include <tqregexp.h>
57#include <tqtextedit.h>
58#include <tqtoolbutton.h>
59#include <tqwhatsthis.h>
60
61#include <stdlib.h>
62#include <unistd.h>
63//END Includes
64
65KateExternalToolsCommand *KateExternalToolsCommand::s_self=0;
66
67//BEGIN KateExternalTool
68KateExternalTool::KateExternalTool( const TQString &name,
69 const TQString &command,
70 const TQString &icon,
71 const TQString &tryexec,
72 const TQStringList &mimetypes,
73 const TQString &acname,
74 const TQString &cmdname,
75 int save )
76 : name ( name ),
77 command ( command ),
78 icon ( icon ),
79 tryexec ( tryexec ),
80 mimetypes ( mimetypes ),
81 acname ( acname ),
82 cmdname ( cmdname ),
83 save ( save )
84{
85 //if ( ! tryexec.isEmpty() )
86 hasexec = checkExec();
87}
88
89bool KateExternalTool::checkExec()
90{
91 // if tryexec is empty, it is the first word of command
92 if ( tryexec.isEmpty() )
93 tryexec = command.section( " ", 0, 0, TQString::SectionSkipEmpty );
94
95 // NOTE this code is modified taken from kdesktopfile.cpp, from KDesktopFile::tryExec()
96 if (!tryexec.isEmpty()) {
97 if (tryexec[0] == '/') {
98 if (::access(TQFile::encodeName(tryexec), R_OK | X_OK))
99 return false;
100
101 m_exec = tryexec;
102 } else {
103 // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
104 // Environment PATH may contain filenames in 8bit locale cpecified
105 // encoding (Like a filenames).
106 TQStringList dirs = TQStringList::split(':', TQFile::decodeName(::getenv("PATH")));
107 TQStringList::Iterator it(dirs.begin());
108 bool match = false;
109 for (; it != dirs.end(); ++it)
110 {
111 TQString fName = *it + "/" + tryexec;
112 if (::access(TQFile::encodeName(fName), R_OK | X_OK) == 0)
113 {
114 match = true;
115 m_exec = fName;
116 break;
117 }
118 }
119 // didn't match at all
120 if (!match)
121 return false;
122 }
123 return true;
124 }
125 return false;
126}
127
128bool KateExternalTool::valid( const TQString &mt ) const
129{
130 return mimetypes.isEmpty() || mimetypes.contains( mt );
131}
132//END KateExternalTool
133
134//BEGIN KateExternalToolsCommand
135KateExternalToolsCommand::KateExternalToolsCommand() : Kate::Command() {
136 m_inited=false;
137 reload();
138}
139
140TQStringList KateExternalToolsCommand::cmds () {
141 return m_list;
142}
143
144KateExternalToolsCommand *KateExternalToolsCommand::self () {
145 if (s_self) return s_self;
146 s_self=new KateExternalToolsCommand;
147 return s_self;
148}
149
150void KateExternalToolsCommand::reload () {
151 m_list.clear();
152 m_map.clear();
153
154 TDEConfig config("externaltools", false, false, "appdata");
155 config.setGroup("Global");
156 TQStringList tools = config.readListEntry("tools");
157
158
159 for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
160 {
161 if ( *it == "---" )
162 continue;
163
164
165 config.setGroup( *it );
166
167 KateExternalTool t = KateExternalTool(
168 config.readEntry( "name", "" ),
169 config.readEntry( "command", ""),
170 config.readEntry( "icon", ""),
171 config.readEntry( "executable", ""),
172 config.readListEntry( "mimetypes" ),
173 config.readEntry( "acname", "" ),
174 config.readEntry( "cmdname", "" ) );
175 if ( t.hasexec && (!t.cmdname.isEmpty())) {
176 m_list.append("exttool-"+t.cmdname);
177 m_map.insert("exttool-"+t.cmdname,t.acname);
178 }
179 }
180 if (m_inited) {
181 Kate::Document::unregisterCommand(this);
182 Kate::Document::registerCommand(this);
183 }
184 else m_inited=true;
185}
186
187bool KateExternalToolsCommand::exec (Kate::View *view, const TQString &cmd, TQString &) {
188 TQWidget *wv=dynamic_cast<TQWidget*>(view);
189 if (!wv) {
190// kdDebug(13001)<<"KateExternalToolsCommand::exec: Could not get view widget"<<endl;
191 return false;
192 }
193 KateMDI::MainWindow *dmw=dynamic_cast<KateMDI::MainWindow*>(wv->topLevelWidget());
194 if (!dmw) {
195// kdDebug(13001)<<"KateExternalToolsCommand::exec: Could not get main window"<<endl;
196 return false;
197 }
198// kdDebug(13001)<<"cmd="<<cmd.stripWhiteSpace()<<endl;
199 TQString actionName=m_map[cmd.stripWhiteSpace()];
200 if (actionName.isEmpty()) return false;
201// kdDebug(13001)<<"actionName is not empty:"<<actionName<<endl;
202 KateExternalToolsMenuAction *a=
203 dynamic_cast<KateExternalToolsMenuAction*>(dmw->action("tools_external"));
204 if (!a) return false;
205// kdDebug(13001)<<"trying to find action"<<endl;
206 TDEAction *a1=a->actionCollection()->action(static_cast<const char *>(actionName.utf8()));
207 if (!a1) return false;
208// kdDebug(13001)<<"activating action"<<endl;
209 a1->activate();
210 return true;
211}
212
213bool KateExternalToolsCommand::help (Kate::View *, const TQString &, TQString &) {
214 return false;
215}
216//END KateExternalToolsCommand
217
218//BEGIN KateExternalToolAction
219KateExternalToolAction::KateExternalToolAction( TQObject *parent,
220 const char *name, KateExternalTool *t)
221 : TDEAction( parent, name ),
222 tool ( t )
223{
224 setText( t->name );
225 if ( ! t->icon.isEmpty() )
226 setIconSet( SmallIconSet( t->icon ) );
227
228 connect( this ,TQ_SIGNAL(activated()), this, TQ_SLOT(slotRun()) );
229}
230
231bool KateExternalToolAction::expandMacro( const TQString &str, TQStringList &ret )
232{
233 KateMainWindow *mw = (KateMainWindow*)parent()->parent();
234
235 Kate::View *view = mw->viewManager()->activeView();
236 if ( ! view ) return false;
237
238
239 if ( str == "URL" )
240 ret += mw->activeDocumentUrl().url();
241 else if ( str == "directory" ) // directory of current doc
242 ret += mw->activeDocumentUrl().directory();
243 else if ( str == "filename" )
244 ret += mw->activeDocumentUrl().fileName();
245 else if ( str == "line" ) // cursor line of current doc
246 ret += TQString::number( view->cursorLine() );
247 else if ( str == "col" ) // cursor col of current doc
248 ret += TQString::number( view->cursorColumn() );
249 else if ( str == "selection" ) // selection of current doc if any
250 ret += view->getDoc()->selection();
251 else if ( str == "text" ) // text of current doc
252 ret += view->getDoc()->text();
253 else if ( str == "URLs" ) {
254 for( Kate::Document *doc = KateDocManager::self()->firstDocument(); doc; doc = KateDocManager::self()->nextDocument() )
255 if ( ! doc->url().isEmpty() )
256 ret += doc->url().url();
257 } else
258 return false;
259 return true;
260}
261
262KateExternalToolAction::~KateExternalToolAction() {
263 delete(tool);
264}
265
266void KateExternalToolAction::slotRun()
267{
268 // expand the macros in command if any,
269 // and construct a command with an absolute path
270 TQString cmd = tool->command;
271
272 if ( ! expandMacrosShellQuote( cmd ) )
273 {
274 KMessageBox::sorry( (KateMainWindow*)parent()->parent(),
275 i18n("Failed to expand the command '%1'.").arg( cmd ),
276 i18n( "Kate External Tools") );
277 return;
278 }
279 kdDebug(13001)<<"externaltools: Running command: "<<cmd<<endl;
280
281 // save documents if requested
282 KateMainWindow *mw = (KateMainWindow*)parent()->parent();
283 if ( tool->save == 1 )
284 mw->viewManager()->activeView()->document()->save();
285 else if ( tool->save == 2 )
286 mw->actionCollection()->action("file_save_all")->activate();
287
288 KRun::runCommand( cmd, tool->tryexec, tool->icon );
289}
290//END KateExternalToolAction
291
292//BEGIN KateExternalToolsMenuAction
293KateExternalToolsMenuAction::KateExternalToolsMenuAction( const TQString &text,
294 TQObject *parent,
295 const char* name,
296 KateMainWindow *mw )
297 : TDEActionMenu( text, parent, name ),
298 mainwindow( mw )
299{
300
301 m_actionCollection = new TDEActionCollection( mainwindow );
302
303 connect(KateDocManager::self(),TQ_SIGNAL(documentChanged()),this,TQ_SLOT(slotDocumentChanged()));
304
305 reload();
306}
307
308void KateExternalToolsMenuAction::reload()
309{
310 m_actionCollection->clear ();
311 popupMenu()->clear();
312
313 // load all the tools, and create a action for each of them
314 TDEConfig *config = new TDEConfig( "externaltools", false, false, "appdata" );
315 config->setGroup( "Global" );
316 TQStringList tools = config->readListEntry( "tools" );
317
318 // if there are tools that are present but not explicitly removed,
319 // add them to the end of the list
320 config->setReadDefaults( true );
321 TQStringList dtools = config->readListEntry( "tools" );
322 int gver = config->readNumEntry( "version", 1 );
323 config->setReadDefaults( false );
324
325 int ver = config->readNumEntry( "version" );
326 if ( ver <= gver )
327 {
328 TQStringList removed = config->readListEntry( "removed" );
329 bool sepadded = false;
330 for (TQStringList::iterator itg = dtools.begin(); itg != dtools.end(); ++itg )
331 {
332 if ( ! tools.contains( *itg ) &&
333 ! removed.contains( *itg ) )
334 {
335 if ( ! sepadded )
336 {
337 tools << "---";
338 sepadded = true;
339 }
340 tools << *itg;
341 }
342 }
343
344 config->writeEntry( "tools", tools );
345 config->sync();
346 config->writeEntry( "version", gver );
347 }
348
349 for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
350 {
351 if ( *it == "---" )
352 {
353 popupMenu()->insertSeparator();
354 // a separator
355 continue;
356 }
357
358 config->setGroup( *it );
359
360 KateExternalTool *t = new KateExternalTool(
361 config->readEntry( "name", "" ),
362 config->readEntry( "command", ""),
363 config->readEntry( "icon", ""),
364 config->readEntry( "executable", ""),
365 config->readListEntry( "mimetypes" ),
366 config->readEntry( "acname", "" ),
367 config->readEntry( "cmdname", "" ),
368 config->readNumEntry( "save", 0 ) );
369
370 if ( t->hasexec )
371 insert( new KateExternalToolAction( m_actionCollection, t->acname.ascii(), t ) );
372 }
373
374 m_actionCollection->readShortcutSettings( "Shortcuts", config );
375 slotDocumentChanged();
376 delete config;
377}
378
379void KateExternalToolsMenuAction::slotDocumentChanged()
380{
381 // try to enable/disable to match current mime type
382 Kate::DocumentExt *de = documentExt( KateDocManager::self()->activeDocument() );
383 if ( de )
384 {
385 TQString mt = de->mimeType();
386 TQStringList l;
387 bool b;
388
389 TDEActionPtrList actions = m_actionCollection->actions();
390 for (TDEActionPtrList::iterator it = actions.begin(); it != actions.end(); ++it )
391 {
392 KateExternalToolAction *action = dynamic_cast<KateExternalToolAction*>(*it);
393 if ( action )
394 {
395 l = action->tool->mimetypes;
396 b = ( ! l.count() || l.contains( mt ) );
397 action->setEnabled( b );
398 }
399 }
400 }
401}
402//END KateExternalToolsMenuAction
403
404//BEGIN ToolItem
409class ToolItem : public TQListBoxPixmap
410{
411 public:
412 ToolItem( TQListBox *lb, const TQPixmap &icon, KateExternalTool *tool )
413 : TQListBoxPixmap( lb, icon, tool->name ),
414 tool ( tool )
415 {;}
416
417 ~ToolItem() {};
418
419 KateExternalTool *tool;
420};
421//END ToolItem
422
423//BEGIN KateExternalToolServiceEditor
424KateExternalToolServiceEditor::KateExternalToolServiceEditor( KateExternalTool *tool,
425 TQWidget *parent, const char *name )
426 : KDialogBase( parent, name, true, i18n("Edit External Tool"), KDialogBase::Ok|KDialogBase::Cancel ),
427 tool( tool )
428{
429 // create a entry for each property
430 // fill in the values from the service if available
431 TQWidget *w = new TQWidget( this );
432 setMainWidget( w );
433 TQGridLayout *lo = new TQGridLayout( w );
434 lo->setSpacing( KDialogBase::spacingHint() );
435
436 TQLabel *l;
437
438 leName = new TQLineEdit( w );
439 lo->addWidget( leName, 1, 2 );
440 l = new TQLabel( leName, i18n("&Label:"), w );
441 l->setAlignment( l->alignment()|TQt::AlignRight );
442 lo->addWidget( l, 1, 1 );
443 if ( tool ) leName->setText( tool->name );
444 TQWhatsThis::add( leName, i18n(
445 "The name will be displayed in the 'Tools->External' menu") );
446
447 btnIcon = new TDEIconButton( w );
448 btnIcon->setIconSize( TDEIcon::SizeSmall );
449 lo->addWidget( btnIcon, 1, 3 );
450 if ( tool && !tool->icon.isEmpty() )
451 btnIcon->setIcon( tool->icon );
452
453 teCommand = new TQTextEdit( w );
454 lo->addMultiCellWidget( teCommand, 2, 2, 2, 3 );
455 l = new TQLabel( teCommand, i18n("S&cript:"), w );
456 l->setAlignment( TQt::AlignTop|TQt::AlignRight );
457 lo->addWidget( l, 2, 1 );
458 if ( tool ) teCommand->setText( tool->command );
459 TQWhatsThis::add( teCommand, i18n(
460 "<p>The script to execute to invoke the tool. The script is passed "
461 "to /bin/sh for execution. The following macros "
462 "will be expanded:</p>"
463 "<ul><li><code>%URL</code> - the URL of the current document."
464 "<li><code>%URLs</code> - a list of the URLs of all open documents."
465 "<li><code>%directory</code> - the URL of the directory containing "
466 "the current document."
467 "<li><code>%filename</code> - the filename of the current document."
468 "<li><code>%line</code> - the current line of the text cursor in the "
469 "current view."
470 "<li><code>%column</code> - the column of the text cursor in the "
471 "current view."
472 "<li><code>%selection</code> - the selected text in the current view."
473 "<li><code>%text</code> - the text of the current document.</ul>" ) );
474
475
476 leExecutable = new TQLineEdit( w );
477 lo->addMultiCellWidget( leExecutable, 3, 3, 2, 3 );
478 l = new TQLabel( leExecutable, i18n("&Executable:"), w );
479 l->setAlignment( l->alignment()|TQt::AlignRight );
480 lo->addWidget( l, 3, 1 );
481 if ( tool ) leExecutable->setText( tool->tryexec );
482 TQWhatsThis::add( leExecutable, i18n(
483 "The executable used by the command. This is used to check if a tool "
484 "should be displayed; if not set, the first word of <em>command</em> "
485 "will be used.") );
486
487 leMimetypes = new TQLineEdit( w );
488 lo->addWidget( leMimetypes, 4, 2 );
489 l = new TQLabel( leMimetypes, i18n("&Mime types:"), w );
490 l->setAlignment( l->alignment()|TQt::AlignRight );
491 lo->addWidget( l, 4, 1 );
492 if ( tool ) leMimetypes->setText( tool->mimetypes.join("; ") );
493 TQWhatsThis::add( leMimetypes, i18n(
494 "A semicolon-separated list of mime types for which this tool should "
495 "be available; if this is left empty, the tool is always available. "
496 "To choose from known mimetypes, press the button on the right.") );
497
498 TQToolButton *btnMTW = new TQToolButton(w);
499 lo->addWidget( btnMTW, 4, 3 );
500 btnMTW->setIconSet(TQIconSet(SmallIcon("wizard")));
501 connect(btnMTW, TQ_SIGNAL(clicked()), this, TQ_SLOT(showMTDlg()));
502 TQWhatsThis::add( btnMTW, i18n(
503 "Click for a dialog that can help you creating a list of mimetypes.") );
504
505 cmbSave = new TQComboBox(w);
506 lo->addMultiCellWidget( cmbSave, 5, 5, 2, 3 );
507 l = new TQLabel( cmbSave, i18n("&Save:"), w );
508 l->setAlignment( l->alignment()|TQt::AlignRight );
509 lo->addWidget( l, 5, 1 );
510 TQStringList sl;
511 sl << i18n("None") << i18n("Current Document") << i18n("All Documents");
512 cmbSave->insertStringList( sl );
513 if ( tool ) cmbSave->setCurrentItem( tool->save );
514 TQWhatsThis::add( cmbSave, i18n(
515 "You can elect to save the current or all [modified] documents prior to "
516 "running the command. This is helpful if you want to pass URLs to "
517 "an application like, for example, an FTP client.") );
518
519
520 leCmdLine = new TQLineEdit( w );
521 lo->addMultiCellWidget( leCmdLine, 6, 6, 2, 3 );
522 l = new TQLabel( leCmdLine, i18n("&Command line name:"), w );
523 l->setAlignment( l->alignment()|TQt::AlignRight );
524 lo->addWidget( l, 6, 1 );
525 if ( tool ) leCmdLine->setText( tool->cmdname );
526 TQWhatsThis::add( leCmdLine, i18n(
527 "If you specify a name here, you can invoke the command from the view "
528 "command lines with exttool-the_name_you_specified_here. "
529 "Please do not use spaces or tabs in the name."));
530
531}
532
533void KateExternalToolServiceEditor::slotOk()
534{
535 if ( leName->text().isEmpty() ||
536 teCommand->text().isEmpty() )
537 {
538 KMessageBox::information( this, i18n("You must specify at least a name and a command") );
539 return;
540 }
541
542 KDialogBase::slotOk();
543}
544
545void KateExternalToolServiceEditor::showMTDlg()
546{
547 TQString text = i18n("Select the MimeTypes for which to enable this tool.");
548 TQStringList list = TQStringList::split( TQRegExp("\\s*;\\s*"), leMimetypes->text() );
549 KMimeTypeChooserDialog d( i18n("Select Mime Types"), text, list, "text", this );
550 if ( d.exec() == KDialogBase::Accepted ) {
551 leMimetypes->setText( d.chooser()->mimeTypes().join(";") );
552 }
553}
554//END KateExternalToolServiceEditor
555
556//BEGIN KateExternalToolsConfigWidget
557KateExternalToolsConfigWidget::KateExternalToolsConfigWidget( TQWidget *parent, const char* name )
558 : Kate::ConfigPage( parent, name ),
559 m_changed( false )
560{
561 TQGridLayout *lo = new TQGridLayout( this, 5, 5, 0, KDialog::spacingHint() );
562
563 lbTools = new TDEListBox( this );
564 lo->addMultiCellWidget( lbTools, 1, 4, 0, 3 );
565 connect( lbTools, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(slotSelectionChanged()) );
566
567 btnNew = new TQPushButton( i18n("&New..."), this );
568 lo->addWidget( btnNew, 5, 0 );
569 connect( btnNew, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotNew()) );
570
571 btnRemove = new TQPushButton( i18n("&Remove"), this );
572 lo->addWidget( btnRemove, 5, 2 );
573 connect( btnRemove, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotRemove()) );
574
575 btnEdit = new TQPushButton( i18n("&Edit..."), this );
576 lo->addWidget( btnEdit, 5, 1 );
577 connect( btnEdit, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotEdit()) );
578
579 TQPushButton *b = new TQPushButton( i18n("Insert &Separator"), this );
580 lo->addWidget( b, 5, 3 );
581 connect( b, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotInsertSeparator()) );
582
583 btnMoveUp = new TQPushButton( SmallIconSet("go-up"), "", this );
584 lo->addWidget( btnMoveUp, 2, 4 );
585 connect( btnMoveUp, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotMoveUp()) );
586
587 btnMoveDwn = new TQPushButton( SmallIconSet("go-down"), "", this );
588 lo->addWidget( btnMoveDwn, 3, 4 );
589 connect( btnMoveDwn, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotMoveDown()) );
590
591 connect( lbTools, TQ_SIGNAL( doubleClicked ( TQListBoxItem * ) ), this, TQ_SLOT( slotEdit() ) );
592
593 lo->setRowStretch( 1, 1 );
594 lo->setRowStretch( 4, 1 );
595 lo->setColStretch( 0, 1 );
596 lo->setColStretch( 1, 1 );
597 lo->setColStretch( 2, 1 );
598
599
600 TQWhatsThis::add( lbTools, i18n(
601 "This list shows all the configured tools, represented by their menu text.") );
602
603 config = new TDEConfig("externaltools", false, false, "appdata");
604 reload();
605 slotSelectionChanged();
606}
607
608KateExternalToolsConfigWidget::~KateExternalToolsConfigWidget()
609{
610 delete config;
611}
612
613void KateExternalToolsConfigWidget::reload()
614{
615 //m_tools.clear();
616 lbTools->clear();
617
618 // load the files from a TDEConfig
619 config->setGroup( "Global" );
620 TQStringList tools = config->readListEntry("tools");
621
622 for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
623 {
624 if ( *it == "---" )
625 {
626 new TQListBoxText( lbTools, "---" );
627 }
628 else
629 {
630 config->setGroup( *it );
631
632 KateExternalTool *t = new KateExternalTool(
633 config->readEntry( "name", "" ),
634 config->readEntry( "command", ""),
635 config->readEntry( "icon", ""),
636 config->readEntry( "executable", ""),
637 config->readListEntry( "mimetypes" ),
638 config->readEntry( "acname" ),
639 config->readEntry( "cmdname"),
640 config->readNumEntry( "save", 0 ) );
641
642 if ( t->hasexec ) // we only show tools that are also in the menu.
643 new ToolItem( lbTools, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t );
644 }
645 }
646 m_changed = false;
647}
648
649TQPixmap KateExternalToolsConfigWidget::blankIcon()
650{
651 TQPixmap pm( TDEIcon::SizeSmall, TDEIcon::SizeSmall );
652 pm.fill();
653 pm.setMask( pm.createHeuristicMask() );
654 return pm;
655}
656
657void KateExternalToolsConfigWidget::apply()
658{
659 if ( ! m_changed )
660 return;
661 m_changed = false;
662
663 // save a new list
664 // save each item
665 TQStringList tools;
666 for ( uint i = 0; i < lbTools->count(); i++ )
667 {
668 if ( lbTools->text( i ) == "---" )
669 {
670 tools << "---";
671 continue;
672 }
673 KateExternalTool *t = ((ToolItem*)lbTools->item( i ))->tool;
674// kdDebug(13001)<<"adding tool: "<<t->name<<endl;
675 tools << t->acname;
676
677 config->setGroup( t->acname );
678 config->writeEntry( "name", t->name );
679 config->writeEntry( "command", t->command );
680 config->writeEntry( "icon", t->icon );
681 config->writeEntry( "executable", t->tryexec );
682 config->writeEntry( "mimetypes", t->mimetypes );
683 config->writeEntry( "acname", t->acname );
684 config->writeEntry( "cmdname", t->cmdname );
685 config->writeEntry( "save", t->save );
686 }
687
688 config->setGroup("Global");
689 config->writeEntry( "tools", tools );
690
691 // if any tools was removed, try to delete their groups, and
692 // add the group names to the list of removed items.
693 if ( m_removed.count() )
694 {
695 for ( TQStringList::iterator it = m_removed.begin(); it != m_removed.end(); ++it )
696 {
697 if ( config->hasGroup( *it ) )
698 config->deleteGroup( *it );
699 }
700 TQStringList removed = config->readListEntry( "removed" );
701 removed += m_removed;
702
703 // clean up the list of removed items, so that it does not contain
704 // non-existing groups (we can't remove groups from a non-owned global file).
705 config->sync();
706 TQStringList::iterator it1 = removed.begin();
707 while( it1 != removed.end() )
708 {
709 if ( ! config->hasGroup( *it1 ) )
710 it1 = removed.remove( it1 );
711 else
712 ++it1;
713 }
714 config->writeEntry( "removed", removed );
715 }
716
717 config->sync();
718}
719
720void KateExternalToolsConfigWidget::slotSelectionChanged()
721{
722 // update button state
723 bool hs = lbTools->selectedItem() != 0;
724 btnEdit->setEnabled( hs && dynamic_cast<ToolItem*>(lbTools->selectedItem()) );
725 btnRemove->setEnabled( hs );
726 btnMoveUp->setEnabled( ( lbTools->currentItem() > 0 ) && hs );
727 btnMoveDwn->setEnabled( ( lbTools->currentItem() < (int)lbTools->count()-1 )&&hs );
728}
729
730void KateExternalToolsConfigWidget::slotNew()
731{
732 // display a editor, and if it is OK'd, create a new tool and
733 // create a listbox item for it
734 KateExternalToolServiceEditor editor( 0, this );
735
736 if ( editor.exec() )
737 {
738 KateExternalTool *t = new KateExternalTool(
739 editor.leName->text(),
740 editor.teCommand->text(),
741 editor.btnIcon->icon(),
742 editor.leExecutable->text(),
743 TQStringList::split( TQRegExp("\\s*;\\s*"), editor.leMimetypes->text() ) );
744
745 // This is sticky, it does not change again, so that shortcuts sticks
746 // TODO check for dups
747 t->acname = "externaltool_" + TQString(t->name).replace( TQRegExp("\\W+"), "" );
748
749 new ToolItem ( lbTools, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t );
750
751 slotChanged();
752 m_changed = true;
753 }
754}
755
756void KateExternalToolsConfigWidget::slotRemove()
757{
758 // add the tool action name to a list of removed items,
759 // remove the current listbox item
760 if ( lbTools->currentItem() > -1 ) {
761 ToolItem *i = dynamic_cast<ToolItem*>(lbTools->selectedItem());
762 if ( i )
763 m_removed << i->tool->acname;
764
765 lbTools->removeItem( lbTools->currentItem() );
766 slotChanged();
767 m_changed = true;
768 }
769}
770
771void KateExternalToolsConfigWidget::slotEdit()
772{
773 if( !dynamic_cast<ToolItem*>(lbTools->selectedItem()) ) return;
774 // show the item in an editor
775 KateExternalTool *t = ((ToolItem*)lbTools->selectedItem())->tool;
776 KateExternalToolServiceEditor editor( t, this);
777 config->setGroup( "Editor" );
778 editor.resize( config->readSizeEntry( "Size" ) );
779 if ( editor.exec() /*== KDialogBase::Ok*/ )
780 {
781
782 bool elementChanged = ( ( editor.btnIcon->icon() != t->icon ) || (editor.leName->text() != t->name ) ) ;
783
784 t->name = editor.leName->text();
785 t->cmdname = editor.leCmdLine->text();
786 t->command = editor.teCommand->text();
787 t->icon = editor.btnIcon->icon();
788 t->tryexec = editor.leExecutable->text();
789 t->mimetypes = TQStringList::split( TQRegExp("\\s*;\\s*"), editor.leMimetypes->text() );
790 t->save = editor.cmbSave->currentItem();
791
792 //if the icon has changed or name changed, I have to renew the listbox item :S
793 if ( elementChanged )
794 {
795 int idx = lbTools->index( lbTools->selectedItem() );
796 lbTools->removeItem( idx );
797 lbTools->insertItem( new ToolItem( 0, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t ), idx );
798 }
799
800 slotChanged();
801 m_changed = true;
802 }
803
804 config->setGroup( "Editor" );
805 config->writeEntry( "Size", editor.size() );
806 config->sync();
807}
808
809void KateExternalToolsConfigWidget::slotInsertSeparator()
810{
811 lbTools->insertItem( "---", lbTools->currentItem()+1 );
812 slotChanged();
813 m_changed = true;
814}
815
816void KateExternalToolsConfigWidget::slotMoveUp()
817{
818 // move the current item in the listbox upwards if possible
819 TQListBoxItem *item = lbTools->selectedItem();
820 if ( ! item ) return;
821
822 int idx = lbTools->index( item );
823
824 if ( idx < 1 ) return;
825
826 if ( dynamic_cast<ToolItem*>(item) )
827 {
828 KateExternalTool *tool = ((ToolItem*)item)->tool;
829 lbTools->removeItem( idx );
830 lbTools->insertItem( new ToolItem( 0, tool->icon.isEmpty() ? blankIcon() : SmallIcon( tool->icon ), tool ), idx-1 );
831 }
832 else // a separator!
833 {
834 lbTools->removeItem( idx );
835 lbTools->insertItem( new TQListBoxText( 0, "---" ), idx-1 );
836 }
837
838 lbTools->setCurrentItem( idx - 1 );
839 slotSelectionChanged();
840 slotChanged();
841 m_changed = true;
842}
843
844void KateExternalToolsConfigWidget::slotMoveDown()
845{
846 // move the current item in the listbox downwards if possible
847 TQListBoxItem *item = lbTools->selectedItem();
848 if ( ! item ) return;
849
850 uint idx = lbTools->index( item );
851
852 if ( idx > lbTools->count()-1 ) return;
853
854 if ( dynamic_cast<ToolItem*>(item) )
855 {
856 KateExternalTool *tool = ((ToolItem*)item)->tool;
857 lbTools->removeItem( idx );
858 lbTools->insertItem( new ToolItem( 0, tool->icon.isEmpty() ? blankIcon() : SmallIcon( tool->icon ), tool ), idx+1 );
859 }
860 else // a separator!
861 {
862 lbTools->removeItem( idx );
863 lbTools->insertItem( new TQListBoxText( 0, "---" ), idx+1 );
864 }
865
866 lbTools->setCurrentItem( idx+1 );
867 slotSelectionChanged();
868 slotChanged();
869 m_changed = true;
870}
871//END KateExternalToolsConfigWidget
KateExternalToolAction
This Action contains a KateExternalTool.
Definition: kateexternaltools.h:82
KateExternalToolServiceEditor
A Dialog to edit a single KateExternalTool object.
Definition: kateexternaltools.h:203
KateExternalTool
This class defines a single external tool.
Definition: kateexternaltools.h:101
KateExternalTool::save
int save
We can save documents prior to activating the tool command: 0 = nothing, 1 = current document,...
Definition: kateexternaltools.h:121
KateExternalTool::mimetypes
TQStringList mimetypes
Optional list of mimetypes for which this action is valid.
Definition: kateexternaltools.h:117
KateExternalTool::checkExec
bool checkExec()
Definition: kateexternaltools.cpp:89
KateExternalTool::valid
bool valid(const TQString &mimetype) const
Definition: kateexternaltools.cpp:128
KateExternalTool::cmdname
TQString cmdname
The name for the commandline.
Definition: kateexternaltools.h:120
KateExternalTool::acname
TQString acname
The name for the action. This is generated first time the action is is created.
Definition: kateexternaltools.h:119
KateExternalTool::name
TQString name
The name used in the menu.
Definition: kateexternaltools.h:113
KateExternalTool::icon
TQString icon
the icon to use in the menu.
Definition: kateexternaltools.h:115
KateExternalTool::hasexec
bool hasexec
This is set by the constructor by calling checkExec(), if a value is present.
Definition: kateexternaltools.h:118
KateExternalTool::tryexec
TQString tryexec
The name or path of the executable.
Definition: kateexternaltools.h:116
KateExternalTool::command
TQString command
The command to execute.
Definition: kateexternaltools.h:114
KateExternalToolsCommand
A Singleton class for invoking external tools with the view command line.
Definition: kateexternaltools.h:182
KateExternalToolsMenuAction
The external tools action This action creates a menu, in which each item will launch a process with t...
Definition: kateexternaltools.h:55
KateExternalToolsMenuAction::reload
void reload()
This will load all the confiured services.
Definition: kateexternaltools.cpp:308
Kate
Namespace collecting as much of the internal Kate classes as we can manage.
Definition: kateapp.h:32

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kate

Skip menu "kate"
  • kate
  • libkonq
  • twin
  •   lib
Generated for kate by doxygen 1.9.4
This website is maintained by Timothy Pearson.