1/*
2 Copyright (c) 1998, 2008, 2009 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2 of the License or (at
7 your option) version 3 or, at the discretion of KDE e.V. (which shall
8 act as a proxy as in section 14 of the GPLv3), any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "knameandurlinputdialog.h"
22
23#include <klineedit.h>
24#include <kurlrequester.h>
25#include <kprotocolmanager.h>
26#include <QFormLayout>
27#include <QLabel>
28
29class KNameAndUrlInputDialogPrivate
30{
31public:
32 KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog* qq) : m_fileNameEdited(false), q(qq) {}
33
34 void _k_slotNameTextChanged(const QString&);
35 void _k_slotURLTextChanged(const QString&);
36
37 /**
38 * The line edit widget for the fileName
39 */
40 KLineEdit *m_leName;
41 /**
42 * The URL requester for the URL :)
43 */
44 KUrlRequester *m_urlRequester;
45 /**
46 * True if the filename was manually edited.
47 */
48 bool m_fileNameEdited;
49 KNameAndUrlInputDialog* q;
50};
51
52KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString& nameLabel, const QString& urlLabel, const KUrl& startDir, QWidget *parent)
53 : KDialog(parent), d(new KNameAndUrlInputDialogPrivate(this))
54{
55 setButtons(Ok | Cancel);
56
57 QWidget* plainPage = new QWidget(this);
58 setMainWidget(plainPage);
59
60 QFormLayout* topLayout = new QFormLayout(plainPage);
61 topLayout->setMargin(0);
62
63 // First line: filename
64 d->m_leName = new KLineEdit;
65 d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3);
66 d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect
67 connect(d->m_leName, SIGNAL(textChanged(QString)),
68 SLOT(_k_slotNameTextChanged(QString)));
69 topLayout->addRow(nameLabel, d->m_leName);
70
71 // Second line: url
72 d->m_urlRequester = new KUrlRequester;
73 d->m_urlRequester->setStartDir(startDir);
74 d->m_urlRequester->setMode(KFile::File | KFile::Directory);
75
76 d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3);
77 connect(d->m_urlRequester->lineEdit(), SIGNAL(textChanged(QString)),
78 SLOT(_k_slotURLTextChanged(QString)));
79 topLayout->addRow(urlLabel, d->m_urlRequester);
80
81 d->m_fileNameEdited = false;
82 enableButtonOk(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty());
83 d->m_leName->setFocus();
84}
85
86KNameAndUrlInputDialog::~KNameAndUrlInputDialog()
87{
88 delete d;
89}
90
91KUrl KNameAndUrlInputDialog::url() const
92{
93 if (result() == QDialog::Accepted) {
94 return d->m_urlRequester->url();
95 }
96 else
97 return KUrl();
98}
99
100QString KNameAndUrlInputDialog::name() const
101{
102 if (result() == QDialog::Accepted)
103 return d->m_leName->text();
104 else
105 return QString();
106}
107
108void KNameAndUrlInputDialogPrivate::_k_slotNameTextChanged(const QString&)
109{
110 m_fileNameEdited = true;
111 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
112}
113
114void KNameAndUrlInputDialogPrivate::_k_slotURLTextChanged(const QString&)
115{
116 if (!m_fileNameEdited) {
117 // use URL as default value for the filename
118 // (we copy only its filename if protocol supports listing,
119 // but for HTTP we don't want tons of index.html links)
120 KUrl url(m_urlRequester->url());
121 if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty())
122 m_leName->setText(url.fileName());
123 else
124 m_leName->setText(url.url());
125 m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously
126 }
127 q->enableButtonOk(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
128}
129
130void KNameAndUrlInputDialog::setSuggestedName(const QString& name)
131{
132 d->m_leName->setText(name);
133 d->m_urlRequester->setFocus();
134}
135
136void KNameAndUrlInputDialog::setSuggestedUrl(const KUrl& url)
137{
138 d->m_urlRequester->setUrl(url);
139}
140
141#include "knameandurlinputdialog.moc"
142