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