1 /*
2     SPDX-FileCopyrightText: 2001 Jason Harris <kstars@30doradus.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "addlinkdialog.h"
8 #include "Options.h"
9 
10 #include <QPushButton>
11 #include <QUrl>
12 #include <QDesktopServices>
13 
14 #include <KMessageBox>
15 
16 #include "skyobjects/skyobject.h"
17 
AddLinkDialogUI(QWidget * parent)18 AddLinkDialogUI::AddLinkDialogUI(QWidget *parent) : QFrame(parent)
19 {
20     setupUi(this);
21 }
22 
AddLinkDialog(QWidget * parent,const QString & oname)23 AddLinkDialog::AddLinkDialog(QWidget *parent, const QString &oname) : QDialog(parent), ObjectName(oname)
24 {
25 #ifdef Q_OS_OSX
26     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
27 #endif
28     ald = new AddLinkDialogUI(this);
29 
30     setWindowTitle(i18nc("@title:window", "Add Custom URL to %1", oname));
31 
32     QVBoxLayout *mainLayout = new QVBoxLayout;
33     mainLayout->addWidget(ald);
34     setLayout(mainLayout);
35 
36     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
37     mainLayout->addWidget(buttonBox);
38     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
39     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
40 
41     //connect signals to slots
42     connect(ald->URLButton, SIGNAL(clicked()), this, SLOT(checkURL()));
43     connect(ald->ImageRadio, SIGNAL(toggled(bool)), this, SLOT(changeDefaultDescription(bool)));
44 
45     ald->ImageRadio->setChecked(true);
46     ald->DescBox->setText(i18n("Show image of ") + ObjectName);
47 }
48 
checkURL(void)49 void AddLinkDialog::checkURL(void)
50 {
51     QUrl _url(url());
52     if (_url.isValid()) //Is the string a valid URL?
53     {
54         QDesktopServices::openUrl(_url); //If so, launch the browser to see if it's the correct document
55     }
56     else //If not, print a warning message box that offers to open the browser to a search engine.
57     {
58         QString message =
59             i18n("The URL is not valid. Would you like to open a browser window\nto the Google search engine?");
60         QString caption = i18n("Invalid URL");
61         if (KMessageBox::warningYesNo(nullptr, message, caption, KGuiItem(i18n("Browse Google")),
62                                       KGuiItem(i18n("Do Not Browse"))) == KMessageBox::Yes)
63         {
64             QDesktopServices::openUrl(QUrl("https://www.google.com"));
65         }
66     }
67 }
68 
changeDefaultDescription(bool imageEnabled)69 void AddLinkDialog::changeDefaultDescription(bool imageEnabled)
70 {
71     if (imageEnabled)
72         ald->DescBox->setText(i18n("Show image of ") + ObjectName);
73     else
74         ald->DescBox->setText(i18n("Show webpage about ") + ObjectName);
75 }
76