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

0001 /***************************************************************************
0002     Private helpers for the homes shares handler
0003                              -------------------
0004     begin                : Mo Apr 11 2011
0005     copyright            : (C) 2011-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, 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 "smb4khomesshareshandler_p.h"
0032 #include "smb4ksettings.h"
0033 #include "smb4kshare.h"
0034 
0035 // Qt includes
0036 #include <QString>
0037 #include <QWidget>
0038 #include <QVBoxLayout>
0039 #include <QHBoxLayout>
0040 #include <QGridLayout>
0041 #include <QLabel>
0042 #include <QLineEdit>
0043 #include <QPushButton>
0044 #include <QDialogButtonBox>
0045 #include <QWindow>
0046 
0047 // KDE includes
0048 #define TRANSLATION_DOMAIN "smb4k-core"
0049 #include <KI18n/KLocalizedString>
0050 #include <KIconThemes/KIconLoader>
0051 #include <KConfigGui/KWindowConfig>
0052 
0053 Smb4KHomesUsers::Smb4KHomesUsers(const SharePtr &s, const QStringList &u)
0054 {
0055   m_workgroup_name = s->workgroupName();
0056   m_host_name      = s->hostName();
0057   m_share_name     = s->shareName();
0058   m_host_ip.setAddress(s->hostIpAddress());
0059   m_users          = u;
0060 }
0061 
0062 
0063 Smb4KHomesUsers::Smb4KHomesUsers(const Smb4KHomesUsers &u)
0064 {
0065   m_workgroup_name = u.workgroupName();
0066   m_host_name      = u.hostName();
0067   m_share_name     = u.shareName();
0068   m_host_ip.setAddress(u.hostIP());
0069   m_users          = u.users();
0070   m_profile        = u.profile();
0071 }
0072 
0073 
0074 Smb4KHomesUsers::Smb4KHomesUsers()
0075 {
0076 }
0077 
0078 
0079 Smb4KHomesUsers::~Smb4KHomesUsers()
0080 {
0081 }
0082 
0083 
0084 QString Smb4KHomesUsers::workgroupName() const
0085 {
0086   return m_workgroup_name;
0087 }
0088 
0089 
0090 void Smb4KHomesUsers::setWorkgroupName(const QString& name)
0091 {
0092   m_workgroup_name = name;
0093 }
0094 
0095 
0096 QString Smb4KHomesUsers::hostName() const
0097 {
0098   return m_host_name;
0099 }
0100 
0101 
0102 void Smb4KHomesUsers::setHostName(const QString& name)
0103 {
0104   m_host_name = name;
0105 }
0106 
0107 
0108 QString Smb4KHomesUsers::shareName() const
0109 {
0110   return m_share_name;
0111 }
0112 
0113 
0114 void Smb4KHomesUsers::setShareName(const QString& name)
0115 {
0116   m_share_name = name;
0117 }
0118 
0119 
0120 QString Smb4KHomesUsers::hostIP() const
0121 {
0122   return m_host_ip.toString();
0123 }
0124 
0125 
0126 void Smb4KHomesUsers::setHostIP(const QString& ip)
0127 {
0128   m_host_ip.setAddress(ip);
0129 }
0130 
0131 
0132 QStringList Smb4KHomesUsers::users() const
0133 {
0134   return m_users;
0135 }
0136 
0137 
0138 void Smb4KHomesUsers::setUsers(const QStringList& users)
0139 {
0140   m_users = users;
0141 }
0142 
0143 
0144 QString Smb4KHomesUsers::profile() const
0145 {
0146   return m_profile;
0147 }
0148 
0149 
0150 void Smb4KHomesUsers::setProfile(const QString& profile)
0151 {
0152   m_profile = profile;
0153 }
0154 
0155 
0156 
0157 Smb4KHomesUserDialog::Smb4KHomesUserDialog(const SharePtr &share, QWidget *parent) 
0158 : QDialog(parent), m_share(share)
0159 {
0160   //
0161   // Set the window title
0162   // 
0163   setWindowTitle(i18n("Specify User"));
0164 
0165   //
0166   // Setup the view
0167   // 
0168   setupView();
0169   
0170   //
0171   // Set the dialog size
0172   // 
0173   create();
0174 
0175   KConfigGroup group(Smb4KSettings::self()->config(), "HomesUserDialog");
0176   QSize dialogSize;
0177   
0178   if (group.exists())
0179   {
0180     KWindowConfig::restoreWindowSize(windowHandle(), group);
0181     dialogSize = windowHandle()->size();
0182   }
0183   else
0184   {
0185     dialogSize = sizeHint();
0186   }
0187   
0188   resize(dialogSize); // workaround for QTBUG-40584
0189 
0190   //
0191   // Fill the completion object
0192   // 
0193   m_user_combo->completionObject()->setItems(group.readEntry("HomesUsersCompletion", QStringList()));
0194 }
0195 
0196 
0197 Smb4KHomesUserDialog::~Smb4KHomesUserDialog()
0198 {
0199 }
0200 
0201 
0202 void Smb4KHomesUserDialog::setupView()
0203 {
0204   QVBoxLayout *layout = new QVBoxLayout(this);
0205   layout->setSpacing(5);
0206 
0207   QWidget *description = new QWidget(this);
0208 
0209   QHBoxLayout *desc_layout = new QHBoxLayout(description);
0210   desc_layout->setSpacing(5);
0211   desc_layout->setMargin(0);
0212 
0213   QLabel *pixmap = new QLabel(description);
0214   QPixmap user_pix = KDE::icon("user-identity").pixmap(KIconLoader::SizeHuge);
0215   pixmap->setPixmap(user_pix);
0216   pixmap->setAlignment(Qt::AlignBottom);
0217 
0218   QLabel *label = new QLabel(i18n("Please specify a username for share<br><b>%1</b>.", m_share->displayString()), description);
0219   label->setWordWrap(true);
0220   label->setAlignment(Qt::AlignBottom);
0221 
0222   desc_layout->addWidget(pixmap, 0);
0223   desc_layout->addWidget(label, Qt::AlignBottom);
0224   
0225   QWidget *input = new QWidget(this);
0226   
0227   QGridLayout *input_layout = new QGridLayout(input);
0228   input_layout->setSpacing(5);
0229   input_layout->setMargin(0);
0230   input_layout->setColumnStretch(0, 0);
0231   input_layout->setColumnStretch(1, 1);
0232   
0233   QLabel *input_label = new QLabel(i18n("User:"), input);
0234 
0235   m_user_combo = new KComboBox(true, input);
0236   m_user_combo->setDuplicatesEnabled(false);
0237   m_user_combo->setEditable(true);
0238   
0239   input_layout->addWidget(input_label, 0, 0, 0);
0240   input_layout->addWidget(m_user_combo, 0, 1, 0);
0241   
0242   QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
0243   m_clear_button = buttonBox->addButton(i18n("Clear List"), QDialogButtonBox::ActionRole);
0244   m_clear_button->setIcon(KDE::icon("edit-clear"));
0245   m_clear_button->setEnabled(false);
0246   m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
0247   m_ok_button->setEnabled(false);
0248   m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
0249   
0250   m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
0251   m_cancel_button->setShortcut(Qt::Key_Escape);
0252   
0253   m_ok_button->setDefault(true);
0254 
0255   layout->addWidget(description, 0);
0256   layout->addWidget(input, 0);
0257   layout->addWidget(buttonBox, 0);
0258   
0259   m_user_combo->setFocus();
0260   
0261   connect(m_user_combo, SIGNAL(currentTextChanged(QString)), SLOT(slotTextChanged(QString)));
0262   connect(m_user_combo->lineEdit(), SIGNAL(editingFinished()), SLOT(slotHomesUserEntered()));
0263   connect(m_clear_button, SIGNAL(clicked()), SLOT(slotClearClicked()));
0264   connect(m_ok_button, SIGNAL(clicked()), SLOT(slotOkClicked()));
0265   connect(m_cancel_button, SIGNAL(clicked()), SLOT(reject()));
0266 }
0267 
0268 
0269 void Smb4KHomesUserDialog::setUserNames(const QStringList &users)
0270 {
0271   if (!users.isEmpty())
0272   {
0273     m_user_combo->addItems(users);
0274     m_user_combo->setCurrentItem("");
0275     m_clear_button->setEnabled(true);
0276   }
0277 }
0278 
0279 
0280 QStringList Smb4KHomesUserDialog::userNames()
0281 {
0282   QStringList users;
0283   
0284   for (int i = 0; i < m_user_combo->count(); ++i)
0285   {
0286     users << m_user_combo->itemText(i);
0287   }
0288   
0289   if (!users.contains(m_user_combo->currentText()))
0290   {
0291     users << m_user_combo->currentText();
0292   }
0293   
0294   return users;
0295 }
0296 
0297 
0298 void Smb4KHomesUserDialog::slotTextChanged(const QString &text)
0299 {
0300   m_ok_button->setEnabled(!text.isEmpty());
0301 }
0302 
0303 
0304 void Smb4KHomesUserDialog::slotClearClicked()
0305 {
0306   m_user_combo->clearEditText();
0307   m_user_combo->clear();
0308   m_clear_button->setEnabled(false);
0309 }
0310 
0311 
0312 void Smb4KHomesUserDialog::slotOkClicked()
0313 {
0314   KConfigGroup group(Smb4KSettings::self()->config(), "HomesUserDialog");
0315   KWindowConfig::saveWindowSize(windowHandle(), group);
0316   group.writeEntry("HomesUsersCompletion", m_user_combo->completionObject()->items());
0317   accept();
0318 }
0319 
0320 
0321 void Smb4KHomesUserDialog::slotHomesUserEntered()
0322 {
0323   KCompletion *completion = m_user_combo->completionObject();
0324 
0325   if (!m_user_combo->currentText().isEmpty())
0326   {
0327     completion->addItem(m_user_combo->currentText());
0328   }
0329 }
0330