• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

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

kate

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

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.1
This website is maintained by Timothy Pearson.