changelogdialog.cpp 6.01 KB
Newer Older
Christian Loose's avatar
Christian Loose committed
1
/*
2
 *  Copyright (C) 1999-2002 Bernd Gehrmann
Bernd Gehrmann's avatar
Bernd Gehrmann committed
3
 *                          bernd@mail.berlios.de
4
 *  Copyright (c) 2002-2007 Christian Loose <christian.loose@hamburg.de>
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
10 11 12
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14 15 16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20 21
 */


Matt Rogers's avatar
Matt Rogers committed
22
#include "changelogdialog.h"
23

24
#include <QDate>
25 26
#include <qfile.h>
#include <qtextstream.h>
27

Martin Koller's avatar
Martin Koller committed
28
#include <KSharedConfig>
Waldo Bastian's avatar
Waldo Bastian committed
29
#include <kmessagebox.h>
30
#include <ktextedit.h>
Martin Koller's avatar
Martin Koller committed
31
#include <KConfigGroup>
Martin Koller's avatar
Martin Koller committed
32 33
#include <KLocalizedString>

Martin Koller's avatar
Martin Koller committed
34 35 36
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
37 38

#include "cervisiasettings.h"
39
#include "misc.h"
40

41

Christian Loose's avatar
Christian Loose committed
42
static inline QString DateStringISO8601()
43
{
44
    return QDate::currentDate().toString(Qt::ISODate);
45 46 47 48 49 50
}


ChangeLogDialog::Options *ChangeLogDialog::options = 0;


51
ChangeLogDialog::ChangeLogDialog(KConfig& cfg, QWidget *parent)
Martin Koller's avatar
Martin Koller committed
52
    : QDialog(parent)
53
    , partConfig(cfg)
54
{
Martin Koller's avatar
Martin Koller committed
55
    setWindowTitle(i18n("Edit ChangeLog"));
André Wöbbeking's avatar
André Wöbbeking committed
56
    setModal(true);
Martin Koller's avatar
Martin Koller committed
57 58 59 60 61 62 63 64 65 66 67
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
    QWidget *mainWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    mainLayout->addWidget(mainWidget);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    okButton->setDefault(true);
André Wöbbeking's avatar
André Wöbbeking committed
68

69
    edit = new KTextEdit(this);
70 71
    edit->setAcceptRichText(false);
    edit->setFont(CervisiaSettings::changeLogFont());
72
    edit->setFocus();
73
    edit->setLineWrapMode(KTextEdit::NoWrap);
74 75 76
    QFontMetrics const fm(edit->fontMetrics());
    edit->setMinimumSize(fm.width('0') * 80,
                         fm.lineSpacing() * 20);
77

Martin Koller's avatar
Martin Koller committed
78
    mainLayout->addWidget(edit);
79

Martin Koller's avatar
Martin Koller committed
80 81
    mainLayout->addWidget(buttonBox);

André Wöbbeking's avatar
André Wöbbeking committed
82
    KConfigGroup cg(&partConfig, "ChangeLogDialog");
Martin Koller's avatar
Martin Koller committed
83
    restoreGeometry(cg.readEntry<QByteArray>("geometry", QByteArray()));
Martin Koller's avatar
Martin Koller committed
84
    connect(okButton,SIGNAL(clicked()),this,SLOT(slotOk()));
85 86 87
}


88
ChangeLogDialog::~ChangeLogDialog()
89
{
André Wöbbeking's avatar
André Wöbbeking committed
90
    KConfigGroup cg(&partConfig, "ChangeLogDialog");
Martin Koller's avatar
Martin Koller committed
91
    cg.writeEntry("geometry", saveGeometry());
92
}
93

94 95 96 97 98

void ChangeLogDialog::slotOk()
{
    // Write changelog
    QFile f(fname);
Nicolas Goutte's avatar
Nicolas Goutte committed
99
    if (!f.open(QIODevice::ReadWrite))
100 101 102 103 104 105 106 107
    {
        KMessageBox::sorry(this,
                           i18n("The ChangeLog file could not be written."),
                           "Cervisia");
        return;
    }

    QTextStream stream(&f);
André Wöbbeking's avatar
André Wöbbeking committed
108
    stream << edit->toPlainText();
109 110
    f.close();

Martin Koller's avatar
Martin Koller committed
111
    QDialog::accept();
112 113 114 115 116 117
}


bool ChangeLogDialog::readFile(const QString &filename)
{
    fname = filename;
118 119

    if (!QFile::exists(filename))
120
        {
Waldo Bastian's avatar
Waldo Bastian committed
121
            if (KMessageBox::warningContinueCancel(this,
122
                                         i18n("A ChangeLog file does not exist. Create one?"),
Waldo Bastian's avatar
Waldo Bastian committed
123
                                         i18n("Create")) != KMessageBox::Continue)
124 125 126 127 128
                return false;
        }
    else
        {
            QFile f(filename);
Nicolas Goutte's avatar
Nicolas Goutte committed
129
            if (!f.open(QIODevice::ReadWrite))
130
                {
Waldo Bastian's avatar
Waldo Bastian committed
131 132 133
                    KMessageBox::sorry(this,
                                       i18n("The ChangeLog file could not be read."),
                                       "Cervisia");
134 135 136
                    return false;
                }
            QTextStream stream(&f);
137
            edit->setPlainText(stream.readAll());
138 139 140
            f.close();
        }

Laurent Montel's avatar
Compile  
Laurent Montel committed
141 142
    KConfigGroup cs(&partConfig, "General");
    const QString username = cs.readEntry("Username", Cervisia::UserName());
143

144 145
    edit->insertPlainText(DateStringISO8601() + "  " + username + "\n\n\t* \n\n");
    edit->textCursor().movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2);
Christian Loose's avatar
Christian Loose committed
146

147 148 149 150 151 152
    return true;
}


QString ChangeLogDialog::message()
{
Laurent Montel's avatar
Laurent Montel committed
153
#ifdef __GNUC__
154
#warning disabled to make it compile
Laurent Montel's avatar
Laurent Montel committed
155
#endif
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
//     int no = 0;
//     // Find first line which begins with non-whitespace
//     while (no < edit->lines())
//         {
//             QString str = edit->text(no);
//             if (!str.isEmpty() && !str[0].isSpace())
//                 break;
//             ++no;
//         }
//     ++no;
//     // Skip empty lines
//     while (no < edit->lines())
//         {
//             QString str = edit->text(no);
//             if( str.isEmpty() || str == " " )
//                 break;
//             ++no;
//         }
//     QString res;
//     // Use all lines until one which begins with non-whitespace
//     // Remove tabs or 8 whitespace at beginning of each line
//     while (no < edit->lines())
//         {
//             QString str = edit->text(no);
//             if (!str.isEmpty() && !str[0].isSpace())
//                 break;
//             if (!str.isEmpty() && str[0] == '\t')
//                 str.remove(0, 1);
//             else
//                 {
//                     int j;
//                     for (j = 0; j < (int)str.length(); ++j)
//                         if (!str[j].isSpace())
//                             break;
190
//                     str.remove(0, qMin(j, 8));
191 192 193 194 195 196 197 198 199 200 201 202 203 204
//                 }
//             res += str;
//             res += '\n';
//             ++no;
//         }
//     // Remove newlines at end
//     int l;
//     for (l = res.length()-1; l > 0; --l)
//         if (res[l] != '\n')
//             break;
//     res.truncate(l+1);
//     return res;

    return "";
205 206 207 208 209 210
}


// Local Variables:
// c-basic-offset: 4
// End: