2009-10-24 16:15:28 +00:00
|
|
|
/**
|
|
|
|
* \file GuiCompare.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Vincent van Ravesteijn
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "GuiCompare.h"
|
|
|
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "BufferList.h"
|
|
|
|
#include "buffer_funcs.h"
|
2009-10-25 13:41:46 +00:00
|
|
|
#include "Compare.h"
|
2009-10-24 16:15:28 +00:00
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "GuiView.h"
|
|
|
|
#include "LyXRC.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
|
|
|
#include "support/debug.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/FileName.h"
|
|
|
|
#include "support/gettext.h"
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
|
|
|
|
GuiCompare::GuiCompare(GuiView & lv)
|
|
|
|
: GuiDialog(lv, "compare", qt_("Compare LyX files")),
|
2009-10-25 13:41:46 +00:00
|
|
|
compare_(0), dest_buffer_(0), old_buffer_(0), new_buffer_(0)
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
setModal(Qt::WindowModal);
|
|
|
|
|
|
|
|
connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
|
|
|
|
connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));
|
|
|
|
|
2009-10-24 17:04:09 +00:00
|
|
|
connect(newFilePB, SIGNAL(clicked()), this, SLOT(selectNewFile()));
|
|
|
|
connect(oldFilePB, SIGNAL(clicked()), this, SLOT(selectOldFile()));
|
2009-10-24 16:15:28 +00:00
|
|
|
|
|
|
|
connect(newFileCB, SIGNAL(currentIndexChanged(int)),
|
2009-10-24 17:04:09 +00:00
|
|
|
this, SLOT(changeAdaptor()));
|
2009-10-24 16:15:28 +00:00
|
|
|
connect(newFileCB, SIGNAL(editTextChanged(const QString &)),
|
2009-10-24 17:04:09 +00:00
|
|
|
this, SLOT(changeAdaptor()));
|
2009-10-24 16:15:28 +00:00
|
|
|
connect(oldFileCB, SIGNAL(currentIndexChanged(int)),
|
2009-10-24 17:04:09 +00:00
|
|
|
this, SLOT(changeAdaptor()));
|
2009-10-24 16:15:28 +00:00
|
|
|
connect(oldFileCB, SIGNAL(editTextChanged(const QString &)),
|
2009-10-24 17:04:09 +00:00
|
|
|
this, SLOT(changeAdaptor()));
|
2009-10-24 16:15:28 +00:00
|
|
|
|
|
|
|
newSettingsRB->setChecked(true);
|
|
|
|
|
|
|
|
progressBar->setValue(0);
|
|
|
|
progressBar->setEnabled(false);
|
|
|
|
|
|
|
|
bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
|
|
|
|
bc().setOK(okPB);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiCompare::~GuiCompare()
|
|
|
|
{
|
2009-10-25 13:41:46 +00:00
|
|
|
if (compare_)
|
|
|
|
delete compare_;
|
2009-10-24 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiCompare::closeEvent(QCloseEvent *)
|
|
|
|
{
|
|
|
|
slotCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-24 17:04:09 +00:00
|
|
|
void GuiCompare::changeAdaptor()
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GuiCompare::isValid()
|
|
|
|
{
|
|
|
|
bool const valid = !newFileCB->currentText().isEmpty()
|
|
|
|
&& !oldFileCB->currentText().isEmpty();
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCompare::updateContents()
|
|
|
|
{
|
2009-10-25 13:41:46 +00:00
|
|
|
if (compare_ && compare_->isRunning())
|
|
|
|
return;
|
|
|
|
|
2009-10-24 16:15:28 +00:00
|
|
|
QString restore_filename1 = newFileCB->currentText();
|
|
|
|
QString restore_filename2 = oldFileCB->currentText();
|
|
|
|
newFileCB->clear();
|
|
|
|
oldFileCB->clear();
|
|
|
|
progressBar->setValue(0);
|
|
|
|
BufferList::iterator it = theBufferList().begin();
|
|
|
|
BufferList::iterator const end = theBufferList().end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
QString filename = toqstr((*it)->absFileName());
|
|
|
|
newFileCB->addItem(filename);
|
|
|
|
oldFileCB->addItem(filename);
|
|
|
|
}
|
|
|
|
if (lyxview().documentBufferView())
|
|
|
|
newFileCB->setEditText(toqstr(buffer().absFileName()));
|
|
|
|
else
|
|
|
|
newFileCB->setEditText(restore_filename1);
|
|
|
|
|
|
|
|
if (!restore_filename2.isEmpty())
|
|
|
|
oldFileCB->setEditText(restore_filename2);
|
|
|
|
else
|
|
|
|
oldFileCB->clearEditText();
|
|
|
|
|
|
|
|
if (isValid()) {
|
|
|
|
bc().setValid(isValid());
|
|
|
|
bc().apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-24 17:04:09 +00:00
|
|
|
void GuiCompare::selectNewFile()
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
QString name = browse(newFileCB->currentText());
|
|
|
|
if (!name.isEmpty())
|
|
|
|
newFileCB->setEditText(name);
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-24 17:04:09 +00:00
|
|
|
void GuiCompare::selectOldFile()
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
QString name = browse(oldFileCB->currentText());
|
|
|
|
if (!name.isEmpty())
|
|
|
|
oldFileCB->setEditText(name);
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString GuiCompare::browse(QString const & in_name) const
|
|
|
|
{
|
|
|
|
QString const title = qt_("Select document");
|
|
|
|
|
|
|
|
QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
|
|
|
|
|
|
|
|
QString filename;
|
|
|
|
if (lyxview().documentBufferView()) {
|
|
|
|
QString path = bufferFilepath();
|
|
|
|
filename = browseRelFile(in_name, path, title, filters, false,
|
|
|
|
qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
|
|
|
|
} else {
|
|
|
|
QString path = toqstr(lyxrc.document_path);
|
|
|
|
QString rel_filename = browseRelFile(in_name, path, title, filters, false,
|
|
|
|
qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
|
|
|
|
filename = makeAbsPath(rel_filename, path);
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCompare::enableControls(bool enable) const
|
|
|
|
{
|
|
|
|
newFileLA->setEnabled(enable);
|
|
|
|
newFilePB->setEnabled(enable);
|
|
|
|
newFileCB->setEnabled(enable);
|
|
|
|
oldFileLA->setEnabled(enable);
|
|
|
|
oldFilePB->setEnabled(enable);
|
|
|
|
oldFileCB->setEnabled(enable);
|
|
|
|
okPB->setEnabled(enable);
|
|
|
|
groupBox->setEnabled(enable);
|
|
|
|
progressBar->setEnabled(!enable);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
closePB->setText(qt_("Close"));
|
|
|
|
else
|
|
|
|
closePB->setText(qt_("Cancel"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-25 13:41:46 +00:00
|
|
|
void GuiCompare::error()
|
|
|
|
{
|
|
|
|
Alert::error(_("Error"), _("Error while comparing documents."));
|
|
|
|
window_title_ = windowTitle();
|
|
|
|
finished(true);
|
|
|
|
}
|
|
|
|
|
2009-10-24 16:15:28 +00:00
|
|
|
void GuiCompare::finished(bool aborted)
|
|
|
|
{
|
|
|
|
enableControls(true);
|
2009-10-25 13:56:44 +00:00
|
|
|
|
2009-10-25 13:41:46 +00:00
|
|
|
if (compare_) {
|
|
|
|
delete compare_;
|
|
|
|
compare_ = 0;
|
|
|
|
}
|
2009-10-24 16:15:28 +00:00
|
|
|
|
|
|
|
if (aborted) {
|
2009-10-25 13:41:46 +00:00
|
|
|
if (dest_buffer_) {
|
|
|
|
dest_buffer_->markClean();
|
|
|
|
theBufferList().release(dest_buffer_);
|
|
|
|
}
|
2009-10-24 16:15:28 +00:00
|
|
|
setWindowTitle(window_title_);
|
|
|
|
progressBar->setValue(0);
|
|
|
|
} else {
|
|
|
|
hideView();
|
|
|
|
bc().ok();
|
2009-10-25 13:41:46 +00:00
|
|
|
if (dest_buffer_) {
|
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
|
|
|
|
dest_buffer_->absFileName()));
|
|
|
|
}
|
2009-10-24 16:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-25 13:41:46 +00:00
|
|
|
void GuiCompare::progress(int val)
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
progressBar->setValue(progressBar->value() + val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-24 17:04:09 +00:00
|
|
|
void GuiCompare::progressMax(int max) const
|
2009-10-24 16:15:28 +00:00
|
|
|
{
|
|
|
|
progressBar->setMaximum(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCompare::slotOK()
|
|
|
|
{
|
|
|
|
enableControls(false);
|
2009-10-25 13:41:46 +00:00
|
|
|
if (!run())
|
|
|
|
error();
|
2009-10-24 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiCompare::slotCancel()
|
|
|
|
{
|
2009-10-25 13:41:46 +00:00
|
|
|
if (compare_ && compare_->isRunning()) {
|
|
|
|
window_title_ = windowTitle();
|
|
|
|
setWindowTitle(window_title_ + " " + qt_("(cancelling)"));
|
|
|
|
compare_->abort();
|
|
|
|
} else {
|
|
|
|
GuiDialog::slotClose();
|
|
|
|
progressBar->setValue(0);
|
|
|
|
}
|
2009-10-24 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Buffer const * GuiCompare::bufferFromFileName(string const & file) const
|
|
|
|
{
|
|
|
|
FileName fname;
|
|
|
|
if (FileName::isAbsolute(file))
|
|
|
|
fname.set(file);
|
|
|
|
else if (lyxview().documentBufferView())
|
|
|
|
fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
|
|
|
|
|
|
|
|
if (fname.empty()
|
|
|
|
|| (!fname.exists() && !theBufferList().getBuffer(fname))) {
|
|
|
|
LYXERR0( "Unable to read: " << file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return loadIfNeeded(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiCompare::run()
|
|
|
|
{
|
|
|
|
progressBar->setValue(0);
|
|
|
|
|
|
|
|
new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
|
|
|
|
old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
|
|
|
|
|
|
|
|
// new buffer that will carry the output
|
|
|
|
FileName initpath(lyxrc.document_path);
|
|
|
|
dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
|
2009-10-25 13:41:46 +00:00
|
|
|
|
|
|
|
if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
|
|
|
|
return 0;
|
|
|
|
|
2010-01-08 02:03:54 +00:00
|
|
|
dest_buffer_->changed(true);
|
2009-10-24 16:15:28 +00:00
|
|
|
dest_buffer_->markDirty();
|
|
|
|
|
2009-10-25 13:41:46 +00:00
|
|
|
// get the options from the dialog
|
|
|
|
CompareOptions options;
|
|
|
|
options.settings_from_new = newSettingsRB->isChecked();
|
|
|
|
|
|
|
|
// init the compare object and start it
|
|
|
|
compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
|
|
|
|
connect(compare_, SIGNAL(error()), this, SLOT(error()));
|
|
|
|
connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
|
|
|
|
connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
|
|
|
|
connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
|
|
|
|
compare_->start(QThread::LowPriority);
|
|
|
|
return 1;
|
2009-10-24 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
|
|
|
|
#include "moc_GuiCompare.cpp"
|