LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "dumper.h"
31 #include <QtDebug>
32 #include <util/sll/slotclosure.h>
33 
34 namespace LC
35 {
36 namespace Util
37 {
38  Dumper::Dumper (const QString& from, const QString& to, QObject *parent)
39  : QObject { parent }
40  , Dumper_ { new QProcess { this } }
41  , Restorer_ { new QProcess { this } }
42  {
43  Iface_.reportStarted ();
44 
45  Dumper_->setStandardOutputProcess (Restorer_);
46 
47  new Util::SlotClosure<Util::NoDeletePolicy>
48  {
49  [this] { HandleProcessError (Dumper_); },
50  Dumper_,
51  SIGNAL (error (QProcess::ProcessError)),
52  Dumper_
53  };
54  new Util::SlotClosure<Util::NoDeletePolicy>
55  {
56  [this] { HandleProcessError (Restorer_); },
57  Restorer_,
58  SIGNAL (error (QProcess::ProcessError)),
59  Restorer_
60  };
61 
62  new Util::SlotClosure<Util::NoDeletePolicy>
63  {
64  [this] { HandleProcessFinished (Dumper_); },
65  Dumper_,
66  SIGNAL (finished (int, QProcess::ExitStatus)),
67  Dumper_
68  };
69  new Util::SlotClosure<Util::NoDeletePolicy>
70  {
71  [this] { HandleProcessFinished (Restorer_); },
72  Restorer_,
73  SIGNAL (finished (int, QProcess::ExitStatus)),
74  Restorer_
75  };
76 
77  Dumper_->start ("sqlite3", { from, ".dump" });
78  Restorer_->start ("sqlite3", { to });
79  }
80 
82  {
83  return Iface_.future ();
84  }
85 
86  void Dumper::HandleProcessFinished (QProcess *process)
87  {
88  const auto& stderr = QString::fromUtf8 (process->readAllStandardError ());
89  const auto exitCode = process->exitCode ();
90 
91  qDebug () << Q_FUNC_INFO
92  << process->exitStatus ()
93  << exitCode
94  << stderr;
95 
96  switch (process->exitStatus ())
97  {
98  case QProcess::CrashExit:
99  if (HadError_)
100  break;
101 
102  HadError_ = true;
103  ReportResult (tr ("Dumping process crashed: %1.")
104  .arg (stderr.isEmpty () ?
105  process->errorString () :
106  stderr));
107  break;
108  case QProcess::NormalExit:
109  if (exitCode)
110  ReportResult (tr ("Dumping process finished with error: %1 (%2).")
111  .arg (stderr)
112  .arg (exitCode));
113  else if (++FinishedCount_ == 2)
114  {
115  ReportResult (Finished {});
116  deleteLater ();
117  }
118  break;
119  }
120  }
121 
122  void Dumper::HandleProcessError (const QProcess *process)
123  {
124  qWarning () << Q_FUNC_INFO
125  << process->error ()
126  << process->errorString ();
127 
128  if (HadError_)
129  return;
130 
131  HadError_ = true;
132 
133  if (process->error () == QProcess::FailedToStart)
134  ReportResult (tr ("Unable to start dumping process: %1. Do you have sqlite3 installed?")
135  .arg (process->errorString ()));
136  else
137  ReportResult (tr ("Unable to dump the database: %1.")
138  .arg (process->errorString ()));
139  }
140 
141  void Dumper::ReportResult (const Result_t& result)
142  {
143  Iface_.reportFinished (&result);
144  }
145 }
146 }
Dumper(const QString &from, const QString &to, QObject *=nullptr)
Definition: dumper.cpp:38
QFuture< Result_t > GetFuture()
Definition: dumper.cpp:81
Definition: constants.h:35