Skip to content

Commit

Permalink
TemplateSelector: Handle host not available (texstudio-org#3519)
Browse files Browse the repository at this point in the history
  • Loading branch information
octaeder authored Feb 13, 2024
1 parent 90a386d commit a7eb62b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/templateselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ TemplateSelector::TemplateSelector(QString name, QWidget *parent)

TemplateSelector::~TemplateSelector()
{
if (networkManager) {
networkManager->deleteLater();
networkManager=nullptr;
}
}

void TemplateSelector::addResource(AbstractTemplateResource *res)
Expand Down Expand Up @@ -157,7 +161,10 @@ const QNetworkRequest::Attribute tplAttributeItem = static_cast<QNetworkRequest:
*/
void TemplateSelector::makeRequest(QString url, QString path,QTreeWidgetItem *item,bool download)
{

if(!networkManager){
networkManager = new QNetworkAccessManager();
if(!networkManager) return;
}
QString m_url=appendPath(url,path);
if(download){
// check if cached
Expand All @@ -179,6 +186,11 @@ void TemplateSelector::makeRequest(QString url, QString path,QTreeWidgetItem *it
request.setAttribute(tplAttributeItem,QVariant::fromValue(item));
QNetworkReply *reply = networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, &TemplateSelector::onRequestCompleted);
#if QT_VERSION_MAJOR<6
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onRequestError()));
#else
connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), SLOT(onRequestError()));
#endif
}

/*!
Expand All @@ -203,7 +215,6 @@ void TemplateSelector::saveToCache(const QByteArray &data, const QString &path)
}
}


void TemplateSelector::itemExpanded(QTreeWidgetItem* item){
bool populated=item->data(0,PopulatedRole).toBool();
if(populated) return;
Expand Down Expand Up @@ -618,3 +629,15 @@ void TemplateSelector::openTemplateLocation()
}
}

void TemplateSelector::onRequestError()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (!reply) return;

QTreeWidgetItem *rootItem=reply->request().attribute(tplAttributeItem).value<QTreeWidgetItem*>();
if (!rootItem) return;
rootItem->child(0)->setText(0,tr("Repository not found. Network error:%1").arg("\n"+reply->errorString()));

networkManager->deleteLater();
networkManager=nullptr;
}
1 change: 1 addition & 0 deletions src/templateselector.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private slots:
void editTemplateInfo();
void removeTemplate();
void openTemplateLocation();
void onRequestError();

private:
QString orDefault(const QString &val, const QString &defaultIfValEmpty) const
Expand Down

0 comments on commit a7eb62b

Please sign in to comment.