swift
crashinfo.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/crashinfo.h"
5 
6 #include "misc/fileutils.h"
7 #include "misc/worker.h"
8 
10 
11 namespace swift::misc
12 {
13  QString CCrashInfo::convertToQString(bool i18n) const
14  {
15  Q_UNUSED(i18n);
16  return QStringLiteral("{ %1, %2 }").arg(this->getInfo(), this->getUserName());
17  }
18 
19  void CCrashInfo::appendInfo(const QString &extraInfo)
20  {
21  if (extraInfo.isEmpty()) { return; }
22  if (m_info.isEmpty())
23  {
24  this->setInfo(extraInfo);
25  return;
26  }
27  m_info += u' ' % extraInfo;
28  }
29 
30  void CCrashInfo::setLogPathAndFileName(const QString &fileName) { m_logFileAndPath = fileName; }
31 
33  {
34  if (index.isMyself()) { return QVariant::fromValue(*this); }
35  switch (index.frontCasted<ColumnIndex>())
36  {
37  case IndexUserName: return QVariant::fromValue(m_userName);
38  case IndexInfo: return QVariant::fromValue(m_info);
39  case IndexSimulatorString: return QVariant::fromValue(m_simulatorString);
40  case IndexFlightNetworkInfo: return QVariant::fromValue(m_flightNetwork);
41  default: break;
42  }
43  return CValueObject::propertyByIndex(index);
44  }
45 
46  void CCrashInfo::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
47  {
48  if (index.isMyself())
49  {
50  (*this) = variant.value<CCrashInfo>();
51  return;
52  }
53  switch (index.frontCasted<ColumnIndex>())
54  {
55  case IndexUserName: this->setUserName(variant.toString()); break;
56  case IndexInfo: this->setInfo(variant.toString()); break;
57  case IndexSimulatorString: this->setSimulatorString(variant.toString()); break;
58  case IndexFlightNetworkInfo: this->setFlightNetworkString(variant.toString()); break;
59  default: CValueObject::setPropertyByIndex(index, variant); break;
60  }
61  }
62 
63  int CCrashInfo::comparePropertyByIndex(CPropertyIndexRef index, const CCrashInfo &compareValue) const
64  {
65  if (index.isMyself()) { return this->getInfo().compare(compareValue.getInfo()); }
66  switch (index.frontCasted<ColumnIndex>())
67  {
68  case IndexUserName: return this->getUserName().compare(compareValue.getUserName());
69  case IndexInfo: return this->getInfo().compare(compareValue.getInfo());
70  case IndexSimulatorString: return this->getSimulatorString().compare(compareValue.getInfo());
71  case IndexFlightNetworkInfo:
72  return this->getFlightNetworkString().compare(compareValue.getFlightNetworkString());
73  default: return CValueObject::comparePropertyByIndex(index.copyFrontRemoved(), compareValue);
74  }
75  }
76 
78  {
79  if (m_logFileAndPath.isEmpty()) { return; }
80  CWorker::fromTask(qApp, Q_FUNC_INFO, [this] { writeToFile(); });
81  }
82 
84  {
85  if (m_logFileAndPath.isEmpty()) { return false; }
86  return CFileUtils::writeStringToFile(this->summary(), m_logFileAndPath);
87  }
88 
89  QString CCrashInfo::summary() const
90  {
91  return (m_userName.isEmpty() ? QStringLiteral("") : u"user name: " % m_userName % u"\n") %
92  (m_simulatorString.isEmpty() ? QStringLiteral("") : u"simulator: " % m_simulatorString % u"\n") %
93  (m_flightNetwork.isEmpty() ? QStringLiteral("") : u"network: " % m_flightNetwork % u"\n") %
94  (m_info.isEmpty() ? QStringLiteral("") : u"info: " % m_info % u"\n");
95  }
96 } // namespace swift::misc
const QString & getSimulatorString() const
Simulator string.
Definition: crashinfo.h:51
void appendInfo(const QString &extraInfo)
Append some info.
Definition: crashinfo.cpp:19
QString summary() const
Summary.
Definition: crashinfo.cpp:89
void setFlightNetworkString(const QString &network)
Network string.
Definition: crashinfo.h:60
int comparePropertyByIndex(CPropertyIndexRef index, const CCrashInfo &compareValue) const
Compare for index.
Definition: crashinfo.cpp:63
bool writeToFile() const
Write to file (synchronous)
Definition: crashinfo.cpp:83
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: crashinfo.cpp:13
const QString & getUserName() const
Get user name.
Definition: crashinfo.h:36
void setUserName(const QString &userName)
User name.
Definition: crashinfo.h:39
const QString & getFlightNetworkString() const
Network string.
Definition: crashinfo.h:57
void setLogPathAndFileName(const QString &fileName)
Set path and file name.
Definition: crashinfo.cpp:30
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: crashinfo.cpp:46
void setSimulatorString(const QString &simString)
Simulator string.
Definition: crashinfo.h:54
void triggerWritingFile() const
Trigger writing this to file (in background)
Definition: crashinfo.cpp:77
ColumnIndex
Properties by index.
Definition: crashinfo.h:25
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: crashinfo.cpp:32
void setInfo(const QString &info)
Set info.
Definition: crashinfo.h:48
const QString & getInfo() const
Get the info.
Definition: crashinfo.h:45
static bool writeStringToFile(const QString &content, const QString &fileNameAndPath)
Write string to text file.
Definition: fileutils.cpp:40
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
static CWorker * fromTask(QObject *owner, const QString &name, F &&task)
Returns a new worker object which lives in a new thread.
Definition: worker.h:201
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition: mixinindex.h:187
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
Free functions in swift::misc.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67