summaryrefslogtreecommitdiffstats
path: root/tdefilereplace/tdefilereplacelib.cpp
blob: 90c8ef47349e203ba850a9e8e7cc7ff5083db589 (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
/***************************************************************************
                           tdefilereplacelib.cpp  -  File library
                                      -------------------
    begin                : lun mai  3 20:19:52 CEST 1999

    copyright            : (C) 1999 by François Dupoux
                           (C) 2003 Andras Mantia <amantia@kde.org>
                           (C) 2004 Emiliano Gulmini <emi_barbarossa@yahoo.it>
    email                : dupoux@dupoux.com

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <tqstringlist.h>
#include <tqwidget.h>
#include <tqlistview.h>
#include <tqfileinfo.h>

//KDE
#include <kdebug.h>
#include <tdemessagebox.h>
#include <tdelistview.h>
#include <kiconloader.h>

//needed for malloc/free
#include <stdlib.h>

// local
#include "tdefilereplacelib.h"

const double kilo = 1024.0;
const double mega = 1048576.0;//1024^2
const double giga = 1073741824.0;//1024^3
const double tera = 1099511627776.0;//1024^4

/**
 Format a path, from a path and a filename, or another sub-path (avoid double '/' risks)
 Parameters::.....* basePath: fist path (can be "/" if root, or "/usr/bin/" or "/usr/bin" for example)
 .................* fileName: second path (can be "/doc/html/", or "doc/html/" or "doc/html/index.html" for example)
 Return values:...* Full valid path (without double "/")
*/
TQString TDEFileReplaceLib::formatFullPath(const TQString& basePath, const TQString &fileName)
{
  TQString fullPath = basePath;
  TQString fname = fileName;

  if (fname.startsWith("/")) // skip beginning '/'
    fname = fname.remove(0,1);

  if (fullPath.endsWith("/"))
    fullPath.append(fname);
  else
    fullPath.append("/"+fname);

  return fullPath;
}

TQString TDEFileReplaceLib::formatFileSize(double size)
{
  TQString stringSize;

  if(size < kilo)
    {
      const int asInt = (int) size;
      stringSize = i18n("1 byte", "%n bytes", asInt);
    }
  else
  if(size >= kilo && size < mega)
    {
      double d = size / kilo;
      stringSize = i18n("%1 KB").arg(TQString::number(d,'f',2));
    }
  else
  if(size >= mega && size < giga)
    {
      double d = size / mega;
      stringSize = i18n("%1 MB").arg(TQString::number(d,'f',2));
    }
  else
  if(size >= giga)
    {
      double d = size / giga;
      stringSize =  i18n("%1 GB").arg(TQString::number(d,'f',2));
    }
  return stringSize;
}

bool TDEFileReplaceLib::isAnAccessibleFile(const TQString& filePath, const TQString& fileName, RCOptions* info)
{
  TQString bkExt = info->m_backupExtension;
  if(fileName == ".." || fileName == "." || (!bkExt.isEmpty() && fileName.right(bkExt.length()) == bkExt))
    return false;

  TQFileInfo fi;
  if(filePath.isEmpty())
    fi.setFile(fileName);
  else
    fi.setFile(filePath+"/"+fileName);

  if(fi.isDir())
    return true;

   int minSize = info->m_minSize,
       maxSize = info->m_maxSize;
   TQString minDate = info->m_minDate,
           maxDate = info->m_maxDate,
           dateAccess = info->m_dateAccess;

  // Avoid files that not match access date requirements
  TQString last = "unknown";
  if(dateAccess == "Last Writing Access")
    last = fi.lastModified().toString(Qt::ISODate);
  if(dateAccess == "Last Reading Access")
    last = fi.lastRead().toString(Qt::ISODate);

  if(last != "unknown")
    {
      if(minDate != "unknown" && maxDate != "unknown")
        { //If out of range then exit
          if((minDate > last) || (maxDate < last))
            return false;
        }
      else
        {
          if(minDate != "unknown")
            { //If out of range then exit
              if(minDate > last)
                return false;
            }
          else
            {
              if(maxDate != "unknown")
              //If out of range then exit
              if(maxDate < last)
                return false;
            }
        }
    }
  // Avoid files that not match size requirements
  int size = fi.size();
  if(maxSize != FileSizeOption && minSize != FileSizeOption)
    if(size > (maxSize*1024) || size < (minSize*1024))
      return false;

  // Avoid files that not match ownership requirements
  if(info->m_ownerUserIsChecked)
    {
      TQString fileOwnerUser;
      if(info->m_ownerUserType == "Name")
        fileOwnerUser = fi.owner();
      else
        fileOwnerUser = TQString::number(fi.ownerId(),10);

      if(info->m_ownerUserBool == "Equals To")
        {
          if(info->m_ownerUserValue != fileOwnerUser)
            return false;
        }
      else
        {
          if(info->m_ownerUserValue == fileOwnerUser)
            return false;
        }
    }

  if(info->m_ownerGroupIsChecked)
    {
      TQString fileOwnerGroup;
      if(info->m_ownerGroupType == "Name")
        fileOwnerGroup = fi.group();
      else
        fileOwnerGroup = TQString::number(fi.groupId(),10);
      if(info->m_ownerGroupBool == "Equals To")
        {
          if(info->m_ownerGroupValue != fileOwnerGroup)
            return false;
        }
      else
        {
          if(info->m_ownerGroupValue == fileOwnerGroup)
            return false;
        }
    }

  //If we are here then all requirements have been verified
  return true;
}

void TDEFileReplaceLib::setIconForFileEntry(TQListViewItem* item, TQString path)
{
  TQFileInfo fi(path);
  TQString extension = fi.extension(),
          baseName = fi.baseName();

  KeyValueMap extensionMap;

  extensionMap["a"] = "application-octet-stream";
  extensionMap["am"] = "text-x-script";
  extensionMap["bz"] = "application-vnd.tde.overlay.zip";
  extensionMap["bz2"] = "application-vnd.tde.overlay.zip";
  extensionMap["c"] = "text-x-csrc";
  extensionMap["cc"] = "text-x-c++src";
  extensionMap["cpp"] = "text-x-c++src";
  extensionMap["eml"] = "message";
  extensionMap["exe"] = "application-x-mswinurl";
  extensionMap["gz"] = "application-vnd.tde.overlay.zip";
  extensionMap["h"] = "text-x-hsrc";
  extensionMap["htm"] = "text-html";
  extensionMap["html"] = "text-html";
  extensionMap["in"] = "text-x-script";
  extensionMap["java"] = "text-x-java";
  extensionMap["jpg"] = "image-x-generic";
  extensionMap["kfr"] = "text-html";
  extensionMap["kmdr"] = "application-x-designer";
  extensionMap["kwd"] = "x-office-document";
  extensionMap["log"] = "text-x-log";
  extensionMap["moc"] = "text-x-mocsrc";
  extensionMap["mp3"] = "audio-x-generic";
  extensionMap["o"] = "text-x-osrc";
  extensionMap["pdf"] = "application-pdf";
  extensionMap["php"] = "text-x-php";
  extensionMap["py"] = "text-x-python";
  extensionMap["pl"] = "text-x-perl";
  extensionMap["p"] = "text-x-psrc";
  extensionMap["ps"] = "application-postscript";
  extensionMap["png"] = "image-x-generic";
  extensionMap["sa"] = "application-octet-stream";
  extensionMap["sh"] = "text-x-script";
  extensionMap["so"] = "application-octet-stream";
  extensionMap["tar"] = "application-x-tar";
  extensionMap["tex"] = "text-x-tex";
  extensionMap["tgz"] = "application-x-tarz";
  extensionMap["txt"] = "text-plain";
  extensionMap["ui"] = "application-x-designer";
  extensionMap["uml"] = "umbrellofile";
  extensionMap["wav"] = "audio-x-generic";
  extensionMap["xml"] = "text-html";
  extensionMap["xpm"] = "image-x-generic";

  KeyValueMap::Iterator itExtensionMap;

  for(itExtensionMap = extensionMap.begin(); itExtensionMap != extensionMap.end(); ++itExtensionMap)
    {
      if(extension == itExtensionMap.key())
        {
          item->setPixmap(0,SmallIcon(itExtensionMap.data()));
          return;
        }
    }

  KeyValueMap baseNameMap;

  baseNameMap["configure"] = "text-x-script";
  baseNameMap["core"] = "application-x-core";
  baseNameMap["makefile"] = "text-x-makefile";
  baseNameMap["readme"] = "text-x-readme";
  baseNameMap["README"] = "text-x-readme";
  baseNameMap["Readme"] = "text-x-readme";
  baseNameMap["TODO"] = "text-plain";

  KeyValueMap::Iterator itBaseNameMap;

  for(itBaseNameMap = baseNameMap.begin(); itBaseNameMap != baseNameMap.end(); ++itBaseNameMap)
    {
      if(baseName == itBaseNameMap.key())
        {
          item->setPixmap(0,SmallIcon(itBaseNameMap.data()));
          return;
        }
    }
}