File indexing completed on 2024-04-21 13:19:05

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "urldlg.h"
0021 #include "ui_urldlg.h"
0022 
0023 #include <KConfigGroup>
0024 #include <KHistoryComboBox>
0025 #include <KLocalizedString>
0026 #include <KSharedConfig>
0027 #include <KUrlRequester>
0028 #include <QLabel>
0029 #include <QVBoxLayout>
0030 
0031 UrlDlg::UrlDlg(QWidget *parent)
0032     : QDialog(parent)
0033     , m_urlRequester(nullptr)
0034     , m_ui(new Ui::UrlDlg)
0035 {
0036     m_ui->setupUi(this);
0037     m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0038     m_ui->buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0039 
0040     KHistoryComboBox *combo = new KHistoryComboBox(this);
0041     combo->setDuplicatesEnabled(false);
0042     KConfigGroup kc = KSharedConfig::openConfig()->group("Open-repository settings");
0043     int max = kc.readEntry(QLatin1String("Maximum history"), 15);
0044     combo->setMaxCount(max);
0045     const QStringList list = kc.readEntry(QLatin1String("History"), QStringList());
0046     combo->setHistoryItems(list);
0047     combo->setMinimumWidth(100);
0048     combo->adjustSize();
0049     if (combo->width() > 300) {
0050         combo->resize(300, combo->height());
0051     }
0052 
0053     m_urlRequester = new KUrlRequester(combo, this);
0054     m_ui->topLayout->insertWidget(1, m_urlRequester);
0055     m_urlRequester->setFocus();
0056     m_urlRequester->setMode(KFile::ExistingOnly | KFile::Directory);
0057     connect(m_urlRequester->comboBox(), &KComboBox::currentTextChanged, this, &UrlDlg::slotTextChanged);
0058 
0059     slotTextChanged(QString());
0060     m_urlRequester->adjustSize();
0061 
0062     connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &UrlDlg::accept);
0063 }
0064 
0065 UrlDlg::~UrlDlg()
0066 {
0067     delete m_ui;
0068 }
0069 
0070 void UrlDlg::accept()
0071 {
0072     KHistoryComboBox *combo = static_cast<KHistoryComboBox *>(m_urlRequester->comboBox());
0073     if (combo) {
0074         combo->addToHistory(m_urlRequester->url().url());
0075         KConfigGroup kc = KSharedConfig::openConfig()->group("Open-repository settings");
0076         kc.writeEntry(QLatin1String("History"), combo->historyItems());
0077         kc.sync();
0078     }
0079     QDialog::accept();
0080 }
0081 
0082 /*!
0083     \fn UrlDlg::slotTextChanged(const QString&)
0084  */
0085 void UrlDlg::slotTextChanged(const QString &text)
0086 {
0087     bool state = !text.trimmed().isEmpty();
0088     m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
0089 }
0090 
0091 /*!
0092     \fn UrlDlg::getUrl(QWidget*parent)
0093  */
0094 QUrl UrlDlg::getUrl(QWidget *parent)
0095 {
0096     QUrl ret;
0097     QPointer<UrlDlg> dlg(new UrlDlg(parent));
0098     dlg->setWindowTitle(i18nc("@title:window", "Open"));
0099     if (dlg->exec() == QDialog::Accepted) {
0100         // added by Wellu Mäkinen <wellu@wellu.org>
0101         //
0102         // get rid of leading whitespace
0103         // that is %20 in encoded form
0104         QString url = dlg->m_urlRequester->url().toString();
0105 
0106         // decodes %20 to normal spaces
0107         // trims the whitespace from both ends
0108         // of the URL
0109         ret = QUrl(url.trimmed());
0110     }
0111     delete dlg;
0112     return ret;
0113 }
0114 
0115 #include "moc_urldlg.cpp"