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_WORKSPACE_WORKSPACELIBRARYDB_H
21 #define LIBREPCB_WORKSPACE_WORKSPACELIBRARYDB_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
28 #include <librepcb/common/uuid.h>
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 class Version;
38 class SQLiteDatabase;
39 
40 namespace workspace {
41 
42 class Workspace;
43 class WorkspaceLibraryScanner;
44 
45 /*******************************************************************************
46  * Class WorkspaceLibraryDb
47  ******************************************************************************/
48 
52 class WorkspaceLibraryDb final : public QObject {
53  Q_OBJECT
54 
55 public:
56  // Constructors / Destructor
57  WorkspaceLibraryDb() = delete;
58  WorkspaceLibraryDb(const WorkspaceLibraryDb& other) = delete;
59 
68  explicit WorkspaceLibraryDb(Workspace& ws);
69  ~WorkspaceLibraryDb() noexcept;
70 
71  // Getters: Attributes
72  const FilePath& getFilePath() const noexcept { return mFilePath; }
73 
74  // Getters: Libraries
75  QMultiMap<Version, FilePath> getLibraries() const;
76 
77  // Getters: Library Elements by their UUID
78  QMultiMap<Version, FilePath> getLibraries(const Uuid& uuid) const;
79  QMultiMap<Version, FilePath> getComponentCategories(const Uuid& uuid) const;
80  QMultiMap<Version, FilePath> getPackageCategories(const Uuid& uuid) const;
81  QMultiMap<Version, FilePath> getSymbols(const Uuid& uuid) const;
82  QMultiMap<Version, FilePath> getPackages(const Uuid& uuid) const;
83  QMultiMap<Version, FilePath> getComponents(const Uuid& uuid) const;
84  QMultiMap<Version, FilePath> getDevices(const Uuid& uuid) const;
85 
86  // Getters: Best Match Library Elements by their UUID
87  FilePath getLatestLibrary(const Uuid& uuid) const;
88  FilePath getLatestComponentCategory(const Uuid& uuid) const;
89  FilePath getLatestPackageCategory(const Uuid& uuid) const;
90  FilePath getLatestSymbol(const Uuid& uuid) const;
91  FilePath getLatestPackage(const Uuid& uuid) const;
92  FilePath getLatestComponent(const Uuid& uuid) const;
93  FilePath getLatestDevice(const Uuid& uuid) const;
94 
95  // Getters: Library elements by search keyword
96  template <typename ElementType>
97  QList<Uuid> getElementsBySearchKeyword(const QString& keyword) const;
98 
99  // Getters: Library elements of a specified library
100  template <typename ElementType>
101  QList<FilePath> getLibraryElements(const FilePath& lib) const;
102 
103  // Getters: Element Metadata
104  template <typename ElementType>
105  void getElementTranslations(const FilePath& elemDir,
106  const QStringList& localeOrder,
107  QString* name = nullptr, QString* desc = nullptr,
108  QString* keywords = nullptr) const;
109  template <typename ElementType>
110  void getElementMetadata(const FilePath elemDir, Uuid* uuid = nullptr,
111  Version* version = nullptr) const;
112  void getLibraryMetadata(const FilePath libDir, QPixmap* icon = nullptr) const;
113  void getDeviceMetadata(const FilePath& devDir, Uuid* pkgUuid = nullptr,
114  Uuid* cmpUuid = nullptr) const;
115 
116  // Getters: Special
117  QSet<Uuid> getComponentCategoryChilds(const tl::optional<Uuid>& parent) const;
118  QSet<Uuid> getPackageCategoryChilds(const tl::optional<Uuid>& parent) const;
119  QList<Uuid> getComponentCategoryParents(const Uuid& category) const;
120  QList<Uuid> getPackageCategoryParents(const Uuid& category) const;
121  void getComponentCategoryElementCount(const tl::optional<Uuid>& category,
122  int* categories, int* symbols,
123  int* components, int* devices) const;
124  void getPackageCategoryElementCount(const tl::optional<Uuid>& category,
125  int* categories, int* packages) const;
126  QSet<Uuid> getSymbolsByCategory(const tl::optional<Uuid>& category) const;
127  QSet<Uuid> getPackagesByCategory(const tl::optional<Uuid>& category) const;
128  QSet<Uuid> getComponentsByCategory(const tl::optional<Uuid>& category) const;
129  QSet<Uuid> getDevicesByCategory(const tl::optional<Uuid>& category) const;
130  QSet<Uuid> getDevicesOfComponent(const Uuid& component) const;
131 
132  // General Methods
133 
137  void startLibraryRescan() noexcept;
138 
139  // Operator Overloadings
140  WorkspaceLibraryDb& operator=(const WorkspaceLibraryDb& rhs) = delete;
141 
142 signals:
143 
144  void scanStarted();
145  void scanLibraryListUpdated(int libraryCount);
146  void scanProgressUpdate(int percent);
147  void scanSucceeded(int elementCount);
148  void scanFailed(QString errorMsg);
149  void scanFinished();
150 
151 private:
152  // Private Methods
153  void getElementTranslations(const QString& table, const QString& idRow,
154  const FilePath& elemDir,
155  const QStringList& localeOrder, QString* name,
156  QString* desc, QString* keywords) const;
157  void getElementMetadata(const QString& table, const FilePath elemDir,
158  Uuid* uuid, Version* version) const;
159  QMultiMap<Version, FilePath> getElementFilePathsFromDb(
160  const QString& tablename, const Uuid& uuid) const;
162  const QMultiMap<Version, FilePath>& list) const noexcept;
163  QSet<Uuid> getCategoryChilds(const QString& tablename,
164  const tl::optional<Uuid>& categoryUuid) const;
165  QList<Uuid> getCategoryParents(const QString& tablename,
166  const Uuid& category) const;
167  tl::optional<Uuid> getCategoryParent(const QString& tablename,
168  const Uuid& category) const;
169  int getCategoryChildCount(const QString& tablename,
170  const tl::optional<Uuid>& category) const;
171  int getCategoryElementCount(const QString& tablename,
172  const QString& idrowname,
173  const tl::optional<Uuid>& category) const;
174  QSet<Uuid> getElementsByCategory(
175  const QString& tablename, const QString& idrowname,
176  const tl::optional<Uuid>& categoryUuid) const;
177  QList<Uuid> getElementsBySearchKeyword(const QString& tablename,
178  const QString& idrowname,
179  const QString& keyword) const;
180  int getLibraryId(const FilePath& lib) const;
181  QList<FilePath> getLibraryElements(const FilePath& lib,
182  const QString& tablename) const;
183  void createAllTables();
184  void setDbVersion(int version);
185  int getDbVersion() const noexcept;
186 
187  // Attributes
190  QScopedPointer<SQLiteDatabase> mDb;
191  QScopedPointer<WorkspaceLibraryScanner> mLibraryScanner;
192 
193  // Constants
194  static const int sCurrentDbVersion = 2;
195 };
196 
197 /*******************************************************************************
198  * End of File
199  ******************************************************************************/
200 
201 } // namespace workspace
202 } // namespace librepcb
203 
204 #endif // LIBREPCB_WORKSPACE_WORKSPACELIBRARYDB_H
int getDbVersion() const noexcept
Definition: workspacelibrarydb.cpp:972
void startLibraryRescan() noexcept
Rescan the whole library directory and update the SQLite database.
Definition: workspacelibrarydb.cpp:518
QList< Uuid > getCategoryParents(const QString &tablename, const Uuid &category) const
Definition: workspacelibrarydb.cpp:624
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
FilePath mFilePath
path to the SQLite database
Definition: workspacelibrarydb.h:189
void getPackageCategoryElementCount(const tl::optional< Uuid > &category, int *categories, int *packages) const
Definition: workspacelibrarydb.cpp:470
QSet< Uuid > getComponentCategoryChilds(const tl::optional< Uuid > &parent) const
Definition: workspacelibrarydb.cpp:432
Workspace & mWorkspace
Definition: workspacelibrarydb.h:188
QSet< Uuid > getSymbolsByCategory(const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:480
QScopedPointer< SQLiteDatabase > mDb
the SQLite database
Definition: workspacelibrarydb.h:190
The Workspace class represents a workspace with all its data (library, projects, settings, ...)
Definition: workspace.h:62
QSet< Uuid > getCategoryChilds(const QString &tablename, const tl::optional< Uuid > &categoryUuid) const
Definition: workspacelibrarydb.cpp:609
QScopedPointer< WorkspaceLibraryScanner > mLibraryScanner
Definition: workspacelibrarydb.h:191
QList< FilePath > getLibraryElements(const FilePath &lib) const
FilePath getLatestComponent(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:186
void scanLibraryListUpdated(int libraryCount)
const FilePath & getFilePath() const noexcept
Definition: workspacelibrarydb.h:72
Definition: alignment.cpp:30
FilePath getLatestPackage(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:182
QList< Uuid > getPackageCategoryParents(const Uuid &category) const
Definition: workspacelibrarydb.cpp:447
int getCategoryElementCount(const QString &tablename, const QString &idrowname, const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:672
void scanSucceeded(int elementCount)
QSet< Uuid > getElementsByCategory(const QString &tablename, const QString &idrowname, const tl::optional< Uuid > &categoryUuid) const
Definition: workspacelibrarydb.cpp:683
QSet< Uuid > getPackageCategoryChilds(const tl::optional< Uuid > &parent) const
Definition: workspacelibrarydb.cpp:437
QMultiMap< Version, FilePath > getPackageCategories(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:136
WorkspaceLibraryDb & operator=(const WorkspaceLibraryDb &rhs)=delete
FilePath getLatestDevice(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:190
void getDeviceMetadata(const FilePath &devDir, Uuid *pkgUuid=nullptr, Uuid *cmpUuid=nullptr) const
Definition: workspacelibrarydb.cpp:406
QList< Uuid > getElementsBySearchKeyword(const QString &keyword) const
QMultiMap< Version, FilePath > getPackages(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:146
int getLibraryId(const FilePath &lib) const
Definition: workspacelibrarydb.cpp:724
QList< Uuid > getComponentCategoryParents(const Uuid &category) const
Definition: workspacelibrarydb.cpp:442
void getLibraryMetadata(const FilePath libDir, QPixmap *icon=nullptr) const
Definition: workspacelibrarydb.cpp:387
FilePath getLatestComponentCategory(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:169
tl::optional< Uuid > getCategoryParent(const QString &tablename, const Uuid &category) const
Definition: workspacelibrarydb.cpp:641
void getElementTranslations(const FilePath &elemDir, const QStringList &localeOrder, QString *name=nullptr, QString *desc=nullptr, QString *keywords=nullptr) const
static const int sCurrentDbVersion
Definition: workspacelibrarydb.h:194
FilePath getLatestVersionFilePath(const QMultiMap< Version, FilePath > &list) const noexcept
Definition: workspacelibrarydb.cpp:601
QMultiMap< Version, FilePath > getDevices(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:156
FilePath getLatestSymbol(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:178
QMultiMap< Version, FilePath > getComponentCategories(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:131
~WorkspaceLibraryDb() noexcept
Definition: workspacelibrarydb.cpp:95
int getCategoryChildCount(const QString &tablename, const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:664
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
void createAllTables()
Definition: workspacelibrarydb.cpp:768
QSet< Uuid > getPackagesByCategory(const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:485
QSet< Uuid > getDevicesOfComponent(const Uuid &component) const
Definition: workspacelibrarydb.cpp:500
QMultiMap< Version, FilePath > getElementFilePathsFromDb(const QString &tablename, const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:579
QSet< Uuid > getDevicesByCategory(const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:495
FilePath getLatestPackageCategory(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:174
QSet< Uuid > getComponentsByCategory(const tl::optional< Uuid > &category) const
Definition: workspacelibrarydb.cpp:490
QMultiMap< Version, FilePath > getComponents(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:151
FilePath getLatestLibrary(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:165
void getComponentCategoryElementCount(const tl::optional< Uuid > &category, int *categories, int *symbols, int *components, int *devices) const
Definition: workspacelibrarydb.cpp:452
The WorkspaceLibraryDb class.
Definition: workspacelibrarydb.h:52
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
void setDbVersion(int version)
Definition: workspacelibrarydb.cpp:990
void getElementMetadata(const FilePath elemDir, Uuid *uuid=nullptr, Version *version=nullptr) const
QMultiMap< Version, FilePath > getLibraries() const
Definition: workspacelibrarydb.cpp:102
QMultiMap< Version, FilePath > getSymbols(const Uuid &uuid) const
Definition: workspacelibrarydb.cpp:141