00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
00069
00070
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
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
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
00167 ci->setCursorPositionReal( line + numlines - 1, numlines > 1 ? len : col + len );
00168
00169
00170 _file =
KURL ();
00171 _tmpfile.truncate( 0 );
00172 v = 0;
00173 ei = 0;
00174 ci = 0;
00175 }
00176
00177
00178