Skip to content

Commit

Permalink
function to add plugin from repo url
Browse files Browse the repository at this point in the history
  • Loading branch information
research11111 committed Oct 27, 2023
1 parent cf4af7d commit cad72f3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
60 changes: 59 additions & 1 deletion avogadro/qtplugins/plugindownloader/pluginmanagerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void setRawHeaders(QNetworkRequest* request)
PluginManagerWidget::PluginManagerWidget(QWidget* parent)
: QDialog(parent), m_ui(new Ui::PluginManagerWidget)
{

m_filePath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
m_NetworkAccessManager = new QNetworkAccessManager(this);
Expand Down Expand Up @@ -100,7 +101,9 @@ PluginManagerWidget::PluginManagerWidget(QWidget* parent)
}
}
void PluginManagerWidget::addPluginLocationClicked() {
qDebug() << "Should show a popup that create then a json object";
QString pluginLocationUrl = m_ui->pluginLocationUrl->text();
qDebug() << "Should show a popup that create then a json object: " << pluginLocationUrl;

}
void PluginManagerWidget::refreshPluginsListClicked() {
fetchPluginsList();
Expand Down Expand Up @@ -540,4 +543,59 @@ void PluginManagerWidget::appendToPluginsJsonFile(const QJsonObject &newPlugin)
}
}

void PluginManagerWidget::addPluginFromGithubUrl(QString url) {
std::regex pattern(R"(https://github\.com/([^/]+)/([^/]+)/?)");
std::smatch match;

std::string user;
std::string project;
const std::string url_str = url.toUtf8().constData();
if ( std::regex_match(url_str,match,pattern) ) {
user = match[1];
project = match[2];
} else {
QMessageBox::information(nullptr,"Wrong url","Url given seem to have the wrong format must be https://github.com/user/project/");
return;
}

QString realUrl = QString::fromStdString("https://raw.githubusercontent.com/" + user + "/" + project + "/master/plugin.json");
QNetworkRequest request;
setRawHeaders(&request);
request.setUrl(realUrl);

m_reply = m_NetworkAccessManager->get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(addPluginFromGithubUrlResult()));
}
void PluginManagerWidget::addPluginFromGithubUrlResult() {
if ( m_reply->error() != QNetworkReply::NoError) {
QMessageBox::warning(nullptr,"Error","Unable to fetch plugin.json");
return;
}
QByteArray data = m_reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
QJsonObject jsonObj = jsonDoc.object();

QJsonObject plugin;
plugin["name"] = jsonObj["name"];
plugin["type"] = jsonObj["type"];
plugin["repo"] = jsonObj["url"];
plugin["description"] = jsonObj["description"];
plugin["updated_at"] = "2021-05-22T13:50:05Z";
plugin["branch"] = "master";
plugin["has_release"] = false;
plugin["release_version"] = "N/A";
std::regex pattern(R"(https://github\.com/([^/]+)/([^/]+)/?)");
std::smatch match;

const std::string json_url_str = jsonObj["url"].toString().toStdString();
if ( std::regex_match(json_url_str,match,pattern) ) {
std::string user = match[1];
std::string project = match[2];
plugin["zipball_url"] = QString::fromStdString("https://api.github.com/repos/" + user + "/" + project + "/zipball/master");
}
plugin["type"] = jsonObj["type"];

appendToPluginsJsonFile(plugin);
}

} // namespace Avogadro
8 changes: 7 additions & 1 deletion avogadro/qtplugins/plugindownloader/pluginmanagerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QFile>
#include <QtWidgets/QMessageBox>


#include <nlohmann/json.hpp>
#include <regex>
#include <string>
#include <iostream>

class QNetworkAccessManager;
class QNetworkReply;
Expand Down Expand Up @@ -60,7 +65,8 @@ public slots:
QString getPluginsListFilePath();
void addPluginLocationClicked();
void refreshPluginsListClicked();

void addPluginFromGithubUrl(QString url);
void addPluginFromGithubUrlResult();

private:
struct repo
Expand Down
21 changes: 16 additions & 5 deletions avogadro/qtplugins/plugindownloader/pluginmanagerwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="addPluginLocation">
<property name="text">
<string>Add plugin location</string>
</property>
</widget>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="pluginLocationUrl">
<property name="text">
<string>https://github.com/user/project/</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addPluginLocation">
<property name="text">
<string>Add plugin in given github repo</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QComboBox" name="installMethod">
Expand Down

0 comments on commit cad72f3

Please sign in to comment.