LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
dumper.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "dumper.h"
10 #include <QtDebug>
11 
12 namespace LC::Util
13 {
14  Dumper::Dumper (const QString& from, const QString& to, QObject *parent)
15  : QObject { parent }
16  , Dumper_ { new QProcess { this } }
17  , Restorer_ { new QProcess { this } }
18  {
19  Iface_.reportStarted ();
20 
21  Dumper_->setStandardOutputProcess (Restorer_);
22 
23  connect (Dumper_,
24  &QProcess::errorOccurred,
25  this,
26  [this] { HandleProcessError (Dumper_); });
27  connect (Restorer_,
28  &QProcess::errorOccurred,
29  this,
30  [this] { HandleProcessError (Restorer_); });
31  connect (Dumper_,
32  qOverload<int, QProcess::ExitStatus> (&QProcess::finished),
33  this,
34  [this] { HandleProcessFinished (Dumper_); });
35  connect (Restorer_,
36  qOverload<int, QProcess::ExitStatus> (&QProcess::finished),
37  this,
38  [this] { HandleProcessFinished (Restorer_); });
39 
40  static const QString sqliteExecutable = QStringLiteral ("sqlite3");
41  Dumper_->start (sqliteExecutable, { from, QStringLiteral (".dump") });
42  Restorer_->start (sqliteExecutable, { to });
43  }
44 
46  {
47  return Iface_.future ();
48  }
49 
50  void Dumper::HandleProcessFinished (QProcess *process)
51  {
52  const auto& stderr = QString::fromUtf8 (process->readAllStandardError ());
53  const auto exitCode = process->exitCode ();
54 
55  qDebug () << Q_FUNC_INFO
56  << process->exitStatus ()
57  << exitCode
58  << stderr;
59 
60  switch (process->exitStatus ())
61  {
62  case QProcess::CrashExit:
63  {
64  if (HadError_)
65  break;
66 
67  HadError_ = true;
68  auto errMsg = tr ("Dumping process crashed: %1.")
69  .arg (stderr.isEmpty () ?
70  process->errorString () :
71  stderr);
72  ReportResult (Error { std::move (errMsg) });
73  break;
74  }
75  case QProcess::NormalExit:
76  {
77  if (exitCode)
78  {
79  auto errMsg = tr ("Dumping process finished with error: %1 (%2).")
80  .arg (stderr)
81  .arg (exitCode);
82  ReportResult (Error { std::move (errMsg) });
83  }
84  else if (++FinishedCount_ == 2)
85  {
86  ReportResult (Finished {});
87  deleteLater ();
88  }
89  break;
90  }
91  }
92  }
93 
94  void Dumper::HandleProcessError (const QProcess *process)
95  {
96  qWarning () << Q_FUNC_INFO
97  << process->error ()
98  << process->errorString ();
99 
100  if (HadError_)
101  return;
102 
103  HadError_ = true;
104 
105  const auto& errMsg = process->error () == QProcess::FailedToStart ?
106  tr ("Unable to start dumping process: %1. Do you have sqlite3 installed?") :
107  tr ("Unable to dump the database: %1.");
108  ReportResult (Error { errMsg.arg (process->errorString ()) });
109  }
110 
111  void Dumper::ReportResult (const Result_t& result)
112  {
113  Iface_.reportFinished (&result);
114  }
115 }
LC::Util::Dumper::Error
Definition: dumper.h:40
LC::Util::Dumper::Finished
Definition: dumper.h:39
LC::Util
Definition: icoreproxy.h:33
QFuture
Definition: idownload.h:17
LC::Util::Dumper::GetFuture
QFuture< Result_t > GetFuture()
Definition: dumper.cpp:51
dumper.h
LC::Util::Dumper::Dumper
Dumper(const QString &from, const QString &to, QObject *=nullptr)
Definition: dumper.cpp:20