2
2
#include < iostream>
3
3
#include < utility>
4
4
#include < vector>
5
+ #include < deque>
5
6
#include < filesystem>
6
7
#include < thread>
7
8
#include < chrono>
11
12
#include < QApplication>
12
13
#include < QCoreApplication>
13
14
#include < QFontDatabase>
14
- #include < QThread>
15
15
#include < QObject>
16
16
#include < QDialog>
17
17
#include " ui/mainwindow.h"
18
- #include " ui/packageitem.h"
19
- #include " ui/packageinfo.h"
18
+ #include " ui/addrepository.h"
20
19
21
20
// Project globals
22
21
#include " globals.h"
28
27
#include " tools/package.h"
29
28
#include " tools/repo.h"
30
29
31
- const char * repositoryURLs[] = {
30
+ std::deque<std::string> repositoryURLs = {
32
31
" https://p2r3.github.io/spplice-repo/index.json"
33
32
};
34
33
34
+ void addRepository (const std::string url, QVBoxLayout *container) {
35
+
36
+ // Fetch the repository packages
37
+ std::vector<const ToolsPackage::PackageData*> repository = ToolsRepo::fetchRepository (url);
38
+
39
+ // Keep track of added package count to order them properly
40
+ int currPackage = 0 ;
41
+
42
+ for (const ToolsPackage::PackageData *package : repository) {
43
+ // Create PackageItem widget from PackageData
44
+ QWidget *item = ToolsPackage::createPackageItem (package);
45
+ // Add the item to the package list container
46
+ container->insertWidget (currPackage, item);
47
+ currPackage ++;
48
+ // Sleep for a few milliseconds on each package to reduce strain on the network
49
+ std::this_thread::sleep_for (std::chrono::milliseconds (5 ));
50
+ }
51
+
52
+ }
53
+
35
54
int main (int argc, char *argv[]) {
36
55
37
56
try {
@@ -58,126 +77,37 @@ int main (int argc, char *argv[]) {
58
77
// Initialize CURL
59
78
ToolsCURL::init ();
60
79
61
- // Create a vector containing all packages from all repositories
62
- std::vector<const ToolsPackage::PackageData*> allPackages;
63
- // Fetch packages from each repository
64
- for (auto url : repositoryURLs) {
65
- std::vector<const ToolsPackage::PackageData*> repository = ToolsRepo::fetchRepository (url);
66
- allPackages.insert (allPackages.end (), repository.begin (), repository.end ());
67
- }
68
-
69
- // Generate a PackageItem for each package
70
- for (const auto &package : allPackages) {
71
-
72
- // Create the package item widget
73
- QWidget *item = new QWidget;
74
- Ui::PackageItem itemUI;
75
- itemUI.setupUi (item);
76
-
77
- // Set the title and description
78
- itemUI.PackageTitle ->setText (QString::fromStdString (package->title ));
79
- itemUI.PackageDescription ->setText (QString::fromStdString (package->description ));
80
-
81
- // Connect the install button
82
- QPushButton *installButton = itemUI.PackageInstallButton ;
83
- QObject::connect (installButton, &QPushButton::clicked, [installButton, &package]() {
84
-
85
- // Create a thread for asynchronous installation
86
- PackageItemWorker *worker = new PackageItemWorker;
87
- QThread *workerThread = new QThread;
88
- worker->moveToThread (workerThread);
89
-
90
- // Connect the task of installing the package to the worker
91
- QObject::connect (workerThread, &QThread::started, worker, [worker, &package]() {
92
- QMetaObject::invokeMethod (worker, " installPackage" , Q_ARG (const ToolsPackage::PackageData*, package));
93
- });
94
-
95
- // Update the button text based on the installation state
96
- QObject::connect (worker, &PackageItemWorker::installStateUpdate, installButton, [installButton]() {
97
- switch (SPPLICE_INSTALL_STATE) {
98
- case 0 :
99
- installButton->setText (" Install" );
100
- installButton->setStyleSheet (" " );
101
- break ;
102
- case 1 :
103
- installButton->setText (" Installing..." );
104
- installButton->setStyleSheet (" color: #faa81a;" );
105
- break ;
106
- case 2 :
107
- installButton->setText (" Installed" );
108
- installButton->setStyleSheet (" color: #faa81a;" );
109
- break ;
110
- }
111
- });
112
-
113
- // Clean up the thread once it's done
114
- QObject::connect (worker, &PackageItemWorker::packageIconReady, workerThread, &QThread::quit);
115
- QObject::connect (worker, &PackageItemWorker::packageIconReady, worker, &PackageItemWorker::deleteLater);
116
- QObject::connect (workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
117
-
118
- // Start the worker thread
119
- workerThread->start ();
80
+ QVBoxLayout *packageContainer = windowUI.PackageListLayout ;
120
81
121
- });
82
+ // Connect the "Add Repository" button
83
+ QObject::connect (windowUI.TitleButtonR , &QPushButton::clicked, [packageContainer]() {
122
84
123
- // Add the item to the package list container
124
- windowUI.PackageListLayout ->addWidget (item);
85
+ // Create new repository entry dialog
86
+ QDialog *dialog = new QDialog;
87
+ Ui::RepoDialog dialogUI;
88
+ dialogUI.setupUi (dialog);
125
89
126
- // Start a new worker thread for asynchronous icon fetching
127
- PackageItemWorker *worker = new PackageItemWorker;
128
- QThread *workerThread = new QThread;
129
- worker->moveToThread (workerThread);
90
+ QLineEdit *urlTextBox = dialogUI.RepoURL ;
130
91
131
- // Connect the task of fetching the icon to the worker
132
- QSize iconSize = itemUI. PackageIcon -> size ();
133
- QObject::connect (workerThread, &QThread::started, worker, [worker, &package, iconSize]() {
134
- QMetaObject::invokeMethod (worker, " getPackageIcon " , Q_ARG ( const ToolsPackage::PackageData*, package), Q_ARG (QSize, iconSize) );
92
+ // Connect the "OK" button
93
+ QObject::connect (dialogUI. DialogButton , &QPushButton::clicked, [packageContainer, urlTextBox, dialog]() {
94
+ addRepository (urlTextBox-> text (). toStdString (), packageContainer);
95
+ dialog-> hide ( );
135
96
});
136
- QObject::connect (worker, &PackageItemWorker::packageIconResult, itemUI.PackageIcon , &QLabel::setPixmap);
137
-
138
- // Clean up the thread once it's done
139
- QObject::connect (worker, &PackageItemWorker::packageIconReady, workerThread, &QThread::quit);
140
- QObject::connect (worker, &PackageItemWorker::packageIconReady, worker, &PackageItemWorker::deleteLater);
141
- QObject::connect (workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
142
-
143
- // Start the worker thread
144
- workerThread->start ();
145
-
146
- // Connect the "Read more" button
147
- QObject::connect (itemUI.PackageInfoButton , &QPushButton::clicked, [&package]() {
148
-
149
- QDialog *dialog = new QDialog;
150
- Ui::PackageInfo dialogUI;
151
- dialogUI.setupUi (dialog);
152
-
153
- // Set text data (title, author, description)
154
- dialogUI.PackageTitle ->setText (QString::fromStdString (package->title ));
155
- dialogUI.PackageAuthor ->setText (QString::fromStdString (" By " + package->author ));
156
- dialogUI.PackageDescription ->setText (QString::fromStdString (package->description ));
157
-
158
- // Set the icon - assume the image has already been downloaded
159
- size_t imageURLHash = std::hash<std::string>{}(package->icon );
160
- std::filesystem::path imagePath = TEMP_DIR / std::to_string (imageURLHash);
161
-
162
- QSize iconSize = dialogUI.PackageIcon ->size ();
163
-
164
- #ifndef TARGET_WINDOWS
165
- QPixmap iconPixmap = QPixmap (QString::fromStdString (imagePath.string ())).scaled (iconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
166
- #else
167
- QPixmap iconPixmap = QPixmap (QString::fromStdWString (imagePath.wstring ())).scaled (iconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
168
- #endif
169
-
170
- QPixmap iconRoundedPixmap = ToolsQT::getRoundedPixmap (iconPixmap, 10 );
171
- dialogUI.PackageIcon ->setPixmap (iconRoundedPixmap);
172
-
173
- dialog->setWindowTitle (QString::fromStdString (" Details for " + package->title ));
174
- dialog->exec ();
175
97
98
+ // Connect the event of pressing return
99
+ QObject::connect (urlTextBox, &QLineEdit::returnPressed, [packageContainer, urlTextBox, dialog]() {
100
+ addRepository (urlTextBox->text ().toStdString (), packageContainer);
101
+ dialog->hide ();
176
102
});
177
103
178
- // Sleep for a few milliseconds on each package to reduce strain on the network
179
- std::this_thread::sleep_for (std::chrono::milliseconds (5 ));
104
+ dialog->exec ();
105
+
106
+ });
180
107
108
+ // Fetch packages from each repository
109
+ for (const std::string &url : repositoryURLs) {
110
+ addRepository (url, packageContainer);
181
111
}
182
112
183
113
// Clean up CURL on program termination
0 commit comments