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

kate

  • kate
  • app
katemwmodonhddialog.cpp
1/*
2 This library is free software; you can redistribute it and/or
3 modify it under the terms of the GNU Library General Public
4 License version 2 as published by the Free Software Foundation.
5
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
11 You should have received a copy of the GNU Library General Public License
12 along with this library; see the file COPYING.LIB. If not, write to
13 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
14 Boston, MA 02110-1301, USA.
15
16 ---
17 Copyright (C) 2004, Anders Lund <anders@alweb.dk>
18*/
19
20#include "katemwmodonhddialog.h"
21#include "katemwmodonhddialog.moc"
22
23#include "katedocmanager.h"
24
25#include <kate/document.h>
26
27#include <kiconloader.h>
28#include <tdelistview.h>
29#include <tdelocale.h>
30#include <tdemessagebox.h>
31#include <tdeprocio.h>
32#include <krun.h>
33#include <tdetempfile.h>
34#include <kpushbutton.h>
35
36#include <tqlabel.h>
37#include <tqlistview.h>
38#include <tqlayout.h>
39#include <tqpushbutton.h>
40#include <tqwhatsthis.h>
41#include <tqvbox.h>
42
43class KateDocItem : public TQCheckListItem
44{
45 public:
46 KateDocItem( Kate::Document *doc, const TQString &status, TDEListView *lv )
47 : TQCheckListItem( lv, doc->url().pathOrURL(), CheckBox ),
48 document( doc )
49 {
50 setText( 1, status );
51 if ( ! doc->isModified() )
52 setOn( On );
53 }
54 ~KateDocItem() {};
55
56 Kate::Document *document;
57};
58
59
60KateMwModOnHdDialog::KateMwModOnHdDialog( DocVector docs, TQWidget *parent, const char *name )
61 : KDialogBase( parent, name, true, i18n("Documents Modified on Disk"),
62 User1|User2|User3, User3, false,
63 KGuiItem (i18n("&Ignore"), "window-close"),
64 KGuiItem (i18n("&Overwrite"), "document-save"),
65 KGuiItem (i18n("&Reload"), "reload") )
66{
67 setButtonWhatsThis( User1, i18n(
68 "Removes the modified flag from the selected documents and closes the "
69 "dialog if there are no more unhandled documents.") );
70 setButtonWhatsThis( User2, i18n(
71 "Overwrite selected documents, discarding the disk changes and closes the "
72 "dialog if there are no more unhandled documents.") );
73 setButtonWhatsThis( User3, i18n(
74 "Reloads the selected documents from disk and closes the dialog if there "
75 "are no more unhandled documents.") );
76
77 TQVBox *w = makeVBoxMainWidget();
78 w->setSpacing( KDialog::spacingHint() );
79
80 TQHBox *lo1 = new TQHBox( w );
81
82 // dialog text
83 TQLabel *icon = new TQLabel( lo1 );
84 icon->setPixmap( DesktopIcon("messagebox_warning") );
85
86 TQLabel *t = new TQLabel( i18n(
87 "<qt>The documents listed below has changed on disk.<p>Select one "
88 "or more at the time and press an action button until the list is empty.</qt>"), lo1 );
89 lo1->setStretchFactor( t, 1000 );
90
91 // document list
92 lvDocuments = new TDEListView( w );
93 lvDocuments->addColumn( i18n("Filename") );
94 lvDocuments->addColumn( i18n("Status on Disk") );
95 lvDocuments->setSelectionMode( TQListView::Single );
96
97 TQStringList l;
98 l << "" << i18n("Modified") << i18n("Created") << i18n("Deleted");
99 for ( uint i=0; i < docs.size(); i++ )
100 new KateDocItem( docs[i], l[ (uint)KateDocManager::self()->documentInfo( docs[i] )->modifiedOnDiscReason ], lvDocuments );
101
102 connect( lvDocuments, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(slotSelectionChanged()) );
103
104 // diff button
105 TQHBox *lo2 = new TQHBox ( w );
106 TQWidget *d = new TQWidget (lo2);
107 lo2->setStretchFactor (d, 2);
108 btnDiff = new KPushButton( KGuiItem (i18n("&View Difference"), "edit"), lo2 );
109
110 TQWhatsThis::add( btnDiff, i18n(
111 "Calculates the difference between the the editor contents and the disk "
112 "file for the selected document, and shows the difference with the "
113 "default application. Requires diff(1).") );
114 connect( btnDiff, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotDiff()) );
115
116 slotSelectionChanged();
117 m_tmpfile = 0;
118}
119
120KateMwModOnHdDialog::~KateMwModOnHdDialog()
121{
122}
123
124void KateMwModOnHdDialog::slotUser1()
125{
126 handleSelected( Ignore );
127}
128
129void KateMwModOnHdDialog::slotUser2()
130{
131 handleSelected( Overwrite );
132}
133
134void KateMwModOnHdDialog::slotUser3()
135{
136 handleSelected( Reload );
137}
138
139void KateMwModOnHdDialog::handleSelected( int action )
140{
141 // collect all items we can remove
142 TQValueList<TQListViewItem *> itemsToDelete;
143 for ( TQListViewItemIterator it ( lvDocuments ); it.current(); ++it )
144 {
145 KateDocItem *item = static_cast<KateDocItem *>(it.current());
146
147 if ( item->isOn() )
148 {
149 int reason = (int)KateDocManager::self()->documentInfo( item->document )->modifiedOnDiscReason;
150 bool succes = true;
151
152 Kate::DocumentExt *dext = documentExt( item->document );
153 if ( ! dext ) continue;
154
155 dext->setModifiedOnDisk( 0 );
156 switch ( action )
157 {
158 case Overwrite:
159 succes = item->document->save();
160 if ( ! succes )
161 {
162 KMessageBox::sorry( this,
163 i18n("Could not save the document \n'%1'").
164 arg( item->document->url().pathOrURL() ) );
165 }
166 break;
167
168 case Reload:
169 item->document->reloadFile();
170 break;
171
172 default:
173 break;
174 }
175
176 if ( succes )
177 itemsToDelete.append (item);
178 else
179 dext->setModifiedOnDisk( reason );
180 }
181 }
182
183 // remove the marked items
184 for (unsigned int i=0; i < itemsToDelete.count(); ++i)
185 delete itemsToDelete[i];
186
187 // any documents left unhandled?
188 if ( ! lvDocuments->childCount() )
189 done( Ok );
190}
191
192void KateMwModOnHdDialog::slotSelectionChanged()
193{
194 // set the diff button enabled
195 btnDiff->setEnabled( lvDocuments->currentItem() &&
196 KateDocManager::self()->documentInfo( ((KateDocItem*)lvDocuments->currentItem())->document )->modifiedOnDiscReason != 3 );
197}
198
199// ### the code below is slightly modified from tdelibs/kate/part/katedialogs,
200// class KateModOnHdPrompt.
201void KateMwModOnHdDialog::slotDiff()
202{
203 if ( m_tmpfile ) // we are allready somewhere in this process.
204 return;
205
206 if ( ! lvDocuments->currentItem() )
207 return;
208
209 Kate::Document *doc = ((KateDocItem*)lvDocuments->currentItem())->document;
210
211 // don't try o diff a deleted file
212 if ( KateDocManager::self()->documentInfo( doc )->modifiedOnDiscReason == 3 )
213 return;
214
215 // Start a TDEProcess that creates a diff
216 TDEProcIO *p = new TDEProcIO();
217 p->setComm( TDEProcess::All );
218 *p << "diff" << "-u" << "-" << doc->url().path();
219 connect( p, TQ_SIGNAL(processExited(TDEProcess*)), this, TQ_SLOT(slotPDone(TDEProcess*)) );
220 connect( p, TQ_SIGNAL(readReady(TDEProcIO*)), this, TQ_SLOT(slotPRead(TDEProcIO*)) );
221
222 setCursor( WaitCursor );
223
224 p->start( TDEProcess::NotifyOnExit, true );
225
226 uint lastln = doc->numLines();
227 for ( uint l = 0; l < lastln; l++ )
228 p->writeStdin( doc->textLine( l ), l < lastln );
229
230 p->closeWhenDone();
231}
232
233void KateMwModOnHdDialog::slotPRead( TDEProcIO *p)
234{
235 // create a file for the diff if we haven't one allready
236 if ( ! m_tmpfile )
237 m_tmpfile = new KTempFile();
238 // put all the data we have in it
239 TQString stmp;
240 bool dataRead = false;
241 while ( p->readln( stmp, false ) > -1 ) {
242 *m_tmpfile->textStream() << stmp << endl;
243 dataRead = true;
244 }
245
246 if (dataRead)
247 p->ackRead();
248}
249
250void KateMwModOnHdDialog::slotPDone( TDEProcess *p )
251{
252 setCursor( ArrowCursor );
253 if( ! m_tmpfile )
254 {
255 // dominik: there were only whitespace changes, so that the diff returned by
256 // diff(1) has 0 bytes. So slotPRead() is never called, as there is
257 // no data, so that m_tmpfile was never created and thus is NULL.
258 // NOTE: would be nice, if we could produce a fake-diff, so that kompare
259 // tells us "The files are identical". Right now, we get an ugly
260 // "Could not parse diff output".
261 m_tmpfile = new KTempFile();
262 }
263 m_tmpfile->close();
264
265 if ( ! p->normalExit() /*|| p->exitStatus()*/ )
266 {
267 KMessageBox::sorry( this,
268 i18n("The diff command failed. Please make sure that "
269 "diff(1) is installed and in your PATH."),
270 i18n("Error Creating Diff") );
271 delete m_tmpfile;
272 m_tmpfile = 0;
273 return;
274 }
275
276 KRun::runURL( m_tmpfile->name(), "text/x-diff", true );
277 delete m_tmpfile;
278 m_tmpfile = 0;
279}

kate

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

kate

Skip menu "kate"
  • kate
  • libkonq
  • twin
  •   lib
Generated for kate by doxygen 1.9.4
This website is maintained by Timothy Pearson.