dviexport.cpp.svn-base

来自「okular」· SVN-BASE 代码 · 共 510 行 · 第 1/2 页

SVN-BASE
510
字号
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-/** * \file dviexport.h * Distributed under the GNU GPL version 2 or (at your option) * any later version. See accompanying file COPYING or copy at * http://www.gnu.org/copyleft/gpl.html * * \author Angus Leeming * \author Stefan Kebekus * * Classes DVIExportToPDF and DVIExportToPS control the export * of a DVI file to PDF or PostScript format, respectively. * Common functionality is factored out into a common base class, * DVIExport which itself derives from KShared allowing easy, * polymorphic storage of multiple KSharedPtr<DVIExport> variables * in a container of all exported processes. */#include <config.h>#include "dviexport.h"#include "dviFile.h"#include "dviRenderer.h"#include "fontprogress.h"#include "kvs_debug.h"#include <kfiledialog.h>#include <klocale.h>#include <kmessagebox.h>#include <kprinter.h>#include <kprocess.h>#include <QFileInfo>#include <QLabel>#include <QTemporaryFile>#include <cassert>//#define DVIEXPORT_USE_QPROCESSDVIExport::DVIExport(dviRenderer& parent, QWidget* parent_widget)  : started_(false),    process_(0),    progress_(0),    parent_(&parent),    parent_widget_(parent_widget){}DVIExport::~DVIExport(){  delete progress_;  delete process_;}void DVIExport::initialise_progress_dialog(int total_steps,                                           const QString& label_text,                                           const QString& whats_this_text,                                           const QString& tooltip_text,                                           const QString& caption){  assert(!progress_);  progress_ = new fontProgressDialog(QString(),                                     label_text,                                     QString(),                                     whats_this_text,                                     tooltip_text,                                     parent_widget_,                                     caption,                                     false);  if (progress_) {    progress_->TextLabel2->setText(i18n("Please be patient"));    progress_->setTotalSteps(total_steps);    connect(progress_, SIGNAL(finished()), this, SLOT(abort_process()));  }}void DVIExport::start(const QString& command,                      const QStringList& args,                      const QString& working_directory,                      const QString& error_message){  assert(!process_);#ifndef DVIEXPORT_USE_QPROCESS  process_ = new KProcess;  connect(process_, SIGNAL(receivedStderr(KProcess* , char* , int)), this, SLOT(output_receiver(KProcess* , char* , int)));  connect(process_, SIGNAL(receivedStdout(KProcess* , char* , int)), this, SLOT(output_receiver(KProcess* , char* , int)));  connect(process_, SIGNAL(processExited(KProcess* )), this, SLOT(finished(KProcess*)));  *process_ << command << args;#else  process_ = new QProcess;  process_->setReadChannelMode(QProcess::MergedChannels);  connect(process_, SIGNAL(readyReadStandardOutput()), this, SLOT(output_receiver()));  connect(process_, SIGNAL(finished(int)), this, SLOT(finished(int)));#endif  if (!working_directory.isEmpty())    process_->setWorkingDirectory(working_directory);  error_message_ = error_message;#ifndef DVIEXPORT_USE_QPROCESS  if (!process_->start(KProcess::NotifyOnExit, KProcess::AllOutput))#else  process_->start(command, args, QIODevice::ReadOnly);  if (!process_->waitForStarted(-1))#endif    kError(kvs::dvi) << command << " failed to start" << endl;  else    started_ = true;}void DVIExport::abort_process_impl(){  if (progress_) {    // Explicitly disconnect to prevent a recursive call of abort_process.    disconnect(progress_, SIGNAL(finished()), 0, 0);    if (progress_->isVisible())      progress_->hide();    delete progress_;    progress_ = 0;  }  // deleting process_ kills the external process itself  // if it's still running.  delete process_;  process_ = 0;}#ifndef DVIEXPORT_USE_QPROCESSvoid DVIExport::finished_impl()#elsevoid DVIExport::finished_impl(int exit_code)#endif{  if (progress_) {    // Explicitly disconnect to prevent a recursive call of abort_process.    disconnect(progress_, SIGNAL(finished()), 0, 0);    if (progress_->isVisible())      progress_->hide();  }#ifndef DVIEXPORT_USE_QPROCESS  if (process_ && process_->normalExit() && process_->exitStatus() != 0)#else  if (process_ && exit_code != 0)#endif    KMessageBox::error(parent_widget_, error_message_);  // Remove this from the store of all export processes.  parent_->export_finished(this);}#ifndef DVIEXPORT_USE_QPROCESSvoid DVIExport::output_receiver(KProcess* , char* buffer, int buflen)#elsevoid DVIExport::output_receiver()#endif{#ifndef DVIEXPORT_USE_QPROCESS  // Paranoia.  if (buflen <= 0)    return;#endif  if (process_) {#ifndef DVIEXPORT_USE_QPROCESS    parent_->update_info_dialog(QString::fromLocal8Bit(buffer, buflen));#else    parent_->update_info_dialog(process_->readAll());#endif    if (progress_)      progress_->show();  }}namespace {/** @returns the contents of environment variable @c envname as a *  QStringList split at the OS-dependent path separator. *  (':' on *nix, ';' on Windows. */const QStringList get_env_path(const char* envname){  if (!envname || !*envname)    return QStringList();#if defined _WIN32  const char path_separator = ';';#else  const char path_separator = ':';#endif  const char* const envvar = ::getenv(envname);  if (!envvar || !*envvar)    return QStringList();  return QString(envvar).split(path_separator);}/** @returns true if the file @c exe_ can be found in the PATH *  and is executable. */bool find_exe(const QString& exe_){#if defined _WIN32  const QFileInfo exe(exe_.endsWith(".exe") ? exe_ : exe_ + ".exe");#else  const QFileInfo exe(exe_);#endif  if (exe.isAbsolute())    return exe.exists() && exe.isReadable() && exe.isExecutable();  const QStringList path = get_env_path("PATH");  if (path.isEmpty())    return false;  typedef QStringList::const_iterator iterator;  const iterator end = path.end();  for (iterator it = path.begin(); it != end; ++it) {    const QString dir = it->endsWith("/") ? *it : *it + '/';    const QFileInfo abs_exe = dir + exe.filePath();    if (abs_exe.exists())      return abs_exe.isReadable() && abs_exe.isExecutable();  }  return false;}} // namespace anonDVIExportToPDF::DVIExportToPDF(dviRenderer& parent, QWidget* parent_widget)  : DVIExport(parent, parent_widget){  // Neither of these should happen. Paranoia checks.  if (!parent.dviFile)    return;  const dvifile& dvi = *(parent.dviFile);  const QFileInfo input(dvi.filename);  if (!input.exists() || !input.isReadable())

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?