KDevelop API Documentation

document.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2001 Harald Fernengel <harry@kdevelop.org>
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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <stdlib.h>
00020 
00021 #include <qapplication.h>
00022 #include <qprocess.h>
00023 #include <qtimer.h>
00024 #include <qvariant.h>
00025 
00026 #include <kdebug.h>
00027 #include <kwin.h>
00028 #include <kwinmodule.h>
00029 #include <kprocess.h>
00030 
00031 #include "document.h"
00032 #include "view.h"
00033 #include "kneditfactory.h"
00034 
00035 namespace KNEdit
00036 {
00037 
00038 // NEdit Highlightmodes are hard-coded, so we hard-code them here, too ;)
00039 // Took from NEdit 5.2
00040 static const char* hlModes[] = { "Plain", "Ada", "Awk", "C++", "C", "CSS", "Csh",
00041         "Fortran", "Java", "JavaScript", "LaTeX", "Lex", "Makefile",
00042         "Matlab", "NEdit Macro", "Pascal", "Perl", "PostScript",
00043         "Python", "Regex", "SGML HTML", "SQL", "Sh Ksh Bash", "Tcl",
00044         "VHDL", "Verilog", "XML", "X Resources", "Yacc", 0 };
00045 static const uint hlMCount = 29;
00046 
00047 Document::Document(bool bReadOnly, bool bSingleView, QWidget *parentWidget, const char *widgetName,
00048                    QObject *parent, const char *name) : KTextEditor::Document (parent, name)
00049 {
00050   m_isReady = false;
00051   activeView = 0;
00052   m_wid = 0;
00053   m_singleView = bSingleView;
00054   m_readOnly = bReadOnly;
00055   m_kwm = new KWinModule( this );
00056   m_timer = new QTimer( this, "NEdit Timer" );
00057   connect( m_timer, SIGNAL(timeout()), this, SLOT(checkForNEditServer()) );
00058 
00059   setInstance( KNEditFactory::instance() );
00060 
00061   // unique NEdit server name
00063   m_serverName = QString::fromLatin1( "ktexteditor_nedit_" + QString::number( documentNumber() ) );
00064 
00065   // start one NEdit server per document
00066   proc = new QProcess( QString::fromLatin1( "nedit" ), this, "nedit process" );
00067   proc->addArgument( "-server" );
00068   proc->addArgument( "-svrname" );
00069   proc->addArgument( m_serverName );
00070   connect(proc, SIGNAL(processExited()), this, SLOT(processExited()));
00071   connect(m_kwm, SIGNAL(windowAdded(WId)), this, SLOT(launchFinished(WId)) );
00072   if ( !proc->launch( QByteArray() ) ) {
00073     kdDebug() << "cannot start NEdit server" << endl;
00074     delete proc;
00075     proc = 0;
00076   }
00077 
00078   if ( bSingleView ) {
00079     KTextEditor::View *v = createView( parentWidget, widgetName );
00080     v->show();
00081     setWidget( v );
00082   }
00083 }
00084 
00085 Document::~Document()
00086 {
00087   kdDebug() << "Document::~Document" << endl;
00088 }
00089 
00090 KTextEditor::View *Document::createView( QWidget *parent, const char *name )
00091 {
00092   return new View( this, parent, name );
00093 }
00094 
00095 bool Document::openFile()
00096 {
00097   invokeNC( QString::null, true );
00098   return true;
00099 }
00100 
00101 bool Document::saveFile()
00102 {
00103   invokeNC("-do \"save()\"", true);
00104   return true;
00105 }
00106 
00107 void Document::addView(KTextEditor::View *view)
00108 {
00109   _views.append( view );
00110   activeView = view;
00111 }
00112 
00113 void Document::removeView(KTextEditor::View *view)
00114 {
00115   if (activeView == view)
00116     activeView = 0;
00117 
00118   _views.removeRef( view  );
00119 }
00120 
00121 void Document::invokeNC( const QString& command, bool appendFilename )
00122 {
00123   QString cmd = command;
00124   if ( appendFilename )
00125 #if (KDE_VERSION > 305)
00126     cmd += " " + KProcess::quote(m_file);
00127 #else
00128     cmd += " " + KShellProcess::quote(m_file);
00129 #endif
00130 
00131   if ( !m_isReady ) {
00132     // if NEdit server is not ready yet we store the commands for later
00133     m_commands += cmd;
00134   } else {
00135 #if (KDE_VERSION > 305)
00136     cmd = "nc -noask -svrname " + KProcess::quote(serverName()) + " " + cmd;
00137 #else
00138     cmd = "nc -noask -svrname " + KShellProcess::quote(serverName()) + " " + cmd;
00139 #endif
00140     system( cmd );
00141   }
00142 }
00143 
00144 // processes all commands in the queue
00145 void Document::processCommands()
00146 {
00147   while ( !m_commands.isEmpty() ) {
00148     invokeNC( m_commands.first(), false );
00149     m_commands.pop_front();
00150   }
00151 }
00152 
00153 // call this function when the NEdit Server is up and running
00154 // and ready to receive commands
00155 void Document::serverReady( WId wid )
00156 {
00157   if ( m_isReady )
00158     return;
00159 
00160   disconnect( m_kwm, SIGNAL(windowAdded(WId)), this, SLOT(launchFinished(WId)) );
00161 
00162   m_wid = wid;
00163   m_isReady = true;
00164 
00165   if ( activeView ) {
00166     View* aView = (View*)activeView->qt_cast( "KNEdit::View" );
00167     if ( aView ) {
00168         aView->embedNEdit( wid );
00169     }
00170   }
00171 
00172   processCommands();
00173 }
00174 
00175 
00176 void Document::launchFinished( WId wid )
00177 {
00178   if ( m_isReady ) {
00179     return;
00180   }
00181 
00182   KWin::Info inf = KWin::info( wid );
00183   kdDebug() << "launchFinished: " << wid << " " << inf.pid << " " << inf.name << " || " << inf.visibleName << endl;
00184 
00185   if ( inf.name.startsWith( "-" + m_serverName + "-" ) ) {
00186     // yepp - this is the Process we wanted...
00187     serverReady( wid );
00188   } else {
00189     // NEdit server is launched, but not ready to receive commands yet
00190     // so we need a stupid timer to check when it is ready...
00191     m_widCache += wid;
00192     if ( !m_timer->isActive() ) {
00193       m_timer->start( 200, false );
00194     }
00195   }
00196 }
00197 
00198 // checks whether our NEdit server is ready to process commands...
00199 void Document::checkForNEditServer()
00200 {
00201   QValueList<WId>::iterator it;
00202   for ( it = m_widCache.begin(); it != m_widCache.end(); ++it ) {
00203     KWin::Info inf = KWin::info( *it );
00204     if ( inf.name.startsWith( "-" + m_serverName + "-" ) ) {
00205       // yepp - this is the Process we wanted...
00206       m_timer->stop();
00207       disconnect( m_timer, SIGNAL(timeout()), this, SLOT(checkForNEditServer()) );
00208 
00209       serverReady( *it );
00210       m_widCache.clear();
00211       return;
00212     }
00213   }
00214 }
00215 
00216 
00217 void Document::processExited()
00218 {
00219   // NEdit has been closed by user, so the document gets inactive
00220   m_isReady = false;
00221   delete proc;
00222   proc = 0;
00223 
00225 
00226   kdDebug() << "Process Exited" << endl;
00227 
00228   // life is useless without an NEdit server...
00229   deleteLater();
00230 }
00231 
00232 uint Document::hlMode ()
00233 {
00234   return 0; 
00235 }
00236 
00237 bool Document::setHlMode (uint mode)
00238 {
00239   if ( ( mode < hlMCount ) ) {
00240     invokeNC("-do \"set_language_mode(\\\"" + QString::fromLatin1(hlModes[mode]) + "\\\")\"", true);
00241   }
00242   return false;
00243 }
00244 
00245 uint Document::hlModeCount ()
00246 {
00247   return hlMCount;
00248 }
00249 
00250 QString Document::hlModeName (uint mode)
00251 {
00252   if ( mode < hlMCount )
00253     return QString::fromLatin1( hlModes[mode] );
00254   return QString::null;
00255 }
00256 
00257 QString Document::hlModeSectionName (uint mode)
00258 {
00259   return QString::null;
00260 }
00261 
00262 };
00263 
00264 #include "document.moc"
00265 
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003