Kstars

binarylistcomponent.h
1 /*
2  SPDX-FileCopyrightText: 2018 Valentin Boettcher <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QDataStream>
10 
11 #include "listcomponent.h"
12 #include "binarylistcomponent.h"
13 #include "auxiliary/kspaths.h"
14 
15 //TODO: Error Handling - SERIOUSLY
16 
17 /**
18  * @class BinaryListComponent
19  * @short provides functionality for loading the component data from Binary
20  * @author Valentin Boettcher
21  * @version 1.0
22  *
23  * This class is an abstract Template which requires that the type `T` is some child of
24  * `SkyObject` and the type `Component` is some child of `ListComponent`. The class `T`
25  * must provide a static `TYPE` property of the type `SkyObject::TYPE`. This is required
26  * because access to the `type()` method is inconvenient here!
27  *
28  * The derived class must provide a `void loadFromText()` method, which loads the component
29  * via `addListObject` or similar. (This method implements parsing etc, and cannot be
30  * abstracted by this class.)
31  *
32  * Finally, one has to add this template as a friend class upon deriving it.
33  * This is a concession to the already present architecture.
34  *
35  * File paths are determent by the means of KSPaths::writableLocation.
36  */
37 template <class T, typename Component>
39 {
40 public:
41  /**
42  * @brief BinaryListComponent
43  * @param parent a reference to the inheriting child
44  * @param basename the base filename for the binary
45  *
46  * Sets the file extensions to `dat` for text and `bin` for binary.
47  */
48  BinaryListComponent(Component* parent, QString basename);
49 
50  /**
51  * @brief BinaryListComponent
52  * @param parent a reference to the inheriting child
53  * @param basename the base filename for the binary
54  * @param txtExt text data file extension
55  * @param binExt binary data file extension
56  */
57  BinaryListComponent(Component* parent, QString basename, QString txtExt, QString binExt);
58 
59 protected:
60  /**
61  * @brief loadData
62  * @short Calls `loadData(false)`
63  */
64  virtual void loadData();
65 
66  /**
67  * @brief loadData
68  * @short Load the component data from binary (if available) or from text
69  * @param dropBinaryFile whether to drop the current binary (and to recreate it)
70  *
71  * Tip: If you want to reload the data and recreate the binfile, just call
72  * loadData(true).
73  */
74  virtual void loadData(bool dropBinaryFile);
75 
76  /**
77  * @brief loadDataFromBinary
78  * @short Opens the default binfile and calls `loadDataFromBinary([FILE])`
79  */
80  virtual void loadDataFromBinary();
81 
82  /**
83  * @brief loadDataFromBinary
84  * @param binfile the binary file
85  * @short Loads the component data from the given binary.
86  */
87  virtual void loadDataFromBinary(QFile &binfile);
88 
89  /**
90  * @brief writeBinary
91  * @short Opens the default binfile and calls `writeBinary([FILE])`
92  */
93  virtual void writeBinary();
94 
95  /**
96  * @brief writeBinary
97  * @param binfile
98  * @short Writes the component data to the specified binary. (Destructive)
99  */
100  virtual void writeBinary(QFile &binfile);
101 
102  /**
103  * @brief loadDataFromText
104  * @short Load the component data from text.
105  *
106  * This method shall be implemented by those who derive this class.
107  *
108  * This method should load the component data from text by the use of
109  * `addListObject` or similar.
110  */
111  virtual void loadDataFromText() = 0;
112 
113  // TODO: Rename, and integrate it into a wrapper
114  //virtual void updateDataFile() = 0; // legacy from current implementation!
115 
116  /**
117  * @brief dropBinary
118  * @short Removes the binary file.
119  * @return True if operation succeeds
120  */
121  virtual bool dropBinary();
122 
123  /**
124  * @brief dropText
125  * @short Removes the text file.
126  * @return True if operation succeeds
127  */
128  virtual bool dropText();
129 
130  /**
131  * @brief clearData
132  * @short Removes the current component data where necessary.
133  */
134  virtual void clearData();
135 
136  QString filepath_txt;
137  QString filepath_bin;
138 
139 // Don't allow the children to mess with the Binary Version!
140 private:
142  Component* parent;
143 };
144 
145 template<class T, typename Component>
146  BinaryListComponent<T, Component>::BinaryListComponent(Component *parent, QString basename) : BinaryListComponent<T, Component>(parent, basename, "dat", "bin") {}
147 
148 template<class T, typename Component>
149  BinaryListComponent<T, Component>::BinaryListComponent(Component *parent, QString basename, QString txtExt, QString binExt) : parent { parent }
150 {
151  filepath_bin = QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath(basename + '.' + binExt);
152  filepath_txt = QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath(basename + '.' + txtExt);
153 }
154 
155 template<class T, typename Component>
157 {
158  loadData(false);
159 }
160 
161 template<class T, typename Component>
163 {
164  // Clear old Stuff (in case of reload)
165  clearData();
166 
167  // Drop Binary file for a fresh reload
168  if(dropBinaryFile)
169  dropBinary();
170 
171  QFile binfile(filepath_bin);
172  if (binfile.exists()) {
173  loadDataFromBinary(binfile);
174  } else {
175  loadDataFromText();
176  writeBinary(binfile);
177  }
178 }
179 
180 template<class T, typename Component>
182 {
183  QFile binfile(filepath_bin);
184  loadDataFromBinary(binfile);
185 }
186 
187 template<class T, typename Component>
189 {
190  // Open our binary file and create a Stream
191  if (binfile.open(QIODevice::ReadOnly))
192  {
193  QDataStream in(&binfile);
194 
195  // Use the specified binary version
196  // TODO: Place this into the config
197  in.setVersion(binversion);
199 
200  while(!in.atEnd()){
201  T *new_object = nullptr;
202  in >> new_object;
203 
204  parent->appendListObject(new_object);
205  // Add name to the list of object names
206  parent->objectNames(T::TYPE).append(new_object->name());
207  parent->objectLists(T::TYPE).append(QPair<QString, const SkyObject *>(new_object->name(), new_object));
208  }
209  binfile.close();
210  }
211  else qWarning() << "Failed loading binary data from" << binfile.fileName();
212 }
213 
214 template<class T, typename Component>
216 {
217  QFile binfile(filepath_bin);
218  writeBinary(binfile);
219 }
220 
221 template<class T, typename Component>
223 {
224  // Open our file and create a stream
226  QDataStream out(&binfile);
227  out.setVersion(binversion);
229 
230  // Now just dump out everything
231  for(auto object : parent->m_ObjectList){
232  out << *((T*)object);
233  }
234 
235  binfile.close();
236 }
237 
238 template<class T, typename Component>
240 {
241  return QFile::remove(filepath_bin);
242 }
243 
244 template<class T, typename Component>
246 {
247  return QFile::remove(filepath_txt);
248 }
249 
250 template<class T, typename Component>
252 {
253  // Clear lists
254  qDeleteAll(parent->m_ObjectList);
255  parent->m_ObjectList.clear();
256  parent->m_ObjectHash.clear();
257 
258  parent->objectLists(T::TYPE).clear();
259  parent->objectNames(T::TYPE).clear();
260 }
void setVersion(int v)
bool atEnd() const const
bool remove()
virtual bool open(QIODevice::OpenMode mode) override
provides functionality for loading the component data from Binary
virtual void writeBinary()
writeBinary
virtual void loadDataFromText()=0
loadDataFromText
bool exists() const const
virtual QString fileName() const const override
virtual bool dropText()
dropText
virtual bool dropBinary()
dropBinary
BinaryListComponent(Component *parent, QString basename)
BinaryListComponent.
virtual void loadData()
loadData
virtual void loadDataFromBinary()
loadDataFromBinary
virtual void close() override
QString filePath(const QString &fileName) const const
virtual void clearData()
clearData
void setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 8 2022 04:13:18 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.