19 #include "insertfileplugin.h"
20 #include "insertfileplugin.moc"
22 #include <tdetexteditor/document.h>
23 #include <tdetexteditor/viewcursorinterface.h>
24 #include <tdetexteditor/editinterface.h>
27 #include <tdeio/job.h>
28 #include <tdeaction.h>
29 #include <tdefiledialog.h>
30 #include <kgenericfactory.h>
32 #include <tdemessagebox.h>
33 #include <kpushbutton.h>
34 #include <tdetempfile.h>
38 #include <tqtextstream.h>
44 InsertFilePlugin::InsertFilePlugin( TQObject *parent, const
char* name, const TQStringList& )
45 : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
49 InsertFilePlugin::~InsertFilePlugin()
53 void InsertFilePlugin::addView(KTextEditor::View *view)
55 InsertFilePluginView *nview =
new InsertFilePluginView (view,
"Insert File Plugin");
56 m_views.append (nview);
59 void InsertFilePlugin::removeView(KTextEditor::View *view)
61 for (uint z=0; z < m_views.count(); z++)
62 if (m_views.at(z)->parentClient() == view)
64 InsertFilePluginView *nview = m_views.at(z);
65 m_views.remove (nview);
72 InsertFilePluginView::InsertFilePluginView( KTextEditor::View *view,
const char *name )
73 : TQObject( view,
name ),
76 view->insertChildClient(
this );
79 (void)
new TDEAction( i18n(
"Insert File..."), 0,
this, TQ_SLOT(slotInsertFile()), actionCollection(),
"tools_insert_file" );
80 setXMLFile(
"tdetexteditor_insertfileui.rc" );
83 void InsertFilePluginView::slotInsertFile()
85 KFileDialog dlg(
"::insertfile",
"", (TQWidget*)parent(),
"filedialog",
true);
86 dlg.setOperationMode( KFileDialog::Opening );
88 dlg.setCaption(i18n(
"Choose File to Insert"));
89 dlg.okButton()->setText(i18n(
"&Insert"));
90 dlg.setMode( KFile::File );
93 _file = dlg.selectedURL().url();
94 if ( _file.isEmpty() )
return;
96 if ( _file.isLocalFile() ) {
97 _tmpfile = _file.path();
102 _tmpfile = tempFile.name();
106 _job = TDEIO::file_copy( _file, destURL, 0600,
true,
false,
true );
107 connect( _job, TQ_SIGNAL( result( TDEIO::Job * ) ),
this, TQ_SLOT( slotFinished ( TDEIO::Job * ) ) );
111 void InsertFilePluginView::slotFinished( TDEIO::Job *job )
113 assert( job == _job );
116 KMessageBox::error( (TQWidget*)parent(), i18n(
"Failed to load file:\n\n") + job->errorString(), i18n(
"Insert File Error") );
121 void InsertFilePluginView::insertFile()
124 if ( _tmpfile.isEmpty() )
128 fi.setFile( _tmpfile );
129 if (!fi.exists() || !fi.isReadable())
130 error = i18n(
"<p>The file <strong>%1</strong> does not exist or is not readable, aborting.").arg(_file.fileName());
132 TQFile f( _tmpfile );
133 if ( !f.open(IO_ReadOnly) )
134 error = i18n(
"<p>Unable to open file <strong>%1</strong>, aborting.").arg(_file.fileName());
136 if ( ! error.isEmpty() ) {
142 TQTextStream stream(&f);
146 while (!stream.eof()) {
149 tmp = stream.readLine();
157 error = i18n(
"<p>File <strong>%1</strong> had no contents.").arg(_file.fileName());
158 if ( ! error.isEmpty() ) {
164 KTextEditor::EditInterface *ei;
165 KTextEditor::ViewCursorInterface *ci;
166 KTextEditor::View *v = (KTextEditor::View*)parent();
167 ei = KTextEditor::editInterface( v->document() );
168 ci = KTextEditor::viewCursorInterface( v );
170 ci->cursorPositionReal( &line, &col );
171 ei->insertText( line, col, str );
174 ci->setCursorPositionReal( line + numlines - 1, numlines > 1 ? len : col + len );
178 _tmpfile.truncate( 0 );
static void error(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
void setPath(const TQString &path)
TQString name(StdAccel id)