1/***************************************************************************
2 report.cpp - Report document class
3 -------------------
4 begin : fri aug 13 15:29:46 CEST 2004
5
6 copyright : (C) 2004 Emiliano Gulmini
7 email : emi_barbarossa@yahoo.it
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18// QT
19#include <qstring.h>
20#include <qfile.h>
21//Added by qt3to4:
22#include <QTextStream>
23
24// KDE
25#include <k3listview.h>
26#include <kmessagebox.h>
27#include <kuser.h>
28
29// local
30#include "report.h"
31#include "configurationclasses.h"
32
33void Report::createReportFile()
34{
35 QString xmlFileName = m_docPath + ".xml",
36 cssFileName = m_docPath + ".css";
37
38 // Generates a report file
39 // a) Open the file
40 QFile report(xmlFileName);
41 if (!report.open( QIODevice::WriteOnly ))
42 {
43 KMessageBox::error(0, i18n("<qt>Cannot open the file <b>%1</b>.</qt>", xmlFileName));
44 return ;
45 }
46
47 // b) Write the header of the XML file
48
49 QDateTime datetime = QDateTime::currentDateTime(Qt::LocalTime);
50 QString dateString = datetime.toString(Qt::LocalDate);
51 KUser user;
52 QString columnTextFour,
53 columnReplaceWith;
54 if(!m_isSearchFlag)
55 {
56 columnTextFour = i18n("Replaced Strings");
57 columnReplaceWith = i18n("Replace with");
58 }
59 else
60 {
61 columnTextFour = i18n("Total number occurrences");
62 columnReplaceWith = i18n("-");
63 }
64
65 QString css = cssFileName.mid(cssFileName.lastIndexOf("/")+1,cssFileName.length()-(cssFileName.lastIndexOf("/")+1));
66 QTextStream oTStream( &report );
67 oTStream << "<?xml version=\"1.0\"?>\n"
68 "<?xml-stylesheet href=\""+css+"\" type=\"text/css\"?>"
69 "<report>\n"
70 " <title> "+i18n("KFileReplace Report")+" </title>\n"
71 " <createdby>"+user.fullName()+'('+user.loginName()+")</createdby>\n"
72 " <date>"+dateString+"</date>\n"
73 "<hr/>\n"
74 " <table>\n"
75 " <tablecaption> "+i18n("Searching/Replacing Strings Table")+" </tablecaption>\n"
76 " <header>\n"
77 " <row>\n"
78 " <searchfor class=\"header\">"+i18n("Search for")+"</searchfor>\n";
79
80 if(!m_isSearchFlag)
81 oTStream<< " <replacewith class=\"header\" >"+columnReplaceWith+"</replacewith>\n";
82
83 oTStream<< " </row>\n"
84 " </header>\n";
85 // c) Write the strings list
86 Q3ListViewItem *lviCurItem,
87 *lviFirst;
88
89 lviCurItem = lviFirst = m_stringsView->firstChild();
90
91 if(lviCurItem == 0)
92 return ;
93
94 QString rowType="a1";
95
96 do
97 { QString rowTag = "<row >\n"
98 " <searchfor class=\""+rowType+"\"><![CDATA["+lviCurItem->text(0)+"]]></searchfor>\n"
99 " <replacewith class=\""+rowType+"\"><![CDATA["+lviCurItem->text(1)+"]]></replacewith>\n"
100 "</row>\n";
101
102 oTStream << rowTag;
103
104 rowType = ((rowType == "a1") ? "a2" : "a1");
105
106 lviCurItem = lviCurItem->nextSibling();
107 } while(lviCurItem && lviCurItem != lviFirst);
108
109 oTStream<< "</table>\n";
110
111 oTStream<< "<whiteline/>\n"
112 " <table>\n"
113 " <tablecaption> "+i18n("Results Table")+ " </tablecaption>"
114 " <header>\n"
115 " <row>\n"
116 " <name class=\"header\">"+i18n("Name")+"</name>\n"
117 " <folder class=\"header\">"+i18n("Folder")+"</folder>\n";
118 if(m_isSearchFlag)
119 {
120 oTStream<< " <oldsize class=\"header\">"+i18n("Size")+"</oldsize>\n";
121 }
122 else
123 {
124 oTStream<< " <oldsize class=\"header\">"+i18n("Old Size")+"</oldsize>\n"
125 " <newsize class=\"header\">"+i18n("New Size")+"</newsize>\n";
126 }
127 oTStream<< " <replacedstrings class=\"header\" >"+columnTextFour+"</replacedstrings>\n"
128 " <owneruser class=\"header\">"+i18n("Owner User")+"</owneruser>\n"
129 " <ownergroup class=\"header\">"+i18n("Owner Group")+"</ownergroup>\n"
130 " </row>\n"
131 " </header>\n";
132
133 // d) Write the result list
134
135 lviCurItem = lviFirst = m_resultsView->firstChild();
136
137 if(lviCurItem == 0)
138 return ;
139
140 unsigned int totalOccurrences = 0;
141
142 rowType="a1";
143
144 do
145 { QString rowTag = " <row >\n"
146 " <name class=\""+rowType+"\"><![CDATA["+lviCurItem->text(0)+"]]></name>\n"
147 " <folder class=\""+rowType+"\"><![CDATA["+lviCurItem->text(1)+"]]></folder>\n";
148 if(m_isSearchFlag)
149 {
150 rowTag += " <oldsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(2)+"]]></oldsize>\n"
151 " <replacedstrings class=\""+rowType+"\"><![CDATA["+lviCurItem->text(3)+"]]></replacedstrings>\n"
152 " <owneruser class=\""+rowType+"\"><![CDATA["+lviCurItem->text(4)+"]]></owneruser>\n"
153 " <ownergroup class=\""+rowType+"\"><![CDATA["+lviCurItem->text(5)+"]]></ownergroup>\n"
154 " </row>\n";
155 }
156 else
157 {
158 rowTag += " <oldsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(2)+"]]></oldsize>\n"
159 " <newsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(3)+"]]></newsize>\n"
160 " <replacedstrings class=\""+rowType+"\"><![CDATA["+lviCurItem->text(4)+"]]></replacedstrings>\n"
161 " <owneruser class=\""+rowType+"\"><![CDATA["+lviCurItem->text(5)+"]]></owneruser>\n"
162 " <ownergroup class=\""+rowType+"\"><![CDATA["+lviCurItem->text(6)+"]]></ownergroup>\n"
163 " </row>\n";
164 }
165
166 oTStream << rowTag;
167
168 rowType = ((rowType == "a1") ? "a2" : "a1");
169
170 if(m_isSearchFlag)
171 totalOccurrences += lviCurItem->text(3).toInt();
172 else
173 totalOccurrences += lviCurItem->text(4).toInt();
174
175 lviCurItem = lviCurItem->nextSibling();
176 } while(lviCurItem && lviCurItem != lviFirst);
177
178
179 // e) Write the end of the file
180
181 oTStream<< " </table>\n"
182 "<totaloccurrences>"
183 << totalOccurrences
184 << "</totaloccurrences>\n"
185 "</report>\n";
186
187 report.close();
188}
189
190void Report::createStyleSheet()
191{
192 QString cssFileName = m_docPath +".css";
193 QFile styleSheet(cssFileName);
194 if (!styleSheet.open( QIODevice::WriteOnly ))
195 {
196 KMessageBox::error(0, i18n("<qt>Cannot open the file <b>%1</b>.</qt>", cssFileName));
197 return ;
198 }
199
200 QTextStream oTStream( &styleSheet );
201
202 QString css = "title { display:block;font:40px bold sans-serif; }\n\n"
203 "createdby:before { content :\""+i18n("Created by")+": \"; }\n"
204 "createdby { display:inline; }\n\n"
205 "date:before { content :\"-"+i18n("date")+": \"; }\n"
206 "date { display:inline; }\n\n"
207 "totaloccurrences:before { content :\""+i18n("Total occurrences")+": \"; }\n"
208 "totaloccurrences { display:block;text-align:right; font-weight:bold;margin-top:5px;margin-right:5px;}\n"
209 "tablecaption {display:table-caption;font:20px bold sans-serif;}\n\n"
210 "hr {display:block;background:black;height:1px;margin:5px 0px 5px;}\n"
211 "whiteline {display:block;height:16px;}\n\n"
212 "searchfor {\n"
213 " display:table-cell;\n"
214 " border:1px solid black;\n"
215 " padding:0 7px 0; }\n\n";
216
217 if(!m_isSearchFlag)
218 {
219 css += "replacewith {\n"
220 " display:table-cell;\n"
221 " border:1px solid black;\n"
222 " padding:0 7px 0; }\n\n";
223 }
224
225 css += "folder {\n"
226 " display:table-cell;\n"
227 " border:1px solid black;\n"
228 " padding:0 7px 0; }\n\n"
229 "header { display: table-header-group; }\n\n"
230 "name {\n"
231 " display:table-cell;\n"
232 " border:1px solid black;\n"
233 " padding:0 7px 0; }\n\n"
234 "newsize {\n"
235 " display:table-cell;\n"
236 " border:1px solid black;\n"
237 " padding:0 7px 0;\n"
238 " text-align:right; }\n\n"
239 "oldsize {\n"
240 " display:table-cell;\n"
241 " border:1px solid black;\n"
242 " padding:0 7px 0;\n"
243 " text-align:right; }\n\n"
244 "ownergroup {\n"
245 " display:table-cell;\n"
246 " border:1px solid black;\n"
247 " padding:0 7px 0; }\n\n"
248 "owneruser {\n"
249 " display:table-cell;\n"
250 " border:1px solid black;\n"
251 " padding:0 7px 0; }\n\n"
252 "replacedstrings {\n"
253 " text-align:right;\n"
254 " display:table-cell;\n"
255 " border:1px solid black;\n"
256 " padding:0 7px 0; }\n\n"
257 "*[class~=header] {\n"
258 " background : lightgray;\n"
259 " text-align : center; }\n\n"
260 "row { display : table-row; }\n\n"
261 "table {\n"
262 " display:table;\n"
263 " border-collapse: collapse; }\n\n"
264 "*[class~=a1] {\n"
265 " background-color:aliceblue;\n"
266 " font-weight : bold;font-size:15px; }\n\n"
267 "*[class~=a2] {\n"
268 " background-color:khaki;\n"
269 " font-weight : bold;\n"
270 " font-size:15px; }\n\n";
271
272 oTStream << css;
273
274 styleSheet.close();
275}
276
277void Report::createDocument(const QString& docPath)
278{
279 m_docPath = docPath;
280
281 createStyleSheet();
282 createReportFile();
283}
284
285