katesessionmanagedialog.cpp 14.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  SPDX-License-Identifier: LGPL-2.0-or-later

    Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
20
21
22

#include "katesessionmanagedialog.h"

Christoph Cullmann's avatar
Christoph Cullmann committed
23
#include "kateapp.h"
24
#include "katesessionchooseritem.h"
25
#include "katesessionmanager.h"
26

27
28
#include <KColorScheme>
#include <KConfigGroup>
29
30
#include <KLocalizedString>
#include <KMessageBox>
31
#include <KSharedConfig>
32
33
34
35
#include <KStandardGuiItem>

#include <QInputDialog>

36
KateSessionManageDialog::KateSessionManageDialog(QWidget *parent)
37
38
    : QDialog(parent)
{
39
    setupUi(this);
40
    setWindowTitle(i18n("Manage Sessions"));
41
    m_dontAskCheckBox->hide();
42

43
44
45
46
    m_sessionList->installEventFilter(this);
    connect(m_sessionList, &QTreeWidget::currentItemChanged, this, &KateSessionManageDialog::selectionChanged);
    connect(m_sessionList, &QTreeWidget::itemDoubleClicked, this, &KateSessionManageDialog::openSession);
    m_sessionList->header()->moveSection(0, 1); // Re-order columns to "Files, Sessions"
47

48
49
    m_filterBox->installEventFilter(this);
    connect(m_filterBox, &QLineEdit::textChanged, this, &KateSessionManageDialog::filterChanged);
50

51
    connect(m_newButton, &QPushButton::clicked, this, &KateSessionManageDialog::openNewSession);
52

53
54
55
    KGuiItem::assign(m_openButton, KStandardGuiItem::open());
    m_openButton->setDefault(true);
    connect(m_openButton, &QPushButton::clicked, this, &KateSessionManageDialog::openSession);
56

57
    connect(m_templateButton, &QPushButton::clicked, this, &KateSessionManageDialog::openSessionAsTemplate);
58

59
    connect(m_copyButton, &QPushButton::clicked, this, &KateSessionManageDialog::copySession);
60

61
    connect(m_renameButton, &QPushButton::clicked, this, &KateSessionManageDialog::editBegin);
62

63
    connect(m_deleteButton, &QPushButton::clicked, this, &KateSessionManageDialog::updateDeleteList);
64

65
66
    KGuiItem::assign(m_closeButton, KStandardGuiItem::close());
    connect(m_closeButton, &QPushButton::clicked, this, &KateSessionManageDialog::closeDialog);
67

68
    connect(KateApp::self()->sessionManager(), &KateSessionManager::sessionListChanged, this, &KateSessionManageDialog::updateSessionList);
69
70

    updateSessionList();
71
}
72

73
74
75
76
77
78
79
80
KateSessionManageDialog::KateSessionManageDialog(QWidget *parent, const QString &lastSession)
    : KateSessionManageDialog(parent)
{
    setWindowTitle(i18n("Session Chooser"));
    m_dontAskCheckBox->show();
    m_chooserMode = true;
    connect(m_dontAskCheckBox, &QCheckBox::toggled, this, &KateSessionManageDialog::dontAskToggled);

81
82
    m_preferredSession = lastSession;
    updateSessionList();
83
84
}

85
KateSessionManageDialog::~KateSessionManageDialog()
86
87
{
}
88
89

void KateSessionManageDialog::dontAskToggled()
90
{
91
    m_templateButton->setEnabled(!m_dontAskCheckBox->isChecked());
92
93
}

94
void KateSessionManageDialog::filterChanged()
95
{
96
    static QPointer<QTimer> delay;
97

98
99
100
101
102
103
104
105
106
107
108
109
    if (!delay) {
        delay = new QTimer(this); // Should be auto cleard by Qt when we die
        delay->setSingleShot(true);
        delay->setInterval(400);
        connect(delay, &QTimer::timeout, this, &KateSessionManageDialog::updateSessionList);
    }

    delay->start();
}

