LibrePCB Developers Documentation
workspacelibraryscanner.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_WORKSPACELIBRARYSCANNER_H
21 #define LIBREPCB_CORE_WORKSPACELIBRARYSCANNER_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/filepath.h"
27 
28 #include <QtCore>
29 
30 #include <memory>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 class Library;
38 class SQLiteDatabase;
39 class WorkspaceLibraryDbWriter;
40 
41 /*******************************************************************************
42  * Class WorkspaceLibraryScanner
43  ******************************************************************************/
44 
52 class WorkspaceLibraryScanner final : public QThread {
53  Q_OBJECT
54 
55 public:
56  // Constructors / Destructor
57  WorkspaceLibraryScanner(const FilePath& librariesPath,
58  const FilePath& dbFilePath) noexcept;
59  WorkspaceLibraryScanner(const WorkspaceLibraryScanner& other) = delete;
60  ~WorkspaceLibraryScanner() noexcept;
61 
62  // Getters
63  int getProgressPercent() const noexcept { return mLastProgressPercent; }
64 
65  // General Methods
66  void startScan() noexcept;
67 
68  // Operator Overloadings
70  delete;
71 
72 signals:
73  void scanStarted();
74  void scanLibraryListUpdated(int libraryCount);
75  void scanProgressUpdate(int percent);
76  void scanSucceeded(int elementCount);
77  void scanFailed(QString errorMsg);
78  void scanFinished();
79 
80 private: // Methods
81  void run() noexcept override;
82  void scan() noexcept;
83  void getLibrariesOfDirectory(const QString& root,
84  QList<std::shared_ptr<Library>>& libs) noexcept;
85 
86  QHash<FilePath, int> updateLibraries(
88  const QList<std::shared_ptr<Library>>& libs);
89  template <typename ElementType>
90  int addElementsToDb(WorkspaceLibraryDbWriter& writer, const FilePath& libPath,
91  const QStringList& dirs, int libId);
92  template <typename ElementType>
93  int addElementToDb(WorkspaceLibraryDbWriter& writer, int libId,
94  const ElementType& element);
95  template <typename ElementType>
96  void addTranslationsToDb(WorkspaceLibraryDbWriter& writer, int elementId,
97  const ElementType& element);
98  template <typename ElementType>
99  void addToCategories(WorkspaceLibraryDbWriter& writer, int elementId,
100  const ElementType& element);
101  template <typename ElementType>
102  std::unique_ptr<ElementType> openAndMigrate(const FilePath& fp);
103 
104 private: // Data
107  QSemaphore mSemaphore;
108  volatile bool mAbort;
110 };
111 
112 /*******************************************************************************
113  * End of File
114  ******************************************************************************/
115 
116 } // namespace librepcb
117 
118 #endif
int getProgressPercent() const noexcept
Definition: workspacelibraryscanner.h:63
~WorkspaceLibraryScanner() noexcept
Definition: workspacelibraryscanner.cpp:63
WorkspaceLibraryScanner & operator=(const WorkspaceLibraryScanner &rhs)=delete
QHash< FilePath, int > updateLibraries(SQLiteDatabase &db, WorkspaceLibraryDbWriter &writer, const QList< std::shared_ptr< Library >> &libs)
Definition: workspacelibraryscanner.cpp:209
void addTranslationsToDb(WorkspaceLibraryDbWriter &writer, int elementId, const ElementType &element)
Definition: workspacelibraryscanner.cpp:353
int mLastProgressPercent
Definition: workspacelibraryscanner.h:109
WorkspaceLibraryScanner(const FilePath &librariesPath, const FilePath &dbFilePath) noexcept
Definition: workspacelibraryscanner.cpp:49
Definition: occmodel.cpp:76
QSemaphore mSemaphore
Definition: workspacelibraryscanner.h:107
void startScan() noexcept
Definition: workspacelibraryscanner.cpp:80
int addElementsToDb(WorkspaceLibraryDbWriter &writer, const FilePath &libPath, const QStringList &dirs, int libId)
Definition: workspacelibraryscanner.cpp:267
Database write functions for librepcb::WorkspaceLibraryDb.
Definition: workspacelibrarydbwriter.h:57
const FilePath mLibrariesPath
Path to workspace libraries directory.
Definition: workspacelibraryscanner.h:105
The WorkspaceLibraryScanner class.
Definition: workspacelibraryscanner.h:52
void scanProgressUpdate(int percent)
void scan() noexcept
Definition: workspacelibraryscanner.cpp:103
void run() noexcept override
Definition: workspacelibraryscanner.cpp:88
int addElementToDb(WorkspaceLibraryDbWriter &writer, int libId, const ElementType &element)
Definition: workspacelibraryscanner.cpp:290
void scanFailed(QString errorMsg)
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
The SQLiteDatabase class.
Definition: sqlitedatabase.h:43
volatile bool mAbort
Definition: workspacelibraryscanner.h:108
const FilePath mDbFilePath
Path to the SQLite database file.
Definition: workspacelibraryscanner.h:106
void addToCategories(WorkspaceLibraryDbWriter &writer, int elementId, const ElementType &element)
Definition: workspacelibraryscanner.cpp:365
void scanLibraryListUpdated(int libraryCount)
std::unique_ptr< ElementType > openAndMigrate(const FilePath &fp)
Definition: workspacelibraryscanner.cpp:374
void scanSucceeded(int elementCount)
void getLibrariesOfDirectory(const QString &root, QList< std::shared_ptr< Library >> &libs) noexcept
Definition: workspacelibraryscanner.cpp:188