• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

insertfileplugin.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002 Anders Lund <anders@alweb.dk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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 //BEGIN InsertFilePlugin
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     // Loop written for the unlikely case of a view being added more than once
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 //END InsertFilePlugin
00076 
00077 //BEGIN InsertFilePluginView
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   // now grab file contents
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   // insert !!
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   // move the cursor
00184   v->setCursorPosition ( KTextEditor::Cursor (line + numlines - 1, numlines > 1 ? len : col + len)  );
00185 
00186   // clean up
00187   _file = KUrl ();
00188   _tmpfile.truncate( 0 );
00189 }
00190 
00191 //END InsertFilePluginView
00192 

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal