Use KIO::filesize_t to be able to handle big filesizes (now 4GB DVD image filesizes...
[kdenetwork.git] / kppp / logview / export.cpp
blob444621cd7294e0c34544bbf50e07cf2da084c513
1 /*
2 * kPPPlogview: a accounting log system for kPPP
4 * Copyright (C) 1998 Mario Weilguni <mweilguni@kde.org>
6 * This file has been contributed by Tilo Ulbrich <TiloUlbrich@web.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "export.h"
25 #include <qpushbutton.h>
26 #include <qtextcodec.h>
28 class Export;
30 /***** ExportFormats *****/
31 static struct {
32 int id;
33 QString name;
34 QString desc;
35 QString ext;
38 ExportFormats [] = {
39 { 1, I18N_NOOP("CSV"),
40 I18N_NOOP("Export to a text file, using semicolons as separators.<p></p>Can be used for spreadsheet programs like <i>KSpread</i>."),
41 "csv" },
42 { 2, I18N_NOOP("HTML"),
43 I18N_NOOP("Export to a HTML Page.<p></p>Can be used for easy exchange over the <i>Internet</i>."),
44 "html" },
45 { 0, 0, 0, 0 } /* !! don't forget !! */
49 /***** ExportWizard *****/
50 ExportWizard::ExportWizard(QWidget *parent, const QString &_date)
51 : K3Wizard(parent, "", true) {
52 date = _date;
54 filterID = 0;
56 setWindowTitle(i18n("Export Wizard for kPPP Logs"));
58 /* format-page */
59 formatPage = new QWidget();
60 QHBoxLayout *formatLayout = new QHBoxLayout(formatPage);
62 typeList = new Q3ListBox(formatPage);
63 connect(typeList, SIGNAL(highlighted(int)), SLOT (typeHighlighted(int)));
64 typeList->setMinimumSize(50, 200);
65 typeList->setToolTip( i18n("List with possible output formats"));
66 int i=0;
67 while (ExportFormats[i].id) { // add each format to the list
68 typeList->insertItem(i18n(ExportFormats[i].name.toUtf8()));
69 i++;
72 formatLayout->addWidget(typeList);
73 formatLayout->addSpacing(10);
75 typeInfo = new QLabel(formatPage);
76 typeInfo->setAlignment(Qt::AlignTop);
77 typeInfo->setWordWrap( true );
78 typeInfo->setText(i18n("<qt><b>Please choose the output format on the left side.</b></qt>"));
79 typeInfo->setMinimumSize(350, 200);
80 formatLayout->addWidget(typeInfo);
82 addPage(formatPage, i18n("Selection of Filetype"));
85 /* filename-page */
86 filenamePage = new QWidget();
87 QVBoxLayout *filenameLayout = new QVBoxLayout( filenamePage );
89 QLabel *fnLbl = new QLabel(filenamePage);
90 fnLbl->setText(i18n("Filename:"));
91 filenameLayout->addWidget(fnLbl);
93 fnLine = new QLineEdit(filenamePage);
94 fnLine->setText(i18n("[No file selected]"));
95 fnLine->setReadOnly(true);
96 filenameLayout->addWidget(fnLine);
97 filenameLayout->addStretch(1);
99 fnGet = new QPushButton(filenamePage);
100 fnGet->setText(i18n("&Select File..."));
101 fnGet->setMaximumWidth(200);
102 fnGet->setToolTip( i18n("Select the filename of the exported output file"));
103 filenameLayout->addWidget(fnGet);
104 connect(fnGet, SIGNAL(clicked()), SLOT(getFilename()));
105 filenameLayout->addStretch(2);
107 addPage(filenamePage, i18n("Selection of Filename"));
108 setNextEnabled( filenamePage, false );
109 setHelpEnabled( filenamePage, false );
111 setNextEnabled( formatPage, false );
112 setHelpEnabled( formatPage, false );
115 Export * ExportWizard::createExportFilter() {
116 switch (filterID) { // IDs: see data-struct ExportFormats
117 case 1 : return new CSVExport(filename, ";");
118 case 2 : return new HTMLExport(filename, date);
119 default : return NULL; // oops..
123 void ExportWizard::typeHighlighted(int index) {
124 typeInfo->setText("<qt><b>"+i18n(ExportFormats[index].name.toUtf8())+' ' +
125 i18n("File Format") + "</b><p></p>" + i18n(ExportFormats[index].desc.toUtf8())
126 +"</qt>");
127 setNextEnabled(formatPage, true );
130 void ExportWizard::getFilename() {
131 int i = typeList->currentItem();
132 if ( i == -1 )
133 return;
134 // prepare filter: e.g.: HTML (*.html *.HTML)
135 QString filter = "*." + ExportFormats[i].ext + " *." + ExportFormats[i].ext.toUpper() + '|' +
136 i18n(ExportFormats[i].name.toUtf8()) + " (*." + ExportFormats[i].ext + " *." +
137 ExportFormats[i].ext.toUpper() + ')';
139 filename = KFileDialog::getSaveFileName(date + '.' + ExportFormats[i].ext, filter, 0, i18n("Please Choose File"));
140 if (filename.isEmpty()) // no file selected
141 return;
142 fnLine->setText(filename);
143 setFinishEnabled(filenamePage, true);
146 void ExportWizard::reject() {
147 hide();
148 filename.clear();
151 void ExportWizard::accept() {
152 filterID = typeList->currentItem() + 1; // translate to ID-count in ExportFormats
153 hide();
157 /***** Export *****/
158 Export::Export(const QString &_filename)
159 : filename(_filename),
160 buffer("")
164 Export::~Export()
168 bool Export::openFile() {
169 file.setFileName(filename);
170 return file.open(QIODevice::WriteOnly);
173 bool Export::closeFile() {
174 bool ok = true;
175 if (file.write(buffer.toLocal8Bit(), buffer.length())<0)
176 ok = false;
177 file.close();
178 return ok;
182 /***** CSVExport *****/
183 CSVExport::CSVExport(const QString &filename, const QString &_separator)
184 : Export(filename),
185 separator(_separator)
189 void CSVExport::addHeadline(const QString &a, const QString &b,
190 const QString &c, const QString &d,
191 const QString &e, const QString &f,
192 const QString &g, const QString &h) {
193 // no especially style
194 addDataline(a, b, c, d, e, f, g, h);
197 void CSVExport::addDataline(const QString &a, const QString &b,
198 const QString &c, const QString &d,
199 const QString &e, const QString &f,
200 const QString &g, const QString &h) {
201 buffer+=a + separator +
202 b + separator +
203 c + separator +
204 d + separator +
205 e + separator +
206 f + separator +
207 g + separator +
208 h + separator + '\n';
211 void CSVExport::addEmptyLine() {
212 // not needed
215 void CSVExport::setFinishCode() {
216 // not needed
220 /***** HTMLExport *****/
221 HTMLExport::HTMLExport(const QString &filename, const QString &date)
222 : Export(filename) {
223 QString title = i18n("Connection log for %1", date);
224 buffer = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
225 buffer.append("<html>\n<head>\n <title>"+title+"</title>\n");
226 buffer.append(QLatin1String(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=")
227 + QTextCodec::codecForLocale()->name() +
228 QLatin1String("\">"));
229 buffer.append("\n</head>\n<body>\n<h1>"+title+"</h1>\n\n");
230 buffer.append("<table width=\"100%\" border=\"1\">\n");
232 trStartCode = "<tr>";
233 trEndCode = "</tr>\n";
234 tdStartCode = "<td>";
235 tdEndCode = "</td>";
238 void HTMLExport::addHeadline(const QString &a, const QString &b,
239 const QString &c, const QString &d,
240 const QString &e, const QString &f,
241 const QString &g, const QString &h) {
242 // simply bold font
243 QString bak1 = tdStartCode; tdStartCode.append("<b>");
244 QString bak2 = tdEndCode; tdEndCode.prepend("</b>");
246 addDataline(a, b, c, d, e, f, g, h);
248 // reset font
249 tdStartCode = bak1;
250 tdEndCode = bak2;
253 void HTMLExport::addDataline(const QString &a, const QString &b,
254 const QString &c, const QString &d,
255 const QString &e, const QString &f,
256 const QString &g, const QString &h) {
257 buffer+= trStartCode +
258 tdStartCode + a + tdEndCode +
259 tdStartCode + b + tdEndCode +
260 tdStartCode + c + tdEndCode +
261 tdStartCode + d + tdEndCode +
262 tdStartCode + e + tdEndCode +
263 tdStartCode + f + tdEndCode +
264 tdStartCode + g + tdEndCode +
265 tdStartCode + h + tdEndCode +
266 trEndCode;
269 void HTMLExport::addEmptyLine() {
270 addDataline("&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;");
273 void HTMLExport::setFinishCode() {
274 buffer+= "</table>\n</body>\n</html>\n";
277 #include "export.moc"