void KateSessionManageDialog::done(int result)
{
110
    for (const auto &session : qAsConst(m_deleteList)) {
111
112
113
114
115
116
        KateApp::self()->sessionManager()->deleteSession(session);
    }
    m_deleteList.clear(); // May not needed, but anyway

    if (ResultQuit == result) {
        QDialog::done(0);
117
118
        return;
    }
119

120
121
122
123
    if (m_chooserMode && m_dontAskCheckBox->isChecked()) {
        // write back our nice boolean :)
        KConfigGroup generalConfig(KSharedConfig::openConfig(), QStringLiteral("General"));
        switch (result) {
124
125
126
127
128
129
130
131
        case ResultOpen:
            generalConfig.writeEntry("Startup Session", "last");
            break;
        case ResultNew:
            generalConfig.writeEntry("Startup Session", "new");
            break;
        default:
            break;
132
133
134
        }
        generalConfig.sync();
    }
135

136
137
138
139
140
141
142
143
144
    QDialog::done(1);
}

void KateSessionManageDialog::selectionChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    Q_UNUSED(previous);

    if (m_editByUser) {
        editDone(); // Field was left unchanged, no need to apply
145
146
        return;
    }
147

148
149
150
151
152
153
    if (!current) {
        m_openButton->setEnabled(false);
        m_templateButton->setEnabled(false);
        m_copyButton->setEnabled(false);
        m_renameButton->setEnabled(false);
        m_deleteButton->setEnabled(false);
154
155
        return;
    }
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    const KateSession::Ptr activeSession = KateApp::self()->sessionManager()->activeSession();
    const bool notActiveSession = !KateApp::self()->sessionManager()->sessionIsActive(currentSelectedSession()->name());

    m_deleteButton->setEnabled(notActiveSession);

    if (m_deleteList.contains(currentSelectedSession())) {
        m_deleteButton->setText(i18n("Restore"));
        m_openButton->setEnabled(false);
        m_templateButton->setEnabled(false);
        m_copyButton->setEnabled(true); // Looks a little strange but is OK
        m_renameButton->setEnabled(false);
    } else {
        KGuiItem::assign(m_deleteButton, KStandardGuiItem::del());
        m_openButton->setEnabled(currentSelectedSession() != activeSession);
        m_templateButton->setEnabled(true);
        m_copyButton->setEnabled(true);
        m_renameButton->setEnabled(true);
174
    }
175
176
}

177
178
179
180
181
182
183
184
185
186
187
188
189
190
void KateSessionManageDialog::disableButtons()
{
    m_openButton->setEnabled(false);
    m_newButton->setEnabled(false);
    m_templateButton->setEnabled(false);
    m_dontAskCheckBox->setEnabled(false);
    m_copyButton->setEnabled(false);
    m_renameButton->setEnabled(false);
    m_deleteButton->setEnabled(false);
    m_closeButton->setEnabled(false);
    m_filterBox->setEnabled(false);
}

void KateSessionManageDialog::editBegin()
191
{
192
193
194
195
196
    if (m_editByUser) {
        return;
    }

    KateSessionChooserItem *item = currentSessionItem();
197

198
199
200
    if (!item) {
        return;
    }
201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    disableButtons();

    item->setFlags(item->flags() | Qt::ItemIsEditable);
    m_sessionList->clearSelection();
    m_sessionList->editItem(item, 0);

    // Always apply changes user did, like Dolphin
    connect(m_sessionList, &QTreeWidget::itemChanged, this, &KateSessionManageDialog::editApply);
    connect(m_sessionList->itemWidget(item, 0), &QObject::destroyed, this, &KateSessionManageDialog::editApply);

    m_editByUser = item; // Do it last to block eventFilter() actions until we are ready
}

void KateSessionManageDialog::editDone()
{
    m_editByUser = nullptr;
    disconnect(m_sessionList, &QTreeWidget::itemChanged, this, &KateSessionManageDialog::editApply);
219
    updateSessionList();
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

    m_newButton->setEnabled(true);
    m_dontAskCheckBox->setEnabled(true);
    m_closeButton->setEnabled(true);
    m_filterBox->setEnabled(true);

    m_sessionList->setFocus();
}

