summaryrefslogtreecommitdiffstats
path: root/kfilereplace/report.cpp
blob: 5497b04568fb6d71d53af0fdab9ca77e2d6c8bde (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
277
278
279
280
281
282
/***************************************************************************
                           report.cpp  -  Report document class
                                      -------------------
    begin                : fri aug  13 15:29:46 CEST 2004

    copyright            : (C) 2004 Emiliano Gulmini
    email                : emi_barbarossa@yahoo.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
// QT
#include <tqstring.h>
#include <tqfile.h>

// KDE
#include <klistview.h>
#include <kmessagebox.h>
#include <kuser.h>

// local
#include "report.h"
#include "configurationclasses.h"

void Report::createReportFile()
{
  TQString xmlFileName = m_docPath + ".xml",
          cssFileName = m_docPath + ".css";

  // Generates a report file
  // a) Open the file
  TQFile report(xmlFileName);
  if (!report.open( IO_WriteOnly ))
    {
      KMessageBox::error(0, i18n("<qt>Cannot open the file <b>%1</b>.</qt>").tqarg(xmlFileName));
      return ;
    }

  // b) Write the header of the XML file

  TQDateTime datetime = TQDateTime::tqcurrentDateTime(Qt::LocalTime);
  TQString dateString = datetime.toString(Qt::LocalDate);
  KUser user;
  TQString columnTextFour,
          columnReplaceWith;
  if(!m_isSearchFlag)
    {
      columnTextFour = i18n("Replaced Strings");
      columnReplaceWith = i18n("Replace with");
    }
  else
    {
      columnTextFour = i18n("Total number occurrences");
      columnReplaceWith = i18n("-");
    }

  TQString css =  cssFileName.mid(cssFileName.tqfindRev("/")+1,cssFileName.length()-(cssFileName.tqfindRev("/")+1));
  TQTextStream oTStream( &report );
  oTStream << "<?xml version=\"1.0\"?>\n"
              "<?xml-stylesheet href=\""+css+"\" type=\"text/css\"?>"
              "<report>\n"
              " <title> "+i18n("KFileReplace Report")+" </title>\n"
              " <createdby>"+user.fullName()+"("+user.loginName()+")"+"</createdby>\n"
              " <date>"+dateString+"</date>\n"
              "<hr/>\n"
              " <table>\n"
              " <tablecaption> "+i18n("Searching/Replacing Strings Table")+" </tablecaption>\n"
              " <header>\n"
              "  <row>\n"
              "   <searchfor class=\"header\">"+i18n("Search for")+"</searchfor>\n";

              if(!m_isSearchFlag)
                oTStream<< "   <replacewith class=\"header\" >"+columnReplaceWith+"</replacewith>\n";

              oTStream<< "  </row>\n"
              " </header>\n";
  // c) Write the strings list
  TQListViewItem *lviCurItem,
                *lviFirst;

  lviCurItem = lviFirst = m_stringsView->firstChild();

  if(lviCurItem == 0)
    return ;

  TQString rowType="a1";

  do
    { TQString rowTag = "<row >\n"
                       " <searchfor class=\""+rowType+"\"><![CDATA["+lviCurItem->text(0)+"]]></searchfor>\n"
                       " <replacewith class=\""+rowType+"\"><![CDATA["+lviCurItem->text(1)+"]]></replacewith>\n"
                       "</row>\n";

      oTStream << rowTag;

      rowType = ((rowType == "a1") ? "a2" : "a1");

      lviCurItem = lviCurItem->nextSibling();
    } while(lviCurItem && lviCurItem != lviFirst);

  oTStream<< "</table>\n";

  oTStream<< "<whiteline/>\n"
             " <table>\n"
             "  <tablecaption> "+i18n("Results Table")+ " </tablecaption>"
             "  <header>\n"
             "   <row>\n"
             "    <name class=\"header\">"+i18n("Name")+"</name>\n"
             "    <folder class=\"header\">"+i18n("Folder")+"</folder>\n";
             if(m_isSearchFlag)
               {
                 oTStream<< "    <oldsize class=\"header\">"+i18n("Size")+"</oldsize>\n";
               }
             else
               {
                 oTStream<< "    <oldsize class=\"header\">"+i18n("Old Size")+"</oldsize>\n"
                            "    <newsize class=\"header\">"+i18n("New Size")+"</newsize>\n";
               }
  oTStream<< "    <replacedstrings class=\"header\" >"+columnTextFour+"</replacedstrings>\n"
             "    <owneruser class=\"header\">"+i18n("Owner User")+"</owneruser>\n"
             "    <ownergroup class=\"header\">"+i18n("Owner Group")+"</ownergroup>\n"
             "   </row>\n"
             "  </header>\n";

  // d) Write the result list

  lviCurItem = lviFirst = m_resultsView->firstChild();

  if(lviCurItem == 0)
    return ;

  unsigned int totalOccurrences = 0;

  rowType="a1";

  do
    { TQString rowTag = "   <row >\n"
                       "    <name class=\""+rowType+"\"><![CDATA["+lviCurItem->text(0)+"]]></name>\n"
                           "    <folder class=\""+rowType+"\"><![CDATA["+lviCurItem->text(1)+"]]></folder>\n";
      if(m_isSearchFlag)
        {
          rowTag += "    <oldsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(2)+"]]></oldsize>\n"
                    "    <replacedstrings class=\""+rowType+"\"><![CDATA["+lviCurItem->text(3)+"]]></replacedstrings>\n"
                    "    <owneruser class=\""+rowType+"\"><![CDATA["+lviCurItem->text(4)+"]]></owneruser>\n"
                    "    <ownergroup class=\""+rowType+"\"><![CDATA["+lviCurItem->text(5)+"]]></ownergroup>\n"
                    "   </row>\n";
        }
      else
        {
          rowTag += "    <oldsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(2)+"]]></oldsize>\n"
                    "    <newsize class=\""+rowType+"\"><![CDATA["+lviCurItem->text(3)+"]]></newsize>\n"
                    "    <replacedstrings class=\""+rowType+"\"><![CDATA["+lviCurItem->text(4)+"]]></replacedstrings>\n"
                    "    <owneruser class=\""+rowType+"\"><![CDATA["+lviCurItem->text(5)+"]]></owneruser>\n"
                    "    <ownergroup class=\""+rowType+"\"><![CDATA["+lviCurItem->text(6)+"]]></ownergroup>\n"
                    "   </row>\n";
        }

      oTStream << rowTag;

      rowType = ((rowType == "a1") ? "a2" : "a1");

      if(m_isSearchFlag)
        totalOccurrences += lviCurItem->text(3).toInt();
      else
        totalOccurrences += lviCurItem->text(4).toInt();

      lviCurItem = lviCurItem->nextSibling();
    } while(lviCurItem && lviCurItem != lviFirst);


  // e) Write the end of the file

   oTStream<< " </table>\n"
              "<totaloccurrences>"
           << totalOccurrences
           << "</totaloccurrences>\n"
              "</report>\n";

   report.close();
}

void Report::createStyleSheet()
{
  TQString cssFileName = m_docPath +".css";
  TQFile styleSheet(cssFileName);
  if (!styleSheet.open( IO_WriteOnly ))
    {
      KMessageBox::error(0, i18n("<qt>Cannot open the file <b>%1</b>.</qt>").tqarg(cssFileName));
      return ;
    }

  TQTextStream oTStream( &styleSheet );

  TQString css = "title { display:block;font:40px bold sans-serif; }\n\n"
                "createdby:before { content :\""+i18n("Created by")+": \"; }\n"
                "createdby { display:inline; }\n\n"
                "date:before { content :\"-"+i18n("date")+": \"; }\n"
                "date { display:inline; }\n\n"
                "totaloccurrences:before { content :\""+i18n("Total occurrences")+": \"; }\n"
                "totaloccurrences { display:block;text-align:right; font-weight:bold;margin-top:5px;margin-right:5px;}\n"
                "tablecaption {display:table-caption;font:20px bold sans-serif;}\n\n"
                "hr {display:block;background:black;height:1px;margin:5px 0px 5px;}\n"
                "whiteline {display:block;height:16px;}\n\n"
                "searchfor {\n"
                "           display:table-cell;\n"
                "           border:1px solid black;\n"
                "           padding:0 7px 0; }\n\n";

                if(!m_isSearchFlag)
                  {
                    css += "replacewith {\n"
                           "             display:table-cell;\n"
                           "             border:1px solid black;\n"
                        "             padding:0 7px 0; }\n\n";
                  }

                css += "folder {\n"
                       "        display:table-cell;\n"
                       "        border:1px solid black;\n"
                       "        padding:0 7px 0; }\n\n"
                       "header { display: table-header-group; }\n\n"
                       "name {\n"
                       "      display:table-cell;\n"
                       "      border:1px solid black;\n"
                       "      padding:0 7px 0; }\n\n"
                       "newsize {\n"
                       "         display:table-cell;\n"
                       "         border:1px solid black;\n"
                       "         padding:0 7px 0;\n"
                       "         text-align:right; }\n\n"
                       "oldsize {\n"
                       "         display:table-cell;\n"
                       "         border:1px solid black;\n"
                       "         padding:0 7px 0;\n"
                       "         text-align:right; }\n\n"
                       "ownergroup {\n"
                       "            display:table-cell;\n"
                       "            border:1px solid black;\n"
                       "            padding:0 7px 0; }\n\n"
                       "owneruser {\n"
                       "           display:table-cell;\n"
                       "           border:1px solid black;\n"
                       "           padding:0 7px 0; }\n\n"
                "replacedstrings {\n"
                "                 text-align:right;\n"
                "                 display:table-cell;\n"
                "                 border:1px solid black;\n"
                "                 padding:0 7px 0; }\n\n"
                "*[class~=header] {\n"
                "                  background : lightgray;\n"
                "                  text-align : center; }\n\n"
                "row { display : table-row; }\n\n"
                "table {\n"
                "       display:table;\n"
                "       border-collapse: collapse; }\n\n"
                "*[class~=a1] {\n"
                "              background-color:aliceblue;\n"
                "              font-weight : bold;font-size:15px; }\n\n"
                "*[class~=a2] {\n"
                "              background-color:khaki;\n"
                "              font-weight : bold;\n"
                "              font-size:15px; }\n\n";

  oTStream << css;

  styleSheet.close();
}

void Report::createDocument(const TQString& docPath)
{
  m_docPath = docPath;

  createStyleSheet();
  createReportFile();
}