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