kate Library API Documentation

katemwmodonhddialog.cpp

00001 /*
00002     This library is free software; you can redistribute it and/or
00003     modify it under the terms of the GNU Library General Public
00004     License version 2 as published by the Free Software Foundation.
00005 
00006     This library is distributed in the hope that it will be useful,
00007     but WITHOUT ANY WARRANTY; without even the implied warranty of
00008     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00009     Library General Public License for more details.
00010 
00011     You should have received a copy of the GNU Library General Public License
00012     along with this library; see the file COPYING.LIB.  If not, write to
00013     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00014     Boston, MA 02111-1307, USA.
00015 
00016     ---
00017     Copyright (C) 2004, Anders Lund <anders@alweb.dk>
00018 */
00019 
00020 #include "katemwmodonhddialog.h"
00021 #include "katemwmodonhddialog.moc"
00022 
00023 #include "katedocmanager.h"
00024 
00025 #include <kate/document.h>
00026 
00027 #include <kiconloader.h>
00028 #include <klistview.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031 #include <kprocio.h>
00032 #include <krun.h>
00033 #include <ktempfile.h>
00034 
00035 #include <qlabel.h>
00036 #include <qlistview.h>
00037 #include <qlayout.h>
00038 #include <qpushbutton.h>
00039 #include <qwhatsthis.h>
00040 
00041 class KateDocItem : public QCheckListItem
00042 {
00043   public:
00044     KateDocItem( Kate::Document *doc, const QString &status, KListView *lv )
00045   : QCheckListItem( lv, doc->url().prettyURL(), CheckBox ),
00046         document( doc )
00047     {
00048       setText( 1, status );
00049       if ( ! doc->isModified() )
00050         setOn( On );
00051     }
00052     ~KateDocItem() {};
00053 
00054     Kate::Document *document;
00055 };
00056 
00057 
00058 KateMwModOnHdDialog::KateMwModOnHdDialog( DocVector docs, QWidget *parent, const char *name )
00059   : KDialogBase( parent, name, true, i18n("Documents Modified on Disk"),
00060                  User1|User2|User3|Cancel, User3, false,
00061                  i18n("&Ignore"), i18n("&Overwrite"), i18n("&Reload") )
00062 {
00063   setButtonWhatsThis( User1, i18n(
00064       "Removes the modified flag from the selected documents and closes the "
00065       "dialog if there are no more unhandled documents.") );
00066   setButtonWhatsThis( User2, i18n(
00067       "Overwrite selected documents, discarding the disk changes and closes the "
00068       "dialog if there are no more unhandled documents.") );
00069   setButtonWhatsThis( User3, i18n(
00070       "Reloads the selected documents from disk and closes the dialog if there "
00071       "are no more unhandled documents.") );
00072   setButtonWhatsThis( Cancel, i18n("Do not handle the changes now. You will be "
00073       "prompted when individual documents are focused.") );
00074 
00075   QFrame *w = makeMainWidget();
00076   QVBoxLayout *lo = new QVBoxLayout( w );
00077   lo->setSpacing( KDialog::spacingHint() );
00078 
00079   QHBoxLayout *lo1 = new QHBoxLayout( lo );
00080 
00081   // dialog text
00082   QLabel *icon = new QLabel( w );
00083   icon->setPixmap( DesktopIcon("messagebox_warning") );
00084   lo1->addWidget( icon );
00085 
00086 
00087   QLabel *t = new QLabel( i18n(
00088       "<qt>The documents listed below has changed on disk.<p>Select one "
00089       "or more at the time and press an action button until the list is empty.</qt>"), w );
00090   lo1->addWidget( t );
00091   lo1->setStretchFactor( t, 1000 );
00092 
00093   // document list
00094   lvDocuments = new KListView( w );
00095   lvDocuments->addColumn( i18n("Filename") );
00096   lvDocuments->addColumn( i18n("Status on Disk") );
00097   lvDocuments->setSelectionMode( QListView::Single );
00098 
00099   QStringList l;
00100   l << "" << i18n("Modified") << i18n("Created") << i18n("Deleted");
00101   for ( uint i=0; i < docs.size(); i++ )
00102     new KateDocItem( docs[i], l[ (uint)KateDocManager::self()->documentInfo( docs[i] )->modifiedOnDiscReason ], lvDocuments );
00103   lo->addWidget( lvDocuments );
00104   connect( lvDocuments, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()) );
00105 
00106   // diff button
00107   QHBoxLayout *lo2 = new QHBoxLayout( lo );
00108   lo2->addStretch(10);
00109   btnDiff = new QPushButton( i18n("&View Difference"), w );
00110   lo2->addWidget( btnDiff );
00111   QWhatsThis::add( btnDiff, i18n(
00112       "Calculates the difference between the the editor contents and the disk "
00113       "file for the selected document, and shows the difference with the "
00114       "default application. Requires diff(1).") );
00115   connect( btnDiff, SIGNAL(clicked()), this, SLOT(slotDiff()) );
00116 
00117   slotSelectionChanged();
00118   m_tmpfile = 0;
00119 }
00120 
00121 void KateMwModOnHdDialog::slotUser1()
00122 {
00123   handleSelected( Ignore );
00124 }
00125 
00126 void KateMwModOnHdDialog::slotUser2()
00127 {
00128   handleSelected( Overwrite );
00129 }
00130 
00131 void KateMwModOnHdDialog::slotUser3()
00132 {
00133   handleSelected( Reload );
00134 }
00135 
00136 void KateMwModOnHdDialog::handleSelected( int action )
00137 {
00138   QListViewItemIterator it ( lvDocuments );
00139   while ( it.current() )
00140   {
00141     KateDocItem *item = (KateDocItem*)it.current();
00142     if ( item->isOn() )
00143     {
00144       int reason = (int)KateDocManager::self()->documentInfo( item->document )->modifiedOnDiscReason;
00145       bool succes = true;
00146       Kate::DocumentExt *dext = documentExt( item->document );
00147       if ( ! dext ) return;
00148 
00149       dext->setModifiedOnDisk( 0 );
00150       switch ( action )
00151       {
00152         case Overwrite:
00153           succes = item->document->save();
00154           if ( ! succes )
00155           {
00156             KMessageBox::sorry( this,
00157                                 i18n("Could not save the document \n'%1'").
00158                                     arg( item->document->url().prettyURL() ) );
00159           }
00160           break;
00161         case Reload:
00162           item->document->reloadFile();
00163           break;
00164         default:
00165           break;
00166       }
00167 
00168       if ( succes )
00169       {
00170         lvDocuments->takeItem( item );
00171         delete item;
00172       }
00173       else
00174         dext->setModifiedOnDisk( reason );
00175     }
00176   }
00177 
00178   if ( ! lvDocuments->childCount() )
00179     done( Ok );
00180 }
00181 
00182 void KateMwModOnHdDialog::slotSelectionChanged()
00183 {
00184   // set the diff button enabled
00185   btnDiff->setEnabled( lvDocuments->currentItem() &&
00186       KateDocManager::self()->documentInfo( ((KateDocItem*)lvDocuments->currentItem())->document )->modifiedOnDiscReason != 3 );
00187 }
00188 
00189 // ### the code below is slightly modified from kdelibs/kate/part/katedialogs,
00190 // class KateModOnHdPrompt.
00191 void KateMwModOnHdDialog::slotDiff()
00192 {
00193   if ( m_tmpfile ) // we are allready somewhere in this process.
00194     return;
00195 
00196   if ( ! lvDocuments->currentItem() )
00197     return;
00198 
00199   Kate::Document *doc = ((KateDocItem*)lvDocuments->currentItem())->document;
00200 
00201   // don't try o diff a deleted file
00202   if ( KateDocManager::self()->documentInfo( doc )->modifiedOnDiscReason == 3 )
00203     return;
00204 
00205   // Start a KProcess that creates a diff
00206   KProcIO *p = new KProcIO();
00207   p->setComm( KProcess::All );
00208   *p << "diff" << "-ub" << "-" <<  doc->url().path();
00209   connect( p, SIGNAL(processExited(KProcess*)), this, SLOT(slotPDone(KProcess*)) );
00210   connect( p, SIGNAL(readReady(KProcIO*)), this, SLOT(slotPRead(KProcIO*)) );
00211 
00212   setCursor( WaitCursor );
00213 
00214   p->start( KProcess::NotifyOnExit, true );
00215 
00216   uint lastln =  doc->numLines();
00217   for ( uint l = 0; l <  lastln; l++ )
00218     p->writeStdin(  doc->textLine( l ), l < lastln );
00219 
00220   p->closeWhenDone();
00221 }
00222 
00223 void KateMwModOnHdDialog::slotPRead( KProcIO *p)
00224 {
00225   // create a file for the diff if we haven't one allready
00226   if ( ! m_tmpfile )
00227     m_tmpfile = new KTempFile();
00228   // put all the data we have in it
00229   QString stmp;
00230   while ( p->readln( stmp, false ) > -1 )
00231     *m_tmpfile->textStream() << stmp << endl;
00232 
00233   p->ackRead();
00234 }
00235 
00236 void KateMwModOnHdDialog::slotPDone( KProcess *p )
00237 {
00238   setCursor( ArrowCursor );
00239   m_tmpfile->close();
00240 
00241   if ( ! p->normalExit() /*|| p->exitStatus()*/ )
00242   {
00243     KMessageBox::sorry( this,
00244                         i18n("The diff command failed. Please make sure that "
00245                             "diff(1) is installed and in your PATH."),
00246                         i18n("Error Creating Diff") );
00247     delete m_tmpfile;
00248     m_tmpfile = 0;
00249     return;
00250   }
00251 
00252   KRun::runURL( m_tmpfile->name(), "text/x-diff", true );
00253   delete m_tmpfile;
00254   m_tmpfile = 0;
00255 }
00256 
00257 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.4.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jun 14 01:53:51 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003