diff --git a/src/lay/laySaltGrain.cc b/src/lay/laySaltGrain.cc index d39664ad5..d1a93d396 100644 --- a/src/lay/laySaltGrain.cc +++ b/src/lay/laySaltGrain.cc @@ -29,6 +29,7 @@ #include #include #include +#include namespace lay { @@ -109,6 +110,38 @@ SaltGrain::set_doc_url (const std::string &u) m_doc_url = u; } +std::string +SaltGrain::eff_doc_url () const +{ + if (m_doc_url.empty ()) { + return std::string (); + } + + QUrl url (tl::to_qstring (m_doc_url)); + if (! url.scheme ().isEmpty ()) { + return m_doc_url; + } + + if (! path ().empty ()) { + + // if the URL is a relative URL, make it absolute relative to the grain's installation directory + QFileInfo fi (url.toLocalFile ()); + if (! fi.isAbsolute ()) { + url = QUrl::fromLocalFile (QDir (tl::to_qstring (path ())).absoluteFilePath (fi.filePath ())); + } + url.setScheme (tl::to_qstring ("file")); + return tl::to_string (url.toString ()); + + } else { + + // base the documentation URL on the download URL + QUrl eff_url = QUrl (tl::to_qstring (m_url)); + eff_url.setPath (eff_url.path () + QString::fromUtf8 ("/") + url.path ()); + return tl::to_string (eff_url.toString ()); + + } +} + void SaltGrain::set_author (const std::string &a) { diff --git a/src/lay/laySaltGrain.h b/src/lay/laySaltGrain.h index 9f92763db..a8c7bff8c 100644 --- a/src/lay/laySaltGrain.h +++ b/src/lay/laySaltGrain.h @@ -145,6 +145,14 @@ public: */ void set_doc_url (const std::string &u); + /** + * @brief Gets the effective documentation URL + * + * The effective documentation URL is formed from the installation path + * and the documentation URL if the latter is a relative one. + */ + std::string eff_doc_url () const; + /** * @brief Gets the version of the grain * diff --git a/src/lay/laySaltGrainDetailsTextWidget.cc b/src/lay/laySaltGrainDetailsTextWidget.cc index 660eed4d1..59ea4f260 100644 --- a/src/lay/laySaltGrainDetailsTextWidget.cc +++ b/src/lay/laySaltGrainDetailsTextWidget.cc @@ -30,6 +30,7 @@ #include #include #include +#include namespace lay { @@ -37,7 +38,9 @@ namespace lay SaltGrainDetailsTextWidget::SaltGrainDetailsTextWidget (QWidget *w) : QTextBrowser (w), mp_grain (0) { - // .. nothing yet .. + setOpenLinks (false); + setOpenExternalLinks (false); + connect (this, SIGNAL (anchorClicked (const QUrl &)), this, SLOT (open_link (const QUrl &))); } void SaltGrainDetailsTextWidget::set_grain (SaltGrain *g) @@ -46,6 +49,12 @@ void SaltGrainDetailsTextWidget::set_grain (SaltGrain *g) setHtml (details_text ()); } +void +SaltGrainDetailsTextWidget::open_link (const QUrl &url) +{ + QDesktopServices::openUrl (url); +} + QVariant SaltGrainDetailsTextWidget::loadResource (int type, const QUrl &url) { @@ -237,7 +246,7 @@ SaltGrainDetailsTextWidget::details_text () stream << "

"; if (! g->doc_url ().empty ()) { - stream << "" << QObject::tr ("Documentation link") << ": doc_url ()) << "\">" << tl::to_qstring (tl::escaped_to_html (g->doc_url ())) << ""; + stream << "" << QObject::tr ("Documentation link") << ": eff_doc_url ()) << "\">" << tl::to_qstring (tl::escaped_to_html (g->eff_doc_url ())) << ""; } else { stream << ""; stream << QObject::tr ("This package does not have a documentation link. " diff --git a/src/lay/laySaltGrainDetailsTextWidget.h b/src/lay/laySaltGrainDetailsTextWidget.h index bb25a0e48..6ba47f144 100644 --- a/src/lay/laySaltGrainDetailsTextWidget.h +++ b/src/lay/laySaltGrainDetailsTextWidget.h @@ -36,6 +36,8 @@ class SaltGrain; class SaltGrainDetailsTextWidget : public QTextBrowser { +Q_OBJECT + public: /** * @brief Constructor @@ -50,6 +52,9 @@ public: protected: virtual QVariant loadResource (int type, const QUrl &url); +private slots: + void open_link (const QUrl &url); + private: lay::SaltGrain *mp_grain; diff --git a/src/lay/laySaltGrainPropertiesDialog.cc b/src/lay/laySaltGrainPropertiesDialog.cc index f1786a553..bfc24ab6f 100644 --- a/src/lay/laySaltGrainPropertiesDialog.cc +++ b/src/lay/laySaltGrainPropertiesDialog.cc @@ -327,7 +327,8 @@ void SaltGrainPropertiesDialog::url_changed (const QString &url) { // inserts the URL into the label - open_label->setText (m_open_label.arg (url)); + m_grain.set_doc_url (tl::to_string (url)); + open_label->setText (m_open_label.arg (tl::to_qstring (m_grain.eff_doc_url ()))); open_label->setEnabled (! url.isEmpty ()); }