Kate
insertfileplugin.cpp
Go to the documentation of this file.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
00024 #include <assert.h>
00025 #include <kio/job.h>
00026 #include <kaction.h>
00027 #include <kactioncollection.h>
00028 #include <kfiledialog.h>
00029 #include <kpluginfactory.h>
00030 #include <kpluginloader.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <kpushbutton.h>
00034 #include <kaboutdata.h>
00035 #include <ktemporaryfile.h>
00036 #include <kurl.h>
00037
00038 #include <QtCore/QFile>
00039 #include <QtCore/QTextStream>
00040
00041 K_PLUGIN_FACTORY( InsertFilePluginFactory, registerPlugin<InsertFilePlugin>(); )
00042 K_EXPORT_PLUGIN( InsertFilePluginFactory( KAboutData( "ktexteditor_insertfile", "ktexteditor_plugins", ki18n("Insert File"), "0.1", ki18n("Insert File"), KAboutData::License_LGPL_V2 ) ) )
00043
00044
00045 InsertFilePlugin::InsertFilePlugin( QObject *parent, const QVariantList& )
00046 : KTextEditor::Plugin ( parent )
00047 {
00048 }
00049
00050 InsertFilePlugin::~InsertFilePlugin()
00051 {
00052 }
00053
00054 void InsertFilePlugin::addView(KTextEditor::View *view)
00055 {
00056 InsertFilePluginView *nview = new InsertFilePluginView (view, "Insert File Plugin");
00057 m_views.append (nview);
00058 }
00059
00060 void InsertFilePlugin::removeView(KTextEditor::View *view)
00061 {
00062 int z=0;
00063
00064 while (z < m_views.count())
00065 {
00066 InsertFilePluginView *nview = m_views.at(z);
00067 if (nview->parentClient() == view)
00068 {
00069 m_views.removeAll (nview);
00070 delete nview;
00071 }
00072 else
00073 ++z;
00074 }
00075 }
00076
00077
00078
00079 InsertFilePluginView::InsertFilePluginView( KTextEditor::View *view, const char *name )
00080 : QObject( view ),
00081 KXMLGUIClient( view )
00082 {
00083 setObjectName( name );
00084
00085 setComponentData( InsertFilePluginFactory::componentData() );
00086 _job = 0;
00087
00088 KAction *action = new KAction( i18n("Insert File..."), this );
00089 actionCollection()->addAction( "tools_insert_file", action );
00090 connect( action, SIGNAL( triggered( bool ) ), this, SLOT(slotInsertFile()) );
00091
00092 setXMLFile( "ktexteditor_insertfileui.rc" );
00093 }
00094
00095 void InsertFilePluginView::slotInsertFile()
00096 {
00097 KFileDialog dlg( KUrl( "kfiledialog:///insertfile?global" ), "", (QWidget*)parent());
00098 dlg.setOperationMode( KFileDialog::Opening );
00099
00100 dlg.setCaption(i18n("Choose File to Insert"));
00101 dlg.okButton()->setText(i18n("&Insert"));
00102 dlg.setMode( KFile::File );
00103 dlg.exec();
00104
00105 _file = dlg.selectedUrl().url();
00106 if ( _file.isEmpty() ) return;
00107
00108 if ( _file.isLocalFile() ) {
00109 _tmpfile = _file.toLocalFile();
00110 insertFile();
00111 }
00112 else {
00113 KTemporaryFile tempFile;
00114 tempFile.setAutoRemove(false);
00115 tempFile.open();
00116 _tmpfile = tempFile.fileName();
00117
00118 KUrl destURL;
00119 destURL.setPath( _tmpfile );
00120 _job = KIO::file_copy( _file, destURL, 0600, KIO::Overwrite );
00121 connect( _job, SIGNAL( result( KJob * ) ), this, SLOT( slotFinished ( KJob * ) ) );
00122 }
00123 }
00124
00125 void InsertFilePluginView::slotFinished( KJob *job )
00126 {
00127 assert( job == _job );
00128 _job = 0;
00129 if ( job->error() )
00130 KMessageBox::error( (QWidget*)parent(), i18n("Failed to load file:\n\n") + job->errorString(), i18n("Insert File Error") );
00131 else
00132 insertFile();
00133 }
00134
00135 void InsertFilePluginView::insertFile()
00136 {
00137 QString error;
00138 if ( _tmpfile.isEmpty() )
00139 return;
00140
00141 QFileInfo fi;
00142 fi.setFile( _tmpfile );
00143 if (!fi.exists() || !fi.isReadable())
00144 error = i18n("<p>The file <strong>%1</strong> does not exist or is not readable, aborting.</p>", _file.fileName());
00145
00146 QFile f( _tmpfile );
00147 if ( !f.open(QIODevice::ReadOnly) )
00148 error = i18n("<p>Unable to open file <strong>%1</strong>, aborting.</p>", _file.fileName());
00149
00150 if ( ! error.isEmpty() ) {
00151 KMessageBox::sorry( (QWidget*)parent(), error, i18n("Insert File Error") );
00152 return;
00153 }
00154
00155
00156 QTextStream stream(&f);
00157 QString str, tmp;
00158 uint numlines = 0;
00159 uint len = 0;
00160 while (!stream.atEnd()) {
00161 if ( numlines )
00162 str += '\n';
00163 tmp = stream.readLine();
00164 str += tmp;
00165 len = tmp.length();
00166 numlines++;
00167 }
00168 f.close();
00169
00170 if ( str.isEmpty() )
00171 error = i18n("<p>File <strong>%1</strong> had no contents.</p>", _file.fileName());
00172 if ( ! error.isEmpty() ) {
00173 KMessageBox::sorry( (QWidget*)parent(), error, i18n("Insert File Error") );
00174 return;
00175 }
00176
00177
00178 KTextEditor::View *v = (KTextEditor::View*)parent();
00179 int line, col;
00180 line = v->cursorPosition().line();
00181 col = v->cursorPosition().column();
00182 v->document()->insertText( v->cursorPosition(), str );
00183
00184
00185 v->setCursorPosition ( KTextEditor::Cursor (line + numlines - 1, numlines > 1 ? len : col + len) );
00186
00187
00188 _file = KUrl ();
00189 _tmpfile.truncate( 0 );
00190 }
00191
00192
00193