kate Library API Documentation

insertfileplugin.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002 Anders Lund <anders@alweb.dk>
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 "insertfileplugin.h"
00020 #include "insertfileplugin.moc"
00021 
00022 #include <ktexteditor/document.h>
00023 #include <ktexteditor/viewcursorinterface.h>
00024 #include <ktexteditor/editinterface.h>
00025 
00026 #include <assert.h>
00027 #include <kio/job.h>
00028 #include <kaction.h>
00029 #include <kfiledialog.h>
00030 #include <kgenericfactory.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <ktempfile.h>
00034 #include <kurl.h>
00035 
00036 #include <qfile.h>
00037 #include <qtextstream.h>
00038 
00039 K_EXPORT_COMPONENT_FACTORY( ktexteditor_insertfile, KGenericFactory<InsertFilePlugin>( "ktexteditor_insertfile" ) )
00040 
00041 
00042 //BEGIN InsertFilePlugin
00043 InsertFilePlugin::InsertFilePlugin( QObject *parent, const char* name, const QStringList& )
00044     : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
00045 {
00046 }
00047 
00048 InsertFilePlugin::~InsertFilePlugin()
00049 {
00050 }
00051 
00052 void InsertFilePlugin::addView(KTextEditor::View *view)
00053 {
00054   InsertFilePluginView *nview = new InsertFilePluginView (view, "Insert File Plugin");
00055   m_views.append (nview);
00056 }
00057 
00058 void InsertFilePlugin::removeView(KTextEditor::View *view)
00059 {
00060   for (uint z=0; z < m_views.count(); z++)
00061     if (m_views.at(z)->parentClient() == view)
00062     {
00063        InsertFilePluginView *nview = m_views.at(z);
00064        m_views.remove (nview);
00065        delete nview;
00066     }
00067 }
00068 //END InsertFilePlugin
00069 
00070 //BEGIN InsertFilePluginView
00071 InsertFilePluginView::InsertFilePluginView( KTextEditor::View *view, const char *name )
00072   : QObject( view, name ),
00073     KXMLGUIClient( view )
00074 {
00075   view->insertChildClient( this );
00076   setInstance( KGenericFactory<InsertFilePlugin>::instance() );
00077   _job = 0;
00078   (void) new KAction( i18n("Insert File..."), 0, this, SLOT(slotInsertFile()), actionCollection(), "tools_insert_file" );
00079   setXMLFile( "ktexteditor_insertfileui.rc" );
00080 }
00081 
00082 void InsertFilePluginView::slotInsertFile()
00083 {
00084   _file = KFileDialog::getOpenURL( "::insertfile", "",
00085                                              (QWidget*)parent(),
00086                                              i18n("Choose File to Insert") ).url();
00087   if ( _file.isEmpty() ) return;
00088 
00089   if ( _file.isLocalFile() ) {
00090     _tmpfile = _file.path();
00091     insertFile();
00092   }
00093   else {
00094     KTempFile tempFile( QString::null );
00095     _tmpfile = tempFile.name();
00096 
00097     KURL destURL;
00098     destURL.setPath( _tmpfile );
00099     _job = KIO::file_copy( _file, destURL, 0600, true, false, true );
00100     connect( _job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotFinished ( KIO::Job * ) ) );
00101   }
00102 }
00103 
00104 void InsertFilePluginView::slotFinished( KIO::Job *job )
00105 {
00106   assert( job == _job );
00107   _job = 0;
00108   if ( job->error() )
00109     KMessageBox::error( (QWidget*)parent(), i18n("Failed to load file:\n\n") + job->errorString(), i18n("Insert File Error") );
00110   else
00111     insertFile();
00112 }
00113 
00114 void InsertFilePluginView::insertFile()
00115 {
00116   QString error;
00117   if ( _tmpfile.isEmpty() )
00118     return;
00119 
00120   QFileInfo fi;
00121   fi.setFile( _tmpfile );
00122   if (!fi.exists() || !fi.isReadable())
00123     error = i18n("<p>The file <strong>%1</strong> does not exist or is not readable, aborting.").arg(_file.fileName());
00124 
00125   QFile f( _tmpfile );
00126   if ( !f.open(IO_ReadOnly) )
00127     error = i18n("<p>Unable to open file <strong>%1</strong>, aborting.").arg(_file.fileName());
00128 
00129   if ( ! error.isEmpty() ) {
00130     KMessageBox::sorry( (QWidget*)parent(), error, i18n("Insert file error") );
00131     return;
00132   }
00133 
00134   // now grab file contents
00135   QTextStream stream(&f);
00136   QString str, tmp;
00137   uint numlines = 0;
00138   uint len = 0;
00139   while (!stream.eof()) {
00140     if ( numlines )
00141       str += "\n";
00142     tmp = stream.readLine();
00143     str += tmp;
00144     len = tmp.length();
00145     numlines++;
00146   }
00147   f.close();
00148 
00149   if ( str.isEmpty() )
00150     error = i18n("<p>File <strong>%1</strong> had no contents.").arg(_file.fileName());
00151   if ( ! error.isEmpty() ) {
00152     KMessageBox::sorry( (QWidget*)parent(), error, i18n("Insert file error") );
00153     return;
00154   }
00155 
00156   // insert !!
00157   KTextEditor::EditInterface *ei;
00158   KTextEditor::ViewCursorInterface *ci;
00159   KTextEditor::View *v = (KTextEditor::View*)parent();
00160   ei = KTextEditor::editInterface( v->document() );
00161   ci = KTextEditor::viewCursorInterface( v );
00162   uint line, col;
00163   ci->cursorPositionReal( &line, &col );
00164   ei->insertText( line, col, str );
00165 
00166   // move the cursor
00167   ci->setCursorPositionReal( line + numlines - 1, numlines > 1 ? len : col + len  );
00168 
00169   // clean up
00170   _file = KURL ();
00171   _tmpfile.truncate( 0 );
00172   v = 0;
00173   ei = 0;
00174   ci = 0;
00175 }
00176 
00177 //END InsertFilePluginView
00178 
KDE Logo
This file is part of the documentation for kate Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 14 09:18:57 2006 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003