summaryrefslogtreecommitdiffstats
path: root/konversation/src/logfilereader.cpp
blob: 22357f4ee8788a2257a482bea6e41b0fadaaca26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
/*
  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.
*/

/*
  Shows the content of a log file
  begin:     Fri Dec 5 2003
  copyright: (C) 2003 by Dario Abatianni
  email:     eisfuchs@tigress.com
*/

#include "logfilereader.h"
#include "konversationapplication.h"
#include "ircview.h"
#include "ircviewbox.h"

#include <qlayout.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qdockarea.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qregexp.h>
#include <qspinbox.h>
#include <qstylesheet.h>
#include <qwhatsthis.h>

#include <kdialog.h>
#include <ktoolbar.h>
#include <kmessagebox.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kdebug.h>
#include <kio/jobclasses.h>


LogfileReader::LogfileReader(QWidget* parent, const QString& log) : ChatWindow(parent)
{
    setType(ChatWindow::LogFileReader);

    fileName = log;
    QDockArea* toolBarDock = new QDockArea(Qt::Horizontal,QDockArea::Normal,this,"logfile_toolbar_dock");
    toolBar = new KToolBar(toolBarDock,"logfile_toolbar",true,true);

    toolBar->insertButton("filesaveas",0,SIGNAL(clicked()),this,SLOT(saveLog()),true,i18n("Save As..."));

    new QLabel(i18n("Show last:"),toolBar,"logfile_size_label");
    sizeSpin = new QSpinBox(10,1000,10,toolBar,"logfile_size_spinbox");
    QWhatsThis::add(sizeSpin, i18n("Use this box to set the maximum size of the log file. This setting does not take effect until you restart Konversation. Each log file may have a separate setting."));
    sizeSpin->setValue(Preferences::logfileBufferSize());
    sizeSpin->setSuffix(i18n(" KB"));
    sizeSpin->installEventFilter(this);

    toolBar->insertButton("reload",0,SIGNAL(clicked()),this,SLOT(updateView()),true,i18n("Reload"));
    toolBar->insertButton("editdelete",0,SIGNAL(clicked()),this,SLOT(clearLog()),true,i18n("Clear Logfile"));

    IRCViewBox* ircBox = new IRCViewBox(this, 0);
    setTextView(ircBox->ircView());
    QWhatsThis::add(getTextView(), i18n("The messages in the log file are displayed here. The oldest messages are at the top and the most recent are at the bottom."));

    updateView();
    resize(Preferences::logfileReaderSize());
    ircBox->ircView()->setFocusPolicy(QWidget::StrongFocus);
    setFocusPolicy(QWidget::StrongFocus);
    setFocusProxy(ircBox->ircView());

    connect(getTextView(), SIGNAL(gotFocus()), getTextView(), SLOT(setFocus()));
}

LogfileReader::~LogfileReader()
{
    Preferences::setLogfileReaderSize(size());
    Preferences::setLogfileBufferSize(sizeSpin->value());

    delete toolBar;
}

bool LogfileReader::eventFilter(QObject* /* watched */, QEvent* e)
{
    if (e->type() == QEvent::KeyPress)
    {
        QKeyEvent* ke = static_cast<QKeyEvent*>(e);

        if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)
        {
            updateView();

            return true;
        }
        else
            return false;
    }

    return false;
}

void LogfileReader::updateView()
{
    // get maximum size of logfile to display
    unsigned long pos=sizeSpin->value()*1024;
    getTextView()->clear();

    QFile file(fileName);

    if(file.open(IO_ReadOnly))
    {
        QTextStream stream(&file);
        stream.setEncoding(QTextStream::UnicodeUTF8);

        // Set file pointer to <pos> bytes from the end
        if(stream.device()->size()>pos)
            stream.device()->at(stream.device()->size()-pos);
        // Skip first line, since it may be incomplete
        stream.readLine();
        QString str;

        while(!stream.eof())
        {
            str = QStyleSheet::escape(stream.readLine());
            getTextView()->appendRaw(str, true);
        }

        stream.unsetDevice();
        file.close();
    }
}

void LogfileReader::clearLog()
{
    if(KMessageBox::warningContinueCancel(this,
        i18n("Do you really want to permanently discard all log information of this file?"),
        i18n("Clear Logfile"),
        KStdGuiItem::del(),
        "ClearLogfileQuestion")==KMessageBox::Continue)
    {
        QFile::remove(fileName);
        updateView();
    }
}

void LogfileReader::saveLog()
{
    KMessageBox::information(this,
        i18n("Note: By saving the logfile you will save all data in the file, not only the part you can see in this viewer."),
        i18n("Save Logfile"),
        "SaveLogfileNote");

    QString destination=KFileDialog::getSaveFileName(fileName,
        QString(),
        this,
        i18n("Choose Destination Folder"));
    if(!destination.isEmpty())
    {
        // replace # with %25 to make it URL conforming
        KIO::Job* job=KIO::copy(KURL(fileName.replace("#","%23")),
            KURL(destination),
            true);

        connect(job,SIGNAL(result(KIO::Job*)),this,SLOT(copyResult(KIO::Job*)));
    }
}

void LogfileReader::copyResult(KIO::Job* job)
{
    if(job->error()) job->showErrorDialog(this);

    job->deleteLater();
}

void LogfileReader::closeLog()
{
    delete this;
}

void LogfileReader::childAdjustFocus()
{
  getTextView()->setFocus();
}

int LogfileReader::margin() { return KDialog::marginHint(); }
int LogfileReader::spacing() { return KDialog::spacingHint(); }
bool LogfileReader::searchView() { return true; }

#include "logfilereader.moc"