File indexing completed on 2024-04-21 04:58:20
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2008 Eduardo Robles Elvira <edulix@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "konqsessiondlg.h" 0008 #include "konqsettingsxt.h" 0009 #include "konqviewmanager.h" 0010 #include "konqsessionmanager.h" 0011 #include "konqmainwindow.h" 0012 #include "ui_konqsessiondlg_base.h" 0013 #include "ui_konqnewsessiondlg_base.h" 0014 0015 #include <QDir> 0016 #include <QDirIterator> 0017 #include <QFileInfo> 0018 #include <QIcon> 0019 #include <QPushButton> 0020 #include <QStandardPaths> 0021 0022 #include "konqdebug.h" 0023 #include <kio/copyjob.h> 0024 #include <kio/renamedialog.h> 0025 #include <kfileitemdelegate.h> 0026 #include <kdirlister.h> 0027 #include <kdirmodel.h> 0028 #include <kstandardguiitem.h> 0029 #include <kio/global.h> 0030 #include <KLocalizedString> 0031 #include <kconfig.h> 0032 #include <kseparator.h> 0033 #include <kmessagebox.h> 0034 #include <KConfigGroup> 0035 #include <QDialogButtonBox> 0036 #include <KGuiItem> 0037 #include <QVBoxLayout> 0038 0039 class KonqSessionDlg::KonqSessionDlgPrivate : public QWidget, 0040 public Ui::KonqSessionDlgBase 0041 { 0042 public: 0043 KonqSessionDlgPrivate(KonqViewManager *manager, QWidget *parent = nullptr) 0044 : QWidget(parent), m_pViewManager(manager), m_pParent(parent) 0045 { 0046 setupUi(this); 0047 } 0048 KonqViewManager *const m_pViewManager; 0049 KDirModel *m_pModel; 0050 QWidget *m_pParent; 0051 QDialogButtonBox *m_buttonBox; 0052 }; 0053 0054 KonqSessionDlg::KonqSessionDlg(KonqViewManager *manager, QWidget *parent) 0055 : QDialog(parent) 0056 , d(new KonqSessionDlgPrivate(manager, this)) 0057 { 0058 setObjectName(QStringLiteral("konq_session_dialog")); 0059 setModal(true); 0060 setWindowTitle(i18nc("@title:window", "Manage Sessions")); 0061 0062 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0063 mainLayout->addWidget(d); 0064 0065 d->m_pSaveCurrentButton->setIcon(QIcon::fromTheme(QStringLiteral("document-save"))); 0066 d->m_pRenameButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename"))); 0067 d->m_pDeleteButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete"))); 0068 d->m_pNewButton->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); 0069 0070 QString dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/sessions/"); 0071 QDir().mkpath(dir); 0072 0073 d->m_pModel = new KDirModel(d->m_pListView); 0074 d->m_pModel->sort(QDir::Name); 0075 d->m_pModel->dirLister()->setDirOnlyMode(true); 0076 d->m_pModel->dirLister()->openUrl(QUrl::fromLocalFile(dir)); 0077 d->m_pListView->setModel(d->m_pModel); 0078 0079 d->m_pListView->setMinimumSize(d->m_pListView->sizeHint()); 0080 0081 connect(d->m_pListView->selectionModel(), SIGNAL(selectionChanged( 0082 const QItemSelection &, const QItemSelection &)), this, SLOT( 0083 slotSelectionChanged())); 0084 0085 d->m_pOpenTabsInsideCurrentWindow->setChecked( 0086 KonqSettings::openTabsInsideCurrentWindow()); 0087 0088 connect(d->m_pNewButton, &QPushButton::clicked, this, &KonqSessionDlg::slotNew); 0089 connect(d->m_pSaveCurrentButton, &QPushButton::clicked, this, &KonqSessionDlg::slotSave); 0090 connect(d->m_pRenameButton, SIGNAL(clicked()), SLOT(slotRename())); 0091 connect(d->m_pDeleteButton, &QPushButton::clicked, this, &KonqSessionDlg::slotDelete); 0092 0093 d->m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Open | QDialogButtonBox::Close); 0094 connect(d->m_buttonBox, SIGNAL(accepted()), this, SLOT(accept())); 0095 connect(d->m_buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 0096 mainLayout->addWidget(d->m_buttonBox); 0097 d->m_buttonBox->button(QDialogButtonBox::Close)->setDefault(true); 0098 QPushButton *openButton = d->m_buttonBox->button(QDialogButtonBox::Open); 0099 connect(openButton, &QPushButton::clicked, this, &KonqSessionDlg::slotOpen); 0100 0101 slotSelectionChanged(); 0102 0103 } 0104 0105 KonqSessionDlg::~KonqSessionDlg() 0106 { 0107 KonqSettings::setOpenTabsInsideCurrentWindow( 0108 d->m_pOpenTabsInsideCurrentWindow->isChecked()); 0109 } 0110 0111 void KonqSessionDlg::slotOpen() 0112 { 0113 if (!d->m_pListView->currentIndex().isValid()) { 0114 return; 0115 } 0116 0117 KonqSessionManager::self()->restoreSessions(d->m_pModel->itemForIndex( 0118 d->m_pListView->currentIndex()).url().path(), 0119 d->m_pOpenTabsInsideCurrentWindow->isChecked(), 0120 d->m_pViewManager->mainWindow()); 0121 close(); 0122 } 0123 0124 void KonqSessionDlg::slotSave() 0125 { 0126 if (!d->m_pListView->currentIndex().isValid()) { 0127 return; 0128 } 0129 0130 QFileInfo fileInfo( 0131 d->m_pModel->itemForIndex(d->m_pListView->currentIndex()).url().path()); 0132 0133 KonqNewSessionDlg newDialog(this, d->m_pViewManager->mainWindow(), 0134 KIO::encodeFileName(fileInfo.fileName()), KonqNewSessionDlg::ReplaceFile); 0135 0136 newDialog.exec(); 0137 } 0138 0139 void KonqSessionDlg::slotNew() 0140 { 0141 KonqNewSessionDlg newDialog(this, d->m_pViewManager->mainWindow()); 0142 newDialog.exec(); 0143 } 0144 0145 void KonqSessionDlg::slotDelete() 0146 { 0147 if (!d->m_pListView->currentIndex().isValid()) { 0148 return; 0149 } 0150 0151 const QString dir = d->m_pModel->itemForIndex(d->m_pListView->currentIndex()).url().toLocalFile(); 0152 if (!QDir(dir).removeRecursively()) { 0153 // TODO show error msg box 0154 } 0155 } 0156 0157 void KonqSessionDlg::slotRename(QUrl dirpathTo) 0158 { 0159 if (!d->m_pListView->currentIndex().isValid()) { 0160 return; 0161 } 0162 0163 QUrl dirpathFrom = d->m_pModel->itemForIndex( 0164 d->m_pListView->currentIndex()).url(); 0165 0166 dirpathTo = (dirpathTo == QUrl()) ? dirpathFrom : dirpathTo; 0167 0168 KIO::RenameDialog dlg(this, i18nc("@title:window", "Rename Session"), dirpathFrom, 0169 dirpathTo, KIO::RenameDialog_Options()); 0170 0171 if (dlg.exec() == KIO::Result_Rename) { 0172 dirpathTo = dlg.newDestUrl(); 0173 QDir dir(dirpathTo.path()); 0174 if (dir.exists()) { 0175 slotRename(dirpathTo); 0176 } else { 0177 QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/sessions/")); 0178 dir.rename(dirpathFrom.fileName(), dlg.newDestUrl().fileName()); 0179 } 0180 } 0181 } 0182 0183 void KonqSessionDlg::slotSelectionChanged() 0184 { 0185 bool enable = !d->m_pListView->selectionModel()->selectedIndexes().isEmpty(); 0186 d->m_pSaveCurrentButton->setEnabled(enable); 0187 d->m_pRenameButton->setEnabled(enable); 0188 d->m_pDeleteButton->setEnabled(enable); 0189 QPushButton *openButton = d->m_buttonBox->button(QDialogButtonBox::Open); 0190 openButton->setEnabled(enable); 0191 } 0192 0193 class KonqNewSessionDlg::KonqNewSessionDlgPrivate : public QWidget, 0194 public Ui::KonqNewSessionDlgBase 0195 { 0196 public: 0197 KonqNewSessionDlgPrivate(QWidget *parent = nullptr, KonqMainWindow *mainWindow = nullptr, 0198 KonqNewSessionDlg::Mode m = KonqNewSessionDlg::NewFile) 0199 : QWidget(parent), m_pParent(parent), m_mainWindow(mainWindow), m_mode(m) 0200 { 0201 setupUi(this); 0202 } 0203 QWidget *m_pParent; 0204 KonqMainWindow *m_mainWindow; 0205 KonqNewSessionDlg::Mode m_mode; 0206 QDialogButtonBox *m_buttonBox; 0207 }; 0208 0209 KonqNewSessionDlg::KonqNewSessionDlg(QWidget *parent, KonqMainWindow *mainWindow, QString sessionName, Mode mode) 0210 : QDialog(parent) 0211 , d(new KonqNewSessionDlgPrivate(this, mainWindow, mode)) 0212 { 0213 setObjectName(QStringLiteral("konq_new_session_dialog")); 0214 setModal(true); 0215 setWindowTitle(i18nc("@title:window", "Save Session")); 0216 0217 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0218 mainLayout->addWidget(d); 0219 0220 d->m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); 0221 mainLayout->addWidget(d->m_buttonBox); 0222 QPushButton *okButton = d->m_buttonBox->button(QDialogButtonBox::Ok); 0223 okButton->setDefault(true); 0224 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0225 okButton->setEnabled(false); 0226 0227 if (!sessionName.isEmpty()) { 0228 d->m_pSessionName->setText(sessionName); 0229 okButton->setEnabled(true); 0230 } 0231 0232 d->m_pSessionName->setFocus(); 0233 0234 connect(okButton, &QPushButton::clicked, this, &KonqNewSessionDlg::slotAddSession); 0235 connect(d->m_pSessionName, SIGNAL(textChanged(QString)), this, 0236 SLOT(slotTextChanged(QString))); 0237 0238 resize(sizeHint()); 0239 connect(d->m_buttonBox, SIGNAL(accepted()), this, SLOT(accept())); 0240 connect(d->m_buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 0241 } 0242 0243 void KonqNewSessionDlg::slotAddSession() 0244 { 0245 QString dirpath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/sessions/") + KIO::encodeFileName(d->m_pSessionName->text()); 0246 0247 QDir dir(dirpath); 0248 if (dir.exists()) { 0249 if ((d->m_mode == ReplaceFile) || 0250 KMessageBox::questionTwoActions(this, 0251 i18n("A session with the name '%1' already exists, do you want to overwrite it?", d->m_pSessionName->text()), 0252 i18nc("@title:window", "Session exists. Overwrite?"), 0253 KStandardGuiItem::overwrite(), 0254 KStandardGuiItem::cancel()) == KMessageBox::PrimaryAction) { 0255 QDir(dirpath).removeRecursively(); 0256 } else { 0257 return; 0258 } 0259 } 0260 0261 if (d->m_pAllWindows->isChecked()) { 0262 KonqSessionManager::self()->saveCurrentSessions(dirpath); 0263 } else { 0264 KonqSessionManager::self()->saveCurrentSessionToFile(dirpath + QLatin1String("/1"), d->m_mainWindow); 0265 } 0266 } 0267 0268 void KonqNewSessionDlg::slotTextChanged(const QString &text) 0269 { 0270 QPushButton *okButton = d->m_buttonBox->button(QDialogButtonBox::Ok); 0271 okButton->setEnabled(!text.isEmpty()); 0272 } 0273 0274 KonqNewSessionDlg::~KonqNewSessionDlg() 0275 { 0276 } 0277