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

kate

kateexternaltools.cpp

00001 /*
00002    This file is part of the Kate text editor of the KDE project.
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 
00018    ---
00019    Copyright (C) 2004, Anders Lund <anders@alweb.dk>
00020 */
00021 // TODO
00022 // Icons
00023 // Direct shortcut setting
00024 //BEGIN Includes
00025 #include "kateexternaltools.h"
00026 #include "kateexternaltools.moc"
00027 #include "katedocmanager.h"
00028 #include "kateviewmanager.h"
00029 #include "kateapp.h"
00030 
00031 #include "katemainwindow.h"
00032 
00033 #include <kate/view.h>
00034 #include <kate/document.h>
00035 
00036 #include <tdelistbox.h>
00037 #include <tdelocale.h>
00038 #include <kiconloader.h>
00039 #include <tdemessagebox.h>
00040 #include <kmimetypechooser.h>
00041 #include <tdeconfig.h>
00042 #include <krun.h>
00043 #include <kicondialog.h>
00044 #include <tdepopupmenu.h>
00045 #include <kdebug.h>
00046 
00047 #include <tqbitmap.h>
00048 #include <tqcombobox.h>
00049 #include <tqfile.h>
00050 #include <tqpushbutton.h>
00051 #include <tqlineedit.h>
00052 #include <tqlayout.h>
00053 #include <tqlabel.h>
00054 #include <tqlistbox.h>
00055 #include <tqmap.h>
00056 #include <tqregexp.h>
00057 #include <tqtextedit.h>
00058 #include <tqtoolbutton.h>
00059 #include <tqwhatsthis.h>
00060 
00061 #include <stdlib.h>
00062 #include <unistd.h>
00063 //END Includes
00064 
00065 KateExternalToolsCommand *KateExternalToolsCommand::s_self=0;
00066 
00067 //BEGIN KateExternalTool
00068 KateExternalTool::KateExternalTool( const TQString &name,
00069                       const TQString &command,
00070                       const TQString &icon,
00071                       const TQString &tryexec,
00072                       const TQStringList &mimetypes,
00073                       const TQString &acname,
00074                       const TQString &cmdname,
00075                       int save )
00076   : name ( name ),
00077     command ( command ),
00078     icon ( icon ),
00079     tryexec ( tryexec ),
00080     mimetypes ( mimetypes ),
00081     acname ( acname ),
00082     cmdname ( cmdname ),
00083     save ( save )
00084 {
00085   //if ( ! tryexec.isEmpty() )
00086     hasexec = checkExec();
00087 }
00088 
00089 bool KateExternalTool::checkExec()
00090 {
00091   // if tryexec is empty, it is the first word of command
00092   if ( tryexec.isEmpty() )
00093     tryexec = command.section( " ", 0, 0, TQString::SectionSkipEmpty );
00094 
00095   // NOTE this code is modified taken from kdesktopfile.cpp, from KDesktopFile::tryExec()
00096   if (!tryexec.isEmpty()) {
00097     if (tryexec[0] == '/') {
00098       if (::access(TQFile::encodeName(tryexec), R_OK | X_OK))
00099     return false;
00100 
00101       m_exec = tryexec;
00102     } else {
00103       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00104       // Environment PATH may contain filenames in 8bit locale cpecified
00105       // encoding (Like a filenames).
00106       TQStringList dirs = TQStringList::split(':', TQFile::decodeName(::getenv("PATH")));
00107       TQStringList::Iterator it(dirs.begin());
00108       bool match = false;
00109       for (; it != dirs.end(); ++it)
00110       {
00111     TQString fName = *it + "/" + tryexec;
00112     if (::access(TQFile::encodeName(fName), R_OK | X_OK) == 0)
00113     {
00114       match = true;
00115           m_exec = fName;
00116       break;
00117     }
00118       }
00119       // didn't match at all
00120       if (!match)
00121         return false;
00122     }
00123     return true;
00124   }
00125   return false;
00126 }
00127 
00128 bool KateExternalTool::valid( const TQString &mt ) const
00129 {
00130   return mimetypes.isEmpty() || mimetypes.contains( mt );
00131 }
00132 //END KateExternalTool
00133 
00134 //BEGIN KateExternalToolsCommand
00135 KateExternalToolsCommand::KateExternalToolsCommand() : Kate::Command() {
00136     m_inited=false;
00137     reload();
00138 }
00139 
00140 TQStringList KateExternalToolsCommand::cmds () {
00141     return m_list;
00142 }
00143 
00144 KateExternalToolsCommand *KateExternalToolsCommand::self () {
00145     if (s_self) return s_self;
00146     s_self=new KateExternalToolsCommand;
00147     return s_self;
00148 }
00149 
00150 void KateExternalToolsCommand::reload () {
00151   m_list.clear();
00152   m_map.clear();
00153 
00154   TDEConfig config("externaltools", false, false, "appdata");
00155   config.setGroup("Global");
00156   TQStringList tools = config.readListEntry("tools");
00157 
00158 
00159   for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
00160   {
00161     if ( *it == "---" )
00162       continue;
00163 
00164 
00165     config.setGroup( *it );
00166 
00167     KateExternalTool t = KateExternalTool(
00168         config.readEntry( "name", "" ),
00169         config.readEntry( "command", ""),
00170         config.readEntry( "icon", ""),
00171         config.readEntry( "executable", ""),
00172         config.readListEntry( "mimetypes" ),
00173         config.readEntry( "acname", "" ),
00174         config.readEntry( "cmdname", "" ) );
00175     if ( t.hasexec && (!t.cmdname.isEmpty())) {
00176         m_list.append("exttool-"+t.cmdname);
00177         m_map.insert("exttool-"+t.cmdname,t.acname);
00178     }
00179   }
00180   if (m_inited) {
00181       Kate::Document::unregisterCommand(this);
00182       Kate::Document::registerCommand(this);
00183    }
00184   else m_inited=true;
00185 }
00186 
00187 bool KateExternalToolsCommand::exec (Kate::View *view, const TQString &cmd, TQString &) {
00188     TQWidget *wv=tqt_dynamic_cast<TQWidget*>(view);
00189     if (!wv) {
00190 //      kdDebug(13001)<<"KateExternalToolsCommand::exec: Could not get view widget"<<endl;
00191         return false;
00192     }
00193   KateMDI::MainWindow *dmw=tqt_dynamic_cast<KateMDI::MainWindow*>(wv->topLevelWidget());
00194     if (!dmw) {
00195 //      kdDebug(13001)<<"KateExternalToolsCommand::exec: Could not get main window"<<endl;
00196         return false;
00197     }
00198 //  kdDebug(13001)<<"cmd="<<cmd.stripWhiteSpace()<<endl;
00199     TQString actionName=m_map[cmd.stripWhiteSpace()];
00200     if (actionName.isEmpty()) return false;
00201 //  kdDebug(13001)<<"actionName is not empty:"<<actionName<<endl;
00202     KateExternalToolsMenuAction *a=
00203         tqt_dynamic_cast<KateExternalToolsMenuAction*>(dmw->action("tools_external"));
00204     if (!a) return false;
00205 //  kdDebug(13001)<<"trying to find action"<<endl;
00206     TDEAction *a1=a->actionCollection()->action(static_cast<const char *>(actionName.utf8()));
00207     if (!a1) return false;
00208 //  kdDebug(13001)<<"activating action"<<endl;
00209     a1->activate();
00210     return true;
00211 }
00212 
00213 bool KateExternalToolsCommand::help (Kate::View *, const TQString &, TQString &) {
00214     return false;
00215 }
00216 //END KateExternalToolsCommand
00217 
00218 //BEGIN KateExternalToolAction
00219 KateExternalToolAction::KateExternalToolAction( TQObject *parent,
00220              const char *name, KateExternalTool *t)
00221   : TDEAction( parent, name ),
00222     tool ( t )
00223 {
00224   setText( t->name );
00225   if ( ! t->icon.isEmpty() )
00226     setIconSet( SmallIconSet( t->icon ) );
00227 
00228   connect( this ,TQT_SIGNAL(activated()), this, TQT_SLOT(slotRun()) );
00229 }
00230 
00231 bool KateExternalToolAction::expandMacro( const TQString &str, TQStringList &ret )
00232 {
00233   KateMainWindow *mw = (KateMainWindow*)parent()->parent();
00234 
00235   Kate::View *view = mw->viewManager()->activeView();
00236   if ( ! view ) return false;
00237 
00238 
00239   if ( str == "URL" )
00240     ret += mw->activeDocumentUrl().url();
00241   else if ( str == "directory" ) // directory of current doc
00242     ret += mw->activeDocumentUrl().directory();
00243   else if ( str == "filename" )
00244     ret += mw->activeDocumentUrl().fileName();
00245   else if ( str == "line" ) // cursor line of current doc
00246     ret += TQString::number( view->cursorLine() );
00247   else if ( str == "col" ) // cursor col of current doc
00248     ret += TQString::number( view->cursorColumn() );
00249   else if ( str == "selection" ) // selection of current doc if any
00250     ret += view->getDoc()->selection();
00251   else if ( str == "text" ) // text of current doc
00252     ret += view->getDoc()->text();
00253   else if ( str == "URLs" ) {
00254     for( Kate::Document *doc = KateDocManager::self()->firstDocument(); doc; doc = KateDocManager::self()->nextDocument() )
00255       if ( ! doc->url().isEmpty() )
00256         ret += doc->url().url();
00257   } else
00258     return false;
00259   return true;
00260 }
00261 
00262 KateExternalToolAction::~KateExternalToolAction() {
00263     delete(tool);
00264 }
00265 
00266 void KateExternalToolAction::slotRun()
00267 {
00268   // expand the macros in command if any,
00269   // and construct a command with an absolute path
00270   TQString cmd = tool->command;
00271 
00272   if ( ! expandMacrosShellQuote( cmd ) )
00273   {
00274     KMessageBox::sorry( (KateMainWindow*)parent()->parent(),
00275                          i18n("Failed to expand the command '%1'.").arg( cmd ),
00276                          i18n( "Kate External Tools") );
00277     return;
00278   }
00279   kdDebug(13001)<<"externaltools: Running command: "<<cmd<<endl;
00280 
00281   // save documents if requested
00282   KateMainWindow *mw = (KateMainWindow*)parent()->parent();
00283   if ( tool->save == 1 )
00284     mw->viewManager()->activeView()->document()->save();
00285   else if ( tool->save == 2 )
00286     mw->actionCollection()->action("file_save_all")->activate();
00287 
00288   KRun::runCommand( cmd, tool->tryexec, tool->icon );
00289 }
00290 //END KateExternalToolAction
00291 
00292 //BEGIN KateExternalToolsMenuAction
00293 KateExternalToolsMenuAction::KateExternalToolsMenuAction( const TQString &text,
00294                                                TQObject *parent,
00295                                                const char* name,
00296                                                KateMainWindow *mw )
00297     : TDEActionMenu( text, parent, name ),
00298       mainwindow( mw )
00299 {
00300 
00301   m_actionCollection = new TDEActionCollection( mainwindow );
00302 
00303   connect(KateDocManager::self(),TQT_SIGNAL(documentChanged()),this,TQT_SLOT(slotDocumentChanged()));
00304 
00305   reload();
00306 }
00307 
00308 void KateExternalToolsMenuAction::reload()
00309 {
00310   m_actionCollection->clear ();
00311   popupMenu()->clear();
00312 
00313   // load all the tools, and create a action for each of them
00314   TDEConfig *config = new TDEConfig( "externaltools", false, false, "appdata" );
00315   config->setGroup( "Global" );
00316   TQStringList tools = config->readListEntry( "tools" );
00317 
00318   // if there are tools that are present but not explicitly removed,
00319   // add them to the end of the list
00320   config->setReadDefaults( true );
00321   TQStringList dtools = config->readListEntry( "tools" );
00322   int gver = config->readNumEntry( "version", 1 );
00323   config->setReadDefaults( false );
00324 
00325   int ver = config->readNumEntry( "version" );
00326   if ( ver <= gver )
00327   {
00328     TQStringList removed = config->readListEntry( "removed" );
00329     bool sepadded = false;
00330     for (TQStringList::iterator itg = dtools.begin(); itg != dtools.end(); ++itg )
00331     {
00332       if ( ! tools.contains( *itg ) &&
00333             ! removed.contains( *itg ) )
00334       {
00335         if ( ! sepadded )
00336         {
00337           tools << "---";
00338           sepadded = true;
00339         }
00340         tools << *itg;
00341       }
00342     }
00343 
00344     config->writeEntry( "tools", tools );
00345     config->sync();
00346     config->writeEntry( "version", gver );
00347   }
00348 
00349   for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
00350   {
00351     if ( *it == "---" )
00352     {
00353       popupMenu()->insertSeparator();
00354       // a separator
00355       continue;
00356     }
00357 
00358     config->setGroup( *it );
00359 
00360     KateExternalTool *t = new KateExternalTool(
00361         config->readEntry( "name", "" ),
00362         config->readEntry( "command", ""),
00363         config->readEntry( "icon", ""),
00364         config->readEntry( "executable", ""),
00365         config->readListEntry( "mimetypes" ),
00366         config->readEntry( "acname", "" ),
00367         config->readEntry( "cmdname", "" ),
00368         config->readNumEntry( "save", 0 ) );
00369 
00370     if ( t->hasexec )
00371       insert( new KateExternalToolAction( m_actionCollection, t->acname.ascii(), t ) );
00372   }
00373 
00374   m_actionCollection->readShortcutSettings( "Shortcuts", config );
00375   slotDocumentChanged();
00376   delete config;
00377 }
00378 
00379 void KateExternalToolsMenuAction::slotDocumentChanged()
00380 {
00381   // try to enable/disable to match current mime type
00382   Kate::DocumentExt *de = documentExt( KateDocManager::self()->activeDocument() );
00383   if ( de )
00384   {
00385     TQString mt = de->mimeType();
00386     TQStringList l;
00387     bool b;
00388 
00389     TDEActionPtrList actions = m_actionCollection->actions();
00390     for (TDEActionPtrList::iterator it = actions.begin(); it != actions.end(); ++it )
00391     {
00392       KateExternalToolAction *action = tqt_dynamic_cast<KateExternalToolAction*>(*it);
00393       if ( action )
00394       {
00395         l = action->tool->mimetypes;
00396         b = ( ! l.count() || l.contains( mt ) );
00397         action->setEnabled( b );
00398       }
00399     }
00400   }
00401 }
00402 //END KateExternalToolsMenuAction
00403 
00404 //BEGIN ToolItem
00409 class ToolItem : public TQListBoxPixmap
00410 {
00411   public:
00412     ToolItem( TQListBox *lb, const TQPixmap &icon, KateExternalTool *tool )
00413         : TQListBoxPixmap( lb, icon, tool->name ),
00414           tool ( tool )
00415     {;}
00416 
00417     ~ToolItem() {};
00418 
00419     KateExternalTool *tool;
00420 };
00421 //END ToolItem
00422 
00423 //BEGIN KateExternalToolServiceEditor
00424 KateExternalToolServiceEditor::KateExternalToolServiceEditor( KateExternalTool *tool,
00425                                 TQWidget *parent, const char *name )
00426     : KDialogBase( parent, name, true, i18n("Edit External Tool"), KDialogBase::Ok|KDialogBase::Cancel ),
00427       tool( tool )
00428 {
00429     // create a entry for each property
00430     // fill in the values from the service if available
00431   TQWidget *w = new TQWidget( this );
00432   setMainWidget( w );
00433   TQGridLayout *lo = new TQGridLayout( w );
00434   lo->setSpacing( KDialogBase::spacingHint() );
00435 
00436   TQLabel *l;
00437 
00438   leName = new TQLineEdit( w );
00439   lo->addWidget( leName, 1, 2 );
00440   l = new TQLabel( leName, i18n("&Label:"), w );
00441   l->setAlignment( l->alignment()|Qt::AlignRight );
00442   lo->addWidget( l, 1, 1 );
00443   if ( tool ) leName->setText( tool->name );
00444   TQWhatsThis::add( leName, i18n(
00445       "The name will be displayed in the 'Tools->External' menu") );
00446 
00447   btnIcon = new TDEIconButton( w );
00448   btnIcon->setIconSize( TDEIcon::SizeSmall );
00449   lo->addWidget( btnIcon, 1, 3 );
00450   if ( tool && !tool->icon.isEmpty() )
00451     btnIcon->setIcon( tool->icon );
00452 
00453   teCommand = new TQTextEdit( w );
00454   lo->addMultiCellWidget( teCommand, 2, 2, 2, 3 );
00455   l = new TQLabel( teCommand, i18n("S&cript:"), w );
00456   l->setAlignment( Qt::AlignTop|Qt::AlignRight );
00457   lo->addWidget( l, 2, 1 );
00458   if ( tool ) teCommand->setText( tool->command );
00459   TQWhatsThis::add( teCommand, i18n(
00460       "<p>The script to execute to invoke the tool. The script is passed "
00461       "to /bin/sh for execution. The following macros "
00462       "will be expanded:</p>"
00463       "<ul><li><code>%URL</code> - the URL of the current document."
00464       "<li><code>%URLs</code> - a list of the URLs of all open documents."
00465       "<li><code>%directory</code> - the URL of the directory containing "
00466       "the current document."
00467       "<li><code>%filename</code> - the filename of the current document."
00468       "<li><code>%line</code> - the current line of the text cursor in the "
00469       "current view."
00470       "<li><code>%column</code> - the column of the text cursor in the "
00471       "current view."
00472       "<li><code>%selection</code> - the selected text in the current view."
00473       "<li><code>%text</code> - the text of the current document.</ul>" ) );
00474 
00475 
00476   leExecutable = new TQLineEdit( w );
00477   lo->addMultiCellWidget( leExecutable, 3, 3, 2, 3 );
00478   l = new TQLabel( leExecutable, i18n("&Executable:"), w );
00479   l->setAlignment( l->alignment()|Qt::AlignRight );
00480   lo->addWidget( l, 3, 1 );
00481   if ( tool ) leExecutable->setText( tool->tryexec );
00482   TQWhatsThis::add( leExecutable, i18n(
00483       "The executable used by the command. This is used to check if a tool "
00484       "should be displayed; if not set, the first word of <em>command</em> "
00485       "will be used.") );
00486 
00487   leMimetypes = new TQLineEdit( w );
00488   lo->addWidget( leMimetypes, 4, 2 );
00489   l = new TQLabel( leMimetypes, i18n("&Mime types:"), w );
00490   l->setAlignment( l->alignment()|Qt::AlignRight );
00491   lo->addWidget( l, 4, 1 );
00492   if ( tool ) leMimetypes->setText( tool->mimetypes.join("; ") );
00493   TQWhatsThis::add( leMimetypes, i18n(
00494       "A semicolon-separated list of mime types for which this tool should "
00495       "be available; if this is left empty, the tool is always available. "
00496       "To choose from known mimetypes, press the button on the right.") );
00497 
00498   TQToolButton *btnMTW = new TQToolButton(w);
00499   lo->addWidget( btnMTW, 4, 3 );
00500   btnMTW->setIconSet(TQIconSet(SmallIcon("wizard")));
00501   connect(btnMTW, TQT_SIGNAL(clicked()), this, TQT_SLOT(showMTDlg()));
00502   TQWhatsThis::add( btnMTW, i18n(
00503       "Click for a dialog that can help you creating a list of mimetypes.") );
00504 
00505   cmbSave = new TQComboBox(w);
00506   lo->addMultiCellWidget( cmbSave, 5, 5, 2, 3 );
00507   l = new TQLabel( cmbSave, i18n("&Save:"), w );
00508   l->setAlignment( l->alignment()|Qt::AlignRight );
00509   lo->addWidget( l, 5, 1 );
00510   TQStringList sl;
00511   sl << i18n("None") << i18n("Current Document") << i18n("All Documents");
00512   cmbSave->insertStringList( sl );
00513   if ( tool ) cmbSave->setCurrentItem( tool->save );
00514   TQWhatsThis::add( cmbSave, i18n(
00515       "You can elect to save the current or all [modified] documents prior to "
00516       "running the command. This is helpful if you want to pass URLs to "
00517       "an application like, for example, an FTP client.") );
00518 
00519 
00520   leCmdLine = new TQLineEdit( w );
00521   lo->addMultiCellWidget( leCmdLine, 6, 6, 2, 3 );
00522   l = new TQLabel( leCmdLine, i18n("&Command line name:"), w );
00523   l->setAlignment( l->alignment()|Qt::AlignRight );
00524   lo->addWidget( l, 6, 1 );
00525   if ( tool ) leCmdLine->setText( tool->cmdname );
00526   TQWhatsThis::add( leCmdLine, i18n(
00527       "If you specify a name here, you can invoke the command from the view "
00528       "command lines with exttool-the_name_you_specified_here. "
00529       "Please do not use spaces or tabs in the name."));
00530 
00531 }
00532 
00533 void KateExternalToolServiceEditor::slotOk()
00534 {
00535   if ( leName->text().isEmpty() ||
00536        teCommand->text().isEmpty() )
00537   {
00538     KMessageBox::information( this, i18n("You must specify at least a name and a command") );
00539     return;
00540   }
00541 
00542   KDialogBase::slotOk();
00543 }
00544 
00545 void KateExternalToolServiceEditor::showMTDlg()
00546 {
00547   TQString text = i18n("Select the MimeTypes for which to enable this tool.");
00548   TQStringList list = TQStringList::split( TQRegExp("\\s*;\\s*"), leMimetypes->text() );
00549   KMimeTypeChooserDialog d( i18n("Select Mime Types"), text, list, "text", this );
00550   if ( d.exec() == KDialogBase::Accepted ) {
00551     leMimetypes->setText( d.chooser()->mimeTypes().join(";") );
00552   }
00553 }
00554 //END KateExternalToolServiceEditor
00555 
00556 //BEGIN KateExternalToolsConfigWidget
00557 KateExternalToolsConfigWidget::KateExternalToolsConfigWidget( TQWidget *parent, const char* name )
00558   : Kate::ConfigPage( parent, name ),
00559     m_changed( false )
00560 {
00561   TQGridLayout *lo = new TQGridLayout( this, 5, 5, 0, KDialog::spacingHint() );
00562 
00563   lbTools = new TDEListBox( this );
00564   lo->addMultiCellWidget( lbTools, 1, 4, 0, 3 );
00565   connect( lbTools, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()) );
00566 
00567   btnNew = new TQPushButton( i18n("&New..."), this );
00568   lo->addWidget( btnNew, 5, 0 );
00569   connect( btnNew, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNew()) );
00570 
00571   btnRemove = new TQPushButton( i18n("&Remove"), this );
00572   lo->addWidget( btnRemove, 5, 2 );
00573   connect( btnRemove, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotRemove()) );
00574 
00575   btnEdit = new TQPushButton( i18n("&Edit..."), this );
00576   lo->addWidget( btnEdit, 5, 1 );
00577   connect( btnEdit, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotEdit()) );
00578 
00579   TQPushButton *b = new TQPushButton( i18n("Insert &Separator"), this );
00580   lo->addWidget( b, 5, 3 );
00581   connect( b, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotInsertSeparator()) );
00582 
00583   btnMoveUp = new TQPushButton( SmallIconSet("go-up"), "", this );
00584   lo->addWidget( btnMoveUp, 2, 4 );
00585   connect( btnMoveUp, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotMoveUp()) );
00586 
00587   btnMoveDwn = new TQPushButton( SmallIconSet("go-down"), "", this );
00588   lo->addWidget( btnMoveDwn, 3, 4 );
00589   connect( btnMoveDwn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotMoveDown()) );
00590 
00591   connect( lbTools, TQT_SIGNAL( doubleClicked ( TQListBoxItem * ) ), this,  TQT_SLOT( slotEdit() ) );
00592 
00593   lo->setRowStretch( 1, 1 );
00594   lo->setRowStretch( 4, 1 );
00595   lo->setColStretch( 0, 1 );
00596   lo->setColStretch( 1, 1 );
00597   lo->setColStretch( 2, 1 );
00598 
00599 
00600   TQWhatsThis::add( lbTools, i18n(
00601       "This list shows all the configured tools, represented by their menu text.") );
00602 
00603   config = new TDEConfig("externaltools", false, false, "appdata");
00604   reload();
00605   slotSelectionChanged();
00606 }
00607 
00608 KateExternalToolsConfigWidget::~KateExternalToolsConfigWidget()
00609 {
00610   delete config;
00611 }
00612 
00613 void KateExternalToolsConfigWidget::reload()
00614 {
00615   //m_tools.clear();
00616   lbTools->clear();
00617 
00618   // load the files from a TDEConfig
00619   config->setGroup( "Global" );
00620   TQStringList tools = config->readListEntry("tools");
00621 
00622   for( TQStringList::Iterator it = tools.begin(); it != tools.end(); ++it )
00623   {
00624     if ( *it == "---" )
00625     {
00626       new TQListBoxText( lbTools, "---" );
00627     }
00628     else
00629     {
00630       config->setGroup( *it );
00631 
00632       KateExternalTool *t = new KateExternalTool(
00633           config->readEntry( "name", "" ),
00634           config->readEntry( "command", ""),
00635           config->readEntry( "icon", ""),
00636           config->readEntry( "executable", ""),
00637           config->readListEntry( "mimetypes" ),
00638           config->readEntry( "acname" ),
00639       config->readEntry( "cmdname"),
00640           config->readNumEntry( "save", 0 ) );
00641 
00642       if ( t->hasexec ) // we only show tools that are also in the menu.
00643         new ToolItem( lbTools, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t );
00644     }
00645   }
00646   m_changed = false;
00647 }
00648 
00649 TQPixmap KateExternalToolsConfigWidget::blankIcon()
00650 {
00651   TQPixmap pm( TDEIcon::SizeSmall, TDEIcon::SizeSmall );
00652   pm.fill();
00653   pm.setMask( pm.createHeuristicMask() );
00654   return pm;
00655 }
00656 
00657 void KateExternalToolsConfigWidget::apply()
00658 {
00659   if ( ! m_changed )
00660     return;
00661   m_changed = false;
00662 
00663   // save a new list
00664   // save each item
00665   TQStringList tools;
00666   for ( uint i = 0; i < lbTools->count(); i++ )
00667   {
00668     if ( lbTools->text( i ) == "---" )
00669     {
00670       tools << "---";
00671       continue;
00672     }
00673     KateExternalTool *t = ((ToolItem*)lbTools->item( i ))->tool;
00674 //     kdDebug(13001)<<"adding tool: "<<t->name<<endl;
00675     tools << t->acname;
00676 
00677     config->setGroup( t->acname );
00678     config->writeEntry( "name", t->name );
00679     config->writeEntry( "command", t->command );
00680     config->writeEntry( "icon", t->icon );
00681     config->writeEntry( "executable", t->tryexec );
00682     config->writeEntry( "mimetypes", t->mimetypes );
00683     config->writeEntry( "acname", t->acname );
00684     config->writeEntry( "cmdname", t->cmdname );
00685     config->writeEntry( "save", t->save );
00686   }
00687 
00688   config->setGroup("Global");
00689   config->writeEntry( "tools", tools );
00690 
00691   // if any tools was removed, try to delete their groups, and
00692   // add the group names to the list of removed items.
00693   if ( m_removed.count() )
00694   {
00695     for ( TQStringList::iterator it = m_removed.begin(); it != m_removed.end(); ++it )
00696     {
00697       if ( config->hasGroup( *it ) )
00698         config->deleteGroup( *it  );
00699     }
00700     TQStringList removed = config->readListEntry( "removed" );
00701     removed += m_removed;
00702 
00703     // clean up the list of removed items, so that it does not contain
00704     // non-existing groups (we can't remove groups from a non-owned global file).
00705     config->sync();
00706     TQStringList::iterator it1 = removed.begin();
00707     while( it1 != removed.end() )
00708     {
00709       if ( ! config->hasGroup( *it1 ) )
00710         it1 = removed.remove( it1 );
00711       else
00712         ++it1;
00713     }
00714     config->writeEntry( "removed", removed );
00715   }
00716 
00717   config->sync();
00718 }
00719 
00720 void KateExternalToolsConfigWidget::slotSelectionChanged()
00721 {
00722   // update button state
00723   bool hs =  lbTools->selectedItem() != 0;
00724   btnEdit->setEnabled( hs && dynamic_cast<ToolItem*>(lbTools->selectedItem()) );
00725   btnRemove->setEnabled( hs );
00726   btnMoveUp->setEnabled( ( lbTools->currentItem() > 0 ) && hs );
00727   btnMoveDwn->setEnabled( ( lbTools->currentItem() < (int)lbTools->count()-1 )&&hs );
00728 }
00729 
00730 void KateExternalToolsConfigWidget::slotNew()
00731 {
00732   // display a editor, and if it is OK'd, create a new tool and
00733   // create a listbox item for it
00734   KateExternalToolServiceEditor editor( 0, this );
00735 
00736   if ( editor.exec() )
00737   {
00738     KateExternalTool *t = new KateExternalTool(
00739       editor.leName->text(),
00740       editor.teCommand->text(),
00741       editor.btnIcon->icon(),
00742       editor.leExecutable->text(),
00743       TQStringList::split( TQRegExp("\\s*;\\s*"), editor.leMimetypes->text() ) );
00744 
00745     // This is sticky, it does not change again, so that shortcuts sticks
00746     // TODO check for dups
00747     t->acname = "externaltool_" + TQString(t->name).replace( TQRegExp("\\W+"), "" );
00748 
00749     new ToolItem ( lbTools, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t );
00750 
00751     slotChanged();
00752     m_changed = true;
00753   }
00754 }
00755 
00756 void KateExternalToolsConfigWidget::slotRemove()
00757 {
00758   // add the tool action name to a list of removed items,
00759   // remove the current listbox item
00760   if ( lbTools->currentItem() > -1 ) {
00761     ToolItem *i = dynamic_cast<ToolItem*>(lbTools->selectedItem());
00762     if ( i )
00763       m_removed << i->tool->acname;
00764 
00765     lbTools->removeItem( lbTools->currentItem() );
00766     slotChanged();
00767     m_changed = true;
00768   }
00769 }
00770 
00771 void KateExternalToolsConfigWidget::slotEdit()
00772 {
00773   if( !dynamic_cast<ToolItem*>(lbTools->selectedItem()) ) return;
00774   // show the item in an editor
00775   KateExternalTool *t = ((ToolItem*)lbTools->selectedItem())->tool;
00776   KateExternalToolServiceEditor editor( t, this);
00777   config->setGroup( "Editor" );
00778   editor.resize( config->readSizeEntry( "Size" ) );
00779   if ( editor.exec() /*== KDialogBase::Ok*/ )
00780   {
00781 
00782       bool elementChanged = ( ( editor.btnIcon->icon() != t->icon ) || (editor.leName->text() != t->name ) ) ;
00783 
00784     t->name = editor.leName->text();
00785     t->cmdname = editor.leCmdLine->text();
00786     t->command = editor.teCommand->text();
00787     t->icon = editor.btnIcon->icon();
00788     t->tryexec = editor.leExecutable->text();
00789     t->mimetypes = TQStringList::split( TQRegExp("\\s*;\\s*"), editor.leMimetypes->text() );
00790     t->save = editor.cmbSave->currentItem();
00791 
00792     //if the icon has changed or name changed, I have to renew the listbox item :S
00793     if ( elementChanged )
00794     {
00795       int idx = lbTools->index( lbTools->selectedItem() );
00796       lbTools->removeItem( idx );
00797       lbTools->insertItem( new ToolItem( 0, t->icon.isEmpty() ? blankIcon() : SmallIcon( t->icon ), t ), idx );
00798     }
00799 
00800     slotChanged();
00801     m_changed = true;
00802   }
00803 
00804   config->setGroup( "Editor" );
00805   config->writeEntry( "Size", editor.size() );
00806   config->sync();
00807 }
00808 
00809 void KateExternalToolsConfigWidget::slotInsertSeparator()
00810 {
00811   lbTools->insertItem( "---", lbTools->currentItem()+1 );
00812   slotChanged();
00813   m_changed = true;
00814 }
00815 
00816 void KateExternalToolsConfigWidget::slotMoveUp()
00817 {
00818   // move the current item in the listbox upwards if possible
00819   TQListBoxItem *item = lbTools->selectedItem();
00820   if ( ! item ) return;
00821 
00822   int idx = lbTools->index( item );
00823 
00824   if ( idx < 1 ) return;
00825 
00826   if ( dynamic_cast<ToolItem*>(item) )
00827   {
00828     KateExternalTool *tool = ((ToolItem*)item)->tool;
00829     lbTools->removeItem( idx );
00830     lbTools->insertItem( new ToolItem( 0, tool->icon.isEmpty() ? blankIcon() : SmallIcon( tool->icon ), tool ), idx-1 );
00831   }
00832   else // a separator!
00833   {
00834     lbTools->removeItem( idx );
00835     lbTools->insertItem( new TQListBoxText( 0, "---" ), idx-1 );
00836   }
00837 
00838   lbTools->setCurrentItem( idx - 1 );
00839   slotSelectionChanged();
00840   slotChanged();
00841   m_changed = true;
00842 }
00843 
00844 void KateExternalToolsConfigWidget::slotMoveDown()
00845 {
00846   // move the current item in the listbox downwards if possible
00847   TQListBoxItem *item = lbTools->selectedItem();
00848   if ( ! item ) return;
00849 
00850   uint idx = lbTools->index( item );
00851 
00852   if ( idx > lbTools->count()-1 ) return;
00853 
00854   if ( dynamic_cast<ToolItem*>(item) )
00855   {
00856     KateExternalTool *tool = ((ToolItem*)item)->tool;
00857     lbTools->removeItem( idx );
00858     lbTools->insertItem( new ToolItem( 0, tool->icon.isEmpty() ? blankIcon() : SmallIcon( tool->icon ), tool ), idx+1 );
00859   }
00860   else // a separator!
00861   {
00862     lbTools->removeItem( idx );
00863     lbTools->insertItem( new TQListBoxText( 0, "---" ), idx+1 );
00864   }
00865 
00866   lbTools->setCurrentItem( idx+1 );
00867   slotSelectionChanged();
00868   slotChanged();
00869   m_changed = true;
00870 }
00871 //END KateExternalToolsConfigWidget
00872 // kate: space-indent on; indent-width 2; replace-tabs on;

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