File: tmpdir.cpp

package info (click to toggle)
audex 0.78-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,844 kB
  • ctags: 1,461
  • sloc: cpp: 10,604; makefile: 7; sh: 3
file content (113 lines) | stat: -rw-r--r-- 2,963 bytes parent folder | download | duplicates (2)
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
/* AUDEX CDDA EXTRACTOR
 * Copyright (C) 2007-2014 Marco Nelles (audex@maniatek.com)
 * <http://kde.maniatek.com/audex>
 *
 * 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 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tmpdir.h"

TmpDir::TmpDir(const QString& appName, const QString& sub) : QObject() {

  QStringList dirs = KGlobal::dirs()->resourceDirs("tmp");
  p_tmp_path_base = dirs.size()?dirs[0]:"/var/tmp/";
  kDebug() << "Found temporary path" << p_tmp_path_base;

  p_error = FALSE;

  PID pid;
  p_tmp_path = p_tmp_path_base;
  if (p_tmp_path.right(1) != "/") p_tmp_path += "/";
  p_tmp_path += appName+"."+QString("%1").arg(pid.getPID())+"/";
  p_tmp_path_app = p_tmp_path;
  if (!sub.isEmpty()) {
    p_tmp_path += sub+"/";
  }
  kDebug() << "Temporary folder in use:" << p_tmp_path;
  p_error = !p_create_dir(p_tmp_path);
  
}

TmpDir::~TmpDir() {

  //do we have a sub component in the path?
  if (p_tmp_path_app != p_tmp_path) {
    
    if (p_remove_dir(p_tmp_path)) {
      kDebug() << QString("Deleting temporary folder \"%1\"").arg(p_tmp_path);
    } else {
      kDebug() << QString("Deleting temporary folder \"%1\" failed").arg(p_tmp_path);
    }
  
  }
  
  QDir dir(p_tmp_path_app);
  if ((dir.exists()) && (!dir.rmdir(p_tmp_path_app))) {
    kDebug() << QString("Temporary folder \"%1\" not removed yet.").arg(p_tmp_path_app);
  }
  
}

const QString TmpDir::tmpPath() {
 
  p_error = !p_create_dir(p_tmp_path);
  return p_tmp_path;
  
}

quint64 TmpDir::freeSpace() const {
  
  KDiskFreeSpaceInfo diskfreespace = KDiskFreeSpaceInfo::freeSpaceInfo(p_tmp_path);
  return diskfreespace.available();
  
}

bool TmpDir::p_create_dir(const QString &dirName) {
 
  QDir *dir = new QDir(dirName);
  if (!dir->exists()) {
    if (!dir->mkpath(dirName)) {
      return FALSE;
    }
  }
  
  return TRUE;
  
}

bool TmpDir::p_remove_dir(const QString &dirName) {

  bool result = TRUE;
  QDir dir(dirName);
 
  if (dir.exists(dirName)) {

    Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
      if (info.isDir()) {
        result = p_remove_dir(info.absoluteFilePath());
      } else {
        result = QFile::remove(info.absoluteFilePath());
      }
      if (!result) {
        return result;
      }
    }
        
    result = dir.rmdir(dirName);
  
  }
 
  return result;

}