void KateSessionManageDialog::editApply()
{
    if (!m_editByUser) {
        return;
    }

    KateApp::self()->sessionManager()->renameSession(m_editByUser->session, m_editByUser->text(0));
    editDone();
237
238
}

239
void KateSessionManageDialog::copySession()
240
{
241
242
243
244
245
246
    KateSessionChooserItem *item = currentSessionItem();

    if (!item) {
        return;
    }

247
    m_preferredSession = KateApp::self()->sessionManager()->copySession(item->session);
248
249
250
251
252
253
    m_sessionList->setFocus(); // Only needed when user abort
}

void KateSessionManageDialog::openSession()
{
    KateSessionChooserItem *item = currentSessionItem();
254

255
256
257
    if (!item) {
        return;
    }
258

259
    hide();
260
261
262
263

    // this might fail, e.g. if session is in use, then e.g. end kate, bug 390740
    const bool success = KateApp::self()->sessionManager()->activateSession(item->session);
    done(success ? ResultOpen : ResultQuit);
264
265
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
void KateSessionManageDialog::openSessionAsTemplate()
{
    KateSessionChooserItem *item = currentSessionItem();

    if (!item) {
        return;
    }

    hide();

    KateSessionManager *sm = KateApp::self()->sessionManager();
    KateSession::Ptr ns = KateSession::createAnonymousFrom(item->session, sm->anonymousSessionFile());
    sm->activateSession(ns);

    done(ResultOpen);
}

void KateSessionManageDialog::openNewSession()
{
    hide();
    KateApp::self()->sessionManager()->sessionNew();
    done(ResultNew);
}

void KateSessionManageDialog::updateDeleteList()
{
    KateSessionChooserItem *item = currentSessionItem();

    if (!item) {
        return;
    }

    const KateSession::Ptr session = item->session;
    if (m_deleteList.contains(session)) {
        m_deleteList.remove(session);
        item->setForeground(0, QBrush(KColorScheme(QPalette::Active).foreground(KColorScheme::NormalText).color()));
        item->setIcon(0, QIcon());
        item->setToolTip(0, QString());
    } else {
        m_deleteList.insert(session);
        markItemAsToBeDeleted(item);
    }

    // To ease multiple deletions, move the selection
    QTreeWidgetItem *newItem = m_sessionList->itemBelow(item) ? m_sessionList->itemBelow(item) : m_sessionList->topLevelItem(0);
    m_sessionList->setCurrentItem(newItem);
    m_sessionList->setFocus();
}

void KateSessionManageDialog::markItemAsToBeDeleted(QTreeWidgetItem *item)
{
    item->setForeground(0, QBrush(KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText).color()));
    item->setIcon(0, QIcon::fromTheme(QStringLiteral("emblem-warning")));
    item->setToolTip(0, i18n("Session will be deleted on dialog close"));
}

void KateSessionManageDialog::closeDialog()
{
    done(ResultQuit);
}

