| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 2000-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 "editfiledlg.h" |
|---|
| 22 | |
|---|
| 23 | #include "config.h" |
|---|
| 24 | |
|---|
| 25 | #include <QDialogButtonBox> |
|---|
| 26 | #include <QFile> |
|---|
| 27 | #include <QFileInfo> |
|---|
| 28 | #include <QPushButton> |
|---|
| 29 | #include <QTextStream> |
|---|
| 30 | #include <QVBoxLayout> |
|---|
| 31 | |
|---|
| 32 | #include "core/messagebox.h" |
|---|
| 33 | |
|---|
| 34 | #include "helpers/support.h" |
|---|
| 35 | |
|---|
| 36 | #include "widgets/mledit.h" |
|---|
| 37 | |
|---|
| 38 | using namespace LicqQtGui; |
|---|
| 39 | /* TRANSLATOR LicqQtGui::EditFileDlg */ |
|---|
| 40 | |
|---|
| 41 | EditFileDlg::EditFileDlg(QString fname, QWidget* parent) |
|---|
| 42 | : QDialog(parent), |
|---|
| 43 | myFile(fname) |
|---|
| 44 | { |
|---|
| 45 | setAttribute(Qt::WA_DeleteOnClose, true); |
|---|
| 46 | Support::setWidgetProps(this, "EditFileDialog"); |
|---|
| 47 | |
|---|
| 48 | QVBoxLayout* top_lay = new QVBoxLayout(this); |
|---|
| 49 | |
|---|
| 50 | myFileEdit = new MLEdit(false, this, true); |
|---|
| 51 | myFileEdit->setMinimumHeight(myFileEdit->frameWidth() * 2 + 20 * myFileEdit->fontMetrics().lineSpacing()); |
|---|
| 52 | myFileEdit->setMinimumWidth(myFileEdit->fontMetrics().width("_") * 80); |
|---|
| 53 | connect(myFileEdit, SIGNAL(undoAvailable(bool)), SLOT(saveEnable(bool))); |
|---|
| 54 | top_lay->addWidget(myFileEdit); |
|---|
| 55 | |
|---|
| 56 | QDialogButtonBox* buttonBox = new QDialogButtonBox( |
|---|
| 57 | QDialogButtonBox::Save | |
|---|
| 58 | QDialogButtonBox::Close | |
|---|
| 59 | QDialogButtonBox::Reset); |
|---|
| 60 | |
|---|
| 61 | connect(buttonBox, SIGNAL(accepted()), SLOT(save())); |
|---|
| 62 | connect(buttonBox, SIGNAL(rejected()), SLOT(close())); |
|---|
| 63 | |
|---|
| 64 | btnSave = buttonBox->button(QDialogButtonBox::Reset); // Temporarily |
|---|
| 65 | btnSave->setText(tr("Revert")); |
|---|
| 66 | connect(btnSave, SIGNAL(clicked()), SLOT(revert())); |
|---|
| 67 | |
|---|
| 68 | btnSave = buttonBox->button(QDialogButtonBox::Save); |
|---|
| 69 | |
|---|
| 70 | top_lay->addWidget(buttonBox); |
|---|
| 71 | |
|---|
| 72 | revert(); |
|---|
| 73 | |
|---|
| 74 | show(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void EditFileDlg::save() |
|---|
| 78 | { |
|---|
| 79 | QFile f(myFile); |
|---|
| 80 | if (!f.open(QIODevice::WriteOnly)) |
|---|
| 81 | { |
|---|
| 82 | WarnUser(this, tr("Failed to open file:\n%1").arg(myFile)); |
|---|
| 83 | return; |
|---|
| 84 | } |
|---|
| 85 | QTextStream t(&f); |
|---|
| 86 | t << myFileEdit->toPlainText(); |
|---|
| 87 | f.close(); |
|---|
| 88 | revert(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void EditFileDlg::saveEnable(bool yes) |
|---|
| 92 | { |
|---|
| 93 | btnSave->setEnabled(yes); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void EditFileDlg::setTitle(QString postfix) |
|---|
| 97 | { |
|---|
| 98 | if (!postfix.isNull()) |
|---|
| 99 | postfix.prepend(" "); |
|---|
| 100 | postfix.prepend(tr("Licq File Editor - %1").arg(myFile)); |
|---|
| 101 | setWindowTitle(postfix); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void EditFileDlg::revert() |
|---|
| 105 | { |
|---|
| 106 | QFile f(myFile); |
|---|
| 107 | if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) |
|---|
| 108 | { |
|---|
| 109 | WarnUser(this, tr("Failed to open file:\n%1").arg(myFile)); |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | QTextStream t(&f); |
|---|
| 114 | myFileEdit->setPlainText(t.readAll()); |
|---|
| 115 | f.close(); |
|---|
| 116 | |
|---|
| 117 | QFileInfo fi(f); |
|---|
| 118 | if (fi.isWritable()) |
|---|
| 119 | { |
|---|
| 120 | setTitle(); |
|---|
| 121 | myFileEdit->setReadOnly(false); |
|---|
| 122 | myFileEdit->setFocus(); |
|---|
| 123 | myFileEdit->ensureCursorVisible(); |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | setTitle(tr("[ Read-Only ]")); |
|---|
| 128 | myFileEdit->setReadOnly(true); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | saveEnable(false); |
|---|
| 132 | } |
|---|