LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 <condition_variable>
29
30#include <QtCore>
31
32#include <memory>
33#include <mutex>
34
35/*******************************************************************************
36 * Namespace / Forward Declarations
37 ******************************************************************************/
38namespace librepcb {
39
40class Library;
41class SQLiteDatabase;
42class WorkspaceLibraryDbWriter;
43
44/*******************************************************************************
45 * Class WorkspaceLibraryScanner
46 ******************************************************************************/
47
55class WorkspaceLibraryScanner final : public QThread {
56 Q_OBJECT
57
58 enum class State {
59 Idle,
64 };
65
66public:
67 // Constructors / Destructor
68 WorkspaceLibraryScanner(const FilePath& librariesPath,
69 const FilePath& dbFilePath) noexcept;
71 ~WorkspaceLibraryScanner() noexcept;
72
73 // Getters
74 int getProgressPercent() const noexcept { return mLastProgressPercent; }
75
76 // General Methods
77 void startScan() noexcept;
78 bool cancelScan() noexcept;
79
80 // Operator Overloadings
82 delete;
83
84signals:
86 void scanLibraryListUpdated(int libraryCount);
87 void scanProgressUpdate(int percent);
88 void scanSucceeded(int elementCount);
89 void scanFailed(QString errorMsg);
91 void scanInProgressChanged(bool inProgress);
92
93private: // Methods
94 void run() noexcept override;
95 void scan() noexcept;
96 void getLibrariesOfDirectory(const QString& root,
97 QList<std::shared_ptr<Library>>& libs) noexcept;
98
99 QHash<FilePath, int> updateLibraries(
101 const QList<std::shared_ptr<Library>>& libs);
102 template <typename ElementType>
103 int addElementsToDb(WorkspaceLibraryDbWriter& writer, const FilePath& libPath,
104 const QStringList& dirs, int libId);
105 template <typename ElementType>
106 int addElementToDb(WorkspaceLibraryDbWriter& writer, int libId,
107 const ElementType& element);
108 template <typename ElementType>
109 void addTranslationsToDb(WorkspaceLibraryDbWriter& writer, int elementId,
110 const ElementType& element);
111 template <typename ElementType>
112 void addToCategories(WorkspaceLibraryDbWriter& writer, int elementId,
113 const ElementType& element);
114 template <typename ElementType>
115 void addResourcesToDb(WorkspaceLibraryDbWriter& writer, int elementId,
116 const ElementType& element);
117 template <typename ElementType>
118 std::unique_ptr<ElementType> openAndMigrate(const FilePath& fp);
119 bool abortRequested() const noexcept;
120
121private: // Data
124 mutable std::mutex mMutex;
126 std::condition_variable mStateCV;
128};
129
130/*******************************************************************************
131 * End of File
132 ******************************************************************************/
133
134} // namespace librepcb
135
136#endif
This class represents absolute, well-formatted paths to files or directories.
Definition filepath.h:127
The Library class represents a library directory.
Definition library.h:46
The SQLiteDatabase class.
Definition sqlitedatabase.h:43
Database write functions for librepcb::WorkspaceLibraryDb.
Definition workspacelibrarydbwriter.h:58
The WorkspaceLibraryScanner class.
Definition workspacelibraryscanner.h:55
bool abortRequested() const noexcept
Definition workspacelibraryscanner.cpp:510
void run() noexcept override
Definition workspacelibraryscanner.cpp:122
~WorkspaceLibraryScanner() noexcept
Definition workspacelibraryscanner.cpp:68
void scanProgressUpdate(int percent)
int addElementsToDb(WorkspaceLibraryDbWriter &writer, const FilePath &libPath, const QStringList &dirs, int libId)
Definition workspacelibraryscanner.cpp:324
std::unique_ptr< ElementType > openAndMigrate(const FilePath &fp)
Definition workspacelibraryscanner.cpp:487
bool cancelScan() noexcept
Definition workspacelibraryscanner.cpp:101
State mState
Protected by mMutex.
Definition workspacelibraryscanner.h:125
void scanFailed(QString errorMsg)
State
Definition workspacelibraryscanner.h:58
void scanInProgressChanged(bool inProgress)
void scanSucceeded(int elementCount)
std::condition_variable mStateCV
To notify about mState changes.
Definition workspacelibraryscanner.h:126
void scanLibraryListUpdated(int libraryCount)
void startScan() noexcept
Definition workspacelibraryscanner.cpp:91
void getLibrariesOfDirectory(const QString &root, QList< std::shared_ptr< Library > > &libs) noexcept
Definition workspacelibraryscanner.cpp:245
void scan() noexcept
Definition workspacelibraryscanner.cpp:149
int addElementToDb(WorkspaceLibraryDbWriter &writer, int libId, const ElementType &element)
Definition workspacelibraryscanner.cpp:347
const FilePath mDbFilePath
Path to the SQLite database file.
Definition workspacelibraryscanner.h:123
std::mutex mMutex
Mutex to protect mState.
Definition workspacelibraryscanner.h:124
void addTranslationsToDb(WorkspaceLibraryDbWriter &writer, int elementId, const ElementType &element)
Definition workspacelibraryscanner.cpp:456
void addToCategories(WorkspaceLibraryDbWriter &writer, int elementId, const ElementType &element)
Definition workspacelibraryscanner.cpp:468
int getProgressPercent() const noexcept
Definition workspacelibraryscanner.h:74
WorkspaceLibraryScanner(const WorkspaceLibraryScanner &other)=delete
QHash< FilePath, int > updateLibraries(SQLiteDatabase &db, WorkspaceLibraryDbWriter &writer, const QList< std::shared_ptr< Library > > &libs)
Definition workspacelibraryscanner.cpp:266
void addResourcesToDb(WorkspaceLibraryDbWriter &writer, int elementId, const ElementType &element)
Definition workspacelibraryscanner.cpp:477
int mLastProgressPercent
Definition workspacelibraryscanner.h:127
const FilePath mLibrariesPath
Path to workspace libraries directory.
Definition workspacelibraryscanner.h:122
Definition occmodel.cpp:77
Definition uuid.h:186