Kate
katerecoverbar.cpp
Go to the documentation of this file.
00001 /* This file is part of the Kate project. 00002 * 00003 * Copyright (C) 2010 Dominik Haumann <dhaumann kde org> 00004 * Copyright (C) 2010 Diana-Victoria Tiriplica <diana.tiriplica@gmail.com> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "katerecoverbar.h" 00023 #include "ui_recoverwidget.h" 00024 #include "kateswapfile.h" 00025 #include "kateview.h" 00026 #include "katedocument.h" 00027 00028 #include <kprocess.h> 00029 #include <kmessagebox.h> 00030 #include <ktemporaryfile.h> 00031 #include <krun.h> 00032 00033 #include <QWhatsThis> 00034 00035 //BEGIN KateRecoverBar 00036 KateRecoverBar::KateRecoverBar(KateView *view, QWidget *parent) 00037 : KateViewBarWidget( false, parent ) 00038 , m_view ( view ) 00039 , m_ui (new Ui::RecoverWidget()) 00040 , m_proc(0) 00041 { 00042 m_ui->setupUi( centralWidget() ); 00043 00044 // clicking on the "Help" link pops up the content as what's this 00045 connect(m_ui->lblSwap, SIGNAL(linkActivated(const QString&)), 00046 this, SLOT(showWhatsThis(const QString&))); 00047 00048 // set icons, but keep text from ui file 00049 m_ui->btnRecover->setGuiItem(KGuiItem(m_ui->btnRecover->text(), KIcon("edit-redo"))); 00050 m_ui->btnDiscard->setGuiItem(KStandardGuiItem::discard()); 00051 m_ui->lblIcon->setPixmap(KIcon("dialog-warning").pixmap(64, 64)); 00052 00053 // use queued connections because this (all) KateRecoverBar widgets are deleted 00054 connect(m_ui->btnRecover, SIGNAL(clicked()), m_view->doc()->swapFile(), SLOT(recover()), Qt::QueuedConnection); 00055 connect(m_ui->btnDiscard, SIGNAL(clicked()), m_view->doc()->swapFile(), SLOT(discard()), Qt::QueuedConnection); 00056 connect(m_ui->btnDiff, SIGNAL(clicked()), this, SLOT(viewDiff())); 00057 } 00058 00059 KateRecoverBar::~KateRecoverBar () 00060 { 00061 delete m_ui; 00062 } 00063 00064 void KateRecoverBar::showWhatsThis(const QString& text) 00065 { 00066 QWhatsThis::showText(QCursor::pos(), text); 00067 } 00068 00069 void KateRecoverBar::viewDiff() 00070 { 00071 // create a document with the recovered data 00072 KateDocument recoverDoc; 00073 recoverDoc.setText(m_view->doc()->text()); 00074 00075 QString path = m_view->doc()->swapFile()->fileName(); 00076 00077 if (path.isNull()) 00078 return; 00079 00080 QFile swp(path); 00081 if (!swp.open(QIODevice::ReadOnly)) { 00082 kWarning( 13020 ) << "Can't open swap file"; 00083 return; 00084 } 00085 00086 QDataStream stream(&swp); 00087 00088 recoverDoc.swapFile()->recover(stream); 00089 00090 // create a KProcess proc 00091 m_proc = new KProcess(this); 00092 m_proc->setOutputChannelMode(KProcess::MergedChannels); 00093 *m_proc << "diff" << "-u" << "-" << m_view->doc()->url().toLocalFile(); 00094 00095 connect(m_proc, SIGNAL(readyRead()), this, SLOT(slotDataAvailable())); 00096 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotDiffFinished())); 00097 00098 setCursor(Qt::WaitCursor); 00099 00100 // disable the "View Changes" button, so the user won't click it twice 00101 m_ui->btnDiff->setEnabled(false); 00102 00103 m_diffContent.clear(); 00104 00105 m_proc->start(); 00106 00107 QTextStream ts(m_proc); 00108 int lineCount = recoverDoc.lines(); 00109 for (int line = 0; line < lineCount; ++line) 00110 ts << recoverDoc.line(line) << '\n'; 00111 ts.flush(); 00112 m_proc->closeWriteChannel(); 00113 } 00114 00115 void KateRecoverBar::slotDataAvailable() 00116 { 00117 // collect diff output 00118 m_diffContent += m_proc->readAll(); 00119 } 00120 00121 void KateRecoverBar::slotDiffFinished() 00122 { 00123 m_ui->btnDiff->setEnabled(true); 00124 unsetCursor(); 00125 00126 // get the exit status to check whether diff command run successfully 00127 const QProcess::ExitStatus es = m_proc->exitStatus(); 00128 delete m_proc; 00129 m_proc = 0; 00130 00131 KTemporaryFile tempFile; 00132 tempFile.setSuffix(".diff"); 00133 if (!tempFile.open()) { 00134 kWarning( 13020 ) << "Can't open diff temporary file"; 00135 return; 00136 } 00137 00138 // write the buffered data to the temporary file 00139 tempFile.write(m_diffContent); 00140 00141 // check exit status 00142 if (es != QProcess::NormalExit) 00143 { 00144 KMessageBox::sorry(this, 00145 i18n("The diff command failed. Please make sure that " 00146 "diff(1) is installed and in your PATH."), 00147 i18n("Error Creating Diff")); 00148 return; 00149 } 00150 00151 // sanity check: is there any diff content? 00152 if ( tempFile.size() == 0 ) 00153 { 00154 KMessageBox::information(this, 00155 i18n("The files are identical."), 00156 i18n("Diff Output")); 00157 return; 00158 } 00159 00160 tempFile.setAutoRemove(false); 00161 KUrl url = KUrl::fromPath(tempFile.fileName()); 00162 00163 // KRun::runUrl should delete the file, once the client exits 00164 KRun::runUrl(url, "text/x-patch", this, true ); 00165 } 00166 00167 //END KateRecoverBar 00168 00169 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference