LibrePCB Developers Documentation
workspacelibrarydb.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_WORKSPACELIBRARYDB_H
21 #define LIBREPCB_CORE_WORKSPACELIBRARYDB_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/filepath.h"
27 #include "../types/uuid.h"
28 #include "../types/version.h"
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 class QSqlQuery;
36 
37 namespace librepcb {
38 
39 class Component;
40 class ComponentCategory;
41 class Device;
42 class Package;
43 class PackageCategory;
44 class SQLiteDatabase;
45 class Symbol;
46 class WorkspaceLibraryScanner;
47 
48 /*******************************************************************************
49  * Class WorkspaceLibraryDb
50  ******************************************************************************/
51 
55 class WorkspaceLibraryDb final : public QObject {
56  Q_OBJECT
57 
58 public:
59  // Constructors / Destructor
60  WorkspaceLibraryDb() = delete;
61  WorkspaceLibraryDb(const WorkspaceLibraryDb& other) = delete;
62 
71  explicit WorkspaceLibraryDb(const FilePath& librariesPath);
72  ~WorkspaceLibraryDb() noexcept;
73 
74  // Getters
75 
81  const FilePath& getFilePath() const noexcept { return mFilePath; }
82 
88  bool isScanInProgress() const noexcept {
89  return getScanProgressPercent() < 100;
90  }
91 
97  int getScanProgressPercent() const noexcept;
98 
110  template <typename ElementType>
111  QMultiMap<Version, FilePath> getAll(
112  const tl::optional<Uuid>& uuid = tl::nullopt,
113  const FilePath& lib = FilePath()) const {
114  return getAll(getTable<ElementType>(), uuid, lib);
115  }
116 
126  template <typename ElementType>
127  FilePath getLatest(const Uuid& uuid) const {
128  return getLatestVersionFilePath(getAll<ElementType>(uuid));
129  }
130 
140  template <typename ElementType>
141  QList<Uuid> find(const QString& keyword) const {
142  return find(getTable<ElementType>(), keyword);
143  }
144 
165  template <typename ElementType>
166  bool getTranslations(const FilePath& elemDir, const QStringList& localeOrder,
167  QString* name = nullptr, QString* description = nullptr,
168  QString* keywords = nullptr) const {
169  return getTranslations(getTable<ElementType>(), elemDir, localeOrder, name,
170  description, keywords);
171  }
172 
189  template <typename ElementType>
190  bool getMetadata(const FilePath elemDir, Uuid* uuid = nullptr,
191  Version* version = nullptr,
192  bool* deprecated = nullptr) const {
193  return getMetadata(getTable<ElementType>(), elemDir, uuid, version,
194  deprecated);
195  }
196 
207  bool getLibraryMetadata(const FilePath libDir, QPixmap* icon = nullptr) const;
208 
221  template <typename ElementType>
222  bool getCategoryMetadata(const FilePath catDir,
223  tl::optional<Uuid>* parent = nullptr) const {
224  static_assert(std::is_same<ElementType, ComponentCategory>::value ||
225  std::is_same<ElementType, PackageCategory>::value,
226  "Unsupported ElementType");
227  return getCategoryMetadata(getTable<ElementType>(), catDir, parent);
228  }
229 
242  bool getDeviceMetadata(const FilePath& devDir, Uuid* cmpUuid = nullptr,
243  Uuid* pkgUuid = nullptr) const;
244 
258  template <typename ElementType>
259  QSet<Uuid> getChilds(const tl::optional<Uuid>& parent) const {
260  static_assert(std::is_same<ElementType, ComponentCategory>::value ||
261  std::is_same<ElementType, PackageCategory>::value,
262  "Unsupported ElementType");
263  return getChilds(getTable<ElementType>(), parent);
264  }
265 
283  template <typename ElementType>
284  QSet<Uuid> getByCategory(const tl::optional<Uuid>& category,
285  int limit = -1) const {
286  static_assert(std::is_same<ElementType, Symbol>::value ||
287  std::is_same<ElementType, Package>::value ||
288  std::is_same<ElementType, Component>::value ||
289  std::is_same<ElementType, Device>::value,
290  "Unsupported ElementType");
291  return getByCategory(getTable<ElementType>(),
292  getCategoryTable<ElementType>(), category, limit);
293  }
294 
302  QSet<Uuid> getComponentDevices(const Uuid& component) const;
303 
304  // General Methods
305 
309  void startLibraryRescan() noexcept;
310 
311  // Operator Overloadings
312  WorkspaceLibraryDb& operator=(const WorkspaceLibraryDb& rhs) = delete;
313 
314 signals:
315  void scanStarted();
316  void scanLibraryListUpdated(int libraryCount);
317  void scanProgressUpdate(int percent);
318  void scanSucceeded(int elementCount);
319  void scanFailed(QString errorMsg);
320  void scanFinished();
321 
322 private:
323  // Private Methods
324  QMultiMap<Version, FilePath> getAll(const QString& elementsTable,
325  const tl::optional<Uuid>& uuid,
326  const FilePath& lib) const;
328  const QMultiMap<Version, FilePath>& list) const noexcept;
329  QList<Uuid> find(const QString& elementsTable, const QString& keyword) const;
330  bool getTranslations(const QString& elementsTable, const FilePath& elemDir,
331  const QStringList& localeOrder, QString* name,
332  QString* description, QString* keywords) const;
333  bool getMetadata(const QString& elementsTable, const FilePath elemDir,
334  Uuid* uuid, Version* version, bool* deprecated) const;
335  bool getCategoryMetadata(const QString& categoriesTable,
336  const FilePath catDir,
337  tl::optional<Uuid>* parent) const;
338  QSet<Uuid> getChilds(const QString& categoriesTable,
339  const tl::optional<Uuid>& categoryUuid) const;
340  QSet<Uuid> getByCategory(const QString& elementsTable,
341  const QString& categoryTable,
342  const tl::optional<Uuid>& category, int limit) const;
343  static QSet<Uuid> getUuidSet(QSqlQuery& query);
344  int getDbVersion() const noexcept;
345  template <typename ElementType>
346  static QString getTable() noexcept;
347  template <typename ElementType>
348  static QString getCategoryTable() noexcept;
349 
350  // Attributes
353  QScopedPointer<SQLiteDatabase> mDb;
354  QScopedPointer<WorkspaceLibraryScanner> mLibraryScanner;
355 
356  // Constants
357  static const int sCurrentDbVersion = 3;
358 };
359 
360 /*******************************************************************************
361  * End of File
362  ******************************************************************************/
363 
364 } // namespace librepcb
365 
366 #endif
static QSet< Uuid > getUuidSet(QSqlQuery &query)
Definition: workspacelibrarydb.cpp:414
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
QList< Uuid > find(const QString &keyword) const
Find elements by keyword.
Definition: workspacelibrarydb.h:141
QSet< Uuid > getComponentDevices(const Uuid &component) const
Get all devices of a specific component.
Definition: workspacelibrarydb.cpp:153
static const int sCurrentDbVersion
Definition: workspacelibrarydb.h:357
void scanLibraryListUpdated(int libraryCount)
static QString getCategoryTable() noexcept
Definition: workspacelibrarydb.cpp:456
Definition: airwiresbuilder.cpp:34
void scanFailed(QString errorMsg)
int getDbVersion() const noexcept
Definition: workspacelibrarydb.cpp:422
const FilePath mFilePath
Path to the SQLite database file.
Definition: workspacelibrarydb.h:352
QSet< Uuid > getByCategory(const tl::optional< Uuid > &category, int limit=-1) const
Get elements of a specific category.
Definition: workspacelibrarydb.h:284
const FilePath & getFilePath() const noexcept
Get the file path of the SQLite database.
Definition: workspacelibrarydb.h:81
void scanSucceeded(int elementCount)
QMultiMap< Version, FilePath > getAll(const tl::optional< Uuid > &uuid=tl::nullopt, const FilePath &lib=FilePath()) const
Get elements, optionally matching some criteria.
Definition: workspacelibrarydb.h:111
bool isScanInProgress() const noexcept
Check if there is currently a library scan in progress.
Definition: workspacelibrarydb.h:88
QScopedPointer< WorkspaceLibraryScanner > mLibraryScanner
Definition: workspacelibrarydb.h:354
~WorkspaceLibraryDb() noexcept
Definition: workspacelibrarydb.cpp:95
static QString getTable() noexcept
Definition: workspacelibrarydb.cpp:442
bool getCategoryMetadata(const FilePath catDir, tl::optional< Uuid > *parent=nullptr) const
Get additional metadata of a specific category.
Definition: workspacelibrarydb.h:222
QScopedPointer< SQLiteDatabase > mDb
The SQLite database.
Definition: workspacelibrarydb.h:353
void scanProgressUpdate(int percent)
bool getLibraryMetadata(const FilePath libDir, QPixmap *icon=nullptr) const
Get additional metadata of a specific library.
Definition: workspacelibrarydb.cpp:106
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
const FilePath mLibrariesPath
Path to workspace libraries directory.
Definition: workspacelibrarydb.h:351
bool getMetadata(const FilePath elemDir, Uuid *uuid=nullptr, Version *version=nullptr, bool *deprecated=nullptr) const
Get metadata of a specific element.
Definition: workspacelibrarydb.h:190
int getScanProgressPercent() const noexcept
Get the current progress of the library rescan.
Definition: workspacelibrarydb.cpp:102
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
bool getDeviceMetadata(const FilePath &devDir, Uuid *cmpUuid=nullptr, Uuid *pkgUuid=nullptr) const
Get additional metadata of a specific device.
Definition: workspacelibrarydb.cpp:126
FilePath getLatest(const Uuid &uuid) const
Get an element of a specific UUID and the highest version.
Definition: workspacelibrarydb.h:127
bool getTranslations(const FilePath &elemDir, const QStringList &localeOrder, QString *name=nullptr, QString *description=nullptr, QString *keywords=nullptr) const
Get translations of a specific element.
Definition: workspacelibrarydb.h:166
FilePath getLatestVersionFilePath(const QMultiMap< Version, FilePath > &list) const noexcept
Definition: workspacelibrarydb.cpp:225
WorkspaceLibraryDb & operator=(const WorkspaceLibraryDb &rhs)=delete
void startLibraryRescan() noexcept
Rescan the whole library directory and update the SQLite database.
Definition: workspacelibrarydb.cpp:168
The WorkspaceLibraryDb class.
Definition: workspacelibrarydb.h:55
QSet< Uuid > getChilds(const tl::optional< Uuid > &parent) const
Get children categories of a specific category.
Definition: workspacelibrarydb.h:259