• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • swapfile
kateswapdiffcreator.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  *
3  * Copyright (C) 2010-2012 Dominik Haumann <dhaumann kde org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kateswapdiffcreator.h"
22 #include "kateswapfile.h"
23 #include "katedocument.h"
24 
25 #include <kprocess.h>
26 #include <kmessagebox.h>
27 #include <krun.h>
28 #include <klocale.h>
29 
30 //BEGIN SwapDiffCreator
31 SwapDiffCreator::SwapDiffCreator(Kate::SwapFile* swapFile)
32  : QObject (swapFile)
33  , m_swapFile (swapFile)
34  , m_proc(0)
35 {
36 }
37 
38 SwapDiffCreator::~SwapDiffCreator ()
39 {
40 }
41 
42 void SwapDiffCreator::viewDiff()
43 {
44  QString path = m_swapFile->fileName();
45  if (path.isNull())
46  return;
47 
48  QFile swp(path);
49  if (!swp.open(QIODevice::ReadOnly)) {
50  kWarning( 13020 ) << "Can't open swap file";
51  return;
52  }
53 
54  // create all needed tempfiles
55  m_originalFile.setSuffix(".original");
56  m_recoveredFile.setSuffix(".recovered");
57  m_diffFile.setSuffix(".diff");
58 
59  if (!m_originalFile.open() || !m_recoveredFile.open() || !m_diffFile.open()) {
60  kWarning( 13020 ) << "Can't open temporary files needed for diffing";
61  return;
62  }
63 
64  // truncate files, just in case
65  m_originalFile.resize (0);
66  m_recoveredFile.resize (0);
67  m_diffFile.resize (0);
68 
69  // create a document with the recovered data
70  KateDocument recoverDoc;
71  recoverDoc.setText(m_swapFile->document()->text());
72 
73  // store original text in a file as utf-8 and close it
74  {
75  QTextStream stream (&m_originalFile);
76  stream.setCodec (QTextCodec::codecForName("UTF-8"));
77  stream << recoverDoc.text ();
78  }
79  m_originalFile.close ();
80 
81  // recover data
82  QDataStream stream(&swp);
83  recoverDoc.swapFile()->recover(stream, false);
84 
85  // store recovered text in a file as utf-8 and close it
86  {
87  QTextStream stream (&m_recoveredFile);
88  stream.setCodec (QTextCodec::codecForName("UTF-8"));
89  stream << recoverDoc.text ();
90  }
91  m_recoveredFile.close ();
92 
93  // create a KProcess proc for diff
94  m_proc = new KProcess(this);
95  m_proc->setOutputChannelMode(KProcess::MergedChannels);
96  *m_proc << "diff" << "-u" << m_originalFile.fileName() << m_recoveredFile.fileName();
97 
98  connect(m_proc, SIGNAL(readyRead()), this, SLOT(slotDataAvailable()));
99  connect(m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotDiffFinished()));
100 
101 // setCursor(Qt::WaitCursor);
102 
103  m_proc->start();
104 
105  QTextStream ts(m_proc);
106  int lineCount = recoverDoc.lines();
107  for (int line = 0; line < lineCount; ++line)
108  ts << recoverDoc.line(line) << '\n';
109  ts.flush();
110  m_proc->closeWriteChannel();
111 }
112 
113 void SwapDiffCreator::slotDataAvailable()
114 {
115  // collect diff output
116  m_diffFile.write (m_proc->readAll());
117 }
118 
119 void SwapDiffCreator::slotDiffFinished()
120 {
121  // collect last diff output, if any
122  m_diffFile.write (m_proc->readAll());
123 
124  // get the exit status to check whether diff command run successfully
125  const QProcess::ExitStatus es = m_proc->exitStatus();
126  delete m_proc;
127  m_proc = 0;
128 
129  // check exit status
130  if (es != QProcess::NormalExit)
131  {
132  KMessageBox::sorry(0,
133  i18n("The diff command failed. Please make sure that "
134  "diff(1) is installed and in your PATH."),
135  i18n("Error Creating Diff"));
136  deleteLater();
137  return;
138  }
139 
140  // sanity check: is there any diff content?
141  if ( m_diffFile.size() == 0 )
142  {
143  KMessageBox::information(0,
144  i18n("The files are identical."),
145  i18n("Diff Output"));
146  deleteLater();
147  return;
148  }
149 
150  // close diffFile and avoid removal, KRun will do that later!
151  m_diffFile.close ();
152  m_diffFile.setAutoRemove (false);
153 
154  // KRun::runUrl should delete the file, once the client exits
155  KRun::runUrl (KUrl::fromPath(m_diffFile.fileName()), "text/x-patch", m_swapFile->document()->activeView(), true );
156 
157  deleteLater();
158 }
159 
160 //END SwapDiffCreator
161 
162 // kate: space-indent on; indent-width 2; replace-tabs on;
QTextStream::setCodec
void setCodec(QTextCodec *codec)
KateDocument::line
virtual QString line(int line) const
Definition: katedocument.cpp:447
KateDocument::setText
virtual bool setText(const QString &)
Definition: katedocument.cpp:457
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
SwapDiffCreator::slotDataAvailable
void slotDataAvailable()
Definition: kateswapdiffcreator.cpp:113
SwapDiffCreator::SwapDiffCreator
SwapDiffCreator(Kate::SwapFile *swapFile)
Definition: kateswapdiffcreator.cpp:31
kateswapfile.h
KateDocument::activeView
virtual KTextEditor::View * activeView() const
Definition: katedocument.h:156
QDataStream
Kate::SwapFile::recover
void recover()
Definition: kateswapfile.cpp:208
kateswapdiffcreator.h
KateDocument::swapFile
Kate::SwapFile * swapFile()
Definition: katedocument.cpp:5455
katedocument.h
QFile
QTextStream
QString::isNull
bool isNull() const
Kate::SwapFile
Class for tracking editing actions.
Definition: kateswapfile.h:46
QObject
KateDocument::lines
virtual int lines() const
Definition: katedocument.cpp:753
Kate::SwapFile::fileName
QString fileName()
Definition: kateswapfile.cpp:561
QObject::deleteLater
void deleteLater()
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
KateDocument
Definition: katedocument.h:74
SwapDiffCreator::~SwapDiffCreator
~SwapDiffCreator()
Definition: kateswapdiffcreator.cpp:38
QTextStream::flush
void flush()
SwapDiffCreator::viewDiff
void viewDiff()
Definition: kateswapdiffcreator.cpp:42
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
SwapDiffCreator::slotDiffFinished
void slotDiffFinished()
Definition: kateswapdiffcreator.cpp:119
KateDocument::text
virtual QString text(const KTextEditor::Range &range, bool blockwise=false) const
Definition: katedocument.cpp:337
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kate::SwapFile::document
KateDocument * document()
Definition: kateswapfile.cpp:134
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal