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

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.8.1.2
This website is maintained by Timothy Pearson.