| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 1999-2006 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "logwindow.h" |
|---|
| 22 | |
|---|
| 23 | #include "config.h" |
|---|
| 24 | |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | |
|---|
| 27 | #include <QDialogButtonBox> |
|---|
| 28 | #include <QFile> |
|---|
| 29 | #include <QPushButton> |
|---|
| 30 | #include <QShowEvent> |
|---|
| 31 | #include <QSocketNotifier> |
|---|
| 32 | #include <QTextStream> |
|---|
| 33 | #include <QVBoxLayout> |
|---|
| 34 | |
|---|
| 35 | #ifdef USE_KDE |
|---|
| 36 | #include <KDE/KFileDialog> |
|---|
| 37 | #else |
|---|
| 38 | #include <QFileDialog> |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #include <licq_icq.h> |
|---|
| 42 | |
|---|
| 43 | #include "core/messagebox.h" |
|---|
| 44 | |
|---|
| 45 | #include "helpers/support.h" |
|---|
| 46 | |
|---|
| 47 | #include "widgets/mledit.h" |
|---|
| 48 | |
|---|
| 49 | #undef connect |
|---|
| 50 | |
|---|
| 51 | using namespace LicqQtGui; |
|---|
| 52 | /* TRANSLATOR LicqQtGui::LogWindow */ |
|---|
| 53 | |
|---|
| 54 | LogWindow::LogWindow(QWidget* parent) |
|---|
| 55 | : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint |
|---|
| 56 | | Qt::WindowMinimizeButtonHint) |
|---|
| 57 | { |
|---|
| 58 | Support::setWidgetProps(this, "NetworkLog"); |
|---|
| 59 | setWindowTitle(tr("Licq Network Log")); |
|---|
| 60 | |
|---|
| 61 | QVBoxLayout* top_lay = new QVBoxLayout(this); |
|---|
| 62 | |
|---|
| 63 | outputBox = new MLEdit(false, this, true); |
|---|
| 64 | outputBox->setReadOnly(true); |
|---|
| 65 | outputBox->setMinimumHeight(outputBox->frameWidth() * 2 |
|---|
| 66 | + 16 * outputBox->fontMetrics().lineSpacing()); |
|---|
| 67 | outputBox->setMinimumWidth(outputBox->minimumHeight() * 2); |
|---|
| 68 | |
|---|
| 69 | QTextDocument* doc = outputBox->document(); |
|---|
| 70 | // hardcoded limit, maybe should be user configurable? |
|---|
| 71 | doc->setMaximumBlockCount(500); |
|---|
| 72 | outputBox->setDocument(doc); |
|---|
| 73 | |
|---|
| 74 | top_lay->addWidget(outputBox); |
|---|
| 75 | |
|---|
| 76 | QDialogButtonBox* buttons = new QDialogButtonBox( |
|---|
| 77 | QDialogButtonBox::Save | |
|---|
| 78 | QDialogButtonBox::Close); |
|---|
| 79 | |
|---|
| 80 | connect(buttons, SIGNAL(accepted()), SLOT(save())); |
|---|
| 81 | connect(buttons, SIGNAL(rejected()), SLOT(hide())); |
|---|
| 82 | |
|---|
| 83 | buttons->button(QDialogButtonBox::Close)->setDefault(true); |
|---|
| 84 | |
|---|
| 85 | QPushButton* btnClear = buttons->addButton(tr("Clear"), |
|---|
| 86 | QDialogButtonBox::ActionRole); |
|---|
| 87 | btnClear->setAutoDefault(false); |
|---|
| 88 | connect(btnClear, SIGNAL(clicked()), outputBox, SLOT(clear())); |
|---|
| 89 | |
|---|
| 90 | top_lay->addWidget(buttons); |
|---|
| 91 | |
|---|
| 92 | adjustSize(); |
|---|
| 93 | |
|---|
| 94 | sn = new QSocketNotifier(Pipe(), QSocketNotifier::Read, this); |
|---|
| 95 | connect(sn, SIGNAL(activated(int)), SLOT(log(int))); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | void LogWindow::log(int fd) |
|---|
| 99 | { |
|---|
| 100 | char buf[4]; |
|---|
| 101 | read(fd, buf, 1); |
|---|
| 102 | |
|---|
| 103 | QString str = QString::fromUtf8(NextLogMsg()); |
|---|
| 104 | |
|---|
| 105 | outputBox->appendNoNewLine(str); |
|---|
| 106 | outputBox->GotoEnd(); |
|---|
| 107 | |
|---|
| 108 | /* The next call will block, so we need to clear the log so that processing |
|---|
| 109 | can continue */ |
|---|
| 110 | unsigned short nNextLogType = NextLogType(); |
|---|
| 111 | ClearLog(); |
|---|
| 112 | |
|---|
| 113 | if (nNextLogType == L_ERROR) |
|---|
| 114 | CriticalUser(NULL, str); |
|---|
| 115 | |
|---|
| 116 | else if (nNextLogType == L_MESSAGE) |
|---|
| 117 | InformUser(NULL, str); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void LogWindow::save() |
|---|
| 121 | { |
|---|
| 122 | QString fn; |
|---|
| 123 | |
|---|
| 124 | #ifdef USE_KDE |
|---|
| 125 | KUrl u = KFileDialog::getSaveUrl(QString(QDir::homePath() + "/licq.log"), |
|---|
| 126 | QString::null, this); |
|---|
| 127 | fn = u.path(); |
|---|
| 128 | #else |
|---|
| 129 | fn = QFileDialog::getSaveFileName(this, QString::null, QDir::homePath() + "/licq.log"); |
|---|
| 130 | #endif |
|---|
| 131 | |
|---|
| 132 | if (fn.isNull()) |
|---|
| 133 | return; |
|---|
| 134 | |
|---|
| 135 | QFile f(fn); |
|---|
| 136 | if (!f.open(QIODevice::WriteOnly)) |
|---|
| 137 | { |
|---|
| 138 | WarnUser(this, tr("Failed to open file:\n%1").arg(fn)); |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | { |
|---|
| 142 | QTextStream t(&f); |
|---|
| 143 | t << outputBox->toPlainText(); |
|---|
| 144 | f.close(); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void LogWindow::showEvent(QShowEvent* /* event */) |
|---|
| 149 | { |
|---|
| 150 | outputBox->GotoEnd(); |
|---|
| 151 | } |
|---|