Warning, file /sdk/cervisia/changelogdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (C) 1999-2002 Bernd Gehrmann
0003  *                          bernd@mail.berlios.de
0004  *  Copyright (c) 2002-2007 Christian Loose <christian.loose@hamburg.de>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0019  */
0020 
0021 #include "changelogdialog.h"
0022 
0023 #include <QDate>
0024 #include <qfile.h>
0025 #include <qtextstream.h>
0026 
0027 #include <KConfigGroup>
0028 #include <KLocalizedString>
0029 #include <KSharedConfig>
0030 #include <kmessagebox.h>
0031 
0032 #include <QDialogButtonBox>
0033 #include <QPlainTextEdit>
0034 #include <QPushButton>
0035 #include <QScrollBar>
0036 #include <QTextBlock>
0037 #include <QVBoxLayout>
0038 
0039 #include "cervisiasettings.h"
0040 #include "misc.h"
0041 
0042 ChangeLogDialog::Options *ChangeLogDialog::options = 0;
0043 
0044 ChangeLogDialog::ChangeLogDialog(KConfig &cfg, QWidget *parent)
0045     : QDialog(parent)
0046     , partConfig(cfg)
0047 {
0048     setWindowTitle(i18n("Edit ChangeLog"));
0049     setModal(true);
0050 
0051     auto mainLayout = new QVBoxLayout;
0052     setLayout(mainLayout);
0053 
0054     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0055     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0056     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0057     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0058     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0059 
0060     edit = new QPlainTextEdit(this);
0061     edit->setFont(CervisiaSettings::changeLogFont());
0062     edit->setFocus();
0063     edit->setLineWrapMode(QPlainTextEdit::NoWrap);
0064     QFontMetrics const fm(edit->fontMetrics());
0065     edit->setMinimumSize(fm.width('0') * 80, fm.lineSpacing() * 20);
0066 
0067     mainLayout->addWidget(edit);
0068     mainLayout->addWidget(buttonBox);
0069 
0070     KConfigGroup cg(&partConfig, "ChangeLogDialog");
0071     restoreGeometry(cg.readEntry<QByteArray>("geometry", QByteArray()));
0072     connect(okButton, SIGNAL(clicked()), this, SLOT(slotOk()));
0073 }
0074 
0075 ChangeLogDialog::~ChangeLogDialog()
0076 {
0077     KConfigGroup cg(&partConfig, "ChangeLogDialog");
0078     cg.writeEntry("geometry", saveGeometry());
0079 }
0080 
0081 void ChangeLogDialog::slotOk()
0082 {
0083     // Write changelog
0084     QFile f(fname);
0085     if (!f.open(QIODevice::ReadWrite)) {
0086         KMessageBox::error(this, i18n("The ChangeLog file could not be written."), "Cervisia");
0087         return;
0088     }
0089 
0090     QTextStream stream(&f);
0091     stream << edit->toPlainText();
0092     f.close();
0093 
0094     QDialog::accept();
0095 }
0096 
0097 bool ChangeLogDialog::readFile(const QString &filename)
0098 {
0099     fname = filename;
0100 
0101     if (!QFile::exists(filename)) {
0102         if (KMessageBox::warningContinueCancel(this, i18n("A ChangeLog file does not exist. Create one?"), i18n("Create")) != KMessageBox::Continue)
0103             return false;
0104     } else {
0105         QFile f(filename);
0106         if (!f.open(QIODevice::ReadWrite)) {
0107             KMessageBox::error(this, i18n("The ChangeLog file could not be read."), "Cervisia");
0108             return false;
0109         }
0110         QTextStream stream(&f);
0111         edit->setPlainText(stream.readAll());
0112         f.close();
0113     }
0114 
0115     KConfigGroup cs(&partConfig, "General");
0116     const QString username = cs.readEntry("Username", Cervisia::UserName());
0117 
0118     edit->insertPlainText(QDate::currentDate().toString(Qt::ISODate) + "  " + username + "\n\n\t* \n\n");
0119     QTextCursor cursor = edit->textCursor();
0120     cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 2);
0121     edit->setTextCursor(cursor);
0122 
0123     edit->verticalScrollBar()->setValue(0); // show top line
0124 
0125     return true;
0126 }
0127 
0128 QString ChangeLogDialog::message()
0129 {
0130     int no = 0;
0131     // Find first line which begins with non-whitespace
0132     while (no < edit->document()->lineCount()) {
0133         QString str = edit->document()->findBlockByLineNumber(no).text();
0134 
0135         if (!str.isEmpty() && !str[0].isSpace())
0136             break;
0137 
0138         ++no;
0139     }
0140     ++no;
0141     // Skip empty lines
0142     while (no < edit->document()->lineCount()) {
0143         QString str = edit->document()->findBlockByLineNumber(no).text();
0144 
0145         if (str.isEmpty() || str == " ")
0146             break;
0147 
0148         ++no;
0149     }
0150     QString res;
0151     // Use all lines until one which begins with non-whitespace
0152     // Remove tabs or 8 whitespace at beginning of each line
0153     while (no < edit->document()->lineCount()) {
0154         QString str = edit->document()->findBlockByLineNumber(no).text();
0155 
0156         if (!str.isEmpty() && !str[0].isSpace())
0157             break;
0158         if (!str.isEmpty() && str[0] == '\t')
0159             str.remove(0, 1);
0160         else {
0161             int j;
0162             for (j = 0; j < (int)str.length(); ++j)
0163                 if (!str[j].isSpace())
0164                     break;
0165             str.remove(0, qMin(j, 8));
0166         }
0167         res += str;
0168         res += '\n';
0169         ++no;
0170     }
0171     // Remove newlines at end
0172     int l;
0173     for (l = res.length() - 1; l > 0; --l)
0174         if (res[l] != '\n')
0175             break;
0176     res.truncate(l + 1);
0177     return res;
0178 }
0179 
0180 // Local Variables:
0181 // c-basic-offset: 4
0182 // End: