1 /*
2     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "servedialog.h"
8 #include "servewrapper.h"
9 #include "fileviewhgpluginsettings.h"
10 #include "hgwrapper.h"
11 
12 #include <QSpinBox>
13 #include <QTextEdit>
14 #include <QLabel>
15 #include <QDesktopServices>
16 #include <KLocalizedString>
17 #include <KMessageBox>
18 
HgServeDialog(QWidget * parent)19 HgServeDialog::HgServeDialog(QWidget *parent) :
20     DialogBase(QDialogButtonBox::NoButton)
21 {
22     Q_UNUSED(parent)
23     // dialog properties
24     this->setWindowTitle(xi18nc("@title:window",
25                 "<application>Hg</application> Serve"));
26 
27     //
28     m_serverWrapper = HgServeWrapper::instance();
29 
30     //
31     setupUI();
32     loadConfig();
33 
34     // Load saved settings
35     FileViewHgPluginSettings *settings = FileViewHgPluginSettings::self();
36     this->resize(QSize(settings->serveDialogWidth(),
37                                settings->serveDialogHeight()));
38 
39     // connections
40     connect(this, SIGNAL(finished(int)), this, SLOT(saveGeometry()));
41     connect(m_startButton, &QAbstractButton::clicked, this, &HgServeDialog::slotStart);
42     connect(m_stopButton, &QAbstractButton::clicked, this, &HgServeDialog::slotStop);
43     connect(m_browseButton, &QAbstractButton::clicked, this, &HgServeDialog::slotBrowse);
44     connect(m_serverWrapper, &HgServeWrapper::finished,
45             this, &HgServeDialog::slotUpdateButtons);
46     connect(m_serverWrapper, &HgServeWrapper::started,
47             this, &HgServeDialog::slotUpdateButtons);
48     connect(m_serverWrapper, &HgServeWrapper::error,
49             this, &HgServeDialog::slotUpdateButtons);
50     connect(m_serverWrapper, &HgServeWrapper::error,
51             this, &HgServeDialog::slotServerError);
52     connect(m_serverWrapper, &HgServeWrapper::readyReadLine,
53             this, &HgServeDialog::appendServerOutput);
54 }
55 
setupUI()56 void HgServeDialog::setupUI()
57 {
58     m_portNumber = new QSpinBox;
59     m_portNumber->setMinimum(0);
60     m_portNumber->setMaximum(65535);
61     m_portNumber->setValue(8000);
62 
63     m_startButton = new QPushButton(xi18nc("@label:button", "Start Server"));
64     m_stopButton = new QPushButton(xi18nc("@label:button", "Stop Server"));
65     m_browseButton = new QPushButton(xi18nc("@label:button", "Open in browser"));
66     m_browseButton->setDisabled(true);
67 
68     m_logEdit = new QTextEdit;
69     m_repoPathLabel = new QLabel;
70     m_logEdit->setReadOnly(true);
71     m_logEdit->setFontFamily(QLatin1String("Monospace"));
72 
73     QVBoxLayout *buttonLayout = new QVBoxLayout;
74     buttonLayout->addWidget(m_startButton);
75     buttonLayout->addWidget(m_stopButton);
76     buttonLayout->addStretch(1);
77     buttonLayout->addWidget(m_browseButton);
78     buttonLayout->addStretch(2);
79 
80     QHBoxLayout *portLayout = new QHBoxLayout;
81     portLayout->addWidget(new QLabel(xi18nc("@label", "Port")));
82     portLayout->addWidget(m_portNumber);
83     portLayout->addStretch();
84 
85     QHBoxLayout *midLayout = new QHBoxLayout;
86     midLayout->addWidget(m_logEdit);
87     midLayout->addLayout(buttonLayout);
88 
89     QVBoxLayout *lay = new QVBoxLayout;
90     lay->addWidget(m_repoPathLabel);
91     lay->addLayout(portLayout);
92     lay->addLayout(midLayout);
93 
94     layout()->insertLayout(0, lay);
95 }
96 
loadConfig()97 void HgServeDialog::loadConfig()
98 {
99     HgWrapper *hgw = HgWrapper::instance();
100     m_repoPathLabel->setText("<b>" + hgw->getBaseDir() + "</b>");
101 
102     slotUpdateButtons();
103 }
104 
slotUpdateButtons()105 void HgServeDialog::slotUpdateButtons()
106 {
107     if (m_serverWrapper->running(HgWrapper::instance()->getBaseDir())) {
108         m_startButton->setEnabled(false);
109         m_stopButton->setEnabled(true);
110         m_portNumber->setEnabled(false);
111     }
112     else {
113         m_startButton->setEnabled(true);
114         m_stopButton->setEnabled(false);
115         m_portNumber->setEnabled(true);
116         m_serverWrapper->cleanUnused();
117     }
118 }
119 
slotStart()120 void HgServeDialog::slotStart()
121 {
122     m_serverWrapper->startServer(HgWrapper::instance()->getBaseDir(),
123                                  m_portNumber->value());
124     m_browseButton->setDisabled(false);
125 }
126 
slotStop()127 void HgServeDialog::slotStop()
128 {
129     m_serverWrapper->stopServer(HgWrapper::instance()->getBaseDir());
130     m_browseButton->setDisabled(true);
131 }
132 
slotBrowse()133 void HgServeDialog::slotBrowse()
134 {
135     QDesktopServices::openUrl(QUrl(QString("http://localhost:%1").
136         arg(m_portNumber->value())));
137 }
138 
139 
slotServerError()140 void HgServeDialog::slotServerError()
141 {
142     m_serverWrapper->cleanUnused();
143 }
144 
appendServerOutput(const QString & repoLocation,const QString & line)145 void HgServeDialog::appendServerOutput(const QString &repoLocation, const QString &line)
146 {
147     if (HgWrapper::instance()->getBaseDir() == repoLocation) {
148         m_logEdit->append(line);
149     }
150 }
151 
saveGeometry()152 void HgServeDialog::saveGeometry()
153 {
154     FileViewHgPluginSettings *settings = FileViewHgPluginSettings::self();
155     settings->setServeDialogHeight(this->height());
156     settings->setServeDialogWidth(this->width());
157     settings->save();
158 }
159 
160 
161 
162