File indexing completed on 2024-05-26 16:14:44
0001 /* This file is part of the KDE project 0002 Copyright (C) 2005 Laurent Montel <montel@kde.org> 0003 Copyright (C) 2006 Fredrik Edemar <f_edemar@linux.se> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "KoVersionDialog.h" 0022 0023 #include "KoMainWindow.h" 0024 #include "KoDocumentEntry.h" 0025 #include "KoPart.h" 0026 0027 #include <QFile> 0028 #include <QGridLayout> 0029 #include <QHBoxLayout> 0030 #include <QLabel> 0031 #include <QLayout> 0032 #include <QList> 0033 #include <QPushButton> 0034 #include <QToolButton> 0035 #include <QTreeWidget> 0036 0037 #include <MainDebug.h> 0038 #include <klocalizedstring.h> 0039 #include <kmessagebox.h> 0040 #include <QTemporaryFile> 0041 0042 #include <QTextEdit> 0043 0044 0045 KoVersionDialog::KoVersionDialog(QWidget* parent, KoDocument *doc) 0046 : KoDialog(parent) 0047 { 0048 setCaption(i18n("Version")); 0049 setButtons(Close); 0050 setDefaultButton(Close); 0051 m_doc = doc; 0052 0053 QWidget* page = new QWidget(this); 0054 setMainWidget(page); 0055 setModal(true); 0056 0057 QGridLayout* grid1 = new QGridLayout(page); 0058 0059 list = new QTreeWidget(page); 0060 list->setColumnCount(3); 0061 QStringList h; 0062 h.append(i18n("Date & Time")); 0063 h.append(i18n("Saved By")); 0064 h.append(i18n("Comment")); 0065 list->setHeaderLabels(h); 0066 0067 updateVersionList(); 0068 0069 grid1->addWidget(list, 0, 0, 9, 1); 0070 0071 m_pAdd = new QPushButton(i18n("&Add"), page); 0072 grid1->addWidget(m_pAdd, 1, 2); 0073 0074 m_pRemove = new QPushButton(i18n("&Remove"), page); 0075 grid1->addWidget(m_pRemove, 2, 2); 0076 0077 m_pModify = new QPushButton(i18n("&Modify"), page); 0078 grid1->addWidget(m_pModify, 3, 2); 0079 0080 m_pOpen = new QPushButton(i18n("&Open"), page); 0081 grid1->addWidget(m_pOpen, 4, 2); 0082 0083 0084 connect(m_pRemove, SIGNAL(clicked()), this, SLOT(slotRemove())); 0085 connect(m_pAdd, SIGNAL(clicked()), this, SLOT(slotAdd())); 0086 connect(m_pOpen, SIGNAL(clicked()), this, SLOT(slotOpen())); 0087 connect(m_pModify, SIGNAL(clicked()), this, SLOT(slotModify())); 0088 0089 updateButton(); 0090 0091 resize(600, 250); 0092 0093 } 0094 0095 KoVersionDialog::~KoVersionDialog() 0096 { 0097 } 0098 0099 void KoVersionDialog::updateVersionList() 0100 { 0101 list->clear(); 0102 // add all versions to the tree widget 0103 QList<KoVersionInfo> versions = m_doc->versionList(); 0104 QList<QTreeWidgetItem *> items; 0105 for (int i = 0; i < versions.size(); ++i) { 0106 QStringList l; 0107 l.append(versions.at(i).date.toString()); 0108 l.append(versions.at(i).saved_by); 0109 l.append(versions.at(i).comment); 0110 items.append(new QTreeWidgetItem(l)); 0111 } 0112 list->insertTopLevelItems(0, items); 0113 } 0114 0115 0116 void KoVersionDialog::updateButton() 0117 { 0118 #if 0 0119 bool state = (list->currentItem() >= 0); 0120 m_pRemove->setEnabled(state); 0121 #endif 0122 } 0123 0124 void KoVersionDialog::slotAdd() 0125 { 0126 KoVersionModifyDialog * dlg = new KoVersionModifyDialog(this, 0); 0127 if (!dlg->exec()) { 0128 delete dlg; 0129 return; 0130 } 0131 0132 if (!m_doc->addVersion(dlg->comment())) 0133 KMessageBox::error(this, i18n("A new version could not be added")); 0134 0135 delete dlg; 0136 0137 updateVersionList(); 0138 } 0139 0140 void KoVersionDialog::slotRemove() 0141 { 0142 if (!list->currentItem()) 0143 return; 0144 0145 for (int i = 0; i < m_doc->versionList().size(); ++i) { 0146 if (m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0)) { 0147 m_doc->versionList().takeAt(i); 0148 delete list->currentItem(); 0149 return; 0150 } 0151 } 0152 } 0153 0154 void KoVersionDialog::slotModify() 0155 { 0156 if (!list->currentItem()) 0157 return; 0158 0159 KoVersionInfo *version = 0; 0160 for (int i = 0; i < m_doc->versionList().size(); ++i) { 0161 if (m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0)) { 0162 version = &m_doc->versionList()[i]; 0163 break; 0164 } 0165 } 0166 if (!version) 0167 return; 0168 0169 KoVersionModifyDialog * dlg = new KoVersionModifyDialog(this, version); 0170 if (dlg->exec()) { 0171 version->comment = dlg->comment(); 0172 list->currentItem()->setText(2, version->comment); 0173 } 0174 delete dlg; 0175 0176 } 0177 0178 void KoVersionDialog::slotOpen() 0179 { 0180 if (!list->currentItem()) 0181 return; 0182 0183 KoVersionInfo *version = 0; 0184 for (int i = 0; i < m_doc->versionList().size(); ++i) { 0185 if (m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0)) { 0186 version = &m_doc->versionList()[i]; 0187 break; 0188 } 0189 } 0190 if (!version) 0191 return; 0192 0193 QTemporaryFile tmp; 0194 tmp.setAutoRemove(false); 0195 tmp.open(); 0196 tmp.write(version->data); 0197 tmp.flush(); 0198 tmp.setPermissions(QFile::ReadUser); 0199 tmp.flush(); 0200 0201 if (!m_doc->documentPart()->mainWindows().isEmpty()) { //open the version in a new window if possible 0202 KoDocumentEntry entry = KoDocumentEntry::queryByMimeType(m_doc->nativeOasisMimeType()); 0203 if (entry.isEmpty()) { 0204 entry = KoDocumentEntry::queryByMimeType(m_doc->nativeFormatMimeType()); 0205 } 0206 Q_ASSERT(!entry.isEmpty()); 0207 QString errorMsg; 0208 KoPart *part= entry.createKoPart(&errorMsg); 0209 if (!part) { 0210 if (!errorMsg.isEmpty()) 0211 KMessageBox::error(0, errorMsg); 0212 return; 0213 } 0214 KoMainWindow *mainWindow = part->createMainWindow(); 0215 mainWindow ->openDocument(QUrl::fromLocalFile(tmp.fileName())); 0216 mainWindow ->show(); 0217 } else { 0218 m_doc->openUrl(QUrl::fromUserInput(tmp.fileName())); 0219 } 0220 0221 tmp.setAutoRemove(true); 0222 slotButtonClicked(Close); 0223 } 0224 0225 KoVersionModifyDialog::KoVersionModifyDialog(QWidget* parent, KoVersionInfo *info) 0226 : KoDialog(parent) 0227 { 0228 setCaption(i18n("Comment")); 0229 setButtons(Ok | Cancel); 0230 setDefaultButton(Ok); 0231 setModal(true); 0232 0233 QWidget* page = new QWidget(this); 0234 setMainWidget(page); 0235 0236 QVBoxLayout *grid1 = new QVBoxLayout(page); 0237 0238 QLabel *l = new QLabel(page); 0239 if (info) 0240 l->setText(i18n("Date: %1", info->date.toString())); 0241 else 0242 l->setText(i18n("Date: %1", QDateTime::currentDateTime().toString(Qt::ISODate))); 0243 grid1->addWidget(l); 0244 0245 m_textEdit = new QTextEdit(page); 0246 if (info) 0247 m_textEdit->setText(info->comment); 0248 grid1->addWidget(m_textEdit); 0249 0250 } 0251 0252 QString KoVersionModifyDialog::comment() const 0253 { 0254 return m_textEdit->toPlainText(); 0255 }