summaryrefslogtreecommitdiffstats
path: root/kppp/logview/export.cpp
blob: 7f810a6aac9043e06af4241d2961bf8d61a55ddf (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * kPPPlogview: a accounting log system for kPPP
 *
 *            Copyright (C) 1998 Mario Weilguni <[email protected]>
 *
 * This file has been contributed by Tilo Ulbrich <[email protected]>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "export.h"

#include <qpushbutton.h>
#include <qtextcodec.h>

class Export;

/***** ExportFormats *****/
static struct {
    int id;
    QString name;
    QString desc;
    QString ext;
  }

ExportFormats [] = {
    { 1, I18N_NOOP("CSV"),
      I18N_NOOP("Export to a text file, using semicolons as separators.<p></p>Can be used for spreadsheet programs like <i>KSpread</i>."),
      "csv" },
    { 2, I18N_NOOP("HTML"),
      I18N_NOOP("Export to a HTML Page.<p></p>Can be used for easy exchange over the <i>Internet</i>."),
      "html" },
    { 0, 0, 0, 0 }  /* !! don't forget !! */
};


/***** ExportWizard *****/
ExportWizard::ExportWizard(QWidget *parent, const QString &_date)
  : KWizard(parent, "", true) {
  date = _date;

  filterID = 0;

  setCaption(i18n("Export Wizard for kPPP Logs"));

  /* format-page */
  formatPage = new QWidget();
  QHBoxLayout *formatLayout = new QHBoxLayout(formatPage);

  typeList = new QListBox(formatPage);
  connect(typeList, SIGNAL(highlighted(int)), SLOT (typeHighlighted(int)));
  typeList->setMinimumSize(50, 200);
  QToolTip::add(typeList, i18n("List with possible output formats"));
  int i=0;
  while (ExportFormats[i].id) { // add each format to the list
    typeList->insertItem(i18n(ExportFormats[i].name.utf8()));
    i++;
  }

  formatLayout->addWidget(typeList);
  formatLayout->addSpacing(10);

  typeInfo = new QLabel(formatPage);
  typeInfo->setAlignment(Qt::AlignTop | Qt::WordBreak);
  typeInfo->setText(i18n("<qt><b>Please choose the output format on the left side.</b></qt>"));
  typeInfo->setMinimumSize(350, 200);
  formatLayout->addWidget(typeInfo);

  addPage(formatPage, i18n("Selection of Filetype"));


  /* filename-page */
  filenamePage = new QWidget();
  QVBoxLayout *filenameLayout = new QVBoxLayout( filenamePage );

  QLabel *fnLbl = new QLabel(filenamePage);
  fnLbl->setText(i18n("Filename:"));
  filenameLayout->addWidget(fnLbl);

  fnLine = new QLineEdit(filenamePage);
  fnLine->setText(i18n("[No file selected]"));
  fnLine->setReadOnly(true);
  filenameLayout->addWidget(fnLine);
  filenameLayout->addStretch(1);

  fnGet = new QPushButton(filenamePage);
  fnGet->setText(i18n("&Select File..."));
  fnGet->setMaximumWidth(200);
  QToolTip::add(fnGet, i18n("Select the filename of the exported output file"));
  filenameLayout->addWidget(fnGet);
  connect(fnGet, SIGNAL(clicked()), SLOT(getFilename()));
  filenameLayout->addStretch(2);

  addPage(filenamePage, i18n("Selection of Filename"));
  setNextEnabled( filenamePage, false );
  setHelpEnabled( filenamePage, false );

  setNextEnabled( formatPage, false );
  setHelpEnabled( formatPage, false );
}

Export * ExportWizard::createExportFilter() {
  switch (filterID) { // IDs: see data-struct ExportFormats
    case 1 : return new CSVExport(filename, ";");
    case 2 : return new HTMLExport(filename, date);
    default : return NULL; // oops..
  };
}

void ExportWizard::typeHighlighted(int index) {
  typeInfo->setText("<qt><b>"+i18n(ExportFormats[index].name.utf8())+" " +
                    i18n("File Format") + "</b><p></p>" + i18n(ExportFormats[index].desc.utf8())
                    +"</qt>");
  setNextEnabled(formatPage, true );
}

void ExportWizard::getFilename() {
  int i = typeList->currentItem();
  if ( i == -1 )
      return;
  // prepare filter: e.g.: HTML (*.html *.HTML)
  QString filter = "*." + ExportFormats[i].ext + " *." + ExportFormats[i].ext.upper() + "|" +
                   i18n(ExportFormats[i].name.utf8()) + " (*." + ExportFormats[i].ext + " *." +
                   ExportFormats[i].ext.upper() + ")";

  filename = KFileDialog::getSaveFileName(date + "." + ExportFormats[i].ext, filter, 0, i18n("Please Choose File"));
  if (filename.isEmpty()) // no file selected
    return;
  fnLine->setText(filename);
  setFinishEnabled(filenamePage, true);
}

void ExportWizard::reject()  {
    hide();
    filename = QString::null;
}

void ExportWizard::accept() {
  filterID = typeList->currentItem() + 1; // translate to ID-count in ExportFormats
  hide();
}


/***** Export *****/
Export::Export(const QString &_filename)
  : filename(_filename),
    buffer("")
{
}

Export::~Export()
{
}

bool Export::openFile() {
  file.setName(filename);
  return file.open(IO_WriteOnly);
}

bool Export::closeFile() {
  bool ok = true;
  if (file.writeBlock(buffer.local8Bit(), buffer.length())<0)
    ok = false;
  file.close();
  return ok;
}


/***** CSVExport *****/
CSVExport::CSVExport(const QString &filename, const QString &_separator)
  : Export(filename),
    separator(_separator)
{
}

void CSVExport::addHeadline(const QString &a, const QString &b,
			     const QString &c, const QString &d,
			     const QString &e, const QString &f,
			     const QString &g, const QString &h) {
  // no especially style
  addDataline(a, b, c, d, e, f, g, h);
}

void CSVExport::addDataline(const QString &a, const QString &b,
			    const QString &c, const QString &d,
			    const QString &e, const QString &f,
			    const QString &g, const QString &h) {
  buffer+=a + separator +
          b + separator +
          c + separator +
          d + separator +
          e + separator +
          f + separator +
          g + separator +
          h + separator + "\n";
}

void CSVExport::addEmptyLine() {
  // not needed
}

void CSVExport::setFinishCode() {
  // not needed
}


/***** HTMLExport *****/
HTMLExport::HTMLExport(const QString &filename, const QString &date)
  : Export(filename) {
  QString title = i18n("Connection log for %1").arg(date);
  buffer = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
  buffer.append("<html>\n<head>\n  <title>"+title+"</title>\n");
  buffer.append(QString::fromLatin1("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=")
                + QTextCodec::codecForLocale()->mimeName() +
                QString::fromLatin1("\">"));
  buffer.append("\n</head>\n<body>\n<h1>"+title+"</h1>\n\n");
  buffer.append("<table width=\"100%\" border=\"1\">\n");

  trStartCode = "<tr>";
  trEndCode   = "</tr>\n";
  tdStartCode = "<td>";
  tdEndCode   = "</td>";
}

void HTMLExport::addHeadline(const QString &a, const QString &b,
			     const QString &c, const QString &d,
			     const QString &e, const QString &f,
			     const QString &g, const QString &h) {
  // simply bold font
  QString bak1 = tdStartCode; tdStartCode.append("<b>");
  QString bak2 = tdEndCode; tdEndCode.prepend("</b>");

  addDataline(a, b, c, d, e, f, g, h);

  // reset font
  tdStartCode = bak1;
  tdEndCode = bak2;
}

void HTMLExport::addDataline(const QString &a, const QString &b,
			     const QString &c, const QString &d,
			     const QString &e, const QString &f,
			     const QString &g, const QString &h) {
  buffer+=  trStartCode +
  	    tdStartCode + a + tdEndCode +
  	    tdStartCode + b + tdEndCode +
  	    tdStartCode + c + tdEndCode +
  	    tdStartCode + d + tdEndCode +
  	    tdStartCode + e + tdEndCode +
  	    tdStartCode + f + tdEndCode +
  	    tdStartCode + g + tdEndCode +
  	    tdStartCode + h + tdEndCode +
  	    trEndCode;
}

void HTMLExport::addEmptyLine() {
  addDataline("&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;");
}

void HTMLExport::setFinishCode() {
  buffer+= "</table>\n</body>\n</html>\n";
}

#include "export.moc"