1 /*
2 * Copyright (c) 2018 Jouni Pentikäinen <joupent@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <KisSessionResource.h>
19 #include <KisResourceServerProvider.h>
20 #include <QInputDialog>
21 #include <QMessageBox>
22 #include <KisPart.h>
23 #include "KisSessionManagerDialog.h"
24
KisSessionManagerDialog(QWidget * parent)25 KisSessionManagerDialog::KisSessionManagerDialog(QWidget *parent)
26 : QDialog(parent)
27 {
28 setupUi(this);
29
30 updateSessionList();
31
32 connect(btnNew, SIGNAL(clicked()), this, SLOT(slotNewSession()));
33 connect(btnRename, SIGNAL(clicked()), this, SLOT(slotRenameSession()));
34 connect(btnSwitchTo, SIGNAL(clicked()), this, SLOT(slotSwitchSession()));
35 connect(btnDelete, SIGNAL(clicked()), this, SLOT(slotDeleteSession()));
36 connect(btnClose, SIGNAL(clicked()), this, SLOT(slotClose()));
37
38 connect(lstSessions, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotSessionDoubleClicked(QListWidgetItem*)));
39 }
40
updateSessionList()41 void KisSessionManagerDialog::updateSessionList() {
42 KoResourceServer<KisSessionResource> *server = KisResourceServerProvider::instance()->sessionServer();
43
44 lstSessions->clear();
45 Q_FOREACH(KisSessionResource *session, server->resources()) {
46 lstSessions->addItem(session->name());
47 }
48 }
49
slotNewSession()50 void KisSessionManagerDialog::slotNewSession()
51 {
52 QString name = QInputDialog::getText(this,
53 i18n("Create session"),
54 i18n("Session name:"), QLineEdit::Normal
55 );
56 if (name.isNull() || name.isEmpty()) return;
57
58 auto *session = new KisSessionResource(QString());
59
60 KoResourceServer<KisSessionResource> *server = KisResourceServerProvider::instance()->sessionServer();
61 QString saveLocation = server->saveLocation();
62 QFileInfo fileInfo(saveLocation + name + session->defaultFileExtension());
63 int i = 1;
64 while (fileInfo.exists()) {
65 fileInfo.setFile(saveLocation + name + QString("%1").arg(i) + session->defaultFileExtension());
66 i++;
67 }
68
69 session->setFilename(fileInfo.filePath());
70 session->setName(name);
71 session->storeCurrentWindows();
72
73 server->addResource(session);
74
75 KisPart::instance()->setCurrentSession(session);
76
77 updateSessionList();
78 }
79
slotRenameSession()80 void KisSessionManagerDialog::slotRenameSession()
81 {
82 QString name = QInputDialog::getText(this,
83 i18n("Rename session"),
84 i18n("New name:"), QLineEdit::Normal
85 );
86 if (name.isNull() || name.isEmpty()) return;
87
88 KisSessionResource *session = getSelectedSession();
89 if (!session) return;
90
91 session->setName(name);
92 session->save();
93
94 updateSessionList();
95 }
96
slotSessionDoubleClicked(QListWidgetItem *)97 void KisSessionManagerDialog::slotSessionDoubleClicked(QListWidgetItem* /*item*/)
98 {
99 slotSwitchSession();
100 slotClose();
101 }
102
slotSwitchSession()103 void KisSessionManagerDialog::slotSwitchSession()
104 {
105 KisSessionResource *session = getSelectedSession();
106
107 if (session) {
108 bool closed = KisPart::instance()->closeSession(true);
109 if (closed) {
110 session->restore();
111 }
112 }
113 }
114
getSelectedSession() const115 KisSessionResource *KisSessionManagerDialog::getSelectedSession() const
116 {
117 QListWidgetItem *item = lstSessions->currentItem();
118 if (item) {
119 KoResourceServer<KisSessionResource> *server = KisResourceServerProvider::instance()->sessionServer();
120 return server->resourceByName(item->text());
121 }
122 return nullptr;
123 }
124
slotDeleteSession()125 void KisSessionManagerDialog::slotDeleteSession()
126 {
127 KisSessionResource *session = getSelectedSession();
128 if (!session) return;
129
130 if (QMessageBox::warning(this,
131 i18nc("@title:window", "Krita"),
132 QString(i18n("Permanently delete session %1?", session->name())),
133 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
134
135 KisPart::instance()->setCurrentSession(0);
136 const QString filename = session->filename();
137
138 KoResourceServer<KisSessionResource> *server = KisResourceServerProvider::instance()->sessionServer();
139 server->removeResourceFromServer(session);
140
141 QFile file(filename);
142 file.remove();
143
144 updateSessionList();
145 }
146 }
147
slotClose()148 void KisSessionManagerDialog::slotClose()
149 {
150 hide();
151 }
152