20 #include "katemwmodonhddialog.h"
21 #include "katemwmodonhddialog.moc"
23 #include "katedocmanager.h"
25 #include <kate/document.h>
27 #include <kiconloader.h>
28 #include <tdelistview.h>
29 #include <tdelocale.h>
30 #include <tdemessagebox.h>
33 #include <tdetempfile.h>
34 #include <kpushbutton.h>
37 #include <tqlistview.h>
39 #include <tqpushbutton.h>
40 #include <tqwhatsthis.h>
43 class KateDocItem :
public TQCheckListItem
46 KateDocItem( Kate::Document *doc,
const TQString &status, TDEListView *lv )
47 : TQCheckListItem( lv, doc->url().pathOrURL(), CheckBox ),
51 if ( ! doc->isModified() )
56 Kate::Document *document;
60 KateMwModOnHdDialog::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") )
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.") );
77 TQVBox *w = makeVBoxMainWidget();
78 w->setSpacing( KDialog::spacingHint() );
80 TQHBox *lo1 =
new TQHBox( w );
83 TQLabel *icon =
new TQLabel( lo1 );
84 icon->setPixmap( DesktopIcon(
"messagebox_warning") );
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 );
92 lvDocuments =
new TDEListView( w );
93 lvDocuments->addColumn( i18n(
"Filename") );
94 lvDocuments->addColumn( i18n(
"Status on Disk") );
95 lvDocuments->setSelectionMode( TQListView::Single );
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 );
102 connect( lvDocuments, TQ_SIGNAL(selectionChanged()),
this, TQ_SLOT(slotSelectionChanged()) );
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 );
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()) );
116 slotSelectionChanged();
120 KateMwModOnHdDialog::~KateMwModOnHdDialog()
124 void KateMwModOnHdDialog::slotUser1()
126 handleSelected( Ignore );
129 void KateMwModOnHdDialog::slotUser2()
131 handleSelected( Overwrite );
134 void KateMwModOnHdDialog::slotUser3()
136 handleSelected( Reload );
139 void KateMwModOnHdDialog::handleSelected(
int action )
142 TQValueList<TQListViewItem *> itemsToDelete;
143 for ( TQListViewItemIterator it ( lvDocuments ); it.current(); ++it )
145 KateDocItem *item =
static_cast<KateDocItem *
>(it.current());
149 int reason = (int)KateDocManager::self()->documentInfo( item->document )->modifiedOnDiscReason;
152 Kate::DocumentExt *dext = documentExt( item->document );
153 if ( ! dext )
continue;
155 dext->setModifiedOnDisk( 0 );
159 succes = item->document->save();
162 KMessageBox::sorry(
this,
163 i18n(
"Could not save the document \n'%1'").
164 arg( item->document->url().pathOrURL() ) );
169 item->document->reloadFile();
177 itemsToDelete.append (item);
179 dext->setModifiedOnDisk( reason );
184 for (
unsigned int i=0; i < itemsToDelete.count(); ++i)
185 delete itemsToDelete[i];
188 if ( ! lvDocuments->childCount() )
192 void KateMwModOnHdDialog::slotSelectionChanged()
195 btnDiff->setEnabled( lvDocuments->currentItem() &&
196 KateDocManager::self()->documentInfo( ((KateDocItem*)lvDocuments->currentItem())->document )->modifiedOnDiscReason != 3 );
201 void KateMwModOnHdDialog::slotDiff()
206 if ( ! lvDocuments->currentItem() )
209 Kate::Document *doc = ((KateDocItem*)lvDocuments->currentItem())->document;
212 if ( KateDocManager::self()->documentInfo( doc )->modifiedOnDiscReason == 3 )
216 KProcIO *p =
new KProcIO();
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(KProcIO*)),
this, TQ_SLOT(slotPRead(KProcIO*)) );
222 setCursor( WaitCursor );
224 p->start( TDEProcess::NotifyOnExit,
true );
226 uint lastln = doc->numLines();
227 for ( uint l = 0; l < lastln; l++ )
228 p->writeStdin( doc->textLine( l ), l < lastln );
233 void KateMwModOnHdDialog::slotPRead( KProcIO *p)
237 m_tmpfile =
new KTempFile();
240 bool dataRead =
false;
241 while ( p->readln( stmp,
false ) > -1 ) {
242 *m_tmpfile->textStream() << stmp << endl;
250 void KateMwModOnHdDialog::slotPDone( TDEProcess *p )
252 setCursor( ArrowCursor );
261 m_tmpfile =
new KTempFile();
265 if ( ! p->normalExit() )
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") );
276 KRun::runURL( m_tmpfile->name(),
"text/x-diff",
true );