327
void KateSessionManageDialog::updateSessionList()
328
{
329
330
331
332
333
334
335
336
337
    if (m_editByUser) {
        // Don't crash accidentally an ongoing edit
        return;
    }

    KateSession::Ptr currSelSession = currentSelectedSession();
    KateSession::Ptr activeSession = KateApp::self()->sessionManager()->activeSession();

    m_sessionList->clear();
338

Christoph Cullmann's avatar
Christoph Cullmann committed
339
    KateSessionList slist = KateApp::self()->sessionManager()->sessionList();
340
341
342
343
    // SortAlphabetical:
    // std::sort(slist.begin(), slist.end(), KateSession::compareByName);
    // SortChronological:
    // std::sort(slist.begin(), slist.end(), KateSession::compareByTimeDesc);
344

345
    KateSessionChooserItem *preferredItem = nullptr;
346
    KateSessionChooserItem *currSessionItem = nullptr;
347
    KateSessionChooserItem *activeSessionItem = nullptr;
348
349
350
351
352
353
354
355
356
357
358
359
360

    for (const KateSession::Ptr &session : qAsConst(slist)) {
        if (!m_filterBox->text().isEmpty()) {
            if (!session->name().contains(m_filterBox->text(), Qt::CaseInsensitive)) {
                continue;
            }
        }

        KateSessionChooserItem *item = new KateSessionChooserItem(m_sessionList, session);
        if (session == currSelSession) {
            currSessionItem = item;
        } else if (session == activeSession) {
            activeSessionItem = item;
361
362
363
        } else if (session->name() == m_preferredSession) {
            preferredItem = item;
            m_preferredSession.clear();
364
365
366
367
368
369
        }

        if (m_deleteList.contains(session)) {
            markItemAsToBeDeleted(item);
        }
    }
370

371
372
373
374
    m_sessionList->header()->setStretchLastSection(false);
    m_sessionList->header()->setSectionResizeMode(0, QHeaderView::Stretch); // stretch "Session Name" column
    m_sessionList->resizeColumnToContents(1); // Fit "Files" column
    m_sessionList->resizeColumnToContents(2); // Fit "Last update" column
375
    m_sessionList->sortByColumn(2, Qt::DescendingOrder); // sort by "Last update" column.. don't worry, it only sorts when the model data changes.
376
377
378

    if (!preferredItem) {
        preferredItem = currSessionItem ? currSessionItem : activeSessionItem;
379
380
    }

381
382
383
    if (preferredItem) {
        m_sessionList->setCurrentItem(preferredItem);
        m_sessionList->scrollToItem(preferredItem);
384
385
386
387
    } else if (m_sessionList->topLevelItemCount() > 0) {
        m_sessionList->setCurrentItem(m_sessionList->topLevelItem(0));
    }

388
    if (m_filterBox->hasFocus()) {
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
        return;
    }

    if (m_sessionList->topLevelItemCount() == 0) {
        m_newButton->setFocus();
    } else {
        m_sessionList->setFocus();
    }
}

KateSessionChooserItem *KateSessionManageDialog::currentSessionItem() const
{
    return static_cast<KateSessionChooserItem *>(m_sessionList->currentItem());
}

KateSession::Ptr KateSessionManageDialog::currentSelectedSession() const
{
    KateSessionChooserItem *item = currentSessionItem();

    if (!item) {
        return KateSession::Ptr();
410
    }
411
412

    return item->session;
413
414
}

415
416
bool KateSessionManageDialog::eventFilter(QObject *object, QEvent *event)
{
417
    QKeyEvent *ke = static_cast<QKeyEvent *>(event);
418
419
420
421
422
423
424
425

    if (object == m_sessionList) {
        if (!m_editByUser) { // No need for further action
            return false;
        }

        if (event->type() == QEvent::KeyPress) {
            switch (ke->key()) {
426
427
428
429
430
431
432
433
            // Avoid to apply changes with untypical keys/don't left edit field this way
            case Qt::Key_Up:
            case Qt::Key_Down:
            case Qt::Key_PageUp:
            case Qt::Key_PageDown:
                return true;
            default:
                break;
434
435
436
437
            }

        } else if (event->type() == QEvent::KeyRelease) {
            switch (ke->key()) {
438
439
440
441
442
443
444
445
            case Qt::Key_Escape:
                editDone(); // Abort edit
                break;
            case Qt::Key_Return:
                editApply();
                break;
            default:
                break;
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
            }
        }

    } else if (object == m_filterBox) {
        // Catch Return key to avoid to finish the dialog
        if (event->type() == QEvent::KeyPress && (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)) {
            updateSessionList();
            m_sessionList->setFocus();
            return true;
        }
    }

    return false;
}

void KateSessionManageDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);

    if (m_editByUser) {
        // We must catch closeEvent here due to connected signal of QLineEdit::destroyed->editApply()->crash!
        editDone(); // editApply() don't work, m_editByUser->text(0) will not updated from QLineEdit
    }
}