TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

insertfileplugin.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2002 Anders Lund <anders@alweb.dk>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License version 2 as published by the Free Software Foundation.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; see the file COPYING.LIB. If not, write to
  12. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, USA.
  14. */
  15. #include "insertfileplugin.h"
  16. #include "insertfileplugin.moc"
  17. #include <tdetexteditor/document.h>
  18. #include <tdetexteditor/viewcursorinterface.h>
  19. #include <tdetexteditor/editinterface.h>
  20. #include <assert.h>
  21. #include <tdeio/job.h>
  22. #include <tdeaction.h>
  23. #include <tdefiledialog.h>
  24. #include <kgenericfactory.h>
  25. #include <tdelocale.h>
  26. #include <tdemessagebox.h>
  27. #include <kpushbutton.h>
  28. #include <tdetempfile.h>
  29. #include <kurl.h>
  30. #include <tqfile.h>
  31. #include <tqtextstream.h>
  32. K_EXPORT_COMPONENT_FACTORY( tdetexteditor_insertfile, KGenericFactory<InsertFilePlugin>( "tdetexteditor_insertfile" ) )
  33. //BEGIN InsertFilePlugin
  34. InsertFilePlugin::InsertFilePlugin( TQObject *parent, const char* name, const TQStringList& )
  35. : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
  36. {
  37. }
  38. InsertFilePlugin::~InsertFilePlugin()
  39. {
  40. }
  41. void InsertFilePlugin::addView(KTextEditor::View *view)
  42. {
  43. InsertFilePluginView *nview = new InsertFilePluginView (view, "Insert File Plugin");
  44. m_views.append (nview);
  45. }
  46. void InsertFilePlugin::removeView(KTextEditor::View *view)
  47. {
  48. for (uint z=0; z < m_views.count(); z++)
  49. if (m_views.at(z)->parentClient() == view)
  50. {
  51. InsertFilePluginView *nview = m_views.at(z);
  52. m_views.remove (nview);
  53. delete nview;
  54. }
  55. }
  56. //END InsertFilePlugin
  57. //BEGIN InsertFilePluginView
  58. InsertFilePluginView::InsertFilePluginView( KTextEditor::View *view, const char *name )
  59. : TQObject( view, name ),
  60. KXMLGUIClient( view )
  61. {
  62. view->insertChildClient( this );
  63. setInstance( KGenericFactory<InsertFilePlugin>::instance() );
  64. _job = 0;
  65. (void) new TDEAction( i18n("Insert File..."), 0, this, TQT_SLOT(slotInsertFile()), actionCollection(), "tools_insert_file" );
  66. setXMLFile( "tdetexteditor_insertfileui.rc" );
  67. }
  68. void InsertFilePluginView::slotInsertFile()
  69. {
  70. KFileDialog dlg("::insertfile", "", (TQWidget*)parent(), "filedialog", true);
  71. dlg.setOperationMode( KFileDialog::Opening );
  72. dlg.setCaption(i18n("Choose File to Insert"));
  73. dlg.okButton()->setText(i18n("&Insert"));
  74. dlg.setMode( KFile::File );
  75. dlg.exec();
  76. _file = dlg.selectedURL().url();
  77. if ( _file.isEmpty() ) return;
  78. if ( _file.isLocalFile() ) {
  79. _tmpfile = _file.path();
  80. insertFile();
  81. }
  82. else {
  83. KTempFile tempFile( TQString::null );
  84. _tmpfile = tempFile.name();
  85. KURL destURL;
  86. destURL.setPath( _tmpfile );
  87. _job = TDEIO::file_copy( _file, destURL, 0600, true, false, true );
  88. connect( _job, TQT_SIGNAL( result( TDEIO::Job * ) ), this, TQT_SLOT( slotFinished ( TDEIO::Job * ) ) );
  89. }
  90. }
  91. void InsertFilePluginView::slotFinished( TDEIO::Job *job )
  92. {
  93. assert( job == _job );
  94. _job = 0;
  95. if ( job->error() )
  96. KMessageBox::error( (TQWidget*)parent(), i18n("Failed to load file:\n\n") + job->errorString(), i18n("Insert File Error") );
  97. else
  98. insertFile();
  99. }
  100. void InsertFilePluginView::insertFile()
  101. {
  102. TQString error;
  103. if ( _tmpfile.isEmpty() )
  104. return;
  105. TQFileInfo fi;
  106. fi.setFile( _tmpfile );
  107. if (!fi.exists() || !fi.isReadable())
  108. error = i18n("<p>The file <strong>%1</strong> does not exist or is not readable, aborting.").arg(_file.fileName());
  109. TQFile f( _tmpfile );
  110. if ( !f.open(IO_ReadOnly) )
  111. error = i18n("<p>Unable to open file <strong>%1</strong>, aborting.").arg(_file.fileName());
  112. if ( ! error.isEmpty() ) {
  113. KMessageBox::sorry( (TQWidget*)parent(), error, i18n("Insert File Error") );
  114. return;
  115. }
  116. // now grab file contents
  117. TQTextStream stream(&f);
  118. TQString str, tmp;
  119. uint numlines = 0;
  120. uint len = 0;
  121. while (!stream.eof()) {
  122. if ( numlines )
  123. str += "\n";
  124. tmp = stream.readLine();
  125. str += tmp;
  126. len = tmp.length();
  127. numlines++;
  128. }
  129. f.close();
  130. if ( str.isEmpty() )
  131. error = i18n("<p>File <strong>%1</strong> had no contents.").arg(_file.fileName());
  132. if ( ! error.isEmpty() ) {
  133. KMessageBox::sorry( (TQWidget*)parent(), error, i18n("Insert File Error") );
  134. return;
  135. }
  136. // insert !!
  137. KTextEditor::EditInterface *ei;
  138. KTextEditor::ViewCursorInterface *ci;
  139. KTextEditor::View *v = (KTextEditor::View*)parent();
  140. ei = KTextEditor::editInterface( v->document() );
  141. ci = KTextEditor::viewCursorInterface( v );
  142. uint line, col;
  143. ci->cursorPositionReal( &line, &col );
  144. ei->insertText( line, col, str );
  145. // move the cursor
  146. ci->setCursorPositionReal( line + numlines - 1, numlines > 1 ? len : col + len );
  147. // clean up
  148. _file = KURL ();
  149. _tmpfile.truncate( 0 );
  150. v = 0;
  151. ei = 0;
  152. ci = 0;
  153. }
  154. //END InsertFilePluginView