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.
25 #include <qpushbutton.h>
26 #include <qtextcodec.h>
30 /***** 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>."),
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>."),
45 { 0, 0, 0, 0 } /* !! don't forget !! */
49 /***** ExportWizard *****/
50 ExportWizard::ExportWizard(QWidget
*parent
, const QString
&_date
)
51 : K3Wizard(parent
, "", true) {
56 setWindowTitle(i18n("Export Wizard for kPPP Logs"));
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"));
67 while (ExportFormats
[i
].id
) { // add each format to the list
68 typeList
->insertItem(i18n(ExportFormats
[i
].name
.toUtf8()));
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"));
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())
127 setNextEnabled(formatPage
, true );
130 void ExportWizard::getFilename() {
131 int i
= typeList
->currentItem();
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
142 fnLine
->setText(filename
);
143 setFinishEnabled(filenamePage
, true);
146 void ExportWizard::reject() {
151 void ExportWizard::accept() {
152 filterID
= typeList
->currentItem() + 1; // translate to ID-count in ExportFormats
158 Export::Export(const QString
&_filename
)
159 : filename(_filename
),
168 bool Export::openFile() {
169 file
.setFileName(filename
);
170 return file
.open(QIODevice::WriteOnly
);
173 bool Export::closeFile() {
175 if (file
.write(buffer
.toLocal8Bit(), buffer
.length())<0)
182 /***** CSVExport *****/
183 CSVExport::CSVExport(const QString
&filename
, const QString
&_separator
)
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
+
208 h
+ separator
+ '\n';
211 void CSVExport::addEmptyLine() {
215 void CSVExport::setFinishCode() {
220 /***** HTMLExport *****/
221 HTMLExport::HTMLExport(const QString
&filename
, const QString
&date
)
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>";
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
) {
243 QString bak1
= tdStartCode
; tdStartCode
.append("<b>");
244 QString bak2
= tdEndCode
; tdEndCode
.prepend("</b>");
246 addDataline(a
, b
, c
, d
, e
, f
, g
, h
);
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
+
269 void HTMLExport::addEmptyLine() {
270 addDataline(" ", " ", " ", " ", " ", " ", " ", " ");
273 void HTMLExport::setFinishCode() {
274 buffer
+= "</table>\n</body>\n</html>\n";
277 #include "export.moc"