File indexing completed on 2024-04-21 15:42:49

0001 /***************************************************************************
0002     This file contains private helper classes for the Smb4KMounter class.
0003                              -------------------
0004     begin                : Do Jul 19 2007
0005     copyright            : (C) 2007-2019 by Alexander Reinholdt
0006     email                : alexander.reinholdt@kdemail.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful, but   *
0016  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
0018  *   General Public License for more details.                              *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
0023  *   MA 02110-1335, USA                                                    *
0024  ***************************************************************************/
0025 
0026 #ifdef HAVE_CONFIG_H
0027 #include <config.h>
0028 #endif
0029 
0030 // application specific includes
0031 #include "smb4kmounter_p.h"
0032 #include "smb4ksettings.h"
0033 #include "smb4knotification.h"
0034 #include "smb4khomesshareshandler.h"
0035 #include "smb4kglobal.h"
0036 #include "smb4kcustomoptions.h"
0037 #include "smb4kcustomoptionsmanager.h"
0038 
0039 // Qt includes
0040 #include <QUrl>
0041 #include <QHBoxLayout>
0042 #include <QVBoxLayout>
0043 #include <QGridLayout>
0044 #include <QLabel>
0045 #include <QDialogButtonBox>
0046 #include <QWindow>
0047 
0048 // KDE includes
0049 #define TRANSLATION_DOMAIN "smb4k-core"
0050 #include <KI18n/KLocalizedString>
0051 #include <KConfigGui/KWindowConfig>
0052 #include <KIconThemes/KIconLoader>
0053 
0054 using namespace Smb4KGlobal;
0055 
0056 
0057 Smb4KMountDialog::Smb4KMountDialog(const SharePtr &share, QWidget *parent)
0058 : QDialog(parent), m_share(share), m_valid(true)
0059 {
0060   //
0061   // Set the title
0062   // 
0063   setWindowTitle(i18n("Mount Share"));
0064   
0065   //
0066   // Set up the view
0067   // 
0068   setupView();
0069   
0070   //
0071   // Set the dialog size
0072   // 
0073   create();
0074 
0075   KConfigGroup group(Smb4KSettings::self()->config(), "MountDialog");
0076   QSize dialogSize;
0077   
0078   if (group.exists())
0079   {
0080     KWindowConfig::restoreWindowSize(windowHandle(), group);
0081     dialogSize = windowHandle()->size();
0082   }
0083   else
0084   {
0085     dialogSize = sizeHint();
0086   }
0087   
0088   resize(dialogSize); // workaround for QTBUG-40584
0089 
0090   //
0091   // Fill the completion objects
0092   // 
0093   m_share_input->completionObject()->setItems(group.readEntry("ShareNameCompletion", QStringList()));
0094   m_ip_input->completionObject()->setItems(group.readEntry("IPAddressCompletion", QStringList()));
0095   m_workgroup_input->completionObject()->setItems(group.readEntry("WorkgroupCompletion", QStringList()));
0096 }
0097 
0098 
0099 Smb4KMountDialog::~Smb4KMountDialog()
0100 {
0101 }
0102 
0103 
0104 void Smb4KMountDialog::setupView()
0105 {
0106   QVBoxLayout *layout = new QVBoxLayout(this);
0107   layout->setSpacing(5);
0108 
0109   QWidget *description = new QWidget(this);
0110 
0111   QHBoxLayout *desc_layout = new QHBoxLayout(description);
0112   desc_layout->setSpacing(5);
0113 
0114   QLabel *pixmap = new QLabel(description);
0115   QPixmap mount_pix = KDE::icon("view-form", QStringList("emblem-mounted")).pixmap(KIconLoader::SizeHuge);
0116   pixmap->setPixmap(mount_pix);
0117   pixmap->setAlignment(Qt::AlignBottom);
0118 
0119   QLabel *label = new QLabel(i18n("Enter the location and optionally the IP address and workgroup to mount a share."), description);
0120   label->setWordWrap(true);
0121   label->setAlignment(Qt::AlignBottom);
0122 
0123   desc_layout->addWidget(pixmap, 0);
0124   desc_layout->addWidget(label, Qt::AlignBottom);
0125 
0126   QWidget *edit_widget = new QWidget(this);
0127 
0128   QGridLayout *edit_layout = new QGridLayout(edit_widget);
0129   layout->setSpacing(5);
0130 
0131   QLabel *shareLabel = new QLabel(i18n("Location:"), edit_widget);
0132   m_share_input = new KLineEdit(edit_widget);
0133   m_share_input->setWhatsThis(i18n("The location of the share is provided by the Uniform Resource Locator (URL). It generally has the following syntax: "
0134     "[smb:]//[USER:PASSWORD@]HOST:PORT/SHARE. The username, password and port are optional. You should omit to enter the password here, because it is shown in cleartext."));
0135 //   m_share_input->setToolTip(i18n("The URL of the share"));
0136   m_share_input->setCompletionMode(KCompletion::CompletionPopupAuto);
0137   m_share_input->setClearButtonEnabled(true);
0138   m_share_input->setMinimumWidth(200);
0139   m_share_input->setFocus();
0140 
0141   QLabel *addressLabel = new QLabel(i18n("IP Address:"), edit_widget);
0142   m_ip_input = new KLineEdit(edit_widget);
0143   m_ip_input->setWhatsThis(i18n("The Internet Protocol (IP) address identifies the "
0144     "host in the network and indicates where it is. It has two valid formats, the one "
0145     "known as IP version 4 (e.g. 192.168.2.11) and the version 6 format "
0146     "(e.g. 2001:0db8:85a3:08d3:1319:8a2e:0370:7334)."));
0147 //   m_ip_input->setToolTip(i18n("The IP address of the host where the share is located"));
0148   m_ip_input->setCompletionMode(KCompletion::CompletionPopupAuto);
0149   m_ip_input->setClearButtonEnabled(true);
0150   m_ip_input->setMinimumWidth(200);
0151 
0152   QLabel *workgroupLabel = new QLabel(i18n("Workgroup:"), edit_widget);
0153   m_workgroup_input = new KLineEdit(edit_widget);
0154   m_workgroup_input->setWhatsThis(i18n("The workgroup or domain identifies the "
0155     "peer-to-peer computer network the host is located in."));
0156 //   m_workgroup_input->setToolTip(i18n("The workgroup where the host is located"));
0157   m_workgroup_input->setCompletionMode(KCompletion::CompletionPopupAuto);
0158   m_workgroup_input->setClearButtonEnabled(true);
0159   m_workgroup_input->setMinimumWidth(200);
0160 
0161   edit_layout->addWidget(shareLabel, 0, 0, 0);
0162   edit_layout->addWidget(m_share_input, 0, 1, 0);
0163   edit_layout->addWidget(addressLabel, 1, 0, 0);
0164   edit_layout->addWidget(m_ip_input, 1, 1, 0);
0165   edit_layout->addWidget(workgroupLabel, 2, 0, 0);
0166   edit_layout->addWidget(m_workgroup_input, 2, 1, 0);
0167 
0168   m_bookmark = new QCheckBox(i18n("Add this share to the bookmarks"), this);
0169   m_bookmark->setWhatsThis(i18n("If you tick this checkbox, the share will be bookmarked "
0170     "and you can access it e.g. through the \"Bookmarks\" menu entry in the main window."));
0171 //   m_bookmark->setToolTip(i18n("Add this share to the bookmarks"));
0172   
0173   QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
0174   m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
0175   m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
0176   
0177   m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
0178   m_cancel_button->setShortcut(Qt::Key_Escape);
0179 
0180   layout->addWidget(description, Qt::AlignBottom);
0181   layout->addWidget(edit_widget, 0);
0182   layout->addWidget(m_bookmark, 0);
0183   layout->addWidget(buttonBox, 0);
0184 
0185   slotChangeInputValue(m_share_input->text());
0186 
0187   // 
0188   // Connections
0189   // 
0190   connect(m_share_input, SIGNAL(textChanged(QString)), this, SLOT(slotChangeInputValue(QString)));
0191   connect(m_share_input, SIGNAL(editingFinished()), this, SLOT(slotShareNameEntered()));
0192   connect(m_ip_input, SIGNAL(editingFinished()), this, SLOT(slotIPEntered()));
0193   connect(m_workgroup_input, SIGNAL(editingFinished()), this, SLOT(slotWorkgroupEntered()));
0194   connect(m_ok_button, SIGNAL(clicked()), this, SLOT(slotOkClicked()));
0195   connect(m_cancel_button, SIGNAL(clicked()), this, SLOT(slotCancelClicked()));
0196 }
0197 
0198 
0199 /////////////////////////////////////////////////////////////////////////////
0200 //  SLOT IMPLEMENTATIONS
0201 /////////////////////////////////////////////////////////////////////////////
0202 
0203 void Smb4KMountDialog::slotChangeInputValue(const QString& _test)
0204 {
0205   m_ok_button->setEnabled(!_test.isEmpty());
0206 }
0207 
0208 
0209 void Smb4KMountDialog::slotOkClicked()
0210 {
0211   if (!m_share_input->text().trimmed().isEmpty())
0212   {
0213     //
0214     // Get the URL
0215     // 
0216     QString userInput = m_share_input->text().trimmed();
0217     
0218     //
0219     // Take care of a Windows-like UNC addresses
0220     // 
0221     if (userInput.startsWith(QLatin1String("\\")))
0222     {
0223       userInput.replace("\\", "/");
0224     }
0225     
0226     //
0227     // Set the URL and adjust the scheme
0228     // 
0229     QUrl smbUrl = QUrl::fromUserInput(userInput);
0230     smbUrl.setScheme("smb");
0231     
0232     //
0233     // Set the URL of the share
0234     // 
0235     if (smbUrl.isValid() && !smbUrl.host().isEmpty() && !smbUrl.path().isEmpty() && !smbUrl.path().endsWith(QLatin1String("/")))
0236     {
0237       m_share->setUrl(smbUrl);
0238       m_share->setWorkgroupName(m_workgroup_input->text().trimmed());
0239       m_share->setHostIpAddress(m_ip_input->text().trimmed());
0240     }
0241     else
0242     {
0243       Smb4KNotification::invalidURLPassed();
0244       m_valid = false;
0245     }
0246   }
0247   
0248   KConfigGroup group(Smb4KSettings::self()->config(), "MountDialog");
0249   KWindowConfig::saveWindowSize(windowHandle(), group);
0250   group.writeEntry("ShareNameCompletion", m_share_input->completionObject()->items());
0251   group.writeEntry("IPAddressCompletion", m_ip_input->completionObject()->items());
0252   group.writeEntry("WorkgroupCompletion", m_workgroup_input->completionObject()->items());
0253   
0254   accept();
0255 }
0256 
0257 
0258 void Smb4KMountDialog::slotCancelClicked()
0259 {
0260   Smb4KMounter::self()->abort();
0261   reject();
0262 }
0263 
0264 
0265 void Smb4KMountDialog::slotShareNameEntered()
0266 {
0267   KCompletion *completion = m_share_input->completionObject();
0268   QUrl url(m_share_input->userText());
0269   url.setScheme("smb");
0270 
0271   if (url.isValid() && !url.isEmpty())
0272   {
0273     completion->addItem(m_share_input->userText());
0274   }
0275 }
0276 
0277 
0278 void Smb4KMountDialog::slotIPEntered()
0279 {
0280   KCompletion *completion = m_ip_input->completionObject();
0281 
0282   if (!m_ip_input->userText().isEmpty())
0283   {
0284     completion->addItem(m_ip_input->userText());
0285   }
0286 }
0287 
0288 
0289 void Smb4KMountDialog::slotWorkgroupEntered()
0290 {
0291   KCompletion *completion = m_workgroup_input->completionObject();
0292 
0293   if (!m_workgroup_input->userText().isEmpty())
0294   {
0295     completion->addItem(m_workgroup_input->userText());
0296   }
0297 }
0298