File indexing completed on 2024-04-28 04:02:30

0001 /***************************************************************************
0002                           dlgconnect.cpp  -  Connect dialog
0003     This file is a part of KMuddy distribution.
0004                              -------------------
0005     begin                : Ut Jul 23 2002
0006     copyright            : (C) 2002-2008 by Tomas Mecir
0007     email                : kmuddy@kmuddy.com
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #include "dlgconnect.h"
0020 
0021 #include "dlgeditprofile.h"
0022 
0023 #include "cprofilemanager.h"
0024 #include "cprofilesettings.h"
0025 
0026 #include <QCheckBox>
0027 #include <QDialogButtonBox>
0028 #include <QGridLayout>
0029 #include <QInputDialog>
0030 #include <QPushButton>
0031 #include <QSortFilterProxyModel>
0032 #include <QTreeView>
0033 #include <QVBoxLayout>
0034 
0035 #include <KLocalizedString>
0036 #include <kmessagebox.h>
0037 
0038 dlgConnect::dlgConnect(QWidget *parent) : QDialog (parent)
0039 {
0040   setWindowTitle (i18n ("Connect"));
0041 
0042   //create main dialog's widget
0043   QGridLayout *layout = new QGridLayout (this);
0044 
0045   //put some widgets there
0046   lw = new QTreeView (this);
0047   QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel (this);
0048   proxyModel->setSourceModel (cProfileManager::self()->model());
0049   proxyModel->setSortCaseSensitivity (Qt::CaseInsensitive);
0050   lw->setModel (proxyModel);
0051   lw->setUniformRowHeights (true);
0052   lw->setRootIsDecorated (false);
0053   lw->setItemsExpandable (false);
0054   lw->setSortingEnabled (true);
0055   lw->sortByColumn (0, Qt::AscendingOrder);
0056   lw->setWhatsThis(
0057     i18n ("This list shows currently defined profiles.<p><b>Profiles</b> "
0058       "allow you to speed up connecting to your MUD, as well as to use "
0059       "more advanced features like <i>aliases</i> or <i>triggers</i>."));
0060 
0061   QWidget *vb = new QWidget (this);
0062   QVBoxLayout *vblayout = new QVBoxLayout (vb);
0063   vblayout->setSpacing (5);
0064   QPushButton *addButton = new QPushButton (i18n ("&New profile"), vb);
0065   QPushButton *modifyButton = new QPushButton (i18n ("&Modify profile"), vb);
0066   QPushButton *deleteButton = new QPushButton (i18n ("&Delete profile"), vb);
0067   QPushButton *duplicateButton = new QPushButton (i18n ("&Duplicate profile"), vb);
0068   vblayout->addWidget (addButton);
0069   vblayout->addWidget (modifyButton);
0070   vblayout->addWidget (deleteButton);
0071   vblayout->addWidget (duplicateButton);
0072 
0073   chkSendNothing = new QCheckBox (i18n ("Do not &send login sequence"), this);
0074   chkSendNothing->setWhatsThis( i18n ("Login sequence won't be sent for "
0075       "this connect. Useful when you're creating a new character and you "
0076       "want to use QuickConnect for some reason."));
0077   chkSendNothing->setChecked (false);
0078   
0079   chkOffline = new QCheckBox (i18n ("&Offline editing"), this);
0080   chkOffline->setWhatsThis( i18n ("This allows offline editing of "
0081       "profiles."));
0082   chkOffline->setChecked(false);
0083 
0084   buttons = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0085   QPushButton *button = buttons->button (QDialogButtonBox::Ok);
0086   button->setText (i18n ("&Connect"));
0087   button->setToolTip (i18n ("Establishes connection with specified parameters."));
0088   button = buttons->button (QDialogButtonBox::Cancel);
0089   button->setText (i18n ("C&lose"));
0090   button->setToolTip (i18n ("Closes this dialog box without connecting."));
0091   connect (buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
0092   connect (buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
0093 
0094   layout->setRowStretch (0, 0);
0095   layout->setRowStretch (1, 5);
0096   layout->setSpacing (5);
0097   
0098   layout->addWidget (lw, 0, 0, 2, 1);
0099   layout->addWidget (vb, 0, 1);
0100   layout->addWidget (chkSendNothing, 2, 0);
0101   layout->addWidget (chkOffline, 3, 0);
0102   layout->addWidget (buttons, 4, 0, 1, 2);
0103   
0104   //connect signals
0105   connect (addButton, &QPushButton::clicked, this, &dlgConnect::addPressed);
0106   connect (modifyButton, &QPushButton::clicked, this, &dlgConnect::modifyPressed);
0107   connect (deleteButton, &QPushButton::clicked, this, &dlgConnect::deletePressed);
0108   connect (duplicateButton, &QPushButton::clicked, this, &dlgConnect::duplicatePressed);
0109   
0110   connect (lw->selectionModel(), &QItemSelectionModel::selectionChanged, this, &dlgConnect::selectionChanged);
0111   connect (lw, &QTreeView::doubleClicked, this, &dlgConnect::doubleClicked);
0112 
0113   lw->setFocus ();
0114 }
0115 
0116 dlgConnect::~dlgConnect()
0117 {
0118 }
0119 
0120 QSize dlgConnect::sizeHint() const
0121 {
0122   return QSize (500, 400);
0123 }
0124 
0125 QString dlgConnect::selectedProfile ()
0126 {
0127   QItemSelection sel = lw->selectionModel()->selection();
0128   if (sel.empty()) return QString();
0129   QSortFilterProxyModel *model = dynamic_cast<QSortFilterProxyModel *> (lw->model());
0130   if (!model) return QString();
0131   sel = model->mapSelectionToSource (sel);
0132   int idx = sel.indexes().first().row();
0133   return cProfileManager::self()->profileList()[idx];
0134 }
0135 
0136 bool dlgConnect::sendNothing ()
0137 {
0138   return chkSendNothing->isChecked ();
0139 }
0140 
0141 bool dlgConnect::isOffLine()
0142 {
0143   return chkOffline->isChecked ();
0144 }
0145 
0146 void dlgConnect::selectionChanged (const QItemSelection &index)
0147 {
0148   //enable/disable Connect button
0149   QPushButton *button = buttons->button (QDialogButtonBox::Ok);
0150   button->setEnabled (index.indexes().empty() ? false : true);
0151 }
0152 
0153 void dlgConnect::doubleClicked (const QModelIndex &index)
0154 {
0155   if (index.isValid ())
0156     accept();
0157 }
0158 
0159 void dlgConnect::addPressed ()
0160 {
0161   //so first we have to create the dialog...
0162   mdlg = new dlgEditProfile (this);
0163 
0164   //then we connect() all its signals - this handles everything that the dialog offers...
0165   connect (mdlg, &dlgEditProfile::accepted, this, &dlgConnect::doAdd);
0166 
0167   //fill in default login sequence
0168   QStringList ls;
0169   ls.append ("$name");
0170   ls.append ("$password");
0171   mdlg->setConnectionString (ls);
0172 
0173   //dialog is ready - show it!
0174   mdlg->exec ();
0175   delete mdlg;
0176 }
0177 
0178 void dlgConnect::modifyPressed ()
0179 {
0180   cProfileManager *mgr = cProfileManager::self();
0181   QString profile = selectedProfile();
0182   cProfileSettings *sett = mgr->settings (profile);
0183   if (!sett) return;   // no profile selected
0184 
0185   //so first we have to create the dialog...
0186   mdlg = new dlgEditProfile (this);
0187 
0188   //then we connect() all its signals - this handles everything that the dialog offers...
0189   connect (mdlg, &dlgEditProfile::accepted, this, &dlgConnect::doModify);
0190 
0191   mdlg->setName (mgr->visibleProfileName (profile));
0192   mdlg->setServer (sett->getString ("server"));
0193   mdlg->setPort (sett->getInt ("port"));
0194   mdlg->setLogin (sett->getString ("login"));
0195   mdlg->setPassword (sett->getString ("password"));
0196   QStringList conn;
0197   int cnt = sett->getInt ("on-connect-count");
0198   for (int i = 0; i < cnt; ++i)
0199     conn << sett->getString ("on-connect-"+QString::number(i));
0200   mdlg->setConnectionString (conn);
0201 
0202   //dialog is ready - show it!
0203   mdlg->exec ();
0204 
0205   delete mdlg;
0206 }
0207 
0208 void dlgConnect::updateProfileFromDialog (const QString &profile)
0209 {
0210   cProfileManager *mgr = cProfileManager::self();
0211   cProfileSettings *sett = mgr->settings (profile);
0212   if (!sett) return;
0213   sett->setString ("server", mdlg->server());
0214   sett->setInt ("port", mdlg->port());
0215   sett->setString ("login", mdlg->login());
0216   sett->setString ("password", mdlg->password());
0217   QStringList con = mdlg->connectionString();
0218   sett->setInt ("on-connect-count", con.size());
0219   for (int i = 0; i < con.size(); ++i)
0220     sett->setString ("on-connect-"+QString::number(i), con[i]);
0221   sett->save();
0222 
0223   mgr->profileInfoChanged (profile);
0224 }
0225 
0226 void dlgConnect::deletePressed ()
0227 {
0228   cProfileManager *mgr = cProfileManager::self();
0229   QString profile = selectedProfile();
0230   cProfileSettings *sett = mgr->settings (profile);
0231   if (!sett) return;
0232 
0233   // can we do that ?
0234   if (mgr->hasSessionAssigned (profile)) {
0235     KMessageBox::error (this, i18n ("This profile cannot be deleted, because you have a connection open using this profile."), i18n ("Unable to delete"));
0236     return;
0237   }
0238 
0239   if (KMessageBox::questionTwoActions (this, i18n ("Do you really want to delete profile %1?", mgr->visibleProfileName (profile)), i18n ("Delete profile"), KGuiItem(i18n("Delete")), KStandardGuiItem::cancel()) != KMessageBox::PrimaryAction)
0240     return;
0241 
0242   // wants to delete
0243   // TODO: offer the option to also delete the files
0244   mgr->deleteProfile (profile, false);
0245 }
0246 
0247 void dlgConnect::duplicatePressed ()
0248 {
0249   cProfileManager *mgr = cProfileManager::self();
0250   QString profile = selectedProfile();
0251   cProfileSettings *sett = mgr->settings (profile);
0252   if (!sett) return;
0253   
0254   bool ok;
0255   QString newName = QInputDialog::getText (this, i18n ("Duplicate Profile"), i18n ("Please enter name for the duplicated profile"), QLineEdit::Normal, mgr->visibleProfileName (profile), &ok);
0256   if (!mgr->duplicateProfile (profile, newName))
0257     KMessageBox::error (this, i18n ("There was an error trying to duplicate the profile. Please ensure that you have write access to the profile directory."), i18n ("Unable to duplicate"));
0258 }
0259 
0260 void dlgConnect::doAdd ()
0261 {
0262   cProfileManager *mgr = cProfileManager::self();
0263   QString profile = mgr->newProfile (mdlg->name());
0264   updateProfileFromDialog (profile);
0265 }
0266 
0267 void dlgConnect::doModify ()
0268 {
0269   cProfileManager *mgr = cProfileManager::self();
0270   QString profile = selectedProfile();
0271   mgr->renameProfile (profile, mdlg->name());
0272   updateProfileFromDialog (profile);
0273 }
0274 
0275 #include "moc_dlgconnect.cpp"